Skip to content

Commit

Permalink
[release-v2.0] server: Prefer min one mix capable outbound peer.
Browse files Browse the repository at this point in the history
This adds logic to attempt to help maintain at least one mix capable
outbound peer to ensure better connectivity among the mix capable peers.
  • Loading branch information
davecgh committed Jun 4, 2024
1 parent 4debc02 commit b136e30
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,11 @@ type server struct {
bytesSent atomic.Uint64 // Total bytes sent by all peers since start.
shutdown atomic.Bool

// targetOutbound is the calculated number of target outbound peers to
// maintain. It is set at creation time and never modified afterwards, so
// it does not need to be protected for concurrent access.
targetOutbound uint32

// minKnownWork houses the minimum known work from the associated network
// params converted to a uint256 so the conversion only needs to be
// performed once when the server is initialized. Ideally, the chain params
Expand Down Expand Up @@ -1010,6 +1015,28 @@ func (sp *serverPeer) OnVersion(_ *peer.Peer, msg *wire.MsgVersion) {
return
}

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

if !hasMixCapableOutbound && numOutbound+1 == sp.server.targetOutbound {
srvrLog.Debugf("Rejecting outbound peer %s with protocol version "+
"%d in favor of a peer with minimum version %d", sp,
msg.ProtocolVersion, wire.MixVersion)
sp.Disconnect()
}
}

// Reject outbound peers that are not full nodes.
wantServices := wire.SFNodeNetwork
if !isInbound && !hasServices(msg.Services, wantServices) {
Expand Down Expand Up @@ -3686,6 +3713,7 @@ func newServer(ctx context.Context, listenAddrs []string, db database.DB,
}

s := server{
targetOutbound: defaultTargetOutbound,
chainParams: chainParams,
addrManager: amgr,
peerState: makePeerState(),
Expand Down Expand Up @@ -4039,15 +4067,14 @@ func newServer(ctx context.Context, listenAddrs []string, db database.DB,
}

// Create a connection manager.
targetOutbound := defaultTargetOutbound
if cfg.MaxPeers < targetOutbound {
targetOutbound = cfg.MaxPeers
if uint32(cfg.MaxPeers) < s.targetOutbound {
s.targetOutbound = uint32(cfg.MaxPeers)
}
cmgr, err := connmgr.New(&connmgr.Config{
Listeners: listeners,
OnAccept: s.inboundPeerConnected,
RetryDuration: connectionRetryInterval,
TargetOutbound: uint32(targetOutbound),
TargetOutbound: s.targetOutbound,
Dial: s.attemptDcrdDial,
Timeout: cfg.DialTimeout,
OnConnection: s.outboundPeerConnected,
Expand Down

0 comments on commit b136e30

Please sign in to comment.