diff --git a/blockdag/dag.go b/blockdag/dag.go index 86c88014c..dd23a2e2c 100644 --- a/blockdag/dag.go +++ b/blockdag/dag.go @@ -88,6 +88,9 @@ type BlockDAG struct { // a tree-shaped structure. index *blockIndex + // blockCount holds the number of blocks in the DAG + blockCount uint64 + // virtual tracks the current tips. virtual *virtualBlock @@ -449,10 +452,16 @@ func (dag *BlockDAG) connectToDAG(node *blockNode, parentNodes blockSet, block * // Connect the block to the DAG. err := dag.connectBlock(node, block, fastAdd) - if _, ok := err.(RuleError); ok { - dag.index.SetStatusFlags(node, statusValidateFailed) - } else { - return err + if err != nil { + if _, ok := err.(RuleError); ok { + dag.index.SetStatusFlags(node, statusValidateFailed) + } else { + return err + } + } + + if dag.index.NodeStatus(node).KnownValid() { + dag.blockCount++ } // Intentionally ignore errors writing updated node status to DB. If @@ -997,22 +1006,8 @@ func (dag *BlockDAG) Height() int32 { } // BlockCount returns the number of blocks in the DAG -func (dag *BlockDAG) BlockCount() int64 { - count := int64(-1) - visited := newSet() - queue := []*blockNode{&dag.virtual.blockNode} - for len(queue) > 0 { - node := queue[0] - queue = queue[1:] - if !visited.contains(node) { - visited.add(node) - count++ - for _, parent := range node.parents { - queue = append(queue, parent) - } - } - } - return count +func (dag *BlockDAG) BlockCount() uint64 { + return dag.blockCount } // TipHashes returns the hashes of the DAG's tips @@ -1524,6 +1519,7 @@ func New(config *Config) (*BlockDAG, error) { prevOrphans: make(map[daghash.Hash][]*orphanBlock), warningCaches: newThresholdCaches(vbNumBits), deploymentCaches: newThresholdCaches(dagconfig.DefinedDeployments), + blockCount: 1, } // Initialize the chain state from the passed database. When the db diff --git a/blockdag/dag_test.go b/blockdag/dag_test.go index c60149cf5..1216b95ac 100644 --- a/blockdag/dag_test.go +++ b/blockdag/dag_test.go @@ -63,7 +63,7 @@ func TestBlockCount(t *testing.T) { } } - expectedBlockCount := int64(6) + expectedBlockCount := uint64(6) if dag.BlockCount() != expectedBlockCount { t.Errorf("TestBlockCount: BlockCount expected to return %v but got %v", expectedBlockCount, dag.BlockCount()) } diff --git a/blockdag/dagio.go b/blockdag/dagio.go index 1ecb29538..1698494b9 100644 --- a/blockdag/dagio.go +++ b/blockdag/dagio.go @@ -913,6 +913,10 @@ func (dag *BlockDAG) initDAGState() error { node.status = status dag.index.addNode(node) + if blockStatus(status).KnownValid() { + dag.blockCount++ + } + lastNode = node i++ }