-
Notifications
You must be signed in to change notification settings - Fork 1k
Optimize: better random sampling for peers #4212
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
Optimize: better random sampling for peers #4212
Conversation
919520b
to
e9472b2
Compare
Some months ago I was discussing this on the Discord channel and forgot to open the issue. "Currently, the Peer.OnTimer method selects new peers to connect to randomly from the UnconnectedPeers list. This can lead to a node connecting to many peers within the same network or hosting provider, which can make the node more susceptible to network-level attacks like eclipse attacks and reduce overall network topology health. Suggestion: Modify the peer selection logic to prioritize connections to peers from diverse network address ranges. By grouping peers by their IP address prefix (e.g., /16 for IPv4), the node can actively seek to connect to peers in networks it's not yet connected to." Improved Decentralization: Creates a more resilient and geographically/topologically diverse network mesh. private static string GetAddressGroup(IPAddress address)
{
var unmapped = address.UnMap();
if (unmapped.AddressFamily == AddressFamily.InterNetwork) // IPv4
{
// Use /16 prefix for grouping to avoid connecting to too many peers from the same network.
var bytes = unmapped.GetAddressBytes();
return $"v4:{bytes[0]}.{bytes[1]}";
}
else // IPv6
{
// For IPv6, use /32 prefix.
var bytes = unmapped.GetAddressBytes();
return $"v6:{bytes[0]:X2}{bytes[1]:X2}:{bytes[2]:X2}{bytes[3]:X2}";
}
} |
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.
From my side, if it is going to change it should change to some smarter logic, as I commented above.
This is another issue. |
That may be true. Changing this feature we should do it the right way instead of changing it this way. It wastes everyone's time. Just remember other developers have to stop their work to review code. So you should do what needs to be done or close this PR. So no more time isn't wasted. |
{ | ||
if (currentIndex < reservoir.Length) | ||
{ | ||
reservoir[currentIndex] = item; |
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.
If we have the same length there is no rand order
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.
If we have the same length there is no rand order
All peers selected in this case. and the items in a hashset is unordered
So order doesn't matter in this case, I think.
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.
So order doesn't matter in this case, I think.
Agree, although the logic changes, it's ok to me
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.
Use a solution that fixes the current problem that @vncoelho said. This PR isn't a workaround or fix to the real problem.
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.
- #4212 (comment) problem is different from this PR and should be solved separately.
- UnconnectedMax is just 1000, practically we're much closer to 100 length, so while this PR is more efficient, I'm not sure it's worth added complexity.
THAT is another issue! |
So what issue does this PR solve? |
The previous implementation is inefficient. |
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.
Based off previous implementation of code there could have an issue or bug with ordering.
Description
Use reservoir sampling algorithm instead of random sorting.
Now it only needs to iterate once.
The previous implementation is inefficient.
Type of change
Checklist: