2023-04-14 09:54:07 +02:00
using KubernetesWorkflow ;
2023-04-26 11:12:33 +02:00
using Utils ;
2023-04-14 09:54:07 +02:00
namespace DistTestCore.Marketplace
{
2023-04-18 13:45:48 +02:00
public class GethCompanionNodeStarter : BaseStarter
2023-04-14 09:54:07 +02:00
{
2023-05-03 10:21:15 +02:00
private int companionAccountIndex = 0 ;
2023-08-10 10:58:18 +02:00
public GethCompanionNodeStarter ( TestLifecycle lifecycle )
: base ( lifecycle )
2023-04-14 09:54:07 +02:00
{
}
2023-04-26 11:12:33 +02:00
public GethCompanionNodeInfo StartCompanionNodeFor ( CodexSetup codexSetup , MarketplaceNetwork marketplace )
2023-04-14 09:54:07 +02:00
{
2023-04-26 11:12:33 +02:00
LogStart ( $"Initializing companion for {codexSetup.NumberOfNodes} Codex nodes." ) ;
2023-04-14 09:54:07 +02:00
2023-05-03 10:21:15 +02:00
var config = CreateCompanionNodeStartupConfig ( marketplace . Bootstrap , codexSetup . NumberOfNodes ) ;
2023-04-14 09:54:07 +02:00
2023-08-10 10:58:18 +02:00
var workflow = lifecycle . WorkflowCreator . CreateWorkflow ( ) ;
2023-05-03 10:21:15 +02:00
var containers = workflow . Start ( 1 , Location . Unspecified , new GethContainerRecipe ( ) , CreateStartupConfig ( config ) ) ;
2023-04-26 11:12:33 +02: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 09:54:07 +02:00
2023-05-03 10:21:15 +02:00
var node = CreateCompanionInfo ( container , marketplace , config ) ;
2023-04-26 11:12:33 +02:00
EnsureCompanionNodeIsSynced ( node , marketplace ) ;
2023-04-14 09:54:07 +02:00
2023-04-26 11:12:33 +02: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 09:19:06 +02:00
2023-05-03 10:21:15 +02:00
private GethCompanionNodeInfo CreateCompanionInfo ( RunningContainer container , MarketplaceNetwork marketplace , GethStartupConfig config )
2023-04-14 09:54:07 +02:00
{
2023-05-03 10:21:15 +02:00
var accounts = ExtractAccounts ( marketplace , config ) ;
2023-04-26 11:12:33 +02:00
return new GethCompanionNodeInfo ( container , accounts ) ;
}
2023-05-03 10:21:15 +02:00
private static GethAccount [ ] ExtractAccounts ( MarketplaceNetwork marketplace , GethStartupConfig config )
2023-04-26 11:12:33 +02:00
{
2023-05-03 10:21:15 +02:00
return marketplace . Bootstrap . AllAccounts . Accounts
. Skip ( 1 + config . CompanionAccountStartIndex )
. Take ( config . NumberOfCompanionAccounts )
. ToArray ( ) ;
2023-04-24 14:09:23 +02:00
}
private void EnsureCompanionNodeIsSynced ( GethCompanionNodeInfo node , MarketplaceNetwork marketplace )
{
2023-04-24 16:07:32 +02:00
try
{
2023-05-03 10:21:15 +02:00
Time . WaitUntil ( ( ) = >
{
2023-06-01 15:56:26 +02:00
var interaction = node . StartInteraction ( lifecycle , node . Accounts . First ( ) ) ;
2023-05-03 10:21:15 +02:00
return interaction . IsSynced ( marketplace . Marketplace . Address , marketplace . Marketplace . Abi ) ;
} , TimeSpan . FromMinutes ( 1 ) , TimeSpan . FromSeconds ( 3 ) ) ;
2023-04-24 16:07:32 +02:00
}
catch ( Exception e )
{
throw new Exception ( "Geth companion node did not sync within timeout. Test infra failure." , e ) ;
}
2023-04-14 09:54:07 +02:00
}
2023-05-03 10:21:15 +02: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 09:54:07 +02:00
{
var config = new StartupConfig ( ) ;
2023-05-03 10:21:15 +02:00
config . Add ( gethConfig ) ;
2023-04-14 09:54:07 +02:00
return config ;
}
}
}