Skip to content

Commit

Permalink
Adds proofSubmitted event
Browse files Browse the repository at this point in the history
  • Loading branch information
benbierens committed Mar 4, 2025
1 parent d6cd776 commit 07c3e5f
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 17 deletions.
11 changes: 7 additions & 4 deletions ProjectPlugins/CodexContractsPlugin/ChainMonitor/ChainEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ private ChainEvents(
RequestFailedEventDTO[] failed,
SlotFilledEventDTO[] slotFilled,
SlotFreedEventDTO[] slotFreed,
SlotReservationsFullEventDTO[] slotReservationsFull
SlotReservationsFullEventDTO[] slotReservationsFull,
ProofSubmittedEventDTO[] proofSubmitted
)
{
BlockInterval = blockInterval;
Expand All @@ -25,8 +26,8 @@ SlotReservationsFullEventDTO[] slotReservationsFull
SlotFilled = slotFilled;
SlotFreed = slotFreed;
SlotReservationsFull = slotReservationsFull;

All = ConcatAll<IHasBlock>(requests, fulfilled, cancelled, failed, slotFilled, SlotFreed, SlotReservationsFull);
ProofSubmitted = proofSubmitted;
All = ConcatAll<IHasBlock>(requests, fulfilled, cancelled, failed, slotFilled, SlotFreed, SlotReservationsFull, ProofSubmitted);
}

public BlockInterval BlockInterval { get; }
Expand All @@ -37,6 +38,7 @@ SlotReservationsFullEventDTO[] slotReservationsFull
public SlotFilledEventDTO[] SlotFilled { get; }
public SlotFreedEventDTO[] SlotFreed { get; }
public SlotReservationsFullEventDTO[] SlotReservationsFull { get; }
public ProofSubmittedEventDTO[] ProofSubmitted { get; }
public IHasBlock[] All { get; }

public static ChainEvents FromBlockInterval(ICodexContracts contracts, BlockInterval blockInterval)
Expand All @@ -59,7 +61,8 @@ public static ChainEvents FromContractEvents(ICodexContractsEvents events)
events.GetRequestFailedEvents(),
events.GetSlotFilledEvents(),
events.GetSlotFreedEvents(),
events.GetSlotReservationsFull()
events.GetSlotReservationsFullEvents(),
events.GetProofSubmittedEvents()
);
}

Expand Down
14 changes: 12 additions & 2 deletions ProjectPlugins/CodexContractsPlugin/ChainMonitor/ChainState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface IChainStateChangeHandler
void OnSlotFilled(RequestEvent requestEvent, EthAddress host, BigInteger slotIndex);
void OnSlotFreed(RequestEvent requestEvent, BigInteger slotIndex);
void OnSlotReservationsFull(RequestEvent requestEvent, BigInteger slotIndex);

void OnProofSubmitted(BlockTimeEntry block, string id);
void OnError(string msg);
}

Expand All @@ -39,12 +39,14 @@ public class ChainState
private readonly ILog log;
private readonly ICodexContracts contracts;
private readonly IChainStateChangeHandler handler;
private readonly bool doProofPeriodMonitoring;

public ChainState(ILog log, ICodexContracts contracts, IChainStateChangeHandler changeHandler, DateTime startUtc)
public ChainState(ILog log, ICodexContracts contracts, IChainStateChangeHandler changeHandler, DateTime startUtc, bool doProofPeriodMonitoring)
{
this.log = new LogPrefixer(log, "(ChainState) ");
this.contracts = contracts;
handler = changeHandler;
this.doProofPeriodMonitoring = doProofPeriodMonitoring;
TotalSpan = new TimeRange(startUtc, startUtc);
PeriodMonitor = new PeriodMonitor(this.log, contracts);
}
Expand Down Expand Up @@ -98,6 +100,7 @@ private void Apply(ChainEvents events)

private void UpdatePeriodMonitor(ulong blockNumber, DateTime eventUtc)
{
if (!doProofPeriodMonitoring) return;
PeriodMonitor.Update(blockNumber, eventUtc, Requests);
}

Expand Down Expand Up @@ -173,6 +176,13 @@ private void ApplyEvent(SlotReservationsFullEventDTO @event)
handler.OnSlotReservationsFull(new RequestEvent(@event.Block, r), @event.SlotIndex);
}

