diff --git a/Framework/NethereumWorkflow/BlockUtils/BlockTimeFinder.cs b/Framework/NethereumWorkflow/BlockUtils/BlockTimeFinder.cs index d9a6ac2..c174de9 100644 --- a/Framework/NethereumWorkflow/BlockUtils/BlockTimeFinder.cs +++ b/Framework/NethereumWorkflow/BlockUtils/BlockTimeFinder.cs @@ -29,7 +29,8 @@ namespace NethereumWorkflow.BlockUtils public ulong? GetHighestBlockNumberBefore(DateTime moment) { bounds.Initialize(); - if (moment <= bounds.Genesis.Utc) return null; + if (moment < bounds.Genesis.Utc) return null; + if (moment == bounds.Genesis.Utc) return bounds.Genesis.BlockNumber; if (moment >= bounds.Current.Utc) return bounds.Current.BlockNumber; return Log(() => Search(bounds.Genesis, bounds.Current, moment, HighestBeforeSelector)); @@ -38,7 +39,8 @@ namespace NethereumWorkflow.BlockUtils public ulong? GetLowestBlockNumberAfter(DateTime moment) { bounds.Initialize(); - if (moment >= bounds.Current.Utc) return null; + if (moment > bounds.Current.Utc) return null; + if (moment == bounds.Current.Utc) return bounds.Current.BlockNumber; if (moment <= bounds.Genesis.Utc) return bounds.Genesis.BlockNumber; return Log(()=> Search(bounds.Genesis, bounds.Current, moment, LowestAfterSelector)); ; diff --git a/Tests/FrameworkTests/NethereumWorkflow/BlockTimeFinderTests.cs b/Tests/FrameworkTests/NethereumWorkflow/BlockTimeFinderTests.cs index d2be5da..17f5ee6 100644 --- a/Tests/FrameworkTests/NethereumWorkflow/BlockTimeFinderTests.cs +++ b/Tests/FrameworkTests/NethereumWorkflow/BlockTimeFinderTests.cs @@ -19,11 +19,20 @@ namespace FrameworkTests.NethereumWorkflow { var start = DateTime.UtcNow.AddDays(-1).AddSeconds(-30); blocks = new Dictionary(); - + + Block? prev = null; for (ulong i = 0; i < 30; i++) { ulong d = 100 + i; - blocks.Add(d, new Block(d, start + TimeSpan.FromSeconds(i * 2))); + var newBlock = new Block(d, start + TimeSpan.FromSeconds(i * 2)); + blocks.Add(d, newBlock); + + if (prev != null) + { + prev.Next = newBlock; + newBlock.Previous = prev; + } + prev = newBlock; } } @@ -99,23 +108,23 @@ namespace FrameworkTests.NethereumWorkflow } [Test] - public void FailsToFindBlockBeforeFrontOfChain() + public void FindsGenesisBlockAtFrontOfChain() { var first = blocks.First().Value; - var notFound = finder.GetHighestBlockNumberBefore(first.Time); + var firstNumber = finder.GetHighestBlockNumberBefore(first.Time); - Assert.That(notFound, Is.Null); + Assert.That(firstNumber, Is.EqualTo(first.Number)); } [Test] - public void FailsToFindBlockAfterTailOfChain() + public void FindsCurrentBlockAtTailOfChain() { var last = blocks.Last().Value; - var notFound = finder.GetLowestBlockNumberAfter(last.Time); + var lastNumber = finder.GetLowestBlockNumberAfter(last.Time); - Assert.That(notFound, Is.Null); + Assert.That(lastNumber, Is.EqualTo(last.Number)); } [Test] @@ -143,13 +152,27 @@ namespace FrameworkTests.NethereumWorkflow { foreach (var pair in blocks) { - finder.GetHighestBlockNumberBefore(pair.Value.JustBefore); - finder.GetHighestBlockNumberBefore(pair.Value.Time); - finder.GetHighestBlockNumberBefore(pair.Value.JustAfter); + var block = pair.Value; - finder.GetLowestBlockNumberAfter(pair.Value.JustBefore); - finder.GetLowestBlockNumberAfter(pair.Value.Time); - finder.GetLowestBlockNumberAfter(pair.Value.JustAfter); + AssertLink(block.Previous, finder.GetHighestBlockNumberBefore(block.JustBefore)); + AssertLink(block, finder.GetHighestBlockNumberBefore(block.Time)); + AssertLink(block, finder.GetHighestBlockNumberBefore(block.JustAfter)); + + AssertLink(block, finder.GetLowestBlockNumberAfter(block.JustBefore)); + AssertLink(block, finder.GetLowestBlockNumberAfter(block.Time)); + AssertLink(block.Next, finder.GetLowestBlockNumberAfter(block.JustAfter)); + } + } + + private void AssertLink(Block? expected, ulong? actual) + { + if (expected == null) + { + Assert.That(actual, Is.Null); + } + else + { + Assert.That(expected.Number, Is.EqualTo(actual!.Value)); } } } @@ -167,6 +190,9 @@ namespace FrameworkTests.NethereumWorkflow public DateTime JustBefore { get { return Time.AddSeconds(-1); } } public DateTime JustAfter { get { return Time.AddSeconds(1); } } + public Block? Next { get; set; } + public Block? Previous { get; set; } + public override string ToString() { return $"[{Number}]"; diff --git a/Tools/TestNetRewarder/BotClient.cs b/Tools/TestNetRewarder/BotClient.cs index 3ee1fb9..6a5c575 100644 --- a/Tools/TestNetRewarder/BotClient.cs +++ b/Tools/TestNetRewarder/BotClient.cs @@ -18,7 +18,6 @@ namespace TestNetRewarder public async Task IsOnline() { var result = await HttpGet(); - log.Log("Is DiscordBot online: " + result); return result == "Pong"; }