114 lines
3.0 KiB
C#
Raw Normal View History

2024-04-01 20:40:03 +02:00
using ArgsUniform;
using AutoClient;
using AutoClient.Modes;
2024-11-26 15:57:34 +01:00
using AutoClient.Modes.FolderStore;
2025-01-16 15:13:16 +01:00
using CodexClient;
using GethPlugin;
using Utils;
2024-04-01 20:40:03 +02:00
2024-09-12 12:05:42 +02:00
public class Program
2024-04-01 20:40:03 +02:00
{
2024-09-12 14:38:15 +02:00
private readonly App app;
private readonly List<IMode> modes = new List<IMode>();
2024-09-12 12:05:42 +02:00
2024-09-12 14:38:15 +02:00
public Program(Configuration config)
2024-09-12 12:05:42 +02:00
{
2024-09-12 14:38:15 +02:00
app = new App(config);
2024-09-12 12:05:42 +02:00
}
public static async Task Main(string[] args)
2024-04-01 20:40:03 +02:00
{
var cts = new CancellationTokenSource();
Console.CancelKeyPress += (sender, args) => cts.Cancel();
var uniformArgs = new ArgsUniform<Configuration>(PrintHelp, args);
var config = uniformArgs.Parse(true);
2024-06-28 08:47:09 +02:00
if (config.NumConcurrentPurchases < 1)
{
throw new Exception("Number of concurrent purchases must be > 0");
}
2024-09-12 14:38:15 +02:00
var p = new Program(config);
await p.Run();
2024-09-12 12:05:42 +02:00
}
2024-04-01 20:40:03 +02:00
2024-09-12 14:38:15 +02:00
public async Task Run()
2024-09-12 12:05:42 +02:00
{
2025-01-16 15:13:16 +01:00
await Task.CompletedTask;
var codexNodes = CreateCodexWrappers();
2024-09-12 14:38:15 +02:00
var i = 0;
2025-01-16 15:13:16 +01:00
foreach (var cdx in codexNodes)
2024-09-12 14:38:15 +02:00
{
var mode = CreateMode();
modes.Add(mode);
mode.Start(cdx, i);
2024-09-12 14:38:15 +02:00
i++;
}
2024-09-12 14:38:15 +02:00
app.Cts.Token.WaitHandle.WaitOne();
2024-04-01 20:40:03 +02:00
foreach (var mode in modes) mode.Stop();
modes.Clear();
2024-09-12 14:38:15 +02:00
app.Log.Log("Done");
2024-09-12 12:05:42 +02:00
}
2024-04-01 20:47:56 +02:00
private IMode CreateMode()
{
2024-10-30 11:09:13 +01:00
if (!string.IsNullOrEmpty(app.Config.FolderToStore))
{
return CreateFolderStoreMode();
}
return new PurchasingMode(app);
}
2024-10-30 11:09:13 +01:00
private IMode CreateFolderStoreMode()
{
if (app.Config.ContractDurationMinutes - 1 < 5) throw new Exception("Contract duration config option not long enough!");
2024-11-27 13:43:38 +01:00
return new FolderStoreMode(app, app.Config.FolderToStore, new PurchaseInfo(
purchaseDurationTotal: TimeSpan.FromMinutes(app.Config.ContractDurationMinutes),
purchaseDurationSafe: TimeSpan.FromMinutes(app.Config.ContractDurationMinutes - 120)
));
2024-10-30 11:09:13 +01:00
}
2025-01-16 15:13:16 +01:00
private CodexWrapper[] CreateCodexWrappers()
2024-09-12 12:05:42 +02:00
{
2024-09-12 14:38:15 +02:00
var endpointStrs = app.Config.CodexEndpoints.Split(";", StringSplitOptions.RemoveEmptyEntries);
2025-01-16 15:13:16 +01:00
var result = new List<CodexWrapper>();
2024-06-28 08:47:09 +02:00
2024-09-12 12:05:42 +02:00
foreach (var e in endpointStrs)
2024-06-28 08:47:09 +02:00
{
2025-01-16 15:13:16 +01:00
result.Add(CreateCodexWrapper(e));
2024-06-28 08:47:09 +02:00
}
2024-04-01 20:40:03 +02:00
2024-09-12 12:05:42 +02:00
return result.ToArray();
2024-04-01 20:40:03 +02:00
}
2025-01-16 15:13:16 +01:00
private CodexWrapper CreateCodexWrapper(string endpoint)
{
2024-09-12 12:05:42 +02:00
var splitIndex = endpoint.LastIndexOf(':');
var host = endpoint.Substring(0, splitIndex);
var port = Convert.ToInt32(endpoint.Substring(splitIndex + 1));
var address = new Address(
logName: $"cdx@{host}:{port}",
2024-09-12 12:05:42 +02:00
host: host,
port: port
);
var instance = CodexInstance.CreateFromApiEndpoint("ac", address, EthAccountGenerator.GenerateNew());
2025-01-16 15:13:16 +01:00
var node = app.CodexNodeFactory.CreateCodexNode(instance);
return new CodexWrapper(app, node);
2024-04-01 20:47:56 +02:00
}
2024-04-01 20:40:03 +02:00
private static void PrintHelp()
{
Console.WriteLine("Generates fake data and creates Codex storage contracts for it.");
}
}