diff --git a/ProjectPlugins/CodexDiscordBotPlugin/DiscordBotContainerRecipe.cs b/ProjectPlugins/CodexDiscordBotPlugin/DiscordBotContainerRecipe.cs index 9a2e3fe..86ee1e2 100644 --- a/ProjectPlugins/CodexDiscordBotPlugin/DiscordBotContainerRecipe.cs +++ b/ProjectPlugins/CodexDiscordBotPlugin/DiscordBotContainerRecipe.cs @@ -7,7 +7,7 @@ namespace CodexDiscordBotPlugin public class DiscordBotContainerRecipe : ContainerRecipeFactory { public override string AppName => "discordbot-bibliotech"; - public override string Image => "codexstorage/codex-discordbot:sha-8c64352"; + public override string Image => "codexstorage/codex-discordbot:sha-22cf82b"; public static string RewardsPort = "bot_rewards_port"; @@ -33,6 +33,8 @@ namespace CodexDiscordBotPlugin AddEnvVar("CODEXCONTRACTS_TOKENADDRESS", gethInfo.TokenAddress); AddEnvVar("CODEXCONTRACTS_ABI", gethInfo.Abi); + AddEnvVar("NODISCORD", "1"); + AddInternalPortAndVar("REWARDAPIPORT", RewardsPort); if (!string.IsNullOrEmpty(config.DataPath)) diff --git a/Tests/CodexTests/UtilityTests/DiscordBotTests.cs b/Tests/CodexTests/UtilityTests/DiscordBotTests.cs index d642525..2223f92 100644 --- a/Tests/CodexTests/UtilityTests/DiscordBotTests.cs +++ b/Tests/CodexTests/UtilityTests/DiscordBotTests.cs @@ -2,6 +2,7 @@ using CodexDiscordBotPlugin; using CodexPlugin; using GethPlugin; +using KubernetesWorkflow.Types; using NUnit.Framework; using Utils; @@ -10,86 +11,42 @@ namespace CodexTests.UtilityTests [TestFixture] public class DiscordBotTests : AutoBootstrapDistTest { + private readonly TestToken hostInitialBalance = 3000.TestTokens(); + private readonly TestToken clientInitialBalance = 1000000000.TestTokens(); + private readonly ByteSize fileSize = 11.MB(); + [Test] - [Ignore("Used for debugging bots")] public void BotRewardTest() { - var myAccount = EthAccount.GenerateNew(); - - var sellerInitialBalance = 234.TestTokens(); - var buyerInitialBalance = 100000.TestTokens(); - var fileSize = 11.MB(); + var clientAccount = EthAccount.GenerateNew(); var geth = Ci.StartGethNode(s => s.IsMiner().WithName("disttest-geth")); var contracts = Ci.StartCodexContracts(geth); + var gethInfo = CreateGethInfo(geth, contracts); - // 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, - intervalMinutes: "1", - historyStartUtc: GetTestRunTimeRange().From - TimeSpan.FromMinutes(3), - gethInfo: gethInfo, - dataPath: null - )); + var botContainer = StartDiscordBot(gethInfo); - var numberOfHosts = 3; + var hostAccount = EthAccount.GenerateNew(); + StartHosts(hostAccount, geth, contracts); - for (var i = 0; i < numberOfHosts; i++) - { - var seller = StartCodex(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())); + StartRewarderBot(gethInfo, botContainer); - var availability = new StorageAvailability( - totalSpace: 10.GB(), - maxDuration: TimeSpan.FromMinutes(30), - minPriceForTotalSpace: 1.TestTokens(), - maxCollateral: 20.TestTokens() - ); - seller.Marketplace.MakeStorageAvailable(availability); - } + var client = StartClient(geth, contracts, clientAccount); + var purchaseContract = ClientPurchasesStorage(client); + + //purchaseContract.WaitForStorageContractStarted(); + //purchaseContract.WaitForStorageContractFinished(); + Thread.Sleep(TimeSpan.FromMinutes(5)); + + var botLog = Ci.ExecuteContainerCommand(botContainer, "cat", "/app/datapath/logs/discordbot.log"); + var aaaa = 0; + } + + private StoragePurchaseContract ClientPurchasesStorage(ICodexNode client) + { var testFile = GenerateTestFile(fileSize); - - var buyer = StartCodex(s => s - .WithName("Buyer") - .EnableMarketplace(geth, contracts, m => m - .WithAccount(myAccount) - .WithInitial(10.Eth(), buyerInitialBalance))); - - var contentId = buyer.UploadFile(testFile); - + var contentId = client.UploadFile(testFile); var purchase = new StoragePurchaseRequest(contentId) { PricePerSlotPerSecond = 2.TestTokens(), @@ -101,11 +58,84 @@ namespace CodexTests.UtilityTests Expiry = TimeSpan.FromMinutes(5) }; - var purchaseContract = buyer.Marketplace.RequestStorage(purchase); + return client.Marketplace.RequestStorage(purchase); + } - purchaseContract.WaitForStorageContractStarted(); + private ICodexNode StartClient(IGethNode geth, ICodexContracts contracts, EthAccount clientAccount) + { + return StartCodex(s => s + .WithName("Client") + .EnableMarketplace(geth, contracts, m => m + .WithAccount(clientAccount) + .WithInitial(10.Eth(), clientInitialBalance))); + } - purchaseContract.WaitForStorageContractFinished(); + private void StartRewarderBot(DiscordBotGethInfo gethInfo, RunningContainer botContainer) + { + Ci.DeployRewarderBot(new RewarderBotStartupConfig( + discordBotHost: botContainer.GetInternalAddress(DiscordBotContainerRecipe.RewardsPort).Host, + discordBotPort: botContainer.GetInternalAddress(DiscordBotContainerRecipe.RewardsPort).Port, + intervalMinutes: "10", + historyStartUtc: DateTime.UtcNow, + gethInfo: gethInfo, + dataPath: null + )); + } + + private DiscordBotGethInfo CreateGethInfo(IGethNode geth, ICodexContracts contracts) + { + return 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 + ); + } + + private RunningContainer StartDiscordBot(DiscordBotGethInfo gethInfo) + { + 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 + )); + return bot.Containers.Single(); + } + + private void StartHosts(EthAccount hostAccount, IGethNode geth, ICodexContracts contracts) + { + var numberOfHosts = 5; + var hosts = StartCodex(numberOfHosts, s => s + .WithName("Host") + .WithLogLevel(CodexLogLevel.Trace, new CodexLogCustomTopics(CodexLogLevel.Error, CodexLogLevel.Error, CodexLogLevel.Warn) + { + ContractClock = CodexLogLevel.Trace, + }) + .WithStorageQuota(11.GB()) + .EnableMarketplace(geth, contracts, m => m + .WithAccount(hostAccount) + .WithInitial(10.Eth(), hostInitialBalance) + .AsStorageNode() + .AsValidator())); + + var availability = new StorageAvailability( + totalSpace: 10.GB(), + maxDuration: TimeSpan.FromMinutes(30), + minPriceForTotalSpace: 1.TestTokens(), + maxCollateral: 20.TestTokens() + ); + + foreach (var host in hosts) + { + host.Marketplace.MakeStorageAvailable(availability); + } } } }