2023-04-13 07:33:10 +00:00
|
|
|
|
using DistTestCore;
|
2023-07-31 07:16:52 +00:00
|
|
|
|
using DistTestCore.Codex;
|
|
|
|
|
using DistTestCore.Marketplace;
|
|
|
|
|
using Newtonsoft.Json;
|
2023-03-17 10:43:29 +00:00
|
|
|
|
using NUnit.Framework;
|
2023-04-18 13:33:12 +00:00
|
|
|
|
using Utils;
|
2023-03-17 10:43:29 +00:00
|
|
|
|
|
2023-03-21 12:20:21 +00:00
|
|
|
|
namespace Tests.BasicTests
|
2023-03-17 10:43:29 +00:00
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
2023-04-14 12:53:39 +00:00
|
|
|
|
public class ExampleTests : DistTest
|
2023-03-17 10:43:29 +00:00
|
|
|
|
{
|
2023-04-13 09:30:19 +00:00
|
|
|
|
[Test]
|
|
|
|
|
public void CodexLogExample()
|
|
|
|
|
{
|
2023-05-31 11:34:12 +00:00
|
|
|
|
var primary = SetupCodexNode();
|
2023-04-13 09:30:19 +00:00
|
|
|
|
|
|
|
|
|
primary.UploadFile(GenerateTestFile(5.MB()));
|
|
|
|
|
|
|
|
|
|
var log = primary.DownloadLog();
|
|
|
|
|
|
|
|
|
|
log.AssertLogContains("Uploaded file");
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-13 12:36:17 +00:00
|
|
|
|
[Test]
|
|
|
|
|
public void TwoMetricsExample()
|
|
|
|
|
{
|
2023-04-25 10:52:11 +00:00
|
|
|
|
var group = SetupCodexNodes(2, s => s.EnableMetrics());
|
|
|
|
|
var group2 = SetupCodexNodes(2, s => s.EnableMetrics());
|
2023-03-22 13:49:01 +00:00
|
|
|
|
|
2023-04-13 12:36:17 +00:00
|
|
|
|
var primary = group[0];
|
|
|
|
|
var secondary = group[1];
|
|
|
|
|
var primary2 = group2[0];
|
|
|
|
|
var secondary2 = group2[1];
|
2023-03-27 08:27:08 +00:00
|
|
|
|
|
2023-04-13 12:36:17 +00:00
|
|
|
|
primary.ConnectToPeer(secondary);
|
|
|
|
|
primary2.ConnectToPeer(secondary2);
|
2023-03-31 06:39:24 +00:00
|
|
|
|
|
2023-04-19 08:42:08 +00:00
|
|
|
|
Thread.Sleep(TimeSpan.FromMinutes(2));
|
2023-03-31 06:39:24 +00:00
|
|
|
|
|
2023-04-13 12:36:17 +00:00
|
|
|
|
primary.Metrics.AssertThat("libp2p_peers", Is.EqualTo(1));
|
|
|
|
|
primary2.Metrics.AssertThat("libp2p_peers", Is.EqualTo(1));
|
|
|
|
|
}
|
2023-03-31 06:39:24 +00:00
|
|
|
|
|
2023-04-14 10:37:05 +00:00
|
|
|
|
[Test]
|
|
|
|
|
public void MarketplaceExample()
|
|
|
|
|
{
|
2023-04-26 09:12:33 +00:00
|
|
|
|
var sellerInitialBalance = 234.TestTokens();
|
|
|
|
|
var buyerInitialBalance = 1000.TestTokens();
|
2023-04-14 10:37:05 +00:00
|
|
|
|
|
2023-04-26 09:12:33 +00:00
|
|
|
|
var seller = SetupCodexNode(s => s
|
|
|
|
|
.WithStorageQuota(11.GB())
|
|
|
|
|
.EnableMarketplace(sellerInitialBalance));
|
2023-04-18 13:33:12 +00:00
|
|
|
|
|
2023-04-26 09:12:33 +00:00
|
|
|
|
seller.Marketplace.AssertThatBalance(Is.EqualTo(sellerInitialBalance));
|
|
|
|
|
seller.Marketplace.MakeStorageAvailable(
|
2023-04-18 13:33:12 +00:00
|
|
|
|
size: 10.GB(),
|
|
|
|
|
minPricePerBytePerSecond: 1.TestTokens(),
|
|
|
|
|
maxCollateral: 20.TestTokens(),
|
|
|
|
|
maxDuration: TimeSpan.FromMinutes(3));
|
2023-04-14 10:37:05 +00:00
|
|
|
|
|
2023-04-18 13:33:12 +00:00
|
|
|
|
var testFile = GenerateTestFile(10.MB());
|
2023-04-14 10:37:05 +00:00
|
|
|
|
|
2023-04-26 09:12:33 +00:00
|
|
|
|
var buyer = SetupCodexNode(s => s
|
|
|
|
|
.WithBootstrapNode(seller)
|
|
|
|
|
.EnableMarketplace(buyerInitialBalance));
|
2023-07-31 07:16:52 +00:00
|
|
|
|
|
|
|
|
|
buyer.Marketplace.AssertThatBalance(Is.EqualTo(buyerInitialBalance));
|
2023-04-26 09:12:33 +00:00
|
|
|
|
|
|
|
|
|
var contentId = buyer.UploadFile(testFile);
|
2023-07-31 07:16:52 +00:00
|
|
|
|
var purchaseId = buyer.Marketplace.RequestStorage(contentId,
|
2023-06-27 13:28:00 +00:00
|
|
|
|
pricePerSlotPerSecond: 2.TestTokens(),
|
2023-04-18 13:33:12 +00:00
|
|
|
|
requiredCollateral: 10.TestTokens(),
|
|
|
|
|
minRequiredNumberOfNodes: 1,
|
|
|
|
|
proofProbability: 5,
|
2023-05-03 08:21:15 +00:00
|
|
|
|
duration: TimeSpan.FromMinutes(1));
|
2023-04-14 10:37:05 +00:00
|
|
|
|
|
2023-07-31 07:16:52 +00:00
|
|
|
|
|
|
|
|
|
WaitForContractToStart(buyer.Marketplace, 10.MB(), purchaseId);
|
2023-04-14 10:37:05 +00:00
|
|
|
|
|
2023-04-26 09:12:33 +00:00
|
|
|
|
seller.Marketplace.AssertThatBalance(Is.LessThan(sellerInitialBalance), "Collateral was not placed.");
|
2023-04-14 10:37:05 +00:00
|
|
|
|
|
2023-07-31 07:16:52 +00:00
|
|
|
|
Time.Sleep(TimeSpan.FromMinutes(2));
|
2023-04-19 09:23:11 +00:00
|
|
|
|
|
2023-04-26 09:12:33 +00:00
|
|
|
|
seller.Marketplace.AssertThatBalance(Is.GreaterThan(sellerInitialBalance), "Seller was not paid for storage.");
|
|
|
|
|
buyer.Marketplace.AssertThatBalance(Is.LessThan(buyerInitialBalance), "Buyer was not charged for storage.");
|
2023-04-14 10:37:05 +00:00
|
|
|
|
}
|
2023-07-31 07:16:52 +00:00
|
|
|
|
|
|
|
|
|
private void WaitForContractToStart(IMarketplaceAccess access, ByteSize fileSize, string purchaseId)
|
|
|
|
|
{
|
|
|
|
|
var lastState = "";
|
|
|
|
|
var waitStart = DateTime.UtcNow;
|
|
|
|
|
var filesizeInMb = fileSize.SizeInBytes / (1024 * 1024);
|
|
|
|
|
var maxWaitTime = TimeSpan.FromSeconds(filesizeInMb * 10.0);
|
|
|
|
|
|
|
|
|
|
Log($"{nameof(WaitForContractToStart)} for {Time.FormatDuration(maxWaitTime)}");
|
|
|
|
|
while (lastState != "started")
|
|
|
|
|
{
|
|
|
|
|
var purchaseStatus = access.GetPurchaseStatus(purchaseId);
|
|
|
|
|
var statusJson = JsonConvert.SerializeObject(purchaseStatus);
|
|
|
|
|
if (purchaseStatus != null && purchaseStatus.state != lastState)
|
|
|
|
|
{
|
|
|
|
|
lastState = purchaseStatus.state;
|
|
|
|
|
Log("Purchase status: " + statusJson);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Thread.Sleep(2000);
|
|
|
|
|
|
|
|
|
|
if (lastState == "errored")
|
|
|
|
|
{
|
|
|
|
|
Assert.Fail("Contract start failed: " + statusJson);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (DateTime.UtcNow - waitStart > maxWaitTime)
|
|
|
|
|
{
|
|
|
|
|
Assert.Fail($"Contract was not picked up within {maxWaitTime.TotalSeconds} seconds timeout: {statusJson}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Log("Contract started.");
|
|
|
|
|
}
|
2023-03-17 10:43:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|