diff --git a/peer/peer.go b/peer/peer.go index b789b797..72a50691 100644 --- a/peer/peer.go +++ b/peer/peer.go @@ -329,23 +329,6 @@ type stallControlMsg struct { message wire.Message } -// stats is the collection of stats related to a peer. -type stats struct { - statsMtx sync.RWMutex // protects all statistics below here. - timeOffset int64 - timeConnected time.Time - lastSend time.Time - lastRecv time.Time - bytesReceived uint64 - bytesSent uint64 - startingHeight int32 - lastBlock int32 - lastAnnouncedBlock *chainhash.Hash - lastPingNonce uint64 // Set to nonce if we have a pending ping. - lastPingTime time.Time // Time we sent last ping. - lastPingMicros int64 // Time for last ping to return. -} - // StatsSnap is a snapshot of peer stats at a point in time. type StatsSnap struct { ID int32 @@ -435,6 +418,22 @@ type Peer struct { prevGetHdrsBegin *chainhash.Hash prevGetHdrsStop *chainhash.Hash + // These fields keep track of statistics for the peer and are protected + // by the statsMtx mutex. + statsMtx sync.RWMutex + timeOffset int64 + timeConnected time.Time + lastSend time.Time + lastRecv time.Time + bytesReceived uint64 + bytesSent uint64 + startingHeight int32 + lastBlock int32 + lastAnnouncedBlock *chainhash.Hash + lastPingNonce uint64 // Set to nonce if we have a pending ping. + lastPingTime time.Time // Time we sent last ping. + lastPingMicros int64 // Time for last ping to return. + stallControl chan stallControlMsg outputQueue chan outMsg sendQueue chan outMsg @@ -444,8 +443,6 @@ type Peer struct { queueQuit chan struct{} outQuit chan struct{} quit chan struct{} - - stats } // String returns the peer's address and directionality as a human-readable @@ -2020,7 +2017,6 @@ func newPeerBase(cfg *Config, inbound bool) *Peer { queueQuit: make(chan struct{}), outQuit: make(chan struct{}), quit: make(chan struct{}), - stats: stats{}, cfg: *cfg, // Copy so caller can't mutate. services: cfg.Services, protocolVersion: protocolVersion, diff --git a/peer/peer_test.go b/peer/peer_test.go index b87608b3..c7c64192 100644 --- a/peer/peer_test.go +++ b/peer/peer_test.go @@ -576,7 +576,7 @@ func TestOutboundPeer(t *testing.T) { // Test Queue Message fakeMsg := wire.NewMsgVerAck() p.QueueMessage(fakeMsg, nil) - done := make(chan struct{}, 5) + done := make(chan struct{}) p.QueueMessage(fakeMsg, done) <-done p.Shutdown() @@ -591,12 +591,14 @@ func TestOutboundPeer(t *testing.T) { return hash, 234439, nil } peerCfg.NewestBlock = newestBlock + r1, w1 := io.Pipe() + c1 := &conn{raddr: "10.0.0.1:8333", Writer: w1, Reader: r1} p1, err := peer.NewOutboundPeer(peerCfg, "10.0.0.1:8333") if err != nil { t.Errorf("NewOutboundPeer: unexpected err - %v\n", err) return } - if err := p1.Connect(c); err != nil { + if err := p1.Connect(c1); err != nil { t.Errorf("Connect: unexpected err %v\n", err) return } @@ -622,12 +624,14 @@ func TestOutboundPeer(t *testing.T) { // Test testnet peerCfg.ChainParams = &chaincfg.TestNetParams peerCfg.Services = wire.SFNodeBloom + r2, w2 := io.Pipe() + c2 := &conn{raddr: "10.0.0.1:8333", Writer: w2, Reader: r2} p2, err := peer.NewOutboundPeer(peerCfg, "10.0.0.1:8333") if err != nil { t.Errorf("NewOutboundPeer: unexpected err - %v\n", err) return } - if err := p2.Connect(c); err != nil { + if err := p2.Connect(c2); err != nil { t.Errorf("Connect: unexpected err %v\n", err) return } @@ -654,11 +658,11 @@ func TestOutboundPeer(t *testing.T) { p2.PushRejectMsg("block", wire.RejectInvalid, "invalid", nil, false) // Test Queue Messages - p2.QueueMessage(wire.NewMsgGetAddr(), done) - p2.QueueMessage(wire.NewMsgPing(1), done) - p2.QueueMessage(wire.NewMsgMemPool(), done) - p2.QueueMessage(wire.NewMsgGetData(), done) - p2.QueueMessage(wire.NewMsgGetHeaders(), done) + p2.QueueMessage(wire.NewMsgGetAddr(), nil) + p2.QueueMessage(wire.NewMsgPing(1), nil) + p2.QueueMessage(wire.NewMsgMemPool(), nil) + p2.QueueMessage(wire.NewMsgGetData(), nil) + p2.QueueMessage(wire.NewMsgGetHeaders(), nil) p2.Shutdown() }