Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 2 additions & 10 deletions src/Nethermind/Nethermind.Serialization.Rlp/BlockDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ private static int GetPreEncodedTxLength(Transaction[] txs, byte[][] encodedTxs)
int sum = 0;
for (int i = 0; i < encodedTxs.Length; i++)
{
int len = encodedTxs[i].Length;
// Legacy txs: CL format = block format. Typed txs: block format wraps in RLP byte string.
sum += txs[i].Type == TxType.Legacy ? len : Rlp.LengthOfSequence(len);
sum += TxDecoder.GetBlockFormatLength(txs[i].Type, encodedTxs[i].Length);
}
return sum;
}
Expand Down Expand Up @@ -128,13 +126,7 @@ public override void Encode(RlpStream stream, Block? item, RlpBehaviors rlpBehav
{
for (int i = 0; i < encodedTxs.Length; i++)
{
byte[] encoded = encodedTxs[i];
if (item.Transactions[i].Type != TxType.Legacy)
{
// Typed txs: CL format is type||rlp(fields), block format wraps in RLP byte string
stream.StartByteArray(encoded.Length, false);
}
stream.Write(encoded);
TxDecoder.WriteBlockFormat(stream, item.Transactions[i].Type, encodedTxs[i]);
}
}
else
Expand Down
19 changes: 19 additions & 0 deletions src/Nethermind/Nethermind.Serialization.Rlp/TxDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ static TxDecoder()
Instance = new TxDecoder(static () => TxObjectPool.Get());
Rlp.RegisterDecoder(typeof(Transaction), Instance);
}

/// <summary>
/// Gets the block-format length of a pre-encoded CL-format transaction.
/// Legacy txs use the same format; typed txs are wrapped in an RLP byte string.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetBlockFormatLength(TxType type, int clEncodedLength)
=> type == TxType.Legacy ? clEncodedLength : Rlp.LengthOfSequence(clEncodedLength);

/// <summary>
/// Writes a pre-encoded CL-format transaction in block format.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void WriteBlockFormat(RlpStream stream, TxType type, byte[] clEncoded)
{
if (type != TxType.Legacy)
stream.StartByteArray(clEncoded.Length, false);
stream.Write(clEncoded);
}
}

public sealed class SystemTxDecoder : TxDecoder<SystemTransaction>;
Expand Down
Loading