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-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.TestTokens();
|
2024-05-20 14:18:01 +00:00
|
|
|
|
private readonly TestToken clientInitialBalance = 1000000000.TestTokens();
|
|
|
|
|
|
2024-04-01 13:31:54 +00:00
|
|
|
|
[Test]
|
|
|
|
|
public void BotRewardTest()
|
|
|
|
|
{
|
2024-05-20 14:18:01 +00:00
|
|
|
|
var clientAccount = EthAccount.GenerateNew();
|
2024-04-01 13:31:54 +00:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
var hostAccount = EthAccount.GenerateNew();
|
|
|
|
|
StartHosts(hostAccount, geth, contracts);
|
|
|
|
|
|
|
|
|
|
StartRewarderBot(gethInfo, botContainer);
|
|
|
|
|
|
|
|
|
|
var client = StartClient(geth, contracts, clientAccount);
|
|
|
|
|
|
|
|
|
|
var purchaseContract = ClientPurchasesStorage(client);
|
|
|
|
|
|
2024-05-21 14:10:14 +00:00
|
|
|
|
var apiCalls = new RewardApiCalls(Ci, botContainer);
|
2024-05-20 14:18:01 +00:00
|
|
|
|
|
2024-05-21 14:10:14 +00:00
|
|
|
|
Time.WaitUntil(() => apiCalls.Get().Length > 4, TimeSpan.FromMinutes(10), TimeSpan.FromSeconds(10), "Waiting for API calls");
|
|
|
|
|
|
|
|
|
|
var calls = apiCalls.Get();
|
|
|
|
|
foreach (var call in calls)
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
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";
|
|
|
|
|
|
|
|
|
|
line += name + " = " + reward.Message;
|
|
|
|
|
}
|
|
|
|
|
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.TestTokens(),
|
|
|
|
|
RequiredCollateral = 10.TestTokens(),
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ICodexNode StartClient(IGethNode geth, ICodexContracts contracts, EthAccount clientAccount)
|
|
|
|
|
{
|
|
|
|
|
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-20 14:18:01 +00:00
|
|
|
|
private void StartHosts(EthAccount hostAccount, IGethNode geth, ICodexContracts contracts)
|
|
|
|
|
{
|
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.TestTokens(),
|
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>();
|
|
|
|
|
|
|
|
|
|
public RewardApiCalls(CoreInterface ci, RunningContainer botContainer)
|
|
|
|
|
{
|
|
|
|
|
this.ci = ci;
|
|
|
|
|
this.botContainer = botContainer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update()
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public GiveRewardsCommand[] Get()
|
|
|
|
|
{
|
|
|
|
|
Update();
|
|
|
|
|
return commands.Select(c => c.Value).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
if (cmd != null) commands.Add(timestamp, cmd);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-01 13:31:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|