Restoring marketplace types

This commit is contained in:
Ben 2024-03-26 11:39:59 +01:00
parent 8fcf351613
commit 85774847b7
No known key found for this signature in database
GPG Key ID: 541B9D8C9F1426A1
7 changed files with 98 additions and 173 deletions

View File

@ -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<DebugPeer>(str);
var result = endpoint.Deserialize<DebugPeer>(str);
result.IsPeerFound = true;
return result;
}
public CodexDebugBlockExchangeResponse GetDebugBlockExchange()
{
return Http().HttpGetJson<CodexDebugBlockExchangeResponse>("debug/blockexchange");
}
public CodexDebugRepoStoreResponse[] GetDebugRepoStore()
{
return LongHttp().HttpGetJson<CodexDebugRepoStoreResponse[]>("debug/repostore");
}
public CodexDebugThresholdBreaches GetDebugThresholdBreaches()
{
return Http().HttpGetJson<CodexDebugThresholdBreaches>("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<CodexLocalDataResponse[]>("data");
return Map(OnCodex(api => api.ListDataAsync()));
}
public CodexSalesAvailabilityResponse SalesAvailability(CodexSalesAvailabilityRequest request)
public StorageAvailability SalesAvailability(StorageAvailability request)
{
return Http().HttpPostJson<CodexSalesAvailabilityRequest, CodexSalesAvailabilityResponse>("sales/availability", request);
var body = Map(request);
var read = OnCodex<SalesAvailabilityREAD>(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<string>(api => api.CreateStorageRequestAsync(request.ContentId.Id, body));
return read;
}
public CodexStoragePurchase GetPurchaseStatus(string purchaseId)
public StoragePurchaseRequest GetPurchaseStatus(string purchaseId)
{
return Http().HttpGetJson<CodexStoragePurchase>($"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

View File

@ -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)

View File

@ -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()

View File

@ -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<CodexOpenApi.DataList> dataList)
{
return Array.Empty<LocalDataset>();
}
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");
}
}
}

View File

@ -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");
}
}
}

View File

@ -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.");
//}
}
}

View File

@ -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));