private void ApplyEvent(ProofSubmittedEventDTO @event)
{
var id = Base58.Encode(@event.Id);
log.Log($"[{@event.Block.BlockNumber}] Proof submitted (id:{id})");
handler.OnProofSubmitted(@event.Block, id);
}

private void ApplyTimeImplicitEvents(ulong blockNumber, DateTime eventsUtc)
{
foreach (var r in requests)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Numerics;
using BlockchainUtils;
using System.Numerics;
using Utils;

namespace CodexContractsPlugin.ChainMonitor
Expand Down Expand Up @@ -56,5 +57,10 @@ public void OnError(string msg)
{
foreach (var handler in Handlers) handler.OnError(msg);
}

public void OnProofSubmitted(BlockTimeEntry block, string id)
{
foreach (var handler in Handlers) handler.OnProofSubmitted(block, id);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Numerics;
using BlockchainUtils;
using System.Numerics;
using Utils;

namespace CodexContractsPlugin.ChainMonitor
Expand Down Expand Up @@ -40,5 +41,9 @@ public void OnSlotReservationsFull(RequestEvent requestEvent, BigInteger slotInd
public void OnError(string msg)
{
}

public void OnProofSubmitted(BlockTimeEntry block, string id)
{
}
}
}
11 changes: 9 additions & 2 deletions ProjectPlugins/CodexContractsPlugin/CodexContractsEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public interface ICodexContractsEvents
RequestFailedEventDTO[] GetRequestFailedEvents();
SlotFilledEventDTO[] GetSlotFilledEvents();
SlotFreedEventDTO[] GetSlotFreedEvents();
SlotReservationsFullEventDTO[] GetSlotReservationsFull();
SlotReservationsFullEventDTO[] GetSlotReservationsFullEvents();
ProofSubmittedEventDTO[] GetProofSubmittedEvents();
}

public class CodexContractsEvents : ICodexContractsEvents
Expand Down Expand Up @@ -86,12 +87,18 @@ public SlotFreedEventDTO[] GetSlotFreedEvents()
return events.Select(SetBlockOnEvent).ToArray();
}

public SlotReservationsFullEventDTO[] GetSlotReservationsFull()
public SlotReservationsFullEventDTO[] GetSlotReservationsFullEvents()
{
var events = gethNode.GetEvents<SlotReservationsFullEventDTO>(deployment.MarketplaceAddress, BlockInterval);
return events.Select(SetBlockOnEvent).ToArray();
}

public ProofSubmittedEventDTO[] GetProofSubmittedEvents()
{
var events = gethNode.GetEvents<ProofSubmittedEventDTO>(deployment.MarketplaceAddress, BlockInterval);
return events.Select(SetBlockOnEvent).ToArray();
}

private T SetBlockOnEvent<T>(EventLog<T> e) where T : IHasBlock
{
var result = e.Event;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,11 @@ public partial class SlotReservationsFullEventDTO : IHasBlock, IHasRequestId
[JsonIgnore]
public BlockTimeEntry Block { get; set; }
}

