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.
This commit is contained in:
Dave Collins 2021-11-19 03:30:39 -06:00
parent 1c160f4a34
commit 20dc378a43
No known key found for this signature in database
GPG Key ID: B8904D9D9C93D1F2
3 changed files with 14 additions and 50 deletions

View File

@ -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 {

View File

@ -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.

View File

@ -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()