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
{
2023-05-03 08:21:15 +00:00
private int companionAccountIndex = 0 ;
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-05-03 08:21:15 +00:00
var config = CreateCompanionNodeStartupConfig ( marketplace . Bootstrap , codexSetup . NumberOfNodes ) ;
2023-04-14 07:54:07 +00:00
var workflow = workflowCreator . CreateWorkflow ( ) ;
2023-05-03 08:21:15 +00:00
var containers = workflow . Start ( 1 , Location . Unspecified , new GethContainerRecipe ( ) , CreateStartupConfig ( config ) ) ;
2023-04-26 09:12:33 +00:00
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-05-03 08:21:15 +00:00
var node = CreateCompanionInfo ( container , marketplace , config ) ;
2023-04-26 09:12:33 +00:00
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-05-03 08:21:15 +00:00
private GethCompanionNodeInfo CreateCompanionInfo ( RunningContainer container , MarketplaceNetwork marketplace , GethStartupConfig config )
2023-04-14 07:54:07 +00:00
{
2023-05-03 08:21:15 +00:00
var accounts = ExtractAccounts ( marketplace , config ) ;
2023-04-26 09:12:33 +00:00
return new GethCompanionNodeInfo ( container , accounts ) ;
}
2023-05-03 08:21:15 +00:00
private static GethAccount [ ] ExtractAccounts ( MarketplaceNetwork marketplace , GethStartupConfig config )
2023-04-26 09:12:33 +00:00
{
2023-05-03 08:21:15 +00:00
return marketplace . Bootstrap . AllAccounts . Accounts
. Skip ( 1 + config . CompanionAccountStartIndex )
. Take ( config . NumberOfCompanionAccounts )
. ToArray ( ) ;
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-05-03 08:21:15 +00:00
Time . WaitUntil ( ( ) = >
{
var interaction = node . StartInteraction ( lifecycle . Log , node . Accounts . First ( ) ) ;
return interaction . IsSynced ( marketplace . Marketplace . Address , marketplace . Marketplace . Abi ) ;
} , TimeSpan . FromMinutes ( 1 ) , TimeSpan . FromSeconds ( 3 ) ) ;
2023-04-24 14:07:32 +00:00
}
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-05-03 08:21:15 +00:00
private GethStartupConfig CreateCompanionNodeStartupConfig ( GethBootstrapNodeInfo bootstrapNode , int numberOfAccounts )
{
var config = new GethStartupConfig ( false , bootstrapNode , companionAccountIndex , numberOfAccounts ) ;
companionAccountIndex + = numberOfAccounts ;
return config ;
}
private StartupConfig CreateStartupConfig ( GethStartupConfig gethConfig )
2023-04-14 07:54:07 +00:00
{
var config = new StartupConfig ( ) ;
2023-05-03 08:21:15 +00:00
config . Add ( gethConfig ) ;
2023-04-14 07:54:07 +00:00
return config ;
}
}
}