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

[WIP] Big RLP decoder refactoring - unifying code #7924

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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 @@ -11,14 +11,14 @@ public ref struct KeccaksIterator
{
private readonly int _length;
private readonly int _startPosition;
private Rlp.ValueDecoderContext _decoderContext;
private RlpValueStream _decoderContext;
private readonly Span<byte> _buffer;
public long Index { get; private set; }

public KeccaksIterator(ReadOnlySpan<byte> data, Span<byte> buffer)
{
if (buffer.Length != 32) throw new ArgumentException("Buffer must be 32 bytes long");
_decoderContext = new Rlp.ValueDecoderContext(data);
_decoderContext = new RlpValueStream(data);
_length = _decoderContext.ReadSequenceLength();
_startPosition = _decoderContext.Position;
_buffer = buffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public ref struct LogEntriesIterator
{
private readonly LogEntry[]? _logs;
private readonly int _length;
private Rlp.ValueDecoderContext _decoderContext;
private RlpValueStream _decoderContext;
private readonly IReceiptRefDecoder _receiptRefDecoder;
public long Index { get; private set; }

public LogEntriesIterator(ReadOnlySpan<byte> data, IReceiptRefDecoder receiptRefDecoder)
{
_decoderContext = new Rlp.ValueDecoderContext(data);
_decoderContext = new RlpValueStream(data);
_length = _decoderContext.ReadSequenceLength();
Index = -1;
_logs = null;
Expand All @@ -26,7 +26,7 @@ public LogEntriesIterator(ReadOnlySpan<byte> data, IReceiptRefDecoder receiptRef

public LogEntriesIterator(LogEntry[] logs)
{
_decoderContext = new Rlp.ValueDecoderContext();
_decoderContext = new RlpValueStream();
_length = logs.Length;
Index = -1;
_logs = logs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public ref struct ReceiptsIterator
{
private readonly IDb _blocksDb;
private readonly int _length;
private Rlp.ValueDecoderContext _decoderContext;
private RlpValueStream _decoderContext;
private readonly int _startingPosition;

private readonly TxReceipt[]? _receipts;
Expand Down Expand Up @@ -51,7 +51,7 @@ public ReceiptsIterator(scoped in Span<byte> receiptsData, IDb blocksDb, Func<IR
/// <param name="receipts"></param>
public ReceiptsIterator(TxReceipt[] receipts)
{
_decoderContext = new Rlp.ValueDecoderContext();
_decoderContext = new RlpValueStream();
_length = receipts.Length;
_blocksDb = null;
_receipts = receipts;
Expand Down Expand Up @@ -118,7 +118,7 @@ public readonly LogEntriesIterator IterateLogs(TxReceiptStructRef receipt)
return receipt.Logs is null ? new LogEntriesIterator(receipt.LogsRlp, _receiptRefDecoder) : new LogEntriesIterator(receipt.Logs);
}

public readonly Hash256[] DecodeTopics(Rlp.ValueDecoderContext valueDecoderContext)
public readonly Hash256[] DecodeTopics(RlpValueStream valueDecoderContext)
{
return _receiptRefDecoder.DecodeTopics(valueDecoderContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void Roundtrip_value((string TestName, AccessList? AccessList) testCase)
RlpStream rlpStream = new(10000);
_decoder.Encode(rlpStream, testCase.AccessList);
rlpStream.Position = 0;
Rlp.ValueDecoderContext ctx = rlpStream.Data.AsSpan().AsRlpValueContext();
RlpValueStream ctx = rlpStream.Data.AsSpan().AsRlpValueContext();
AccessList decoded = _decoder.Decode(ref ctx)!;
if (testCase.AccessList is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public void DecodeValueDecoderContext_CodeAddressIsNull_ThrowsRlpException()
AuthorizationTupleDecoder sut = new();
Assert.That(() =>
{
Rlp.ValueDecoderContext decoderContext = new Rlp.ValueDecoderContext(stream.Data);
sut.Decode(ref decoderContext, RlpBehaviors.None);
RlpValueStream rlpStream = new RlpValueStream(stream.Data);
sut.Decode(ref rlpStream, RlpBehaviors.None);
}
, Throws.TypeOf<RlpException>());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void Can_do_roundtrip_regression([Values(true, false)] bool valueDecoder)
BlockDecoder decoder = new();

byte[] bytes = Bytes.FromHexString(regression5644);
Rlp.ValueDecoderContext valueDecoderContext = new(bytes);
RlpValueStream valueDecoderContext = new(bytes);
Block? decoded = valueDecoder ? decoder.Decode(ref valueDecoderContext) : decoder.Decode(new RlpStream(bytes));
Rlp encoded = decoder.Encode(decoded);
Assert.That(encoded.Bytes.ToHexString(), Is.EqualTo(bytes.ToHexString()));
Expand All @@ -127,7 +127,7 @@ public void Can_do_roundtrip_scenarios([Values(true, false)] bool valueDecoder)
foreach (Block block in _scenarios)
{
Rlp encoded = decoder.Encode(block);
Rlp.ValueDecoderContext valueDecoderContext = new(encoded.Bytes);
RlpValueStream valueDecoderContext = new(encoded.Bytes);
Block? decoded = valueDecoder ? decoder.Decode(ref valueDecoderContext) : decoder.Decode(new RlpStream(encoded.Bytes));
Rlp encoded2 = decoder.Encode(decoded);
Assert.That(encoded2.Bytes.ToHexString(), Is.EqualTo(encoded.Bytes.ToHexString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ TxReceipt BuildReceipt()
TxReceipt? deserialized;
if (valueDecoder)
{
Rlp.ValueDecoderContext valueContext = rlp.Bytes.AsRlpValueContext();
RlpValueStream valueContext = rlp.Bytes.AsRlpValueContext();
deserialized = decoder.Decode(ref valueContext, RlpBehaviors.Storage);
}
else
Expand Down Expand Up @@ -134,7 +134,7 @@ public void Can_do_roundtrip_storage_ref_struct()
CompactReceiptStorageDecoder decoder = new();

byte[] rlpStreamResult = decoder.Encode(txReceipt, RlpBehaviors.Storage).Bytes;
Rlp.ValueDecoderContext ctx = new(rlpStreamResult);
RlpValueStream ctx = new(rlpStreamResult);
decoder.DecodeStructRef(ref ctx, RlpBehaviors.Storage, out var deserialized);

Assert.That(deserialized.TxType, Is.EqualTo(txReceipt.TxType), "tx type");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public void Can_decode(bool hasWithdrawalsRoot)

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

Assert.That(decoded.Hash, Is.EqualTo(header.Hash), "hash");
Expand All @@ -45,8 +45,8 @@ public void Can_decode_aura()

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

Assert.That(decoded.Hash, Is.EqualTo(header.Hash), "hash");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void Can_do_roundtrip_ref_struct()
{
LogEntry logEntry = new(TestItem.AddressA, new byte[] { 1, 2, 3 }, new[] { TestItem.KeccakA, TestItem.KeccakB });
Rlp rlp = Rlp.Encode(logEntry);
Rlp.ValueDecoderContext valueDecoderContext = new(rlp.Bytes);
RlpValueStream valueDecoderContext = new(rlp.Bytes);
LogEntryDecoder.DecodeStructRef(ref valueDecoderContext, RlpBehaviors.None, out LogEntryStructRef decoded);

Assert.That(Bytes.AreEqual(logEntry.Data, decoded.Data), "data");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ TxReceipt BuildReceipt()
TxReceipt? deserialized;
if (valueDecoder)
{
Rlp.ValueDecoderContext valueContext = rlp.Bytes.AsRlpValueContext();
RlpValueStream valueContext = rlp.Bytes.AsRlpValueContext();
deserialized = decoder.Decode(ref valueContext, RlpBehaviors.Storage);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public void Roundtrip_ValueDecoderContext_ExecutionPayloadForm_for_shard_blobs((
_txDecoder.Encode(rlpStream, testCase.Tx);

Span<byte> spanIncomingTxRlp = rlpStream.Data.AsSpan();
Rlp.ValueDecoderContext decoderContext = new(spanIncomingTxRlp);
RlpValueStream rlpValueStream = new(spanIncomingTxRlp);
rlpStream.Position = 0;
Transaction? decoded = _txDecoder.Decode(ref decoderContext);
Transaction? decoded = _txDecoder.Decode(ref rlpValueStream);
decoded!.SenderAddress =
new EthereumEcdsa(TestBlockchainIds.ChainId).RecoverAddress(decoded);
decoded.Hash = decoded.CalculateHash();
Expand All @@ -64,10 +64,10 @@ public void NetworkWrapper_is_decoded_correctly(string rlp, Hash256 signedHash,
{
RlpStream incomingTxRlp = Bytes.FromHexString(rlp).AsRlpStream();
byte[] spanIncomingTxRlp = Bytes.FromHexString(rlp);
Rlp.ValueDecoderContext decoderContext = new(spanIncomingTxRlp.AsSpan());
RlpValueStream rlpStream = new(spanIncomingTxRlp.AsSpan());

Transaction? decoded = _txDecoder.Decode(incomingTxRlp, rlpBehaviors);
Transaction? decodedByValueDecoderContext = _txDecoder.Decode(ref decoderContext, rlpBehaviors);
Transaction? decodedByValueDecoderContext = _txDecoder.Decode(ref rlpStream, rlpBehaviors);

Assert.That(decoded!.Hash, Is.EqualTo(signedHash));
Assert.That(decodedByValueDecoderContext!.Hash, Is.EqualTo(signedHash));
Expand Down
16 changes: 8 additions & 8 deletions src/Nethermind/Nethermind.Core.Test/Encoding/TxDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ public void Roundtrip_ValueDecoderContext((Transaction Tx, string Description) t
_txDecoder.Encode(rlpStream, testCase.Tx);

Span<byte> spanIncomingTxRlp = rlpStream.Data.AsSpan();
Rlp.ValueDecoderContext decoderContext = new(spanIncomingTxRlp);
RlpValueStream rlpValueStream = new(spanIncomingTxRlp);
rlpStream.Position = 0;
Transaction? decoded = _txDecoder.Decode(ref decoderContext);
Transaction? decoded = _txDecoder.Decode(ref rlpValueStream);
decoded!.SenderAddress =
new EthereumEcdsa(TestBlockchainIds.ChainId).RecoverAddress(decoded);
decoded.Hash = decoded.CalculateHash();
Expand All @@ -122,9 +122,9 @@ public void Roundtrip_ValueDecoderContext_WithMemorySlice((Transaction Tx, strin
RlpStream rlpStream = new(10000);
_txDecoder.Encode(rlpStream, testCase.Tx);

Rlp.ValueDecoderContext decoderContext = new(rlpStream.Data.ToArray(), true);
RlpValueStream rlpValueStream = new(rlpStream.Data.ToArray(), true);
rlpStream.Position = 0;
Transaction? decoded = _txDecoder.Decode(ref decoderContext);
Transaction? decoded = _txDecoder.Decode(ref rlpValueStream);
decoded!.SenderAddress =
new EthereumEcdsa(TestBlockchainIds.ChainId).RecoverAddress(decoded);
decoded.Hash = decoded.CalculateHash();
Expand All @@ -139,9 +139,9 @@ public void ValueDecoderContext_DecodeWithMemorySlice_ShouldUseSameBuffer((Trans
RlpStream rlpStream = new(10000);
_txDecoder.Encode(rlpStream, testCase.Tx);

Rlp.ValueDecoderContext decoderContext = new(rlpStream.Data.ToArray(), true);
RlpValueStream rlpValueStream = new(rlpStream.Data.ToArray(), true);
rlpStream.Position = 0;
Transaction? decoded = _txDecoder.Decode(ref decoderContext);
Transaction? decoded = _txDecoder.Decode(ref rlpValueStream);

byte[] data1 = decoded!.Data!.Value.ToArray();
data1.AsSpan().Fill(1);
Expand Down Expand Up @@ -219,8 +219,8 @@ private void ValueDecoderContext_return_the_same_transaction_as_rlp_stream(
TestContext.Out.WriteLine($"Testing {testCase.Hash}");
RlpStream incomingTxRlp = Bytes.FromHexString(testCase.IncomingRlpHex).AsRlpStream();
Span<byte> spanIncomingTxRlp = Bytes.FromHexString(testCase.IncomingRlpHex).AsSpan();
Rlp.ValueDecoderContext decoderContext = new(spanIncomingTxRlp);
Transaction decodedByValueDecoderContext = _txDecoder.Decode(ref decoderContext, wrapping ? RlpBehaviors.SkipTypedWrapping : RlpBehaviors.None)!;
RlpValueStream rlpStream = new(spanIncomingTxRlp);
Transaction decodedByValueDecoderContext = _txDecoder.Decode(ref rlpStream, wrapping ? RlpBehaviors.SkipTypedWrapping : RlpBehaviors.None)!;
Transaction decoded = _txDecoder.Decode(incomingTxRlp, wrapping ? RlpBehaviors.SkipTypedWrapping : RlpBehaviors.None)!;
Rlp encoded = _txDecoder.Encode(decoded);
Rlp encodedWithDecodedByValueDecoderContext = _txDecoder.Encode(decodedByValueDecoderContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public void Should_decode_with_ValueDecoderContext()

codec.Encode(stream, withdrawal);

Rlp.ValueDecoderContext decoderContext = new(stream.Data.AsSpan());
Withdrawal? decoded = codec.Decode(ref decoderContext);
RlpValueStream rlpStream = new(stream.Data.AsSpan());
Withdrawal? decoded = codec.Decode(ref rlpStream);

decoded.Should().BeEquivalentTo(withdrawal);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Core.Test/RlpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public void RlpContextWithSliceMemory_shouldNotCopyUnderlyingData(bool sliceValu
stream.Encode(randomBytes);

Memory<byte> memory = stream.Data.ToArray();
Rlp.ValueDecoderContext context = new Rlp.ValueDecoderContext(memory, sliceValue);
RlpValueStream context = new RlpValueStream(memory, sliceValue);

for (int i = 0; i < 3; i++)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Nethermind/Nethermind.Facade/Find/LogFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ private static IEnumerable<FilterLog> FilterLogsInBlockLowMemoryAllocation(LogFi
logList ??= new List<FilterLog>();
Hash256[] topics = log.Topics;

topics ??= iterator.DecodeTopics(new Rlp.ValueDecoderContext(log.TopicsRlp));
topics ??= iterator.DecodeTopics(new RlpValueStream(log.TopicsRlp));

logList.Add(new FilterLog(
logIndexInBlock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public DisconnectMessage Deserialize(IByteBuffer msgBytes)
}

Span<byte> msg = msgBytes.ReadAllBytesAsSpan();
Rlp.ValueDecoderContext rlpStream = msg.AsRlpValueContext();
RlpValueStream rlpStream = msg.AsRlpValueContext();
if (!rlpStream.IsSequenceNext())
{
rlpStream = new Rlp.ValueDecoderContext(rlpStream.DecodeByteArraySpan());
rlpStream = new RlpValueStream(rlpStream.DecodeByteArraySpan());
}

rlpStream.ReadSequenceLength();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public BlockBodiesMessage Deserialize(IByteBuffer byteBuffer)
{
NettyBufferMemoryOwner memoryOwner = new(byteBuffer);

Rlp.ValueDecoderContext ctx = new(memoryOwner.Memory, true);
RlpValueStream ctx = new(memoryOwner.Memory, true);
int startingPosition = ctx.Position;
BlockBody[]? bodies = ctx.DecodeArray(_blockBodyDecoder, false);
byteBuffer.SetReaderIndex(byteBuffer.ReaderIndex + (ctx.Position - startingPosition));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public FrameInfo ReadFrameHeader(IByteBuffer input)
frameSize = (frameSize << 8) + (HeaderBytes[1] & 0xFF);
frameSize = (frameSize << 8) + (HeaderBytes[2] & 0xFF);

Rlp.ValueDecoderContext headerBodyItems = HeaderBytes.AsSpan(3, 13).AsRlpValueContext();
RlpValueStream headerBodyItems = HeaderBytes.AsSpan(3, 13).AsRlpValueContext();
int headerDataEnd = headerBodyItems.ReadSequenceLength() + headerBodyItems.Position;
int numberOfItems = headerBodyItems.PeekNumberOfItemsRemaining(headerDataEnd);
headerBodyItems.DecodeInt(); // not needed - adaptive IDs - DO NOT COMMENT OUT!!! - decode takes int of the RLP sequence and moves the position
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static OptimismTxReceipt TestStorageEncodingRoundTrip(OptimismTxReceipt decodedR
Assert.That(decodedStorageReceipt.DepositReceiptVersion, includesVersion ? Is.Not.Null : Is.Null);
});

Rlp.ValueDecoderContext valueDecoderCtx = new(encodedRlp.Data);
RlpValueStream valueDecoderCtx = new(encodedRlp.Data);
decodedStorageReceipt = decoder.Decode(ref valueDecoderCtx, RlpBehaviors.SkipTypedWrapping);

Assert.Multiple(() =>
Expand Down
Loading
Loading