Skip to content

Commit

Permalink
peer: Implement sendheaders support (BIP0130).
Browse files Browse the repository at this point in the history
Upstream commit 8a58f8c.

Modified the supported protocol version passed to peer to
wire.BIP0111Version which is the version in Decred just before the new
protocol version for wire.SendHeadersVersion.
  • Loading branch information
davecgh committed May 30, 2016
2 parents 6febb7b + 8a58f8c commit 208eaba
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
45 changes: 35 additions & 10 deletions peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

const (
// MaxProtocolVersion is the max protocol version the peer supports.
MaxProtocolVersion = 2
MaxProtocolVersion = wire.SendHeadersVersion

// outputBufferSize is the number of elements the output channels use.
outputBufferSize = 5000
Expand Down Expand Up @@ -183,6 +183,10 @@ type MessageListeners struct {
// OnReject is invoked when a peer receives a reject wire message.
OnReject func(p *Peer, msg *wire.MsgReject)

// OnSendHeaders is invoked when a peer receives a sendheaders wire
// message.
OnSendHeaders func(p *Peer, msg *wire.MsgSendHeaders)

// OnRead is invoked when a peer receives a wire message. It consists
// of the number of bytes read, the message, and whether or not an error
// in the read occurred. Typically, callers will opt to use the
Expand Down Expand Up @@ -408,15 +412,16 @@ type Peer struct {
cfg Config
inbound bool

flagsMtx sync.Mutex // protects the peer flags below
na *wire.NetAddress
id int32
userAgent string
services wire.ServiceFlag
versionKnown bool
protocolVersion uint32
versionSent bool
verAckReceived bool
flagsMtx sync.Mutex // protects the peer flags below
na *wire.NetAddress
id int32
userAgent string
services wire.ServiceFlag
versionKnown bool
protocolVersion uint32
sendHeadersPreferred bool // peer sent a sendheaders message
versionSent bool
verAckReceived bool

knownInventory *mruInventoryMap
prevGetBlocksMtx sync.Mutex
Expand Down Expand Up @@ -732,6 +737,17 @@ func (p *Peer) StartingHeight() int32 {
return p.startingHeight
}

// WantsHeaders returns if the peer wants header messages instead of
// inventory vectors for blocks.
//
// This function is safe for concurrent access.
func (p *Peer) WantsHeaders() bool {
p.flagsMtx.Lock()
defer p.flagsMtx.Unlock()

return p.sendHeadersPreferred
}

// pushVersionMsg sends a version message to the connected peer using the
// current state.
func (p *Peer) pushVersionMsg() error {
Expand Down Expand Up @@ -1603,6 +1619,15 @@ out:
p.cfg.Listeners.OnReject(p, msg)
}

case *wire.MsgSendHeaders:
p.flagsMtx.Lock()
p.sendHeadersPreferred = true
p.flagsMtx.Unlock()

if p.cfg.Listeners.OnSendHeaders != nil {
p.cfg.Listeners.OnSendHeaders(p, msg)
}

default:
log.Debugf("Received unhandled message of type %v:",
rmsg.Command())
Expand Down
9 changes: 8 additions & 1 deletion peer/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,9 @@ func TestPeerListeners(t *testing.T) {
OnReject: func(p *peer.Peer, msg *wire.MsgReject) {
ok <- msg
},
OnSendHeaders: func(p *peer.Peer, msg *wire.MsgSendHeaders) {
ok <- msg
},
},
UserAgentName: "peer",
UserAgentVersion: "1.0",
Expand Down Expand Up @@ -503,9 +506,13 @@ func TestPeerListeners(t *testing.T) {
// only one version message is allowed
// only one verack message is allowed
{
"OnMsgReject",
"OnReject",
wire.NewMsgReject("block", wire.RejectDuplicate, "dupe block"),
},
{
"OnSendHeaders",
wire.NewMsgSendHeaders(),
},
}
t.Logf("Running %d tests", len(tests))
for _, test := range tests {
Expand Down
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,7 @@ func newPeerConfig(sp *serverPeer) *peer.Config {
ChainParams: sp.server.chainParams,
Services: sp.server.services,
DisableRelayTx: false,
ProtocolVersion: wire.BIP0111Version,
}
}

Expand Down

0 comments on commit 208eaba

Please sign in to comment.