diff --git a/ProjectPlugins/CodexPlugin/Mapper.cs b/ProjectPlugins/CodexPlugin/Mapper.cs index 6e59c68..c175ae1 100644 --- a/ProjectPlugins/CodexPlugin/Mapper.cs +++ b/ProjectPlugins/CodexPlugin/Mapper.cs @@ -63,14 +63,47 @@ namespace CodexPlugin }; } - public StoragePurchase Map(CodexOpenApi.Purchase purchase) - { - return new StoragePurchase - { - State = purchase.State, - Error = purchase.Error - }; - } + // TODO: Fix openapi spec for this call. + //public StoragePurchase Map(CodexOpenApi.Purchase purchase) + //{ + // return new StoragePurchase(Map(purchase.Request)) + // { + // State = purchase.State, + // Error = purchase.Error + // }; + //} + + //public StorageRequest Map(CodexOpenApi.StorageRequest request) + //{ + // return new StorageRequest(Map(request.Ask), Map(request.Content)) + // { + // Id = request.Id, + // Client = request.Client, + // Expiry = TimeSpan.FromSeconds(Convert.ToInt64(request.Expiry)), + // Nonce = request.Nonce + // }; + //} + + //public StorageAsk Map(CodexOpenApi.StorageAsk ask) + //{ + // return new StorageAsk + // { + // Duration = TimeSpan.FromSeconds(Convert.ToInt64(ask.Duration)), + // MaxSlotLoss = ask.MaxSlotLoss, + // ProofProbability = ask.ProofProbability, + // Reward = Convert.ToDecimal(ask.Reward).TstWei(), + // Slots = ask.Slots, + // SlotSize = new ByteSize(Convert.ToInt64(ask.SlotSize)) + // }; + //} + + //public StorageContent Map(CodexOpenApi.Content content) + //{ + // return new StorageContent + // { + // Cid = content.Cid + // }; + //} public StorageAvailability Map(CodexOpenApi.SalesAvailabilityREAD read) { diff --git a/ProjectPlugins/CodexPlugin/MarketplaceAccess.cs b/ProjectPlugins/CodexPlugin/MarketplaceAccess.cs index 8944d95..6f5e47e 100644 --- a/ProjectPlugins/CodexPlugin/MarketplaceAccess.cs +++ b/ProjectPlugins/CodexPlugin/MarketplaceAccess.cs @@ -97,10 +97,13 @@ namespace CodexPlugin this.codexAccess = codexAccess; PurchaseId = purchaseId; Purchase = purchase; + + ContentId = new ContentId(codexAccess.GetPurchaseStatus(purchaseId).Request.Content.Cid); } public string PurchaseId { get; } public StoragePurchaseRequest Purchase { get; } + public ContentId ContentId { get; } public TimeSpan? PendingToSubmitted => contractSubmittedUtc - contractPendingUtc; public TimeSpan? SubmittedToStarted => contractStartedUtc - contractSubmittedUtc; diff --git a/ProjectPlugins/CodexPlugin/MarketplaceTypes.cs b/ProjectPlugins/CodexPlugin/MarketplaceTypes.cs index caeda25..21aa86a 100644 --- a/ProjectPlugins/CodexPlugin/MarketplaceTypes.cs +++ b/ProjectPlugins/CodexPlugin/MarketplaceTypes.cs @@ -1,5 +1,7 @@ using CodexContractsPlugin; +using CodexOpenApi; using Logging; +using System.Data; using Utils; namespace CodexPlugin @@ -37,6 +39,34 @@ namespace CodexPlugin { public string State { get; set; } = string.Empty; public string Error { get; set; } = string.Empty; + public StorageRequest Request { get; set; } = null!; + } + + public class StorageRequest + { + public string Id { get; set; } = string.Empty; + public string Client { get; set; } = string.Empty; + public StorageAsk Ask { get; set; } = null!; + public StorageContent Content { get; set; } = null!; + public string Expiry { get; set; } = string.Empty; + public string Nonce { get; set; } = string.Empty; + } + + public class StorageAsk + { + public int Slots { get; set; } + public string SlotSize { get; set; } = string.Empty; + public string Duration { get; set; } = string.Empty; + public string ProofProbability { get; set; } = string.Empty; + public string Reward { get; set; } = string.Empty; + public int MaxSlotLoss { get; set; } + } + + public class StorageContent + { + public string Cid { get; set; } = string.Empty; + //public ErasureParameters Erasure { get; set; } + //public PoRParameters Por { get; set; } } public class StorageAvailability diff --git a/Tests/CodexTests/BasicTests/MarketplaceTests.cs b/Tests/CodexTests/BasicTests/MarketplaceTests.cs index 5d69387..493d18c 100644 --- a/Tests/CodexTests/BasicTests/MarketplaceTests.cs +++ b/Tests/CodexTests/BasicTests/MarketplaceTests.cs @@ -85,6 +85,47 @@ namespace CodexTests.BasicTests Assert.That(contracts.GetRequestState(request), Is.EqualTo(RequestState.Finished)); } + [Test] + public void CanDownloadContentFromContractCid() + { + var fileSize = 10.MB(); + var geth = Ci.StartGethNode(s => s.IsMiner().WithName("disttest-geth")); + var contracts = Ci.StartCodexContracts(geth); + var testFile = GenerateTestFile(fileSize); + + var client = StartCodex(s => s + .WithName("Client") + .EnableMarketplace(geth, contracts, m => m + .WithInitial(10.Eth(), 10.Tst()))); + + var uploadCid = client.UploadFile(testFile); + + var purchase = new StoragePurchaseRequest(uploadCid) + { + PricePerSlotPerSecond = 2.TstWei(), + RequiredCollateral = 10.TstWei(), + MinRequiredNumberOfNodes = 5, + NodeFailureTolerance = 2, + ProofProbability = 5, + Duration = TimeSpan.FromMinutes(5), + Expiry = TimeSpan.FromMinutes(4) + }; + + var purchaseContract = client.Marketplace.RequestStorage(purchase); + + var contractCid = purchaseContract.ContentId; + + Assert.That(uploadCid.Id, Is.Not.EqualTo(contractCid.Id)); + + var downloader = StartCodex(s => s.WithName("Downloader")); + + var uploadedFile = downloader.DownloadContent(uploadCid); + testFile.AssertIsEqual(uploadedFile); + + var contractFile = downloader.DownloadContent(contractCid); + testFile.AssertIsEqual(contractFile); + } + private void WaitForAllSlotFilledEvents(ICodexContracts contracts, StoragePurchaseRequest purchase, IGethNode geth) { Time.Retry(() =>