server: Use local inbound var in version handler.
This modifies the OnVersion handler for server peers to use a local variable for the inbound status of the peer in order to avoid grabbing the mutex multiple times. While here, it also does some light cleanup. There are no functional changes. Backported from Decred.
This commit is contained in:
parent
4b20d4f86a
commit
d2c7892aa4
1 changed files with 51 additions and 55 deletions
44
server.go
44
server.go
|
@ -405,8 +405,9 @@ func (sp *serverPeer) OnVersion(_ *peer.Peer, msg *wire.MsgVersion) *wire.MsgRej
|
||||||
// NOTE: This is done before rejecting peers that are too old to ensure
|
// NOTE: This is done before rejecting peers that are too old to ensure
|
||||||
// it is updated regardless in the case a new minimum protocol version is
|
// it is updated regardless in the case a new minimum protocol version is
|
||||||
// enforced and the remote node has not upgraded yet.
|
// enforced and the remote node has not upgraded yet.
|
||||||
|
isInbound := sp.Inbound()
|
||||||
addrManager := sp.server.addrManager
|
addrManager := sp.server.addrManager
|
||||||
if !cfg.SimNet && !sp.Inbound() {
|
if !cfg.SimNet && !isInbound {
|
||||||
addrManager.SetServices(sp.NA(), msg.Services)
|
addrManager.SetServices(sp.NA(), msg.Services)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -418,7 +419,7 @@ func (sp *serverPeer) OnVersion(_ *peer.Peer, msg *wire.MsgVersion) *wire.MsgRej
|
||||||
|
|
||||||
// Reject outbound peers that are not full nodes.
|
// Reject outbound peers that are not full nodes.
|
||||||
wantServices := wire.SFNodeNetwork
|
wantServices := wire.SFNodeNetwork
|
||||||
if !sp.Inbound() && !hasServices(msg.Services, wantServices) {
|
if !isInbound && !hasServices(msg.Services, wantServices) {
|
||||||
missingServices := wantServices & ^msg.Services
|
missingServices := wantServices & ^msg.Services
|
||||||
srvrLog.Debugf("Rejecting peer %s with services %v due to not "+
|
srvrLog.Debugf("Rejecting peer %s with services %v due to not "+
|
||||||
"providing desired services %v", sp.Peer, msg.Services,
|
"providing desired services %v", sp.Peer, msg.Services,
|
||||||
|
@ -428,40 +429,26 @@ func (sp *serverPeer) OnVersion(_ *peer.Peer, msg *wire.MsgVersion) *wire.MsgRej
|
||||||
return wire.NewMsgReject(msg.Command(), wire.RejectNonstandard, reason)
|
return wire.NewMsgReject(msg.Command(), wire.RejectNonstandard, reason)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the remote peer time as a sample for creating an offset against
|
|
||||||
// the local clock to keep the network time in sync.
|
|
||||||
sp.server.timeSource.AddTimeSample(sp.Addr(), msg.Timestamp)
|
|
||||||
|
|
||||||
// Signal the sync manager this peer is a new sync candidate.
|
|
||||||
sp.server.syncManager.NewPeer(sp.Peer)
|
|
||||||
|
|
||||||
// Choose whether or not to relay transactions before a filter command
|
|
||||||
// is received.
|
|
||||||
sp.setDisableRelayTx(msg.DisableRelayTx)
|
|
||||||
|
|
||||||
// Update the address manager and request known addresses from the
|
// Update the address manager and request known addresses from the
|
||||||
// remote peer for outbound connections. This is skipped when running
|
// remote peer for outbound connections. This is skipped when running
|
||||||
// on the simulation test network since it is only intended to connect
|
// on the simulation test network since it is only intended to connect
|
||||||
// to specified peers and actively avoids advertising and connecting to
|
// to specified peers and actively avoids advertising and connecting to
|
||||||
// discovered peers.
|
// discovered peers.
|
||||||
if !cfg.SimNet {
|
if !cfg.SimNet && !isInbound {
|
||||||
// Outbound connections.
|
|
||||||
if !sp.Inbound() {
|
|
||||||
// After soft-fork activation, only make outbound
|
// After soft-fork activation, only make outbound
|
||||||
// connection to peers if they flag that they're segwit
|
// connection to peers if they flag that they're segwit
|
||||||
// enabled.
|
// enabled.
|
||||||
chain := sp.server.chain
|
chain := sp.server.chain
|
||||||
segwitActive, err := chain.IsDeploymentActive(chaincfg.DeploymentSegwit)
|
segwitActive, err := chain.IsDeploymentActive(chaincfg.DeploymentSegwit)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
peerLog.Errorf("Unable to query for segwit "+
|
peerLog.Errorf("Unable to query for segwit soft-fork state: %v",
|
||||||
"soft-fork state: %v", err)
|
err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if segwitActive && !sp.IsWitnessEnabled() {
|
if segwitActive && !sp.IsWitnessEnabled() {
|
||||||
peerLog.Infof("Disconnecting non-segwit "+
|
peerLog.Infof("Disconnecting non-segwit peer %v, isn't segwit "+
|
||||||
"peer %v, isn't segwit enabled and "+
|
"enabled and we need more segwit enabled peers", sp)
|
||||||
"we need more segwit enabled peers", sp)
|
|
||||||
sp.Disconnect()
|
sp.Disconnect()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -481,8 +468,7 @@ func (sp *serverPeer) OnVersion(_ *peer.Peer, msg *wire.MsgVersion) *wire.MsgRej
|
||||||
// Request known addresses if the server address manager needs
|
// Request known addresses if the server address manager needs
|
||||||
// more and the peer has a protocol version new enough to
|
// more and the peer has a protocol version new enough to
|
||||||
// include a timestamp with addresses.
|
// include a timestamp with addresses.
|
||||||
hasTimestamp := sp.ProtocolVersion() >=
|
hasTimestamp := sp.ProtocolVersion() >= wire.NetAddressTimeVersion
|
||||||
wire.NetAddressTimeVersion
|
|
||||||
if addrManager.NeedMoreAddresses() && hasTimestamp {
|
if addrManager.NeedMoreAddresses() && hasTimestamp {
|
||||||
sp.QueueMessage(wire.NewMsgGetAddr(), nil)
|
sp.QueueMessage(wire.NewMsgGetAddr(), nil)
|
||||||
}
|
}
|
||||||
|
@ -490,7 +476,17 @@ func (sp *serverPeer) OnVersion(_ *peer.Peer, msg *wire.MsgVersion) *wire.MsgRej
|
||||||
// Mark the address as a known good address.
|
// Mark the address as a known good address.
|
||||||
addrManager.Good(sp.NA())
|
addrManager.Good(sp.NA())
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
// Add the remote peer time as a sample for creating an offset against
|
||||||
|
// the local clock to keep the network time in sync.
|
||||||
|
sp.server.timeSource.AddTimeSample(sp.Addr(), msg.Timestamp)
|
||||||
|
|
||||||
|
// Signal the sync manager this peer is a new sync candidate.
|
||||||
|
sp.server.syncManager.NewPeer(sp.Peer)
|
||||||
|
|
||||||
|
// Choose whether or not to relay transactions before a filter command
|
||||||
|
// is received.
|
||||||
|
sp.setDisableRelayTx(msg.DisableRelayTx)
|
||||||
|
|
||||||
// Add valid peer to the server.
|
// Add valid peer to the server.
|
||||||
sp.server.AddPeer(sp)
|
sp.server.AddPeer(sp)
|
||||||
|
|
Loading…
Reference in a new issue