public partial class ProofSubmittedEventDTO : IHasBlock
{
[JsonIgnore]
public BlockTimeEntry Block { get; set; }
}
}
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
3 changes: 2 additions & 1 deletion Tools/MarketInsights/AverageHistory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public AverageHistory(AppState appState, ICodexContracts contracts, int maxContr
{
this.appState = appState;
this.maxContributions = maxContributions;
chainState = new ChainState(appState.Log, contracts, mux, appState.Config.HistoryStartUtc);
chainState = new ChainState(appState.Log, contracts, mux, appState.Config.HistoryStartUtc,
doProofPeriodMonitoring: false);
}

public MarketTimeSegment[] Segments { get; private set; } = Array.Empty<MarketTimeSegment>();
Expand Down
7 changes: 6 additions & 1 deletion Tools/MarketInsights/ContributionBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CodexContractsPlugin.ChainMonitor;
using BlockchainUtils;
using CodexContractsPlugin.ChainMonitor;
using GethPlugin;
using Logging;
using System.Numerics;
Expand Down Expand Up @@ -58,6 +59,10 @@ public void OnSlotReservationsFull(RequestEvent requestEvent, BigInteger slotInd
{
}

public void OnProofSubmitted(BlockTimeEntry block, string id)
{
}

public void OnError(string msg)
{
log.Error(msg);
Expand Down
6 changes: 6 additions & 0 deletions Tools/TestNetRewarder/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public class Configuration
[Uniform("events-overview", "eo", "EVENTSOVERVIEW", false, "When greater than zero, chain event summary will be generated.")]
public int CreateChainEventsOverview { get; set; } = 1;

[Uniform("proof-period-reports", "ppr", "PROOFPERIODREPORTS", false, "When greater than zero, chain event summary will include period reports of the proving system.")]
public int ShowProofPeriodReports { get; set; } = 1;

[Uniform("proof-submitted-events", "pse", "PROOFSUBMITTEDEVENTS", false, "When greater than zero, chain event summary will include proof-submitted events.")]
public int ShowProofSubmittedEvents { get; set; } = 1;

public string LogPath
{
get
Expand Down
2 changes: 2 additions & 0 deletions Tools/TestNetRewarder/EmojiMaps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public class EmojiMaps
public string Finished => "✅";
public string Cancelled => "🚫";
public string Failed => "❌";
public string ProofSubmitted => "🎵";
public string ProofReport => "🔎";

public string StringToEmojis(string input, int outLength)
{
Expand Down
22 changes: 19 additions & 3 deletions Tools/TestNetRewarder/EventsFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CodexContractsPlugin;
using BlockchainUtils;
using CodexContractsPlugin;
using CodexContractsPlugin.ChainMonitor;
using DiscordRewards;
using GethPlugin;
Expand All @@ -14,6 +15,12 @@ public class EventsFormatter : IChainStateChangeHandler
private readonly List<ChainEventMessage> events = new List<ChainEventMessage>();
private readonly List<string> errors = new List<string>();
private readonly EmojiMaps emojiMaps = new EmojiMaps();
private readonly Configuration config;

public EventsFormatter(Configuration config)
{
this.config = config;
}

public ChainEventMessage[] GetInitializationEvents(Configuration config)
{
Expand Down Expand Up @@ -96,7 +103,16 @@ public void OnSlotReservationsFull(RequestEvent requestEvent, BigInteger slotInd
$"Slot Index: {slotIndex}"
);
}


public void OnProofSubmitted(BlockTimeEntry block, string id)
{
if (config.ShowProofSubmittedEvents < 1) return;

AddBlock(block.BlockNumber, $"{emojiMaps.ProofSubmitted} **Proof submitted**",
$"Id: {id}"
);
}

public void OnError(string msg)
{
errors.Add(msg);
Expand All @@ -106,7 +122,7 @@ public void ProcessPeriodReports(PeriodReport[] periodReports)
{
var lines = periodReports.Select(FormatPeriodReport).ToList();
lines.Insert(0, FormatPeriodReportLine("period", "totalSlots", "required", "missed"));
AddBlock(0, "Proof system report", lines.ToArray());
AddBlock(0, $"{emojiMaps.ProofReport} **Proof system report**", lines.ToArray());
}

private string FormatPeriodReport(PeriodReport report)
Expand Down
6 changes: 4 additions & 2 deletions Tools/TestNetRewarder/Processor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ public Processor(Configuration config, BotClient client, ICodexContracts contrac

builder = new RequestBuilder();
rewardChecker = new RewardChecker(builder);
eventsFormatter = new EventsFormatter();
eventsFormatter = new EventsFormatter(config);

var handler = new ChainStateChangeHandlerMux(
rewardChecker.Handler,
eventsFormatter
);

chainState = new ChainState(log, contracts, handler, config.HistoryStartUtc);
chainState = new ChainState(log, contracts, handler, config.HistoryStartUtc,
doProofPeriodMonitoring: config.ShowProofPeriodReports > 0);
}

public async Task Initialize()
Expand Down Expand Up @@ -85,6 +86,7 @@ private async Task<int> ProcessEvents(TimeRange timeRange)

private void ProcessPeriodUpdate()
{
if (config.ShowProofPeriodReports < 1) return;
if (DateTime.UtcNow < (lastPeriodUpdateUtc + TimeSpan.FromHours(1.0))) return;
lastPeriodUpdateUtc = DateTime.UtcNow;

Expand Down
4 changes: 4 additions & 0 deletions Tools/TestNetRewarder/RewardCheck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public void OnError(string msg)
{
}

public void OnProofSubmitted(BlockTimeEntry block, string id)
{
}

private void GiveReward(RewardConfig reward, EthAddress receiver)
{
giver.Give(reward, receiver);
Expand Down

0 comments on commit 07c3e5f

Please sign in to comment.