135 lines
4.1 KiB
C#
Raw Normal View History

2025-05-20 15:09:38 +02:00
using System.IO.Compression;
using System.Numerics;
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
{
public Entry(DateTime utc, string msg)
{
Utc = utc;
Msg = msg;
}
public DateTime Utc { get; }
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;
private readonly List<string> files = new();
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-05-20 14:16:33 +02:00
folder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
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-05-20 15:09:38 +02:00
files.Add(fileLog.FullFilename + ".log");
2025-05-20 14:16:33 +02:00
foreach (var pair in config.LogReplacements)
{
fileLog.AddStringReplace(pair.Key, pair.Value);
fileLog.AddStringReplace(pair.Key.ToLowerInvariant(), pair.Value);
}
log.Log($"Logging to '{filename}'");
this.log = new LogSplitter(fileLog, log);
2025-05-20 15:09:38 +02:00
this.input = input;
this.config = config;
2025-05-20 12:47:36 +02:00
}
public void LogRequestCreated(RequestEvent requestEvent)
{
2025-05-20 14:16:33 +02:00
Add(requestEvent.Block.Utc, $"Storage request created: '{requestEvent.Request.Request.Id}'");
2025-05-20 12:47:36 +02:00
}
public void LogRequestCancelled(RequestEvent requestEvent)
{
2025-05-20 14:16:33 +02:00
Add(requestEvent.Block.Utc, "Expired");
2025-05-20 12:47:36 +02:00
}
public void LogRequestFailed(RequestEvent requestEvent)
{
2025-05-20 14:16:33 +02:00
Add(requestEvent.Block.Utc, "Failed");
2025-05-20 12:47:36 +02:00
}
public void LogRequestFinished(RequestEvent requestEvent)
{
2025-05-20 14:16:33 +02:00
Add(requestEvent.Block.Utc, "Finished");
2025-05-20 12:47:36 +02:00
}
public void LogRequestStarted(RequestEvent requestEvent)
{
2025-05-20 14:16:33 +02:00
Add(requestEvent.Block.Utc, "Started");
2025-05-20 12:47:36 +02:00
}
public void LogSlotFilled(RequestEvent requestEvent, EthAddress host, BigInteger slotIndex)
{
2025-05-20 14:16:33 +02:00
Add(requestEvent.Block.Utc, $"Slot filled. Index: {slotIndex} Host: '{host}'");
2025-05-20 12:47:36 +02:00
}
public void LogSlotFreed(RequestEvent requestEvent, BigInteger slotIndex)
{
2025-05-20 14:16:33 +02:00
Add(requestEvent.Block.Utc, $"Slot freed. Index: {slotIndex}");
2025-05-20 12:47:36 +02:00
}
public void LogSlotReservationsFull(RequestEvent requestEvent, BigInteger slotIndex)
{
2025-05-20 14:16:33 +02:00
Add(requestEvent.Block.Utc, $"Slot reservations full. Index: {slotIndex}");
2025-05-20 12:47:36 +02:00
}
public void LogReserveSlotCalls(ReserveSlotFunction[] reserveSlotFunctions)
2025-05-20 10:19:07 +02:00
{
2025-05-20 14:16:33 +02:00
foreach (var call in reserveSlotFunctions) LogReserveSlotCall(call);
2025-05-20 10:19:07 +02:00
}
2025-05-20 12:47:36 +02:00
public void WriteContractEvents()
2025-05-20 10:19:07 +02:00
{
2025-05-20 14:16:33 +02:00
var sorted = entries.OrderBy(e => e.Utc).ToArray();
foreach (var e in sorted) Write(e);
}
2025-05-20 15:09:38 +02:00
public LogFile CreateNodeLogTargetFile(string node)
{
var file = log.CreateSubfile(node);
files.Add(file.Filename);
return file;
}
2025-05-20 14:16:33 +02:00
private void Write(Entry e)
{
log.Log($"[{Time.FormatTimestamp(e.Utc)}] {e.Msg}");
}
private void LogReserveSlotCall(ReserveSlotFunction call)
{
Add(call.Block.Utc, $"Reserve-slot called. Index: {call.SlotIndex} Host: '{call.FromAddress}'");
}
2025-05-20 15:09:38 +02:00
public string Package()
2025-05-20 14:16:33 +02:00
{
2025-05-20 15:09:38 +02:00
var outputFolder = config.GetOuputFolder();
Directory.CreateDirectory(outputFolder);
var filename = Path.Combine(outputFolder, $"contract_{input.PurchaseId}.zip");
ZipFile.CreateFromDirectory(folder, filename);
return filename;
2025-05-20 14:16:33 +02:00
}
2025-05-20 15:09:38 +02:00
private void Add(DateTime utc, string msg)
2025-05-20 14:16:33 +02:00
{
2025-05-20 15:09:38 +02:00
entries.Add(new Entry(utc, msg));
2025-05-20 10:19:07 +02:00
}
}
}