2024-05-31 09:19:50 +00:00
|
|
|
|
using CodexContractsPlugin.Marketplace;
|
2024-06-27 08:07:10 +00:00
|
|
|
|
using GethPlugin;
|
2024-05-31 09:19:50 +00:00
|
|
|
|
using Logging;
|
2024-06-27 09:43:25 +00:00
|
|
|
|
using NethereumWorkflow.BlockUtils;
|
2024-05-31 09:19:50 +00:00
|
|
|
|
using System.Numerics;
|
|
|
|
|
using Utils;
|
|
|
|
|
|
|
|
|
|
namespace CodexContractsPlugin.ChainMonitor
|
2024-05-31 08:06:34 +00:00
|
|
|
|
{
|
2024-05-31 09:19:50 +00:00
|
|
|
|
public interface IChainStateChangeHandler
|
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
void OnNewRequest(RequestEvent requestEvent);
|
|
|
|
|
void OnRequestFinished(RequestEvent requestEvent);
|
|
|
|
|
void OnRequestFulfilled(RequestEvent requestEvent);
|
|
|
|
|
void OnRequestCancelled(RequestEvent requestEvent);
|
2024-08-21 11:59:54 +00:00
|
|
|
|
void OnRequestFailed(RequestEvent requestEvent);
|
2024-06-27 09:43:25 +00:00
|
|
|
|
void OnSlotFilled(RequestEvent requestEvent, EthAddress host, BigInteger slotIndex);
|
|
|
|
|
void OnSlotFreed(RequestEvent requestEvent, BigInteger slotIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class RequestEvent
|
|
|
|
|
{
|
|
|
|
|
public RequestEvent(BlockTimeEntry block, IChainStateRequest request)
|
|
|
|
|
{
|
|
|
|
|
Block = block;
|
|
|
|
|
Request = request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public BlockTimeEntry Block { get; }
|
|
|
|
|
public IChainStateRequest Request { get; }
|
2024-05-31 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-31 08:06:34 +00:00
|
|
|
|
public class ChainState
|
|
|
|
|
{
|
2024-05-31 09:19:50 +00:00
|
|
|
|
private readonly List<ChainStateRequest> requests = new List<ChainStateRequest>();
|
|
|
|
|
private readonly ILog log;
|
2024-06-14 09:05:29 +00:00
|
|
|
|
private readonly ICodexContracts contracts;
|
2024-05-31 09:19:50 +00:00
|
|
|
|
private readonly IChainStateChangeHandler handler;
|
|
|
|
|
|
2024-06-14 09:05:29 +00:00
|
|
|
|
public ChainState(ILog log, ICodexContracts contracts, IChainStateChangeHandler changeHandler, DateTime startUtc)
|
2024-05-31 09:19:50 +00:00
|
|
|
|
{
|
2024-06-10 12:04:25 +00:00
|
|
|
|
this.log = new LogPrefixer(log, "(ChainState) ");
|
2024-06-14 09:05:29 +00:00
|
|
|
|
this.contracts = contracts;
|
2024-05-31 09:19:50 +00:00
|
|
|
|
handler = changeHandler;
|
2024-06-14 09:05:29 +00:00
|
|
|
|
TotalSpan = new TimeRange(startUtc, startUtc);
|
2024-05-31 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TimeRange TotalSpan { get; private set; }
|
|
|
|
|
public IChainStateRequest[] Requests => requests.ToArray();
|
|
|
|
|
|
2024-09-30 13:34:40 +00:00
|
|
|
|
public int Update()
|
2024-06-10 12:04:25 +00:00
|
|
|
|
{
|
2024-09-30 13:34:40 +00:00
|
|
|
|
return Update(DateTime.UtcNow);
|
2024-06-10 12:04:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-30 13:34:40 +00:00
|
|
|
|
public int Update(DateTime toUtc)
|
2024-06-10 12:04:25 +00:00
|
|
|
|
{
|
|
|
|
|
var span = new TimeRange(TotalSpan.To, toUtc);
|
|
|
|
|
var events = ChainEvents.FromTimeRange(contracts, span);
|
|
|
|
|
Apply(events);
|
|
|
|
|
|
|
|
|
|
TotalSpan = new TimeRange(TotalSpan.From, span.To);
|
2024-09-30 13:34:40 +00:00
|
|
|
|
return events.All.Length;
|
2024-06-10 12:04:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Apply(ChainEvents events)
|
2024-05-31 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
if (events.BlockInterval.TimeRange.From < TotalSpan.From)
|
|
|
|
|
throw new Exception("Attempt to update ChainState with set of events from before its current record.");
|
|
|
|
|
|
|
|
|
|
log.Log($"ChainState updating: {events.BlockInterval}");
|
|
|
|
|
|
|
|
|
|
// Run through each block and apply the events to the state in order.
|
|
|
|
|
var span = events.BlockInterval.TimeRange.Duration;
|
|
|
|
|
var numBlocks = events.BlockInterval.NumberOfBlocks;
|
|
|
|
|
var spanPerBlock = span / numBlocks;
|
|
|
|
|
|
|
|
|
|
var eventUtc = events.BlockInterval.TimeRange.From;
|
2024-06-10 12:04:25 +00:00
|
|
|
|
for (var b = events.BlockInterval.From; b <= events.BlockInterval.To; b++)
|
2024-05-31 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
var blockEvents = events.All.Where(e => e.Block.BlockNumber == b).ToArray();
|
2024-06-14 08:22:30 +00:00
|
|
|
|
ApplyEvents(b, blockEvents, eventUtc);
|
2024-05-31 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
eventUtc += spanPerBlock;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-14 08:22:30 +00:00
|
|
|
|
private void ApplyEvents(ulong blockNumber, IHasBlock[] blockEvents, DateTime eventsUtc)
|
2024-05-31 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (var e in blockEvents)
|
|
|
|
|
{
|
|
|
|
|
dynamic d = e;
|
|
|
|
|
ApplyEvent(d);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-14 08:22:30 +00:00
|
|
|
|
ApplyTimeImplicitEvents(blockNumber, eventsUtc);
|
2024-05-31 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ApplyEvent(Request request)
|
|
|
|
|
{
|
|
|
|
|
if (requests.Any(r => Equal(r.Request.RequestId, request.RequestId)))
|
|
|
|
|
throw new Exception("Received NewRequest event for id that already exists.");
|
|
|
|
|
|
|
|
|
|
var newRequest = new ChainStateRequest(log, request, RequestState.New);
|
|
|
|
|
requests.Add(newRequest);
|
|
|
|
|
|
2024-06-27 09:43:25 +00:00
|
|
|
|
handler.OnNewRequest(new RequestEvent(request.Block, newRequest));
|
2024-05-31 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 09:43:25 +00:00
|
|
|
|
private void ApplyEvent(RequestFulfilledEventDTO @event)
|
2024-05-31 09:19:50 +00:00
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
var r = FindRequest(@event.RequestId);
|
2024-06-10 12:04:25 +00:00
|
|
|
|
if (r == null) return;
|
2024-06-27 09:43:25 +00:00
|
|
|
|
r.UpdateState(@event.Block.BlockNumber, RequestState.Started);
|
|
|
|
|
handler.OnRequestFulfilled(new RequestEvent(@event.Block, r));
|
2024-05-31 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 09:43:25 +00:00
|
|
|
|
private void ApplyEvent(RequestCancelledEventDTO @event)
|
2024-05-31 09:19:50 +00:00
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
var r = FindRequest(@event.RequestId);
|
2024-06-10 12:04:25 +00:00
|
|
|
|
if (r == null) return;
|
2024-06-27 09:43:25 +00:00
|
|
|
|
r.UpdateState(@event.Block.BlockNumber, RequestState.Cancelled);
|
|
|
|
|
handler.OnRequestCancelled(new RequestEvent(@event.Block, r));
|
2024-05-31 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-21 11:59:54 +00:00
|
|
|
|
private void ApplyEvent(RequestFailedEventDTO @event)
|
|
|
|
|
{
|
|
|
|
|
var r = FindRequest(@event.RequestId);
|
|
|
|
|
if (r == null) return;
|
|
|
|
|
r.UpdateState(@event.Block.BlockNumber, RequestState.Failed);
|
|
|
|
|
handler.OnRequestFailed(new RequestEvent(@event.Block, r));
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 09:43:25 +00:00
|
|
|
|
private void ApplyEvent(SlotFilledEventDTO @event)
|
2024-05-31 09:19:50 +00:00
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
var r = FindRequest(@event.RequestId);
|
2024-06-10 12:04:25 +00:00
|
|
|
|
if (r == null) return;
|
2024-06-27 09:43:25 +00:00
|
|
|
|
r.Hosts.Add(@event.Host, (int)@event.SlotIndex);
|
|
|
|
|
r.Log($"[{@event.Block.BlockNumber}] SlotFilled (host:'{@event.Host}', slotIndex:{@event.SlotIndex})");
|
|
|
|
|
handler.OnSlotFilled(new RequestEvent(@event.Block, r), @event.Host, @event.SlotIndex);
|
2024-05-31 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 09:43:25 +00:00
|
|
|
|
private void ApplyEvent(SlotFreedEventDTO @event)
|
2024-05-31 09:19:50 +00:00
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
var r = FindRequest(@event.RequestId);
|
2024-06-10 12:04:25 +00:00
|
|
|
|
if (r == null) return;
|
2024-06-27 09:43:25 +00:00
|
|
|
|
r.Hosts.RemoveHost((int)@event.SlotIndex);
|
|
|
|
|
r.Log($"[{@event.Block.BlockNumber}] SlotFreed (slotIndex:{@event.SlotIndex})");
|
|
|
|
|
handler.OnSlotFreed(new RequestEvent(@event.Block, r), @event.SlotIndex);
|
2024-05-31 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-14 08:22:30 +00:00
|
|
|
|
private void ApplyTimeImplicitEvents(ulong blockNumber, DateTime eventsUtc)
|
2024-05-31 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (var r in requests)
|
|
|
|
|
{
|
|
|
|
|
if (r.State == RequestState.Started
|
|
|
|
|
&& r.FinishedUtc < eventsUtc)
|
|
|
|
|
{
|
2024-06-14 08:22:30 +00:00
|
|
|
|
r.UpdateState(blockNumber, RequestState.Finished);
|
2024-06-27 09:43:25 +00:00
|
|
|
|
handler.OnRequestFinished(new RequestEvent(new BlockTimeEntry(blockNumber, eventsUtc), r));
|
2024-05-31 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-10 12:04:25 +00:00
|
|
|
|
private ChainStateRequest? FindRequest(byte[] requestId)
|
2024-05-31 09:19:50 +00:00
|
|
|
|
{
|
2024-06-10 12:04:25 +00:00
|
|
|
|
var r = requests.SingleOrDefault(r => Equal(r.Request.RequestId, requestId));
|
|
|
|
|
if (r == null) log.Log("Unable to find request by ID!");
|
|
|
|
|
return r;
|
2024-05-31 09:19:50 +00:00
|
|
|
|
}
|
2024-05-31 08:06:34 +00:00
|
|
|
|
|
2024-05-31 09:19:50 +00:00
|
|
|
|
private bool Equal(byte[] a, byte[] b)
|
|
|
|
|
{
|
|
|
|
|
return a.SequenceEqual(b);
|
|
|
|
|
}
|
2024-05-31 08:06:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|