Adds block numbers to chainstate event logging

This commit is contained in:
benbierens 2024-06-14 10:22:30 +02:00
parent 1fa7787d3b
commit d3488dc907
No known key found for this signature in database
GPG Key ID: 877D2C2E09A22F3A
2 changed files with 18 additions and 12 deletions

View File

@ -29,6 +29,12 @@ namespace CodexContractsPlugin.ChainMonitor
TotalSpan = timeRange;
}
public static ChainState FromTimeRange(ILog log, ICodexContracts contracts, TimeRange timeRange, IChainStateChangeHandler changeHandler)
{
var events = ChainEvents.FromTimeRange(contracts, timeRange);
return FromEvents(log, events, changeHandler);
}
public static ChainState FromEvents(ILog log, ChainEvents events, IChainStateChangeHandler changeHandler)
{
var state = new ChainState(log, changeHandler, events.BlockInterval.TimeRange);
@ -69,13 +75,13 @@ namespace CodexContractsPlugin.ChainMonitor
for (var b = events.BlockInterval.From; b <= events.BlockInterval.To; b++)
{
var blockEvents = events.All.Where(e => e.Block.BlockNumber == b).ToArray();
ApplyEvents(blockEvents, eventUtc);
ApplyEvents(b, blockEvents, eventUtc);
eventUtc += spanPerBlock;
}
}
private void ApplyEvents(IHasBlock[] blockEvents, DateTime eventsUtc)
private void ApplyEvents(ulong blockNumber, IHasBlock[] blockEvents, DateTime eventsUtc)
{
foreach (var e in blockEvents)
{
@ -83,7 +89,7 @@ namespace CodexContractsPlugin.ChainMonitor
ApplyEvent(d);
}
ApplyTimeImplicitEvents(eventsUtc);
ApplyTimeImplicitEvents(blockNumber, eventsUtc);
}
private void ApplyEvent(Request request)
@ -101,7 +107,7 @@ namespace CodexContractsPlugin.ChainMonitor
{
var r = FindRequest(request.RequestId);
if (r == null) return;
r.UpdateState(RequestState.Started);
r.UpdateState(request.Block.BlockNumber, RequestState.Started);
handler.OnRequestFulfilled(r);
}
@ -109,7 +115,7 @@ namespace CodexContractsPlugin.ChainMonitor
{
var r = FindRequest(request.RequestId);
if (r == null) return;
r.UpdateState(RequestState.Cancelled);
r.UpdateState(request.Block.BlockNumber, RequestState.Cancelled);
handler.OnRequestCancelled(r);
}
@ -117,7 +123,7 @@ namespace CodexContractsPlugin.ChainMonitor
{
var r = FindRequest(request.RequestId);
if (r == null) return;
r.Log("SlotFilled");
r.Log($"[{request.Block.BlockNumber}] SlotFilled");
handler.OnSlotFilled(r, request.SlotIndex);
}
@ -125,18 +131,18 @@ namespace CodexContractsPlugin.ChainMonitor
{
var r = FindRequest(request.RequestId);
if (r == null) return;
r.Log("SlotFreed");
r.Log($"[{request.Block.BlockNumber}] SlotFreed");
handler.OnSlotFreed(r, request.SlotIndex);
}
private void ApplyTimeImplicitEvents(DateTime eventsUtc)
private void ApplyTimeImplicitEvents(ulong blockNumber, DateTime eventsUtc)
{
foreach (var r in requests)
{
if (r.State == RequestState.Started
&& r.FinishedUtc < eventsUtc)
{
r.UpdateState(RequestState.Finished);
r.UpdateState(blockNumber, RequestState.Finished);
handler.OnRequestFinished(r);
}
}

View File

@ -24,7 +24,7 @@ namespace CodexContractsPlugin.ChainMonitor
ExpiryUtc = request.Block.Utc + TimeSpan.FromSeconds((double)request.Expiry);
FinishedUtc = request.Block.Utc + TimeSpan.FromSeconds((double)request.Ask.Duration);
Log($"Created as {State}.");
Log($"[{request.Block.BlockNumber}] Created as {State}.");
}
public Request Request { get; }
@ -32,9 +32,9 @@ namespace CodexContractsPlugin.ChainMonitor
public DateTime ExpiryUtc { get; }
public DateTime FinishedUtc { get; }
public void UpdateState(RequestState newState)
public void UpdateState(ulong blockNumber, RequestState newState)
{
Log($"Transit: {State} -> {newState}");
Log($"[{blockNumber}] Transit: {State} -> {newState}");
State = newState;
}