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

330 lines
12 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-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;
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();
private readonly EthAccount hostAccount = EthAccount.GenerateNew();
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
StartRewarderBot(gethInfo, botContainer);
2024-05-23 09:37:57 +00:00
var client = StartClient(geth, contracts);
2024-05-20 14:18:01 +00:00
var purchaseContract = ClientPurchasesStorage(client);
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-20 14:18:01 +00:00
2024-05-23 09:37:57 +00:00
Thread.Sleep(TimeSpan.FromMinutes(10));
apiCalls.Stop();
monitor.Stop();
}
2024-05-21 14:10:14 +00:00
2024-05-23 09:37:57 +00:00
private void OnCommand(GiveRewardsCommand call)
{
var line = "";
if (call.Averages.Any()) line += $"{call.Averages.Length} average. ";
if (call.EventsOverview.Any()) line += $"{call.EventsOverview.Length} events. ";
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);
var isClient = r.UserAddresses.Any(a => a == clientAccount.EthAddress.Address);
var isHost = r.UserAddresses.Any(a => a == hostAccount.EthAddress.Address);
if (isHost && isClient) throw new Exception("what?");
var name = isClient ? "Client" : "Host";
2024-05-21 14:10:14 +00:00
2024-05-23 09:37:57 +00:00
line += name + " = " + reward.Message;
2024-05-21 14:10:14 +00:00
}
2024-05-23 09:37:57 +00:00
Log(line);
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
{
return StartCodex(s => s
.WithName("Client")
.EnableMarketplace(geth, contracts, m => m
.WithAccount(clientAccount)
.WithInitial(10.Eth(), clientInitialBalance)));
}
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
));
}
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: "bot",
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-21 14:10:14 +00:00
.WithStorageQuota(GetFileSizePlus(50))
2024-04-01 13:31:54 +00:00
.EnableMarketplace(geth, contracts, m => m
2024-05-20 14:18:01 +00:00
.WithAccount(hostAccount)
.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-21 14:10:14 +00:00
totalSpace: GetFileSizePlus(5),
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-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;
}
private ByteSize GetFileSizePlus(int plusMb)
{
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;
}
var minFileSize = (minSlotSize * minNumHosts) + 1024;
return new ByteSize(Convert.ToInt64(minFileSize));
}
private uint GetNumberOfRequiredHosts()
{
return Convert.ToUInt32(repo.Rewards.Max(r => r.CheckConfig.MinNumberOfHosts));
}
public class RewardApiCalls
{
private readonly CoreInterface ci;
private readonly RunningContainer botContainer;
private readonly Dictionary<string, GiveRewardsCommand> commands = new Dictionary<string, GiveRewardsCommand>();
2024-05-23 09:37:57 +00:00
private readonly CancellationTokenSource cts = new CancellationTokenSource();
private Task worker = Task.CompletedTask;
private Action<GiveRewardsCommand> onCommand = c => { };
2024-05-21 14:10:14 +00:00
public RewardApiCalls(CoreInterface ci, RunningContainer botContainer)
{
this.ci = ci;
this.botContainer = botContainer;
}
2024-05-23 09:37:57 +00:00
public void Start(Action<GiveRewardsCommand> onCommand)
2024-05-21 14:10:14 +00:00
{
2024-05-23 09:37:57 +00:00
this.onCommand = onCommand;
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;
var botLog = ci.ExecuteContainerCommand(botContainer, "cat", "/app/datapath/logs/discordbot.log");
var lines = botLog.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
AddToCache(line);
}
2024-05-21 14:10:14 +00:00
}
private void AddToCache(string line)
{
try
{
var timestamp = line.Substring(0, 30);
if (commands.ContainsKey(timestamp)) return;
var json = line.Substring(31);
var cmd = JsonConvert.DeserializeObject<GiveRewardsCommand>(json);
2024-05-23 09:37:57 +00:00
if (cmd != null)
{
commands.Add(timestamp, cmd);
onCommand(cmd);
}
2024-05-21 14:10:14 +00:00
}
catch
{
}
}
}
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
}
}