2023-09-15 14:27:08 +00:00
|
|
|
|
using CodexContractsPlugin;
|
|
|
|
|
using CodexPlugin;
|
2023-09-12 13:43:30 +00:00
|
|
|
|
using DistTestCore;
|
2023-09-15 13:52:02 +00:00
|
|
|
|
using GethPlugin;
|
2023-09-13 09:25:08 +00:00
|
|
|
|
using MetricsPlugin;
|
2023-09-12 13:43:30 +00:00
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Utils;
|
|
|
|
|
|
|
|
|
|
namespace Tests.BasicTests
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
2023-09-19 14:22:07 +00:00
|
|
|
|
public class ExampleTests : CodexDistTest
|
2023-09-12 13:43:30 +00:00
|
|
|
|
{
|
|
|
|
|
[Test]
|
|
|
|
|
public void CodexLogExample()
|
|
|
|
|
{
|
2023-09-19 14:22:07 +00:00
|
|
|
|
var primary = AddCodex();
|
2023-09-12 13:43:30 +00:00
|
|
|
|
|
|
|
|
|
primary.UploadFile(GenerateTestFile(5.MB()));
|
|
|
|
|
|
2023-09-14 13:26:46 +00:00
|
|
|
|
var log = Ci.DownloadLog(primary);
|
2023-09-12 13:43:30 +00:00
|
|
|
|
|
|
|
|
|
log.AssertLogContains("Uploaded file");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TwoMetricsExample()
|
|
|
|
|
{
|
2023-09-19 14:22:07 +00:00
|
|
|
|
var group = AddCodex(2, s => s.EnableMetrics());
|
|
|
|
|
var group2 = AddCodex(2, s => s.EnableMetrics());
|
2023-09-13 09:25:08 +00:00
|
|
|
|
|
2023-09-13 09:59:21 +00:00
|
|
|
|
var primary = group[0];
|
|
|
|
|
var secondary = group[1];
|
|
|
|
|
var primary2 = group2[0];
|
|
|
|
|
var secondary2 = group2[1];
|
2023-09-12 13:43:30 +00:00
|
|
|
|
|
2023-09-19 09:51:59 +00:00
|
|
|
|
var metrics = Ci.GetMetricsFor(primary, primary2);
|
2023-09-12 13:43:30 +00:00
|
|
|
|
|
2023-09-13 09:59:21 +00:00
|
|
|
|
primary.ConnectToPeer(secondary);
|
|
|
|
|
primary2.ConnectToPeer(secondary2);
|
2023-09-12 13:43:30 +00:00
|
|
|
|
|
2023-09-13 09:59:21 +00:00
|
|
|
|
Thread.Sleep(TimeSpan.FromMinutes(2));
|
2023-09-12 13:43:30 +00:00
|
|
|
|
|
2023-09-13 09:59:21 +00:00
|
|
|
|
metrics[0].AssertThat("libp2p_peers", Is.EqualTo(1));
|
|
|
|
|
metrics[1].AssertThat("libp2p_peers", Is.EqualTo(1));
|
2023-09-12 13:43:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void MarketplaceExample()
|
|
|
|
|
{
|
2023-09-19 14:22:07 +00:00
|
|
|
|
var sellerInitialBalance = 234.TestTokens();
|
|
|
|
|
var buyerInitialBalance = 1000.TestTokens();
|
|
|
|
|
var fileSize = 10.MB();
|
2023-09-15 13:52:02 +00:00
|
|
|
|
|
2023-09-19 14:22:07 +00:00
|
|
|
|
var geth = Ci.StartGethNode(s => s.IsMiner().WithName("disttest-geth"));
|
2023-09-15 14:27:08 +00:00
|
|
|
|
var contracts = Ci.DeployCodexContracts(geth);
|
|
|
|
|
|
2023-09-19 14:22:07 +00:00
|
|
|
|
var seller = AddCodex(s => s
|
|
|
|
|
.WithStorageQuota(11.GB())
|
|
|
|
|
.EnableMarketplace(geth, contracts));
|
|
|
|
|
geth.SendEth(seller, 10.Eth());
|
|
|
|
|
contracts.MintTestTokens(geth, seller, sellerInitialBalance);
|
|
|
|
|
|
|
|
|
|
AssertBalance(geth, contracts, seller, Is.EqualTo(sellerInitialBalance));
|
|
|
|
|
seller.Marketplace.MakeStorageAvailable(
|
|
|
|
|
size: 10.GB(),
|
|
|
|
|
minPricePerBytePerSecond: 1.TestTokens(),
|
|
|
|
|
maxCollateral: 20.TestTokens(),
|
|
|
|
|
maxDuration: TimeSpan.FromMinutes(3));
|
2023-09-12 13:43:30 +00:00
|
|
|
|
|
2023-09-19 14:22:07 +00:00
|
|
|
|
var testFile = GenerateTestFile(fileSize);
|
2023-09-12 13:43:30 +00:00
|
|
|
|
|
2023-09-19 14:22:07 +00:00
|
|
|
|
var buyer = AddCodex(s => s
|
|
|
|
|
.WithBootstrapNode(seller)
|
|
|
|
|
.EnableMarketplace(geth, contracts));
|
|
|
|
|
geth.SendEth(seller, 10.Eth());
|
|
|
|
|
contracts.MintTestTokens(geth, seller, buyerInitialBalance);
|
2023-09-12 13:43:30 +00:00
|
|
|
|
|
2023-09-19 14:22:07 +00:00
|
|
|
|
AssertBalance(geth, contracts, buyer, Is.EqualTo(buyerInitialBalance));
|
2023-09-12 13:43:30 +00:00
|
|
|
|
|
2023-09-19 14:22:07 +00:00
|
|
|
|
var contentId = buyer.UploadFile(testFile);
|
|
|
|
|
var purchaseContract = buyer.Marketplace.RequestStorage(contentId,
|
|
|
|
|
pricePerSlotPerSecond: 2.TestTokens(),
|
|
|
|
|
requiredCollateral: 10.TestTokens(),
|
|
|
|
|
minRequiredNumberOfNodes: 1,
|
|
|
|
|
proofProbability: 5,
|
|
|
|
|
duration: TimeSpan.FromMinutes(1));
|
2023-09-12 13:43:30 +00:00
|
|
|
|
|
2023-09-19 14:22:07 +00:00
|
|
|
|
purchaseContract.WaitForStorageContractStarted(fileSize);
|
2023-09-12 13:43:30 +00:00
|
|
|
|
|
2023-09-19 14:22:07 +00:00
|
|
|
|
AssertBalance(geth, contracts, seller, Is.LessThan(sellerInitialBalance), "Collateral was not placed.");
|
2023-09-12 13:43:30 +00:00
|
|
|
|
|
2023-09-19 14:22:07 +00:00
|
|
|
|
purchaseContract.WaitForStorageContractFinished();
|
2023-09-12 13:43:30 +00:00
|
|
|
|
|
2023-09-19 14:22:07 +00:00
|
|
|
|
AssertBalance(geth, contracts, seller, Is.GreaterThan(sellerInitialBalance), "Seller was not paid for storage.");
|
|
|
|
|
AssertBalance(geth, contracts, buyer, Is.LessThan(buyerInitialBalance), "Buyer was not charged for storage.");
|
2023-09-12 13:43:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|