Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wire, peer: fix broken ibd #2281

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@ import (
"sync/atomic"
"time"

"github.com/btcsuite/go-socks/socks"
"github.com/davecgh/go-spew/spew"
"github.com/decred/dcrd/lru"

"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/go-socks/socks"
"github.com/davecgh/go-spew/spew"
"github.com/decred/dcrd/lru"
)

const (
// MaxProtocolVersion is the max protocol version the peer supports.
MaxProtocolVersion = wire.SendAddrV2Version
MaxProtocolVersion = wire.AddrV2Version

// DefaultTrickleInterval is the min time between attempts to send an
// inv message to a peer.
Expand Down Expand Up @@ -878,8 +877,8 @@ func (p *Peer) PushAddrMsg(addresses []*wire.NetAddress) ([]*wire.NetAddress, er
//
// This function is safe for concurrent access.
func (p *Peer) PushAddrV2Msg(addrs []*wire.NetAddressV2) (
[]*wire.NetAddressV2, error,
) {
[]*wire.NetAddressV2, error) {

count := len(addrs)

// Nothing to send.
Expand Down Expand Up @@ -1901,8 +1900,8 @@ func (p *Peer) QueueMessage(msg wire.Message, doneChan chan<- struct{}) {
//
// This function is safe for concurrent access.
func (p *Peer) QueueMessageWithEncoding(msg wire.Message, doneChan chan<- struct{},
encoding wire.MessageEncoding,
) {
encoding wire.MessageEncoding) {

// Avoid risk of deadlock if goroutine already exited. The goroutine
// we will be sending to hangs around until it knows for a fact that
// it is marked as disconnected and *then* it drains the channels.
Expand Down Expand Up @@ -2153,7 +2152,7 @@ func (p *Peer) writeLocalVersionMsg() error {
// writeSendAddrV2Msg writes our sendaddrv2 message to the remote peer if the
// peer supports protocol version 70016 and above.
func (p *Peer) writeSendAddrV2Msg(pver uint32) error {
if pver < wire.SendAddrV2Version {
if pver < wire.AddrV2Version {
return nil
}

Expand Down Expand Up @@ -2181,7 +2180,7 @@ func (p *Peer) waitToFinishNegotiation(pver uint32) error {

switch m := remoteMsg.(type) {
case *wire.MsgSendAddrV2:
if pver >= wire.SendAddrV2Version {
if pver >= wire.AddrV2Version {
p.flagsMtx.Lock()
p.sendAddrV2 = true
p.flagsMtx.Unlock()
Expand Down
11 changes: 4 additions & 7 deletions wire/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ func makeEmptyMessage(command string) (Message, error) {
case CmdSendAddrV2:
msg = &MsgSendAddrV2{}

case CmdWTxIdRelay:
msg = &MsgWTxIdRelay{}

case CmdGetAddr:
msg = &MsgGetAddr{}

Expand Down Expand Up @@ -280,8 +277,8 @@ func WriteMessage(w io.Writer, msg Message, pver uint32, btcnet BitcoinNet) erro
// to specify the message encoding format to be used when serializing wire
// messages.
func WriteMessageWithEncodingN(w io.Writer, msg Message, pver uint32,
btcnet BitcoinNet, encoding MessageEncoding,
) (int, error) {
btcnet BitcoinNet, encoding MessageEncoding) (int, error) {

totalBytes := 0

// Enforce max command size.
Expand Down Expand Up @@ -357,8 +354,8 @@ func WriteMessageWithEncodingN(w io.Writer, msg Message, pver uint32,
// allows the caller to specify which message encoding is to to consult when
// decoding wire messages.
func ReadMessageWithEncodingN(r io.Reader, pver uint32, btcnet BitcoinNet,
enc MessageEncoding,
) (int, Message, []byte, error) {
enc MessageEncoding) (int, Message, []byte, error) {

totalBytes := 0
n, hdr, err := readMessageHeader(r)
totalBytes += n
Expand Down
4 changes: 2 additions & 2 deletions wire/msgsendaddrv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type MsgSendAddrV2 struct{}
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
// This is part of the Message interface implementation.
func (msg *MsgSendAddrV2) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
if pver < SendAddrV2Version {
if pver < AddrV2Version {
str := fmt.Sprintf("sendaddrv2 message invalid for protocol "+
"version %d", pver)
return messageError("MsgSendAddrV2.BtcDecode", str)
Expand All @@ -27,7 +27,7 @@ func (msg *MsgSendAddrV2) BtcDecode(r io.Reader, pver uint32, enc MessageEncodin
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
// This is part of the Message interface implementation.
func (msg *MsgSendAddrV2) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
if pver < SendAddrV2Version {
if pver < AddrV2Version {
str := fmt.Sprintf("sendaddrv2 message invalid for protocol "+
"version %d", pver)
return messageError("MsgSendAddrV2.BtcEncode", str)
Expand Down
20 changes: 10 additions & 10 deletions wire/msgsendaddrv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestSendAddrV2(t *testing.T) {

// Older protocol versions should fail encode since message didn't
// exist yet.
oldPver := SendAddrV2Version - 1
oldPver := AddrV2Version - 1
err = msg.BtcEncode(&buf, oldPver, enc)
if err == nil {
s := "encode of MsgSendAddrV2 passed for old protocol " +
Expand All @@ -72,10 +72,10 @@ func TestSendAddrV2(t *testing.T) {
}

// TestSendAddrV2BIP0130 tests the MsgSendAddrV2 API against the protocol
// prior to version SendAddrV2Version.
// prior to version AddrV2Version.
func TestSendAddrV2BIP0130(t *testing.T) {
// Use the protocol version just prior to SendAddrV2Version changes.
pver := SendAddrV2Version - 1
// Use the protocol version just prior to AddrV2Version changes.
pver := AddrV2Version - 1
enc := BaseEncoding

msg := NewMsgSendAddrV2()
Expand All @@ -98,7 +98,7 @@ func TestSendAddrV2BIP0130(t *testing.T) {
}

// TestSendAddrV2CrossProtocol tests the MsgSendAddrV2 API when encoding with
// the latest protocol version and decoding with SendAddrV2Version.
// the latest protocol version and decoding with AddrV2Version.
func TestSendAddrV2CrossProtocol(t *testing.T) {
enc := BaseEncoding
msg := NewMsgSendAddrV2()
Expand All @@ -113,7 +113,7 @@ func TestSendAddrV2CrossProtocol(t *testing.T) {

// Decode with old protocol version.
readmsg := NewMsgSendAddrV2()
err = readmsg.BtcDecode(&buf, SendAddrV2Version, enc)
err = readmsg.BtcDecode(&buf, AddrV2Version, enc)
if err != nil {
t.Errorf("decode of MsgSendAddrV2 failed [%v] err <%v>", buf,
err)
Expand Down Expand Up @@ -142,21 +142,21 @@ func TestSendAddrV2Wire(t *testing.T) {
BaseEncoding,
},

// Protocol version SendAddrV2Version+1
// Protocol version AddrV2Version+1
{
msgSendAddrV2,
msgSendAddrV2,
msgSendAddrV2Encoded,
SendAddrV2Version + 1,
AddrV2Version + 1,
BaseEncoding,
},

// Protocol version SendAddrV2Version
// Protocol version AddrV2Version
{
msgSendAddrV2,
msgSendAddrV2,
msgSendAddrV2Encoded,
SendAddrV2Version,
AddrV2Version,
BaseEncoding,
},
}
Expand Down
4 changes: 2 additions & 2 deletions wire/msgwtxidrelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type MsgWTxIdRelay struct{}
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
// This is part of the Message interface implementation.
func (msg *MsgWTxIdRelay) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
if pver < WTxIdRelayVersion {
if pver < AddrV2Version {
str := fmt.Sprintf("wtxidrelay message invalid for protocol "+
"version %d", pver)
return messageError("MsgWTxIdRelay.BtcDecode", str)
Expand All @@ -31,7 +31,7 @@ func (msg *MsgWTxIdRelay) BtcDecode(r io.Reader, pver uint32, enc MessageEncodin
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
// This is part of the Message interface implementation.
func (msg *MsgWTxIdRelay) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
if pver < WTxIdRelayVersion {
if pver < AddrV2Version {
str := fmt.Sprintf("wtxidrelay message invalid for protocol "+
"version %d", pver)
return messageError("MsgWTxIdRelay.BtcEncode", str)
Expand Down
20 changes: 10 additions & 10 deletions wire/msgwtxidrelay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestWTxIdRelay(t *testing.T) {

// Older protocol versions should fail encode since message didn't
// exist yet.
oldPver := WTxIdRelayVersion - 1
oldPver := AddrV2Version - 1
err = msg.BtcEncode(&buf, oldPver, enc)
if err == nil {
s := "encode of MsgWTxIdRelay passed for old protocol " +
Expand All @@ -72,10 +72,10 @@ func TestWTxIdRelay(t *testing.T) {
}

// TestWTxIdRelayBIP0130 tests the MsgWTxIdRelay API against the protocol
// prior to version WTxIdRelayVersion.
// prior to version AddrV2Version.
func TestWTxIdRelayBIP0130(t *testing.T) {
// Use the protocol version just prior to WTxIdRelayVersion changes.
pver := WTxIdRelayVersion - 1
// Use the protocol version just prior to AddrV2Version changes.
pver := AddrV2Version - 1
enc := BaseEncoding

msg := NewMsgWTxIdRelay()
Expand All @@ -98,7 +98,7 @@ func TestWTxIdRelayBIP0130(t *testing.T) {
}

// TestWTxIdRelayCrossProtocol tests the MsgWTxIdRelay API when encoding with
// the latest protocol version and decoding with WTxIdRelayVersion.
// the latest protocol version and decoding with AddrV2Version.
func TestWTxIdRelayCrossProtocol(t *testing.T) {
enc := BaseEncoding
msg := NewMsgWTxIdRelay()
Expand All @@ -113,7 +113,7 @@ func TestWTxIdRelayCrossProtocol(t *testing.T) {

// Decode with old protocol version.
readmsg := NewMsgWTxIdRelay()
err = readmsg.BtcDecode(&buf, WTxIdRelayVersion, enc)
err = readmsg.BtcDecode(&buf, AddrV2Version, enc)
if err != nil {
t.Errorf("decode of MsgWTxIdRelay failed [%v] err <%v>", buf,
err)
Expand Down Expand Up @@ -142,21 +142,21 @@ func TestWTxIdRelayWire(t *testing.T) {
BaseEncoding,
},

// Protocol version WTxIdRelayVersion+1
// Protocol version AddrV2Version+1
{
msgWTxIdRelay,
msgWTxIdRelay,
msgWTxIdRelayEncoded,
WTxIdRelayVersion + 1,
AddrV2Version + 1,
BaseEncoding,
},

// Protocol version WTxIdRelayVersion
// Protocol version AddrV2Version
{
msgWTxIdRelay,
msgWTxIdRelay,
msgWTxIdRelayEncoded,
WTxIdRelayVersion,
AddrV2Version,
BaseEncoding,
},
}
Expand Down
16 changes: 6 additions & 10 deletions wire/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,12 @@ const (
// feefilter message.
FeeFilterVersion uint32 = 70013

// SendAddrV2Version is the protocol version which added two new
// messages. sendaddrv2 is sent during the version-verack handshake
// and signals support for sending and receiving the addrv2 message. In
// the future, new messages that occur during the version-verack
// handshake will not come with a protocol version bump.
// In addition, wtxidrelay was also added as an optional message in the
// same protocol version.
SendAddrV2Version uint32 = 70016
WTxIdRelayVersion uint32 = SendAddrV2Version
AddrV2Version uint32 = SendAddrV2Version // Keep for upstream compatibility
// AddrV2Version is the protocol version which added two new messages.
// sendaddrv2 is sent during the version-verack handshake and signals
// support for sending and receiving the addrv2 message. In the future,
// new messages that occur during the version-verack handshake will not
// come with a protocol version bump.
AddrV2Version uint32 = 70016
)

const (
Expand Down