Skip to content

Commit

Permalink
Merge pull request btcsuite#1698 from wpaulino/external-peer-testing
Browse files Browse the repository at this point in the history
peer: allow external testing of peer.Peer
  • Loading branch information
Roasbeef authored Mar 13, 2021
2 parents 556620f + fdb479f commit 01c6a6f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 29 deletions.
2 changes: 2 additions & 0 deletions peer/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func mockRemotePeer() error {
UserAgentVersion: "1.0.0", // User agent version to advertise.
ChainParams: &chaincfg.SimNetParams,
TrickleInterval: time.Second * 10,
AllowSelfConns: true,
}

// Accept connections on the simnet port.
Expand Down Expand Up @@ -81,6 +82,7 @@ func Example_newOutboundPeer() {
verack <- struct{}{}
},
},
AllowSelfConns: true,
}
p, err := peer.NewOutboundPeer(peerCfg, "127.0.0.1:18555")
if err != nil {
Expand Down
18 changes: 0 additions & 18 deletions peer/export_test.go

This file was deleted.

12 changes: 6 additions & 6 deletions peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ var (
// sentNonces houses the unique nonces that are generated when pushing
// version messages that are used to detect self connections.
sentNonces = lru.NewCache(50)

// allowSelfConns is only used to allow the tests to bypass the self
// connection detecting and disconnect logic since they intentionally
// do so for testing purposes.
allowSelfConns bool
)

// MessageListeners defines callback function pointers to invoke with message
Expand Down Expand Up @@ -276,6 +271,11 @@ type Config struct {
// TrickleInterval is the duration of the ticker which trickles down the
// inventory to a peer.
TrickleInterval time.Duration

// AllowSelfConns is only used to allow the tests to bypass the self
// connection detecting and disconnect logic since they intentionally
// do so for testing purposes.
AllowSelfConns bool
}

// minUint32 is a helper function to return the minimum of two uint32s.
Expand Down Expand Up @@ -1896,7 +1896,7 @@ func (p *Peer) readRemoteVersionMsg() error {
}

// Detect self connections.
if !allowSelfConns && sentNonces.Contains(msg.Nonce) {
if !p.cfg.AllowSelfConns && sentNonces.Contains(msg.Nonce) {
return errors.New("disconnecting peer connected to self")
}

Expand Down
12 changes: 7 additions & 5 deletions peer/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ func TestPeerConnection(t *testing.T) {
ProtocolVersion: wire.RejectVersion, // Configure with older version
Services: 0,
TrickleInterval: time.Second * 10,
AllowSelfConns: true,
}
peer2Cfg := &peer.Config{
Listeners: peer1Cfg.Listeners,
Expand All @@ -247,6 +248,7 @@ func TestPeerConnection(t *testing.T) {
ChainParams: &chaincfg.MainNetParams,
Services: wire.SFNodeNetwork | wire.SFNodeWitness,
TrickleInterval: time.Second * 10,
AllowSelfConns: true,
}

wantStats1 := peerStats{
Expand Down Expand Up @@ -452,6 +454,7 @@ func TestPeerListeners(t *testing.T) {
ChainParams: &chaincfg.MainNetParams,
Services: wire.SFNodeBloom,
TrickleInterval: time.Second * 10,
AllowSelfConns: true,
}
inConn, outConn := pipe(
&conn{raddr: "10.0.0.1:8333"},
Expand Down Expand Up @@ -623,6 +626,7 @@ func TestOutboundPeer(t *testing.T) {
ChainParams: &chaincfg.MainNetParams,
Services: 0,
TrickleInterval: time.Second * 10,
AllowSelfConns: true,
}

r, w := io.Pipe()
Expand Down Expand Up @@ -764,6 +768,7 @@ func TestUnsupportedVersionPeer(t *testing.T) {
ChainParams: &chaincfg.MainNetParams,
Services: 0,
TrickleInterval: time.Second * 10,
AllowSelfConns: true,
}

localNA := wire.NewNetAddressIPPort(
Expand Down Expand Up @@ -874,6 +879,7 @@ func TestDuplicateVersionMsg(t *testing.T) {
UserAgentVersion: "1.0",
ChainParams: &chaincfg.MainNetParams,
Services: 0,
AllowSelfConns: true,
}
inConn, outConn := pipe(
&conn{laddr: "10.0.0.1:9108", raddr: "10.0.0.2:9108"},
Expand Down Expand Up @@ -935,6 +941,7 @@ func TestUpdateLastBlockHeight(t *testing.T) {
UserAgentVersion: "1.0",
ChainParams: &chaincfg.MainNetParams,
Services: 0,
AllowSelfConns: true,
}
remotePeerCfg := peerCfg
remotePeerCfg.NewestBlock = func() (*chainhash.Hash, int32, error) {
Expand Down Expand Up @@ -982,8 +989,3 @@ func TestUpdateLastBlockHeight(t *testing.T) {
remotePeerHeight+1)
}
}

func init() {
// Allow self connection when running the tests.
peer.TstAllowSelfConns()
}

0 comments on commit 01c6a6f

Please sign in to comment.