2024-05-31 09:19:50 +00:00
|
|
|
|
using CodexContractsPlugin.Marketplace;
|
2024-06-17 13:34:08 +00:00
|
|
|
|
using GethPlugin;
|
2024-05-31 09:19:50 +00:00
|
|
|
|
using Logging;
|
|
|
|
|
|
|
|
|
|
namespace CodexContractsPlugin.ChainMonitor
|
|
|
|
|
{
|
|
|
|
|
public interface IChainStateRequest
|
|
|
|
|
{
|
|
|
|
|
Request Request { get; }
|
|
|
|
|
RequestState State { get; }
|
|
|
|
|
DateTime ExpiryUtc { get; }
|
|
|
|
|
DateTime FinishedUtc { get; }
|
2024-06-17 13:34:08 +00:00
|
|
|
|
EthAddress Client { get; }
|
|
|
|
|
RequestHosts Hosts { get; }
|
2024-05-31 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ChainStateRequest : IChainStateRequest
|
|
|
|
|
{
|
|
|
|
|
private readonly ILog log;
|
|
|
|
|
|
|
|
|
|
public ChainStateRequest(ILog log, Request request, RequestState state)
|
|
|
|
|
{
|
|
|
|
|
this.log = log;
|
|
|
|
|
Request = request;
|
|
|
|
|
State = state;
|
|
|
|
|
|
|
|
|
|
ExpiryUtc = request.Block.Utc + TimeSpan.FromSeconds((double)request.Expiry);
|
|
|
|
|
FinishedUtc = request.Block.Utc + TimeSpan.FromSeconds((double)request.Ask.Duration);
|
|
|
|
|
|
2024-06-14 08:22:30 +00:00
|
|
|
|
Log($"[{request.Block.BlockNumber}] Created as {State}.");
|
2024-06-17 13:34:08 +00:00
|
|
|
|
|
|
|
|
|
Client = new EthAddress(request.Client);
|
|
|
|
|
Hosts = new RequestHosts();
|
2024-05-31 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Request Request { get; }
|
|
|
|
|
public RequestState State { get; private set; }
|
|
|
|
|
public DateTime ExpiryUtc { get; }
|
|
|
|
|
public DateTime FinishedUtc { get; }
|
2024-06-17 13:34:08 +00:00
|
|
|
|
public EthAddress Client { get; }
|
|
|
|
|
public RequestHosts Hosts { get; }
|
2024-05-31 09:19:50 +00:00
|
|
|
|
|
2024-06-14 08:22:30 +00:00
|
|
|
|
public void UpdateState(ulong blockNumber, RequestState newState)
|
2024-05-31 09:19:50 +00:00
|
|
|
|
{
|
2024-06-14 08:22:30 +00:00
|
|
|
|
Log($"[{blockNumber}] Transit: {State} -> {newState}");
|
2024-05-31 09:19:50 +00:00
|
|
|
|
State = newState;
|
|
|
|
|
}
|
2024-06-10 12:04:25 +00:00
|
|
|
|
|
|
|
|
|
public void Log(string msg)
|
|
|
|
|
{
|
|
|
|
|
log.Log($"Request '{Request.Id}': {msg}");
|
|
|
|
|
}
|
2024-05-31 09:19:50 +00:00
|
|
|
|
}
|
2024-06-17 13:34:08 +00:00
|
|
|
|
|
|
|
|
|
public class RequestHosts
|
|
|
|
|
{
|
|
|
|
|
private readonly Dictionary<int, EthAddress> hosts = new Dictionary<int, EthAddress>();
|
|
|
|
|
|
|
|
|
|
public void Add(EthAddress host, int index)
|
|
|
|
|
{
|
|
|
|
|
hosts.Add(index, host);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveHost(int index)
|
|
|
|
|
{
|
|
|
|
|
hosts.Remove(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EthAddress? GetHost(int index)
|
|
|
|
|
{
|
|
|
|
|
if (!hosts.ContainsKey(index)) return null;
|
|
|
|
|
return hosts[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EthAddress[] GetHosts()
|
|
|
|
|
{
|
|
|
|
|
return hosts.Values.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-31 09:19:50 +00:00
|
|
|
|
}
|