Skip to content

Commit

Permalink
Fix bugs relating to reorganizations (#236)
Browse files Browse the repository at this point in the history
Two bugs relating to reorganizations were fixed. The first was an off
by one bug on initial sync which rarely would ever affect the end user.
The second was that logic causing the removal of all blocks to the
genesis block on a disconnection of an unknown block would cause
irreversible corruption of the wallet. The new code ignores the hash
of the block to remove and assumes that daemon removing blocks to some
common unknown forking point between the daemon and wallet before
connecting the side chain's blocks.
  • Loading branch information
cjepson authored and alexlyp committed May 10, 2016
1 parent 08936c5 commit d9f551d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 23 deletions.
30 changes: 8 additions & 22 deletions wallet/chainntfns.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,29 +289,15 @@ func (w *Wallet) disconnectBlock(b wtxmgr.BlockMeta) error {

// Disconnect the last seen block from the manager if it matches the
// removed block.
iter := w.Manager.NewIterateRecentBlocks()
if iter != nil && iter.BlockStamp().Hash == b.Hash {
if iter.Prev() {
prev := iter.BlockStamp()
w.Manager.SetSyncedTo(&prev)
err := w.TxStore.Rollback(prev.Height + 1)
if err != nil {
return err
}
} else {
// The reorg is farther back than the recently-seen list
// of blocks has recorded, so set it to unsynced which
// will in turn lead to a rescan from either the
// earliest blockstamp the addresses in the manager are
// known to have been created.
w.Manager.SetSyncedTo(nil)
// Rollback everything but the genesis block.
err := w.TxStore.Rollback(1)
if err != nil {
return err
}
}
err := w.TxStore.Rollback(b.Height)
if err != nil {
return err
}
prev, err := w.TxStore.GetBlockHash(b.Height - 1)
if err != nil {
return err
}
w.Manager.SetSyncedTo(&waddrmgr.BlockStamp{Hash: prev, Height: b.Height - 1})

// Notify interested clients of the disconnected block.
w.NtfnServer.notifyDetachedBlock(&b.Hash)
Expand Down
2 changes: 1 addition & 1 deletion wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ func (w *Wallet) syncWithChain() error {
curBlock = &bl.MsgBlock().Header.PrevBlock

// Break early if we hit the genesis block.
if i == 1 {
if i == 0 {
break
}
}
Expand Down

0 comments on commit d9f551d

Please sign in to comment.