2013-10-15 21:53:49 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Conformal Systems LLC <info@conformal.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
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.
2013-10-30 02:22:14 +01:00
|
|
|
"sync"
|
2013-10-15 21:53:49 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
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.
2013-10-30 02:22:14 +01:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-15 21:53:49 +02:00
|
|
|
// 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 {
|
|
|
|
// Temporary files append the current time to the normal file name.
|
|
|
|
// In caes of failure, the most recent temporary file can be inspected
|
|
|
|
// for validity, and moved to replace the main file.
|
|
|
|
timeStr := fmt.Sprintf("%v", time.Now().Unix())
|
|
|
|
|
|
|
|
wdir := walletdir(cfg, w.name)
|
|
|
|
wfilepath := filepath.Join(wdir, "wallet.bin")
|
|
|
|
txfilepath := filepath.Join(wdir, "tx.bin")
|
|
|
|
utxofilepath := filepath.Join(wdir, "utxo.bin")
|
|
|
|
|
|
|
|
// Wallet
|
|
|
|
if w.dirty {
|
2013-10-15 22:55:28 +02:00
|
|
|
w.mtx.Lock()
|
|
|
|
defer w.mtx.Unlock()
|
2013-10-15 21:53:49 +02:00
|
|
|
tmpfilepath := wfilepath + "-" + timeStr
|
|
|
|
tmpfile, err := os.Create(tmpfilepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err = w.WriteTo(tmpfile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-24 00:40:02 +02:00
|
|
|
tmpfile.Close()
|
2013-10-15 21:53:49 +02:00
|
|
|
|
|
|
|
// TODO(jrick): this should be atomic on *nix, but is not on
|
|
|
|
// Windows. Use _windows.go to provide atomic renames.
|
|
|
|
if err = os.Rename(tmpfilepath, wfilepath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-15 22:55:28 +02:00
|
|
|
|
|
|
|
w.dirty = false
|
2013-10-15 21:53:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transactions
|
|
|
|
if w.TxStore.dirty {
|
2013-10-15 22:55:28 +02:00
|
|
|
w.TxStore.Lock()
|
|
|
|
defer w.TxStore.Unlock()
|
2013-10-15 21:53:49 +02:00
|
|
|
tmpfilepath := txfilepath + "-" + timeStr
|
|
|
|
tmpfile, err := os.Create(tmpfilepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err = w.TxStore.s.WriteTo(tmpfile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-24 00:40:02 +02:00
|
|
|
tmpfile.Close()
|
2013-10-15 21:53:49 +02:00
|
|
|
|
|
|
|
// TODO(jrick): this should be atomic on *nix, but is not on
|
|
|
|
// Windows. Use _windows.go to provide atomic renames.
|
|
|
|
if err = os.Rename(tmpfilepath, txfilepath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-15 22:55:28 +02:00
|
|
|
|
|
|
|
w.TxStore.dirty = false
|
2013-10-15 21:53:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// UTXOs
|
|
|
|
if w.UtxoStore.dirty {
|
2013-10-15 22:55:28 +02:00
|
|
|
w.UtxoStore.Lock()
|
|
|
|
defer w.UtxoStore.Unlock()
|
2013-10-15 21:53:49 +02:00
|
|
|
tmpfilepath := utxofilepath + "-" + timeStr
|
|
|
|
tmpfile, err := os.Create(tmpfilepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err = w.UtxoStore.s.WriteTo(tmpfile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-24 00:40:02 +02:00
|
|
|
tmpfile.Close()
|
2013-10-15 21:53:49 +02:00
|
|
|
|
|
|
|
// TODO(jrick): this should be atomic on *nix, but is not on
|
|
|
|
// Windows. Use _windows.go to provide atomic renames.
|
|
|
|
if err = os.Rename(tmpfilepath, utxofilepath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-15 22:55:28 +02:00
|
|
|
|
|
|
|
w.UtxoStore.dirty = false
|
2013-10-15 21:53:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|