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

multi: Remove reject wire protocol support. #3254

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
17 changes: 7 additions & 10 deletions peer/peer.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2013-2016 The btcsuite developers
// Copyright (c) 2016-2021 The Decred developers
// Copyright (c) 2016-2024 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -182,8 +182,9 @@ type MessageListeners struct {

// OnReject is invoked when a peer receives a reject wire message.
//
// Deprecated: This will be removed in a future release. Callers should
// avoid using it.
// Deprecated: This will be removed in a future release. The underlying
// message is no longer valid and thus this will no longer ever be invoked.
// Callers should avoid using it.
OnReject func(p *Peer, msg *wire.MsgReject)

// OnSendHeaders is invoked when a peer receives a sendheaders wire
Expand Down Expand Up @@ -1403,11 +1404,6 @@ out:
p.cfg.Listeners.OnFeeFilter(p, msg)
}

case *wire.MsgReject:
if p.cfg.Listeners.OnReject != nil {
p.cfg.Listeners.OnReject(p, msg)
}

case *wire.MsgSendHeaders:
p.flagsMtx.Lock()
p.sendHeadersPreferred = true
Expand Down Expand Up @@ -1846,9 +1842,10 @@ func (p *Peer) readRemoteVersionMsg() error {
}

// Disconnect clients that have a protocol version that is too old.
if msg.ProtocolVersion < int32(wire.InitialProcotolVersion) {
const reqProtocolVersion = int32(wire.RemoveRejectVersion)
if msg.ProtocolVersion < reqProtocolVersion {
return fmt.Errorf("protocol version must be %d or greater",
wire.InitialProcotolVersion)
reqProtocolVersion)
}

