2024-01-26 22:29:57 +00:00
|
|
|
|
using CodexContractsPlugin;
|
|
|
|
|
using CodexContractsPlugin.Marketplace;
|
|
|
|
|
using GethPlugin;
|
2024-04-08 14:07:52 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
2024-04-08 14:07:52 +00:00
|
|
|
|
|
|
|
|
|
public void CleanUpOldRequests()
|
|
|
|
|
{
|
|
|
|
|
storageRequests.RemoveAll(r =>
|
|
|
|
|
r.State == RequestState.Cancelled ||
|
|
|
|
|
r.State == RequestState.Finished ||
|
|
|
|
|
r.State == RequestState.Failed
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-05-29 12:05:16 +00:00
|
|
|
|
|
|
|
|
|
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; }
|
2024-04-08 14:07:52 +00:00
|
|
|
|
|
|
|
|
|
[JsonIgnore]
|
2024-01-26 22:29:57 +00:00
|
|
|
|
public bool RecentlyStarted { get; private set; }
|
2024-04-08 14:07:52 +00:00
|
|
|
|
|
|
|
|
|
[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)
|
|
|
|
|
{
|
2024-04-08 14:07:52 +00:00
|
|
|
|
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;
|
2024-04-08 14:07:52 +00:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|