Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clone BlockHeader before executing RPC #3800

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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