2023-09-15 14:27:08 +00:00
|
|
|
|
using CodexContractsPlugin;
|
2023-10-07 07:48:12 +00:00
|
|
|
|
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-12-20 12:21:53 +00:00
|
|
|
|
using Nethereum.Hex.HexConvertors.Extensions;
|
2023-09-12 13:43:30 +00:00
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Utils;
|
2024-02-28 09:06:11 +00:00
|
|
|
|
using Request = CodexContractsPlugin.Marketplace.Request;
|
2023-09-12 13:43:30 +00:00
|
|
|
|
|
2023-10-09 14:59:52 +00:00
|
|
|
|
namespace CodexTests.BasicTests
|
2023-09-12 13:43:30 +00:00
|
|
|
|
{
|
|
|
|
|
[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-10-07 07:48:12 +00:00
|
|
|
|
var primary = AddCodex(s => s.WithLogLevel(CodexLogLevel.Trace, new CodexLogCustomTopics(CodexLogLevel.Warn, CodexLogLevel.Warn)));
|
2023-09-12 13:43:30 +00:00
|
|
|
|
|
2023-11-10 07:20:08 +00:00
|
|
|
|
var cid = primary.UploadFile(GenerateTestFile(5.MB()));
|
|
|
|
|
|
|
|
|
|
var content = primary.LocalFiles();
|
|
|
|
|
CollectionAssert.Contains(content.Select(c => c.Cid), cid);
|
2023-09-12 13:43:30 +00:00
|
|
|
|
|
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]
|
2023-09-22 05:43:46 +00:00
|
|
|
|
public void MarketplaceExample()
|
2023-09-12 13:43:30 +00:00
|
|
|
|
{
|
2023-09-19 14:22:07 +00:00
|
|
|
|
var sellerInitialBalance = 234.TestTokens();
|
2024-03-20 10:11:41 +00:00
|
|
|
|
var buyerInitialBalance = 100000.TestTokens();
|
2023-09-19 14:22:07 +00:00
|
|
|
|
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-20 07:16:57 +00:00
|
|
|
|
var contracts = Ci.StartCodexContracts(geth);
|
2023-09-15 14:27:08 +00:00
|
|
|
|
|
2023-09-22 05:43:46 +00:00
|
|
|
|
var seller = AddCodex(s => s
|
2024-02-26 14:08:04 +00:00
|
|
|
|
.WithName("Seller")
|
2024-01-18 09:24:59 +00:00
|
|
|
|
.WithLogLevel(CodexLogLevel.Trace, new CodexLogCustomTopics(CodexLogLevel.Error, CodexLogLevel.Error, CodexLogLevel.Warn))
|
2023-09-22 05:43:46 +00:00
|
|
|
|
.WithStorageQuota(11.GB())
|
2024-03-13 09:29:26 +00:00
|
|
|
|
.EnableMarketplace(geth, contracts, initialEth: 10.Eth(), initialTokens: sellerInitialBalance, s => s
|
|
|
|
|
.AsStorageNode()
|
2024-03-15 15:07:10 +00:00
|
|
|
|
.AsValidator()));
|
2023-10-09 14:59:52 +00:00
|
|
|
|
|
2023-10-30 12:30:14 +00:00
|
|
|
|
AssertBalance(contracts, seller, Is.EqualTo(sellerInitialBalance));
|
2024-03-20 10:11:41 +00:00
|
|
|
|
|
|
|
|
|
var availability = new StorageAvailability(
|
|
|
|
|
totalSpace: 10.GB(),
|
|
|
|
|
maxDuration: TimeSpan.FromMinutes(30),
|
2023-09-20 10:02:32 +00:00
|
|
|
|
minPriceForTotalSpace: 1.TestTokens(),
|
2024-03-20 10:11:41 +00:00
|
|
|
|
maxCollateral: 20.TestTokens()
|
|
|
|
|
);
|
|
|
|
|
seller.Marketplace.MakeStorageAvailable(availability);
|
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
|
2024-02-26 14:08:04 +00:00
|
|
|
|
.WithName("Buyer")
|
2023-09-19 14:22:07 +00:00
|
|
|
|
.WithBootstrapNode(seller)
|
2023-09-20 06:45:55 +00:00
|
|
|
|
.EnableMarketplace(geth, contracts, initialEth: 10.Eth(), initialTokens: buyerInitialBalance));
|
2023-10-09 14:59:52 +00:00
|
|
|
|
|
2023-10-30 12:30:14 +00:00
|
|
|
|
AssertBalance(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);
|
2024-03-20 10:11:41 +00:00
|
|
|
|
|
2024-03-26 10:39:59 +00:00
|
|
|
|
var purchase = new StoragePurchaseRequest(contentId)
|
2024-03-20 10:11:41 +00:00
|
|
|
|
{
|
|
|
|
|
PricePerSlotPerSecond = 2.TestTokens(),
|
|
|
|
|
RequiredCollateral = 10.TestTokens(),
|
|
|
|
|
MinRequiredNumberOfNodes = 5,
|
|
|
|
|
NodeFailureTolerance = 2,
|
|
|
|
|
ProofProbability = 5,
|
|
|
|
|
Duration = TimeSpan.FromMinutes(5),
|
|
|
|
|
Expiry = TimeSpan.FromMinutes(4)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var purchaseContract = buyer.Marketplace.RequestStorage(purchase);
|
2023-09-12 13:43:30 +00:00
|
|
|
|
|
2024-03-20 12:36:06 +00:00
|
|
|
|
purchaseContract.WaitForStorageContractStarted();
|
2023-09-12 13:43:30 +00:00
|
|
|
|
|
2023-10-30 12:30:14 +00:00
|
|
|
|
AssertBalance(contracts, seller, Is.LessThan(sellerInitialBalance), "Collateral was not placed.");
|
2023-09-12 13:43:30 +00:00
|
|
|
|
|
2024-02-28 09:06:11 +00:00
|
|
|
|
var request = GetOnChainStorageRequest(contracts);
|
2024-03-20 10:11:41 +00:00
|
|
|
|
AssertStorageRequest(request, purchase, contracts, buyer);
|
2024-03-20 10:11:59 +00:00
|
|
|
|
AssertSlotFilledEvents(contracts, purchase, request, seller);
|
2024-02-28 09:06:11 +00:00
|
|
|
|
AssertContractSlot(contracts, request, 0, seller);
|
2023-12-20 10:34:23 +00:00
|
|
|
|
|
2023-09-19 14:22:07 +00:00
|
|
|
|
purchaseContract.WaitForStorageContractFinished();
|
2023-09-12 13:43:30 +00:00
|
|
|
|
|
2023-10-30 12:30:14 +00:00
|
|
|
|
AssertBalance(contracts, seller, Is.GreaterThan(sellerInitialBalance), "Seller was not paid for storage.");
|
|
|
|
|
AssertBalance(contracts, buyer, Is.LessThan(buyerInitialBalance), "Buyer was not charged for storage.");
|
2024-01-22 15:27:32 +00:00
|
|
|
|
Assert.That(contracts.GetRequestState(request), Is.EqualTo(RequestState.Finished));
|
2023-09-12 13:43:30 +00:00
|
|
|
|
}
|
2023-11-21 09:33:11 +00:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void GethBootstrapTest()
|
|
|
|
|
{
|
|
|
|
|
var boot = Ci.StartGethNode(s => s.WithName("boot").IsMiner());
|
|
|
|
|
var disconnected = Ci.StartGethNode(s => s.WithName("disconnected"));
|
|
|
|
|
var follow = Ci.StartGethNode(s => s.WithBootstrapNode(boot).WithName("follow"));
|
|
|
|
|
|
|
|
|
|
Thread.Sleep(12000);
|
|
|
|
|
|
|
|
|
|
var bootN = boot.GetSyncedBlockNumber();
|
|
|
|
|
var discN = disconnected.GetSyncedBlockNumber();
|
|
|
|
|
var followN = follow.GetSyncedBlockNumber();
|
|
|
|
|
|
|
|
|
|
Assert.That(bootN, Is.EqualTo(followN));
|
|
|
|
|
Assert.That(discN, Is.LessThan(bootN));
|
|
|
|
|
}
|
2024-02-28 09:06:11 +00:00
|
|
|
|
|
2024-03-26 10:39:59 +00:00
|
|
|
|
private void AssertSlotFilledEvents(ICodexContracts contracts, StoragePurchaseRequest purchase, Request request, ICodexNode seller)
|
2024-02-28 09:06:11 +00:00
|
|
|
|
{
|
2024-03-20 10:11:59 +00:00
|
|
|
|
// Expect 1 fulfilled event for the purchase.
|
2024-02-28 09:06:11 +00:00
|
|
|
|
var requestFulfilledEvents = contracts.GetRequestFulfilledEvents(GetTestRunTimeRange());
|
|
|
|
|
Assert.That(requestFulfilledEvents.Length, Is.EqualTo(1));
|
|
|
|
|
CollectionAssert.AreEqual(request.RequestId, requestFulfilledEvents[0].RequestId);
|
2024-03-20 10:11:59 +00:00
|
|
|
|
|
|
|
|
|
// Expect 1 filled-slot event for each slot in the purchase.
|
2024-02-28 09:06:11 +00:00
|
|
|
|
var filledSlotEvents = contracts.GetSlotFilledEvents(GetTestRunTimeRange());
|
2024-03-20 10:11:59 +00:00
|
|
|
|
Assert.That(filledSlotEvents.Length, Is.EqualTo(purchase.MinRequiredNumberOfNodes));
|
|
|
|
|
for (var i = 0; i < purchase.MinRequiredNumberOfNodes; i++)
|
|
|
|
|
{
|
2024-03-20 10:32:16 +00:00
|
|
|
|
var filledSlotEvent = filledSlotEvents.Single(e => e.SlotIndex == i);
|
2024-03-20 10:11:59 +00:00
|
|
|
|
Assert.That(filledSlotEvent.RequestId.ToHex(), Is.EqualTo(request.RequestId.ToHex()));
|
|
|
|
|
Assert.That(filledSlotEvent.Host, Is.EqualTo(seller.EthAddress));
|
|
|
|
|
}
|
2024-02-28 09:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 10:39:59 +00:00
|
|
|
|
private void AssertStorageRequest(Request request, StoragePurchaseRequest purchase, ICodexContracts contracts, ICodexNode buyer)
|
2024-02-28 09:06:11 +00:00
|
|
|
|
{
|
|
|
|
|
Assert.That(contracts.GetRequestState(request), Is.EqualTo(RequestState.Started));
|
|
|
|
|
Assert.That(request.ClientAddress, Is.EqualTo(buyer.EthAddress));
|
2024-03-20 10:11:41 +00:00
|
|
|
|
Assert.That(request.Ask.Slots, Is.EqualTo(purchase.MinRequiredNumberOfNodes));
|
2024-02-28 09:06:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Request GetOnChainStorageRequest(ICodexContracts contracts)
|
|
|
|
|
{
|
|
|
|
|
var requests = contracts.GetStorageRequests(GetTestRunTimeRange());
|
|
|
|
|
Assert.That(requests.Length, Is.EqualTo(1));
|
|
|
|
|
return requests.Single();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AssertContractSlot(ICodexContracts contracts, Request request, int contractSlotIndex, ICodexNode expectedSeller)
|
|
|
|
|
{
|
|
|
|
|
var slotHost = contracts.GetSlotHost(request, contractSlotIndex);
|
|
|
|
|
Assert.That(slotHost, Is.EqualTo(expectedSeller.EthAddress));
|
|
|
|
|
}
|
2023-09-12 13:43:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|