mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-07-07 05:12:31 +00:00
[DEV-86] Update BlockDAG.initDAGState to load the whole dag
* [DEV-86] remove selectedPrevBlock from initDAGState * [DEV-86] re-implement dagview to be dag compatible * [DEV-86] change comments
This commit is contained in:
parent
460216be65
commit
713b01c69d
@ -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.
|
// 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 {
|
for _, block := range bs {
|
||||||
return block
|
return block
|
||||||
}
|
}
|
||||||
|
@ -9,11 +9,12 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"math/rand"
|
||||||
|
|
||||||
"github.com/daglabs/btcd/dagconfig"
|
"github.com/daglabs/btcd/dagconfig"
|
||||||
"github.com/daglabs/btcd/dagconfig/daghash"
|
"github.com/daglabs/btcd/dagconfig/daghash"
|
||||||
"github.com/daglabs/btcd/wire"
|
"github.com/daglabs/btcd/wire"
|
||||||
"github.com/daglabs/btcutil"
|
"github.com/daglabs/btcutil"
|
||||||
"math/rand"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestHaveBlock tests the HaveBlock API to ensure proper functionality.
|
// TestHaveBlock tests the HaveBlock API to ensure proper functionality.
|
||||||
|
@ -999,10 +999,7 @@ func (dag *BlockDAG) initDAGState() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine the parent block node. Since we iterate block headers
|
parents := newSet()
|
||||||
// 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
|
|
||||||
if lastNode == nil {
|
if lastNode == nil {
|
||||||
blockHash := header.BlockHash()
|
blockHash := header.BlockHash()
|
||||||
if !blockHash.IsEqual(dag.dagParams.GenesisHash) {
|
if !blockHash.IsEqual(dag.dagParams.GenesisHash) {
|
||||||
@ -1010,23 +1007,25 @@ func (dag *BlockDAG) initDAGState() error {
|
|||||||
"first entry in block index to be genesis block, "+
|
"first entry in block index to be genesis block, "+
|
||||||
"found %s", blockHash))
|
"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 {
|
} else {
|
||||||
parent = dag.index.LookupNode(header.SelectedPrevBlock())
|
for _, hash := range header.PrevBlocks {
|
||||||
if parent == nil {
|
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 "+
|
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,
|
// Initialize the block node for the block, connect it,
|
||||||
// and add it to the block index.
|
// and add it to the block index.
|
||||||
node := &blockNodes[i]
|
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
|
node.status = status
|
||||||
dag.index.addNode(node)
|
dag.index.addNode(node)
|
||||||
|
|
||||||
|
@ -11,9 +11,9 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/daglabs/btcd/dagconfig/daghash"
|
||||||
"github.com/daglabs/btcd/database"
|
"github.com/daglabs/btcd/database"
|
||||||
"github.com/daglabs/btcd/wire"
|
"github.com/daglabs/btcd/wire"
|
||||||
"github.com/daglabs/btcd/dagconfig/daghash"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestErrNotInDAG ensures the functions related to errNotInDAG work
|
// TestErrNotInDAG ensures the functions related to errNotInDAG work
|
||||||
|
Loading…
x
Reference in New Issue
Block a user