cs-codex-dist-tests/Framework/NethereumWorkflow/BlockTimeFinder.cs

254 lines
8.5 KiB
C#
Raw Normal View History

2023-12-20 08:48:22 +00:00
using Logging;
using Nethereum.RPC.Eth.DTOs;
2023-12-19 14:30:46 +00:00
using Nethereum.Web3;
using Utils;
namespace NethereumWorkflow
{
2023-12-20 09:33:44 +00:00
public partial class BlockTimeFinder
2023-12-19 14:30:46 +00:00
{
private const ulong FetchRange = 6;
private const int MaxEntries = 1024;
2023-12-20 09:33:44 +00:00
private static readonly Dictionary<ulong, BlockTimeEntry> entries = new Dictionary<ulong, BlockTimeEntry>();
2023-12-19 14:30:46 +00:00
private readonly Web3 web3;
2023-12-20 08:48:22 +00:00
private readonly ILog log;
2023-12-19 14:30:46 +00:00
2023-12-20 08:48:22 +00:00
public BlockTimeFinder(Web3 web3, ILog log)
2023-12-19 14:30:46 +00:00
{
this.web3 = web3;
2023-12-20 08:48:22 +00:00
this.log = log;
2023-12-19 14:30:46 +00:00
}
public ulong GetHighestBlockNumberBefore(DateTime moment)
{
2023-12-20 08:48:22 +00:00
log.Log("Looking for highest block before " + moment.ToString("o"));
2023-12-19 14:30:46 +00:00
AssertMomentIsInPast(moment);
Initialize();
var closestBefore = FindClosestBeforeEntry(moment);
var closestAfter = FindClosestAfterEntry(moment);
2023-12-20 08:48:22 +00:00
2023-12-20 09:33:44 +00:00
if (closestBefore != null &&
closestAfter != null &&
closestBefore.Utc < moment &&
2023-12-19 14:30:46 +00:00
closestAfter.Utc > moment &&
closestBefore.BlockNumber + 1 == closestAfter.BlockNumber)
{
2023-12-20 08:48:22 +00:00
log.Log("Found highest-Before: " + closestBefore);
2023-12-19 14:30:46 +00:00
return closestBefore.BlockNumber;
}
FetchBlocksAround(moment);
return GetHighestBlockNumberBefore(moment);
}
public ulong GetLowestBlockNumberAfter(DateTime moment)
{
2023-12-20 08:48:22 +00:00
log.Log("Looking for lowest block after " + moment.ToString("o"));
2023-12-19 14:30:46 +00:00
AssertMomentIsInPast(moment);
Initialize();
var closestBefore = FindClosestBeforeEntry(moment);
var closestAfter = FindClosestAfterEntry(moment);
2023-12-20 09:33:44 +00:00
if (closestBefore != null &&
closestAfter != null &&
closestBefore.Utc < moment &&
2023-12-19 14:30:46 +00:00
closestAfter.Utc > moment &&
closestBefore.BlockNumber + 1 == closestAfter.BlockNumber)
{
2023-12-20 08:48:22 +00:00
log.Log("Found lowest-after: " + closestAfter);
2023-12-19 14:30:46 +00:00
return closestAfter.BlockNumber;
}
FetchBlocksAround(moment);
return GetLowestBlockNumberAfter(moment);
}
private void FetchBlocksAround(DateTime moment)
{
var timePerBlock = EstimateTimePerBlock();
EnsureRecentBlockIfNecessary(moment, timePerBlock);
var max = entries.Keys.Max();
2023-12-20 09:33:44 +00:00
var blockDifference = CalculateBlockDifference(moment, timePerBlock, max);
2023-12-19 14:30:46 +00:00
2023-12-20 09:33:44 +00:00
FetchUp(max, blockDifference);
FetchDown(max, blockDifference);
}
2023-12-19 14:30:46 +00:00
2023-12-20 09:33:44 +00:00
private void FetchDown(ulong max, ulong blockDifference)
{
var target = max - blockDifference - 1;
2023-12-20 08:48:22 +00:00
var fetchDown = FetchRange;
2023-12-20 09:33:44 +00:00
while (fetchDown > 0)
2023-12-19 14:30:46 +00:00
{
2023-12-20 08:48:22 +00:00
if (!entries.ContainsKey(target))
{
var newBlock = AddBlockNumber(target);
2023-12-20 09:33:44 +00:00
if (newBlock == null) return;
fetchDown--;
2023-12-20 08:48:22 +00:00
}
2023-12-20 09:33:44 +00:00
target--;
if (target <= 0) return;
2023-12-20 08:48:22 +00:00
}
2023-12-20 09:33:44 +00:00
}
2023-12-20 08:48:22 +00:00
2023-12-20 09:33:44 +00:00
private void FetchUp(ulong max, ulong blockDifference)
{
var target = max - blockDifference;
var fetchUp = FetchRange;
while (fetchUp > 0)
2023-12-20 08:48:22 +00:00
{
if (!entries.ContainsKey(target))
{
var newBlock = AddBlockNumber(target);
2023-12-20 09:33:44 +00:00
if (newBlock == null) return;
fetchUp--;
2023-12-20 08:48:22 +00:00
}
2023-12-20 09:33:44 +00:00
target++;
if (target >= max) return;
2023-12-19 14:30:46 +00:00
}
}
2023-12-20 09:33:44 +00:00
private ulong CalculateBlockDifference(DateTime moment, TimeSpan timePerBlock, ulong max)
{
var latest = entries[max];
var timeDifference = latest.Utc - moment;
double secondsDifference = Math.Abs(timeDifference.TotalSeconds);
double secondsPerBlock = timePerBlock.TotalSeconds;
double numberOfBlocksDifference = secondsDifference / secondsPerBlock;
var blockDifference = Convert.ToUInt64(numberOfBlocksDifference);
if (blockDifference < 1) blockDifference = 1;
return blockDifference;
}
2023-12-19 14:30:46 +00:00
private void EnsureRecentBlockIfNecessary(DateTime moment, TimeSpan timePerBlock)
{
var max = entries.Keys.Max();
var latest = entries[max];
var maxRetry = 10;
while (moment > latest.Utc)
{
var newBlock = AddCurrentBlock();
2023-12-20 08:48:22 +00:00
if (newBlock == null || newBlock.BlockNumber == latest.BlockNumber)
2023-12-19 14:30:46 +00:00
{
maxRetry--;
if (maxRetry == 0) throw new Exception("Unable to fetch recent block after 10x tries.");
Thread.Sleep(timePerBlock);
}
2023-12-20 09:33:44 +00:00
max = entries.Keys.Max();
latest = entries[max];
2023-12-19 14:30:46 +00:00
}
}
2023-12-20 08:48:22 +00:00
private BlockTimeEntry? AddBlockNumber(decimal blockNumber)
2023-12-19 14:30:46 +00:00
{
return AddBlockNumber(Convert.ToUInt64(blockNumber));
}
2023-12-20 08:48:22 +00:00
private BlockTimeEntry? AddBlockNumber(ulong blockNumber)
2023-12-19 14:30:46 +00:00
{
if (entries.ContainsKey(blockNumber))
{
return entries[blockNumber];
}
if (entries.Count > MaxEntries)
{
entries.Clear();
Initialize();
}
var time = GetTimestampFromBlock(blockNumber);
2023-12-20 08:48:22 +00:00
if (time == null) return null;
var entry = new BlockTimeEntry(blockNumber, time.Value);
log.Log("Found block " + entry.BlockNumber + " at " + entry.Utc.ToString("o"));
2023-12-19 14:30:46 +00:00
entries.Add(blockNumber, entry);
return entry;
}
private TimeSpan EstimateTimePerBlock()
{
var min = entries.Keys.Min();
var max = entries.Keys.Max();
2023-12-20 09:33:44 +00:00
var clippedMin = Math.Max(max - 100, min);
2023-12-19 14:30:46 +00:00
var minTime = entries[min].Utc;
2023-12-20 09:33:44 +00:00
var clippedMinBlock = AddBlockNumber(clippedMin);
if (clippedMinBlock != null) minTime = clippedMinBlock.Utc;
2023-12-19 14:30:46 +00:00
var maxTime = entries[max].Utc;
var elapsedTime = maxTime - minTime;
double elapsedSeconds = elapsedTime.TotalSeconds;
double numberOfBlocks = max - min;
double secondsPerBlock = elapsedSeconds / numberOfBlocks;
return TimeSpan.FromSeconds(secondsPerBlock);
}
private void Initialize()
{
if (!entries.Any())
{
AddCurrentBlock();
AddBlockNumber(entries.Single().Key - 1);
}
}
private static void AssertMomentIsInPast(DateTime moment)
{
if (moment > DateTime.UtcNow) throw new Exception("Moment must be UTC and must be in the past.");
}
2023-12-20 08:48:22 +00:00
private BlockTimeEntry? AddCurrentBlock()
2023-12-19 14:30:46 +00:00
{
var number = Time.Wait(web3.Eth.Blocks.GetBlockNumber.SendRequestAsync());
var blockNumber = number.ToDecimal();
return AddBlockNumber(blockNumber);
}
2023-12-20 08:48:22 +00:00
private DateTime? GetTimestampFromBlock(ulong blockNumber)
2023-12-19 14:30:46 +00:00
{
var block = Time.Wait(web3.Eth.Blocks.GetBlockWithTransactionsByNumber.SendRequestAsync(new BlockParameter(blockNumber)));
2023-12-20 08:48:22 +00:00
if (block == null) return null;
2023-12-19 14:30:46 +00:00
return DateTimeOffset.FromUnixTimeSeconds(Convert.ToInt64(block.Timestamp.ToDecimal())).UtcDateTime;
}
2023-12-20 08:48:22 +00:00
private BlockTimeEntry? FindClosestBeforeEntry(DateTime moment)
2023-12-19 14:30:46 +00:00
{
2023-12-20 08:48:22 +00:00
BlockTimeEntry? result = null;
2023-12-19 14:30:46 +00:00
foreach (var entry in entries.Values)
{
2023-12-20 08:48:22 +00:00
if (result == null)
{
if (entry.Utc < moment) result = entry;
}
else
2023-12-19 14:30:46 +00:00
{
2023-12-20 08:48:22 +00:00
if (entry.Utc > result.Utc && entry.Utc < moment) result = entry;
2023-12-19 14:30:46 +00:00
}
}
return result;
}
2023-12-20 08:48:22 +00:00
private BlockTimeEntry? FindClosestAfterEntry(DateTime moment)
2023-12-19 14:30:46 +00:00
{
2023-12-20 08:48:22 +00:00
BlockTimeEntry? result = null;
2023-12-19 14:30:46 +00:00
foreach (var entry in entries.Values)
{
2023-12-20 08:48:22 +00:00
if (result == null)
{
if (entry.Utc > moment) result = entry;
}
else
2023-12-19 14:30:46 +00:00
{
2023-12-20 08:48:22 +00:00
if (entry.Utc < result.Utc && entry.Utc > moment) result = entry;
2023-12-19 14:30:46 +00:00
}
}
return result;
}
}
}