131 lines
6.1 KiB
C#
Raw Normal View History

2023-09-15 16:27:08 +02:00
using CodexContractsPlugin;
2024-02-19 09:11:36 +01:00
using CodexDiscordBotPlugin;
using CodexPlugin;
2023-09-12 15:43:30 +02:00
using DistTestCore;
2023-09-15 15:52:02 +02:00
using GethPlugin;
using Nethereum.Hex.HexConvertors.Extensions;
2023-09-12 15:43:30 +02:00
using NUnit.Framework;
using Utils;
namespace CodexTests.BasicTests
2023-09-12 15:43:30 +02:00
{
[TestFixture]
2023-09-19 16:22:07 +02:00
public class ExampleTests : CodexDistTest
2023-09-12 15:43:30 +02:00
{
[Test]
2024-02-19 09:11:36 +01:00
public void BotRewardTest()
2023-09-12 15:43:30 +02:00
{
2023-09-19 16:22:07 +02:00
var sellerInitialBalance = 234.TestTokens();
var buyerInitialBalance = 1000.TestTokens();
var fileSize = 10.MB();
2023-09-15 15:52:02 +02:00
2023-09-19 16:22:07 +02:00
var geth = Ci.StartGethNode(s => s.IsMiner().WithName("disttest-geth"));
var contracts = Ci.StartCodexContracts(geth);
2023-09-15 16:27:08 +02:00
2023-09-22 07:43:46 +02:00
var seller = AddCodex(s => s
2024-01-18 10:24:59 +01:00
.WithLogLevel(CodexLogLevel.Trace, new CodexLogCustomTopics(CodexLogLevel.Error, CodexLogLevel.Error, CodexLogLevel.Warn))
2023-09-22 07:43:46 +02:00
.WithStorageQuota(11.GB())
.EnableMarketplace(geth, contracts, initialEth: 10.Eth(), initialTokens: sellerInitialBalance, isValidator: true)
.WithSimulateProofFailures(failEveryNProofs: 3));
2023-10-30 13:30:14 +01:00
AssertBalance(contracts, seller, Is.EqualTo(sellerInitialBalance));
2023-09-19 16:22:07 +02:00
seller.Marketplace.MakeStorageAvailable(
size: 10.GB(),
2023-09-20 12:02:32 +02:00
minPriceForTotalSpace: 1.TestTokens(),
2023-09-19 16:22:07 +02:00
maxCollateral: 20.TestTokens(),
maxDuration: TimeSpan.FromMinutes(3));
2023-09-12 15:43:30 +02:00
2023-09-19 16:22:07 +02:00
var testFile = GenerateTestFile(fileSize);
2023-09-12 15:43:30 +02:00
2023-09-19 16:22:07 +02:00
var buyer = AddCodex(s => s
.WithBootstrapNode(seller)
2023-09-20 08:45:55 +02:00
.EnableMarketplace(geth, contracts, initialEth: 10.Eth(), initialTokens: buyerInitialBalance));
2023-10-30 13:30:14 +01:00
AssertBalance(contracts, buyer, Is.EqualTo(buyerInitialBalance));
2023-09-12 15:43:30 +02:00
2024-02-19 09:11:36 +01: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
);
var bot = Ci.DeployCodexDiscordBot(new DiscordBotStartupConfig(
name: "bot",
token: "MTE2NDEyNzk3MDU4NDE3NDU5Mw.GTpoV6.aDR7zxMNf7vDgMjKASJBQs-RtNP_lYJEY-OglI",
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",
historyStartUtc: DateTime.UtcNow.AddHours(-1),
gethInfo: gethInfo,
dataPath: null
));
var sellerAddress = seller.EthAddress;
var buyerAddress = buyer.EthAddress;
var i = 0;
2023-09-19 16:22:07 +02: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 15:43:30 +02:00
2023-09-19 16:22:07 +02:00
purchaseContract.WaitForStorageContractStarted(fileSize);
2023-09-12 15:43:30 +02:00
2024-02-19 15:41:48 +01:00
var blockRange = geth.ConvertTimeRangeToBlockRange(GetTestRunTimeRange());
var requests = contracts.GetStorageRequests(blockRange);
2023-12-20 10:55:29 +01:00
Assert.That(requests.Length, Is.EqualTo(1));
var request = requests.Single();
Assert.That(contracts.GetRequestState(request), Is.EqualTo(RequestState.Started));
2023-12-20 11:34:23 +01:00
Assert.That(request.ClientAddress, Is.EqualTo(buyer.EthAddress));
2023-12-20 10:55:29 +01:00
Assert.That(request.Ask.Slots, Is.EqualTo(1));
2023-10-30 13:30:14 +01:00
AssertBalance(contracts, seller, Is.LessThan(sellerInitialBalance), "Collateral was not placed.");
2023-09-12 15:43:30 +02:00
2024-02-19 15:41:48 +01:00
var requestFulfilledEvents = contracts.GetRequestFulfilledEvents(blockRange);
Assert.That(requestFulfilledEvents.Length, Is.EqualTo(1));
CollectionAssert.AreEqual(request.RequestId, requestFulfilledEvents[0].RequestId);
2024-02-19 15:41:48 +01:00
var filledSlotEvents = contracts.GetSlotFilledEvents(blockRange);
Assert.That(filledSlotEvents.Length, Is.EqualTo(1));
var filledSlotEvent = filledSlotEvents.Single();
Assert.That(filledSlotEvent.SlotIndex.IsZero);
Assert.That(filledSlotEvent.RequestId.ToHex(), Is.EqualTo(request.RequestId.ToHex()));
Assert.That(filledSlotEvent.Host, Is.EqualTo(seller.EthAddress));
2023-12-20 11:34:23 +01:00
var slotHost = contracts.GetSlotHost(request, 0);
Assert.That(slotHost, Is.EqualTo(seller.EthAddress));
2023-09-19 16:22:07 +02:00
purchaseContract.WaitForStorageContractFinished();
2023-09-12 15:43:30 +02:00
2023-10-30 13:30:14 +01: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.");
Assert.That(contracts.GetRequestState(request), Is.EqualTo(RequestState.Finished));
2024-01-18 09:55:07 +01:00
var log = Ci.DownloadLog(seller);
log.AssertLogContains("Received a request to store a slot!");
log.AssertLogContains("Received proof challenge");
log.AssertLogContains("Collecting input for proof");
2023-12-20 09:48:22 +01:00
//CheckLogForErrors(seller, buyer);
2023-09-12 15:43:30 +02:00
}
}
}