2024-06-14 11:05:29 +02:00
|
|
|
|
using CodexContractsPlugin;
|
|
|
|
|
|
using CodexContractsPlugin.ChainMonitor;
|
2024-01-26 18:17:56 -05:00
|
|
|
|
using Logging;
|
|
|
|
|
|
using Utils;
|
|
|
|
|
|
|
|
|
|
|
|
namespace TestNetRewarder
|
|
|
|
|
|
{
|
2024-06-17 15:34:08 +02:00
|
|
|
|
public class Processor : ITimeSegmentHandler
|
2024-01-26 18:17:56 -05:00
|
|
|
|
{
|
2024-06-17 15:34:08 +02:00
|
|
|
|
private readonly RequestBuilder builder;
|
2024-06-27 10:07:10 +02:00
|
|
|
|
private readonly EventsFormatter eventsFormatter;
|
2024-06-14 11:05:29 +02:00
|
|
|
|
private readonly ChainState chainState;
|
2024-12-09 10:17:47 +01:00
|
|
|
|
private readonly Configuration config;
|
2024-06-17 15:34:08 +02:00
|
|
|
|
private readonly BotClient client;
|
2024-01-26 18:17:56 -05:00
|
|
|
|
private readonly ILog log;
|
2025-03-04 15:24:25 +01:00
|
|
|
|
private DateTime lastPeriodUpdateUtc;
|
2024-01-26 18:17:56 -05:00
|
|
|
|
|
2024-06-17 15:34:08 +02:00
|
|
|
|
public Processor(Configuration config, BotClient client, ICodexContracts contracts, ILog log)
|
2024-01-26 18:17:56 -05:00
|
|
|
|
{
|
2024-12-09 10:17:47 +01:00
|
|
|
|
this.config = config;
|
2024-06-17 15:34:08 +02:00
|
|
|
|
this.client = client;
|
2024-01-26 18:17:56 -05:00
|
|
|
|
this.log = log;
|
2025-03-04 15:24:25 +01:00
|
|
|
|
lastPeriodUpdateUtc = DateTime.UtcNow;
|
2024-06-14 11:05:29 +02:00
|
|
|
|
|
2024-06-17 15:34:08 +02:00
|
|
|
|
builder = new RequestBuilder();
|
2025-03-04 15:58:45 +01:00
|
|
|
|
eventsFormatter = new EventsFormatter(config);
|
2024-06-17 15:34:08 +02:00
|
|
|
|
|
2025-04-16 15:17:40 +02:00
|
|
|
|
chainState = new ChainState(log, contracts, eventsFormatter, config.HistoryStartUtc,
|
2025-03-04 15:58:45 +01:00
|
|
|
|
doProofPeriodMonitoring: config.ShowProofPeriodReports > 0);
|
2024-01-26 18:17:56 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-09 10:17:47 +01:00
|
|
|
|
public async Task Initialize()
|
|
|
|
|
|
{
|
|
|
|
|
|
var events = eventsFormatter.GetInitializationEvents(config);
|
2025-04-16 16:25:31 +02:00
|
|
|
|
var request = builder.Build(chainState, events, Array.Empty<string>());
|
2024-12-09 10:17:47 +01:00
|
|
|
|
if (request.HasAny())
|
|
|
|
|
|
{
|
|
|
|
|
|
await client.SendRewards(request);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-30 15:34:40 +02:00
|
|
|
|
public async Task<TimeSegmentResponse> OnNewSegment(TimeRange timeRange)
|
2024-01-26 18:17:56 -05:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2024-09-30 15:34:40 +02:00
|
|
|
|
var sw = System.Diagnostics.Stopwatch.StartNew();
|
|
|
|
|
|
var numberOfChainEvents = await ProcessEvents(timeRange);
|
|
|
|
|
|
var duration = sw.Elapsed;
|
2024-06-17 15:34:08 +02:00
|
|
|
|
|
2025-04-17 16:06:34 +02:00
|
|
|
|
if (duration > TimeSpan.FromSeconds(1)) return TimeSegmentResponse.Underload;
|
2025-04-16 16:25:31 +02:00
|
|
|
|
if (duration > TimeSpan.FromSeconds(3)) return TimeSegmentResponse.Overload;
|
2024-09-30 15:34:40 +02:00
|
|
|
|
return TimeSegmentResponse.OK;
|
2024-01-26 18:17:56 -05:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2024-06-21 08:56:20 +02:00
|
|
|
|
var msg = "Exception processing time segment: " + ex;
|
|
|
|
|
|
log.Error(msg);
|
2024-10-18 11:03:05 +02:00
|
|
|
|
eventsFormatter.OnError(msg);
|
2024-04-01 13:56:07 +02:00
|
|
|
|
throw;
|
2024-01-26 18:17:56 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-09-30 15:34:40 +02:00
|
|
|
|
|
|
|
|
|
|
private async Task<int> ProcessEvents(TimeRange timeRange)
|
|
|
|
|
|
{
|
|
|
|
|
|
var numberOfChainEvents = chainState.Update(timeRange.To);
|
2025-03-04 15:24:25 +01:00
|
|
|
|
ProcessPeriodUpdate();
|
2024-09-30 15:34:40 +02:00
|
|
|
|
|
|
|
|
|
|
var events = eventsFormatter.GetEvents();
|
2024-10-18 11:03:05 +02:00
|
|
|
|
var errors = eventsFormatter.GetErrors();
|
2024-09-30 15:34:40 +02:00
|
|
|
|
|
2025-04-16 16:25:31 +02:00
|
|
|
|
var request = builder.Build(chainState, events, errors);
|
2024-09-30 15:34:40 +02:00
|
|
|
|
if (request.HasAny())
|
|
|
|
|
|
{
|
|
|
|
|
|
await client.SendRewards(request);
|
|
|
|
|
|
}
|
|
|
|
|
|
return numberOfChainEvents;
|
|
|
|
|
|
}
|
2025-03-04 15:24:25 +01:00
|
|
|
|
|
|
|
|
|
|
private void ProcessPeriodUpdate()
|
|
|
|
|
|
{
|
2025-03-04 15:58:45 +01:00
|
|
|
|
if (config.ShowProofPeriodReports < 1) return;
|
2025-03-04 15:24:25 +01:00
|
|
|
|
if (DateTime.UtcNow < (lastPeriodUpdateUtc + TimeSpan.FromHours(1.0))) return;
|
|
|
|
|
|
lastPeriodUpdateUtc = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
|
|
eventsFormatter.ProcessPeriodReports(chainState.PeriodMonitor.GetAndClearReports());
|
|
|
|
|
|
}
|
2024-01-26 18:17:56 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|