2023-04-14 07:54:07 +00:00
using KubernetesWorkflow ;
2023-04-26 09:12:33 +00:00
using Utils ;
2023-04-14 07:54:07 +00:00
namespace DistTestCore.Marketplace
{
2023-04-18 11:45:48 +00:00
public class GethCompanionNodeStarter : BaseStarter
2023-04-14 07:54:07 +00:00
{
public GethCompanionNodeStarter ( TestLifecycle lifecycle , WorkflowCreator workflowCreator )
2023-04-18 11:45:48 +00:00
: base ( lifecycle , workflowCreator )
2023-04-14 07:54:07 +00:00
{
}
2023-04-26 09:12:33 +00:00
public GethCompanionNodeInfo StartCompanionNodeFor ( CodexSetup codexSetup , MarketplaceNetwork marketplace )
2023-04-14 07:54:07 +00:00
{
2023-04-26 09:12:33 +00:00
LogStart ( $"Initializing companion for {codexSetup.NumberOfNodes} Codex nodes." ) ;
2023-04-14 07:54:07 +00:00
2023-04-26 09:12:33 +00:00
var startupConfig = CreateCompanionNodeStartupConfig ( marketplace . Bootstrap , codexSetup . NumberOfNodes ) ;
2023-04-14 07:54:07 +00:00
var workflow = workflowCreator . CreateWorkflow ( ) ;
2023-04-26 09:12:33 +00:00
var containers = workflow . Start ( 1 , Location . Unspecified , new GethContainerRecipe ( ) , startupConfig ) ;
WaitForAccountCreation ( codexSetup . NumberOfNodes ) ;
if ( containers . Containers . Length ! = 1 ) throw new InvalidOperationException ( "Expected one Geth companion node to be created. Test infra failure." ) ;
var container = containers . Containers [ 0 ] ;
2023-04-14 07:54:07 +00:00
2023-04-26 09:12:33 +00:00
var node = CreateCompanionInfo ( workflow , container , codexSetup . NumberOfNodes ) ;
EnsureCompanionNodeIsSynced ( node , marketplace ) ;
2023-04-14 07:54:07 +00:00
2023-04-26 09:12:33 +00:00
LogEnd ( $"Initialized one companion node for {codexSetup.NumberOfNodes} Codex nodes. Their accounts: [{string.Join(" , ", node.Accounts.Select(a => a.Account))}]" ) ;
return node ;
}
2023-04-19 07:19:06 +00:00
2023-04-26 09:12:33 +00:00
private void WaitForAccountCreation ( int numberOfNodes )
{
// We wait proportional to the number of account the node has to create. It takes a few seconds for each one to generate the keys and create the files
// we will be trying to read in 'ExtractAccount', later on in the start-up process.
Time . Sleep ( TimeSpan . FromSeconds ( 4.5 * numberOfNodes ) ) ;
2023-04-14 07:54:07 +00:00
}
2023-04-26 09:12:33 +00:00
private GethCompanionNodeInfo CreateCompanionInfo ( StartupWorkflow workflow , RunningContainer container , int numberOfAccounts )
2023-04-14 07:54:07 +00:00
{
2023-04-25 11:38:26 +00:00
var extractor = new ContainerInfoExtractor ( lifecycle . Log , workflow , container ) ;
2023-04-26 09:12:33 +00:00
var accounts = ExtractAccounts ( extractor , numberOfAccounts ) . ToArray ( ) ;
return new GethCompanionNodeInfo ( container , accounts ) ;
}
private IEnumerable < GethCompanionAccount > ExtractAccounts ( ContainerInfoExtractor extractor , int numberOfAccounts )
{
for ( int i = 0 ; i < numberOfAccounts ; i + + ) yield return ExtractAccount ( extractor , i + 1 ) ;
}
private GethCompanionAccount ExtractAccount ( ContainerInfoExtractor extractor , int orderNumber )
{
var account = extractor . ExtractAccount ( orderNumber ) ;
var privKey = extractor . ExtractPrivateKey ( orderNumber ) ;
return new GethCompanionAccount ( account , privKey ) ;
2023-04-24 12:09:23 +00:00
}
private void EnsureCompanionNodeIsSynced ( GethCompanionNodeInfo node , MarketplaceNetwork marketplace )
{
2023-04-24 14:07:32 +00:00
try
{
2023-04-26 09:12:33 +00:00
var interaction = node . StartInteraction ( lifecycle . Log , node . Accounts . First ( ) ) ;
2023-04-24 14:07:32 +00:00
interaction . EnsureSynced ( marketplace . Marketplace . Address , marketplace . Marketplace . Abi ) ;
}
catch ( Exception e )
{
throw new Exception ( "Geth companion node did not sync within timeout. Test infra failure." , e ) ;
}
2023-04-14 07:54:07 +00:00
}
2023-04-26 09:12:33 +00:00
private StartupConfig CreateCompanionNodeStartupConfig ( GethBootstrapNodeInfo bootstrapNode , int numberOfAccounts )
2023-04-14 07:54:07 +00:00
{
var config = new StartupConfig ( ) ;
2023-04-26 09:12:33 +00:00
config . Add ( new GethStartupConfig ( false , bootstrapNode , numberOfAccounts ) ) ;
2023-04-14 07:54:07 +00:00
return config ;
}
}
}