mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00
[DEV-224] Fix getblockcount RPC command to show real data (#97)
* [DEV-224] Fix getblockcount RPC command to show real data * [DEV-227] provisionalNode.commit() should not update the original parents if the provisionalNode was created with withRelatives=false * [DEV-224] remove debug info from TestBlockCount and BlockDAG.BlockCount()
This commit is contained in:
parent
85d221f322
commit
3deaea6c15
@ -948,6 +948,25 @@ func (dag *BlockDAG) Height() int32 {
|
||||
return dag.virtual.tips().maxHeight()
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// TipHashes returns the hashes of the DAG's tips
|
||||
func (dag *BlockDAG) TipHashes() []daghash.Hash {
|
||||
return dag.virtual.tips().hashes()
|
||||
|
@ -22,6 +22,53 @@ import (
|
||||
"github.com/daglabs/btcd/wire"
|
||||
)
|
||||
|
||||
func TestBlockCount(t *testing.T) {
|
||||
// Load up blocks such that there is a fork in the DAG.
|
||||
// (genesis block) -> 1 -> 2 -> 3 -> 4
|
||||
// \-> 3b
|
||||
testFiles := []string{
|
||||
"blk_0_to_4.dat",
|
||||
"blk_3B.dat",
|
||||
}
|
||||
|
||||
var blocks []*util.Block
|
||||
for _, file := range testFiles {
|
||||
blockTmp, err := loadBlocks(file)
|
||||
if err != nil {
|
||||
t.Fatalf("Error loading file: %v\n", err)
|
||||
}
|
||||
blocks = append(blocks, blockTmp...)
|
||||
}
|
||||
|
||||
// Create a new database and chain instance to run tests against.
|
||||
dag, teardownFunc, err := DAGSetup("haveblock",
|
||||
&dagconfig.SimNetParams)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to setup chain instance: %v", err)
|
||||
}
|
||||
defer teardownFunc()
|
||||
|
||||
// Since we're not dealing with the real block chain, set the coinbase
|
||||
// maturity to 1.
|
||||
dag.TstSetCoinbaseMaturity(1)
|
||||
|
||||
for i := 1; i < len(blocks); i++ {
|
||||
isOrphan, err := dag.ProcessBlock(blocks[i], BFNone)
|
||||
if err != nil {
|
||||
t.Fatalf("ProcessBlock fail on block %v: %v\n", i, err)
|
||||
}
|
||||
if isOrphan {
|
||||
t.Fatalf("ProcessBlock incorrectly returned block %v "+
|
||||
"is an orphan\n", i)
|
||||
}
|
||||
}
|
||||
|
||||
expectedBlockCount := int64(6)
|
||||
if dag.BlockCount() != expectedBlockCount {
|
||||
t.Errorf("TestBlockCount: BlockCount expected to return %v but got %v", expectedBlockCount, dag.BlockCount())
|
||||
}
|
||||
}
|
||||
|
||||
// TestHaveBlock tests the HaveBlock API to ensure proper functionality.
|
||||
func TestHaveBlock(t *testing.T) {
|
||||
// Load up blocks such that there is a fork in the DAG.
|
||||
|
@ -1264,7 +1264,7 @@ func handleGetBlockDAGInfo(s *Server, cmd interface{}, closeChan <-chan struct{}
|
||||
|
||||
// handleGetBlockCount implements the getblockcount command.
|
||||
func handleGetBlockCount(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
|
||||
return int64(s.cfg.DAG.Height()), nil //TODO: (Ori) This is wrong. Done only for compilation
|
||||
return s.cfg.DAG.BlockCount(), nil
|
||||
}
|
||||
|
||||
// handleGetBlockHash implements the getblockhash command.
|
||||
|
Loading…
x
Reference in New Issue
Block a user