From 095bba1a25fa0cff48209dad922ecbc4bc1c701e Mon Sep 17 00:00:00 2001 From: Jim Posen Date: Mon, 14 Aug 2017 18:42:51 -0700 Subject: [PATCH] peer: Move IsWitnessEnabled() from serverPeer to Peer. serverPeer is a problematic struct because it is local to the main package. --- peer/peer.go | 19 +++++++++++++++++++ server.go | 21 --------------------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/peer/peer.go b/peer/peer.go index dc6f8b4a..4075c435 100644 --- a/peer/peer.go +++ b/peer/peer.go @@ -416,6 +416,7 @@ type Peer struct { protocolVersion uint32 // negotiated protocol version sendHeadersPreferred bool // peer sent a sendheaders message verAckReceived bool + witnessEnabled bool wireEncoding wire.MessageEncoding @@ -765,6 +766,18 @@ func (p *Peer) WantsHeaders() bool { 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 // remote peer. 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. 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() // Once the version message has been exchanged, we're able to determine diff --git a/server.go b/server.go index f7199856..945d9777 100644 --- a/server.go +++ b/server.go @@ -214,8 +214,6 @@ type serverPeer struct { connReq *connmgr.ConnReq server *server - witnessMtx sync.Mutex - witnessEnabled bool persistent bool continueHash *chainhash.Hash 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 // required by the configuration for the peer package. func (sp *serverPeer) newestBlock() (*chainhash.Hash, int32, error) { @@ -368,14 +355,6 @@ func (sp *serverPeer) OnVersion(_ *peer.Peer, msg *wire.MsgVersion) { // is received. 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 // remote peer for outbound connections. This is skipped when running // on the simulation test network since it is only intended to connect