From ac7d32320145a9195520b43a67a5e2e2514a018d Mon Sep 17 00:00:00 2001 From: benbierens Date: Mon, 3 Jun 2024 10:38:48 +0200 Subject: [PATCH] wip --- .../DoNothingChainEventHandler.cs | 35 ++++++++++++++ .../UtilityTests/DiscordBotTests.cs | 47 ++++++++++++------- 2 files changed, 64 insertions(+), 18 deletions(-) create mode 100644 ProjectPlugins/CodexContractsPlugin/ChainMonitor/DoNothingChainEventHandler.cs diff --git a/ProjectPlugins/CodexContractsPlugin/ChainMonitor/DoNothingChainEventHandler.cs b/ProjectPlugins/CodexContractsPlugin/ChainMonitor/DoNothingChainEventHandler.cs new file mode 100644 index 0000000..65e038b --- /dev/null +++ b/ProjectPlugins/CodexContractsPlugin/ChainMonitor/DoNothingChainEventHandler.cs @@ -0,0 +1,35 @@ +using System.Numerics; + +namespace CodexContractsPlugin.ChainMonitor +{ + public class DoNothingChainEventHandler : IChainStateChangeHandler + { + public void OnNewRequest(IChainStateRequest request) + { + } + + public void OnRequestCancelled(IChainStateRequest request) + { + } + + public void OnRequestFinished(IChainStateRequest request) + { + } + + public void OnRequestFulfilled(IChainStateRequest request) + { + } + + public void OnRequestStarted(IChainStateRequest request) + { + } + + public void OnSlotFilled(IChainStateRequest request, BigInteger slotIndex) + { + } + + public void OnSlotFreed(IChainStateRequest request, BigInteger slotIndex) + { + } + } +} diff --git a/Tests/CodexTests/UtilityTests/DiscordBotTests.cs b/Tests/CodexTests/UtilityTests/DiscordBotTests.cs index 998699f..c73cd23 100644 --- a/Tests/CodexTests/UtilityTests/DiscordBotTests.cs +++ b/Tests/CodexTests/UtilityTests/DiscordBotTests.cs @@ -1,4 +1,5 @@ using CodexContractsPlugin; +using CodexContractsPlugin.ChainMonitor; using CodexContractsPlugin.Marketplace; using CodexDiscordBotPlugin; using CodexPlugin; @@ -43,6 +44,14 @@ namespace CodexTests.UtilityTests var client = StartClient(geth, contracts); + + var events = ChainEvents.FromTimeRange(contracts, GetTestRunTimeRange()); + var chainState = CodexContractsPlugin.ChainMonitor.ChainState.FromEvents( + GetTestLog(), + events, + new DoNothingChainEventHandler()); + + var apiCalls = new RewardApiCalls(Ci, botContainer); apiCalls.Start(OnCommand); var rewarderLog = new RewarderLogMonitor(Ci, rewarderContainer.Containers.Single()); @@ -59,6 +68,8 @@ namespace CodexTests.UtilityTests Thread.Sleep(rewarderInterval * 2); + chainState.Apply(ChainEvents.FromTimeRange(contracts, GetTestRunTimeRange())); + Log("Seen:"); foreach (var seen in rewardsSeen) { @@ -78,8 +89,8 @@ namespace CodexTests.UtilityTests private void OnCommand(GiveRewardsCommand call) { - if (call.Averages.Any()) Log($"{call.Averages.Length} average."); - if (call.EventsOverview.Any()) Log($"{call.EventsOverview.Length} events."); + if (call.Averages.Any()) Log($"API call: {call.Averages.Length} average."); + if (call.EventsOverview.Any()) Log($"API call: {call.EventsOverview.Length} events."); foreach (var r in call.Rewards) { var reward = repo.Rewards.Single(a => a.RoleId == r.RewardId); @@ -87,7 +98,7 @@ namespace CodexTests.UtilityTests foreach (var address in r.UserAddresses) { var user = IdentifyAccount(address); - Log(user + ": " + reward.Message); + Log("API call: " + user + ": " + reward.Message); } } } @@ -306,20 +317,20 @@ namespace CodexTests.UtilityTests //$"ChainState=[{JsonConvert.SerializeObject(this)}]" + //$"HistoricState=[{historicState.EntireString()}]"; - var stateOpenTag = "ChainState=["; - var historicOpenTag = "]HistoricState=["; + //var stateOpenTag = "ChainState=["; + //var historicOpenTag = "]HistoricState=["; - if (!line.Contains(stateOpenTag)) return; - if (!line.Contains(historicOpenTag)) return; + //if (!line.Contains(stateOpenTag)) return; + //if (!line.Contains(historicOpenTag)) return; - var stateStr = Between(line, stateOpenTag, historicOpenTag); - var historicStr = Between(line, historicOpenTag, "]"); + //var stateStr = Between(line, stateOpenTag, historicOpenTag); + //var historicStr = Between(line, historicOpenTag, "]"); - var chainState = JsonConvert.DeserializeObject(stateStr); - var historicState = JsonConvert.DeserializeObject(historicStr)!; - chainState!.Set(new HistoricState(historicState)); + //var chainState = JsonConvert.DeserializeObject(stateStr); + //var historicState = JsonConvert.DeserializeObject(historicStr)!; + //chainState!.Set(new HistoricState(historicState)); - log(string.Join(",", chainState!.GenerateOverview())); + //log(string.Join(",", chainState!.GenerateOverview())); } private string Between(string s, string open, string close) @@ -427,14 +438,14 @@ namespace CodexTests.UtilityTests private void Update() { - var start = last; - var stop = DateTime.UtcNow; - last = stop; + //var start = last; + //var stop = DateTime.UtcNow; + //last = stop; - var range = geth.ConvertTimeRangeToBlockRange(new TimeRange(start, stop)); + //var range = geth.ConvertTimeRangeToBlockRange(new TimeRange(start, stop)); - throw new Exception(); + //throw new Exception(); //LogEvents(nameof(contracts.GetStorageRequests), contracts.GetStorageRequests, range); //LogEvents(nameof(contracts.GetRequestFulfilledEvents), contracts.GetRequestFulfilledEvents, range); //LogEvents(nameof(contracts.GetRequestCancelledEvents), contracts.GetRequestCancelledEvents, range);