moves bot tests

This commit is contained in:
benbierens 2024-04-01 15:31:54 +02:00
parent 6c4b9345cb
commit ceed913143
No known key found for this signature in database
GPG Key ID: 877D2C2E09A22F3A
2 changed files with 146 additions and 93 deletions

View File

@ -0,0 +1,110 @@
using CodexContractsPlugin;
using CodexDiscordBotPlugin;
using CodexPlugin;
using GethPlugin;
using NUnit.Framework;
using Utils;
namespace CodexTests.BasicTests
{
[TestFixture]
public class DiscordBotTests : AutoBootstrapDistTest
{
[Test]
public void BotRewardTest()
{
var myAccount = EthAccount.GenerateNew();
var sellerInitialBalance = 234.TestTokens();
var buyerInitialBalance = 100000.TestTokens();
var fileSize = 11.MB();
var geth = Ci.StartGethNode(s => s.IsMiner().WithName("disttest-geth"));
var contracts = Ci.StartCodexContracts(geth);
// 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: "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",
historyStartUtc: GetTestRunTimeRange().From - TimeSpan.FromMinutes(3),
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);
}
var testFile = GenerateTestFile(fileSize);
var buyer = AddCodex(s => s
.WithName("Buyer")
.EnableMarketplace(geth, contracts, m => m
.WithAccount(myAccount)
.WithInitial(10.Eth(), buyerInitialBalance)));
var contentId = buyer.UploadFile(testFile);
var purchase = new StoragePurchaseRequest(contentId)
{
PricePerSlotPerSecond = 2.TestTokens(),
RequiredCollateral = 10.TestTokens(),
MinRequiredNumberOfNodes = 5,
NodeFailureTolerance = 2,
ProofProbability = 5,
Duration = TimeSpan.FromMinutes(6),
Expiry = TimeSpan.FromMinutes(5)
};
var purchaseContract = buyer.Marketplace.RequestStorage(purchase);
purchaseContract.WaitForStorageContractStarted();
purchaseContract.WaitForStorageContractFinished();
}
}
}

View File

