Skip to content

Commit 8f28947

Browse files
committed
wire, peer: fix broken ibd
IBD for new nodes were broken due to the version handshake failing between nodes that recognized wtxid based relays. Reverting the changes that were made so that the node is able to connect to those nodes.
1 parent c9782b7 commit 8f28947

7 files changed

+44
-52
lines changed

peer/peer.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,18 @@ import (
1818
"sync/atomic"
1919
"time"
2020

21-
"github.com/btcsuite/go-socks/socks"
22-
"github.com/davecgh/go-spew/spew"
23-
"github.com/decred/dcrd/lru"
24-
2521
"github.com/btcsuite/btcd/blockchain"
2622
"github.com/btcsuite/btcd/chaincfg"
2723
"github.com/btcsuite/btcd/chaincfg/chainhash"
2824
"github.com/btcsuite/btcd/wire"
25+
"github.com/btcsuite/go-socks/socks"
26+
"github.com/davecgh/go-spew/spew"
27+
"github.com/decred/dcrd/lru"
2928
)
3029

3130
const (
3231
// MaxProtocolVersion is the max protocol version the peer supports.
33-
MaxProtocolVersion = wire.SendAddrV2Version
32+
MaxProtocolVersion = wire.AddrV2Version
3433

3534
// DefaultTrickleInterval is the min time between attempts to send an
3635
// inv message to a peer.
@@ -882,8 +881,8 @@ func (p *Peer) PushAddrMsg(addresses []*wire.NetAddress) ([]*wire.NetAddress, er
882881
//
883882
// This function is safe for concurrent access.
884883
func (p *Peer) PushAddrV2Msg(addrs []*wire.NetAddressV2) (
885-
[]*wire.NetAddressV2, error,
886-
) {
884+
[]*wire.NetAddressV2, error) {
885+
887886
count := len(addrs)
888887

889888
// Nothing to send.
@@ -1928,8 +1927,8 @@ func (p *Peer) QueueMessage(msg wire.Message, doneChan chan<- struct{}) {
19281927
//
19291928
// This function is safe for concurrent access.
19301929
func (p *Peer) QueueMessageWithEncoding(msg wire.Message, doneChan chan<- struct{},
1931-
encoding wire.MessageEncoding,
1932-
) {
1930+
encoding wire.MessageEncoding) {
1931+
19331932
// Avoid risk of deadlock if goroutine already exited. The goroutine
19341933
// we will be sending to hangs around until it knows for a fact that
19351934
// it is marked as disconnected and *then* it drains the channels.
@@ -2186,7 +2185,7 @@ func (p *Peer) writeLocalVersionMsg() error {
21862185
// writeSendAddrV2Msg writes our sendaddrv2 message to the remote peer if the
21872186
// peer supports protocol version 70016 and above.
21882187
func (p *Peer) writeSendAddrV2Msg(pver uint32) error {
2189-
if pver < wire.SendAddrV2Version {
2188+
if pver < wire.AddrV2Version {
21902189
return nil
21912190
}
21922191

@@ -2214,7 +2213,7 @@ func (p *Peer) waitToFinishNegotiation(pver uint32) error {
22142213

22152214
switch m := remoteMsg.(type) {
22162215
case *wire.MsgSendAddrV2:
2217-
if pver >= wire.SendAddrV2Version {
2216+
if pver >= wire.AddrV2Version {
22182217
p.flagsMtx.Lock()
22192218
p.sendAddrV2 = true
22202219
p.flagsMtx.Unlock()

wire/message.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,6 @@ func makeEmptyMessage(command string) (Message, error) {
112112
case CmdSendAddrV2:
113113
msg = &MsgSendAddrV2{}
114114

115-
case CmdWTxIdRelay:
116-
msg = &MsgWTxIdRelay{}
117-
118115
case CmdGetAddr:
119116
msg = &MsgGetAddr{}
120117

@@ -280,8 +277,8 @@ func WriteMessage(w io.Writer, msg Message, pver uint32, btcnet BitcoinNet) erro
280277
// to specify the message encoding format to be used when serializing wire
281278
// messages.
282279
func WriteMessageWithEncodingN(w io.Writer, msg Message, pver uint32,
283-
btcnet BitcoinNet, encoding MessageEncoding,
284-
) (int, error) {
280+
btcnet BitcoinNet, encoding MessageEncoding) (int, error) {
281+
285282
totalBytes := 0
286283

287284
// Enforce max command size.
@@ -357,8 +354,8 @@ func WriteMessageWithEncodingN(w io.Writer, msg Message, pver uint32,
357354
// allows the caller to specify which message encoding is to to consult when
358355
// decoding wire messages.
359356
func ReadMessageWithEncodingN(r io.Reader, pver uint32, btcnet BitcoinNet,
360-
enc MessageEncoding,
361-
) (int, Message, []byte, error) {
357+
enc MessageEncoding) (int, Message, []byte, error) {
358+
362359
totalBytes := 0
363360
n, hdr, err := readMessageHeader(r)
364361
totalBytes += n

wire/msgsendaddrv2.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type MsgSendAddrV2 struct{}
1515
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
1616
// This is part of the Message interface implementation.
1717
func (msg *MsgSendAddrV2) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
18-
if pver < SendAddrV2Version {
18+
if pver < AddrV2Version {
1919
str := fmt.Sprintf("sendaddrv2 message invalid for protocol "+
2020
"version %d", pver)
2121
return messageError("MsgSendAddrV2.BtcDecode", str)
@@ -27,7 +27,7 @@ func (msg *MsgSendAddrV2) BtcDecode(r io.Reader, pver uint32, enc MessageEncodin
2727
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
2828
// This is part of the Message interface implementation.
2929
func (msg *MsgSendAddrV2) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
30-
if pver < SendAddrV2Version {
30+
if pver < AddrV2Version {
3131
str := fmt.Sprintf("sendaddrv2 message invalid for protocol "+
3232
"version %d", pver)
3333
return messageError("MsgSendAddrV2.BtcEncode", str)

wire/msgsendaddrv2_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestSendAddrV2(t *testing.T) {
4545

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

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

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

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

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

145-
// Protocol version SendAddrV2Version+1
145+
// Protocol version AddrV2Version+1
146146
{
147147
msgSendAddrV2,
148148
msgSendAddrV2,
149149
msgSendAddrV2Encoded,
150-
SendAddrV2Version + 1,
150+
AddrV2Version + 1,
151151
BaseEncoding,
152152
},
153153

154-
// Protocol version SendAddrV2Version
154+
// Protocol version AddrV2Version
155155
{
156156
msgSendAddrV2,
157157
msgSendAddrV2,
158158
msgSendAddrV2Encoded,
159-
SendAddrV2Version,
159+
AddrV2Version,
160160
BaseEncoding,
161161
},
162162
}

wire/msgwtxidrelay.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type MsgWTxIdRelay struct{}
1919
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
2020
// This is part of the Message interface implementation.
2121
func (msg *MsgWTxIdRelay) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
22-
if pver < WTxIdRelayVersion {
22+
if pver < AddrV2Version {
2323
str := fmt.Sprintf("wtxidrelay message invalid for protocol "+
2424
"version %d", pver)
2525
return messageError("MsgWTxIdRelay.BtcDecode", str)
@@ -31,7 +31,7 @@ func (msg *MsgWTxIdRelay) BtcDecode(r io.Reader, pver uint32, enc MessageEncodin
3131
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
3232
// This is part of the Message interface implementation.
3333
func (msg *MsgWTxIdRelay) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
34-
if pver < WTxIdRelayVersion {
34+
if pver < AddrV2Version {
3535
str := fmt.Sprintf("wtxidrelay message invalid for protocol "+
3636
"version %d", pver)
3737
return messageError("MsgWTxIdRelay.BtcEncode", str)

wire/msgwtxidrelay_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestWTxIdRelay(t *testing.T) {
4545

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

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

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

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

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

145-
// Protocol version WTxIdRelayVersion+1
145+
// Protocol version AddrV2Version+1
146146
{
147147
msgWTxIdRelay,
148148
msgWTxIdRelay,
149149
msgWTxIdRelayEncoded,
150-
WTxIdRelayVersion + 1,
150+
AddrV2Version + 1,
151151
BaseEncoding,
152152
},
153153

154-
// Protocol version WTxIdRelayVersion
154+
// Protocol version AddrV2Version
155155
{
156156
msgWTxIdRelay,
157157
msgWTxIdRelay,
158158
msgWTxIdRelayEncoded,
159-
WTxIdRelayVersion,
159+
AddrV2Version,
160160
BaseEncoding,
161161
},
162162
}

wire/protocol.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,12 @@ const (
5252
// feefilter message.
5353
FeeFilterVersion uint32 = 70013
5454

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

6763
const (

0 commit comments

Comments
 (0)