diff --git a/blockdag/blockset.go b/blockdag/blockset.go index 561d5f3dd..b62d754d7 100644 --- a/blockdag/blockset.go +++ b/blockdag/blockset.go @@ -115,7 +115,7 @@ func (bs blockSet) hashes() []daghash.Hash { } // first returns the first block in this set or nil if this set is empty. -func (bs blockSet) first() *blockNode { +func (bs blockSet) first() *blockNode { //TODO: (Ori) This is wrong. Done only for compilation. We should probably get rid of this method for _, block := range bs { return block } diff --git a/blockdag/dag_test.go b/blockdag/dag_test.go index e6e034a6b..c0d03b68d 100644 --- a/blockdag/dag_test.go +++ b/blockdag/dag_test.go @@ -9,11 +9,12 @@ import ( "testing" "time" + "math/rand" + "github.com/daglabs/btcd/dagconfig" "github.com/daglabs/btcd/dagconfig/daghash" "github.com/daglabs/btcd/wire" "github.com/daglabs/btcutil" - "math/rand" ) // TestHaveBlock tests the HaveBlock API to ensure proper functionality. diff --git a/blockdag/dagio.go b/blockdag/dagio.go index b69b1f5ed..d5da742ff 100644 --- a/blockdag/dagio.go +++ b/blockdag/dagio.go @@ -999,10 +999,7 @@ func (dag *BlockDAG) initDAGState() error { return err } - // Determine the parent block node. Since we iterate block headers - // in order of height, if the blocks are mostly linear there is a - // very good chance the previous header processed is the parent. - var parent *blockNode + parents := newSet() if lastNode == nil { blockHash := header.BlockHash() if !blockHash.IsEqual(dag.dagParams.GenesisHash) { @@ -1010,23 +1007,25 @@ func (dag *BlockDAG) initDAGState() error { "first entry in block index to be genesis block, "+ "found %s", blockHash)) } - } else if *header.SelectedPrevBlock() == lastNode.hash { - // Since we iterate block headers in order of height, if the - // blocks are mostly linear there is a very good chance the - // previous header processed is the parent. - parent = lastNode } else { - parent = dag.index.LookupNode(header.SelectedPrevBlock()) - if parent == nil { + for _, hash := range header.PrevBlocks { + parent := dag.index.LookupNode(&hash) + if parent == nil { + return AssertError(fmt.Sprintf("initDAGState: Could "+ + "not find parent %s for block %s", hash, header.BlockHash())) + } + parents.add(parent) + } + if len(parents) == 0 { return AssertError(fmt.Sprintf("initDAGState: Could "+ - "not find parent for block %s", header.BlockHash())) + "not find any parent for block %s", header.BlockHash())) } } // Initialize the block node for the block, connect it, // and add it to the block index. node := &blockNodes[i] - initBlockNode(node, header, setFromSlice(parent), dag.dagParams.K) // TODO: (Stas) This is wrong. Modified only to satisfy compilation. + initBlockNode(node, header, parents, dag.dagParams.K) node.status = status dag.index.addNode(node) diff --git a/blockdag/dagio_test.go b/blockdag/dagio_test.go index d790cfa1f..92ab494ee 100644 --- a/blockdag/dagio_test.go +++ b/blockdag/dagio_test.go @@ -11,9 +11,9 @@ import ( "reflect" "testing" + "github.com/daglabs/btcd/dagconfig/daghash" "github.com/daglabs/btcd/database" "github.com/daglabs/btcd/wire" - "github.com/daglabs/btcd/dagconfig/daghash" ) // TestErrNotInDAG ensures the functions related to errNotInDAG work