From 1df2cf4d140641e67c700d417fef152ea3e5ece9 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Tue, 26 Dec 2023 12:01:45 +0000 Subject: [PATCH] Remove duplicate calls to FindHeader in eth_getLogs (#6421) * Remove duplicate calls to FindHeader in eth_getLogs * Check cancellation token * Fix tests due to params change --- .../Nethermind.Facade/BlockchainBridge.cs | 25 ++++++++++++++++++- .../Nethermind.Facade/Filters/ILogFinder.cs | 2 ++ .../Nethermind.Facade/Filters/LogFinder.cs | 13 ++++++++-- .../Nethermind.Facade/IBlockchainBridge.cs | 3 +++ .../Modules/Eth/EthRpcModuleTests.cs | 9 ++++--- .../Modules/Eth/EthRpcModule.cs | 14 ++++++++--- 6 files changed, 55 insertions(+), 11 deletions(-) diff --git a/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs b/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs index 107bfc8df27..5a40fd961f1 100644 --- a/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs +++ b/src/Nethermind/Nethermind.Facade/BlockchainBridge.cs @@ -302,10 +302,28 @@ public IEnumerable GetLogs( IEnumerable? topics = null, CancellationToken cancellationToken = default) { - LogFilter filter = _filterStore.CreateLogFilter(fromBlock, toBlock, address, topics, false); + LogFilter filter = GetFilter(fromBlock, toBlock, address, topics); return _logFinder.FindLogs(filter, cancellationToken); } + public LogFilter GetFilter( + BlockParameter fromBlock, + BlockParameter toBlock, + object? address = null, + IEnumerable? topics = null) + { + return _filterStore.CreateLogFilter(fromBlock, toBlock, address, topics, false); + } + + public IEnumerable GetLogs( + LogFilter filter, + BlockHeader fromBlock, + BlockHeader toBlock, + CancellationToken cancellationToken = default) + { + return _logFinder.FindLogs(filter, fromBlock, toBlock, cancellationToken); + } + public bool TryGetLogs(int filterId, out IEnumerable filterLogs, CancellationToken cancellationToken = default) { LogFilter? filter; @@ -379,6 +397,11 @@ public bool HasStateForRoot(Hash256 stateRoot) return _processingEnv.StateReader.HasStateForRoot(stateRoot); } + public IEnumerable FindLogs(LogFilter filter, BlockHeader fromBlock, BlockHeader toBlock, CancellationToken cancellationToken = default) + { + return _logFinder.FindLogs(filter, fromBlock, toBlock, cancellationToken); + } + public IEnumerable FindLogs(LogFilter filter, CancellationToken cancellationToken = default) { return _logFinder.FindLogs(filter, cancellationToken); diff --git a/src/Nethermind/Nethermind.Facade/Filters/ILogFinder.cs b/src/Nethermind/Nethermind.Facade/Filters/ILogFinder.cs index cc81d81b3ef..a9a2455c78b 100644 --- a/src/Nethermind/Nethermind.Facade/Filters/ILogFinder.cs +++ b/src/Nethermind/Nethermind.Facade/Filters/ILogFinder.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Threading; using Nethermind.Blockchain.Filters; +using Nethermind.Core; using Nethermind.Facade.Filters; namespace Nethermind.Blockchain.Find @@ -11,5 +12,6 @@ namespace Nethermind.Blockchain.Find public interface ILogFinder { IEnumerable FindLogs(LogFilter filter, CancellationToken cancellationToken = default); + IEnumerable FindLogs(LogFilter filter, BlockHeader fromBlock, BlockHeader toBlock, CancellationToken cancellationToken = default); } } diff --git a/src/Nethermind/Nethermind.Facade/Filters/LogFinder.cs b/src/Nethermind/Nethermind.Facade/Filters/LogFinder.cs index 8ef6790b9a4..a2a807d78ed 100644 --- a/src/Nethermind/Nethermind.Facade/Filters/LogFinder.cs +++ b/src/Nethermind/Nethermind.Facade/Filters/LogFinder.cs @@ -54,9 +54,18 @@ BlockHeader FindHeader(BlockParameter blockParameter, string name, bool headLimi _blockFinder.FindHeader(blockParameter, headLimit) ?? throw new ResourceNotFoundException($"Block not found: {name} {blockParameter}"); cancellationToken.ThrowIfCancellationRequested(); - var toBlock = FindHeader(filter.ToBlock, nameof(filter.ToBlock), false); + BlockHeader toBlock = FindHeader(filter.ToBlock, nameof(filter.ToBlock), false); + cancellationToken.ThrowIfCancellationRequested(); + BlockHeader fromBlock = filter.ToBlock == filter.FromBlock ? + toBlock : + FindHeader(filter.FromBlock, nameof(filter.FromBlock), false); + + return FindLogs(filter, fromBlock, toBlock, cancellationToken); + } + + public IEnumerable FindLogs(LogFilter filter, BlockHeader fromBlock, BlockHeader toBlock, CancellationToken cancellationToken = default) + { cancellationToken.ThrowIfCancellationRequested(); - var fromBlock = FindHeader(filter.FromBlock, nameof(filter.FromBlock), false); if (fromBlock.Number > toBlock.Number && toBlock.Number != 0) { diff --git a/src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs b/src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs index aadb2440116..fdcd1ab1d3c 100644 --- a/src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs +++ b/src/Nethermind/Nethermind.Facade/IBlockchainBridge.cs @@ -41,7 +41,10 @@ public interface IBlockchainBridge : ILogFinder FilterType GetFilterType(int filterId); FilterLog[] GetFilterLogs(int filterId); + LogFilter GetFilter(BlockParameter fromBlock, BlockParameter toBlock, object? address = null, IEnumerable? topics = null); + IEnumerable GetLogs(LogFilter filter, BlockHeader fromBlock, BlockHeader toBlock, CancellationToken cancellationToken = default); IEnumerable GetLogs(BlockParameter fromBlock, BlockParameter toBlock, object? address = null, IEnumerable? topics = null, CancellationToken cancellationToken = default); + bool TryGetLogs(int filterId, out IEnumerable filterLogs, CancellationToken cancellationToken = default); void RunTreeVisitor(ITreeVisitor treeVisitor, Hash256 stateRoot); bool HasStateForRoot(Hash256 stateRoot); diff --git a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs index 1505a222793..ddf1ed2da5b 100644 --- a/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs +++ b/src/Nethermind/Nethermind.JsonRpc.Test/Modules/Eth/EthRpcModuleTests.cs @@ -9,6 +9,7 @@ using System.Threading.Tasks; using FluentAssertions; using Nethermind.Blockchain; +using Nethermind.Blockchain.Filters; using Nethermind.Blockchain.Find; using Nethermind.Blockchain.Receipts; using Nethermind.Core; @@ -523,7 +524,7 @@ public async Task Eth_get_logs(string parameter, string expected) { using Context ctx = await Context.Create(); IBlockchainBridge bridge = Substitute.For(); - bridge.GetLogs(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any>(), Arg.Any()) + bridge.GetLogs(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()) .Returns(new[] { new FilterLog(1, 0, 1, TestItem.KeccakA, 1, TestItem.KeccakB, TestItem.AddressA, new byte[] { 1, 2, 3 }, new[] { TestItem.KeccakC, TestItem.KeccakD }) }); bridge.FilterExists(1).Returns(true); @@ -538,10 +539,10 @@ public async Task Eth_get_logs_cancellation() { using Context ctx = await Context.Create(); IBlockchainBridge bridge = Substitute.For(); - bridge.GetLogs(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any>(), Arg.Any()) + bridge.GetLogs(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()) .Returns(c => { - return GetLogs(c.ArgAt(4)); + return GetLogs(c.ArgAt(3)); [DoesNotReturn] IEnumerable GetLogs(CancellationToken ct) @@ -564,7 +565,7 @@ public async Task Eth_get_logs_with_resourceNotFound(string parameter, string ex { using Context ctx = await Context.Create(); IBlockchainBridge bridge = Substitute.For(); - bridge.GetLogs(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any>(), Arg.Any()) + bridge.GetLogs(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()) .Throws(new ResourceNotFoundException("resource not found message")); bridge.FilterExists(1).Returns(true); diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs index 792aba88345..547df1d13ff 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthRpcModule.cs @@ -602,7 +602,9 @@ public ResultWrapper> eth_getLogs(Filter filter) SearchResult toBlockResult; if (filter.FromBlock == filter.ToBlock) + { fromBlockResult = toBlockResult = _blockFinder.SearchForHeader(filter.ToBlock); + } else { toBlockResult = _blockFinder.SearchForHeader(filter.ToBlock); @@ -625,8 +627,11 @@ public ResultWrapper> eth_getLogs(Filter filter) cancellationToken.ThrowIfCancellationRequested(); - long fromBlockNumber = fromBlockResult.Object!.Number; - long toBlockNumber = toBlockResult.Object!.Number; + BlockHeader fromBlock = fromBlockResult.Object!; + BlockHeader toBlock = toBlockResult.Object!; + + long fromBlockNumber = fromBlock.Number; + long toBlockNumber = toBlock.Number; if (fromBlockNumber > toBlockNumber && toBlockNumber != 0) { @@ -637,8 +642,9 @@ public ResultWrapper> eth_getLogs(Filter filter) try { - IEnumerable filterLogs = _blockchainBridge.GetLogs(filter.FromBlock!, filter.ToBlock!, - filter.Address, filter.Topics, cancellationToken); + LogFilter logFilter = _blockchainBridge.GetFilter(filter.FromBlock, filter.ToBlock, + filter.Address, filter.Topics); + IEnumerable filterLogs = _blockchainBridge.GetLogs(logFilter, fromBlock, toBlock, cancellationToken); return ResultWrapper>.Success(GetLogs(filterLogs, cancellationTokenSource)); }