return nil
Expand Down
80 changes: 35 additions & 45 deletions peer/peer_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2015-2016 The btcsuite developers
// Copyright (c) 2016-2021 The Decred developers
// Copyright (c) 2016-2024 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand All @@ -24,9 +24,8 @@ import (
// is used to test peer connection without actually opening a network
// connection.
type conn struct {
io.Reader
io.Writer
io.Closer
io.ReadCloser
io.WriteCloser

// local network, address for the connection.
lnet, laddr string
Expand Down Expand Up @@ -59,7 +58,12 @@ func (c conn) RemoteAddr() net.Addr {

// Close handles closing the connection.
func (c conn) Close() error {
return nil
readCloseErr := c.ReadCloser.Close()
writeCloseErr := c.WriteCloser.Close()
if readCloseErr != nil {
return readCloseErr
}
return writeCloseErr
}

func (c conn) SetDeadline(t time.Time) error { return nil }
Expand All @@ -80,10 +84,10 @@ func pipe(c1, c2 *conn) (*conn, *conn) {
r1, w1 := io.Pipe()
r2, w2 := io.Pipe()

c1.Writer = w1
c2.Reader = r1
c1.Reader = r2
c2.Writer = w2
c1.WriteCloser = w1
c2.ReadCloser = r1
c1.ReadCloser = r2
c2.WriteCloser = w2

return c1, c2
}
Expand Down Expand Up @@ -392,9 +396,6 @@ func TestPeerListeners(t *testing.T) {
OnVerAck: func(p *Peer, msg *wire.MsgVerAck) {
verack <- struct{}{}
},
OnReject: func(p *Peer, msg *wire.MsgReject) {
ok <- msg
},
OnSendHeaders: func(p *Peer, msg *wire.MsgSendHeaders) {
ok <- msg
},
Expand Down Expand Up @@ -580,12 +581,11 @@ func TestPeerListeners(t *testing.T) {
outPeer.Disconnect()
}

// TestDeprecatedRejectListener ensures that the deprecated on reject listener
// is called as expected on older protocol versions.
func TestDeprecatedRejectListener(t *testing.T) {
// TestOldProtocolVersion ensures that peers with protocol versions older than
// the minimum required version are disconnected.
func TestOldProtocolVersion(t *testing.T) {
version := make(chan wire.Message, 1)
verack := make(chan struct{}, 1)
reject := make(chan wire.Message, 20)
peerCfg := &Config{
ProtocolVersion: wire.RemoveRejectVersion - 1,
Listeners: MessageListeners{
Expand All @@ -595,9 +595,6 @@ func TestDeprecatedRejectListener(t *testing.T) {
OnVerAck: func(p *Peer, msg *wire.MsgVerAck) {
verack <- struct{}{}
},
OnReject: func(p *Peer, msg *wire.MsgReject) {
reject <- msg
},
},
UserAgentName: "peer",
UserAgentVersion: "1.0",
Expand All @@ -612,43 +609,36 @@ func TestDeprecatedRejectListener(t *testing.T) {
inPeer.AssociateConnection(inConn)
defer inPeer.Disconnect()

peerCfg.Listeners = MessageListeners{
OnVerAck: func(p *Peer, msg *wire.MsgVerAck) {
verack <- struct{}{}
},
}
outPeer, err := NewOutboundPeer(peerCfg, "10.0.0.1:8333")
peerCfg.Listeners = MessageListeners{}
outPeer, err := NewOutboundPeer(peerCfg, "10.0.0.2:8333")
if err != nil {
t.Errorf("NewOutboundPeer: unexpected err %v\n", err)
t.Errorf("NewOutboundPeer: unexpected err %v", err)
return
}
outPeer.AssociateConnection(outConn)
defer outPeer.Disconnect()

for i := 0; i < 2; i++ {
select {
case <-verack:
case <-time.After(time.Second * 1):
t.Error("TestPeerListeners: verack timeout\n")
return
}
select {
case <-version:
case <-time.After(time.Second * 1):
t.Fatal("version timeout")
}

// Ensure the inbound peer is disconnected and does not receive a verack
// from the outbound side.
select {
case <-version:
case <-inPeer.quit:
case <-verack:
t.Fatal("unexpected verack from outbound peer")
case <-time.After(time.Second * 1):
t.Error("TestPeerListeners: version timeout")
return
t.Fatal("inbound peer disconnect timeout")
}

// Queue the reject message.
msg := wire.NewMsgReject("block", wire.RejectDuplicate, "dupe block")
outPeer.QueueMessage(msg, nil)
// Ensure the outbound peer is disconnected.
select {
case <-reject:
case <-outPeer.quit:
case <-time.After(time.Second * 1):
t.Error("TestPeerListeners: OnReject timeout")
return
t.Fatal("outbound peer disconnect timeout")
}
}

Expand All @@ -665,7 +655,7 @@ func TestOutboundPeer(t *testing.T) {
}

r, w := io.Pipe()
c := &conn{raddr: "10.0.0.1:8333", Writer: w, Reader: r}
c := &conn{raddr: "10.0.0.1:8333", WriteCloser: w, ReadCloser: r}

p, err := NewOutboundPeer(peerCfg, "10.0.0.1:8333")
if err != nil {
Expand Down Expand Up @@ -722,7 +712,7 @@ func TestOutboundPeer(t *testing.T) {

peerCfg.NewestBlock = newestBlock
r1, w1 := io.Pipe()
c1 := &conn{raddr: "10.0.0.1:8333", Writer: w1, Reader: r1}
c1 := &conn{raddr: "10.0.0.1:8333", WriteCloser: w1, ReadCloser: r1}
p1, err := NewOutboundPeer(peerCfg, "10.0.0.1:8333")
if err != nil {
t.Errorf("NewOutboundPeer: unexpected err - %v\n", err)
Expand All @@ -738,7 +728,7 @@ func TestOutboundPeer(t *testing.T) {
peerCfg.Net = wire.TestNet3
peerCfg.Services = wire.SFNodeBloom
r2, w2 := io.Pipe()
c2 := &conn{raddr: "10.0.0.1:8333", Writer: w2, Reader: r2}
c2 := &conn{raddr: "10.0.0.1:8333", WriteCloser: w2, ReadCloser: r2}
p2, err := NewOutboundPeer(peerCfg, "10.0.0.1:8333")
if err != nil {
t.Errorf("NewOutboundPeer: unexpected err - %v\n", err)
Expand Down
6 changes: 3 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -950,11 +950,11 @@ func (sp *serverPeer) OnVersion(_ *peer.Peer, msg *wire.MsgVersion) {
}

// Reject peers that have a protocol version that is too old.
// This is the maximum protocol version negotiated by dcrwallet 1.8.0.
if msg.ProtocolVersion < int32(wire.InitStateVersion) {
const reqProtocolVersion = int32(wire.RemoveRejectVersion)
if msg.ProtocolVersion < reqProtocolVersion {
srvrLog.Debugf("Rejecting peer %s with protocol version %d prior to "+
"the required version %d", sp, msg.ProtocolVersion,
wire.InitStateVersion)
reqProtocolVersion)
sp.Disconnect()
return
}
Expand Down
5 changes: 1 addition & 4 deletions wire/message.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2013-2016 The btcsuite developers
// Copyright (c) 2015-2023 The Decred developers
// Copyright (c) 2015-2024 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -157,9 +157,6 @@ func makeEmptyMessage(command string) (Message, error) {
case CmdGetMiningState:
msg = &MsgGetMiningState{}

case CmdReject:
msg = &MsgReject{}

case CmdSendHeaders:
msg = &MsgSendHeaders{}

Expand Down
2 changes: 0 additions & 2 deletions wire/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ func TestMessage(t *testing.T) {
[]byte("payload"))
msgCFHeaders := NewMsgCFHeaders()
msgCFTypes := NewMsgCFTypes([]FilterType{GCSFilterExtended})
msgReject := NewMsgReject("block", RejectDuplicate, "duplicate block")
msgGetInitState := NewMsgGetInitState()
msgInitState := NewMsgInitState()
msgMixPR, err := NewMsgMixPairReq([33]byte{}, 1, 1, "", 1, 1, 1, 1, []MixPairReqUTXO{}, NewTxOut(0, []byte{}), 1, 1)
Expand Down Expand Up @@ -114,7 +113,6 @@ func TestMessage(t *testing.T) {
{msgGetHeaders, msgGetHeaders, pver, MainNet, 61},
{msgHeaders, msgHeaders, pver, MainNet, 25},
{msgMemPool, msgMemPool, pver, MainNet, 24},
{msgReject, msgReject, RemoveRejectVersion - 1, MainNet, 79},
{msgGetCFilter, msgGetCFilter, pver, MainNet, 57},
{msgGetCFHeaders, msgGetCFHeaders, pver, MainNet, 58},
{msgGetCFTypes, msgGetCFTypes, pver, MainNet, 24},
Expand Down
Loading