Skip to content

Commit

Permalink
netsync: add logger for blocks downloaded from different peers during
Browse files Browse the repository at this point in the history
headers-first block download
  • Loading branch information
kcalvinalvin committed Aug 7, 2024
1 parent aa10e74 commit 9d1c6a2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
58 changes: 58 additions & 0 deletions netsync/blocklogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,61 @@ func (b *blockProgressLogger) LogBlockHeight(block *btcutil.Block, chain *blockc
func (b *blockProgressLogger) SetLastLogTime(time time.Time) {
b.lastBlockLogTime = time
}

// peerLogger logs the progress of blocks downloaded from different peers during
// headers-first download.
type peerLogger struct {
lastPeerLogTime time.Time
peers map[string]int

subsystemLogger btclog.Logger
sync.Mutex
}

// newPeerLogger returns a new peerLogger with fields initialized.
func newPeerLogger(logger btclog.Logger) *peerLogger {
return &peerLogger{
lastPeerLogTime: time.Now(),
subsystemLogger: logger,
peers: make(map[string]int),
}
}

// LogPeers logs how many blocks have been received from which peers in the last
// 10 seconds.
func (p *peerLogger) LogPeers(peer string) {
p.Lock()
defer p.Unlock()

count, found := p.peers[peer]
if found {
count++
p.peers[peer] = count
} else {
p.peers[peer] = 1
}

now := time.Now()
duration := now.Sub(p.lastPeerLogTime)
if duration < time.Second*10 {
return
}
// Truncate the duration to 10s of milliseconds.
durationMillis := int64(duration / time.Millisecond)
tDuration := 10 * time.Millisecond * time.Duration(durationMillis/10)

peerDownloadStr := ""
for peer, blockCount := range p.peers {
peerDownloadStr += fmt.Sprintf("%d blocks from %v, ",
blockCount, peer)
}

p.subsystemLogger.Infof("Peer download stats in the last %s: %s",
tDuration, peerDownloadStr)

// Reset fields.
p.lastPeerLogTime = now
for k := range p.peers {
delete(p.peers, k)
}
}
6 changes: 6 additions & 0 deletions netsync/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ type SyncManager struct {
txMemPool *mempool.TxPool
chainParams *chaincfg.Params
progressLogger *blockProgressLogger
peerLogger *peerLogger
msgChan chan interface{}
wg sync.WaitGroup
quit chan struct{}
Expand Down Expand Up @@ -932,6 +933,10 @@ func (sm *SyncManager) handleBlockMsg(bmsg *blockMsg) {
}
}

if sm.headersFirstMode {
go sm.peerLogger.LogPeers(peer.Addr())
}

// Since we may receive blocks out of order, attempt to find the next block
// and any other descendent blocks that connect to it.
processBlocks := make([]*blockMsg, 0, 1)
Expand Down Expand Up @@ -1917,6 +1922,7 @@ func New(config *Config) (*SyncManager, error) {
requestedBlocks: make(map[chainhash.Hash]struct{}),
peerStates: make(map[*peerpkg.Peer]*peerSyncState),
progressLogger: newBlockProgressLogger("Processed", log),
peerLogger: newPeerLogger(log),
msgChan: make(chan interface{}, config.MaxPeers*3),
headerList: list.New(),
quit: make(chan struct{}),
Expand Down

0 comments on commit 9d1c6a2

Please sign in to comment.