2024-01-22 09:27:07 +00:00
|
|
|
|
using ArgsUniform;
|
|
|
|
|
using Logging;
|
|
|
|
|
using Utils;
|
|
|
|
|
|
|
|
|
|
namespace TestNetRewarder
|
|
|
|
|
{
|
|
|
|
|
public class Program
|
|
|
|
|
{
|
2024-06-17 13:34:08 +00:00
|
|
|
|
public static CancellationToken CancellationToken;
|
|
|
|
|
private static Configuration Config = null!;
|
|
|
|
|
private static ILog Log = null!;
|
|
|
|
|
private static BotClient BotClient = null!;
|
2024-01-27 14:12:20 +00:00
|
|
|
|
private static Processor processor = null!;
|
2024-02-19 14:59:49 +00:00
|
|
|
|
private static DateTime lastCheck = DateTime.MinValue;
|
2024-01-22 09:27:07 +00:00
|
|
|
|
|
|
|
|
|
public static Task Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
var cts = new CancellationTokenSource();
|
|
|
|
|
CancellationToken = cts.Token;
|
|
|
|
|
Console.CancelKeyPress += (sender, args) => cts.Cancel();
|
|
|
|
|
|
|
|
|
|
var uniformArgs = new ArgsUniform<Configuration>(PrintHelp, args);
|
|
|
|
|
Config = uniformArgs.Parse(true);
|
|
|
|
|
|
|
|
|
|
Log = new LogSplitter(
|
|
|
|
|
new FileLog(Path.Combine(Config.LogPath, "testnetrewarder")),
|
|
|
|
|
new ConsoleLog()
|
|
|
|
|
);
|
|
|
|
|
|
2024-06-14 09:05:29 +00:00
|
|
|
|
var connector = GethConnector.GethConnector.Initialize(Log);
|
|
|
|
|
if (connector == null) throw new Exception("Invalid Geth information");
|
|
|
|
|
|
2024-01-26 23:17:56 +00:00
|
|
|
|
BotClient = new BotClient(Config, Log);
|
2024-06-17 13:34:08 +00:00
|
|
|
|
processor = new Processor(Config, BotClient, connector.CodexContracts, Log);
|
2024-01-26 23:17:56 +00:00
|
|
|
|
|
2024-01-22 09:27:07 +00:00
|
|
|
|
EnsurePath(Config.DataPath);
|
|
|
|
|
EnsurePath(Config.LogPath);
|
|
|
|
|
|
|
|
|
|
return new Program().MainAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task MainAsync()
|
|
|
|
|
{
|
2024-01-27 14:12:20 +00:00
|
|
|
|
EnsureGethOnline();
|
|
|
|
|
|
2024-01-22 09:27:07 +00:00
|
|
|
|
Log.Log("Starting TestNet Rewarder...");
|
2024-06-14 09:05:29 +00:00
|
|
|
|
var segmenter = new TimeSegmenter(Log, Config, processor);
|
2024-01-22 09:27:07 +00:00
|
|
|
|
|
|
|
|
|
while (!CancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
2024-01-26 23:17:56 +00:00
|
|
|
|
await EnsureBotOnline();
|
2024-06-14 09:05:29 +00:00
|
|
|
|
await segmenter.ProcessNextSegment();
|
2024-02-19 14:41:48 +00:00
|
|
|
|
await Task.Delay(100, CancellationToken);
|
2024-01-22 09:27:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-27 14:12:20 +00:00
|
|
|
|
private static void EnsureGethOnline()
|
|
|
|
|
{
|
|
|
|
|
Log.Log("Checking Geth...");
|
|
|
|
|
var gc = GethConnector.GethConnector.Initialize(Log);
|
|
|
|
|
if (gc == null) throw new Exception("Geth input incorrect");
|
|
|
|
|
|
|
|
|
|
var blockNumber = gc.GethNode.GetSyncedBlockNumber();
|
|
|
|
|
if (blockNumber == null || blockNumber < 1) throw new Exception("Geth connection failed.");
|
2024-02-19 13:56:49 +00:00
|
|
|
|
Log.Log("Geth OK. Block number: " + blockNumber);
|
2024-01-27 14:12:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task EnsureBotOnline()
|
2024-01-22 09:27:07 +00:00
|
|
|
|
{
|
2024-01-26 23:17:56 +00:00
|
|
|
|
var start = DateTime.UtcNow;
|
2024-02-19 14:59:49 +00:00
|
|
|
|
var timeSince = start - lastCheck;
|
|
|
|
|
if (timeSince.TotalSeconds < 30.0) return;
|
|
|
|
|
|
2024-01-26 23:17:56 +00:00
|
|
|
|
while (! await BotClient.IsOnline() && !CancellationToken.IsCancellationRequested)
|
2024-01-22 09:27:07 +00:00
|
|
|
|
{
|
2024-01-26 23:17:56 +00:00
|
|
|
|
await Task.Delay(5000);
|
2024-01-26 22:29:57 +00:00
|
|
|
|
|
2024-01-26 23:17:56 +00:00
|
|
|
|
var elapsed = DateTime.UtcNow - start;
|
|
|
|
|
if (elapsed.TotalMinutes > 10)
|
|
|
|
|
{
|
|
|
|
|
var msg = "Unable to connect to bot for " + Time.FormatDuration(elapsed);
|
|
|
|
|
Log.Error(msg);
|
|
|
|
|
throw new Exception(msg);
|
|
|
|
|
}
|
2024-01-22 09:27:07 +00:00
|
|
|
|
}
|
2024-02-19 14:59:49 +00:00
|
|
|
|
|
|
|
|
|
lastCheck = start;
|
2024-01-22 09:27:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void PrintHelp()
|
|
|
|
|
{
|
|
|
|
|
Log.Log("TestNet Rewarder");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void EnsurePath(string path)
|
|
|
|
|
{
|
|
|
|
|
if (Directory.Exists(path)) return;
|
|
|
|
|
Directory.CreateDirectory(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|