2024-04-01 18:40:03 +00:00
|
|
|
|
using ArgsUniform;
|
|
|
|
|
using AutoClient;
|
2024-06-27 13:38:13 +00:00
|
|
|
|
using CodexOpenApi;
|
2024-08-23 11:21:31 +00:00
|
|
|
|
using Utils;
|
2024-04-01 18:40:03 +00:00
|
|
|
|
|
2024-09-12 10:05:42 +00:00
|
|
|
|
public class Program
|
2024-04-01 18:40:03 +00:00
|
|
|
|
{
|
2024-09-12 12:38:15 +00:00
|
|
|
|
private readonly App app;
|
2024-09-12 10:05:42 +00:00
|
|
|
|
|
2024-09-12 12:38:15 +00:00
|
|
|
|
public Program(Configuration config)
|
2024-09-12 10:05:42 +00:00
|
|
|
|
{
|
2024-09-12 12:38:15 +00:00
|
|
|
|
app = new App(config);
|
2024-09-12 10:05:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 13:38:13 +00:00
|
|
|
|
public static async Task Main(string[] args)
|
2024-04-01 18:40:03 +00: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 06:47:09 +00:00
|
|
|
|
if (config.NumConcurrentPurchases < 1)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Number of concurrent purchases must be > 0");
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-12 12:38:15 +00:00
|
|
|
|
var p = new Program(config);
|
|
|
|
|
await p.Run();
|
2024-09-12 10:05:42 +00:00
|
|
|
|
}
|
2024-04-01 18:40:03 +00:00
|
|
|
|
|
2024-09-12 12:38:15 +00:00
|
|
|
|
public async Task Run()
|
2024-09-12 10:05:42 +00:00
|
|
|
|
{
|
2024-09-12 12:38:15 +00:00
|
|
|
|
var codexUsers = await CreateUsers();
|
|
|
|
|
|
|
|
|
|
var i = 0;
|
|
|
|
|
foreach (var user in codexUsers)
|
|
|
|
|
{
|
|
|
|
|
user.Start(i);
|
|
|
|
|
i++;
|
|
|
|
|
}
|
2024-06-27 13:38:13 +00:00
|
|
|
|
|
2024-09-12 12:38:15 +00:00
|
|
|
|
app.Cts.Token.WaitHandle.WaitOne();
|
2024-04-01 18:40:03 +00:00
|
|
|
|
|
2024-09-12 12:38:15 +00:00
|
|
|
|
foreach (var user in codexUsers) user.Stop();
|
|
|
|
|
|
|
|
|
|
app.Log.Log("Done");
|
2024-09-12 10:05:42 +00:00
|
|
|
|
}
|
2024-04-01 18:47:56 +00:00
|
|
|
|
|
2024-09-12 10:05:42 +00:00
|
|
|
|
private async Task<CodexUser[]> CreateUsers()
|
|
|
|
|
{
|
2024-09-12 12:38:15 +00:00
|
|
|
|
var endpointStrs = app.Config.CodexEndpoints.Split(";", StringSplitOptions.RemoveEmptyEntries);
|
2024-09-12 10:05:42 +00:00
|
|
|
|
var result = new List<CodexUser>();
|
2024-06-28 06:47:09 +00:00
|
|
|
|
|
2024-09-12 10:05:42 +00:00
|
|
|
|
foreach (var e in endpointStrs)
|
2024-06-28 06:47:09 +00:00
|
|
|
|
{
|
2024-09-12 10:05:42 +00:00
|
|
|
|
result.Add(await CreateUser(e));
|
2024-06-28 06:47:09 +00:00
|
|
|
|
}
|
2024-04-01 18:40:03 +00:00
|
|
|
|
|
2024-09-12 10:05:42 +00:00
|
|
|
|
return result.ToArray();
|
2024-04-01 18:40:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-12 10:05:42 +00:00
|
|
|
|
private async Task<CodexUser> CreateUser(string endpoint)
|
2024-08-23 11:21:31 +00:00
|
|
|
|
{
|
2024-09-12 10:05:42 +00:00
|
|
|
|
var splitIndex = endpoint.LastIndexOf(':');
|
|
|
|
|
var host = endpoint.Substring(0, splitIndex);
|
|
|
|
|
var port = Convert.ToInt32(endpoint.Substring(splitIndex + 1));
|
|
|
|
|
|
|
|
|
|
var address = new Address(
|
|
|
|
|
host: host,
|
|
|
|
|
port: port
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
var client = new HttpClient();
|
|
|
|
|
var codex = new CodexApi(client);
|
|
|
|
|
codex.BaseUrl = $"{address.Host}:{address.Port}/api/codex/v1";
|
|
|
|
|
|
2024-09-12 12:38:15 +00:00
|
|
|
|
app.Log.Log($"Checking Codex at {address}...");
|
2024-09-12 10:05:42 +00:00
|
|
|
|
await CheckCodex(codex);
|
2024-09-12 12:38:15 +00:00
|
|
|
|
app.Log.Log("OK");
|
2024-09-12 10:05:42 +00:00
|
|
|
|
|
2024-09-12 12:38:15 +00:00
|
|
|
|
return new CodexUser(
|
|
|
|
|
app,
|
|
|
|
|
codex,
|
|
|
|
|
client,
|
|
|
|
|
address
|
|
|
|
|
);
|
2024-08-23 11:21:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-12 10:05:42 +00:00
|
|
|
|
private async Task CheckCodex(CodexApi codex)
|
2024-04-01 18:47:56 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-06-27 13:38:13 +00:00
|
|
|
|
var info = await codex.GetDebugInfoAsync();
|
2024-04-01 18:47:56 +00:00
|
|
|
|
if (string.IsNullOrEmpty(info.Id)) throw new Exception("Failed to fetch Codex node id");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-09-12 12:38:15 +00:00
|
|
|
|
app.Log.Error($"Codex not OK: {ex}");
|
2024-04-01 18:47:56 +00:00
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 18:40:03 +00:00
|
|
|
|
private static void PrintHelp()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Generates fake data and creates Codex storage contracts for it.");
|
|
|
|
|
}
|
|
|
|
|
}
|