Cleanup and slightly optimize the progress logging.

Previously the logging function which reports on progress was called for
every block, regardless of whether it was an orphan or not.  This could be
confusing since it could show a different number of blocks processed as
compared to the old versus new heights reported (orphans do not add to the
block height since they aren't extending the main chain).  Further, the
database had to be consulted for the latest block since the block we just
processed might not be the latest one if it was an orphan.  This is quite
a bit more time conusming than it should've been for progress reporting.

This commit modifies that to only include non-orphan blocks.  As a result,
the latest height shown will match the number of blocks processed (even
when there are orphans) and the additional block lookup from the database
is avoided.
This commit is contained in:
Dave Collins 2014-01-30 12:31:13 -06:00
parent 79ff9b76ee
commit c6d865f3b5

View file

@ -6,7 +6,6 @@ package main
import (
"container/list"
"fmt"
"github.com/conformal/btcchain"
"github.com/conformal/btcdb"
"github.com/conformal/btcutil"
@ -268,9 +267,9 @@ func (b *blockManager) handleDonePeerMsg(peers *list.List, p *peer) {
// logBlockHeight logs a new block height as an information message to show
// progress to the user. In order to prevent spam, it limits logging to one
// message every 10 seconds with duration and totals included.
func (b *blockManager) logBlockHeight(numTx, height int64, latestHash *btcwire.ShaHash) {
func (b *blockManager) logBlockHeight(block *btcutil.Block) {
b.receivedLogBlocks++
b.receivedLogTx += numTx
b.receivedLogTx += int64(len(block.MsgBlock().Transactions))
now := time.Now()
duration := now.Sub(b.lastBlockLogTime)
@ -282,13 +281,6 @@ func (b *blockManager) logBlockHeight(numTx, height int64, latestHash *btcwire.S
durationMillis := int64(duration / time.Millisecond)
tDuration := 10 * time.Millisecond * time.Duration(durationMillis/10)
// Attempt to get the timestamp of the latest block.
blockTimeStr := ""
header, err := b.server.db.FetchBlockHeaderBySha(latestHash)
if err == nil {
blockTimeStr = fmt.Sprintf(", %s", header.Timestamp)
}
// Log information about new block height.
blockStr := "blocks"
if b.receivedLogBlocks == 1 {
@ -298,9 +290,9 @@ func (b *blockManager) logBlockHeight(numTx, height int64, latestHash *btcwire.S
if b.receivedLogTx == 1 {
txStr = "transaction"
}
bmgrLog.Infof("Processed %d %s in the last %s (%d %s, height %d%s)",
bmgrLog.Infof("Processed %d %s in the last %s (%d %s, height %d, %s)",
b.receivedLogBlocks, blockStr, tDuration, b.receivedLogTx,
txStr, height, blockTimeStr)
txStr, block.Height(), block.MsgBlock().Header.Timestamp)
b.receivedLogBlocks = 0
b.receivedLogTx = 0
@ -455,21 +447,13 @@ func (b *blockManager) handleBlockMsg(bmsg *blockMsg) {
return
}
// Don't keep track of the peer that sent the block any longer if it's
// not an orphan.
// When the block is not an orphan, don't keep track of the peer that
// sent it any longer and log information about it.
if !b.blockChain.IsKnownOrphan(blockSha) {
delete(b.blockPeer, *blockSha)
b.logBlockHeight(bmsg.block)
}
// Log info about the new block height.
latestHash, height, err := b.server.db.NewestSha()
if err != nil {
bmgrLog.Warnf("Failed to obtain latest sha - %v", err)
return
}
b.logBlockHeight(int64(len(bmsg.block.MsgBlock().Transactions)), height,
latestHash)
// Sync the db to disk.
b.server.db.Sync()
}