-
Notifications
You must be signed in to change notification settings - Fork 465
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/Nethermind/Nethermind.Core/Collections/CappedReadOnlyList.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Nethermind/Nethermind.Core/Collections/IReadOnlyListExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
return new CappedReadOnlyList<T>(readOnlyList, length); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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, | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably |
||
|
||
protected virtual async Task<byte[][]> SendRequest(GetNodeDataMessage message, CancellationToken token) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 pathThere was a problem hiding this comment.
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?