From 09d2f418ebc67c48a3dc7a3fe541e4b16078a296 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 20 Aug 2024 11:44:15 +0200 Subject: [PATCH] Setting up marketplace hooks --- Framework/Utils/RandomUtils.cs | 11 +++++++++++ ProjectPlugins/CodexPlugin/CodexNode.cs | 2 +- ProjectPlugins/CodexPlugin/Hooks/CodexNodeHooks.cs | 7 +++++-- ProjectPlugins/CodexPlugin/MarketplaceAccess.cs | 13 ++++++++++--- .../CodexPlugin/StoragePurchaseContract.cs | 13 ++++++++----- 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/Framework/Utils/RandomUtils.cs b/Framework/Utils/RandomUtils.cs index 0cf0132..acece43 100644 --- a/Framework/Utils/RandomUtils.cs +++ b/Framework/Utils/RandomUtils.cs @@ -11,5 +11,16 @@ remainingItems.RemoveAt(i); return result; } + + public static T[] Shuffled(T[] items) + { + var result = new List(); + var source = items.ToList(); + while (source.Any()) + { + result.Add(RandomUtils.PickOneRandom(source)); + } + return result.ToArray(); + } } } diff --git a/ProjectPlugins/CodexPlugin/CodexNode.cs b/ProjectPlugins/CodexPlugin/CodexNode.cs index 0f8bcfd..13e2276 100644 --- a/ProjectPlugins/CodexPlugin/CodexNode.cs +++ b/ProjectPlugins/CodexPlugin/CodexNode.cs @@ -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() diff --git a/ProjectPlugins/CodexPlugin/Hooks/CodexNodeHooks.cs b/ProjectPlugins/CodexPlugin/Hooks/CodexNodeHooks.cs index b18dd5d..d7c3ff7 100644 --- a/ProjectPlugins/CodexPlugin/Hooks/CodexNodeHooks.cs +++ b/ProjectPlugins/CodexPlugin/Hooks/CodexNodeHooks.cs @@ -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); } } diff --git a/ProjectPlugins/CodexPlugin/MarketplaceAccess.cs b/ProjectPlugins/CodexPlugin/MarketplaceAccess.cs index dded835..004450c 100644 --- a/ProjectPlugins/CodexPlugin/MarketplaceAccess.cs +++ b/ProjectPlugins/CodexPlugin/MarketplaceAccess.cs @@ -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; } diff --git a/ProjectPlugins/CodexPlugin/StoragePurchaseContract.cs b/ProjectPlugins/CodexPlugin/StoragePurchaseContract.cs index 3446231..61f6f59 100644 --- a/ProjectPlugins/CodexPlugin/StoragePurchaseContract.cs +++ b/ProjectPlugins/CodexPlugin/StoragePurchaseContract.cs @@ -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);