cs-codex-dist-tests/DistTestCore/Marketplace/GethCompanionNodeStarter.cs

78 lines
3.2 KiB
C#
Raw Normal View History

2023-04-14 07:54:07 +00:00
using KubernetesWorkflow;
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
{
}
public GethCompanionNodeInfo StartCompanionNodeFor(CodexSetup codexSetup, MarketplaceNetwork marketplace)
2023-04-14 07:54:07 +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));
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);
EnsureCompanionNodeIsSynced(node, marketplace);
2023-04-14 07:54:07 +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-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);
return new GethCompanionNodeInfo(container, accounts);
}
2023-05-03 08:21:15 +00:00
private static GethAccount[] ExtractAccounts(MarketplaceNetwork marketplace, GethStartupConfig config)
{
2023-05-03 08:21:15 +00:00
return marketplace.Bootstrap.AllAccounts.Accounts
.Skip(1 + config.CompanionAccountStartIndex)
.Take(config.NumberOfCompanionAccounts)
.ToArray();
}
private void EnsureCompanionNodeIsSynced(GethCompanionNodeInfo node, MarketplaceNetwork marketplace)
{
try
{
2023-05-03 08:21:15 +00:00
Time.WaitUntil(() =>
{
var interaction = node.StartInteraction(lifecycle, node.Accounts.First());
2023-05-03 08:21:15 +00:00
return interaction.IsSynced(marketplace.Marketplace.Address, marketplace.Marketplace.Abi);
}, TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(3));
}
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;
}
}
}