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

138 lines
5.0 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-11-07 11:42:01 +00:00
Console.Write($" - {i} ({name})\t");
2023-09-21 06:49:09 +00:00
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, new CodexLogCustomTopics(config.Discv5LogLevel, config.Libp2pLogLevel));
2023-09-20 10:02:32 +00:00
s.WithStorageQuota(config.StorageQuota!.Value.MB());
if (config.ShouldMakeStorageAvailable)
{
s.EnableMarketplace(gethNode, contracts, m =>
2024-03-13 09:29:26 +00:00
{
m.WithInitial(100.Eth(), config.InitialTestTokens.TstWei());
if (validatorsLeft > 0) m.AsValidator();
if (config.ShouldMakeStorageAvailable) m.AsStorageNode();
2024-03-13 09:29:26 +00:00
});
}
2023-09-20 10:02:32 +00:00
2023-09-20 10:55:09 +00:00
if (bootstrapNode != null) s.WithBootstrapNode(bootstrapNode);
if (config.MetricsEndpoints) s.EnableMetrics();
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);
2023-10-23 11:08:49 +00:00
if (config.IsPublicTestNet)
{
s.AsPublicTestNet(CreatePublicTestNetConfig(i));
}
2023-09-20 10:02:32 +00:00
});
var debugInfo = codexNode.GetDebugInfo();
2024-03-26 07:58:16 +00:00
if (!string.IsNullOrWhiteSpace(debugInfo.Spr))
2023-06-29 08:23:04 +00:00
{
Console.Write("Online\t");
if (config.ShouldMakeStorageAvailable)
2023-06-29 08:23:04 +00:00
{
2024-03-20 10:11:41 +00:00
var availability = new StorageAvailability(
totalSpace: config.StorageSell!.Value.MB(),
maxDuration: TimeSpan.FromSeconds(config.MaxDuration),
minPriceForTotalSpace: config.MinPrice.TstWei(),
maxCollateral: config.MaxCollateral.TstWei()
2024-03-20 10:11:41 +00:00
);
var response = codexNode.Marketplace.MakeStorageAvailable(availability);
if (!string.IsNullOrEmpty(response))
{
Console.Write("Storage available\t");
}
else throw new Exception("Failed to make storage available.");
2023-06-29 08:23:04 +00:00
}
Console.Write("OK" + Environment.NewLine);
validatorsLeft--;
if (bootstrapNode == null) bootstrapNode = codexNode;
return new CodexNodeStartResult(codexNode);
}
}
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;
}
2023-10-23 11:08:49 +00:00
private CodexTestNetConfig CreatePublicTestNetConfig(int i)
{
var discPort = config.PublicDiscPorts.Split(",")[i];
var listenPort = config.PublicListenPorts.Split(",")[i];
return new CodexTestNetConfig
{
PublicDiscoveryPort = Convert.ToInt32(discPort),
PublicListenPort = Convert.ToInt32(listenPort)
};
}
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; }
}
}