Block range type
This commit is contained in:
parent
da4101a042
commit
1bf693938c
|
@ -19,22 +19,35 @@ namespace NethereumWorkflow
|
|||
this.log = log;
|
||||
}
|
||||
|
||||
public BlockRange ConvertTimeRangeToBlockRange(TimeRange timeRange)
|
||||
{
|
||||
var lowest = GetLowestBlockNumberAfter(timeRange.From);
|
||||
var highest = GetHighestBlockNumberBefore(timeRange.To);
|
||||
|
||||
var fromBlock = Math.Min(lowest, highest);
|
||||
var toBlock = Math.Max(lowest, highest);
|
||||
|
||||
return new BlockRange(fromBlock, toBlock);
|
||||
}
|
||||
|
||||
public ulong GetHighestBlockNumberBefore(DateTime moment)
|
||||
{
|
||||
log.Log("Looking for highest block before " + moment.ToString("o"));
|
||||
AssertMomentIsInPast(moment);
|
||||
Initialize();
|
||||
|
||||
return GetHighestBlockBefore(moment);
|
||||
var result = GetHighestBlockBefore(moment);
|
||||
log.Log($"Highest block before [{moment.ToString("o")}] = {result}");
|
||||
return result;
|
||||
}
|
||||
|
||||
public ulong GetLowestBlockNumberAfter(DateTime moment)
|
||||
{
|
||||
log.Log("Looking for lowest block after " + moment.ToString("o"));
|
||||
AssertMomentIsInPast(moment);
|
||||
Initialize();
|
||||
|
||||
return GetLowestBlockAfter(moment);
|
||||
var result = GetLowestBlockAfter(moment);
|
||||
log.Log($"Lowest block after [{moment.ToString("o")}] = {result}");
|
||||
return result;
|
||||
}
|
||||
|
||||
private ulong GetHighestBlockBefore(DateTime moment)
|
||||
|
@ -48,14 +61,13 @@ namespace NethereumWorkflow
|
|||
closestAfter.Utc > moment &&
|
||||
closestBefore.BlockNumber + 1 == closestAfter.BlockNumber)
|
||||
{
|
||||
log.Log("Found highest-Before: " + closestBefore);
|
||||
return closestBefore.BlockNumber;
|
||||
}
|
||||
|
||||
var newBlocks = FetchBlocksAround(moment);
|
||||
if (newBlocks == 0)
|
||||
{
|
||||
log.Log("Didn't find any new blocks.");
|
||||
log.Debug("Didn't find any new blocks.");
|
||||
if (closestBefore != null) return closestBefore.BlockNumber;
|
||||
throw new Exception("Failed to find highest before.");
|
||||
}
|
||||
|
@ -73,14 +85,13 @@ namespace NethereumWorkflow
|
|||
closestAfter.Utc > moment &&
|
||||
closestBefore.BlockNumber + 1 == closestAfter.BlockNumber)
|
||||
{
|
||||
log.Log("Found lowest-after: " + closestAfter);
|
||||
return closestAfter.BlockNumber;
|
||||
}
|
||||
|
||||
var newBlocks = FetchBlocksAround(moment);
|
||||
if (newBlocks == 0)
|
||||
{
|
||||
log.Log("Didn't find any new blocks.");
|
||||
log.Debug("Didn't find any new blocks.");
|
||||
if (closestAfter != null) return closestAfter.BlockNumber;
|
||||
throw new Exception("Failed to find lowest before.");
|
||||
}
|
||||
|
@ -187,7 +198,7 @@ namespace NethereumWorkflow
|
|||
|
||||
private BlockTimeEntry? AddBlockNumber(string a, ulong blockNumber)
|
||||
{
|
||||
log.Log(a + " - Adding blockNumber: " + blockNumber);
|
||||
log.Debug(a + " - Adding blockNumber: " + blockNumber);
|
||||
if (entries.ContainsKey(blockNumber))
|
||||
{
|
||||
return entries[blockNumber];
|
||||
|
@ -203,7 +214,7 @@ namespace NethereumWorkflow
|
|||
var time = GetTimestampFromBlock(blockNumber);
|
||||
if (time == null)
|
||||
{
|
||||
log.Log("Failed to get block for number: " + blockNumber);
|
||||
log.Debug("Failed to get block for number: " + blockNumber);
|
||||
return null;
|
||||
}
|
||||
var entry = new BlockTimeEntry(blockNumber, time.Value);
|
||||
|
@ -216,7 +227,6 @@ namespace NethereumWorkflow
|
|||
{
|
||||
var min = entries.Keys.Min();
|
||||
var max = entries.Keys.Max();
|
||||
log.Log("min/max: " + min + " / " + max);
|
||||
var clippedMin = Math.Max(max - 100, min);
|
||||
var minTime = entries[min].Utc;
|
||||
var clippedMinBlock = AddBlockNumber("EST", clippedMin);
|
||||
|
|
|
@ -4,6 +4,7 @@ using Nethereum.Contracts;
|
|||
using Nethereum.RPC.Eth.DTOs;
|
||||
using Nethereum.Web3;
|
||||
using Utils;
|
||||
using BlockRange = Utils.BlockRange;
|
||||
|
||||
namespace NethereumWorkflow
|
||||
{
|
||||
|
@ -88,24 +89,24 @@ namespace NethereumWorkflow
|
|||
public List<EventLog<TEvent>> GetEvents<TEvent>(string address, TimeRange timeRange) where TEvent : IEventDTO, new()
|
||||
{
|
||||
var blockTimeFinder = new BlockTimeFinder(web3, log);
|
||||
|
||||
var lowest = blockTimeFinder.GetLowestBlockNumberAfter(timeRange.From);
|
||||
var highest = blockTimeFinder.GetHighestBlockNumberBefore(timeRange.To);
|
||||
|
||||
var fromBlock = Math.Min(lowest, highest);
|
||||
var toBlock = Math.Max(lowest, highest);
|
||||
|
||||
return GetEvents<TEvent>(address, fromBlock, toBlock);
|
||||
var blockRange = blockTimeFinder.ConvertTimeRangeToBlockRange(timeRange);
|
||||
return GetEvents<TEvent>(address, blockRange);
|
||||
}
|
||||
|
||||
public List<EventLog<TEvent>> GetEvents<TEvent>(string address, ulong fromBlockNumber, ulong toBlockNumber) where TEvent : IEventDTO, new()
|
||||
public List<EventLog<TEvent>> GetEvents<TEvent>(string address, BlockRange blockRange) where TEvent : IEventDTO, new()
|
||||
{
|
||||
log.Debug($"Getting events of type [{typeof(TEvent).Name}] in block range [{fromBlockNumber} - {toBlockNumber}]");
|
||||
log.Debug($"Getting events of type [{typeof(TEvent).Name}] in block range [{blockRange.From} - {blockRange.To}]");
|
||||
var eventHandler = web3.Eth.GetEvent<TEvent>(address);
|
||||
var from = new BlockParameter(fromBlockNumber);
|
||||
var to = new BlockParameter(toBlockNumber);
|
||||
var from = new BlockParameter(blockRange.From);
|
||||
var to = new BlockParameter(blockRange.To);
|
||||
var blockFilter = Time.Wait(eventHandler.CreateFilterBlockRangeAsync(from, to));
|
||||
return Time.Wait(eventHandler.GetAllChangesAsync(blockFilter));
|
||||
}
|
||||
|
||||
public BlockRange ConvertTimeRangeToBlockRange(TimeRange timeRange)
|
||||
{
|
||||
var blockTimeFinder = new BlockTimeFinder(web3, log);
|
||||
return blockTimeFinder.ConvertTimeRangeToBlockRange(timeRange);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
namespace Utils
|
||||
{
|
||||
public class BlockRange
|
||||
{
|
||||
public BlockRange(ulong from, ulong to)
|
||||
{
|
||||
if (from < to)
|
||||
{
|
||||
From = from;
|
||||
To = to;
|
||||
}
|
||||
else
|
||||
{
|
||||
From = to;
|
||||
To = from;
|
||||
}
|
||||
}
|
||||
|
||||
public ulong From { get; }
|
||||
public ulong To { get; }
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@ using Nethereum.Contracts;
|
|||
using Nethereum.RPC.Eth.DTOs;
|
||||
using NethereumWorkflow;
|
||||
using Utils;
|
||||
using BlockRange = Utils.BlockRange;
|
||||
|
||||
namespace GethPlugin
|
||||
{
|
||||
|
@ -24,8 +25,9 @@ namespace GethPlugin
|
|||
decimal? GetSyncedBlockNumber();
|
||||
bool IsContractAvailable(string abi, string contractAddress);
|
||||
GethBootstrapNode GetBootstrapRecord();
|
||||
List<EventLog<TEvent>> GetEvents<TEvent>(string address, ulong fromBlockNumber, ulong toBlockNumber) where TEvent : IEventDTO, new();
|
||||
List<EventLog<TEvent>> GetEvents<TEvent>(string address, BlockRange blockRange) where TEvent : IEventDTO, new();
|
||||
List<EventLog<TEvent>> GetEvents<TEvent>(string address, TimeRange timeRange) where TEvent : IEventDTO, new();
|
||||
BlockRange ConvertTimeRangeToBlockRange(TimeRange timeRange);
|
||||
}
|
||||
|
||||
public class DeploymentGethNode : BaseGethNode, IGethNode
|
||||
|
@ -144,9 +146,9 @@ namespace GethPlugin
|
|||
return StartInteraction().IsContractAvailable(abi, contractAddress);
|
||||
}
|
||||
|
||||
public List<EventLog<TEvent>> GetEvents<TEvent>(string address, ulong fromBlockNumber, ulong toBlockNumber) where TEvent : IEventDTO, new()
|
||||
public List<EventLog<TEvent>> GetEvents<TEvent>(string address, BlockRange blockRange) where TEvent : IEventDTO, new()
|
||||
{
|
||||
return StartInteraction().GetEvents<TEvent>(address, fromBlockNumber, toBlockNumber);
|
||||
return StartInteraction().GetEvents<TEvent>(address, blockRange);
|
||||
}
|
||||
|
||||
public List<EventLog<TEvent>> GetEvents<TEvent>(string address, TimeRange timeRange) where TEvent : IEventDTO, new()
|
||||
|
@ -154,6 +156,11 @@ namespace GethPlugin
|
|||
return StartInteraction().GetEvents<TEvent>(address, timeRange);
|
||||
}
|
||||
|
||||
public BlockRange ConvertTimeRangeToBlockRange(TimeRange timeRange)
|
||||
{
|
||||
return StartInteraction().ConvertTimeRangeToBlockRange(timeRange);
|
||||
}
|
||||
|
||||
protected abstract NethereumInteraction StartInteraction();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue