peer: Move IsWitnessEnabled() from serverPeer to Peer.

serverPeer is a problematic struct because it is local to the main
package.
This commit is contained in:
Jim Posen 2017-08-14 18:42:51 -07:00
parent 3b5c2baa2e
commit 095bba1a25
2 changed files with 19 additions and 21 deletions

View file

@ -416,6 +416,7 @@ type Peer struct {
protocolVersion uint32 // negotiated protocol version protocolVersion uint32 // negotiated protocol version
sendHeadersPreferred bool // peer sent a sendheaders message sendHeadersPreferred bool // peer sent a sendheaders message
verAckReceived bool verAckReceived bool
witnessEnabled bool
wireEncoding wire.MessageEncoding wireEncoding wire.MessageEncoding
@ -765,6 +766,18 @@ func (p *Peer) WantsHeaders() bool {
return sendHeadersPreferred return sendHeadersPreferred
} }
// IsWitnessEnabled returns true if the peer has signalled that it supports
// segregated witness.
//
// This function is safe for concurrent access.
func (p *Peer) IsWitnessEnabled() bool {
p.flagsMtx.Lock()
witnessEnabled := p.witnessEnabled
p.flagsMtx.Unlock()
return witnessEnabled
}
// localVersionMsg creates a version message that can be used to send to the // localVersionMsg creates a version message that can be used to send to the
// remote peer. // remote peer.
func (p *Peer) localVersionMsg() (*wire.MsgVersion, error) { func (p *Peer) localVersionMsg() (*wire.MsgVersion, error) {
@ -1051,6 +1064,12 @@ func (p *Peer) handleRemoteVersionMsg(msg *wire.MsgVersion) error {
// Set the remote peer's user agent. // Set the remote peer's user agent.
p.userAgent = msg.UserAgent p.userAgent = msg.UserAgent
// Determine if the peer would like to receive witness data with
// transactions, or not.
if p.services&wire.SFNodeWitness == wire.SFNodeWitness {
p.witnessEnabled = true
}
p.flagsMtx.Unlock() p.flagsMtx.Unlock()
// Once the version message has been exchanged, we're able to determine // Once the version message has been exchanged, we're able to determine

View file

@ -214,8 +214,6 @@ type serverPeer struct {
connReq *connmgr.ConnReq connReq *connmgr.ConnReq
server *server server *server
witnessMtx sync.Mutex
witnessEnabled bool
persistent bool persistent bool
continueHash *chainhash.Hash continueHash *chainhash.Hash
relayMtx sync.Mutex relayMtx sync.Mutex
@ -249,17 +247,6 @@ func newServerPeer(s *server, isPersistent bool) *serverPeer {
} }
} }
// IsWitnessEnabled returns true if the target serverPeer has signalled that it
// supports segregated witness.
//
// This function is safe for concurrent access.
func (sp *serverPeer) IsWitnessEnabled() bool {
sp.witnessMtx.Lock()
enabled := sp.witnessEnabled
sp.witnessMtx.Unlock()
return enabled
}
// newestBlock returns the current best block hash and height using the format // newestBlock returns the current best block hash and height using the format
// required by the configuration for the peer package. // required by the configuration for the peer package.
func (sp *serverPeer) newestBlock() (*chainhash.Hash, int32, error) { func (sp *serverPeer) newestBlock() (*chainhash.Hash, int32, error) {
@ -368,14 +355,6 @@ func (sp *serverPeer) OnVersion(_ *peer.Peer, msg *wire.MsgVersion) {
// is received. // is received.
sp.setDisableRelayTx(msg.DisableRelayTx) sp.setDisableRelayTx(msg.DisableRelayTx)
// Determine if the peer would like to receive witness data with
// transactions, or not.
if sp.Services()&wire.SFNodeWitness == wire.SFNodeWitness {
sp.witnessMtx.Lock()
sp.witnessEnabled = true
sp.witnessMtx.Unlock()
}
// 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