@ -1,6 +1,5 @@
using CodexContractsPlugin; using CodexContractsPlugin;
using CodexContractsPlugin.Marketplace; using CodexContractsPlugin.Marketplace;
using CodexDiscordBotPlugin;
using CodexPlugin; using CodexPlugin;
using GethPlugin; using GethPlugin;
using NUnit.Framework; using NUnit.Framework;
@ -12,83 +11,51 @@ namespace CodexTests.BasicTests
public class MarketplaceTests : AutoBootstrapDistTest public class MarketplaceTests : AutoBootstrapDistTest
{ {
[Test] [Test]
public void BotRewardTest() public void MarketplaceExample()
{ {
var myAccount = EthAccount.GenerateNew(); var hostInitialBalance = 234.TestTokens();
var clientInitialBalance = 100000.TestTokens();
var sellerInitialBalance = 234.TestTokens(); var fileSize = 10.MB();
var buyerInitialBalance = 100000.TestTokens();
var fileSize = 11.MB();
var geth = Ci.StartGethNode(s => s.IsMiner().WithName("disttest-geth")); var geth = Ci.StartGethNode(s => s.IsMiner().WithName("disttest-geth"));
var contracts = Ci.StartCodexContracts(geth); var contracts = Ci.StartCodexContracts(geth);
// 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: "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",
historyStartUtc: GetTestRunTimeRange().From - TimeSpan.FromMinutes(3),
gethInfo: gethInfo,
dataPath: null
));
var numberOfHosts = 3; var numberOfHosts = 3;
for (var i = 0; i < numberOfHosts; i++) for (var i = 0; i < numberOfHosts; i++)
{ {
var seller = AddCodex(s => s var host = AddCodex(s => s
.WithName("Seller") .WithName("Host")
.WithLogLevel(CodexLogLevel.Trace, new CodexLogCustomTopics(CodexLogLevel.Error, CodexLogLevel.Error, CodexLogLevel.Warn) .WithLogLevel(CodexLogLevel.Trace, new CodexLogCustomTopics(CodexLogLevel.Error, CodexLogLevel.Error, CodexLogLevel.Warn)
{ {
ContractClock = CodexLogLevel.Trace, ContractClock = CodexLogLevel.Trace,
}) })
.WithStorageQuota(11.GB()) .WithStorageQuota(11.GB())
.EnableMarketplace(geth, contracts, m => m .EnableMarketplace(geth, contracts, m => m
.WithAccount(myAccount) .WithInitial(10.Eth(), hostInitialBalance)
.WithInitial(10.Eth(), sellerInitialBalance)
.AsStorageNode() .AsStorageNode()
.AsValidator())); .AsValidator()));
AssertBalance(contracts, host, Is.EqualTo(hostInitialBalance));
var availability = new StorageAvailability( var availability = new StorageAvailability(
totalSpace: 10.GB(), totalSpace: 10.GB(),
maxDuration: TimeSpan.FromMinutes(30), maxDuration: TimeSpan.FromMinutes(30),
minPriceForTotalSpace: 1.TestTokens(), minPriceForTotalSpace: 1.TestTokens(),
maxCollateral: 20.TestTokens() maxCollateral: 20.TestTokens()
); );
seller.Marketplace.MakeStorageAvailable(availability); host.Marketplace.MakeStorageAvailable(availability);
} }
var testFile = GenerateTestFile(fileSize); var testFile = GenerateTestFile(fileSize);
var buyer = AddCodex(s => s var client = AddCodex(s => s
.WithName("Buyer") .WithName("Client")
.EnableMarketplace(geth, contracts, m => m .EnableMarketplace(geth, contracts, m => m
.WithAccount(myAccount) .WithInitial(10.Eth(), clientInitialBalance)));
.WithInitial(10.Eth(), buyerInitialBalance)));
var contentId = buyer.UploadFile(testFile); AssertBalance(contracts, client, Is.EqualTo(clientInitialBalance));
var contentId = client.UploadFile(testFile);
var purchase = new StoragePurchaseRequest(contentId) var purchase = new StoragePurchaseRequest(contentId)
{ {
@ -97,37 +64,31 @@ namespace CodexTests.BasicTests
MinRequiredNumberOfNodes = 5, MinRequiredNumberOfNodes = 5,
NodeFailureTolerance = 2, NodeFailureTolerance = 2,
ProofProbability = 5, ProofProbability = 5,
Duration = TimeSpan.FromMinutes(6), Duration = TimeSpan.FromMinutes(5),
Expiry = TimeSpan.FromMinutes(5) Expiry = TimeSpan.FromMinutes(4)
}; };
var purchaseContract = buyer.Marketplace.RequestStorage(purchase); var purchaseContract = client.Marketplace.RequestStorage(purchase);
WaitForAllSlotFilledEvents(contracts, purchase, geth);
purchaseContract.WaitForStorageContractStarted(); purchaseContract.WaitForStorageContractStarted();
//AssertBalance(contracts, seller, Is.LessThan(sellerInitialBalance), "Collateral was not placed."); var request = GetOnChainStorageRequest(contracts, geth);
AssertStorageRequest(request, purchase, contracts, client);
//var blockRange = geth.ConvertTimeRangeToBlockRange(GetTestRunTimeRange()); AssertContractSlot(contracts, request, 0);
//var request = GetOnChainStorageRequest(contracts, blockRange);
//AssertStorageRequest(request, purchase, contracts, buyer);
//AssertSlotFilledEvents(contracts, purchase, request, seller, blockRange);
//AssertContractSlot(contracts, request, 0, seller);
purchaseContract.WaitForStorageContractFinished(); purchaseContract.WaitForStorageContractFinished();
var hold = 0; AssertBalance(contracts, client, Is.LessThan(clientInitialBalance), "Buyer was not charged for storage.");
Assert.That(contracts.GetRequestState(request), Is.EqualTo(RequestState.Finished));
//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));
} }
private void WaitForAllSlotFilledEvents(IGethNode gethNode, ICodexContracts contracts, StoragePurchaseRequest purchase) private void WaitForAllSlotFilledEvents(ICodexContracts contracts, StoragePurchaseRequest purchase, IGethNode geth)
{ {
Time.Retry(() => Time.Retry(() =>
{ {
var blockRange = gethNode.ConvertTimeRangeToBlockRange(GetTestRunTimeRange()); var blockRange = geth.ConvertTimeRangeToBlockRange(GetTestRunTimeRange());
var slotFilledEvents = contracts.GetSlotFilledEvents(blockRange); var slotFilledEvents = contracts.GetSlotFilledEvents(blockRange);
Log($"SlotFilledEvents: {slotFilledEvents.Length} - NumSlots: {purchase.MinRequiredNumberOfNodes}"); Log($"SlotFilledEvents: {slotFilledEvents.Length} - NumSlots: {purchase.MinRequiredNumberOfNodes}");
@ -136,24 +97,6 @@ namespace CodexTests.BasicTests
}, Convert.ToInt32(purchase.Duration.TotalSeconds / 5) + 10, TimeSpan.FromSeconds(5), "Checking SlotFilled events"); }, Convert.ToInt32(purchase.Duration.TotalSeconds / 5) + 10, TimeSpan.FromSeconds(5), "Checking SlotFilled events");
} }
//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));
// }
//}
private void AssertStorageRequest(Request request, StoragePurchaseRequest purchase, ICodexContracts contracts, ICodexNode buyer) private void AssertStorageRequest(Request request, StoragePurchaseRequest purchase, ICodexContracts contracts, ICodexNode buyer)
{ {
Assert.That(contracts.GetRequestState(request), Is.EqualTo(RequestState.Started)); Assert.That(contracts.GetRequestState(request), Is.EqualTo(RequestState.Started));
@ -161,17 +104,17 @@ namespace CodexTests.BasicTests
Assert.That(request.Ask.Slots, Is.EqualTo(purchase.MinRequiredNumberOfNodes)); Assert.That(request.Ask.Slots, Is.EqualTo(purchase.MinRequiredNumberOfNodes));
} }
//private Request GetOnChainStorageRequest(ICodexContracts contracts) private Request GetOnChainStorageRequest(ICodexContracts contracts, IGethNode geth)
//{ {
// var requests = contracts.GetStorageRequests(GetTestRunTimeRange()); var requests = contracts.GetStorageRequests(geth.ConvertTimeRangeToBlockRange(GetTestRunTimeRange()));
// Assert.That(requests.Length, Is.EqualTo(1)); Assert.That(requests.Length, Is.EqualTo(1));
// return requests.Single(); return requests.Single();
//} }
private void AssertContractSlot(ICodexContracts contracts, Request request, int contractSlotIndex, ICodexNode expectedSeller) private void AssertContractSlot(ICodexContracts contracts, Request request, int contractSlotIndex)
{ {
var slotHost = contracts.GetSlotHost(request, contractSlotIndex); var slotHost = contracts.GetSlotHost(request, contractSlotIndex);
Assert.That(slotHost, Is.EqualTo(expectedSeller.EthAddress)); Assert.That(slotHost?.Address, Is.Not.Null);
} }
} }
} }