mirror of
https://github.com/LBRYFoundation/lbcd.git
synced 2025-08-23 17:47:24 +00:00
Add logging for chain forks and reorgs.
This commit adds info level log statements when a block causes a chain fork, extends a side chain (fork), or causes a reorganize. When a reorg happens, the new and old chain heads along with the fork point are logged.
This commit is contained in:
parent
e5eaaee91c
commit
0e7791058a
1 changed files with 33 additions and 2 deletions
33
chain.go
33
chain.go
|
@ -620,6 +620,19 @@ func (b *BlockChain) reorganizeChain(detachNodes, attachNodes *list.List) error
|
||||||
delete(b.blockCache, *n.hash)
|
delete(b.blockCache, *n.hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Log the point where the chain forked.
|
||||||
|
firstAttachNode := attachNodes.Front().Value.(*blockNode)
|
||||||
|
forkNode, err := b.getPrevNodeFromNode(firstAttachNode)
|
||||||
|
if err == nil {
|
||||||
|
log.Infof("REORGANIZE: Chain forks at %v", forkNode.hash)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log the old and new best chain heads.
|
||||||
|
firstDetachNode := detachNodes.Front().Value.(*blockNode)
|
||||||
|
lastAttachNode := attachNodes.Back().Value.(*blockNode)
|
||||||
|
log.Infof("REORGANIZE: Old best chain head was %v", firstDetachNode.hash)
|
||||||
|
log.Infof("REORGANIZE: New best chain head is %v", lastAttachNode.hash)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -670,8 +683,25 @@ func (b *BlockChain) connectBestChain(node *blockNode, block *btcutil.Block) err
|
||||||
if node.workSum.Cmp(b.bestChain.workSum) <= 0 {
|
if node.workSum.Cmp(b.bestChain.workSum) <= 0 {
|
||||||
// Connect the parent node to this node.
|
// Connect the parent node to this node.
|
||||||
node.inMainChain = false
|
node.inMainChain = false
|
||||||
if node.parent != nil {
|
|
||||||
node.parent.children = append(node.parent.children, node)
|
node.parent.children = append(node.parent.children, node)
|
||||||
|
|
||||||
|
// Find the fork point.
|
||||||
|
fork := node
|
||||||
|
for ; fork.parent != nil; fork = fork.parent {
|
||||||
|
if fork.inMainChain {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log information about how the block is forking the chain.
|
||||||
|
if fork.hash.IsEqual(node.parent.hash) {
|
||||||
|
log.Infof("FORK: Block %v forks the chain at height %d"+
|
||||||
|
"/block %v, but does not cause a reorganize",
|
||||||
|
node.hash, fork.height, fork.hash)
|
||||||
|
} else {
|
||||||
|
log.Infof("EXTEND FORK: Block %v extends a side chain "+
|
||||||
|
"which forks the chain at height %d/block %v",
|
||||||
|
node.hash, fork.height, fork.hash)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -686,6 +716,7 @@ func (b *BlockChain) connectBestChain(node *blockNode, block *btcutil.Block) err
|
||||||
detachNodes, attachNodes := b.getReorganizeNodes(node)
|
detachNodes, attachNodes := b.getReorganizeNodes(node)
|
||||||
|
|
||||||
// Reorganize the chain.
|
// Reorganize the chain.
|
||||||
|
log.Infof("REORGANIZE: Block %v is causing a reorganize.", node.hash)
|
||||||
err := b.reorganizeChain(detachNodes, attachNodes)
|
err := b.reorganizeChain(detachNodes, attachNodes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Add table
Reference in a new issue