2024-06-27 08:07:10 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 09:43:25 +00:00
|
|
|
|
public void OnNewRequest(RequestEvent requestEvent)
|
2024-06-27 08:07:10 +00:00
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
var request = requestEvent.Request;
|
|
|
|
|
AddRequestBlock(requestEvent, "New Request",
|
2024-06-27 08:07:10 +00: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)}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 09:43:25 +00:00
|
|
|
|
public void OnRequestCancelled(RequestEvent requestEvent)
|
2024-06-27 08:07:10 +00:00
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
AddRequestBlock(requestEvent, "Cancelled");
|
2024-06-27 08:07:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 09:43:25 +00:00
|
|
|
|
public void OnRequestFinished(RequestEvent requestEvent)
|
2024-06-27 08:07:10 +00:00
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
AddRequestBlock(requestEvent, "Finished");
|
2024-06-27 08:07:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 09:43:25 +00:00
|
|
|
|
public void OnRequestFulfilled(RequestEvent requestEvent)
|
2024-06-27 08:07:10 +00:00
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
AddRequestBlock(requestEvent, "Started");
|
2024-06-27 08:07:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 09:43:25 +00:00
|
|
|
|
public void OnSlotFilled(RequestEvent requestEvent, EthAddress host, BigInteger slotIndex)
|
2024-06-27 08:07:10 +00:00
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
AddRequestBlock(requestEvent, "Slot Filled",
|
2024-06-27 08:07:10 +00:00
|
|
|
|
$"Host: {host}",
|
|
|
|
|
$"Slot Index: {slotIndex}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 09:43:25 +00:00
|
|
|
|
public void OnSlotFreed(RequestEvent requestEvent, BigInteger slotIndex)
|
2024-06-27 08:07:10 +00:00
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
AddRequestBlock(requestEvent, "Slot Freed",
|
2024-06-27 08:07:10 +00:00
|
|
|
|
$"Slot Index: {slotIndex}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 09:43:25 +00:00
|
|
|
|
private void AddRequestBlock(RequestEvent requestEvent, string eventName, params string[] content)
|
2024-06-27 08:07:10 +00:00
|
|
|
|
{
|
2024-06-27 09:43:25 +00:00
|
|
|
|
var blockNumber = $"[{requestEvent.Block.BlockNumber}]";
|
|
|
|
|
var title = $"{blockNumber} **{eventName}** `{requestEvent.Request.Request.Id}`";
|
2024-06-27 08:07:10 +00:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|