Skip to content

Commit

Permalink
Kch/rlp refactor (#6343)
Browse files Browse the repository at this point in the history
* RLP refactor/cleanup (#6243)

Co-authored-by: Kamil Chodoła <kamil@nethermind.io>

* Remove allowLeadingZeros

* Fix Keccak -> Hash256

* fix for optimism

---------

Co-authored-by: Kamil Chodoła <kamil@nethermind.io>
Co-authored-by: Marcin Sobczak <marcindsobczak@gmail.com>
  • Loading branch information
3 people authored Dec 11, 2023
1 parent 7c61e14 commit 1242ecd
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 362 deletions.
5 changes: 3 additions & 2 deletions src/Nethermind/Ethereum.Rlp.Test/RlpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Ethereum.Test.Base;
using Nethermind.Core;
using Nethermind.Core.Extensions;
using Nethermind.Int256;
using Nethermind.Serialization.Rlp;
using NUnit.Framework;

Expand Down Expand Up @@ -116,14 +117,14 @@ public void TestCast()
Assert.That(Nethermind.Serialization.Rlp.Rlp.Encode(1UL).Bytes, Is.EqualTo(expected), "ulong bytes");

byte[] expectedNonce = new byte[] { 136, 0, 0, 0, 0, 0, 0, 0, 1 };
Assert.That(Nethermind.Serialization.Rlp.Rlp.EncodeNonce(1UL).Bytes, Is.EqualTo(expectedNonce), "nonce bytes");
Assert.That(Nethermind.Serialization.Rlp.Rlp.Encode((UInt256)1UL, HeaderDecoder.NonceLength).Bytes, Is.EqualTo(expectedNonce), "nonce bytes");
}

[Test]
public void TestNonce()
{
byte[] expected = { 136, 0, 0, 0, 0, 0, 0, 0, 42 };
Assert.That(Nethermind.Serialization.Rlp.Rlp.EncodeNonce(42UL).Bytes, Is.EqualTo(expected));
Assert.That(Nethermind.Serialization.Rlp.Rlp.Encode((UInt256)42UL, HeaderDecoder.NonceLength).Bytes, Is.EqualTo(expected));
}

//[Ignore("placeholder for various rlp tests")]
Expand Down
24 changes: 0 additions & 24 deletions src/Nethermind/Nethermind.Core.Test/Encoding/HeaderDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,6 @@ public void Can_decode(bool hasWithdrawalsRoot)
Assert.That(decoded.Hash, Is.EqualTo(header.Hash), "hash");
}

[Test]
public void Can_decode_tricky()
{
BlockHeader header = Build.A.BlockHeader
.WithMixHash(Keccak.Compute("mix_hash"))
.WithTimestamp(2730)
.WithNonce(1000)
.TestObject;

HeaderDecoder decoder = new();
Rlp rlp = decoder.Encode(header);
rlp.Bytes[2]++;
string bytesWithAAA = rlp.Bytes.ToHexString();
bytesWithAAA = bytesWithAAA.Replace("820aaa", "83000aaa");

rlp = new Rlp(Bytes.FromHexString(bytesWithAAA));

Rlp.ValueDecoderContext decoderContext = new(rlp.Bytes);
BlockHeader? decoded = decoder.Decode(ref decoderContext);
decoded!.Hash = decoded.CalculateHash();

Assert.That(decoded.Hash, Is.EqualTo(header.Hash), "hash");
}

