cs-codex-dist-tests/Tests/CodexTests/UtilityTests/DiscordBotTests.cs

355 lines
13 KiB
C#
Raw Normal View History

2024-04-01 13:31:54 +00:00
using CodexContractsPlugin;
using CodexDiscordBotPlugin;
using CodexPlugin;
2024-05-21 14:10:14 +00:00
using Core;
using DiscordRewards;
2024-06-17 13:59:54 +00:00
using DistTestCore;
2024-04-01 13:31:54 +00:00
using GethPlugin;
2024-05-20 14:18:01 +00:00
using KubernetesWorkflow.Types;
2024-06-21 06:56:20 +00:00
using Logging;
2024-05-21 14:10:14 +00:00
using Newtonsoft.Json;
2024-04-01 13:31:54 +00:00
using NUnit.Framework;
using Utils;
2024-05-07 07:49:00 +00:00
namespace CodexTests.UtilityTests
2024-04-01 13:31:54 +00:00
{
[TestFixture]
public class DiscordBotTests : AutoBootstrapDistTest
{
2024-05-21 14:10:14 +00:00
private readonly RewardRepo repo = new RewardRepo();
private readonly TestToken hostInitialBalance = 3000000.TstWei();
private readonly TestToken clientInitialBalance = 1000000000.TstWei();
2024-05-23 09:37:57 +00:00
private readonly EthAccount clientAccount = EthAccount.GenerateNew();
2024-05-24 13:34:42 +00:00
private readonly List<EthAccount> hostAccounts = new List<EthAccount>();
2024-05-24 14:11:51 +00:00
private readonly List<ulong> rewardsSeen = new List<ulong>();
private readonly TimeSpan rewarderInterval = TimeSpan.FromMinutes(1);
2024-06-21 07:18:00 +00:00
private readonly List<string> receivedEvents = new List<string>();
2024-04-01 13:31:54 +00:00
[Test]
2024-06-17 13:59:54 +00:00
[DontDownloadLogs]
[Ignore("Used to debug testnet bots.")]
2024-04-01 13:31:54 +00:00
public void BotRewardTest()
{
var geth = Ci.StartGethNode(s => s.IsMiner().WithName("disttest-geth"));
var contracts = Ci.StartCodexContracts(geth);
2024-05-20 14:18:01 +00:00
var gethInfo = CreateGethInfo(geth, contracts);
var botContainer = StartDiscordBot(gethInfo);
2024-05-30 08:55:33 +00:00
var rewarderContainer = StartRewarderBot(gethInfo, botContainer);
2024-05-20 14:18:01 +00:00
2024-06-17 13:59:54 +00:00
StartHosts(geth, contracts);
2024-05-23 09:37:57 +00:00
var client = StartClient(geth, contracts);
2024-05-20 14:18:01 +00:00
2024-06-21 06:56:20 +00:00
var apiCalls = new RewardApiCalls(GetTestLog(), Ci, botContainer);
2024-05-23 09:37:57 +00:00
apiCalls.Start(OnCommand);
2024-05-20 14:18:01 +00:00
2024-05-30 08:55:33 +00:00
var purchaseContract = ClientPurchasesStorage(client);
2024-06-10 12:04:25 +00:00
purchaseContract.WaitForStorageContractStarted();
purchaseContract.WaitForStorageContractFinished();
2024-06-20 10:15:59 +00:00
Thread.Sleep(rewarderInterval * 3);
2024-06-21 06:56:20 +00:00
2024-06-10 12:04:25 +00:00
apiCalls.Stop();
2024-06-21 07:18:00 +00:00
2024-06-21 07:33:52 +00:00
AssertEventOccurance("Created as New.", 1);
AssertEventOccurance("SlotFilled", Convert.ToInt32(GetNumberOfRequiredHosts()));
AssertEventOccurance("Transit: New -> Started", 1);
AssertEventOccurance("Transit: Started -> Finished", 1);
2024-05-24 14:11:51 +00:00
foreach (var r in repo.Rewards)
{
var seen = rewardsSeen.Any(s => r.RoleId == s);
2024-06-10 12:04:25 +00:00
Log($"{Lookup(r.RoleId)} = {seen}");
2024-05-24 14:11:51 +00:00
}
Assert.That(repo.Rewards.All(r => rewardsSeen.Contains(r.RoleId)));
2024-05-23 09:37:57 +00:00
}
2024-05-21 14:10:14 +00:00
2024-06-10 12:04:25 +00:00
private string Lookup(ulong rewardId)
{
var reward = repo.Rewards.Single(r => r.RoleId == rewardId);
return $"({rewardId})'{reward.Message}'";
}
2024-06-21 07:33:52 +00:00
private void AssertEventOccurance(string msg, int expectedCount)
{
Assert.That(receivedEvents.Count(e => e.Contains(msg)), Is.EqualTo(expectedCount),
$"Event '{msg}' did not occure correct number of times.");
}
2024-06-21 06:56:20 +00:00
private void OnCommand(string timestamp, GiveRewardsCommand call)
2024-05-23 09:37:57 +00:00
{
2024-06-21 06:56:20 +00:00
Log($"<API call {timestamp}>");
2024-06-21 07:18:00 +00:00
receivedEvents.AddRange(call.EventsOverview);
2024-06-17 13:59:54 +00:00
foreach (var e in call.EventsOverview)
{
2024-06-20 10:15:59 +00:00
Log("\tEvent: " + e);
2024-06-17 13:59:54 +00:00
}
2024-05-23 09:37:57 +00:00
foreach (var r in call.Rewards)
2024-05-21 14:10:14 +00:00
{
2024-05-23 09:37:57 +00:00
var reward = repo.Rewards.Single(a => a.RoleId == r.RewardId);
2024-05-24 14:11:51 +00:00
if (r.UserAddresses.Any()) rewardsSeen.Add(reward.RoleId);
2024-05-24 13:34:42 +00:00
foreach (var address in r.UserAddresses)
{
var user = IdentifyAccount(address);
2024-06-20 10:15:59 +00:00
Log("\tReward: " + user + ": " + reward.Message);
2024-05-24 13:34:42 +00:00
}
2024-05-21 14:10:14 +00:00
}
2024-06-20 10:15:59 +00:00
Log($"</API call>");
2024-05-20 14:18:01 +00:00
}
2024-05-30 09:33:16 +00:00
private IStoragePurchaseContract ClientPurchasesStorage(ICodexNode client)
2024-05-20 14:18:01 +00:00
{
2024-05-21 14:10:14 +00:00
var testFile = GenerateTestFile(GetMinFileSize());
2024-05-20 14:18:01 +00:00
var contentId = client.UploadFile(testFile);
var purchase = new StoragePurchaseRequest(contentId)
{
PricePerSlotPerSecond = 2.TstWei(),
RequiredCollateral = 10.TstWei(),
2024-05-21 14:10:14 +00:00
MinRequiredNumberOfNodes = GetNumberOfRequiredHosts(),
2024-05-20 14:18:01 +00:00
NodeFailureTolerance = 2,
ProofProbability = 5,
2024-06-21 07:18:00 +00:00
Duration = GetMinRequiredRequestDuration(),
Expiry = GetMinRequiredRequestDuration() - TimeSpan.FromMinutes(1)
2024-05-20 14:18:01 +00:00
};
return client.Marketplace.RequestStorage(purchase);
}
2024-05-23 09:37:57 +00:00
private ICodexNode StartClient(IGethNode geth, ICodexContracts contracts)
2024-05-20 14:18:01 +00:00
{
2024-05-24 13:34:42 +00:00
var node = StartCodex(s => s
2024-05-20 14:18:01 +00:00
.WithName("Client")
.EnableMarketplace(geth, contracts, m => m
.WithAccount(clientAccount)
.WithInitial(10.Eth(), clientInitialBalance)));
2024-05-24 13:34:42 +00:00
Log($"Client {node.EthAccount.EthAddress}");
return node;
2024-05-20 14:18:01 +00:00
}
2024-05-30 08:55:33 +00:00
private RunningPod StartRewarderBot(DiscordBotGethInfo gethInfo, RunningContainer botContainer)
2024-05-20 14:18:01 +00:00
{
2024-05-30 08:55:33 +00:00
return Ci.DeployRewarderBot(new RewarderBotStartupConfig(
name: "rewarder-bot",
2024-05-20 14:18:01 +00:00
discordBotHost: botContainer.GetInternalAddress(DiscordBotContainerRecipe.RewardsPort).Host,
discordBotPort: botContainer.GetInternalAddress(DiscordBotContainerRecipe.RewardsPort).Port,
2024-05-24 14:11:51 +00:00
intervalMinutes: Convert.ToInt32(Math.Round(rewarderInterval.TotalMinutes)),
2024-05-20 14:18:01 +00:00
historyStartUtc: DateTime.UtcNow,
gethInfo: gethInfo,
dataPath: null
));
}
2024-04-01 13:31:54 +00:00
2024-05-20 14:18:01 +00:00
private DiscordBotGethInfo CreateGethInfo(IGethNode geth, ICodexContracts contracts)
{
return new DiscordBotGethInfo(
2024-04-01 13:31:54 +00:00
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
);
2024-05-20 14:18:01 +00:00
}
private RunningContainer StartDiscordBot(DiscordBotGethInfo gethInfo)
{
2024-04-01 13:31:54 +00:00
var bot = Ci.DeployCodexDiscordBot(new DiscordBotStartupConfig(
name: "discord-bot",
2024-04-01 13:31:54 +00:00
token: "aaa",
serverName: "ThatBen's server",
adminRoleName: "bottest-admins",
adminChannelName: "admin-channel",
rewardChannelName: "rewards-channel",
kubeNamespace: "notneeded",
gethInfo: gethInfo
));
2024-05-20 14:18:01 +00:00
return bot.Containers.Single();
}
2024-04-01 13:31:54 +00:00
2024-05-23 09:37:57 +00:00
private void StartHosts(IGethNode geth, ICodexContracts contracts)
2024-05-20 14:18:01 +00:00
{
2024-05-21 14:10:14 +00:00
var hosts = StartCodex(GetNumberOfLiveHosts(), s => s
2024-05-20 14:18:01 +00:00
.WithName("Host")
.WithLogLevel(CodexLogLevel.Trace, new CodexLogCustomTopics(CodexLogLevel.Error, CodexLogLevel.Error, CodexLogLevel.Warn)
{
ContractClock = CodexLogLevel.Trace,
})
2024-05-24 13:34:42 +00:00
.WithStorageQuota(Mult(GetMinFileSizePlus(50), GetNumberOfLiveHosts()))
2024-04-01 13:31:54 +00:00
.EnableMarketplace(geth, contracts, m => m
2024-05-20 14:18:01 +00:00
.WithInitial(10.Eth(), hostInitialBalance)
.AsStorageNode()
.AsValidator()));
2024-04-01 13:31:54 +00:00
2024-05-20 14:18:01 +00:00
var availability = new StorageAvailability(
2024-05-24 13:34:42 +00:00
totalSpace: Mult(GetMinFileSize(), GetNumberOfLiveHosts()),
2024-05-20 14:18:01 +00:00
maxDuration: TimeSpan.FromMinutes(30),
minPriceForTotalSpace: 1.TstWei(),
2024-05-21 14:10:14 +00:00
maxCollateral: hostInitialBalance
2024-05-20 14:18:01 +00:00
);
2024-04-01 13:31:54 +00:00
2024-05-20 14:18:01 +00:00
foreach (var host in hosts)
2024-04-01 13:31:54 +00:00
{
2024-05-24 13:34:42 +00:00
hostAccounts.Add(host.EthAccount);
2024-05-20 14:18:01 +00:00
host.Marketplace.MakeStorageAvailable(availability);
}
2024-04-01 13:31:54 +00:00
}
2024-05-21 14:10:14 +00:00
private int GetNumberOfLiveHosts()
{
return Convert.ToInt32(GetNumberOfRequiredHosts()) + 3;
}
2024-05-24 13:34:42 +00:00
private ByteSize Mult(ByteSize size, int mult)
{
return new ByteSize(size.SizeInBytes * mult);
}
private ByteSize GetMinFileSizePlus(int plusMb)
2024-05-21 14:10:14 +00:00
{
return new ByteSize(GetMinFileSize().SizeInBytes + plusMb.MB().SizeInBytes);
}
private ByteSize GetMinFileSize()
{
ulong minSlotSize = 0;
ulong minNumHosts = 0;
foreach (var r in repo.Rewards)
{
var s = Convert.ToUInt64(r.CheckConfig.MinSlotSize.SizeInBytes);
var h = r.CheckConfig.MinNumberOfHosts;
if (s > minSlotSize) minSlotSize = s;
if (h > minNumHosts) minNumHosts = h;
}
2024-05-24 13:34:42 +00:00
var minFileSize = ((minSlotSize + 1024) * minNumHosts);
2024-05-21 14:10:14 +00:00
return new ByteSize(Convert.ToInt64(minFileSize));
}
private uint GetNumberOfRequiredHosts()
{
return Convert.ToUInt32(repo.Rewards.Max(r => r.CheckConfig.MinNumberOfHosts));
}
2024-06-21 07:18:00 +00:00
private TimeSpan GetMinRequiredRequestDuration()
{
return repo.Rewards.Max(r => r.CheckConfig.MinDuration) + TimeSpan.FromSeconds(10);
}
2024-05-24 13:34:42 +00:00
private string IdentifyAccount(string address)
{
if (address == clientAccount.EthAddress.Address) return "Client";
try
{
var index = hostAccounts.FindIndex(a => a.EthAddress.Address == address);
return "Host" + index;
}
catch
{
return "UNKNOWN";
}
}
2024-05-21 14:10:14 +00:00
public class RewardApiCalls
2024-05-30 08:55:33 +00:00
{
private readonly ContainerFileMonitor monitor;
2024-06-21 06:56:20 +00:00
public RewardApiCalls(ILog log, CoreInterface ci, RunningContainer botContainer)
2024-05-30 08:55:33 +00:00
{
2024-06-21 06:56:20 +00:00
monitor = new ContainerFileMonitor(log, ci, botContainer, "/app/datapath/logs/discordbot.log");
2024-05-30 08:55:33 +00:00
}
2024-06-21 06:56:20 +00:00
public void Start(Action<string, GiveRewardsCommand> onCommand)
2024-05-30 08:55:33 +00:00
{
monitor.Start(line => ParseLine(line, onCommand));
}
public void Stop()
{
monitor.Stop();
}
2024-06-21 06:56:20 +00:00
private void ParseLine(string line, Action<string, GiveRewardsCommand> onCommand)
2024-05-30 08:55:33 +00:00
{
try
{
var timestamp = line.Substring(0, 30);
var json = line.Substring(31);
var cmd = JsonConvert.DeserializeObject<GiveRewardsCommand>(json);
if (cmd != null)
{
2024-06-21 06:56:20 +00:00
onCommand(timestamp, cmd);
2024-05-30 08:55:33 +00:00
}
}
catch
{
}
}
}
public class ContainerFileMonitor
2024-05-21 14:10:14 +00:00
{
2024-06-21 06:56:20 +00:00
private readonly ILog log;
2024-05-21 14:10:14 +00:00
private readonly CoreInterface ci;
private readonly RunningContainer botContainer;
2024-05-30 08:55:33 +00:00
private readonly string filePath;
2024-05-23 09:37:57 +00:00
private readonly CancellationTokenSource cts = new CancellationTokenSource();
2024-05-30 08:55:33 +00:00
private readonly List<string> seenLines = new List<string>();
2024-05-23 09:37:57 +00:00
private Task worker = Task.CompletedTask;
2024-05-30 08:55:33 +00:00
private Action<string> onNewLine = c => { };
2024-05-21 14:10:14 +00:00
2024-06-21 06:56:20 +00:00
public ContainerFileMonitor(ILog log, CoreInterface ci, RunningContainer botContainer, string filePath)
2024-05-21 14:10:14 +00:00
{
2024-06-21 06:56:20 +00:00
this.log = log;
2024-05-21 14:10:14 +00:00
this.ci = ci;
this.botContainer = botContainer;
2024-05-30 08:55:33 +00:00
this.filePath = filePath;
2024-05-21 14:10:14 +00:00
}
2024-05-30 08:55:33 +00:00
public void Start(Action<string> onNewLine)
2024-05-21 14:10:14 +00:00
{
2024-05-30 08:55:33 +00:00
this.onNewLine = onNewLine;
2024-05-23 09:37:57 +00:00
worker = Task.Run(Worker);
}
2024-05-21 14:10:14 +00:00
2024-05-23 09:37:57 +00:00
public void Stop()
{
cts.Cancel();
worker.Wait();
2024-05-21 14:10:14 +00:00
}
2024-06-21 06:56:20 +00:00
// did any container crash? that's why it repeats?
2024-05-23 09:37:57 +00:00
private void Worker()
2024-05-21 14:10:14 +00:00
{
2024-05-23 09:37:57 +00:00
while (!cts.IsCancellationRequested)
{
Update();
}
}
private void Update()
{
Thread.Sleep(TimeSpan.FromSeconds(10));
if (cts.IsCancellationRequested) return;
2024-05-30 08:55:33 +00:00
var botLog = ci.ExecuteContainerCommand(botContainer, "cat", filePath);
2024-05-23 09:37:57 +00:00
var lines = botLog.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
2024-06-21 06:56:20 +00:00
// log.Log("line: " + line);
2024-05-30 08:55:33 +00:00
if (!seenLines.Contains(line))
2024-05-23 09:37:57 +00:00
{
2024-05-30 08:55:33 +00:00
seenLines.Add(line);
onNewLine(line);
2024-05-23 09:37:57 +00:00
}
2024-05-21 14:10:14 +00:00
}
}
}
2024-04-01 13:31:54 +00:00
}
}