cs-codex-dist-tests/Tools/TestNetRewarder/Processor.cs

71 lines
2.3 KiB
C#
Raw Normal View History

2024-06-14 09:05:29 +00:00
using CodexContractsPlugin;
using CodexContractsPlugin.ChainMonitor;
2024-01-26 23:17:56 +00:00
using Logging;
using Utils;
namespace TestNetRewarder
{
public class Processor : ITimeSegmentHandler
2024-01-26 23:17:56 +00:00
{
private readonly RequestBuilder builder;
private readonly RewardChecker rewardChecker;
2024-06-27 08:07:10 +00:00
private readonly EventsFormatter eventsFormatter;
2024-06-14 09:05:29 +00:00
private readonly ChainState chainState;
private readonly BotClient client;
2024-01-26 23:17:56 +00:00
private readonly ILog log;
public Processor(Configuration config, BotClient client, ICodexContracts contracts, ILog log)
2024-01-26 23:17:56 +00:00
{
this.client = client;
2024-01-26 23:17:56 +00:00
this.log = log;
2024-06-14 09:05:29 +00:00
builder = new RequestBuilder();
rewardChecker = new RewardChecker(builder);
2024-06-27 08:07:10 +00:00
eventsFormatter = new EventsFormatter();
2024-08-21 11:59:54 +00:00
var handler = new ChainStateChangeHandlerMux(
rewardChecker.Handler,
2024-06-27 08:07:10 +00:00
eventsFormatter
);
2024-06-27 08:07:10 +00:00
chainState = new ChainState(log, contracts, handler, config.HistoryStartUtc);
2024-01-26 23:17:56 +00:00
}
public async Task<TimeSegmentResponse> OnNewSegment(TimeRange timeRange)
2024-01-26 23:17:56 +00:00
{
try
{
var sw = System.Diagnostics.Stopwatch.StartNew();
var numberOfChainEvents = await ProcessEvents(timeRange);
var duration = sw.Elapsed;
if (numberOfChainEvents == 0) return TimeSegmentResponse.Underload;
if (numberOfChainEvents > 10) return TimeSegmentResponse.Overload;
if (duration > TimeSpan.FromSeconds(1)) return TimeSegmentResponse.Overload;
return TimeSegmentResponse.OK;
2024-01-26 23:17:56 +00:00
}
catch (Exception ex)
{
2024-06-21 06:56:20 +00:00
var msg = "Exception processing time segment: " + ex;
log.Error(msg);
2024-06-27 08:07:10 +00:00
eventsFormatter.AddError(msg);
2024-04-01 11:56:07 +00:00
throw;
2024-01-26 23:17:56 +00:00
}
}
private async Task<int> ProcessEvents(TimeRange timeRange)
{
var numberOfChainEvents = chainState.Update(timeRange.To);
var events = eventsFormatter.GetEvents();
var request = builder.Build(events);
if (request.HasAny())
{
await client.SendRewards(request);
}
return numberOfChainEvents;
}
2024-01-26 23:17:56 +00:00
}
}