Skip to content

Commit 20cf6f5

Browse files
authored
Fix/rlp receipt limits (#9667)
* Revert unnecessary change * Remove small limits on single array receipts * simplify DefaultLimit * Increase default limit to 1 MB for safety * Set MaxHashesFetch at 16K and use it in GetReceiptsMessage * Remove test - IMO it is obsolete, we can handle large receipts, we just limit to 2MB * whitepsace
1 parent 1836d4a commit 20cf6f5

File tree

5 files changed

+9
-23
lines changed

5 files changed

+9
-23
lines changed

src/Nethermind/Nethermind.Network.Stats/SyncLimits/NethermindSyncLimits.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ namespace Nethermind.Stats.SyncLimits
55
{
66
public static class NethermindSyncLimits
77
{
8-
public const int MaxHeaderFetch = 1024; // Number of block headers to be fetched per retrieval request
9-
public const int MaxBodyFetch = 512; // Number of block bodies to be fetched per retrieval request
10-
public const int MaxReceiptFetch = 512; // Number of transaction receipts to allow fetching per request
8+
public const int MaxHeaderFetch = 512; // Number of block headers to be fetched per retrieval request
9+
public const int MaxBodyFetch = 256; // Number of block bodies to be fetched per retrieval request
10+
public const int MaxReceiptFetch = 256; // Number of transaction receipts to allow fetching per request
1111
public const int MaxCodeFetch = 1024; // Number of contract codes to allow fetching per request
12-
public const int MaxHashesFetch = 10000; // Number of hashes to allow fetching per request
12+
public const int MaxHashesFetch = 16384; // Number of hashes to allow fetching per request
1313
}
1414
}

src/Nethermind/Nethermind.Network.Test/P2P/Subprotocols/Eth/V63/Eth63ProtocolHandlerTests.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,6 @@ public async Task Limit_receipt_request()
129129
private ArrayPoolList<T> RepeatPooled<T>(T txReceipts, int count) =>
130130
Enumerable.Repeat(txReceipts, count).ToPooledList(count).AddTo(_disposables);
131131

132-
[Test]
133-
public void Will_not_serve_receipts_requests_above_512()
134-
{
135-
using GetReceiptsMessage getReceiptsMessage = new(RepeatPooled(Keccak.Zero, NethermindSyncLimits.MaxReceiptFetch + 1));
136-
Packet getReceiptsPacket =
137-
new("eth", Eth63MessageCode.GetReceipts, _ctx._getReceiptMessageSerializer.Serialize(getReceiptsMessage));
138-
139-
Assert.Throws<RlpLimitException>(() => _ctx.ProtocolHandler.HandleMessage(getReceiptsPacket));
140-
_ctx.Session.Received().InitiateDisconnect(Arg.Any<DisconnectReason>(), Arg.Any<string>());
141-
}
142-
143132
[Test]
144133
public void Will_not_send_messages_larger_than_2MB()
145134
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Nethermind.Network.P2P.Subprotocols.Eth.V63.Messages
1111
{
1212
public class GetReceiptsMessageSerializer : HashesMessageSerializer<GetReceiptsMessage>
1313
{
14-
private static readonly RlpLimit RlpLimit = RlpLimit.For<GetReceiptsMessage>(NethermindSyncLimits.MaxReceiptFetch, nameof(GetReceiptsMessage.Hashes));
14+
private static readonly RlpLimit RlpLimit = RlpLimit.For<GetReceiptsMessage>(NethermindSyncLimits.MaxHashesFetch, nameof(GetReceiptsMessage.Hashes));
1515

1616
public static GetReceiptsMessage Deserialize(byte[] bytes)
1717
{

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ namespace Nethermind.Network.P2P.Subprotocols.Eth.V63.Messages
1414
{
1515
public class ReceiptsMessageSerializer : IZeroInnerMessageSerializer<ReceiptsMessage>
1616
{
17-
private static readonly RlpLimit RlpLimit = RlpLimit.For<ReceiptsMessage>(NethermindSyncLimits.MaxReceiptFetch * 4, nameof(ReceiptsMessage.TxReceipts));
1817
private readonly ISpecProvider _specProvider;
1918
private readonly IRlpStreamDecoder<TxReceipt> _decoder;
2019
private readonly Func<RlpStream, TxReceipt[]> _decodeArrayFunc;
@@ -25,7 +24,7 @@ protected ReceiptsMessageSerializer(ISpecProvider specProvider, IRlpStreamDecode
2524
{
2625
_specProvider = specProvider ?? throw new ArgumentNullException(nameof(specProvider));
2726
_decoder = decoder ?? throw new ArgumentNullException(nameof(decoder));
28-
_decodeArrayFunc = ctx => ctx.DecodeArray(nestedContext => _decoder.Decode(nestedContext), limit: RlpLimit) ?? [];
27+
_decodeArrayFunc = ctx => ctx.DecodeArray(nestedContext => _decoder.Decode(nestedContext)) ?? [];
2928
}
3029

3130
public void Serialize(IByteBuffer byteBuffer, ReceiptsMessage message)
@@ -91,7 +90,7 @@ public ReceiptsMessage Deserialize(IByteBuffer byteBuffer)
9190

9291
public ReceiptsMessage Deserialize(RlpStream rlpStream)
9392
{
94-
ArrayPoolList<TxReceipt[]> data = rlpStream.DecodeArrayPoolList(_decodeArrayFunc, limit: RlpLimit);
93+
ArrayPoolList<TxReceipt[]> data = rlpStream.DecodeArrayPoolList(_decodeArrayFunc);
9594
ReceiptsMessage message = new(data);
9695

9796
return message;

src/Nethermind/Nethermind.Serialization.Rlp/RlpLimit.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ namespace Nethermind.Serialization.Rlp;
1010

1111
public record struct RlpLimit(int Limit, string TypeName = "", ReadOnlyMemory<char> PropertyName = default)
1212
{
13-
private const int Default = 256 * 1024 * 1024;
14-
1513
// We shouldn't allocate any single array bigger than 1M
16-
public static readonly RlpLimit DefaultLimit = new(Default);
14+
public static readonly RlpLimit DefaultLimit = new();
1715
public static readonly RlpLimit Bloom = For<Bloom>(Core.Bloom.ByteLength);
1816
public static readonly RlpLimit L4 = new(4);
1917
public static readonly RlpLimit L8 = new(8);
@@ -22,7 +20,7 @@ public record struct RlpLimit(int Limit, string TypeName = "", ReadOnlyMemory<ch
2220
public static readonly RlpLimit L65 = new(65);
2321
private string _collectionExpression;
2422

25-
public RlpLimit() : this(Default) { }
23+
public RlpLimit() : this((int)1.MiB()) { }
2624

2725
public string CollectionExpression
2826
{

0 commit comments

Comments
 (0)