2023-04-25 09:31:15 +00:00
using KubernetesWorkflow ;
2023-04-25 11:38:26 +00:00
using Logging ;
2023-04-18 08:22:11 +00:00
using Newtonsoft.Json ;
2023-04-24 12:09:23 +00:00
using Newtonsoft.Json.Linq ;
using Utils ;
2023-04-14 07:54:07 +00:00
namespace DistTestCore.Marketplace
{
2023-04-18 08:22:11 +00:00
public class ContainerInfoExtractor
2023-04-14 07:54:07 +00:00
{
2023-04-25 11:38:26 +00:00
private readonly BaseLog log ;
2023-04-14 07:54:07 +00:00
private readonly StartupWorkflow workflow ;
private readonly RunningContainer container ;
2023-04-25 11:38:26 +00:00
public ContainerInfoExtractor ( BaseLog log , StartupWorkflow workflow , RunningContainer container )
2023-04-14 07:54:07 +00:00
{
2023-04-25 11:38:26 +00:00
this . log = log ;
2023-04-14 07:54:07 +00:00
this . workflow = workflow ;
this . container = container ;
}
2023-05-03 08:21:15 +00:00
public AllGethAccounts ExtractAccounts ( )
2023-04-14 07:54:07 +00:00
{
2023-04-25 11:38:26 +00:00
log . Debug ( ) ;
2023-05-03 08:21:15 +00:00
var accountsCsv = Retry ( ( ) = > FetchAccountsCsv ( ) ) ;
if ( string . IsNullOrEmpty ( accountsCsv ) ) throw new InvalidOperationException ( "Unable to fetch accounts.csv for geth node. Test infra failure." ) ;
2023-04-14 07:54:07 +00:00
2023-05-03 08:21:15 +00:00
var lines = accountsCsv . Split ( '\n' ) ;
return new AllGethAccounts ( lines . Select ( ParseLineToAccount ) . ToArray ( ) ) ;
2023-04-14 07:54:07 +00:00
}
2023-04-17 08:31:14 +00:00
public string ExtractPubKey ( )
{
2023-04-25 11:38:26 +00:00
log . Debug ( ) ;
2023-04-17 08:31:14 +00:00
var pubKey = Retry ( FetchPubKey ) ;
if ( string . IsNullOrEmpty ( pubKey ) ) throw new InvalidOperationException ( "Unable to fetch enode from geth node. Test infra failure." ) ;
2023-04-14 07:54:07 +00:00
2023-04-17 08:31:14 +00:00
return pubKey ;
2023-04-14 07:54:07 +00:00
}
2023-04-18 08:22:11 +00:00
public string ExtractMarketplaceAddress ( )
{
2023-04-25 11:38:26 +00:00
log . Debug ( ) ;
2023-04-18 08:22:11 +00:00
var marketplaceAddress = Retry ( FetchMarketplaceAddress ) ;
if ( string . IsNullOrEmpty ( marketplaceAddress ) ) throw new InvalidOperationException ( "Unable to fetch marketplace account from codex-contracts node. Test infra failure." ) ;
return marketplaceAddress ;
}
2023-04-24 12:09:23 +00:00
public string ExtractMarketplaceAbi ( )
{
2023-04-25 11:38:26 +00:00
log . Debug ( ) ;
2023-04-24 12:09:23 +00:00
var marketplaceAbi = Retry ( FetchMarketplaceAbi ) ;
if ( string . IsNullOrEmpty ( marketplaceAbi ) ) throw new InvalidOperationException ( "Unable to fetch marketplace artifacts from codex-contracts node. Test infra failure." ) ;
return marketplaceAbi ;
}
2023-05-03 08:21:15 +00:00
private string FetchAccountsCsv ( )
2023-04-14 07:54:07 +00:00
{
2023-05-03 08:21:15 +00:00
return workflow . ExecuteCommand ( container , "cat" , GethContainerRecipe . AccountsFilename ) ;
2023-04-18 11:22:41 +00:00
}
2023-04-18 08:22:11 +00:00
private string FetchMarketplaceAddress ( )
{
var json = workflow . ExecuteCommand ( container , "cat" , CodexContractsContainerRecipe . MarketplaceAddressFilename ) ;
var marketplace = JsonConvert . DeserializeObject < MarketplaceJson > ( json ) ;
return marketplace ! . address ;
}
2023-04-24 12:09:23 +00:00
private string FetchMarketplaceAbi ( )
{
var json = workflow . ExecuteCommand ( container , "cat" , CodexContractsContainerRecipe . MarketplaceArtifactFilename ) ;
var artifact = JObject . Parse ( json ) ;
var abi = artifact [ "abi" ] ;
return abi ! . ToString ( Formatting . None ) ;
}
2023-04-17 08:31:14 +00:00
private string FetchPubKey ( )
{
2023-06-01 13:20:21 +00:00
var enodeFinder = new PubKeyFinder ( s = > log . Debug ( s ) ) ;
2023-04-17 08:31:14 +00:00
workflow . DownloadContainerLog ( container , enodeFinder ) ;
return enodeFinder . GetPubKey ( ) ;
}
2023-05-03 08:21:15 +00:00
private GethAccount ParseLineToAccount ( string l )
{
var tokens = l . Replace ( "\r" , "" ) . Split ( ',' ) ;
if ( tokens . Length ! = 2 ) throw new InvalidOperationException ( ) ;
var account = tokens [ 0 ] ;
var privateKey = tokens [ 1 ] ;
return new GethAccount ( account , privateKey ) ;
}
2023-05-10 07:55:36 +00:00
private static string Retry ( Func < string > fetch )
{
2023-05-31 11:15:41 +00:00
return Time . Retry ( fetch , nameof ( ContainerInfoExtractor ) ) ;
2023-05-10 07:55:36 +00:00
}
2023-04-17 08:31:14 +00:00
}
public class PubKeyFinder : LogHandler , ILogHandler
{
2023-04-21 07:11:45 +00:00
private const string openTag = "self=enode://" ;
private const string openTagQuote = "self=\"enode://" ;
2023-06-01 13:20:21 +00:00
private readonly Action < string > debug ;
2023-04-17 08:31:14 +00:00
private string pubKey = string . Empty ;
2023-06-01 13:20:21 +00:00
public PubKeyFinder ( Action < string > debug )
{
this . debug = debug ;
debug ( $"Looking for '{openTag}' in container logs..." ) ;
}
2023-04-17 08:31:14 +00:00
public string GetPubKey ( )
{
2023-06-01 12:03:16 +00:00
if ( string . IsNullOrEmpty ( pubKey ) ) throw new Exception ( "Not found yet exception." ) ;
2023-04-17 08:31:14 +00:00
return pubKey ;
}
protected override void ProcessLine ( string line )
{
2023-06-01 13:20:21 +00:00
debug ( line ) ;
2023-04-17 08:31:14 +00:00
if ( line . Contains ( openTag ) )
{
2023-04-21 07:11:45 +00:00
ExtractPubKey ( openTag , line ) ;
}
else if ( line . Contains ( openTagQuote ) )
{
ExtractPubKey ( openTagQuote , line ) ;
2023-04-17 08:31:14 +00:00
}
}
2023-04-21 07:11:45 +00:00
private void ExtractPubKey ( string tag , string line )
2023-04-17 08:31:14 +00:00
{
2023-04-21 07:11:45 +00:00
var openIndex = line . IndexOf ( tag ) + tag . Length ;
2023-04-17 08:31:14 +00:00
var closeIndex = line . IndexOf ( "@" ) ;
pubKey = line . Substring (
startIndex : openIndex ,
length : closeIndex - openIndex ) ;
}
2023-04-14 07:54:07 +00:00
}
2023-04-18 08:22:11 +00:00
public class MarketplaceJson
{
public string address { get ; set ; } = string . Empty ;
}
2023-04-14 07:54:07 +00:00
}