2
0
mirror of synced 2025-01-23 14:59:14 +00:00

Setting up marketplace hooks

This commit is contained in:
Ben 2024-08-20 11:44:15 +02:00
parent cc9d04acd7
commit 09d2f418eb
No known key found for this signature in database
GPG Key ID: 0F16E812E736C24B
5 changed files with 35 additions and 11 deletions

View File

@ -11,5 +11,16 @@
remainingItems.RemoveAt(i);
return result;
}
public static T[] Shuffled<T>(T[] items)
{
var result = new List<T>();
var source = items.ToList();
while (source.Any())
{
result.Add(RandomUtils.PickOneRandom(source));
}
return result.ToArray();
}
}
}

View File

@ -65,7 +65,7 @@ namespace CodexPlugin
public void Awake()
{
hooks.OnNodeStarting(Container.Recipe.RecipeCreatedUtc, Container.Recipe.Image);
hooks.OnNodeStarting(Container.Recipe.RecipeCreatedUtc, Container.Recipe.Image, ethAccount);
}
public void Initialize()

View File

@ -1,15 +1,18 @@
using Utils;
using GethPlugin;
using Utils;
namespace CodexPlugin.Hooks
{
public interface ICodexNodeHooks
{
void OnNodeStarting(DateTime startUtc, string image);
void OnNodeStarting(DateTime startUtc, string image, EthAccount? ethAccount);
void OnNodeStarted(string peerId, string nodeId);
void OnNodeStopping();
void OnFileUploading(string uid, ByteSize size);
void OnFileUploaded(string uid, ByteSize size, ContentId cid);
void OnFileDownloading(ContentId cid);
void OnFileDownloaded(ByteSize size, ContentId cid);
void OnStorageContractSubmitted(StoragePurchaseContract storagePurchaseContract);
void OnStorageContractUpdated(StoragePurchase purchaseStatus);
}
}

View File

@ -1,4 +1,5 @@
using Logging;
using CodexPlugin.Hooks;
using Logging;
using Utils;
namespace CodexPlugin
@ -13,11 +14,13 @@ namespace CodexPlugin
{
private readonly ILog log;
private readonly CodexAccess codexAccess;
private readonly ICodexNodeHooks hooks;
public MarketplaceAccess(ILog log, CodexAccess codexAccess)
public MarketplaceAccess(ILog log, CodexAccess codexAccess, ICodexNodeHooks hooks)
{
this.log = log;
this.codexAccess = codexAccess;
this.hooks = hooks;
}
public IStoragePurchaseContract RequestStorage(StoragePurchaseRequest purchase)
@ -38,8 +41,11 @@ namespace CodexPlugin
Log($"Storage requested successfully. PurchaseId: '{response}'.");
var contract = new StoragePurchaseContract(log, codexAccess, response, purchase);
var contract = new StoragePurchaseContract(log, codexAccess, response, purchase, hooks);
contract.WaitForStorageContractSubmitted();
hooks.OnStorageContractSubmitted(contract);
return contract;
}
@ -50,6 +56,7 @@ namespace CodexPlugin
var response = codexAccess.SalesAvailability(availability);
Log($"Storage successfully made available. Id: {response.Id}");
hooks.OnStorageAvailabilityCreated(response);
return response.Id;
}

View File

@ -1,4 +1,5 @@
using Logging;
using CodexPlugin.Hooks;
using Logging;
using Newtonsoft.Json;
using Utils;
@ -18,19 +19,20 @@ namespace CodexPlugin
{
private readonly ILog log;
private readonly CodexAccess codexAccess;
private readonly ICodexNodeHooks hooks;
private readonly TimeSpan gracePeriod = TimeSpan.FromSeconds(30);
private readonly DateTime contractPendingUtc = DateTime.UtcNow;
private DateTime? contractSubmittedUtc = DateTime.UtcNow;
private DateTime? contractStartedUtc;
private DateTime? contractFinishedUtc;
public StoragePurchaseContract(ILog log, CodexAccess codexAccess, string purchaseId, StoragePurchaseRequest purchase)
public StoragePurchaseContract(ILog log, CodexAccess codexAccess, string purchaseId, StoragePurchaseRequest purchase, ICodexNodeHooks hooks)
{
this.log = log;
this.codexAccess = codexAccess;
PurchaseId = purchaseId;
Purchase = purchase;
this.hooks = hooks;
ContentId = new ContentId(codexAccess.GetPurchaseStatus(purchaseId).Request.Content.Cid);
}
@ -87,16 +89,17 @@ namespace CodexPlugin
Log($"Waiting for {Time.FormatDuration(timeout)} to reach state '{desiredState}'.");
while (lastState != desiredState)
{
Thread.Sleep(sleep);
var purchaseStatus = codexAccess.GetPurchaseStatus(PurchaseId);
var statusJson = JsonConvert.SerializeObject(purchaseStatus);
if (purchaseStatus != null && purchaseStatus.State != lastState)
{
lastState = purchaseStatus.State;
log.Debug("Purchase status: " + statusJson);
hooks.OnStorageContractUpdated(purchaseStatus);
}
Thread.Sleep(sleep);
if (lastState == "errored")
{
FrameworkAssert.Fail("Contract errored: " + statusJson);