diff --git a/Framework/DiscordRewards/GiveRewardsCommand.cs b/Framework/DiscordRewards/GiveRewardsCommand.cs index 67c611b..6ca5aa4 100644 --- a/Framework/DiscordRewards/GiveRewardsCommand.cs +++ b/Framework/DiscordRewards/GiveRewardsCommand.cs @@ -3,7 +3,7 @@ public class GiveRewardsCommand { public RewardUsersCommand[] Rewards { get; set; } = Array.Empty(); - public string[] EventsOverview { get; set; } = Array.Empty(); + public ChainEventMessage[] EventsOverview { get; set; } = Array.Empty(); public bool HasAny() { @@ -16,4 +16,10 @@ public ulong RewardId { get; set; } public string[] UserAddresses { get; set; } = Array.Empty(); } + + public class ChainEventMessage + { + public ulong BlockNumber { get; set; } + public string Message { get; set; } = string.Empty; + } } diff --git a/Tests/CodexTests/UtilityTests/DiscordBotTests.cs b/Tests/CodexTests/UtilityTests/DiscordBotTests.cs index c327feb..50094dc 100644 --- a/Tests/CodexTests/UtilityTests/DiscordBotTests.cs +++ b/Tests/CodexTests/UtilityTests/DiscordBotTests.cs @@ -23,7 +23,7 @@ namespace CodexTests.UtilityTests private readonly List hostAccounts = new List(); private readonly List rewardsSeen = new List(); private readonly TimeSpan rewarderInterval = TimeSpan.FromMinutes(1); - private readonly List receivedEvents = new List(); + private readonly List receivedEvents = new List(); [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($""); + 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) { diff --git a/Tools/BiblioTech/Rewards/ChainEventsSender.cs b/Tools/BiblioTech/Rewards/ChainEventsSender.cs index 07cb611..dadacdd 100644 --- a/Tools/BiblioTech/Rewards/ChainEventsSender.cs +++ b/Tools/BiblioTech/Rewards/ChainEventsSender.cs @@ -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); diff --git a/Tools/TestNetRewarder/EventsFormatter.cs b/Tools/TestNetRewarder/EventsFormatter.cs index 43c8605..651615d 100644 --- a/Tools/TestNetRewarder/EventsFormatter.cs +++ b/Tools/TestNetRewarder/EventsFormatter.cs @@ -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 events = new List(); + private readonly List events = new List(); 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()) { diff --git a/Tools/TestNetRewarder/RequestBuilder.cs b/Tools/TestNetRewarder/RequestBuilder.cs index 32b48fb..72403b9 100644 --- a/Tools/TestNetRewarder/RequestBuilder.cs +++ b/Tools/TestNetRewarder/RequestBuilder.cs @@ -19,7 +19,7 @@ namespace TestNetRewarder } } - public GiveRewardsCommand Build(string[] lines) + public GiveRewardsCommand Build(ChainEventMessage[] lines) { var result = new GiveRewardsCommand {