[DEV-236] add counter to blockdag that will increment for each valid … (#115)

* [DEV-236] add counter to blockdag that will increment for each valid block that is connected to the dag

* [DEV-236] increment dag.blockCount while building block index in initDagState

* [DEV-236] changed dag.BlockCount to return an unsigned value
This commit is contained in:
Ori Newman 2018-11-05 12:58:37 +02:00 committed by Svarog
parent 35546b62d0
commit d70e2be641
3 changed files with 21 additions and 21 deletions

View File

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

View File

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

View File

@ -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++
}