From 7390a62a8df9b9b03a46e24b66f3cf228edea322 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Tue, 28 Jan 2014 13:34:42 -0600 Subject: [PATCH] Correct internal node children on reorg. Previously the code was only adding a new block node as a child in the inernal node index for the cases it extended the main chain or a side chain and not for a node which caused a reorg. This resulted in the block node pruning code not clearing the parent link of reorged nodes which ultimately led to a sanity check error accordingly. This commit resolves the issue by ensuring new block nodes are added as children of their respective parents in all cases. Closes #4. --- chain.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/chain.go b/chain.go index 9727b8b47..bf4e75a27 100644 --- a/chain.go +++ b/chain.go @@ -825,7 +825,6 @@ func (b *BlockChain) reorganizeChain(detachNodes, attachNodes *list.List) error if err != nil { return err } - } // Disconnect blocks from the main chain. @@ -918,13 +917,13 @@ func (b *BlockChain) connectBestChain(node *blockNode, block *btcutil.Block, fas b.blockCache[*node.hash] = block b.index[*node.hash] = node + // Connect the parent node to this node. + node.inMainChain = false + node.parent.children = append(node.parent.children, node) + // We're extending (or creating) a side chain, but the cumulative // work for this new side chain is not enough to make it the new chain. if node.workSum.Cmp(b.bestChain.workSum) <= 0 { - // Connect the parent node to this node. - node.inMainChain = false - node.parent.children = append(node.parent.children, node) - // Find the fork point. fork := node for ; fork.parent != nil; fork = fork.parent {