From 3c210f96fca591b49607437b2e30a1f397c2b380 Mon Sep 17 00:00:00 2001 From: benbierens Date: Mon, 19 Feb 2024 09:11:36 +0100 Subject: [PATCH] debugging reward api --- .../DiscordBotContainerRecipe.cs | 5 + .../DiscordBotStartupConfig.cs | 8 +- .../RewarderBotContainerRecipe.cs | 2 +- Tests/CodexTests/BasicTests/ExampleTests.cs | 93 ++++++++----------- Tests/CodexTests/CodexTests.csproj | 1 + Tools/BiblioTech/Rewards/RewardsApi.cs | 3 - Tools/CodexNetDeployer/Configuration.cs | 4 + Tools/CodexNetDeployer/Deployer.cs | 3 +- 8 files changed, 56 insertions(+), 63 deletions(-) diff --git a/ProjectPlugins/CodexDiscordBotPlugin/DiscordBotContainerRecipe.cs b/ProjectPlugins/CodexDiscordBotPlugin/DiscordBotContainerRecipe.cs index ea69f3b..b981216 100644 --- a/ProjectPlugins/CodexDiscordBotPlugin/DiscordBotContainerRecipe.cs +++ b/ProjectPlugins/CodexDiscordBotPlugin/DiscordBotContainerRecipe.cs @@ -9,6 +9,8 @@ namespace CodexDiscordBotPlugin public override string AppName => "discordbot-bibliotech"; public override string Image => "thatbenbierens/codex-discordbot:initial"; + public static string RewardsPort = "bot_rewards_port"; + protected override void Initialize(StartupConfig startupConfig) { var config = startupConfig.Get(); @@ -19,6 +21,7 @@ namespace CodexDiscordBotPlugin AddEnvVar("SERVERNAME", config.ServerName); AddEnvVar("ADMINROLE", config.AdminRoleName); AddEnvVar("ADMINCHANNELNAME", config.AdminChannelName); + AddEnvVar("REWARDSCHANNELNAME", config.RewardChannelName); AddEnvVar("KUBECONFIG", "/opt/kubeconfig.yaml"); AddEnvVar("KUBENAMESPACE", config.KubeNamespace); @@ -30,6 +33,8 @@ namespace CodexDiscordBotPlugin AddEnvVar("CODEXCONTRACTS_TOKENADDRESS", gethInfo.TokenAddress); AddEnvVar("CODEXCONTRACTS_ABI", gethInfo.Abi); + AddInternalPortAndVar("REWARDAPIPORT", RewardsPort); + if (!string.IsNullOrEmpty(config.DataPath)) { AddEnvVar("DATAPATH", config.DataPath); diff --git a/ProjectPlugins/CodexDiscordBotPlugin/DiscordBotStartupConfig.cs b/ProjectPlugins/CodexDiscordBotPlugin/DiscordBotStartupConfig.cs index 1e8165d..dfc6ad1 100644 --- a/ProjectPlugins/CodexDiscordBotPlugin/DiscordBotStartupConfig.cs +++ b/ProjectPlugins/CodexDiscordBotPlugin/DiscordBotStartupConfig.cs @@ -2,7 +2,7 @@ { public class DiscordBotStartupConfig { - public DiscordBotStartupConfig(string name, string token, string serverName, string adminRoleName, string adminChannelName, string kubeNamespace, DiscordBotGethInfo gethInfo) + public DiscordBotStartupConfig(string name, string token, string serverName, string adminRoleName, string adminChannelName, string kubeNamespace, DiscordBotGethInfo gethInfo, string rewardChannelName) { Name = name; Token = token; @@ -11,6 +11,7 @@ AdminChannelName = adminChannelName; KubeNamespace = kubeNamespace; GethInfo = gethInfo; + RewardChannelName = rewardChannelName; } public string Name { get; } @@ -18,6 +19,7 @@ public string ServerName { get; } public string AdminRoleName { get; } public string AdminChannelName { get; } + public string RewardChannelName { get; } public string KubeNamespace { get; } public DiscordBotGethInfo GethInfo { get; } public string? DataPath { get; set; } @@ -25,7 +27,7 @@ public class RewarderBotStartupConfig { - public RewarderBotStartupConfig(string discordBotHost, int discordBotPort, TimeSpan interval, DateTime historyStartUtc, DiscordBotGethInfo gethInfo, string? dataPath) + public RewarderBotStartupConfig(string discordBotHost, int discordBotPort, string interval, DateTime historyStartUtc, DiscordBotGethInfo gethInfo, string? dataPath) { DiscordBotHost = discordBotHost; DiscordBotPort = discordBotPort; @@ -37,7 +39,7 @@ public string DiscordBotHost { get; } public int DiscordBotPort { get; } - public TimeSpan Interval { get; } + public string Interval { get; } public DateTime HistoryStartUtc { get; } public DiscordBotGethInfo GethInfo { get; } public string? DataPath { get; set; } diff --git a/ProjectPlugins/CodexDiscordBotPlugin/RewarderBotContainerRecipe.cs b/ProjectPlugins/CodexDiscordBotPlugin/RewarderBotContainerRecipe.cs index fd5159e..3c8ee3b 100644 --- a/ProjectPlugins/CodexDiscordBotPlugin/RewarderBotContainerRecipe.cs +++ b/ProjectPlugins/CodexDiscordBotPlugin/RewarderBotContainerRecipe.cs @@ -17,7 +17,7 @@ namespace CodexDiscordBotPlugin AddEnvVar("DISCORDBOTHOST", config.DiscordBotHost); AddEnvVar("DISCORDBOTPORT", config.DiscordBotPort.ToString()); - AddEnvVar("INTERVALMINUTES", config.Interval.TotalMinutes.ToString()); + AddEnvVar("INTERVALMINUTES", config.Interval); var offset = new DateTimeOffset(config.HistoryStartUtc); AddEnvVar("CHECKHISTORY", offset.ToUnixTimeSeconds().ToString()); diff --git a/Tests/CodexTests/BasicTests/ExampleTests.cs b/Tests/CodexTests/BasicTests/ExampleTests.cs index c73a913..17c911d 100644 --- a/Tests/CodexTests/BasicTests/ExampleTests.cs +++ b/Tests/CodexTests/BasicTests/ExampleTests.cs @@ -1,8 +1,8 @@ using CodexContractsPlugin; +using CodexDiscordBotPlugin; using CodexPlugin; using DistTestCore; using GethPlugin; -using MetricsPlugin; using Nethereum.Hex.HexConvertors.Extensions; using NUnit.Framework; using Utils; @@ -13,44 +13,7 @@ namespace CodexTests.BasicTests public class ExampleTests : CodexDistTest { [Test] - public void CodexLogExample() - { - var primary = AddCodex(s => s.WithLogLevel(CodexLogLevel.Trace, new CodexLogCustomTopics(CodexLogLevel.Warn, CodexLogLevel.Warn))); - - var cid = primary.UploadFile(GenerateTestFile(5.MB())); - - var content = primary.LocalFiles(); - CollectionAssert.Contains(content.Select(c => c.Cid), cid); - - var log = Ci.DownloadLog(primary); - - log.AssertLogContains("Uploaded file"); - } - - [Test] - public void TwoMetricsExample() - { - var group = AddCodex(2, s => s.EnableMetrics()); - var group2 = AddCodex(2, s => s.EnableMetrics()); - - var primary = group[0]; - var secondary = group[1]; - var primary2 = group2[0]; - var secondary2 = group2[1]; - - var metrics = Ci.GetMetricsFor(primary, primary2); - - primary.ConnectToPeer(secondary); - primary2.ConnectToPeer(secondary2); - - Thread.Sleep(TimeSpan.FromMinutes(2)); - - metrics[0].AssertThat("libp2p_peers", Is.EqualTo(1)); - metrics[1].AssertThat("libp2p_peers", Is.EqualTo(1)); - } - - [Test] - public void MarketplaceExample() + public void BotRewardTest() { var sellerInitialBalance = 234.TestTokens(); var buyerInitialBalance = 1000.TestTokens(); @@ -80,6 +43,42 @@ namespace CodexTests.BasicTests AssertBalance(contracts, buyer, Is.EqualTo(buyerInitialBalance)); + // 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; + var contentId = buyer.UploadFile(testFile); var purchaseContract = buyer.Marketplace.RequestStorage(contentId, pricePerSlotPerSecond: 2.TestTokens(), @@ -126,21 +125,5 @@ namespace CodexTests.BasicTests //CheckLogForErrors(seller, buyer); } - [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)); - } } } diff --git a/Tests/CodexTests/CodexTests.csproj b/Tests/CodexTests/CodexTests.csproj index 2c04d03..495caf6 100644 --- a/Tests/CodexTests/CodexTests.csproj +++ b/Tests/CodexTests/CodexTests.csproj @@ -14,6 +14,7 @@ + diff --git a/Tools/BiblioTech/Rewards/RewardsApi.cs b/Tools/BiblioTech/Rewards/RewardsApi.cs index 1b4a87c..71055f9 100644 --- a/Tools/BiblioTech/Rewards/RewardsApi.cs +++ b/Tools/BiblioTech/Rewards/RewardsApi.cs @@ -58,9 +58,6 @@ namespace BiblioTech.Rewards { Program.Log.Error("Exception during HTTP handler: " + ex); } - // Whatever happens, everything's always OK. - context.Response.StatusCode = 200; - context.Response.OutputStream.Close(); }, nameof(HandleConnection)); } } diff --git a/Tools/CodexNetDeployer/Configuration.cs b/Tools/CodexNetDeployer/Configuration.cs index 1d93402..b4d23e2 100644 --- a/Tools/CodexNetDeployer/Configuration.cs +++ b/Tools/CodexNetDeployer/Configuration.cs @@ -112,6 +112,9 @@ namespace CodexNetDeployer [Uniform("dbot-adminchannelname", "dbotacn", "DBOTADMINCHANNELNAME", false, "Required if discord-bot is true. Name of the Discord channel in which admin commands are allowed.")] public string DiscordBotAdminChannelName { get; set; } = string.Empty; + + [Uniform("dbot-rewardchannelname", "dbotrcn", "DBOTREWARDCHANNELNAME", false, "Required if discord-bot is true. Name of the Discord channel in which reward updates are posted.")] + public string DiscordBotRewardChannelName { get; set; } = string.Empty; [Uniform("dbot-datapath", "dbotdp", "DBOTDATAPATH", false, "Optional. Path in container where bot will save all data.")] public string DiscordBotDataPath { get; set; } = string.Empty; @@ -159,6 +162,7 @@ namespace CodexNetDeployer StringIsSet(nameof(DiscordBotServerName), DiscordBotServerName, errors); StringIsSet(nameof(DiscordBotAdminRoleName), DiscordBotAdminRoleName, errors); StringIsSet(nameof(DiscordBotAdminChannelName), DiscordBotAdminChannelName, errors); + StringIsSet(nameof(DiscordBotRewardChannelName), DiscordBotRewardChannelName, errors); } return errors; diff --git a/Tools/CodexNetDeployer/Deployer.cs b/Tools/CodexNetDeployer/Deployer.cs index 1461784..066fc11 100644 --- a/Tools/CodexNetDeployer/Deployer.cs +++ b/Tools/CodexNetDeployer/Deployer.cs @@ -142,7 +142,8 @@ namespace CodexNetDeployer adminRoleName: config.DiscordBotAdminRoleName, adminChannelName: config.DiscordBotAdminChannelName, kubeNamespace: config.KubeNamespace, - gethInfo: info) + gethInfo: info, + rewardChannelName: config.DiscordBotRewardChannelName) { DataPath = config.DiscordBotDataPath });