cs-codex-dist-tests/Tools/TestNetRewarder/HistoricState.cs

98 lines
2.6 KiB
C#
Raw Permalink Normal View History

2024-01-26 22:29:57 +00:00
using CodexContractsPlugin;
using CodexContractsPlugin.Marketplace;
using GethPlugin;
using Newtonsoft.Json;
2024-01-26 22:29:57 +00:00
namespace TestNetRewarder
{
public class HistoricState
{
private readonly List<StorageRequest> storageRequests = new List<StorageRequest>();
public StorageRequest[] StorageRequests { get { return storageRequests.ToArray(); } }
public void ProcessNewRequests(Request[] requests)
{
storageRequests.AddRange(requests.Select(r => new StorageRequest(r)));
}
public void UpdateStorageRequests(ICodexContracts contracts)
{
foreach (var r in storageRequests) r.Update(contracts);
}
public void CleanUpOldRequests()
{
storageRequests.RemoveAll(r =>
r.State == RequestState.Cancelled ||
r.State == RequestState.Finished ||
r.State == RequestState.Failed
);
}
public string EntireString()
{
return JsonConvert.SerializeObject(StorageRequests);
}
2024-05-30 09:17:13 +00:00
public HistoricState()
{
}
public HistoricState(StorageRequest[] requests)
{
storageRequests.AddRange(requests);
}
2024-01-26 22:29:57 +00:00
}
public class StorageRequest
{
public StorageRequest(Request request)
{
Request = request;
Hosts = Array.Empty<EthAddress>();
}
public Request Request { get; }
public EthAddress[] Hosts { get; private set; }
public RequestState State { get; private set; }
[JsonIgnore]
2024-01-26 22:29:57 +00:00
public bool RecentlyStarted { get; private set; }
[JsonIgnore]
2024-04-07 12:04:31 +00:00
public bool RecentlyFinished { get; private set; }
2024-01-26 22:29:57 +00:00
public void Update(ICodexContracts contracts)
{
var newHosts = GetHosts(contracts);
2024-01-26 22:29:57 +00:00
var newState = contracts.GetRequestState(Request);
RecentlyStarted =
State == RequestState.New &&
newState == RequestState.Started;
2024-04-07 12:04:31 +00:00
RecentlyFinished =
2024-01-26 22:29:57 +00:00
State == RequestState.Started &&
newState == RequestState.Finished;
State = newState;
Hosts = newHosts;
}
2024-01-26 22:29:57 +00:00
private EthAddress[] GetHosts(ICodexContracts contracts)
{
var result = new List<EthAddress>();
for (decimal i = 0; i < Request.Ask.Slots; i++)
{
var host = contracts.GetSlotHost(Request, i);
if (host != null) result.Add(host);
}
return result.ToArray();
}
}
}