From 20dc378a43411808d5a09ca267dcf3eb318bb85f Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Fri, 19 Nov 2021 03:30:39 -0600 Subject: [PATCH] multi: Move last ann block from peer to netsync. This moves the tracking of the last announced block hash from the peer package to the net sync manager as it really is an implementation detail that nothing outside of the network sync manager needs as evidenced by the fact that nothing else accesses it. --- internal/netsync/manager.go | 10 ++++++---- peer/peer.go | 40 ++++++++----------------------------- peer/peer_test.go | 14 ------------- 3 files changed, 14 insertions(+), 50 deletions(-) diff --git a/internal/netsync/manager.go b/internal/netsync/manager.go index fd88c1f5..91d7a03f 100644 --- a/internal/netsync/manager.go +++ b/internal/netsync/manager.go @@ -216,6 +216,8 @@ type syncMgrPeer struct { // is used to detect peers that have either diverged so far they are no // longer useful or are otherwise being malicious. numConsecutiveOrphanHeaders int32 + + lastAnnouncedBlock *chainhash.Hash } // headerSyncState houses the state used to track the header sync progress and @@ -848,10 +850,10 @@ func (m *SyncManager) handleBlockMsg(bmsg *blockMsg) { continue } - lastAnnBlock := p.LastAnnouncedBlock() + lastAnnBlock := p.lastAnnouncedBlock if lastAnnBlock != nil && *lastAnnBlock == *blockHash { p.UpdateLastBlockHeight(blockHeight) - p.UpdateLastAnnouncedBlock(nil) + p.lastAnnouncedBlock = nil } } } @@ -1043,7 +1045,7 @@ func (m *SyncManager) handleHeadersMsg(hmsg *headersMsg) { // above and update the height for the peer too. finalHeader := headers[len(headers)-1] finalReceivedHash := &headerHashes[len(headerHashes)-1] - peer.UpdateLastAnnouncedBlock(finalReceivedHash) + peer.lastAnnouncedBlock = finalReceivedHash peer.UpdateLastBlockHeight(int64(finalHeader.Height)) // Update the sync height if the new best known header height exceeds it. @@ -1265,7 +1267,7 @@ func (m *SyncManager) handleInvMsg(imsg *invMsg) { // inventory above (if any). In the case the header for that block is // already known, use that information to update the height for the peer // too. - peer.UpdateLastAnnouncedBlock(&lastBlock.Hash) + peer.lastAnnouncedBlock = &lastBlock.Hash if isCurrent { header, err := m.cfg.Chain.HeaderByHash(&lastBlock.Hash) if err == nil { diff --git a/peer/peer.go b/peer/peer.go index f61a4d7c..3562f02f 100644 --- a/peer/peer.go +++ b/peer/peer.go @@ -452,15 +452,14 @@ type Peer struct { // These fields keep track of statistics for the peer and are protected // by the statsMtx mutex. - statsMtx sync.RWMutex - timeOffset int64 - timeConnected time.Time - startingHeight int64 - lastBlock int64 - 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. + statsMtx sync.RWMutex + timeOffset int64 + timeConnected time.Time + startingHeight int64 + lastBlock int64 + 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 @@ -496,18 +495,6 @@ func (p *Peer) UpdateLastBlockHeight(newHeight int64) { p.statsMtx.Unlock() } -// UpdateLastAnnouncedBlock updates meta-data about the last block hash this -// peer is known to have announced. -// -// This function is safe for concurrent access. -func (p *Peer) UpdateLastAnnouncedBlock(blkHash *chainhash.Hash) { - log.Tracef("Updating last blk for peer %v, %v", p.addr, blkHash) - - p.statsMtx.Lock() - p.lastAnnouncedBlock = blkHash - p.statsMtx.Unlock() -} - // AddKnownInventory adds the passed inventory to the cache of known inventory // for the peer. // @@ -627,17 +614,6 @@ func (p *Peer) UserAgent() string { return userAgent } -// LastAnnouncedBlock returns the last announced block of the remote peer. -// -// This function is safe for concurrent access. -func (p *Peer) LastAnnouncedBlock() *chainhash.Hash { - p.statsMtx.RLock() - lastAnnouncedBlock := p.lastAnnouncedBlock - p.statsMtx.RUnlock() - - return lastAnnouncedBlock -} - // LastPingNonce returns the last ping nonce of the remote peer. // // This function is safe for concurrent access. diff --git a/peer/peer_test.go b/peer/peer_test.go index c28e1745..0eca473f 100644 --- a/peer/peer_test.go +++ b/peer/peer_test.go @@ -662,20 +662,6 @@ func TestOutboundPeer(t *testing.T) { } p1.AssociateConnection(c1) - // Test update latest block - latestBlockHash, err := chainhash.NewHashFromStr("1a63f9cdff1752e6375c8c76e543a71d239e1a2e5c6db1aa679") - if err != nil { - t.Errorf("NewHashFromStr: unexpected err %v\n", err) - return - } - p1.UpdateLastAnnouncedBlock(latestBlockHash) - p1.UpdateLastBlockHeight(234440) - if p1.LastAnnouncedBlock() != latestBlockHash { - t.Errorf("LastAnnouncedBlock: wrong block - got %v, want %v", - p1.LastAnnouncedBlock(), latestBlockHash) - return - } - // Test Queue Inv after connection p1.QueueInventory(fakeInv) p1.Disconnect()