Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
chain/indexers: Allow reorg of block one.
Browse files Browse the repository at this point in the history
This modifies the code to permit a reorganization that includes block
one.  Even though this will never happen on the main network since it's
buried so deep, it is entirely possible for the first block to be
reorged away on test chains.

Not only is it more technically accurate to permit a reorg of the first
block, so long as the new one satisfies all of consensus rules, it also
facilitates upcoming test code which relies on that capability.

It should be noted that this is technically a hard fork, however, it
only applies to the first block and so it would now require a reorg of
over 150,000 blocks which is completely infeasible.
  • Loading branch information
davecgh committed Jul 17, 2017
1 parent 01b9a87 commit 9c5f4d3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
11 changes: 5 additions & 6 deletions blockchain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1174,19 +1174,18 @@ func (b *BlockChain) getReorganizeNodes(node *blockNode) (*list.List, *list.List
// common ancestor adding each block to the list of nodes to detach from
// the main chain.
for n := b.bestNode; n != nil; n = n.parent {
if n.hash == ancestor.hash {
break
}
detachNodes.PushBack(n)

if n.parent == nil {
var err error
n.parent, err = b.findNode(&n.header.PrevBlock, maxSearchDepth)
if err != nil {
return nil, nil, err
}
}

if n.hash == ancestor.hash {
break
}

detachNodes.PushBack(n)
}

return detachNodes, attachNodes, nil
Expand Down
2 changes: 1 addition & 1 deletion blockchain/indexers/addrindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ func (idx *AddrIndex) indexPkScript(data writeIndexData, scriptVersion uint16, p
// transaction using the passed map.
func (idx *AddrIndex) indexBlock(data writeIndexData, block, parent *dcrutil.Block, view *blockchain.UtxoViewpoint) {
var parentRegularTxs []*dcrutil.Tx
if approvesParent(block) {
if approvesParent(block) && block.Height() > 1 {
parentRegularTxs = parent.Transactions()
}
for txIdx, tx := range parentRegularTxs {
Expand Down
2 changes: 1 addition & 1 deletion blockchain/indexers/txindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func dbRemoveTxIndexEntries(dbTx database.Tx, block, parent *dcrutil.Block) erro
}

// Remove all of the regular transactions of the parent if voted valid.
if approvesParent(block) {
if approvesParent(block) && block.Height() > 1 {
if err := removeEntries(parent.Transactions()); err != nil {
return err
}
Expand Down

0 comments on commit 9c5f4d3

Please sign in to comment.