Skip to content

Commit

Permalink
Hotfix multiple instances of txdecoder usage (#7496)
Browse files Browse the repository at this point in the history
  • Loading branch information
flcl42 authored Sep 26, 2024
1 parent 4aa2920 commit 33f711b
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/Nethermind/Nethermind.Crypto/TransactionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
using Nethermind.Core;
using Nethermind.Core.Crypto;
using Nethermind.Serialization.Rlp;
using System;

namespace Nethermind.Crypto
{
public static class TransactionExtensions
{
private static readonly TxDecoder _txDecoder = TxDecoder.Instance;

private static readonly Lazy<TxDecoder> _txDecoder = new(() => TxDecoder.Instance);
public static Hash256 CalculateHash(this Transaction transaction)
{
KeccakRlpStream stream = new();
_txDecoder.Encode(stream, transaction, RlpBehaviors.SkipTypedWrapping);
_txDecoder.Value.Encode(stream, transaction, RlpBehaviors.SkipTypedWrapping);
return stream.GetHash();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Nethermind.JsonRpc.Data;
using Nethermind.Logging;
using Nethermind.Serialization.Rlp;
using Nethermind.Serialization.Rlp.TxDecoders;
using Nethermind.State;
using Nethermind.Trie;

Expand All @@ -38,7 +39,7 @@ public class TraceRpcModule : ITraceRpcModule
private readonly IReceiptFinder _receiptFinder;
private readonly ITracer _tracer;
private readonly IBlockFinder _blockFinder;
private readonly TxDecoder _txDecoder = TxDecoder.Instance;
private readonly Lazy<TxDecoder> _txDecoder = new(() => TxDecoder.Instance);
private readonly IJsonRpcConfig _jsonRpcConfig;
private readonly TimeSpan _cancellationTokenTimeout;
private readonly IStateReader _stateReader;
Expand Down Expand Up @@ -109,7 +110,7 @@ public ResultWrapper<IEnumerable<ParityTxTraceFromReplay>> trace_callMany(Transa
/// </summary>
public ResultWrapper<ParityTxTraceFromReplay> trace_rawTransaction(byte[] data, string[] traceTypes)
{
Transaction tx = _txDecoder.Decode(new RlpStream(data), RlpBehaviors.SkipTypedWrapping);
Transaction tx = _txDecoder.Value.Decode(new RlpStream(data), RlpBehaviors.SkipTypedWrapping);
return TraceTx(tx, traceTypes, BlockParameter.Latest);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public abstract class SyncPeerProtocolHandlerBase : ZeroProtocolHandlerBase, ISy

protected Hash256 _remoteHeadBlockHash;
protected readonly ITimestamper _timestamper;
protected readonly TxDecoder _txDecoder;
protected readonly Lazy<TxDecoder> _txDecoder = new(() => TxDecoder.Instance);

protected readonly MessageQueue<GetBlockHeadersMessage, IOwnedReadOnlyList<BlockHeader?>> _headersRequests;
protected readonly MessageQueue<GetBlockBodiesMessage, (OwnedBlockBodies, long)> _bodiesRequests;
Expand Down Expand Up @@ -87,7 +87,6 @@ protected SyncPeerProtocolHandlerBase(ISession session,
SyncServer = syncServer ?? throw new ArgumentNullException(nameof(syncServer));
BackgroundTaskScheduler = new BackgroundTaskSchedulerWrapper(this, backgroundTaskScheduler ?? throw new ArgumentNullException(nameof(BackgroundTaskScheduler)));
_timestamper = Timestamper.Default;
_txDecoder = TxDecoder.Instance;
_headersRequests = new MessageQueue<GetBlockHeadersMessage, IOwnedReadOnlyList<BlockHeader>>(Send);
_bodiesRequests = new MessageQueue<GetBlockBodiesMessage, (OwnedBlockBodies, long)>(Send);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Linq;
using DotNetty.Buffers;
using Nethermind.Core;
Expand Down Expand Up @@ -56,7 +57,7 @@ public BlockBodiesMessage Deserialize(IByteBuffer byteBuffer)

private class BlockBodyDecoder : IRlpValueDecoder<BlockBody>
{
private readonly TxDecoder _txDecoder = TxDecoder.Instance;
private readonly Lazy<TxDecoder> _txDecoder = new(() => TxDecoder.Instance);
private readonly HeaderDecoder _headerDecoder = new();
private readonly WithdrawalDecoder _withdrawalDecoderDecoder = new();
private readonly ConsensusRequestDecoder _requestsDecoder = new();
Expand All @@ -72,7 +73,7 @@ public int GetBodyLength(BlockBody b) =>
+ (b.Withdrawals is not null ? Rlp.LengthOfSequence(GetWithdrawalsLength(b.Withdrawals)) : 0)
+ (b.Requests is not null ? Rlp.LengthOfSequence(GetRequestsLength(b.Requests)) : 0);

private int GetTxLength(Transaction[] transactions) => transactions.Sum(t => _txDecoder.GetLength(t, RlpBehaviors.None));
private int GetTxLength(Transaction[] transactions) => transactions.Sum(t => _txDecoder.Value.GetLength(t, RlpBehaviors.None));

private int GetUnclesLength(BlockHeader[] headers) => headers.Sum(t => _headerDecoder.GetLength(t, RlpBehaviors.None));

Expand All @@ -91,7 +92,7 @@ public int GetBodyLength(BlockBody b) =>

// quite significant allocations (>0.5%) here based on a sample 3M blocks sync
// (just on these delegates)
Transaction[] transactions = ctx.DecodeArray(_txDecoder);
Transaction[] transactions = ctx.DecodeArray(_txDecoder.Value);
BlockHeader[] uncles = ctx.DecodeArray(_headerDecoder);
Withdrawal[]? withdrawals = null;
ConsensusRequest[]? requests = null;
Expand All @@ -114,7 +115,7 @@ public void Serialize(RlpStream stream, BlockBody body)
stream.StartSequence(GetTxLength(body.Transactions));
foreach (Transaction? txn in body.Transactions)
{
stream.Encode(txn);
_txDecoder.Value.Encode(stream, txn);
}

stream.StartSequence(GetUnclesLength(body.Uncles));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
using Nethermind.Core;
using Nethermind.Core.Collections;
using Nethermind.Serialization.Rlp;
using System;

namespace Nethermind.Network.P2P.Subprotocols.Eth.V62.Messages
{
public class TransactionsMessageSerializer : IZeroInnerMessageSerializer<TransactionsMessage>
{
private readonly TxDecoder _decoder = TxDecoder.Instance;
private readonly Lazy<TxDecoder> _txDecoder = new(() => TxDecoder.Instance);

public void Serialize(IByteBuffer byteBuffer, TransactionsMessage message)
{
Expand All @@ -21,7 +22,7 @@ public void Serialize(IByteBuffer byteBuffer, TransactionsMessage message)
nettyRlpStream.StartSequence(contentLength);
for (int i = 0; i < message.Transactions.Count; i++)
{
nettyRlpStream.Encode(message.Transactions[i], RlpBehaviors.InMempoolForm);
_txDecoder.Value.Encode(nettyRlpStream, message.Transactions[i], RlpBehaviors.InMempoolForm);
}
}

Expand All @@ -37,7 +38,7 @@ public int GetLength(TransactionsMessage message, out int contentLength)
contentLength = 0;
for (int i = 0; i < message.Transactions.Count; i++)
{
contentLength += _decoder.GetLength(message.Transactions[i], RlpBehaviors.InMempoolForm);
contentLength += _txDecoder.Value.GetLength(message.Transactions[i], RlpBehaviors.InMempoolForm);
}

return Rlp.LengthOfSequence(contentLength);
Expand Down
6 changes: 3 additions & 3 deletions src/Nethermind/Nethermind.Serialization.Rlp/BlockDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Nethermind.Serialization.Rlp
public class BlockDecoder : IRlpValueDecoder<Block>, IRlpStreamDecoder<Block>
{
private readonly HeaderDecoder _headerDecoder = new();
private readonly TxDecoder _txDecoder = TxDecoder.Instance;
private readonly Lazy<TxDecoder> _txDecoder = new(() => TxDecoder.Instance);
private readonly WithdrawalDecoder _withdrawalDecoder = new();
private readonly ConsensusRequestDecoder _consensusRequestsDecoder = new();

Expand Down Expand Up @@ -180,7 +180,7 @@ private int GetTxLength(Block item, RlpBehaviors rlpBehaviors)
int txLength = 0;
for (int i = 0; i < item.Transactions.Length; i++)
{
txLength += _txDecoder.GetLength(item.Transactions[i], rlpBehaviors);
txLength += _txDecoder.Value.GetLength(item.Transactions[i], rlpBehaviors);
}

return txLength;
Expand Down Expand Up @@ -338,7 +338,7 @@ public void Encode(RlpStream stream, Block? item, RlpBehaviors rlpBehaviors = Rl
stream.StartSequence(txsLength);
for (int i = 0; i < item.Transactions.Length; i++)
{
stream.Encode(item.Transactions[i]);
_txDecoder.Value.Encode(stream, item.Transactions[i]);
}

stream.StartSequence(unclesLength);
Expand Down
6 changes: 0 additions & 6 deletions src/Nethermind/Nethermind.Serialization.Rlp/RlpStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class RlpStream
private static readonly HeaderDecoder _headerDecoder = new();
private static readonly BlockDecoder _blockDecoder = new();
private static readonly BlockInfoDecoder _blockInfoDecoder = new();
private static readonly TxDecoder _txDecoder = TxDecoder.Instance;
private static readonly WithdrawalDecoder _withdrawalDecoder = new();
private static readonly ConsensusRequestDecoder _requestsDecoder = new();
private static readonly LogEntryDecoder _logEntryDecoder = LogEntryDecoder.Instance;
Expand Down Expand Up @@ -66,11 +65,6 @@ public void Encode(BlockHeader value)
_headerDecoder.Encode(this, value);
}

public void Encode(Transaction value, RlpBehaviors rlpBehaviors = RlpBehaviors.None)
{
_txDecoder.Encode(this, value, rlpBehaviors);
}

public void Encode(Withdrawal value) => _withdrawalDecoder.Encode(this, value);
public void Encode(ConsensusRequest value) => _requestsDecoder.Encode(this, value);

Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Serialization.Rlp/TxDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class TxDecoder : TxDecoder<Transaction>
public static readonly ObjectPool<Transaction> TxObjectPool = new DefaultObjectPool<Transaction>(new Transaction.PoolPolicy(), Environment.ProcessorCount * 4);

#pragma warning disable CS0618
public static readonly TxDecoder Instance = new();
public static TxDecoder Instance => Rlp.GetStreamDecoder<Transaction>() as TxDecoder;
#pragma warning restore CS0618

// Rlp will try to find a public parameterless constructor during static initialization.
Expand Down
4 changes: 2 additions & 2 deletions src/Nethermind/Nethermind.State/Proofs/TxTrie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Nethermind.State.Proofs;
/// </summary>
public class TxTrie : PatriciaTrie<Transaction>
{
private static readonly TxDecoder _txDecoder = TxDecoder.Instance;
private static readonly Lazy<TxDecoder> _txDecoder = new(() => TxDecoder.Instance);

/// <inheritdoc/>
/// <param name="transactions">The transactions to build the trie of.</param>
Expand All @@ -29,7 +29,7 @@ protected override void Initialize(Transaction[] list)

foreach (Transaction? transaction in list)
{
CappedArray<byte> buffer = _txDecoder.EncodeToCappedArray(transaction, RlpBehaviors.SkipTypedWrapping, _bufferPool);
CappedArray<byte> buffer = _txDecoder.Value.EncodeToCappedArray(transaction, RlpBehaviors.SkipTypedWrapping, _bufferPool);
CappedArray<byte> keyBuffer = (key++).EncodeToCappedArray(_bufferPool);

Set(keyBuffer.AsSpan(), buffer);
Expand Down
12 changes: 6 additions & 6 deletions src/Nethermind/Nethermind.TxPool/BlobTxStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Nethermind.TxPool;

public class BlobTxStorage : IBlobTxStorage
{
private static readonly TxDecoder _txDecoder = TxDecoder.Instance;
private static readonly Lazy<TxDecoder> _txDecoder = new(() => TxDecoder.Instance);
private readonly IDb _fullBlobTxsDb;
private readonly IDb _lightBlobTxsDb;
private readonly IDb _processedBlobTxsDb;
Expand Down Expand Up @@ -96,7 +96,7 @@ public bool TryGetBlobTransactionsFromBlock(long blockNumber, out Transaction[]?
if (bytes is not null)
{
RlpStream rlpStream = new(bytes);
blockBlobTransactions = _txDecoder.DecodeArray(rlpStream, RlpBehaviors.InMempoolForm);
blockBlobTransactions = _txDecoder.Value.DecodeArray(rlpStream, RlpBehaviors.InMempoolForm);
return true;
}

Expand Down Expand Up @@ -141,10 +141,10 @@ private static void GetHashPrefixedByTimestamp(UInt256 timestamp, ValueHash256 h

private void EncodeAndSaveTx(Transaction transaction, IDb db, Span<byte> txHashPrefixed)
{
int length = _txDecoder.GetLength(transaction, RlpBehaviors.InMempoolForm);
int length = _txDecoder.Value.GetLength(transaction, RlpBehaviors.InMempoolForm);
IByteBuffer byteBuffer = PooledByteBufferAllocator.Default.Buffer(length);
using NettyRlpStream rlpStream = new(byteBuffer);
rlpStream.Encode(transaction, RlpBehaviors.InMempoolForm);
_txDecoder.Value.Encode(rlpStream, transaction, RlpBehaviors.InMempoolForm);

db.PutSpan(txHashPrefixed, byteBuffer.AsSpan());
}
Expand All @@ -158,7 +158,7 @@ private void EncodeAndSaveTxs(IList<Transaction> blockBlobTransactions, IDb db,
rlpStream.StartSequence(contentLength);
foreach (Transaction transaction in blockBlobTransactions)
{
_txDecoder.Encode(rlpStream, transaction, RlpBehaviors.InMempoolForm);
_txDecoder.Value.Encode(rlpStream, transaction, RlpBehaviors.InMempoolForm);
}

db.PutSpan(blockNumber.ToBigEndianSpanWithoutLeadingZeros(out _), byteBuffer.AsSpan());
Expand All @@ -169,7 +169,7 @@ private int GetLength(IList<Transaction> blockBlobTransactions)
int contentLength = 0;
foreach (Transaction transaction in blockBlobTransactions)
{
contentLength += _txDecoder.GetLength(transaction, RlpBehaviors.InMempoolForm);
contentLength += _txDecoder.Value.GetLength(transaction, RlpBehaviors.InMempoolForm);
}

return contentLength;
Expand Down
5 changes: 3 additions & 2 deletions src/Nethermind/Nethermind.TxPool/TransactionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Runtime.CompilerServices;
using Nethermind.Core;
using Nethermind.Core.Extensions;
Expand All @@ -14,11 +15,11 @@ namespace Nethermind.TxPool
public static class TransactionExtensions
{
private static readonly long MaxSizeOfTxForBroadcast = 4.KiB(); //4KB, as in Geth https://github.com/ethereum/go-ethereum/pull/27618
private static readonly ITransactionSizeCalculator _transactionSizeCalculator = new NetworkTransactionSizeCalculator(TxDecoder.Instance);
private static readonly Lazy<NetworkTransactionSizeCalculator> _transactionSizeCalculator = new(() => new NetworkTransactionSizeCalculator(TxDecoder.Instance));

public static int GetLength(this Transaction tx)
{
return tx.GetLength(_transactionSizeCalculator);
return tx.GetLength(_transactionSizeCalculator.Value);
}

public static bool CanPayBaseFee(this Transaction tx, UInt256 currentBaseFee) => tx.MaxFeePerGas >= currentBaseFee;
Expand Down

0 comments on commit 33f711b

Please sign in to comment.