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

Limit request size per peer #4692

Closed
wants to merge 1 commit into from
Closed
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
43 changes: 43 additions & 0 deletions src/Nethermind/Nethermind.Core/Collections/CappedReadOnlyList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Nethermind.Core.Collections;

public readonly struct CappedReadOnlyList<T>: IReadOnlyList<T>
{
private readonly IReadOnlyList<T> _baseReadonlyList;
private readonly int _cappedLength;

public CappedReadOnlyList(IReadOnlyList<T> baseReadonly, int cappedLength)
{
_baseReadonlyList = baseReadonly;
_cappedLength = cappedLength;
}

public IEnumerator<T> GetEnumerator()
{
return _baseReadonlyList.Take(_cappedLength).GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

public int Count => Math.Min(_baseReadonlyList.Count, _cappedLength);

public T this[int index]
{
get
{
if (index >= _cappedLength)
{
throw new IndexOutOfRangeException($"Index is {index} while count is {_cappedLength}");
}

return _baseReadonlyList[index];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;

namespace Nethermind.Core.Collections;

public static class IReadOnlyListExtensions
{
public static IReadOnlyList<T> CappedTo<T>(this IReadOnlyList<T> readOnlyList, int length)
{
if (length > readOnlyList.Count)
{
return readOnlyList;
}
Comment on lines +9 to +12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original type like List<> can still grow, so we shouldn't ever use this path

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? Its a read only list?


return new CappedReadOnlyList<T>(readOnlyList, length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@
using Nethermind.Blockchain;
using Nethermind.Blockchain.Synchronization;
using Nethermind.Core;
using Nethermind.Core.Attributes;
using Nethermind.Core.Collections;
using Nethermind.Core.Crypto;
using Nethermind.Core.Extensions;
using Nethermind.Int256;
using Nethermind.Logging;
using Nethermind.Network.P2P.Subprotocols.Eth.V62;
using Nethermind.Network.P2P.Subprotocols.Eth.V62.Messages;
using Nethermind.Network.P2P.Subprotocols.Eth.V63;
using Nethermind.Network.P2P.Subprotocols.Eth.V63.Messages;
using Nethermind.Network.Rlpx;
using Nethermind.Stats;
using Nethermind.Stats.Model;
using Nethermind.Synchronization;
Expand Down Expand Up @@ -65,6 +62,14 @@ public abstract class SyncPeerProtocolHandlerBase : ZeroProtocolHandlerBase, ISy
protected readonly MessageQueue<GetBlockHeadersMessage, BlockHeader[]> _headersRequests;
protected readonly MessageQueue<GetBlockBodiesMessage, BlockBody[]> _bodiesRequests;

private static int GetBodiesLatencyHighWatermark = 8000;
private static int GetBodiesLatencyLowWatermark = 5000;
private static double GetBodiesBatchSizeAdjustmentFactor = 1.5;
private static int GetBodiesMaxBatchSize = 256;
private static int GetBodiesMinBatchSize = 1;

private int _getBodiesCurrentBatchSize = 4;

protected SyncPeerProtocolHandlerBase(ISession session,
IMessageSerializationService serializer,
INodeStatsManager statsManager,
Expand All @@ -90,10 +95,39 @@ async Task<BlockBody[]> ISyncPeer.GetBlockBodies(IReadOnlyList<Keccak> blockHash
return Array.Empty<BlockBody>();
}

GetBlockBodiesMessage bodiesMsg = new(blockHashes);
int startingBodiesCountLimit = _getBodiesCurrentBatchSize;
GetBlockBodiesMessage bodiesMsg = new(blockHashes.CappedTo(startingBodiesCountLimit));

BlockBody[] blocks = await SendRequest(bodiesMsg, token);
return blocks;
try
{
Stopwatch sw = Stopwatch.StartNew();
BlockBody[] blocks = await SendRequest(bodiesMsg, token);
long elapsed = sw.ElapsedMilliseconds;
if (elapsed < GetBodiesLatencyLowWatermark && blocks.Length == startingBodiesCountLimit)
{
_getBodiesCurrentBatchSize = Math.Min(
(int)Math.Ceiling(startingBodiesCountLimit * GetBodiesBatchSizeAdjustmentFactor),
GetBodiesMaxBatchSize
);
}
else if (elapsed > GetBodiesLatencyHighWatermark)
{
_getBodiesCurrentBatchSize = Math.Max(
(int)Math.Floor(startingBodiesCountLimit / GetBodiesBatchSizeAdjustmentFactor),
GetBodiesMinBatchSize
);
}

return blocks;
}
catch (Exception)
{
_getBodiesCurrentBatchSize = Math.Max(
(int)Math.Floor(startingBodiesCountLimit / GetBodiesBatchSizeAdjustmentFactor),
GetBodiesMinBatchSize
);
throw;
}
}

protected virtual async Task<BlockBody[]> SendRequest(GetBlockBodiesMessage message, CancellationToken token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Threading.Tasks;
using Nethermind.Consensus;
using Nethermind.Core;
using Nethermind.Core.Collections;
using Nethermind.Core.Crypto;
using Nethermind.Logging;
using Nethermind.Network.P2P.Subprotocols.Eth.V62;
Expand All @@ -38,6 +39,14 @@ public class Eth63ProtocolHandler : Eth62ProtocolHandler

private readonly MessageQueue<GetReceiptsMessage, TxReceipt[][]> _receiptsRequests;

private static int GetReceiptsLatencyHighWatermark = 8000;
private static int GetReceiptsLatencyLowWatermark = 5000;
private static int GetReceiptsMaxBatchSize = 256;
private static int GetReceiptsMinBatchSize = 1;
private static double GetReceiptsBatchSizeAdjustmentFactor = 1.5;

private int _getReceiptsCurrentBatchSize = 4;

public Eth63ProtocolHandler(ISession session,
IMessageSerializationService serializer,
INodeStatsManager nodeStatsManager,
Expand Down Expand Up @@ -142,9 +151,40 @@ public override async Task<TxReceipt[][]> GetReceipts(IReadOnlyList<Keccak> bloc
return Array.Empty<TxReceipt[]>();
}

GetReceiptsMessage msg = new(blockHashes);
TxReceipt[][] txReceipts = await SendRequest(msg, token);
return txReceipts;
int startingReceiptsCountLimit = _getReceiptsCurrentBatchSize;
GetReceiptsMessage msg = new(blockHashes.CappedTo(startingReceiptsCountLimit));

try
{
Stopwatch sw = Stopwatch.StartNew();
TxReceipt[][] txReceipts = await SendRequest(msg, token);

long elapsed = sw.ElapsedMilliseconds;
if (elapsed < GetReceiptsLatencyLowWatermark && txReceipts.Length == startingReceiptsCountLimit)
{
_getReceiptsCurrentBatchSize = Math.Min(
(int)Math.Ceiling(startingReceiptsCountLimit * GetReceiptsBatchSizeAdjustmentFactor),
GetReceiptsMaxBatchSize
);
}
else if (elapsed > GetReceiptsLatencyHighWatermark)
{
_getReceiptsCurrentBatchSize = Math.Max(
(int)Math.Floor(startingReceiptsCountLimit / GetReceiptsBatchSizeAdjustmentFactor),
GetReceiptsMinBatchSize
);
}

return txReceipts;
}
catch (Exception)
{
_getReceiptsCurrentBatchSize = Math.Max(
(int)Math.Floor(startingReceiptsCountLimit / GetReceiptsBatchSizeAdjustmentFactor),
GetReceiptsMinBatchSize
);
throw;
}
}
Comment on lines +154 to 188
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have this code very similar in two places, should we make it reusable somehow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably


protected virtual async Task<byte[][]> SendRequest(GetNodeDataMessage message, CancellationToken token)
Expand Down