cs-codex-dist-tests/DistTestCore/GethStarter.cs

81 lines
3.0 KiB
C#
Raw Normal View History

2023-04-14 07:54:07 +00:00
using DistTestCore.Marketplace;
using KubernetesWorkflow;
namespace DistTestCore
{
public class GethStarter
{
2023-04-14 10:37:05 +00:00
private readonly GethBootstrapNodeCache bootstrapNodeCache;
2023-04-14 08:51:35 +00:00
private readonly GethCompanionNodeStarter companionNodeStarter;
2023-04-14 10:37:05 +00:00
private readonly TestLifecycle lifecycle;
2023-04-14 07:54:07 +00:00
public GethStarter(TestLifecycle lifecycle, WorkflowCreator workflowCreator)
{
2023-04-14 10:37:05 +00:00
bootstrapNodeCache = new GethBootstrapNodeCache(new GethBootstrapNodeStarter(lifecycle, workflowCreator));
2023-04-14 08:51:35 +00:00
companionNodeStarter = new GethCompanionNodeStarter(lifecycle, workflowCreator);
2023-04-14 10:37:05 +00:00
this.lifecycle = lifecycle;
2023-04-14 07:54:07 +00:00
}
2023-04-14 08:51:35 +00:00
public GethStartResult BringOnlineMarketplaceFor(CodexSetup codexSetup)
2023-04-14 07:54:07 +00:00
{
2023-04-14 08:51:35 +00:00
if (codexSetup.MarketplaceConfig == null) return CreateMarketplaceUnavailableResult();
2023-04-14 10:37:05 +00:00
var bootstrapNode = bootstrapNodeCache.Get();
var companionNodes = StartCompanionNodes(codexSetup, bootstrapNode);
2023-04-14 08:51:35 +00:00
2023-04-14 10:37:05 +00:00
TransferInitialBalance(bootstrapNode, codexSetup.MarketplaceConfig.InitialBalance, companionNodes);
return CreateGethStartResult(bootstrapNode, companionNodes);
}
2023-04-14 08:51:35 +00:00
2023-04-14 10:37:05 +00:00
private void TransferInitialBalance(GethBootstrapNodeInfo bootstrapNode, int initialBalance, GethCompanionNodeInfo[] companionNodes)
{
var interaction = bootstrapNode.StartInteraction(lifecycle.Log);
foreach (var node in companionNodes)
{
interaction.TransferTo(node.Account, initialBalance);
}
2023-04-14 07:54:07 +00:00
}
2023-04-14 10:37:05 +00:00
private GethStartResult CreateGethStartResult(GethBootstrapNodeInfo bootstrapNode, GethCompanionNodeInfo[] companionNodes)
2023-04-14 07:54:07 +00:00
{
2023-04-14 10:37:05 +00:00
return new GethStartResult(CreateMarketplaceAccessFactory(bootstrapNode), bootstrapNode, companionNodes);
2023-04-14 07:54:07 +00:00
}
2023-04-14 08:51:35 +00:00
private GethStartResult CreateMarketplaceUnavailableResult()
{
return new GethStartResult(new MarketplaceUnavailableAccessFactory(), null!, Array.Empty<GethCompanionNodeInfo>());
}
2023-04-14 10:37:05 +00:00
private IMarketplaceAccessFactory CreateMarketplaceAccessFactory(GethBootstrapNodeInfo bootstrapNode)
2023-04-14 07:54:07 +00:00
{
2023-04-14 10:37:05 +00:00
return new GethMarketplaceAccessFactory(lifecycle.Log, bootstrapNode!);
2023-04-14 07:54:07 +00:00
}
2023-04-14 10:37:05 +00:00
private GethCompanionNodeInfo[] StartCompanionNodes(CodexSetup codexSetup, GethBootstrapNodeInfo bootstrapNode)
{
return companionNodeStarter.StartCompanionNodesFor(codexSetup, bootstrapNode);
}
}
public class GethBootstrapNodeCache
{
private readonly GethBootstrapNodeStarter bootstrapNodeStarter;
private GethBootstrapNodeInfo? bootstrapNode;
public GethBootstrapNodeCache(GethBootstrapNodeStarter bootstrapNodeStarter)
2023-04-14 08:51:35 +00:00
{
2023-04-14 10:37:05 +00:00
this.bootstrapNodeStarter = bootstrapNodeStarter;
2023-04-14 08:51:35 +00:00
}
2023-04-14 10:37:05 +00:00
public GethBootstrapNodeInfo Get()
2023-04-14 07:54:07 +00:00
{
2023-04-14 10:37:05 +00:00
if (bootstrapNode == null)
{
bootstrapNode = bootstrapNodeStarter.StartGethBootstrapNode();
}
return bootstrapNode;
2023-04-14 07:54:07 +00:00
}
}
}