Skip to content

Commit

Permalink
use GetParent where applicable. fix start up
Browse files Browse the repository at this point in the history
  • Loading branch information
lazynina committed Dec 23, 2024
1 parent 760a5f0 commit f6d8c1c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
24 changes: 11 additions & 13 deletions lib/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1063,8 +1063,9 @@ func (bc *Blockchain) _initChain() error {
// @diamondhands - we could reduce this to just the last hour if we want.
// Walk back the last 24 hours of blocks.
currBlockCounter := 1
for currBlockCounter < 3600*24 && tipNode.Header.PrevBlockHash != nil {
bc.blockIndex.GetBlockNodeByHashAndHeight(tipNode.Header.PrevBlockHash, tipNode.Header.Height-1)
parentNode := tipNode.GetParent(bc.blockIndex)
for currBlockCounter < 3600*24 && parentNode != nil {
parentNode = parentNode.GetParent(bc.blockIndex)
currBlockCounter++
}
}
Expand Down Expand Up @@ -1913,14 +1914,13 @@ func (bc *Blockchain) _FindCommonAncestor(node1 *BlockNode, node2 *BlockNode) *B
// reach the end of the lists. We only need to check node1 for nil
// since they're the same height and we are iterating both back
// in tandem.
var exists bool
for !node1.Hash.IsEqual(node2.Hash) {
node1, exists = bc.blockIndex.GetBlockNodeByHashAndHeight(node1.Header.PrevBlockHash, uint64(node1.Height-1))
if !exists {
node1 = node1.GetParent(bc.blockIndex)
if node1 == nil {
return nil
}
node2, exists = bc.blockIndex.GetBlockNodeByHashAndHeight(node2.Header.PrevBlockHash, uint64(node2.Height-1))
if !exists {
node2 = node2.GetParent(bc.blockIndex)
if node2 == nil {
return nil
}
}
Expand Down Expand Up @@ -2038,9 +2038,8 @@ func (bc *Blockchain) GetReorgBlocks(tip *BlockNode, newNode *BlockNode) (
*currentBlock = *tip
for currentBlock != nil && *currentBlock.Hash != *commonAncestor.Hash {
detachBlocks = append(detachBlocks, currentBlock)
var exists bool
currentBlock, exists = bc.blockIndex.GetBlockNodeByHashAndHeight(currentBlock.Header.PrevBlockHash, uint64(currentBlock.Height-1))
if !exists {
currentBlock = currentBlock.GetParent(bc.blockIndex)
if currentBlock == nil {
glog.Fatalf("GetReorgBlocks: Failed to find parent of block. Parent hash %v", currentBlock.Header.PrevBlockHash)
}
}
Expand All @@ -2057,9 +2056,8 @@ func (bc *Blockchain) GetReorgBlocks(tip *BlockNode, newNode *BlockNode) (
*currentBlock = *newNode
for *currentBlock.Hash != *commonAncestor.Hash {
attachBlocks = append(attachBlocks, currentBlock)
var exists bool
currentBlock, exists = bc.blockIndex.GetBlockNodeByHashAndHeight(currentBlock.Header.PrevBlockHash, uint64(currentBlock.Height-1))
if !exists {
currentBlock = currentBlock.GetParent(bc.blockIndex)
if currentBlock == nil {
// TODO: what should we do here?
glog.Fatal("GetReorgBlocks: Failed to find parent of block")
}
Expand Down
13 changes: 6 additions & 7 deletions lib/pos_blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,8 @@ func (bc *Blockchain) validateAndIndexHeaderPoS(header *MsgDeSoHeader, headerHas
if header.Height < 1 {
return nil, false, errors.New("validateAndIndexHeaderPoS: Header height is less than 1 - no valid parent height")
}
parentBlockNode, parentBlockNodeExists := bc.blockIndex.GetBlockNodeByHashAndHeight(
header.PrevBlockHash, header.Height-1)
if !parentBlockNodeExists {
parentBlockNode := blockNode.GetParent(bc.blockIndex)
if parentBlockNode == nil {
return nil, true, nil
}

Expand Down Expand Up @@ -1685,13 +1684,13 @@ func (bc *Blockchain) canCommitGrandparent(currentBlock *BlockNode) (
) {
// TODO: Is it sufficient that the current block's header points to the parent
// or does it need to have something to do with the QC?
parent, exists := bc.blockIndex.GetBlockNodeByHashAndHeight(currentBlock.Header.PrevBlockHash, uint64(currentBlock.Height-1))
if !exists {
parent := currentBlock.GetParent(bc.blockIndex)
if parent == nil {
glog.Errorf("canCommitGrandparent: Parent block %v not found in best chain map", currentBlock.Header.PrevBlockHash.String())
return nil, false
}
grandParent, exists := bc.blockIndex.GetBlockNodeByHashAndHeight(parent.Header.PrevBlockHash, uint64(parent.Height-1))
if !exists {
grandParent := parent.GetParent(bc.blockIndex)
if grandParent == nil {
glog.Errorf("canCommitGrandparent: Grandparent block %v not found in best chain map", parent.Header.PrevBlockHash.String())
return nil, false
}
Expand Down

0 comments on commit f6d8c1c

Please sign in to comment.