2025-05-22 12:43:58 +02:00
|
|
|
|
using System.Numerics;
|
2025-06-17 09:25:03 +02:00
|
|
|
|
using BlockchainUtils;
|
2025-05-20 12:47:36 +02:00
|
|
|
|
using CodexContractsPlugin.ChainMonitor;
|
2025-05-20 10:19:07 +02:00
|
|
|
|
using CodexContractsPlugin.Marketplace;
|
2025-05-20 12:47:36 +02:00
|
|
|
|
using Logging;
|
|
|
|
|
|
using Utils;
|
2025-05-20 10:19:07 +02:00
|
|
|
|
|
|
|
|
|
|
namespace TraceContract
|
|
|
|
|
|
{
|
|
|
|
|
|
public class Output
|
|
|
|
|
|
{
|
2025-05-20 14:16:33 +02:00
|
|
|
|
private class Entry
|
|
|
|
|
|
{
|
2025-06-17 09:25:03 +02:00
|
|
|
|
public Entry(BlockTimeEntry blk, string msg)
|
2025-05-20 14:16:33 +02:00
|
|
|
|
{
|
2025-06-17 09:25:03 +02:00
|
|
|
|
Blk = blk;
|
2025-05-20 14:16:33 +02:00
|
|
|
|
Msg = msg;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-17 09:25:03 +02:00
|
|
|
|
public BlockTimeEntry Blk { get; }
|
2025-05-20 14:16:33 +02:00
|
|
|
|
public string Msg { get; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-20 12:47:36 +02:00
|
|
|
|
private readonly ILog log;
|
2025-05-20 14:16:33 +02:00
|
|
|
|
private readonly List<Entry> entries = new();
|
|
|
|
|
|
private readonly string folder;
|
2025-05-20 15:09:38 +02:00
|
|
|
|
private readonly Input input;
|
|
|
|
|
|
private readonly Config config;
|
2025-05-20 12:47:36 +02:00
|
|
|
|
|
2025-05-20 14:16:33 +02:00
|
|
|
|
public Output(ILog log, Input input, Config config)
|
2025-05-20 12:47:36 +02:00
|
|
|
|
{
|
2025-06-03 13:34:39 +02:00
|
|
|
|
this.input = input;
|
|
|
|
|
|
this.config = config;
|
|
|
|
|
|
|
2025-05-22 12:43:58 +02:00
|
|
|
|
folder = config.GetOuputFolder();
|
2025-05-20 14:16:33 +02:00
|
|
|
|
Directory.CreateDirectory(folder);
|
|
|
|
|
|
|
2025-05-20 15:09:38 +02:00
|
|
|
|
var filename = Path.Combine(folder, $"contract_{input.PurchaseId}");
|
2025-05-20 14:16:33 +02:00
|
|
|
|
var fileLog = new FileLog(filename);
|
2025-06-03 13:34:39 +02:00
|
|
|
|
log.Log($"Logging to '{filename}'");
|
|
|
|
|
|
|
|
|
|
|
|
this.log = new LogSplitter(fileLog, log);
|
2025-05-20 14:16:33 +02:00
|
|
|
|
foreach (var pair in config.LogReplacements)
|
|
|
|
|
|
{
|
2025-06-03 13:34:39 +02:00
|
|
|
|
this.log.AddStringReplace(pair.Key, pair.Value);
|
|
|
|
|
|
this.log.AddStringReplace(pair.Key.ToLowerInvariant(), pair.Value);
|
2025-05-20 14:16:33 +02:00
|
|
|
|
}
|
2025-05-20 12:47:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void LogRequestCreated(RequestEvent requestEvent)
|
|
|
|
|
|
{
|
2025-06-17 09:25:03 +02:00
|
|
|
|
Add(requestEvent.Block, $"Storage request created: '{requestEvent.Request.Request.Id}'");
|
2025-05-20 12:47:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void LogRequestCancelled(RequestEvent requestEvent)
|
|
|
|
|
|
{
|
2025-06-17 09:25:03 +02:00
|
|
|
|
Add(requestEvent.Block, "Expired");
|
2025-05-20 12:47:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void LogRequestFailed(RequestEvent requestEvent)
|
|
|
|
|
|
{
|
2025-06-17 09:25:03 +02:00
|
|
|
|
Add(requestEvent.Block, "Failed");
|
2025-05-20 12:47:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void LogRequestFinished(RequestEvent requestEvent)
|
|
|
|
|
|
{
|
2025-06-17 09:25:03 +02:00
|
|
|
|
Add(requestEvent.Block, "Finished");
|
2025-05-20 12:47:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void LogRequestStarted(RequestEvent requestEvent)
|
|
|
|
|
|
{
|
2025-06-17 09:25:03 +02:00
|
|
|
|
Add(requestEvent.Block, "Started");
|
2025-05-20 12:47:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-20 14:22:34 +02:00
|
|
|
|
public void LogSlotFilled(RequestEvent requestEvent, EthAddress host, BigInteger slotIndex, bool isRepair)
|
2025-05-20 12:47:36 +02:00
|
|
|
|
{
|
2025-06-20 14:22:34 +02:00
|
|
|
|
Add(requestEvent.Block, $"Slot filled. Index: {slotIndex} Host: '{host}' isRepair: {isRepair}");
|
2025-05-20 12:47:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void LogSlotFreed(RequestEvent requestEvent, BigInteger slotIndex)
|
|
|
|
|
|
{
|
2025-06-17 09:25:03 +02:00
|
|
|
|
Add(requestEvent.Block, $"Slot freed. Index: {slotIndex}");
|
2025-05-20 12:47:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void LogSlotReservationsFull(RequestEvent requestEvent, BigInteger slotIndex)
|
|
|
|
|
|
{
|
2025-06-17 09:25:03 +02:00
|
|
|
|
Add(requestEvent.Block, $"Slot reservations full. Index: {slotIndex}");
|
2025-05-20 12:47:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void WriteContractEvents()
|
2025-05-20 10:19:07 +02:00
|
|
|
|
{
|
2025-06-17 09:25:03 +02:00
|
|
|
|
var sorted = entries.OrderBy(e => e.Blk.Utc).ToArray();
|
2025-05-20 14:16:33 +02:00
|
|
|
|
foreach (var e in sorted) Write(e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-20 15:09:38 +02:00
|
|
|
|
public LogFile CreateNodeLogTargetFile(string node)
|
|
|
|
|
|
{
|
2025-05-22 12:43:58 +02:00
|
|
|
|
return log.CreateSubfile(node);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ShowOutputFiles(ILog console)
|
|
|
|
|
|
{
|
|
|
|
|
|
console.Log("Files in output folder:");
|
|
|
|
|
|
var files = Directory.GetFiles(folder);
|
|
|
|
|
|
foreach (var file in files) console.Log(file);
|
2025-05-20 15:09:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-20 14:16:33 +02:00
|
|
|
|
private void Write(Entry e)
|
|
|
|
|
|
{
|
2025-06-17 09:25:03 +02:00
|
|
|
|
log.Log($"Block: {e.Blk.BlockNumber} [{Time.FormatTimestamp(e.Blk.Utc)}] {e.Msg}");
|
2025-05-20 14:16:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-22 13:55:03 +02:00
|
|
|
|
public void LogReserveSlotCall(ReserveSlotFunction call)
|
2025-05-20 14:16:33 +02:00
|
|
|
|
{
|
2025-06-17 09:25:03 +02:00
|
|
|
|
Add(call.Block, $"Reserve-slot called. Block: {call.Block.BlockNumber} Index: {call.SlotIndex} Host: '{call.FromAddress}'");
|
2025-05-20 14:16:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-17 09:25:03 +02:00
|
|
|
|
private void Add(BlockTimeEntry blk, string msg)
|
2025-05-20 14:16:33 +02:00
|
|
|
|
{
|
2025-06-17 09:25:03 +02:00
|
|
|
|
entries.Add(new Entry(blk, msg));
|
2025-05-20 10:19:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|