From bb35b136a1f166a75ac508e99fed9761b2be966e Mon Sep 17 00:00:00 2001 From: "lukasz.rozmej" Date: Tue, 8 Feb 2022 20:45:33 +0100 Subject: [PATCH] Clone BlockHeader before executing RPC --- .../Nethermind.Blockchain/Tracing/GethStyleTracer.cs | 4 ++-- src/Nethermind/Nethermind.Core/Block.cs | 12 ++++-------- src/Nethermind/Nethermind.Core/BlockHeader.cs | 7 +++++++ src/Nethermind/Nethermind.Core/Bloom.cs | 8 ++++++++ .../Modules/Eth/EthModule.TransactionExecutor.cs | 2 +- 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/Nethermind/Nethermind.Blockchain/Tracing/GethStyleTracer.cs b/src/Nethermind/Nethermind.Blockchain/Tracing/GethStyleTracer.cs index e8866970776..bba0d283e71 100644 --- a/src/Nethermind/Nethermind.Blockchain/Tracing/GethStyleTracer.cs +++ b/src/Nethermind/Nethermind.Blockchain/Tracing/GethStyleTracer.cs @@ -69,7 +69,7 @@ public GethLikeTxTrace Trace(Keccak blockHash, int txIndex, GethTraceOptions opt Block block = _blockTree.FindBlock(blockParameter); if (block is null) throw new InvalidOperationException($"Cannot find block {blockParameter}"); tx.Hash ??= tx.CalculateHash(); - block = block.WithReplacedBody(BlockBody.WithOneTransactionOnly(tx)); + block = block.WithReplacedBodyCloned(BlockBody.WithOneTransactionOnly(tx)); ITransactionProcessorAdapter currentAdapter = _transactionProcessorAdapter.CurrentAdapter; _transactionProcessorAdapter.CurrentAdapter = new TraceTransactionProcessorAdapter(_transactionProcessorAdapter.TransactionProcessor); @@ -116,7 +116,7 @@ public GethLikeTxTrace Trace(Keccak blockHash, int txIndex, GethTraceOptions opt if (block == null) throw new InvalidOperationException("Only historical blocks"); if (tx.Hash == null) throw new InvalidOperationException("Cannot trace transactions without tx hash set."); - block = block.WithReplacedBody(BlockBody.WithOneTransactionOnly(tx)); + block = block.WithReplacedBodyCloned(BlockBody.WithOneTransactionOnly(tx)); GethLikeBlockTracer blockTracer = new(tx.Hash, options); _processor.Process(block, ProcessingOptions.Trace, blockTracer.WithCancellation(cancellationToken)); return blockTracer.BuildResult().SingleOrDefault(); diff --git a/src/Nethermind/Nethermind.Core/Block.cs b/src/Nethermind/Nethermind.Core/Block.cs index 28dfefeb606..49d4dee65ca 100644 --- a/src/Nethermind/Nethermind.Core/Block.cs +++ b/src/Nethermind/Nethermind.Core/Block.cs @@ -44,16 +44,12 @@ public Block(BlockHeader blockHeader) { } - public Block WithReplacedHeader(BlockHeader newHeader) - { - return new(newHeader, Body); - } + public Block WithReplacedHeader(BlockHeader newHeader) => new(newHeader, Body); - public Block WithReplacedBody(BlockBody newBody) - { - return new(Header, newBody); - } + public Block WithReplacedBody(BlockBody newBody) => new(Header, newBody); + public Block WithReplacedBodyCloned(BlockBody newBody) => new(Header.Clone(), newBody); + public BlockHeader Header { get; } public BlockBody Body { get; } diff --git a/src/Nethermind/Nethermind.Core/BlockHeader.cs b/src/Nethermind/Nethermind.Core/BlockHeader.cs index d7e9def93ec..c69cd8c29b3 100644 --- a/src/Nethermind/Nethermind.Core/BlockHeader.cs +++ b/src/Nethermind/Nethermind.Core/BlockHeader.cs @@ -128,5 +128,12 @@ public enum Format Short, FullHashAndNumber } + + public BlockHeader Clone() + { + BlockHeader header = (BlockHeader)MemberwiseClone(); + header.Bloom = Bloom.Clone(); + return header; + } } } diff --git a/src/Nethermind/Nethermind.Core/Bloom.cs b/src/Nethermind/Nethermind.Core/Bloom.cs index 13db4bdf434..9e639d61be0 100644 --- a/src/Nethermind/Nethermind.Core/Bloom.cs +++ b/src/Nethermind/Nethermind.Core/Bloom.cs @@ -15,6 +15,7 @@ // along with the Nethermind. If not, see . using System; +using System.Linq; using Nethermind.Core.Crypto; using Nethermind.Core.Extensions; @@ -214,6 +215,13 @@ public BloomExtract(int index1, int index2, int index3) } public BloomStructRef ToStructRef() => new(Bytes); + + public Bloom Clone() + { + Bloom clone = new(); + Bytes.CopyTo(clone.Bytes, 0); + return clone; + } } public ref struct BloomStructRef diff --git a/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthModule.TransactionExecutor.cs b/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthModule.TransactionExecutor.cs index 76351588368..eca2e8d9410 100644 --- a/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthModule.TransactionExecutor.cs +++ b/src/Nethermind/Nethermind.JsonRpc/Modules/Eth/EthModule.TransactionExecutor.cs @@ -65,7 +65,7 @@ public ResultWrapper ExecuteTx( using CancellationTokenSource cancellationTokenSource = new(_rpcConfig.Timeout); Transaction tx = transactionCall.ToTransaction(_blockchainBridge.GetChainId()); - return ExecuteTx(header, tx, cancellationTokenSource.Token); + return ExecuteTx(header.Clone(), tx, cancellationTokenSource.Token); } protected abstract ResultWrapper ExecuteTx(BlockHeader header, Transaction tx, CancellationToken token);