Skip to content

Commit

Permalink
server: Prefer 3 min mix capable outbound peers.
Browse files Browse the repository at this point in the history
The current logic attempts to maintain at least one mix capable outbound
peer to ensure better connectivity among the mix capable peers.  It
works as intended, however, only having a single mix capable peer means
there will be delays if that peer goes offline until a replacement is
found.  A possible consequence is that any mix clients might potentially
get interrupted if that peer goes offline and a replacement isn't found
quickly enough.

Attempting to maintain a minimum of three mix capable outbound peers
will significantly reduce the likelihood of having periods without any
mix capable peers.

This updates the logic accordingly.
  • Loading branch information
davecgh committed Jun 18, 2024
1 parent 4b485d5 commit bbf9fd7
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1078,24 +1078,34 @@ func (sp *serverPeer) OnVersion(_ *peer.Peer, msg *wire.MsgVersion) {
return
}

// Maintain at least one outbound peer capable of supporting p2p mixing.
// Maintain a minimum desired number of outbound peers capable of supporting
// p2p mixing.
if !isInbound && msg.ProtocolVersion < int32(wire.MixVersion) {
var hasMixCapableOutbound bool
var numOutbound uint32
var numOutbound, numMixCapableOutbound uint32
peerState := &sp.server.peerState
peerState.Lock()
peerState.forAllOutboundPeers(func(sp *serverPeer) {
if sp.ProtocolVersion() >= wire.MixVersion {
hasMixCapableOutbound = true
numMixCapableOutbound++
}
numOutbound++
})
peerState.Unlock()

if !hasMixCapableOutbound && numOutbound+1 == sp.server.targetOutbound {
const defaultWantMixCapableOutbound uint32 = 3
wantMixCapableOutbound := defaultWantMixCapableOutbound
if sp.server.targetOutbound < wantMixCapableOutbound {
wantMixCapableOutbound = sp.server.targetOutbound
}
hasMinMixCapableOuts := numMixCapableOutbound >= wantMixCapableOutbound
needsMoreMixCapable := !hasMinMixCapableOuts &&
numOutbound+wantMixCapableOutbound >= sp.server.targetOutbound
if needsMoreMixCapable {
srvrLog.Debugf("Rejecting outbound peer %s with protocol version "+
"%d in favor of a peer with minimum version %d", sp,
msg.ProtocolVersion, wire.MixVersion)
"%d in favor of a peer with minimum version %d to maintain "+
"min number of mix capable peers (have: %d, target: %d)", sp,
msg.ProtocolVersion, wire.MixVersion, numMixCapableOutbound,
wantMixCapableOutbound)
sp.Disconnect()
}
}
Expand Down

0 comments on commit bbf9fd7

Please sign in to comment.