2
0
mirror of synced 2025-02-11 07:56:58 +00:00

169 lines
5.5 KiB
C#
Raw Normal View History

2024-06-27 10:07:10 +02:00
using CodexContractsPlugin;
using CodexContractsPlugin.ChainMonitor;
2024-10-14 09:43:05 +02:00
using DiscordRewards;
2024-06-27 10:07:10 +02:00
using GethPlugin;
using System.Globalization;
2024-06-27 10:07:10 +02:00
using System.Numerics;
using Utils;
namespace TestNetRewarder
{
public class EventsFormatter : IChainStateChangeHandler
{
private static readonly string nl = Environment.NewLine;
2024-10-14 09:43:05 +02:00
private readonly List<ChainEventMessage> events = new List<ChainEventMessage>();
private readonly List<string> errors = new List<string>();
2024-10-10 11:45:04 +02:00
private readonly EmojiMaps emojiMaps = new EmojiMaps();
2024-06-27 10:07:10 +02:00
2024-10-14 09:43:05 +02:00
public ChainEventMessage[] GetEvents()
2024-06-27 10:07:10 +02:00
{
var result = events.ToArray();
events.Clear();
return result;
}
public string[] GetErrors()
2024-06-27 10:07:10 +02:00
{
var result = errors.ToArray();
errors.Clear();
return result;
2024-06-27 10:07:10 +02:00
}
public void OnNewRequest(RequestEvent requestEvent)
2024-06-27 10:07:10 +02:00
{
var request = requestEvent.Request;
2024-10-10 14:03:42 +02:00
AddRequestBlock(requestEvent, $"{emojiMaps.NewRequest} New Request",
2024-06-27 10:07:10 +02:00
$"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(RequestEvent requestEvent)
2024-06-27 10:07:10 +02:00
{
2024-10-10 14:03:42 +02:00
AddRequestBlock(requestEvent, $"{emojiMaps.Cancelled} Cancelled");
2024-06-27 10:07:10 +02:00
}
2024-08-21 13:59:54 +02:00
public void OnRequestFailed(RequestEvent requestEvent)
{
2024-10-10 14:03:42 +02:00
AddRequestBlock(requestEvent, $"{emojiMaps.Failed} Failed");
2024-08-21 13:59:54 +02:00
}
public void OnRequestFinished(RequestEvent requestEvent)
2024-06-27 10:07:10 +02:00
{
2024-10-10 14:03:42 +02:00
AddRequestBlock(requestEvent, $"{emojiMaps.Finished} Finished");
2024-06-27 10:07:10 +02:00
}
public void OnRequestFulfilled(RequestEvent requestEvent)
2024-06-27 10:07:10 +02:00
{
2024-10-10 14:03:42 +02:00
AddRequestBlock(requestEvent, $"{emojiMaps.Started} Started");
2024-06-27 10:07:10 +02:00
}
public void OnSlotFilled(RequestEvent requestEvent, EthAddress host, BigInteger slotIndex)
2024-06-27 10:07:10 +02:00
{
2024-10-10 14:03:42 +02:00
AddRequestBlock(requestEvent, $"{emojiMaps.SlotFilled} Slot Filled",
2024-06-27 10:07:10 +02:00
$"Host: {host}",
$"Slot Index: {slotIndex}"
);
}
public void OnSlotFreed(RequestEvent requestEvent, BigInteger slotIndex)
2024-06-27 10:07:10 +02:00
{
2024-10-10 14:03:42 +02:00
AddRequestBlock(requestEvent, $"{emojiMaps.SlotFreed} Slot Freed",
2024-06-27 10:07:10 +02:00
$"Slot Index: {slotIndex}"
);
}
public void OnSlotReservationsFull(RequestEvent requestEvent, BigInteger slotIndex)
{
2024-10-10 14:03:42 +02:00
AddRequestBlock(requestEvent, $"{emojiMaps.SlotReservationsFull} Slot Reservations Full",
$"Slot Index: {slotIndex}"
);
}
public void OnError(string msg)
{
errors.Add(msg);
}
private void AddRequestBlock(RequestEvent requestEvent, string eventName, params string[] content)
2024-06-27 10:07:10 +02:00
{
var blockNumber = $"[{requestEvent.Block.BlockNumber} {FormatDateTime(requestEvent.Block.Utc)}]";
2024-10-10 14:03:42 +02:00
var title = $"{blockNumber} **{eventName}** {FormatRequestId(requestEvent)}";
2024-10-14 09:43:05 +02:00
AddBlock(requestEvent.Block.BlockNumber, title, content);
2024-06-27 10:07:10 +02:00
}
2024-10-14 09:43:05 +02:00
private void AddBlock(ulong blockNumber, string title, params string[] content)
2024-06-27 10:07:10 +02:00
{
2024-10-14 09:43:05 +02:00
events.Add(FormatBlock(blockNumber, title, content));
2024-06-27 10:07:10 +02:00
}
2024-10-14 09:43:05 +02:00
private ChainEventMessage FormatBlock(ulong blockNumber, string title, params string[] content)
{
var msg = FormatBlockMessage(title, content);
return new ChainEventMessage
{
BlockNumber = blockNumber,
Message = msg
};
}
private string FormatBlockMessage(string title, string[] content)
2024-06-27 10:07:10 +02:00
{
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 FormatDateTime(DateTime utc)
{
return utc.ToString("yyyy-MM-dd HH:mm:ss UTC", CultureInfo.InvariantCulture);
}
2024-10-10 14:03:42 +02:00
private string FormatRequestId(RequestEvent requestEvent)
{
return
$"({emojiMaps.StringToEmojis(requestEvent.Request.Request.Id, 3)})" +
$"`{requestEvent.Request.Request.Id}`";
}
2024-06-27 10:07:10 +02:00
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();
}
}
}