blockTimeFinder now uses inclusive time ranges

This commit is contained in:
benbierens 2024-10-18 10:00:20 +02:00
parent 60b489ced1
commit e29ffe4f9c
No known key found for this signature in database
GPG Key ID: 877D2C2E09A22F3A
3 changed files with 44 additions and 17 deletions

View File

@ -29,7 +29,8 @@ namespace NethereumWorkflow.BlockUtils
public ulong? GetHighestBlockNumberBefore(DateTime moment) public ulong? GetHighestBlockNumberBefore(DateTime moment)
{ {
bounds.Initialize(); 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; if (moment >= bounds.Current.Utc) return bounds.Current.BlockNumber;
return Log(() => Search(bounds.Genesis, bounds.Current, moment, HighestBeforeSelector)); return Log(() => Search(bounds.Genesis, bounds.Current, moment, HighestBeforeSelector));
@ -38,7 +39,8 @@ namespace NethereumWorkflow.BlockUtils
public ulong? GetLowestBlockNumberAfter(DateTime moment) public ulong? GetLowestBlockNumberAfter(DateTime moment)
{ {
bounds.Initialize(); 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; if (moment <= bounds.Genesis.Utc) return bounds.Genesis.BlockNumber;
return Log(()=> Search(bounds.Genesis, bounds.Current, moment, LowestAfterSelector)); ; return Log(()=> Search(bounds.Genesis, bounds.Current, moment, LowestAfterSelector)); ;

View File

@ -20,10 +20,19 @@ namespace FrameworkTests.NethereumWorkflow
var start = DateTime.UtcNow.AddDays(-1).AddSeconds(-30); var start = DateTime.UtcNow.AddDays(-1).AddSeconds(-30);
blocks = new Dictionary<ulong, Block>(); blocks = new Dictionary<ulong, Block>();
Block? prev = null;
for (ulong i = 0; i < 30; i++) for (ulong i = 0; i < 30; i++)
{ {
ulong d = 100 + 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] [Test]
public void FailsToFindBlockBeforeFrontOfChain() public void FindsGenesisBlockAtFrontOfChain()
{ {
var first = blocks.First().Value; 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] [Test]
public void FailsToFindBlockAfterTailOfChain() public void FindsCurrentBlockAtTailOfChain()
{ {
var last = blocks.Last().Value; 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] [Test]
@ -143,13 +152,27 @@ namespace FrameworkTests.NethereumWorkflow
{ {
foreach (var pair in blocks) foreach (var pair in blocks)
{ {
finder.GetHighestBlockNumberBefore(pair.Value.JustBefore); var block = pair.Value;
finder.GetHighestBlockNumberBefore(pair.Value.Time);
finder.GetHighestBlockNumberBefore(pair.Value.JustAfter);
finder.GetLowestBlockNumberAfter(pair.Value.JustBefore); AssertLink(block.Previous, finder.GetHighestBlockNumberBefore(block.JustBefore));
finder.GetLowestBlockNumberAfter(pair.Value.Time); AssertLink(block, finder.GetHighestBlockNumberBefore(block.Time));
finder.GetLowestBlockNumberAfter(pair.Value.JustAfter); 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 JustBefore { get { return Time.AddSeconds(-1); } }
public DateTime JustAfter { 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() public override string ToString()
{ {
return $"[{Number}]"; return $"[{Number}]";

View File

@ -18,7 +18,6 @@ namespace TestNetRewarder
public async Task<bool> IsOnline() public async Task<bool> IsOnline()
{ {
var result = await HttpGet(); var result = await HttpGet();
log.Log("Is DiscordBot online: " + result);
return result == "Pong"; return result == "Pong";
} }