[Test]
public void Can_decode_aura()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private static StatusMessage Deserialize(RlpStream rlpStream)
if (rlpStream.Position < rlpStream.Length)
{
rlpStream.ReadSequenceLength();
uint forkHash = rlpStream.DecodeUInt();
uint forkHash = (uint)rlpStream.DecodeUInt256(ForkHashLength - 1);
ulong next = rlpStream.DecodeUlong();
ForkId forkId = new(forkHash, next);
statusMessage.ForkId = forkId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace Nethermind.Serialization.Rlp.Eip2930
{
public class AccessListDecoder : IRlpStreamDecoder<AccessList?>, IRlpValueDecoder<AccessList?>
{
private const int IndexLength = 32;

/// <summary>
/// We pay a high code quality tax for the performance optimization on RLP.
/// Adding more RLP decoders is costly (time wise) but the path taken saves a lot of allocations and GC.
Expand Down Expand Up @@ -48,8 +50,8 @@ public class AccessListDecoder : IRlpStreamDecoder<AccessList?>, IRlpValueDecode
int storagesCheck = rlpStream.Position + storagesLength;
while (rlpStream.Position < storagesCheck)
{
int storageItemCheck = rlpStream.Position + 33;
UInt256 index = rlpStream.DecodeUInt256();
int storageItemCheck = rlpStream.Position + IndexLength + 1;
UInt256 index = rlpStream.DecodeUInt256(IndexLength);
accessListBuilder.AddStorage(index);
if ((rlpBehaviors & RlpBehaviors.AllowExtraBytes) != RlpBehaviors.AllowExtraBytes)
{
Expand Down Expand Up @@ -113,8 +115,8 @@ public class AccessListDecoder : IRlpStreamDecoder<AccessList?>, IRlpValueDecode
int storagesCheck = decoderContext.Position + storagesLength;
while (decoderContext.Position < storagesCheck)
{
int storageItemCheck = decoderContext.Position + 33;
UInt256 index = decoderContext.DecodeUInt256();
int storageItemCheck = decoderContext.Position + IndexLength + 1;
UInt256 index = decoderContext.DecodeUInt256(IndexLength);
accessListBuilder.AddStorage(index);
if ((rlpBehaviors & RlpBehaviors.AllowExtraBytes) != RlpBehaviors.AllowExtraBytes)
{
Expand Down Expand Up @@ -167,7 +169,7 @@ public void Encode(RlpStream stream, AccessList? item, RlpBehaviors rlpBehaviors
foreach (UInt256 index in storageKeys)
{
// storage indices are encoded as 32 bytes data arrays
stream.Encode(index, 32);
stream.Encode(index, IndexLength);
}
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/Nethermind/Nethermind.Serialization.Rlp/HeaderDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Nethermind.Serialization.Rlp
{
public class HeaderDecoder : IRlpValueDecoder<BlockHeader>, IRlpStreamDecoder<BlockHeader>
{
public const int NonceLength = 8;

public BlockHeader? Decode(ref Rlp.ValueDecoderContext decoderContext,
RlpBehaviors rlpBehaviors = RlpBehaviors.None)
{
Expand Down Expand Up @@ -57,7 +59,7 @@ public class HeaderDecoder : IRlpValueDecoder<BlockHeader>, IRlpStreamDecoder<Bl
if (decoderContext.PeekPrefixAndContentLength().ContentLength == Hash256.Size)
{
blockHeader.MixHash = decoderContext.DecodeKeccak();
blockHeader.Nonce = (ulong)decoderContext.DecodeUBigInt();
blockHeader.Nonce = (ulong)decoderContext.DecodeUInt256(NonceLength);
}
else
{
Expand All @@ -79,8 +81,8 @@ public class HeaderDecoder : IRlpValueDecoder<BlockHeader>, IRlpStreamDecoder<Bl

if (itemsRemaining >= 3 && decoderContext.Position != headerCheck)
{
blockHeader.BlobGasUsed = decoderContext.DecodeULong(allowLeadingZeroBytes: false);
blockHeader.ExcessBlobGas = decoderContext.DecodeULong(allowLeadingZeroBytes: false);
blockHeader.BlobGasUsed = decoderContext.DecodeULong();
blockHeader.ExcessBlobGas = decoderContext.DecodeULong();
}

if (itemsRemaining == 4 && decoderContext.Position != headerCheck)
Expand Down Expand Up @@ -145,7 +147,7 @@ public class HeaderDecoder : IRlpValueDecoder<BlockHeader>, IRlpStreamDecoder<Bl
if (rlpStream.PeekPrefixAndContentLength().ContentLength == Hash256.Size)
{
blockHeader.MixHash = rlpStream.DecodeKeccak();
blockHeader.Nonce = (ulong)rlpStream.DecodeUBigInt();
blockHeader.Nonce = (ulong)rlpStream.DecodeUInt256(NonceLength);
}
else
{
Expand All @@ -166,8 +168,8 @@ public class HeaderDecoder : IRlpValueDecoder<BlockHeader>, IRlpStreamDecoder<Bl

if (itemsRemaining >= 3 && rlpStream.Position != headerCheck)
{
blockHeader.BlobGasUsed = rlpStream.DecodeUlong(allowLeadingZeroBytes: false);
blockHeader.ExcessBlobGas = rlpStream.DecodeUlong(allowLeadingZeroBytes: false);
blockHeader.BlobGasUsed = rlpStream.DecodeUlong();
blockHeader.ExcessBlobGas = rlpStream.DecodeUlong();
}

if (itemsRemaining == 4 && rlpStream.Position != headerCheck)
Expand Down Expand Up @@ -219,7 +221,7 @@ public void Encode(RlpStream rlpStream, BlockHeader? header, RlpBehaviors rlpBeh
else
{
rlpStream.Encode(header.MixHash);
rlpStream.EncodeNonce(header.Nonce);
rlpStream.Encode(header.Nonce, NonceLength);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public TxReceipt Decode(RlpStream rlpStream, RlpBehaviors rlpBehaviors = RlpBeha
txReceipt.StatusCode = firstItem[0];
txReceipt.GasUsedTotal = (long)rlpStream.DecodeUBigInt();
}
else if (firstItem.Length >= 1 && firstItem.Length <= 4)
else if (firstItem.Length is >= 1 and <= 4)
{
txReceipt.GasUsedTotal = (long)firstItem.ToUnsignedBigInteger();
txReceipt.SkipStateAndStatusInRlp = true;
Expand Down
Loading

0 comments on commit 1242ecd

Please # to comment.