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

feat(networking): prune peers from same ip beyond colocation limit #1765

Merged
merged 3 commits into from
May 31, 2023

Conversation

alrevuelta
Copy link
Contributor

@alrevuelta alrevuelta commented May 29, 2023

closes #1749

Description:

  • Allows a maximum amount of colocationLimit connected peers from the same ip.
  • Runs a periodic task to trigger disconnections from peers not meeting the criteria.

Note that this PR does not prevent connection attempts from peers violating the colocationLimit. These connections will be succesful but cleaned up later on.

case event.kind
of ConnEventKind.Connected:
let direction = if event.incoming: Inbound else: Outbound
discard
of ConnEventKind.Disconnected:
discard

# called when a peer i) first connects to us ii) disconnects all connections from us
proc onPeerEvent(pm: PeerManager, peerId: PeerId, event: PeerEvent) {.async.} =
Copy link
Contributor Author

Choose a reason for hiding this comment

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

minor refactor to avoid some duplicated code. no funcionality change.

Copy link
Contributor

@jm-clius jm-clius left a comment

Choose a reason for hiding this comment

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

Thanks for this! Generally makes sense to me. See comments/suggestions below.

##

# update the table tracking ip and the connected peers
pm.updateIpTable()
Copy link
Contributor

Choose a reason for hiding this comment

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

Mmm. This rebuilds the IP table every time, so I don't see any reason to cache the ipTable on the pm object. In other words, why not have a getPeersPerIp that simply creates and populates such a table every time?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yup, but in the future I plan to use this table for other things. still unclear but some ideas:

  • intercept a connection. before even allowing a connection to go through, check if colocation limit is ok.
  • discovery. if we discover a peer violating the colocation limit, do not add it to the peer store.
  • out connections. dont start connections to peers violating the colocation limit.

so would like to leave it if its fine.

let connsToPrune = peersInIp.len - pm.colocationLimit
for peerId in peersInIp[0..<connsToPrune]:
debug "Pruning connection due to ip colocation", peerId = peerId, ip = ip
await pm.switch.disconnect(peerId)
Copy link
Contributor

Choose a reason for hiding this comment

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

May be obvious, but presumably this removes the peer from all peer books (including our own?) in the peer store as well so that we don't attempt an outgoing connection to it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

actually it wasnt obvious, good catch, the peer was not being removed. added f9c25b4 removing + test case.

right now prunning is only for in connections, which is the attack this PR aims to mitigate. but yep i might implement it later on for out connections.

Comment on lines 698 to 702
asyncSpawn pm.updateMetrics()
asyncSpawn pm.relayConnectivityLoop()
asyncSpawn pm.prunePeerStoreLoop()
asyncSpawn pm.pruneConnsByIpLoop()
asyncSpawn pm.logSummary()
Copy link
Contributor

Choose a reason for hiding this comment

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

We have quite a large number of spawned tasks here. Wouldn't a single maintenance heartbeat be a better approach?

Copy link
Contributor Author

@alrevuelta alrevuelta May 30, 2023

Choose a reason for hiding this comment

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

yup, merged two of them in this commit
49db93e

not sure i can do merge more since each runs at different intervals, but will revisit once the peering strategy is clearer

@alrevuelta alrevuelta merged commit 047d1cf into master May 31, 2023
@alrevuelta alrevuelta deleted the prune-ip-based branch May 31, 2023 07:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: blacklist peers sharing more than x IPs
2 participants