2024-04-01 11:00:52 +02:00
|
|
|
|
using CodexContractsPlugin;
|
|
|
|
|
using CodexContractsPlugin.Marketplace;
|
2024-04-01 11:03:16 +02:00
|
|
|
|
using CodexDiscordBotPlugin;
|
2024-04-01 11:00:52 +02:00
|
|
|
|
using CodexPlugin;
|
|
|
|
|
using GethPlugin;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Utils;
|
|
|
|
|
|
|
|
|
|
namespace CodexTests.BasicTests
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class MarketplaceTests : AutoBootstrapDistTest
|
|
|
|
|
{
|
|
|
|
|
[Test]
|
2024-04-01 11:03:16 +02:00
|
|
|
|
public void BotRewardTest()
|
2024-04-01 11:00:52 +02:00
|
|
|
|
{
|
2024-04-01 11:03:16 +02:00
|
|
|
|
var myAccount = EthAccount.GenerateNew();
|
|
|
|
|
|
|
|
|
|
var sellerInitialBalance = 234.TestTokens();
|
|
|
|
|
var buyerInitialBalance = 100000.TestTokens();
|
2024-04-01 11:22:28 +02:00
|
|
|
|
var fileSize = 11.MB();
|
2024-04-01 11:00:52 +02:00
|
|
|
|
|
|
|
|
|
var geth = Ci.StartGethNode(s => s.IsMiner().WithName("disttest-geth"));
|
|
|
|
|
var contracts = Ci.StartCodexContracts(geth);
|
|
|
|
|
|
2024-04-01 11:03:16 +02:00
|
|
|
|
// start bot and rewarder
|
|
|
|
|
var gethInfo = new DiscordBotGethInfo(
|
|
|
|
|
host: geth.Container.GetInternalAddress(GethContainerRecipe.HttpPortTag).Host,
|
|
|
|
|
port: geth.Container.GetInternalAddress(GethContainerRecipe.HttpPortTag).Port,
|
|
|
|
|
privKey: geth.StartResult.Account.PrivateKey,
|
|
|
|
|
marketplaceAddress: contracts.Deployment.MarketplaceAddress,
|
|
|
|
|
tokenAddress: contracts.Deployment.TokenAddress,
|
|
|
|
|
abi: contracts.Deployment.Abi
|
2024-04-01 11:00:52 +02:00
|
|
|
|
);
|
2024-04-01 11:03:16 +02:00
|
|
|
|
var bot = Ci.DeployCodexDiscordBot(new DiscordBotStartupConfig(
|
|
|
|
|
name: "bot",
|
|
|
|
|
token: "aaa",
|
|
|
|
|
serverName: "ThatBen's server",
|
|
|
|
|
adminRoleName: "bottest-admins",
|
|
|
|
|
adminChannelName: "admin-channel",
|
|
|
|
|
rewardChannelName: "rewards-channel",
|
|
|
|
|
kubeNamespace: "notneeded",
|
|
|
|
|
gethInfo: gethInfo
|
|
|
|
|
));
|
|
|
|
|
var botContainer = bot.Containers.Single();
|
|
|
|
|
Ci.DeployRewarderBot(new RewarderBotStartupConfig(
|
|
|
|
|
//discordBotHost: "http://" + botContainer.GetAddress(GetTestLog(), DiscordBotContainerRecipe.RewardsPort).Host,
|
|
|
|
|
//discordBotPort: botContainer.GetAddress(GetTestLog(), DiscordBotContainerRecipe.RewardsPort).Port,
|
|
|
|
|
discordBotHost: botContainer.GetInternalAddress(DiscordBotContainerRecipe.RewardsPort).Host,
|
|
|
|
|
discordBotPort: botContainer.GetInternalAddress(DiscordBotContainerRecipe.RewardsPort).Port,
|
|
|
|
|
interval: "60",
|
2024-04-01 14:08:30 +02:00
|
|
|
|
historyStartUtc: GetTestRunTimeRange().From - TimeSpan.FromMinutes(3),
|
2024-04-01 11:03:16 +02:00
|
|
|
|
gethInfo: gethInfo,
|
|
|
|
|
dataPath: null
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
var numberOfHosts = 3;
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < numberOfHosts; i++)
|
|
|
|
|
{
|
|
|
|
|
var seller = AddCodex(s => s
|
|
|
|
|
.WithName("Seller")
|
|
|
|
|
.WithLogLevel(CodexLogLevel.Trace, new CodexLogCustomTopics(CodexLogLevel.Error, CodexLogLevel.Error, CodexLogLevel.Warn)
|
|
|
|
|
{
|
|
|
|
|
ContractClock = CodexLogLevel.Trace,
|
|
|
|
|
})
|
|
|
|
|
.WithStorageQuota(11.GB())
|
|
|
|
|
.EnableMarketplace(geth, contracts, m => m
|
|
|
|
|
.WithAccount(myAccount)
|
|
|
|
|
.WithInitial(10.Eth(), sellerInitialBalance)
|
|
|
|
|
.AsStorageNode()
|
|
|
|
|
.AsValidator()));
|
|
|
|
|
|
|
|
|
|
var availability = new StorageAvailability(
|
|
|
|
|
totalSpace: 10.GB(),
|
|
|
|
|
maxDuration: TimeSpan.FromMinutes(30),
|
|
|
|
|
minPriceForTotalSpace: 1.TestTokens(),
|
|
|
|
|
maxCollateral: 20.TestTokens()
|
|
|
|
|
);
|
|
|
|
|
seller.Marketplace.MakeStorageAvailable(availability);
|
|
|
|
|
}
|
2024-04-01 11:00:52 +02:00
|
|
|
|
|
|
|
|
|
var testFile = GenerateTestFile(fileSize);
|
|
|
|
|
|
2024-04-01 11:03:16 +02:00
|
|
|
|
var buyer = AddCodex(s => s
|
|
|
|
|
.WithName("Buyer")
|
2024-04-01 11:00:52 +02:00
|
|
|
|
.EnableMarketplace(geth, contracts, m => m
|
2024-04-01 11:22:28 +02:00
|
|
|
|
.WithAccount(myAccount)
|
2024-04-01 11:03:16 +02:00
|
|
|
|
.WithInitial(10.Eth(), buyerInitialBalance)));
|
2024-04-01 11:00:52 +02:00
|
|
|
|
|
2024-04-01 11:03:16 +02:00
|
|
|
|
var contentId = buyer.UploadFile(testFile);
|
2024-04-01 11:00:52 +02:00
|
|
|
|
|
|
|
|
|
var purchase = new StoragePurchaseRequest(contentId)
|
|
|
|
|
{
|
|
|
|
|
PricePerSlotPerSecond = 2.TestTokens(),
|
|
|
|
|
RequiredCollateral = 10.TestTokens(),
|
|
|
|
|
MinRequiredNumberOfNodes = 5,
|
|
|
|
|
NodeFailureTolerance = 2,
|
|
|
|
|
ProofProbability = 5,
|
2024-04-01 11:22:28 +02:00
|
|
|
|
Duration = TimeSpan.FromMinutes(6),
|
|
|
|
|
Expiry = TimeSpan.FromMinutes(5)
|
2024-04-01 11:00:52 +02:00
|
|
|
|
};
|
|
|
|
|
|
2024-04-01 11:03:16 +02:00
|
|
|
|
var purchaseContract = buyer.Marketplace.RequestStorage(purchase);
|
2024-04-01 11:00:52 +02:00
|
|
|
|
|
|
|
|
|
purchaseContract.WaitForStorageContractStarted();
|
|
|
|
|
|
2024-04-01 11:03:16 +02:00
|
|
|
|
//AssertBalance(contracts, seller, Is.LessThan(sellerInitialBalance), "Collateral was not placed.");
|
2024-04-01 11:00:52 +02:00
|
|
|
|
|
2024-04-01 11:03:16 +02:00
|
|
|
|
//var blockRange = geth.ConvertTimeRangeToBlockRange(GetTestRunTimeRange());
|
|
|
|
|
|
|
|
|
|
//var request = GetOnChainStorageRequest(contracts, blockRange);
|
|
|
|
|
//AssertStorageRequest(request, purchase, contracts, buyer);
|
|
|
|
|
//AssertSlotFilledEvents(contracts, purchase, request, seller, blockRange);
|
|
|
|
|
//AssertContractSlot(contracts, request, 0, seller);
|
2024-04-01 11:00:52 +02:00
|
|
|
|
|
|
|
|
|
purchaseContract.WaitForStorageContractFinished();
|
|
|
|
|
|
2024-04-01 11:03:16 +02:00
|
|
|
|
var hold = 0;
|
|
|
|
|
|
|
|
|
|
//AssertBalance(contracts, seller, Is.GreaterThan(sellerInitialBalance), "Seller was not paid for storage.");
|
|
|
|
|
//AssertBalance(contracts, buyer, Is.LessThan(buyerInitialBalance), "Buyer was not charged for storage.");
|
|
|
|
|
//Assert.That(contracts.GetRequestState(request), Is.EqualTo(RequestState.Finished));
|
2024-04-01 11:00:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 11:22:28 +02:00
|
|
|
|
private void WaitForAllSlotFilledEvents(IGethNode gethNode, ICodexContracts contracts, StoragePurchaseRequest purchase)
|
2024-04-01 11:00:52 +02:00
|
|
|
|
{
|
|
|
|
|
Time.Retry(() =>
|
|
|
|
|
{
|
2024-04-01 11:22:28 +02:00
|
|
|
|
var blockRange = gethNode.ConvertTimeRangeToBlockRange(GetTestRunTimeRange());
|
|
|
|
|
var slotFilledEvents = contracts.GetSlotFilledEvents(blockRange);
|
2024-04-01 11:00:52 +02:00
|
|
|
|
|
|
|
|
|
Log($"SlotFilledEvents: {slotFilledEvents.Length} - NumSlots: {purchase.MinRequiredNumberOfNodes}");
|
|
|
|
|
|
|
|
|
|
if (slotFilledEvents.Length != purchase.MinRequiredNumberOfNodes) throw new Exception();
|
|
|
|
|
}, Convert.ToInt32(purchase.Duration.TotalSeconds / 5) + 10, TimeSpan.FromSeconds(5), "Checking SlotFilled events");
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 11:22:28 +02:00
|
|
|
|
//private void AssertSlotFilledEvents(ICodexContracts contracts, StoragePurchaseRequest purchase, Request request, ICodexNode seller)
|
|
|
|
|
//{
|
|
|
|
|
// // Expect 1 fulfilled event for the purchase.
|
|
|
|
|
// var requestFulfilledEvents = contracts.GetRequestFulfilledEvents(GetTestRunTimeRange());
|
|
|
|
|
// Assert.That(requestFulfilledEvents.Length, Is.EqualTo(1));
|
|
|
|
|
// CollectionAssert.AreEqual(request.RequestId, requestFulfilledEvents[0].RequestId);
|
|
|
|
|
|
|
|
|
|
// // Expect 1 filled-slot event for each slot in the purchase.
|
|
|
|
|
// var filledSlotEvents = contracts.GetSlotFilledEvents(GetTestRunTimeRange());
|
|
|
|
|
// Assert.That(filledSlotEvents.Length, Is.EqualTo(purchase.MinRequiredNumberOfNodes));
|
|
|
|
|
// for (var i = 0; i < purchase.MinRequiredNumberOfNodes; i++)
|
|
|
|
|
// {
|
|
|
|
|
// var filledSlotEvent = filledSlotEvents.Single(e => e.SlotIndex == i);
|
|
|
|
|
// Assert.That(filledSlotEvent.RequestId.ToHex(), Is.EqualTo(request.RequestId.ToHex()));
|
|
|
|
|
// Assert.That(filledSlotEvent.Host, Is.EqualTo(seller.EthAddress));
|
|
|
|
|
// }
|
|
|
|
|
//}
|
2024-04-01 11:00:52 +02:00
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
Assert.That(request.Ask.Slots, Is.EqualTo(purchase.MinRequiredNumberOfNodes));
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 11:22:28 +02:00
|
|
|
|
//private Request GetOnChainStorageRequest(ICodexContracts contracts)
|
|
|
|
|
//{
|
|
|
|
|
// var requests = contracts.GetStorageRequests(GetTestRunTimeRange());
|
|
|
|
|
// Assert.That(requests.Length, Is.EqualTo(1));
|
|
|
|
|
// return requests.Single();
|
|
|
|
|
//}
|
2024-04-01 11:00:52 +02:00
|
|
|
|
|
|
|
|
|
private void AssertContractSlot(ICodexContracts contracts, Request request, int contractSlotIndex, ICodexNode expectedSeller)
|
|
|
|
|
{
|
|
|
|
|
var slotHost = contracts.GetSlotHost(request, contractSlotIndex);
|
|
|
|
|
Assert.That(slotHost, Is.EqualTo(expectedSeller.EthAddress));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|