cs-codex-dist-tests/Tools/CodexNetDeployer/CodexNodeStarter.cs

103 lines
3.6 KiB
C#
Raw Normal View History

2023-09-20 10:02:32 +00:00
using CodexContractsPlugin;
using CodexPlugin;
using Core;
using GethPlugin;
2023-09-08 08:21:40 +00:00
using Utils;
namespace CodexNetDeployer
{
public class CodexNodeStarter
{
private readonly Configuration config;
2023-09-20 10:02:32 +00:00
private readonly CoreInterface ci;
private readonly IGethNode gethNode;
private readonly ICodexContracts contracts;
2023-09-20 10:55:09 +00:00
private ICodexNode? bootstrapNode = null;
private int validatorsLeft;
2023-09-20 10:02:32 +00:00
public CodexNodeStarter(Configuration config, CoreInterface ci, IGethNode gethNode, ICodexContracts contracts, int numberOfValidators)
{
this.config = config;
2023-09-20 10:02:32 +00:00
this.ci = ci;
this.gethNode = gethNode;
this.contracts = contracts;
2023-07-11 08:59:41 +00:00
validatorsLeft = numberOfValidators;
}
public CodexNodeStartResult? Start(int i)
{
2023-09-20 10:02:32 +00:00
var name = GetCodexContainerName(i);
2023-09-21 06:49:09 +00:00
Console.Write($" - {i} ({name})");
Console.CursorLeft = 30;
2023-09-20 10:02:32 +00:00
ICodexNode? codexNode = null;
2023-06-29 08:23:04 +00:00
try
{
2023-09-20 10:02:32 +00:00
codexNode = ci.StartCodexNode(s =>
{
s.WithName(name);
s.WithLogLevel(config.CodexLogLevel);
s.WithStorageQuota(config.StorageQuota!.Value.MB());
s.EnableMarketplace(gethNode, contracts, 100.Eth(), config.InitialTestTokens.TestTokens(), validatorsLeft > 0);
s.EnableMetrics();
2023-09-20 10:55:09 +00:00
if (bootstrapNode != null) s.WithBootstrapNode(bootstrapNode);
2023-09-20 10:02:32 +00:00
if (config.BlockTTL != Configuration.SecondsIn1Day) s.WithBlockTTL(TimeSpan.FromSeconds(config.BlockTTL));
if (config.BlockMI != Configuration.TenMinutes) s.WithBlockMaintenanceInterval(TimeSpan.FromSeconds(config.BlockMI));
if (config.BlockMN != 1000) s.WithBlockMaintenanceNumber(config.BlockMN);
});
var debugInfo = codexNode.GetDebugInfo();
2023-06-29 08:23:04 +00:00
if (!string.IsNullOrWhiteSpace(debugInfo.spr))
{
Console.Write("Online\t");
2023-09-20 10:02:32 +00:00
var response = codexNode.Marketplace.MakeStorageAvailable(
size: config.StorageSell!.Value.MB(),
2023-06-29 08:23:04 +00:00
minPriceForTotalSpace: config.MinPrice.TestTokens(),
maxCollateral: config.MaxCollateral.TestTokens(),
maxDuration: TimeSpan.FromSeconds(config.MaxDuration));
2023-06-29 08:23:04 +00:00
if (!string.IsNullOrEmpty(response))
{
Console.Write("Storage available\tOK" + Environment.NewLine);
2023-06-29 08:23:04 +00:00
validatorsLeft--;
2023-09-20 10:55:09 +00:00
if (bootstrapNode == null) bootstrapNode = codexNode;
2023-09-20 10:02:32 +00:00
return new CodexNodeStartResult(codexNode);
2023-06-29 08:23:04 +00:00
}
}
}
2023-06-29 08:23:04 +00:00
catch (Exception ex)
{
Console.WriteLine("Exception:" + ex.ToString());
}
2023-09-20 10:02:32 +00:00
Console.WriteLine("Unknown failure.");
if (codexNode != null)
{
Console.WriteLine("Downloading container log.");
ci.DownloadLog(codexNode);
}
return null;
}
private string GetCodexContainerName(int i)
{
if (i == 0) return "BOOTSTRAP";
return "CODEX" + i;
}
}
public class CodexNodeStartResult
{
2023-09-20 10:02:32 +00:00
public CodexNodeStartResult(ICodexNode codexNode)
{
2023-09-20 10:02:32 +00:00
CodexNode = codexNode;
}
2023-09-20 10:02:32 +00:00
public ICodexNode CodexNode { get; }
}
}