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

453 lines
16 KiB
C#
Raw Normal View History

2024-04-01 13:31:54 +00:00
using CodexContractsPlugin;
2024-05-30 09:17:13 +00:00
using CodexContractsPlugin.Marketplace;
2024-04-01 13:31:54 +00:00
using CodexDiscordBotPlugin;
using CodexPlugin;
2024-05-21 14:10:14 +00:00
using Core;
using DiscordRewards;
2024-04-01 13:31:54 +00:00
using GethPlugin;
2024-05-20 14:18:01 +00:00
using KubernetesWorkflow.Types;
2024-05-23 09:37:57 +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;
2024-05-30 09:17:13 +00:00
using TestNetRewarder;
2024-04-01 13:31:54 +00:00
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-05-20 14:18:01 +00:00
2024-04-01 13:31:54 +00:00
[Test]
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);
2024-05-23 09:37:57 +00:00
var monitor = new ChainMonitor(contracts, geth, GetTestLog());
monitor.Start();
2024-05-20 14:18:01 +00:00
var botContainer = StartDiscordBot(gethInfo);
2024-05-23 09:37:57 +00:00
StartHosts(geth, contracts);
2024-05-20 14:18:01 +00:00
2024-05-30 08:55:33 +00:00
var rewarderContainer = StartRewarderBot(gethInfo, botContainer);
2024-05-20 14:18:01 +00:00
2024-05-23 09:37:57 +00:00
var client = StartClient(geth, contracts);
2024-05-20 14:18:01 +00:00
2024-05-21 14:10:14 +00:00
var apiCalls = new RewardApiCalls(Ci, botContainer);
2024-05-23 09:37:57 +00:00
apiCalls.Start(OnCommand);
2024-05-30 08:55:33 +00:00
var rewarderLog = new RewarderLogMonitor(Ci, rewarderContainer.Containers.Single());
rewarderLog.Start(l => Log("Rewarder ChainState: " + l));
2024-05-20 14:18:01 +00:00
2024-05-30 08:55:33 +00:00
var purchaseContract = ClientPurchasesStorage(client);
2024-05-24 13:34:42 +00:00
purchaseContract.WaitForStorageContractFinished();
2024-05-23 09:37:57 +00:00
2024-05-30 08:55:33 +00:00
rewarderLog.Stop();
2024-05-23 09:37:57 +00:00
apiCalls.Stop();
monitor.Stop();
2024-05-24 13:34:42 +00:00
Log("Done!");
2024-05-24 14:11:51 +00:00
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)));
2024-05-23 09:37:57 +00:00
}
2024-05-21 14:10:14 +00:00
2024-05-23 09:37:57 +00:00
private void OnCommand(GiveRewardsCommand call)
{
2024-05-24 13:34:42 +00:00
if (call.Averages.Any()) Log($"{call.Averages.Length} average.");
if (call.EventsOverview.Any()) Log($"{call.EventsOverview.Length} events.");
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);
Log(user + ": " + reward.Message);
}
2024-05-21 14:10:14 +00:00
}
2024-05-20 14:18:01 +00:00
}
private StoragePurchaseContract ClientPurchasesStorage(ICodexNode client)
{
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,
Duration = TimeSpan.FromMinutes(6),
Expiry = TimeSpan.FromMinutes(5)
};
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-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;
private readonly Dictionary<string, GiveRewardsCommand> commands = new Dictionary<string, GiveRewardsCommand>();
public RewardApiCalls(CoreInterface ci, RunningContainer botContainer)
{
monitor = new ContainerFileMonitor(ci, botContainer, "/app/datapath/logs/discordbot.log");
}
public void Start(Action<GiveRewardsCommand> onCommand)
{
monitor.Start(line => ParseLine(line, onCommand));
}
public void Stop()
{
monitor.Stop();
}
private void ParseLine(string line, Action<GiveRewardsCommand> onCommand)
{
try
{
var timestamp = line.Substring(0, 30);
if (commands.ContainsKey(timestamp)) return;
var json = line.Substring(31);
var cmd = JsonConvert.DeserializeObject<GiveRewardsCommand>(json);
if (cmd != null)
{
commands.Add(timestamp, cmd);
onCommand(cmd);
}
}
catch
{
}
}
}
public class RewarderLogMonitor
{
private readonly ContainerFileMonitor monitor;
private readonly Dictionary<string, GiveRewardsCommand> commands = new Dictionary<string, GiveRewardsCommand>();
public RewarderLogMonitor(CoreInterface ci, RunningContainer botContainer)
{
monitor = new ContainerFileMonitor(ci, botContainer, "/app/datapath/logs/testnetrewarder.log");
}
public void Start(Action<string> onCommand)
{
monitor.Start(l => ProcessLine(l, onCommand));
}
public void Stop()
{
monitor.Stop();
}
private void ProcessLine(string line, Action<string> log)
{
2024-05-30 09:17:13 +00:00
//$"ChainState=[{JsonConvert.SerializeObject(this)}]" +
//$"HistoricState=[{historicState.EntireString()}]";
var stateOpenTag = "ChainState=[";
var historicOpenTag = "]HistoricState=[";
if (!line.Contains(stateOpenTag)) return;
if (!line.Contains(historicOpenTag)) return;
var stateStr = Between(line, stateOpenTag, historicOpenTag);
var historicStr = Between(line, historicOpenTag, "]");
var chainState = JsonConvert.DeserializeObject<ChainState>(stateStr);
var historicState = JsonConvert.DeserializeObject<TestNetRewarder.StorageRequest[]>(historicStr)!;
chainState!.Set(new HistoricState(historicState));
log(string.Join(",", chainState!.GenerateOverview()));
2024-05-30 08:55:33 +00:00
}
2024-05-30 09:17:13 +00:00
private string Between(string s, string open, string close)
2024-05-30 08:55:33 +00:00
{
2024-05-30 09:17:13 +00:00
var start = s.IndexOf(open) + open.Length;
var end = s.LastIndexOf(close);
2024-05-30 08:55:33 +00:00
return s.Substring(start, end - start);
}
}
public class ContainerFileMonitor
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-05-30 08:55:33 +00:00
public ContainerFileMonitor(CoreInterface ci, RunningContainer botContainer, string filePath)
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-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-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-05-23 09:37:57 +00:00
public class ChainMonitor
{
private readonly ICodexContracts contracts;
private readonly IGethNode geth;
private readonly ILog log;
private readonly CancellationTokenSource cts = new CancellationTokenSource();
private Task worker = Task.CompletedTask;
private DateTime last = DateTime.UtcNow;
public ChainMonitor(ICodexContracts contracts, IGethNode geth, ILog log)
{
this.contracts = contracts;
this.geth = geth;
this.log = log;
}
public void Start()
{
last = DateTime.UtcNow;
worker = Task.Run(Worker);
}
public void Stop()
{
cts.Cancel();
worker.Wait();
}
private void Worker()
{
while (!cts.IsCancellationRequested)
{
Thread.Sleep(TimeSpan.FromSeconds(10));
if (cts.IsCancellationRequested) return;
Update();
}
}
private void Update()
{
var start = last;
var stop = DateTime.UtcNow;
last = stop;
var range = geth.ConvertTimeRangeToBlockRange(new TimeRange(start, stop));
LogEvents(nameof(contracts.GetStorageRequests), contracts.GetStorageRequests, range);
LogEvents(nameof(contracts.GetRequestFulfilledEvents), contracts.GetRequestFulfilledEvents, range);
LogEvents(nameof(contracts.GetRequestCancelledEvents), contracts.GetRequestCancelledEvents, range);
LogEvents(nameof(contracts.GetSlotFilledEvents), contracts.GetSlotFilledEvents, range);
LogEvents(nameof(contracts.GetSlotFreedEvents), contracts.GetSlotFreedEvents, range);
}
private void LogEvents(string n, Func<BlockInterval, object> f, BlockInterval r)
{
var a = (object[])f(r);
a.ToList().ForEach(request => log.Log(n + " - " + JsonConvert.SerializeObject(request)));
}
}
2024-04-01 13:31:54 +00:00
}
}