Skip to content

Commit

Permalink
Clone BlockHeader before executing RPC (#3800)
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaszRozmej authored Feb 8, 2022
1 parent 211fa1a commit e8b7246
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down
12 changes: 4 additions & 8 deletions src/Nethermind/Nethermind.Core/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
7 changes: 7 additions & 0 deletions src/Nethermind/Nethermind.Core/BlockHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,12 @@ public enum Format
Short,
FullHashAndNumber
}

public BlockHeader Clone()
{
BlockHeader header = (BlockHeader)MemberwiseClone();
header.Bloom = Bloom.Clone();
return header;
}
}
}
8 changes: 8 additions & 0 deletions src/Nethermind/Nethermind.Core/Bloom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Linq;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public ResultWrapper<TResult> 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<TResult> ExecuteTx(BlockHeader header, Transaction tx, CancellationToken token);
Expand Down

0 comments on commit e8b7246

Please sign in to comment.