Adds events formatter

This commit is contained in:
Ben 2024-06-27 10:07:10 +02:00
parent 8341807d92
commit 0ef55abdf4
No known key found for this signature in database
GPG Key ID: 541B9D8C9F1426A1
8 changed files with 142 additions and 64 deletions

View File

@ -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)

View File

@ -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)
{
}

View File

@ -1,41 +0,0 @@
using Logging;
namespace TestNetRewarder
{
public class BufferLogger : ILog
{
private readonly List<string> lines = new List<string>();
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;
}
}
}

View File

@ -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)

View File

@ -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<string> events = new List<string>();
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();
}
}
}

View File

@ -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)
{
}

View File

@ -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<string>();
//return lines.Skip(1).ToArray();
return lines;
}
}
}

View File

@ -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);