Skip to content

Commit

Permalink
Merge pull request #237 from kcalvinalvin/2025-01-08-dont-assign-head…
Browse files Browse the repository at this point in the history
…er-as-best-tip-on-process-header

blockchain: don't assign header as the best tip on maybeAcceptBlockHeader
  • Loading branch information
kcalvinalvin authored Jan 8, 2025
2 parents 538c3fc + 6d36a75 commit 89a0bb3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
2 changes: 0 additions & 2 deletions blockchain/accept.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,5 @@ func (b *BlockChain) maybeAcceptBlockHeader(header *wire.BlockHeader, checkHeade
newNode := newBlockNode(header, prevNode)
b.index.AddNode(newNode)

// This node is now the end of the best chain.
b.bestChain.SetTip(newNode)
return newNode, nil
}
37 changes: 18 additions & 19 deletions netsync/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,7 @@ func (sm *SyncManager) handleHeadersMsg(hmsg *headersMsg) {

if sm.headersBuildMode {
var finalHeader *wire.BlockHeader
var finalHeight int32
for _, blockHeader := range msg.Headers {
finalHeader = blockHeader

Expand All @@ -1103,31 +1104,29 @@ func (sm *SyncManager) handleHeadersMsg(hmsg *headersMsg) {
peer.Disconnect()
return
}

prevNodeEl := sm.headerList.Back()
prevNode := prevNodeEl.Value.(*headerNode)

if prevNode.hash.IsEqual(&blockHeader.PrevBlock) {
hash := blockHeader.BlockHash()
node := headerNode{height: prevNode.height + 1, hash: &hash}
finalHeight = node.height
sm.headerList.PushBack(&node)
}
}

finalHash := finalHeader.BlockHash()
finalHeight, err := sm.chain.BlockHeightByHash(&finalHash)
if err != nil {
log.Warnf("Failed to grab block height for last block hash "+
"%v. %v.", finalHash.String(), err)
return
}

// We're now done downloading headers at this point.
if finalHeight >= sm.chain.AssumeUtreexoHeight() {
assumeUtreexoHeight := sm.chain.AssumeUtreexoHeight()
indexHash, err := sm.chain.BlockHashByHeight(assumeUtreexoHeight)
if err != nil {
log.Warnf("Failed to grab block height for last block hash "+
"%v. %v.", finalHash.String(), err)
return
}
if sm.chain.AssumeUtreexoHash() != *indexHash {
log.Warnf("The nodea had hash %v hardcoded in but the valid proof-of-work "+
assumeUtreexoHash := sm.chain.AssumeUtreexoHash()
if !finalHash.IsEqual(&assumeUtreexoHash) {
log.Warnf("The node had hash %v hardcoded in but the valid proof-of-work "+
"chain has the hash %v at height %v. The user should not trust this "+
"software as genuine and there may be attempts to steal funds. The user "+
"should delete the datadir at %v ", sm.chain.AssumeUtreexoHash().String(),
indexHash.String(), assumeUtreexoHeight)
"should delete the datadir", sm.chain.AssumeUtreexoHash().String(),
finalHash.String(), finalHeight)
os.Exit(1)
}

Expand All @@ -1147,7 +1146,7 @@ func (sm *SyncManager) handleHeadersMsg(hmsg *headersMsg) {
"at block %v(%d)", bestState.Hash.String(), bestState.Height)

locator := blockchain.BlockLocator([]*chainhash.Hash{&bestState.Hash})
err = peer.PushGetBlocksMsg(locator, &zeroHash)
err := peer.PushGetBlocksMsg(locator, &zeroHash)
if err != nil {
log.Warnf("Failed to send getblocks message to "+
"peer %s: %v", peer.Addr(), err)
Expand All @@ -1162,7 +1161,7 @@ func (sm *SyncManager) handleHeadersMsg(hmsg *headersMsg) {
// next checkpoint.
locator := blockchain.BlockLocator([]*chainhash.Hash{&finalHash})
stopHash := sm.chain.AssumeUtreexoHash()
err = peer.PushGetHeadersMsg(locator, &stopHash)
err := peer.PushGetHeadersMsg(locator, &stopHash)
if err != nil {
log.Warnf("Failed to send getheaders message to "+
"peer %s: %v", peer.Addr(), err)
Expand Down

0 comments on commit 89a0bb3

Please sign in to comment.