From f6d8c1cb4a678aabae1aeb8bcd2c3704d495c39d Mon Sep 17 00:00:00 2001 From: Lazy Nina Date: Sun, 22 Dec 2024 19:44:57 -0500 Subject: [PATCH] use GetParent where applicable. fix start up --- lib/blockchain.go | 24 +++++++++++------------- lib/pos_blockchain.go | 13 ++++++------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/lib/blockchain.go b/lib/blockchain.go index 71abbf143..6f1db0f12 100644 --- a/lib/blockchain.go +++ b/lib/blockchain.go @@ -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++ } } @@ -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 } } @@ -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) } } @@ -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") } diff --git a/lib/pos_blockchain.go b/lib/pos_blockchain.go index 6da5b03c0..dce96cbb8 100644 --- a/lib/pos_blockchain.go +++ b/lib/pos_blockchain.go @@ -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 } @@ -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 }