From ba9e4b098f06a034ca2b4309ea1676a89a438875 Mon Sep 17 00:00:00 2001 From: benbierens Date: Fri, 24 May 2024 16:11:51 +0200 Subject: [PATCH] checking that all rewards are sent. --- .../CodexPlugin/CodexContainerRecipe.cs | 5 ++- ProjectPlugins/CodexPlugin/CodexSetup.cs | 37 +++++++++++++++++-- ProjectPlugins/GethPlugin/EthAccount.cs | 5 +++ .../UtilityTests/DiscordBotTests.cs | 27 +++++++++++--- 4 files changed, 64 insertions(+), 10 deletions(-) diff --git a/ProjectPlugins/CodexPlugin/CodexContainerRecipe.cs b/ProjectPlugins/CodexPlugin/CodexContainerRecipe.cs index 7bc308ac..07b94ac4 100644 --- a/ProjectPlugins/CodexPlugin/CodexContainerRecipe.cs +++ b/ProjectPlugins/CodexPlugin/CodexContainerRecipe.cs @@ -109,8 +109,9 @@ namespace CodexPlugin // Custom scripting in the Codex test image will write this variable to a private-key file, // and pass the correct filename to Codex. - AddEnvVar("PRIV_KEY", marketplaceSetup.EthAccount.PrivateKey); - Additional(marketplaceSetup.EthAccount); + var account = marketplaceSetup.EthAccountSetup.GetNew(); + AddEnvVar("PRIV_KEY", account.PrivateKey); + Additional(account); SetCommandOverride(marketplaceSetup); if (marketplaceSetup.IsValidator) diff --git a/ProjectPlugins/CodexPlugin/CodexSetup.cs b/ProjectPlugins/CodexPlugin/CodexSetup.cs index 04aaf427..44454c77 100644 --- a/ProjectPlugins/CodexPlugin/CodexSetup.cs +++ b/ProjectPlugins/CodexPlugin/CodexSetup.cs @@ -169,7 +169,7 @@ namespace CodexPlugin public bool IsValidator { get; private set; } public Ether InitialEth { get; private set; } = 0.Eth(); public TestToken InitialTestTokens { get; private set; } = 0.Tst(); - public EthAccount EthAccount { get; private set; } = EthAccount.GenerateNew(); + public EthAccountSetup EthAccountSetup { get; private set; } = new EthAccountSetup(); public IMarketplaceSetup AsStorageNode() { @@ -185,7 +185,7 @@ namespace CodexPlugin public IMarketplaceSetup WithAccount(EthAccount account) { - EthAccount = account; + EthAccountSetup.Pin(account); return this; } @@ -201,10 +201,41 @@ namespace CodexPlugin var result = "[(clientNode)"; // When marketplace is enabled, being a clientNode is implicit. result += IsStorageNode ? "(storageNode)" : "()"; result += IsValidator ? "(validator)" : "() "; - result += $"Address: '{EthAccount.EthAddress}' "; + result += $"Address: '{EthAccountSetup}' "; result += $"{InitialEth.Eth} / {InitialTestTokens}"; result += "] "; return result; } } + + public class EthAccountSetup + { + private readonly List accounts = new List(); + private bool pinned = false; + + public void Pin(EthAccount account) + { + accounts.Add(account); + pinned = true; + } + + public EthAccount GetNew() + { + if (pinned) return accounts.Last(); + + var a = EthAccount.GenerateNew(); + accounts.Add(a); + return a; + } + + public EthAccount[] GetAll() + { + return accounts.ToArray(); + } + + public override string ToString() + { + return string.Join(",", accounts.Select(a => a.ToString()).ToArray()); + } + } } diff --git a/ProjectPlugins/GethPlugin/EthAccount.cs b/ProjectPlugins/GethPlugin/EthAccount.cs index 9f1318b7..60bf40d8 100644 --- a/ProjectPlugins/GethPlugin/EthAccount.cs +++ b/ProjectPlugins/GethPlugin/EthAccount.cs @@ -24,5 +24,10 @@ namespace GethPlugin return new EthAccount(ethAddress, account.PrivateKey); } + + public override string ToString() + { + return EthAddress.ToString(); + } } } diff --git a/Tests/CodexTests/UtilityTests/DiscordBotTests.cs b/Tests/CodexTests/UtilityTests/DiscordBotTests.cs index ab631120..d7ebd8db 100644 --- a/Tests/CodexTests/UtilityTests/DiscordBotTests.cs +++ b/Tests/CodexTests/UtilityTests/DiscordBotTests.cs @@ -20,6 +20,8 @@ namespace CodexTests.UtilityTests private readonly TestToken clientInitialBalance = 1000000000.TstWei(); private readonly EthAccount clientAccount = EthAccount.GenerateNew(); private readonly List hostAccounts = new List(); + private readonly List rewardsSeen = new List(); + private readonly TimeSpan rewarderInterval = TimeSpan.FromMinutes(1); [Test] public void BotRewardTest() @@ -50,6 +52,24 @@ namespace CodexTests.UtilityTests monitor.Stop(); Log("Done!"); + + Thread.Sleep(rewarderInterval * 2); + + Log("Seen:"); + foreach (var seen in rewardsSeen) + { + Log(seen.ToString()); + } + Log(""); + + foreach (var r in repo.Rewards) + { + var seen = rewardsSeen.Any(s => r.RoleId == s); + + Log($"{r.RoleId} = {seen}"); + } + + Assert.That(repo.Rewards.All(r => rewardsSeen.Contains(r.RoleId))); } private void OnCommand(GiveRewardsCommand call) @@ -59,6 +79,7 @@ namespace CodexTests.UtilityTests foreach (var r in call.Rewards) { var reward = repo.Rewards.Single(a => a.RoleId == r.RewardId); + if (r.UserAddresses.Any()) rewardsSeen.Add(reward.RoleId); foreach (var address in r.UserAddresses) { var user = IdentifyAccount(address); @@ -102,7 +123,7 @@ namespace CodexTests.UtilityTests Ci.DeployRewarderBot(new RewarderBotStartupConfig( discordBotHost: botContainer.GetInternalAddress(DiscordBotContainerRecipe.RewardsPort).Host, discordBotPort: botContainer.GetInternalAddress(DiscordBotContainerRecipe.RewardsPort).Port, - intervalMinutes: 1, + intervalMinutes: Convert.ToInt32(Math.Round(rewarderInterval.TotalMinutes)), historyStartUtc: DateTime.UtcNow, gethInfo: gethInfo, dataPath: null @@ -157,14 +178,10 @@ namespace CodexTests.UtilityTests maxCollateral: hostInitialBalance ); - var i = 0; foreach (var host in hosts) { hostAccounts.Add(host.EthAccount); host.Marketplace.MakeStorageAvailable(availability); - - Log($"Host{i} {host.EthAccount.EthAddress}"); - i++; } }