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

93 lines
4.0 KiB
C#
Raw Normal View History

2023-04-14 07:54:07 +00:00
using DistTestCore.Marketplace;
using KubernetesWorkflow;
namespace DistTestCore
{
2023-04-18 11:45:48 +00:00
public class GethStarter : BaseStarter
2023-04-14 07:54:07 +00:00
{
private readonly MarketplaceNetworkCache marketplaceNetworkCache;
2023-04-14 08:51:35 +00:00
private readonly GethCompanionNodeStarter companionNodeStarter;
2023-04-14 07:54:07 +00:00
public GethStarter(TestLifecycle lifecycle, WorkflowCreator workflowCreator)
2023-04-18 11:45:48 +00:00
: base(lifecycle, workflowCreator)
2023-04-14 07:54:07 +00:00
{
marketplaceNetworkCache = new MarketplaceNetworkCache(
new GethBootstrapNodeStarter(lifecycle, workflowCreator),
new CodexContractsStarter(lifecycle, workflowCreator));
2023-04-14 08:51:35 +00:00
companionNodeStarter = new GethCompanionNodeStarter(lifecycle, workflowCreator);
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();
var marketplaceNetwork = marketplaceNetworkCache.Get();
var companionNodes = StartCompanionNodes(codexSetup, marketplaceNetwork);
2023-04-14 08:51:35 +00:00
LogStart("Setting up initial balance...");
TransferInitialBalance(marketplaceNetwork, codexSetup.MarketplaceConfig, companionNodes);
LogEnd($"Initial balance of {codexSetup.MarketplaceConfig.InitialTestTokens.Amount} TestTokens set for {codexSetup.NumberOfNodes} nodes.");
2023-04-14 10:37:05 +00:00
return CreateGethStartResult(marketplaceNetwork, companionNodes);
2023-04-14 10:37:05 +00:00
}
2023-04-14 08:51:35 +00:00
private void TransferInitialBalance(MarketplaceNetwork marketplaceNetwork, MarketplaceInitialConfig marketplaceConfig, GethCompanionNodeInfo[] companionNodes)
2023-04-14 10:37:05 +00:00
{
var interaction = marketplaceNetwork.StartInteraction(lifecycle.Log);
var tokenAddress = marketplaceNetwork.Marketplace.TokenAddress;
2023-04-18 11:45:48 +00:00
2023-04-14 10:37:05 +00:00
foreach (var node in companionNodes)
{
interaction.TransferWeiTo(node.Account, marketplaceConfig.InitialEth.Wei);
2023-04-18 11:22:41 +00:00
interaction.MintTestTokens(node.Account, marketplaceConfig.InitialTestTokens.Amount, tokenAddress);
2023-04-14 10:37:05 +00:00
}
interaction.WaitForAllTransactions();
2023-04-14 07:54:07 +00:00
}
private GethStartResult CreateGethStartResult(MarketplaceNetwork marketplaceNetwork, GethCompanionNodeInfo[] companionNodes)
2023-04-14 07:54:07 +00:00
{
return new GethStartResult(CreateMarketplaceAccessFactory(marketplaceNetwork), marketplaceNetwork, 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>());
}
private IMarketplaceAccessFactory CreateMarketplaceAccessFactory(MarketplaceNetwork marketplaceNetwork)
2023-04-14 07:54:07 +00:00
{
return new GethMarketplaceAccessFactory(lifecycle.Log, marketplaceNetwork);
2023-04-14 07:54:07 +00:00
}
private GethCompanionNodeInfo[] StartCompanionNodes(CodexSetup codexSetup, MarketplaceNetwork marketplaceNetwork)
2023-04-14 10:37:05 +00:00
{
return companionNodeStarter.StartCompanionNodesFor(codexSetup, marketplaceNetwork.Bootstrap);
2023-04-14 10:37:05 +00:00
}
}
public class MarketplaceNetworkCache
2023-04-14 10:37:05 +00:00
{
private readonly GethBootstrapNodeStarter bootstrapNodeStarter;
private readonly CodexContractsStarter codexContractsStarter;
private MarketplaceNetwork? network;
2023-04-14 10:37:05 +00:00
public MarketplaceNetworkCache(GethBootstrapNodeStarter bootstrapNodeStarter, CodexContractsStarter codexContractsStarter)
2023-04-14 08:51:35 +00:00
{
2023-04-14 10:37:05 +00:00
this.bootstrapNodeStarter = bootstrapNodeStarter;
this.codexContractsStarter = codexContractsStarter;
2023-04-14 08:51:35 +00:00
}
public MarketplaceNetwork Get()
2023-04-14 07:54:07 +00:00
{
if (network == null)
2023-04-14 10:37:05 +00:00
{
var bootstrapInfo = bootstrapNodeStarter.StartGethBootstrapNode();
var marketplaceInfo = codexContractsStarter.Start(bootstrapInfo);
network = new MarketplaceNetwork(bootstrapInfo, marketplaceInfo );
2023-04-14 10:37:05 +00:00
}
return network;
2023-04-14 07:54:07 +00:00
}
}
}