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;
|
2023-06-23 06:44:27 +00:00
|
|
|
|
|
|
|
|
|
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;
|
2023-06-23 06:44:27 +00:00
|
|
|
|
private int validatorsLeft;
|
|
|
|
|
|
2023-09-20 10:02:32 +00:00
|
|
|
|
public CodexNodeStarter(Configuration config, CoreInterface ci, IGethNode gethNode, ICodexContracts contracts, int numberOfValidators)
|
2023-06-23 06:44:27 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
2023-06-23 06:44:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-24 09:32:32 +00:00
|
|
|
|
public CodexNodeStartResult? Start(int i)
|
2023-06-23 06:44:27 +00:00
|
|
|
|
{
|
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-06-28 06:48:46 +00:00
|
|
|
|
|
2023-09-20 10:02:32 +00:00
|
|
|
|
ICodexNode? codexNode = null;
|
2023-06-29 08:23:04 +00:00
|
|
|
|
try
|
2023-06-23 06:44:27 +00:00
|
|
|
|
{
|
2023-09-20 10:02:32 +00:00
|
|
|
|
codexNode = ci.StartCodexNode(s =>
|
|
|
|
|
{
|
|
|
|
|
s.WithName(name);
|
2023-10-08 05:29:55 +00:00
|
|
|
|
s.WithLogLevel(config.CodexLogLevel, new CodexLogCustomTopics(config.Discv5LogLevel, config.Libp2pLogLevel));
|
2023-09-20 10:02:32 +00:00
|
|
|
|
s.WithStorageQuota(config.StorageQuota!.Value.MB());
|
2023-10-23 10:52:47 +00:00
|
|
|
|
|
|
|
|
|
if (config.ShouldMakeStorageAvailable)
|
|
|
|
|
{
|
|
|
|
|
s.EnableMarketplace(gethNode, contracts, 100.Eth(), config.InitialTestTokens.TestTokens(), validatorsLeft > 0);
|
|
|
|
|
}
|
2023-09-20 10:02:32 +00:00
|
|
|
|
|
2023-09-20 10:55:09 +00:00
|
|
|
|
if (bootstrapNode != null) s.WithBootstrapNode(bootstrapNode);
|
2023-10-23 08:57:59 +00:00
|
|
|
|
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();
|
2023-06-29 08:23:04 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(debugInfo.spr))
|
|
|
|
|
{
|
|
|
|
|
Console.Write("Online\t");
|
2023-06-23 06:44:27 +00:00
|
|
|
|
|
2023-10-23 10:52:47 +00:00
|
|
|
|
if (config.ShouldMakeStorageAvailable)
|
2023-06-29 08:23:04 +00:00
|
|
|
|
{
|
2023-10-23 10:52:47 +00:00
|
|
|
|
var response = codexNode.Marketplace.MakeStorageAvailable(
|
|
|
|
|
size: config.StorageSell!.Value.MB(),
|
|
|
|
|
minPriceForTotalSpace: config.MinPrice.TestTokens(),
|
|
|
|
|
maxCollateral: config.MaxCollateral.TestTokens(),
|
|
|
|
|
maxDuration: TimeSpan.FromSeconds(config.MaxDuration));
|
2023-06-28 06:48:46 +00:00
|
|
|
|
|
2023-10-23 10:52:47 +00:00
|
|
|
|
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
|
|
|
|
}
|
2023-10-23 10:52:47 +00:00
|
|
|
|
|
|
|
|
|
Console.Write("OK" + Environment.NewLine);
|
|
|
|
|
|
|
|
|
|
validatorsLeft--;
|
|
|
|
|
if (bootstrapNode == null) bootstrapNode = codexNode;
|
|
|
|
|
return new CodexNodeStartResult(codexNode);
|
2023-06-28 06:48:46 +00:00
|
|
|
|
}
|
2023-06-23 06:44:27 +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);
|
|
|
|
|
}
|
2023-06-28 06:48:46 +00:00
|
|
|
|
|
|
|
|
|
return null;
|
2023-06-23 06:44:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
PublicNatIP = config.PublicIP,
|
|
|
|
|
PublicDiscoveryPort = Convert.ToInt32(discPort),
|
|
|
|
|
PublicListenPort = Convert.ToInt32(listenPort)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 06:53:09 +00:00
|
|
|
|
private string GetCodexContainerName(int i)
|
|
|
|
|
{
|
|
|
|
|
if (i == 0) return "BOOTSTRAP";
|
|
|
|
|
return "CODEX" + i;
|
|
|
|
|
}
|
2023-06-23 06:44:27 +00:00
|
|
|
|
}
|
2023-08-24 09:32:32 +00:00
|
|
|
|
|
|
|
|
|
public class CodexNodeStartResult
|
|
|
|
|
{
|
2023-09-20 10:02:32 +00:00
|
|
|
|
public CodexNodeStartResult(ICodexNode codexNode)
|
2023-08-24 09:32:32 +00:00
|
|
|
|
{
|
2023-09-20 10:02:32 +00:00
|
|
|
|
CodexNode = codexNode;
|
2023-08-24 09:32:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 10:02:32 +00:00
|
|
|
|
public ICodexNode CodexNode { get; }
|
2023-08-24 09:32:32 +00:00
|
|
|
|
}
|
2023-06-23 06:44:27 +00:00
|
|
|
|
}
|