2
0
mirror of synced 2025-01-12 17:44:08 +00:00

Orders chain events by blocknumber

This commit is contained in:
benbierens 2024-10-14 09:43:05 +02:00
parent 5896735884
commit 269365e101
No known key found for this signature in database
GPG Key ID: 877D2C2E09A22F3A
5 changed files with 52 additions and 22 deletions

View File

@ -3,7 +3,7 @@
public class GiveRewardsCommand
{
public RewardUsersCommand[] Rewards { get; set; } = Array.Empty<RewardUsersCommand>();
public string[] EventsOverview { get; set; } = Array.Empty<string>();
public ChainEventMessage[] EventsOverview { get; set; } = Array.Empty<ChainEventMessage>();
public bool HasAny()
{
@ -16,4 +16,10 @@
public ulong RewardId { get; set; }
public string[] UserAddresses { get; set; } = Array.Empty<string>();
}
public class ChainEventMessage
{
public ulong BlockNumber { get; set; }
public string Message { get; set; } = string.Empty;
}
}

View File

@ -23,7 +23,7 @@ namespace CodexTests.UtilityTests
private readonly List<EthAccount> hostAccounts = new List<EthAccount>();
private readonly List<ulong> rewardsSeen = new List<ulong>();
private readonly TimeSpan rewarderInterval = TimeSpan.FromMinutes(1);
private readonly List<string> receivedEvents = new List<string>();
private readonly List<ChainEventMessage> receivedEvents = new List<ChainEventMessage>();
[Test]
[DontDownloadLogs]
@ -73,13 +73,18 @@ namespace CodexTests.UtilityTests
private void AssertEventOccurance(string msg, int expectedCount)
{
Assert.That(receivedEvents.Count(e => e.Contains(msg)), Is.EqualTo(expectedCount),
Assert.That(receivedEvents.Count(e => e.Message.Contains(msg)), Is.EqualTo(expectedCount),
$"Event '{msg}' did not occure correct number of times.");
}
private void OnCommand(string timestamp, GiveRewardsCommand call)
{
Log($"<API call {timestamp}>");
foreach (var e in call.EventsOverview)
{
Assert.That(receivedEvents.All(r => r.BlockNumber < e.BlockNumber), "Received event out of order.");
}
receivedEvents.AddRange(call.EventsOverview);
foreach (var e in call.EventsOverview)
{

View File

@ -1,5 +1,7 @@
using Discord.WebSocket;
using DiscordRewards;
using Logging;
using Microsoft.IdentityModel.Tokens;
namespace BiblioTech.Rewards
{
@ -16,7 +18,7 @@ namespace BiblioTech.Rewards
this.eventsChannel = eventsChannel;
}
public async Task ProcessChainEvents(string[] eventsOverview)
public async Task ProcessChainEvents(ChainEventMessage[] eventsOverview)
{
if (eventsChannel == null || eventsOverview == null || !eventsOverview.Any()) return;
try
@ -24,16 +26,7 @@ namespace BiblioTech.Rewards
await Task.Run(async () =>
{
var users = Program.UserRepo.GetAllUserData();
foreach (var e in eventsOverview)
{
if (!string.IsNullOrEmpty(e))
{
var @event = ApplyReplacements(users, e);
await eventsChannel.SendMessageAsync(@event);
await Task.Delay(1000);
}
}
await SendChainEventsInOrder(eventsOverview, eventsChannel, users);
});
}
catch (Exception ex)
@ -42,6 +35,21 @@ namespace BiblioTech.Rewards
}
}
private async Task SendChainEventsInOrder(ChainEventMessage[] eventsOverview, SocketTextChannel eventsChannel, UserData[] users)
{
eventsOverview = eventsOverview.OrderBy(e => e.BlockNumber).ToArray();
foreach (var e in eventsOverview)
{
var msg = e.Message;
if (!string.IsNullOrEmpty(msg))
{
var @event = ApplyReplacements(users, msg);
await eventsChannel.SendMessageAsync(@event);
await Task.Delay(300);
}
}
}
private string ApplyReplacements(UserData[] users, string msg)
{
var result = ApplyUserAddressReplacements(users, msg);

View File

@ -1,5 +1,6 @@
using CodexContractsPlugin;
using CodexContractsPlugin.ChainMonitor;
using DiscordRewards;
using GethPlugin;
using System.Globalization;
using System.Numerics;
@ -10,10 +11,10 @@ namespace TestNetRewarder
public class EventsFormatter : IChainStateChangeHandler
{
private static readonly string nl = Environment.NewLine;
private readonly List<string> events = new List<string>();
private readonly List<ChainEventMessage> events = new List<ChainEventMessage>();
private readonly EmojiMaps emojiMaps = new EmojiMaps();
public string[] GetEvents()
public ChainEventMessage[] GetEvents()
{
var result = events.ToArray();
events.Clear();
@ -22,7 +23,7 @@ namespace TestNetRewarder
public void AddError(string error)
{
AddBlock("📢 **Error**", error);
AddBlock(1, "📢 **Error**", error);
}
public void OnNewRequest(RequestEvent requestEvent)
@ -87,15 +88,25 @@ namespace TestNetRewarder
{
var blockNumber = $"[{requestEvent.Block.BlockNumber} {FormatDateTime(requestEvent.Block.Utc)}]";
var title = $"{blockNumber} **{eventName}** {FormatRequestId(requestEvent)}";
AddBlock(title, content);
AddBlock(requestEvent.Block.BlockNumber, title, content);
}
private void AddBlock(string title, params string[] content)
private void AddBlock(ulong blockNumber, string title, params string[] content)
{
events.Add(FormatBlock(title, content));
events.Add(FormatBlock(blockNumber, title, content));
}
private string FormatBlock(string title, params string[] content)
private ChainEventMessage FormatBlock(ulong blockNumber, string title, params string[] content)
{
var msg = FormatBlockMessage(title, content);
return new ChainEventMessage
{
BlockNumber = blockNumber,
Message = msg
};
}
private string FormatBlockMessage(string title, string[] content)
{
if (content == null || !content.Any())
{

View File

@ -19,7 +19,7 @@ namespace TestNetRewarder
}
}
public GiveRewardsCommand Build(string[] lines)
public GiveRewardsCommand Build(ChainEventMessage[] lines)
{
var result = new GiveRewardsCommand
{