From 0ef55abdf49c4f4253de4851f72e6e2ff4ebdcd6 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 27 Jun 2024 10:07:10 +0200 Subject: [PATCH] Adds events formatter --- .../ChainMonitor/ChainState.cs | 5 +- .../DoNothingChainEventHandler.cs | 5 +- Tools/TestNetRewarder/BufferLogger.cs | 41 ------ Tools/TestNetRewarder/ChainChangeMux.cs | 5 +- Tools/TestNetRewarder/EventsFormatter.cs | 122 ++++++++++++++++++ Tools/TestNetRewarder/MarketTracker.cs | 3 +- Tools/TestNetRewarder/Processor.cs | 22 ++-- Tools/TestNetRewarder/RewardCheck.cs | 3 +- 8 files changed, 142 insertions(+), 64 deletions(-) delete mode 100644 Tools/TestNetRewarder/BufferLogger.cs create mode 100644 Tools/TestNetRewarder/EventsFormatter.cs diff --git a/ProjectPlugins/CodexContractsPlugin/ChainMonitor/ChainState.cs b/ProjectPlugins/CodexContractsPlugin/ChainMonitor/ChainState.cs index bc284eb..92169b7 100644 --- a/ProjectPlugins/CodexContractsPlugin/ChainMonitor/ChainState.cs +++ b/ProjectPlugins/CodexContractsPlugin/ChainMonitor/ChainState.cs @@ -1,4 +1,5 @@ using CodexContractsPlugin.Marketplace; +using GethPlugin; using Logging; using System.Numerics; using Utils; @@ -11,7 +12,7 @@ namespace CodexContractsPlugin.ChainMonitor void OnRequestFinished(IChainStateRequest request); void OnRequestFulfilled(IChainStateRequest request); void OnRequestCancelled(IChainStateRequest request); - void OnSlotFilled(IChainStateRequest request, BigInteger slotIndex); + void OnSlotFilled(IChainStateRequest request, EthAddress host, BigInteger slotIndex); void OnSlotFreed(IChainStateRequest request, BigInteger slotIndex); } @@ -116,7 +117,7 @@ namespace CodexContractsPlugin.ChainMonitor if (r == null) return; r.Hosts.Add(request.Host, (int)request.SlotIndex); r.Log($"[{request.Block.BlockNumber}] SlotFilled (host:'{request.Host}', slotIndex:{request.SlotIndex})"); - handler.OnSlotFilled(r, request.SlotIndex); + handler.OnSlotFilled(r, request.Host, request.SlotIndex); } private void ApplyEvent(SlotFreedEventDTO request) diff --git a/ProjectPlugins/CodexContractsPlugin/ChainMonitor/DoNothingChainEventHandler.cs b/ProjectPlugins/CodexContractsPlugin/ChainMonitor/DoNothingChainEventHandler.cs index 8a3709c..41882c3 100644 --- a/ProjectPlugins/CodexContractsPlugin/ChainMonitor/DoNothingChainEventHandler.cs +++ b/ProjectPlugins/CodexContractsPlugin/ChainMonitor/DoNothingChainEventHandler.cs @@ -1,4 +1,5 @@ -using System.Numerics; +using GethPlugin; +using System.Numerics; namespace CodexContractsPlugin.ChainMonitor { @@ -20,7 +21,7 @@ namespace CodexContractsPlugin.ChainMonitor { } - public void OnSlotFilled(IChainStateRequest request, BigInteger slotIndex) + public void OnSlotFilled(IChainStateRequest request, EthAddress host, BigInteger slotIndex) { } diff --git a/Tools/TestNetRewarder/BufferLogger.cs b/Tools/TestNetRewarder/BufferLogger.cs deleted file mode 100644 index 1f32332..0000000 --- a/Tools/TestNetRewarder/BufferLogger.cs +++ /dev/null @@ -1,41 +0,0 @@ -using Logging; - -namespace TestNetRewarder -{ - public class BufferLogger : ILog - { - private readonly List lines = new List(); - - public void AddStringReplace(string from, string to) - { - throw new NotImplementedException(); - } - - public LogFile CreateSubfile(string ext = "log") - { - throw new NotImplementedException(); - } - - public void Debug(string message = "", int skipFrames = 0) - { - lines.Add(message); - } - - public void Error(string message) - { - lines.Add($"Error: {message}"); - } - - public void Log(string message) - { - lines.Add(message); - } - - public string[] Get() - { - var result = lines.ToArray(); - lines.Clear(); - return result; - } - } -} diff --git a/Tools/TestNetRewarder/ChainChangeMux.cs b/Tools/TestNetRewarder/ChainChangeMux.cs index f70490b..740ba31 100644 --- a/Tools/TestNetRewarder/ChainChangeMux.cs +++ b/Tools/TestNetRewarder/ChainChangeMux.cs @@ -1,4 +1,5 @@ using CodexContractsPlugin.ChainMonitor; +using GethPlugin; using System.Numerics; namespace TestNetRewarder @@ -32,9 +33,9 @@ namespace TestNetRewarder foreach (var handler in handlers) handler.OnRequestFulfilled(request); } - public void OnSlotFilled(IChainStateRequest request, BigInteger slotIndex) + public void OnSlotFilled(IChainStateRequest request, EthAddress host, BigInteger slotIndex) { - foreach (var handler in handlers) handler.OnSlotFilled(request, slotIndex); + foreach (var handler in handlers) handler.OnSlotFilled(request, host, slotIndex); } public void OnSlotFreed(IChainStateRequest request, BigInteger slotIndex) diff --git a/Tools/TestNetRewarder/EventsFormatter.cs b/Tools/TestNetRewarder/EventsFormatter.cs new file mode 100644 index 0000000..0ddf6ee --- /dev/null +++ b/Tools/TestNetRewarder/EventsFormatter.cs @@ -0,0 +1,122 @@ +using CodexContractsPlugin; +using CodexContractsPlugin.ChainMonitor; +using GethPlugin; +using System.Numerics; +using Utils; + +namespace TestNetRewarder +{ + public class EventsFormatter : IChainStateChangeHandler + { + private static readonly string nl = Environment.NewLine; + private readonly List events = new List(); + + public string[] GetEvents() + { + var result = events.ToArray(); + events.Clear(); + return result; + } + + public void AddError(string error) + { + AddBlock("📢 **Error**", error); + } + + public void OnNewRequest(IChainStateRequest request) + { + AddRequestBlock(request, "New Request", + $"Client: {request.Client}", + $"Content: {request.Request.Content.Cid}", + $"Duration: {BigIntToDuration(request.Request.Ask.Duration)}", + $"Expiry: {BigIntToDuration(request.Request.Expiry)}", + $"Collateral: {BitIntToTestTokens(request.Request.Ask.Collateral)}", + $"Reward: {BitIntToTestTokens(request.Request.Ask.Reward)}", + $"Number of Slots: {request.Request.Ask.Slots}", + $"Slot Tolerance: {request.Request.Ask.MaxSlotLoss}", + $"Slot Size: {BigIntToByteSize(request.Request.Ask.SlotSize)}" + ); + } + + public void OnRequestCancelled(IChainStateRequest request) + { + AddRequestBlock(request, "Cancelled"); + } + + public void OnRequestFinished(IChainStateRequest request) + { + AddRequestBlock(request, "Finished"); + } + + public void OnRequestFulfilled(IChainStateRequest request) + { + AddRequestBlock(request, "Started"); + } + + public void OnSlotFilled(IChainStateRequest request, EthAddress host, BigInteger slotIndex) + { + AddRequestBlock(request, "Slot Filled", + $"Host: {host}", + $"Slot Index: {slotIndex}" + ); + } + + public void OnSlotFreed(IChainStateRequest request, BigInteger slotIndex) + { + AddRequestBlock(request, "Slot Freed", + $"Slot Index: {slotIndex}" + ); + } + + private void AddRequestBlock(IChainStateRequest request, string eventName, params string[] content) + { + var blockNumber = $"[{request.Request.Block.BlockNumber}]"; + var title = $"{blockNumber} **{eventName}** `{request.Request.Id}`"; + AddBlock(title, content); + } + + private void AddBlock(string title, params string[] content) + { + events.Add(FormatBlock(title, content)); + } + + private string FormatBlock(string title, params string[] content) + { + if (content == null || !content.Any()) + { + return $"{title}{nl}{nl}"; + } + + return string.Join(nl, + new string[] + { + title, + "```" + } + .Concat(content) + .Concat(new string[] + { + "```" + }) + ) + nl + nl; + } + + private string BigIntToDuration(BigInteger big) + { + var span = TimeSpan.FromSeconds((int)big); + return Time.FormatDuration(span); + } + + private string BigIntToByteSize(BigInteger big) + { + var size = new ByteSize((long)big); + return size.ToString(); + } + + private string BitIntToTestTokens(BigInteger big) + { + var tt = new TestToken(big); + return tt.ToString(); + } + } +} diff --git a/Tools/TestNetRewarder/MarketTracker.cs b/Tools/TestNetRewarder/MarketTracker.cs index 669dd3a..a850e32 100644 --- a/Tools/TestNetRewarder/MarketTracker.cs +++ b/Tools/TestNetRewarder/MarketTracker.cs @@ -1,5 +1,6 @@ using CodexContractsPlugin.ChainMonitor; using DiscordRewards; +using GethPlugin; using Logging; using System.Numerics; @@ -48,7 +49,7 @@ namespace TestNetRewarder { } - public void OnSlotFilled(IChainStateRequest request, BigInteger slotIndex) + public void OnSlotFilled(IChainStateRequest request, EthAddress host, BigInteger slotIndex) { } diff --git a/Tools/TestNetRewarder/Processor.cs b/Tools/TestNetRewarder/Processor.cs index f43a924..642be07 100644 --- a/Tools/TestNetRewarder/Processor.cs +++ b/Tools/TestNetRewarder/Processor.cs @@ -10,7 +10,7 @@ namespace TestNetRewarder private readonly RequestBuilder builder; private readonly RewardChecker rewardChecker; private readonly MarketTracker marketTracker; - private readonly BufferLogger bufferLogger; + private readonly EventsFormatter eventsFormatter; private readonly ChainState chainState; private readonly BotClient client; private readonly ILog log; @@ -23,14 +23,15 @@ namespace TestNetRewarder builder = new RequestBuilder(); rewardChecker = new RewardChecker(builder); marketTracker = new MarketTracker(config, log); - bufferLogger = new BufferLogger(); + eventsFormatter = new EventsFormatter(); var handler = new ChainChangeMux( rewardChecker.Handler, - marketTracker + marketTracker, + eventsFormatter ); - chainState = new ChainState(new LogSplitter(log, bufferLogger), contracts, handler, config.HistoryStartUtc); + chainState = new ChainState(log, contracts, handler, config.HistoryStartUtc); } public async Task OnNewSegment(TimeRange timeRange) @@ -40,9 +41,9 @@ namespace TestNetRewarder chainState.Update(timeRange.To); var averages = marketTracker.GetAverages(); - var lines = RemoveFirstLine(bufferLogger.Get()); + var events = eventsFormatter.GetEvents(); - var request = builder.Build(averages, lines); + var request = builder.Build(averages, events); if (request.HasAny()) { await client.SendRewards(request); @@ -52,16 +53,9 @@ namespace TestNetRewarder { var msg = "Exception processing time segment: " + ex; log.Error(msg); - bufferLogger.Error(msg); + eventsFormatter.AddError(msg); throw; } } - - private string[] RemoveFirstLine(string[] lines) - { - //if (!lines.Any()) return Array.Empty(); - //return lines.Skip(1).ToArray(); - return lines; - } } } diff --git a/Tools/TestNetRewarder/RewardCheck.cs b/Tools/TestNetRewarder/RewardCheck.cs index 9be87d2..0132f04 100644 --- a/Tools/TestNetRewarder/RewardCheck.cs +++ b/Tools/TestNetRewarder/RewardCheck.cs @@ -53,11 +53,10 @@ namespace TestNetRewarder } } - public void OnSlotFilled(IChainStateRequest request, BigInteger slotIndex) + public void OnSlotFilled(IChainStateRequest request, EthAddress host, BigInteger slotIndex) { if (MeetsRequirements(CheckType.HostFilledSlot, request)) { - var host = request.Hosts.GetHost((int)slotIndex); if (host != null) { GiveReward(reward, host);