From 581cc80d5d849cd0b6e2660773ec857028df9312 Mon Sep 17 00:00:00 2001 From: benbierens Date: Sat, 13 Apr 2024 09:19:20 +0200 Subject: [PATCH] Implements getting blockTimeEntries --- .../BlockUtils/BlockTimeFinder.cs | 7 +++++++ .../NethereumWorkflow/NethereumInteraction.cs | 7 +++++++ .../CodexContractsPlugin/CodexContractsAccess.cs | 16 +++++++++++----- ProjectPlugins/GethPlugin/GethNode.cs | 7 +++++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/Framework/NethereumWorkflow/BlockUtils/BlockTimeFinder.cs b/Framework/NethereumWorkflow/BlockUtils/BlockTimeFinder.cs index e4c73d5..b1d84e9 100644 --- a/Framework/NethereumWorkflow/BlockUtils/BlockTimeFinder.cs +++ b/Framework/NethereumWorkflow/BlockUtils/BlockTimeFinder.cs @@ -18,6 +18,13 @@ namespace NethereumWorkflow.BlockUtils bounds = new BlockchainBounds(cache, web3); } + public BlockTimeEntry Get(ulong blockNumber) + { + var b = cache.Get(blockNumber); + if (b != null) return b; + return GetBlock(blockNumber); + } + public ulong? GetHighestBlockNumberBefore(DateTime moment) { bounds.Initialize(); diff --git a/Framework/NethereumWorkflow/NethereumInteraction.cs b/Framework/NethereumWorkflow/NethereumInteraction.cs index 197cf2d..f965c35 100644 --- a/Framework/NethereumWorkflow/NethereumInteraction.cs +++ b/Framework/NethereumWorkflow/NethereumInteraction.cs @@ -121,5 +121,12 @@ namespace NethereumWorkflow to: toBlock.Value ); } + + public BlockTimeEntry GetBlockForNumber(ulong number) + { + var wrapper = new Web3Wrapper(web3, log); + var blockTimeFinder = new BlockTimeFinder(blockCache, wrapper, log); + return blockTimeFinder.Get(number); + } } } diff --git a/ProjectPlugins/CodexContractsPlugin/CodexContractsAccess.cs b/ProjectPlugins/CodexContractsPlugin/CodexContractsAccess.cs index c539af6..188d054 100644 --- a/ProjectPlugins/CodexContractsPlugin/CodexContractsAccess.cs +++ b/ProjectPlugins/CodexContractsPlugin/CodexContractsAccess.cs @@ -5,6 +5,7 @@ using Nethereum.ABI; using Nethereum.Hex.HexTypes; using Nethereum.Util; using NethereumWorkflow; +using NethereumWorkflow.BlockUtils; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using Utils; @@ -89,7 +90,7 @@ namespace CodexContractsPlugin { var requestEvent = i.GetRequest(Deployment.MarketplaceAddress, e.Event.RequestId); var result = requestEvent.ReturnValue1; - result.BlockNumber = e.Log.BlockNumber.ToUlong(); + result.Block = GetBlock(e.Log.BlockNumber.ToUlong()); result.RequestId = e.Event.RequestId; return result; }) @@ -102,7 +103,7 @@ namespace CodexContractsPlugin return events.Select(e => { var result = e.Event; - result.BlockNumber = e.Log.BlockNumber.ToUlong(); + result.Block = GetBlock(e.Log.BlockNumber.ToUlong()); return result; }).ToArray(); } @@ -113,7 +114,7 @@ namespace CodexContractsPlugin return events.Select(e => { var result = e.Event; - result.BlockNumber = e.Log.BlockNumber.ToUlong(); + result.Block = GetBlock(e.Log.BlockNumber.ToUlong()); return result; }).ToArray(); } @@ -124,7 +125,7 @@ namespace CodexContractsPlugin return events.Select(e => { var result = e.Event; - result.BlockNumber = e.Log.BlockNumber.ToUlong(); + result.Block = GetBlock(e.Log.BlockNumber.ToUlong()); result.Host = GetEthAddressFromTransaction(e.Log.TransactionHash); return result; }).ToArray(); @@ -136,7 +137,7 @@ namespace CodexContractsPlugin return events.Select(e => { var result = e.Event; - result.BlockNumber = e.Log.BlockNumber.ToUlong(); + result.Block = GetBlock(e.Log.BlockNumber.ToUlong()); return result; }).ToArray(); } @@ -169,6 +170,11 @@ namespace CodexContractsPlugin return gethNode.Call(Deployment.MarketplaceAddress, func); } + private BlockTimeEntry GetBlock(ulong number) + { + return gethNode.GetBlockForNumber(number); + } + private EthAddress GetEthAddressFromTransaction(string transactionHash) { var transaction = gethNode.GetTransaction(transactionHash); diff --git a/ProjectPlugins/GethPlugin/GethNode.cs b/ProjectPlugins/GethPlugin/GethNode.cs index bfa3d0f..08d0736 100644 --- a/ProjectPlugins/GethPlugin/GethNode.cs +++ b/ProjectPlugins/GethPlugin/GethNode.cs @@ -5,6 +5,7 @@ using Nethereum.ABI.FunctionEncoding.Attributes; using Nethereum.Contracts; using Nethereum.RPC.Eth.DTOs; using NethereumWorkflow; +using NethereumWorkflow.BlockUtils; using Utils; namespace GethPlugin @@ -27,6 +28,7 @@ namespace GethPlugin List> GetEvents(string address, BlockInterval blockRange) where TEvent : IEventDTO, new(); List> GetEvents(string address, TimeRange timeRange) where TEvent : IEventDTO, new(); BlockInterval ConvertTimeRangeToBlockRange(TimeRange timeRange); + BlockTimeEntry GetBlockForNumber(ulong number); } public class DeploymentGethNode : BaseGethNode, IGethNode @@ -160,6 +162,11 @@ namespace GethPlugin return StartInteraction().ConvertTimeRangeToBlockRange(timeRange); } + public BlockTimeEntry GetBlockForNumber(ulong number) + { + return StartInteraction().GetBlockForNumber(number); + } + protected abstract NethereumInteraction StartInteraction(); } }