From 85774847b741d69666efc08e25f455bbca3dd127 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 26 Mar 2024 11:39:59 +0100 Subject: [PATCH] Restoring marketplace types --- ProjectPlugins/CodexPlugin/CodexAccess.cs | 76 +++++-------------- ProjectPlugins/CodexPlugin/CodexNode.cs | 7 +- ProjectPlugins/CodexPlugin/CodexTypes.cs | 11 +++ ProjectPlugins/CodexPlugin/Mapper.cs | 55 +++++++++++++- .../CodexPlugin/MarketplaceTypes.cs | 53 +++---------- .../BasicTests/BlockExchangeTests.cs | 63 --------------- Tests/CodexTests/BasicTests/ExampleTests.cs | 6 +- 7 files changed, 98 insertions(+), 173 deletions(-) delete mode 100644 Tests/CodexTests/BasicTests/BlockExchangeTests.cs diff --git a/ProjectPlugins/CodexPlugin/CodexAccess.cs b/ProjectPlugins/CodexPlugin/CodexAccess.cs index 2d4084a..c4ac8c2 100644 --- a/ProjectPlugins/CodexPlugin/CodexAccess.cs +++ b/ProjectPlugins/CodexPlugin/CodexAccess.cs @@ -33,8 +33,8 @@ namespace CodexPlugin public DebugPeer GetDebugPeer(string peerId) { // Cannot use openAPI: debug/peer endpoint is not specified there. - var endoint = GetEndpoint(); - var str = endoint.HttpGetString($"debug/peer/{peerId}"); + var endpoint = GetEndpoint(); + var str = endpoint.HttpGetString($"debug/peer/{peerId}"); if (str.ToLowerInvariant() == "unable to find peer!") { @@ -44,59 +44,45 @@ namespace CodexPlugin }; } - var result = endoint.Deserialize(str); + var result = endpoint.Deserialize(str); result.IsPeerFound = true; return result; } - public CodexDebugBlockExchangeResponse GetDebugBlockExchange() - { - return Http().HttpGetJson("debug/blockexchange"); - } - - public CodexDebugRepoStoreResponse[] GetDebugRepoStore() - { - return LongHttp().HttpGetJson("debug/repostore"); - } - - public CodexDebugThresholdBreaches GetDebugThresholdBreaches() - { - return Http().HttpGetJson("debug/loop"); - } - public string UploadFile(FileStream fileStream) { - return Http().HttpPostStream("data", fileStream); + return OnCodex(api => api.UploadAsync(fileStream)); } public Stream DownloadFile(string contentId) { - return Http().HttpGetStream("data/" + contentId + "/network"); + var fileResponse = OnCodex(api => api.DownloadNetworkAsync(contentId)); + if (fileResponse.StatusCode != 200) throw new Exception("Download failed with StatusCode: " + fileResponse.StatusCode); + return fileResponse.Stream; } - public CodexLocalDataResponse[] LocalFiles() + public LocalDataset[] LocalFiles() { - return Http().HttpGetJson("data"); + return Map(OnCodex(api => api.ListDataAsync())); } - public CodexSalesAvailabilityResponse SalesAvailability(CodexSalesAvailabilityRequest request) + public StorageAvailability SalesAvailability(StorageAvailability request) { - return Http().HttpPostJson("sales/availability", request); + var body = Map(request); + var read = OnCodex(api => api.OfferStorageAsync(body)); + return Map(read); } - public string RequestStorage(CodexSalesRequestStorageRequest request, string contentId) + public string RequestStorage(StoragePurchaseRequest request) { - return Http().HttpPostJson($"storage/request/{contentId}", request); + var body = Map(request); + var read = ""; /* fix incoming*/ OnCodex(api => api.CreateStorageRequestAsync(request.ContentId.Id, body)); + return read; } - public CodexStoragePurchase GetPurchaseStatus(string purchaseId) + public StoragePurchaseRequest GetPurchaseStatus(string purchaseId) { - return Http().HttpGetJson($"storage/purchases/{purchaseId}"); - } - - public string ConnectToPeer(string peerId, string peerMultiAddress) - { - return Http().HttpGetString($"connect/{peerId}?addrs={peerMultiAddress}"); + return Map(OnCodex(api => api.GetPurchaseAsync(purchaseId))); } public string GetName() @@ -128,30 +114,6 @@ namespace CodexPlugin return result; } - //private IHttp Http() - //{ - // var address = GetAddress(); - // var api = new CodexOpenApi.CodexApi(new HttpClient()); - // api.BaseUrl = $"{address.Host}:{address.Port}/api/codex/v1"; - - // var debugInfo = Time.Wait(api.GetDebugInfoAsync()); - - // using var stream = File.OpenRead("C:\\Users\\thatb\\Desktop\\Collect\\Wallpapers\\demerui_djinn_illuminatus_fullbody_full_body_view_in_the_style__86ea9491-1fe1-44ab-8577-a3636cad1b21.png"); - // var cid = Time.Wait(api.UploadAsync(stream)); - - // var file = Time.Wait(api.DownloadNetworkAsync(cid)); - // while (file.IsPartial) Thread.Sleep(100); - // using var outfile = File.OpenWrite("C:\\Users\\thatb\\Desktop\\output.png"); - // file.Stream.CopyTo(outfile); - - // return tools.CreateHttp(GetAddress(), baseUrl: "/api/codex/v1", CheckContainerCrashed, Container.Name); - //} - - //private IHttp LongHttp() - //{ - // return tools.CreateHttp(GetAddress(), baseUrl: "/api/codex/v1", CheckContainerCrashed, new LongTimeSet(), Container.Name); - //} - private IEndpoint GetEndpoint() { return tools diff --git a/ProjectPlugins/CodexPlugin/CodexNode.cs b/ProjectPlugins/CodexPlugin/CodexNode.cs index 6698d38..244d931 100644 --- a/ProjectPlugins/CodexPlugin/CodexNode.cs +++ b/ProjectPlugins/CodexPlugin/CodexNode.cs @@ -3,6 +3,7 @@ using FileUtils; using GethPlugin; using KubernetesWorkflow; using KubernetesWorkflow.Types; +using Logging; using MetricsPlugin; using Utils; @@ -121,11 +122,7 @@ namespace CodexPlugin public LocalDataset[] LocalFiles() { - return CodexAccess.LocalFiles().Select(l => new LocalDataset() - { - Cid = new ContentId(l.cid) - //, l.manifest - }).ToArray(); + return CodexAccess.LocalFiles(); } public void ConnectToPeer(ICodexNode node) diff --git a/ProjectPlugins/CodexPlugin/CodexTypes.cs b/ProjectPlugins/CodexPlugin/CodexTypes.cs index 06dfc86..813c85c 100644 --- a/ProjectPlugins/CodexPlugin/CodexTypes.cs +++ b/ProjectPlugins/CodexPlugin/CodexTypes.cs @@ -55,6 +55,17 @@ namespace CodexPlugin public ContentId Cid { get; set; } = new ContentId(); } + public class SalesRequestStorageRequest + { + public string Duration { get; set; } = string.Empty; + public string ProofProbability { get; set; } = string.Empty; + public string Reward { get; set; } = string.Empty; + public string Collateral { get; set; } = string.Empty; + public string? Expiry { get; set; } + public uint? Nodes { get; set; } + public uint? Tolerance { get; set; } + } + public class ContentId { public ContentId() diff --git a/ProjectPlugins/CodexPlugin/Mapper.cs b/ProjectPlugins/CodexPlugin/Mapper.cs index 7616a3c..66f1393 100644 --- a/ProjectPlugins/CodexPlugin/Mapper.cs +++ b/ProjectPlugins/CodexPlugin/Mapper.cs @@ -1,4 +1,6 @@ -using Newtonsoft.Json.Linq; +using CodexContractsPlugin; +using Newtonsoft.Json.Linq; +using System.Numerics; namespace CodexPlugin { @@ -17,6 +19,45 @@ namespace CodexPlugin }; } + public LocalDataset[] Map(ICollection dataList) + { + return Array.Empty(); + } + + public CodexOpenApi.SalesAvailabilityCREATE Map(StorageAvailability availability) + { + return new CodexOpenApi.SalesAvailabilityCREATE + { + Duration = ToDecInt(availability.MaxDuration.TotalSeconds), + MinPrice = ToDecInt(availability.MinPriceForTotalSpace), + MaxCollateral = ToDecInt(availability.MaxCollateral), + TotalSize = ToDecInt(availability.TotalSpace.SizeInBytes) + }; + } + + public CodexOpenApi.StorageRequestCreation Map(StoragePurchaseRequest purchase) + { + return new CodexOpenApi.StorageRequestCreation + { + Duration = ToDecInt(purchase.Duration.TotalSeconds), + ProofProbability = ToDecInt(purchase.ProofProbability), + Reward = ToDecInt(purchase.PricePerSlotPerSecond), + Collateral = ToDecInt(purchase.RequiredCollateral), + Expiry = ToDecInt(DateTimeOffset.UtcNow.ToUnixTimeSeconds() + purchase.Expiry.TotalSeconds), + Nodes = purchase.MinRequiredNumberOfNodes, + Tolerance = purchase.NodeFailureTolerance + }; + } + + public StoragePurchase Map(CodexOpenApi.Purchase purchase) + { + return new StoragePurchase + { + State = purchase.State, + Error = purchase.Error + }; + } + private DebugInfoVersion MapDebugInfoVersion(JObject obj) { return new DebugInfoVersion @@ -78,5 +119,17 @@ namespace CodexPlugin } return false; } + + private string ToDecInt(double d) + { + var i = new BigInteger(d); + return i.ToString("D"); + } + + private string ToDecInt(TestToken t) + { + var i = new BigInteger(t.Amount); + return i.ToString("D"); + } } } diff --git a/ProjectPlugins/CodexPlugin/MarketplaceTypes.cs b/ProjectPlugins/CodexPlugin/MarketplaceTypes.cs index dc7f7e5..0adcc8f 100644 --- a/ProjectPlugins/CodexPlugin/MarketplaceTypes.cs +++ b/ProjectPlugins/CodexPlugin/MarketplaceTypes.cs @@ -1,13 +1,12 @@ using CodexContractsPlugin; using Logging; -using System.Numerics; using Utils; namespace CodexPlugin { - public class StoragePurchase : MarketplaceType + public class StoragePurchaseRequest { - public StoragePurchase(ContentId cid) + public StoragePurchaseRequest(ContentId cid) { ContentId = cid; } @@ -21,20 +20,6 @@ namespace CodexPlugin public TimeSpan Duration { get; set; } public TimeSpan Expiry { get; set; } - public CodexSalesRequestStorageRequest ToApiRequest() - { - return new CodexSalesRequestStorageRequest - { - duration = ToDecInt(Duration.TotalSeconds), - proofProbability = ToDecInt(ProofProbability), - reward = ToDecInt(PricePerSlotPerSecond), - collateral = ToDecInt(RequiredCollateral), - expiry = ToDecInt(DateTimeOffset.UtcNow.ToUnixTimeSeconds() + Expiry.TotalSeconds), - nodes = MinRequiredNumberOfNodes, - tolerance = NodeFailureTolerance - }; - } - public void Log(ILog log) { log.Log($"Requesting storage for: {ContentId.Id}... (" + @@ -48,7 +33,13 @@ namespace CodexPlugin } } - public class StorageAvailability : MarketplaceType + public class StoragePurchase + { + public string State { get; set; } = string.Empty; + public string Error { get; set; } = string.Empty; + } + + public class StorageAvailability { public StorageAvailability(ByteSize totalSpace, TimeSpan maxDuration, TestToken minPriceForTotalSpace, TestToken maxCollateral) { @@ -63,17 +54,6 @@ namespace CodexPlugin public TestToken MinPriceForTotalSpace { get; } public TestToken MaxCollateral { get; } - public CodexSalesAvailabilityRequest ToApiRequest() - { - return new CodexSalesAvailabilityRequest - { - totalSize = ToDecInt(TotalSpace.SizeInBytes), - duration = ToDecInt(MaxDuration.TotalSeconds), - maxCollateral = ToDecInt(MaxCollateral), - minPrice = ToDecInt(MinPriceForTotalSpace) - }; - } - public void Log(ILog log) { log.Log($"Making storage available... (" + @@ -83,19 +63,4 @@ namespace CodexPlugin $"maxCollateral: {MaxCollateral})"); } } - - public abstract class MarketplaceType - { - protected string ToDecInt(double d) - { - var i = new BigInteger(d); - return i.ToString("D"); - } - - protected string ToDecInt(TestToken t) - { - var i = new BigInteger(t.Amount); - return i.ToString("D"); - } - } } diff --git a/Tests/CodexTests/BasicTests/BlockExchangeTests.cs b/Tests/CodexTests/BasicTests/BlockExchangeTests.cs deleted file mode 100644 index 83f67d4..0000000 --- a/Tests/CodexTests/BasicTests/BlockExchangeTests.cs +++ /dev/null @@ -1,63 +0,0 @@ -using CodexPlugin; -using NUnit.Framework; -using Utils; - -namespace CodexTests.BasicTests -{ - [TestFixture] - public class BlockExchangeTests : CodexDistTest - { - [Test] - public void EmptyAfterExchange() - { - var bootstrap = AddCodex(s => s.WithName("bootstrap")); - var node = AddCodex(s => s.WithName("node").WithBootstrapNode(bootstrap)); - - AssertExchangeIsEmpty(bootstrap, node); - - var file = GenerateTestFile(1.MB()); - var cid = bootstrap.UploadFile(file); - node.DownloadContent(cid); - - AssertExchangeIsEmpty(bootstrap, node); - } - - [Test] - public void EmptyAfterExchangeWithBystander() - { - var bootstrap = AddCodex(s => s.WithName("bootstrap")); - var node = AddCodex(s => s.WithName("node").WithBootstrapNode(bootstrap)); - var bystander = AddCodex(s => s.WithName("bystander").WithBootstrapNode(bootstrap)); - - AssertExchangeIsEmpty(bootstrap, node, bystander); - - var file = GenerateTestFile(1.MB()); - var cid = bootstrap.UploadFile(file); - node.DownloadContent(cid); - - AssertExchangeIsEmpty(bootstrap, node, bystander); - } - - private void AssertExchangeIsEmpty(params ICodexNode[] nodes) - { - foreach (var node in nodes) - { - // API Call not available in master-line Codex image. - //Time.Retry(() => AssertBlockExchangeIsEmpty(node), nameof(AssertExchangeIsEmpty)); - } - } - - //private void AssertBlockExchangeIsEmpty(ICodexNode node) - //{ - // var msg = $"BlockExchange for {node.GetName()}: "; - // var response = node.GetDebugBlockExchange(); - // foreach (var peer in response.peers) - // { - // var activeWants = peer.wants.Where(w => !w.cancel).ToArray(); - // Assert.That(activeWants.Length, Is.EqualTo(0), msg + "thinks a peer has active wants."); - // } - // Assert.That(response.taskQueue, Is.EqualTo(0), msg + "has tasks in queue."); - // Assert.That(response.pendingBlocks, Is.EqualTo(0), msg + "has pending blocks."); - //} - } -} diff --git a/Tests/CodexTests/BasicTests/ExampleTests.cs b/Tests/CodexTests/BasicTests/ExampleTests.cs index 4973e2a..3250668 100644 --- a/Tests/CodexTests/BasicTests/ExampleTests.cs +++ b/Tests/CodexTests/BasicTests/ExampleTests.cs @@ -89,7 +89,7 @@ namespace CodexTests.BasicTests var contentId = buyer.UploadFile(testFile); - var purchase = new StoragePurchase(contentId) + var purchase = new StoragePurchaseRequest(contentId) { PricePerSlotPerSecond = 2.TestTokens(), RequiredCollateral = 10.TestTokens(), @@ -135,7 +135,7 @@ namespace CodexTests.BasicTests Assert.That(discN, Is.LessThan(bootN)); } - private void AssertSlotFilledEvents(ICodexContracts contracts, StoragePurchase purchase, Request request, ICodexNode seller) + private void AssertSlotFilledEvents(ICodexContracts contracts, StoragePurchaseRequest purchase, Request request, ICodexNode seller) { // Expect 1 fulfilled event for the purchase. var requestFulfilledEvents = contracts.GetRequestFulfilledEvents(GetTestRunTimeRange()); @@ -153,7 +153,7 @@ namespace CodexTests.BasicTests } } - private void AssertStorageRequest(Request request, StoragePurchase purchase, ICodexContracts contracts, ICodexNode buyer) + private void AssertStorageRequest(Request request, StoragePurchaseRequest purchase, ICodexContracts contracts, ICodexNode buyer) { Assert.That(contracts.GetRequestState(request), Is.EqualTo(RequestState.Started)); Assert.That(request.ClientAddress, Is.EqualTo(buyer.EthAddress));