Implement address rescanning.

When a wallet is opened, a rescan request will be sent to btcd with
all active addresses from the wallet, to rescan from the last synced
block (now saved to the wallet file) and the current best block.

As multi-account support is further explored, rescan requests should
be batched together to send a single request for all addresses from
all wallets.

This change introduces several changes to the wallet, tx, and utxo
files.  Wallet files are still compatible, however, a rescan will try
to start at the genesis block since no correct "last synced to" or
"created at block X" was saved.  The tx and utxo files, however, are
not compatible and should be deleted (or an error will occur on read).
If any errors occur opening the utxo file, a rescan will start
beginning at the creation block saved in the wallet.
This commit is contained in:
Josh Rickmar 2013-10-29 21:22:14 -04:00
parent de76220c6b
commit 18fb993d0b
10 changed files with 476 additions and 199 deletions

View file

@ -20,9 +20,47 @@ import (
"fmt"
"os"
"path/filepath"
"sync"
"time"
)
var (
// dirtyWallets holds a set of wallets that include dirty components.
dirtyWallets = struct {
sync.Mutex
m map[*BtcWallet]bool
}{
m: make(map[*BtcWallet]bool),
}
)
// DirtyWalletSyncer synces dirty wallets for cases where the updated
// information was not required to be immediately written to disk. Wallets
// may be added to dirtyWallets and will be checked and processed every 10
// seconds by this function.
//
// This never returns and is meant to be called from a goroutine.
func DirtyWalletSyncer() {
ticker := time.Tick(10 * time.Second)
for {
select {
case <-ticker:
dirtyWallets.Lock()
for w := range dirtyWallets.m {
log.Debugf("Syncing wallet '%v' to disk",
w.Wallet.Name())
if err := w.writeDirtyToDisk(); err != nil {
log.Errorf("cannot sync dirty wallet: %v",
err)
} else {
delete(dirtyWallets.m, w)
}
}
dirtyWallets.Unlock()
}
}
}
// writeDirtyToDisk checks for the dirty flag on an account's wallet,
// txstore, and utxostore, writing them to disk if any are dirty.
func (w *BtcWallet) writeDirtyToDisk() error {