Skip to content

Commit 3e8c094

Browse files
committed
Address RLP writer review comments
1 parent e4e9ac0 commit 3e8c094

6 files changed

Lines changed: 12 additions & 65 deletions

File tree

src/Nethermind/Nethermind.Network.Enr/NodeRecordSigner.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ public NodeRecord Deserialize(ref RlpReader reader)
9393
int noSigSequenceLength = Rlp.LengthOfSequence(noSigContentLength);
9494
byte[] originalContent = new byte[noSigSequenceLength];
9595
RlpWriter writer = new(originalContent);
96-
WriteOriginalContent(ref writer, noSigContentLength, reader.Read(noSigContentLength));
96+
writer.StartSequence(noSigContentLength);
97+
writer.WriteEncodedRlp(reader.Read(noSigContentLength));
9798
reader.Position = startPosition;
9899
nodeRecord.OriginalContentRlp = originalContent;
99100
}
@@ -103,14 +104,6 @@ public NodeRecord Deserialize(ref RlpReader reader)
103104

104105
return nodeRecord;
105106
}
106-
107-
private static void WriteOriginalContent<TWriter>(ref TWriter writer, int contentLength, ReadOnlySpan<byte> content)
108-
where TWriter : struct, IRlpWriteBackend, allows ref struct
109-
{
110-
writer.StartSequence(contentLength);
111-
writer.Write(content);
112-
}
113-
114107
/// <summary>
115108
/// Verifies if the public key recovered from the <see cref="Signature"/> of this record matches
116109
/// the one that is included in the <value>Secp256k1</value> entry.

src/Nethermind/Nethermind.Network.Test/P2P/Subprotocols/Snap/Messages/RlpByteArrayListTests.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,6 @@ public void RlpWriter_WriteByteArrayList_WithWrapper_MatchesCanonicalEncoding(by
8686
Assert.That(buffer.AsSpan(0, writer.Position).ToArray(), Is.EqualTo(expected));
8787
}
8888

89-
[TestCaseSource(nameof(TestCases))]
90-
public void ByteBuffer_WriteByteArrayList_WithWrapper_MatchesCanonicalEncoding(byte[][] items)
91-
{
92-
using RlpByteArrayList list = CreateList(items);
93-
byte[] expected = EncodeItems(items);
94-
95-
using DisposableByteBuffer byteBuffer = Unpooled.Buffer(expected.Length).AsDisposable();
96-
byteBuffer.WriteRlpByteArrayList(list);
97-
98-
Assert.That(byteBuffer.ReadableBytes, Is.EqualTo(expected.Length));
99-
Assert.That(byteBuffer.AsSpan().ToArray(), Is.EqualTo(expected));
100-
}
101-
10289
[TestCaseSource(nameof(TestCases))]
10390
public void ByteBuffer_RlpWriter_WithWrapper_MatchesCanonicalEncoding(byte[][] items)
10491
{

src/Nethermind/Nethermind.Network/P2P/Subprotocols/Eth/V63/Messages/NodeDataMessageSerializer.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ public class NodeDataMessageSerializer : IZeroInnerMessageSerializer<NodeDataMes
1111
{
1212
private static readonly RlpLimit RlpLimit = RlpLimit.For<NodeDataMessage>(NethermindSyncLimits.MaxHashesFetch, nameof(NodeDataMessage.Data));
1313

14-
public void Serialize(IByteBuffer byteBuffer, NodeDataMessage message) =>
15-
byteBuffer.WriteRlpByteArrayList(message.Data);
14+
public void Serialize(IByteBuffer byteBuffer, NodeDataMessage message)
15+
{
16+
byteBuffer.EnsureWritable(GetLength(message, out _));
17+
ByteBufferRlpWriter writer = new(byteBuffer);
18+
writer.WriteByteArrayList(message.Data);
19+
}
1620

1721
public NodeDataMessage Deserialize(IByteBuffer byteBuffer) =>
1822
new(byteBuffer.DecodeRlpByteArrayList());

src/Nethermind/Nethermind.Serialization.Rlp/ByteBufferExtensions.cs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using DotNetty.Buffers;
66
using Nethermind.Core.Buffers;
7-
using Nethermind.Core.Collections;
87
using Nethermind.Core.Extensions;
98

109
namespace Nethermind.Serialization.Rlp
@@ -56,41 +55,6 @@ public static void WriteBytes(this IByteBuffer buffer, scoped ReadOnlySpan<byte>
5655
}
5756
}
5857

59-
public static bool TryWriteRlpByteArrayList(this IByteBuffer byteBuffer, IByteArrayList list)
60-
{
61-
if (list is not IRlpWrapper rlpWrapper) return false;
62-
byteBuffer.WriteRlpWrapper(rlpWrapper);
63-
return true;
64-
}
65-
66-
public static void WriteRlpWrapper(this IByteBuffer byteBuffer, IRlpWrapper rlpWrapper)
67-
{
68-
byteBuffer.EnsureWritable(rlpWrapper.RlpLength);
69-
ByteBufferRlpWriter writer = new(byteBuffer);
70-
rlpWrapper.Write(ref writer);
71-
}
72-
73-
public static void WriteRlpByteArrayList(this IByteBuffer byteBuffer, IByteArrayList list)
74-
{
75-
if (byteBuffer.TryWriteRlpByteArrayList(list))
76-
return;
77-
78-
int contentLength = 0;
79-
for (int i = 0; i < list.Count; i++)
80-
{
81-
contentLength += Rlp.LengthOf(list[i]);
82-
}
83-
84-
int length = Rlp.LengthOfSequence(contentLength);
85-
byteBuffer.EnsureWritable(length);
86-
ByteBufferRlpWriter writer = new(byteBuffer);
87-
writer.StartSequence(contentLength);
88-
for (int i = 0; i < list.Count; i++)
89-
{
90-
writer.Encode(list[i]);
91-
}
92-
}
93-
9458
public static RlpByteArrayList DecodeRlpByteArrayList(this IByteBuffer byteBuffer)
9559
{
9660
NettyBufferMemoryOwner? memoryOwner = new(byteBuffer);

src/Nethermind/Nethermind.Serialization.Rlp/RlpWriter.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ void IRlpWriteBackend.Write(scoped ReadOnlySpan<byte> bytesToWrite)
6666
}
6767
else
6868
{
69-
for (int i = 0; i < bytesToWrite.Length; i++)
70-
{
71-
_byteBuffer.WriteByte(bytesToWrite[i]);
72-
}
69+
_byteBuffer.WriteBytes(bytesToWrite);
7370
}
7471

7572
_position += bytesToWrite.Length;

src/Nethermind/Nethermind.Serialization.Rlp/RlpWriterExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,12 @@ public void Encode(Rlp? rlp)
236236
}
237237
else
238238
{
239-
writer.Write(rlp.Bytes);
239+
writer.WriteEncodedRlp(rlp.Bytes);
240240
}
241241
}
242242

243+
public void WriteEncodedRlp(scoped ReadOnlySpan<byte> encodedRlp) => writer.Write(encodedRlp);
244+
243245
public void Encode(Bloom? bloom)
244246
{
245247
if (ReferenceEquals(bloom, Bloom.Empty))

0 commit comments

Comments
 (0)