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 (
|
2013-11-15 17:44:24 +01:00
|
|
|
// dirtyAccounts holds a set of accounts that include dirty components.
|
|
|
|
dirtyAccounts = struct {
|
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.Mutex
|
2013-11-14 18:15:16 +01:00
|
|
|
m map[*Account]bool
|
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
|
|
|
}{
|
2013-11-14 18:15:16 +01:00
|
|
|
m: make(map[*Account]bool),
|
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
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2013-11-15 17:44:24 +01:00
|
|
|
// DirtyAccountSyncer synces dirty accounts for cases where the updated
|
|
|
|
// information was not required to be immediately written to disk. Accounts
|
|
|
|
// may be added to dirtyAccounts and will be checked and processed every 10
|
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
|
|
|
// seconds by this function.
|
|
|
|
//
|
|
|
|
// This never returns and is meant to be called from a goroutine.
|
2013-11-15 17:44:24 +01:00
|
|
|
func DirtyAccountSyncer() {
|
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
|
|
|
ticker := time.Tick(10 * time.Second)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker:
|
2013-11-15 17:44:24 +01:00
|
|
|
dirtyAccounts.Lock()
|
|
|
|
for a := range dirtyAccounts.m {
|
|
|
|
log.Debugf("Syncing account '%v' to disk",
|
|
|
|
a.Wallet.Name())
|
|
|
|
if err := a.writeDirtyToDisk(); err != nil {
|
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
|
|
|
log.Errorf("cannot sync dirty wallet: %v",
|
|
|
|
err)
|
|
|
|
} else {
|
2013-11-15 17:44:24 +01:00
|
|
|
delete(dirtyAccounts.m, a)
|
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
|
|
|
}
|
|
|
|
}
|
2013-11-15 17:44:24 +01:00
|
|
|
dirtyAccounts.Unlock()
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2013-11-14 18:15:16 +01:00
|
|
|
func (w *Account) writeDirtyToDisk() error {
|
2013-10-15 21:53:49 +02:00
|
|
|
// 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())
|
|
|
|
|
2013-12-02 20:56:06 +01:00
|
|
|
adir := w.accountdir(cfg)
|
|
|
|
if err := w.checkCreateAccountDir(adir); err != nil {
|
2013-11-21 15:24:16 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
wfilepath := filepath.Join(adir, "wallet.bin")
|
|
|
|
txfilepath := filepath.Join(adir, "tx.bin")
|
|
|
|
utxofilepath := filepath.Join(adir, "utxo.bin")
|
2013-10-15 21:53:49 +02:00
|
|
|
|
2013-11-12 20:53:38 +01:00
|
|
|
// UTXOs and transactions are synced to disk first. This prevents
|
|
|
|
// any races from saving a wallet marked to be synced with block N
|
|
|
|
// and btcwallet closing while the UTXO and Tx files are only synced
|
|
|
|
// with block N-1.
|
|
|
|
|
|
|
|
// UTXOs
|
2013-11-21 19:45:14 +01:00
|
|
|
w.UtxoStore.RLock()
|
|
|
|
dirty := w.TxStore.dirty
|
|
|
|
w.UtxoStore.RUnlock()
|
|
|
|
if dirty {
|
2013-11-12 20:53:38 +01:00
|
|
|
w.UtxoStore.Lock()
|
|
|
|
defer w.UtxoStore.Unlock()
|
|
|
|
tmpfilepath := utxofilepath + "-" + timeStr
|
2013-10-15 21:53:49 +02:00
|
|
|
tmpfile, err := os.Create(tmpfilepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-11-12 20:53:38 +01:00
|
|
|
if _, err = w.UtxoStore.s.WriteTo(tmpfile); err != nil {
|
2013-10-15 21:53:49 +02:00
|
|
|
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.
|
2013-11-12 20:53:38 +01:00
|
|
|
if err = os.Rename(tmpfilepath, utxofilepath); err != nil {
|
2013-10-15 21:53:49 +02:00
|
|
|
return err
|
|
|
|
}
|
2013-10-15 22:55:28 +02:00
|
|
|
|
2013-11-12 20:53:38 +01:00
|
|
|
w.UtxoStore.dirty = false
|
2013-10-15 21:53:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transactions
|
2013-11-21 19:45:14 +01:00
|
|
|
w.TxStore.RLock()
|
|
|
|
dirty = w.TxStore.dirty
|
|
|
|
w.TxStore.RUnlock()
|
|
|
|
if 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
|
|
|
}
|
|
|
|
|
2013-11-12 20:53:38 +01:00
|
|
|
// Wallet
|
2013-11-21 19:45:14 +01:00
|
|
|
w.mtx.RLock()
|
|
|
|
dirty = w.dirty
|
|
|
|
w.mtx.RUnlock()
|
|
|
|
if dirty {
|
2013-11-12 20:53:38 +01:00
|
|
|
w.mtx.Lock()
|
|
|
|
defer w.mtx.Unlock()
|
|
|
|
tmpfilepath := wfilepath + "-" + timeStr
|
2013-10-15 21:53:49 +02:00
|
|
|
tmpfile, err := os.Create(tmpfilepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-11-12 20:53:38 +01:00
|
|
|
if _, err = w.WriteTo(tmpfile); err != nil {
|
2013-10-15 21:53:49 +02:00
|
|
|
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.
|
2013-11-12 20:53:38 +01:00
|
|
|
if err = os.Rename(tmpfilepath, wfilepath); err != nil {
|
2013-10-15 21:53:49 +02:00
|
|
|
return err
|
|
|
|
}
|
2013-10-15 22:55:28 +02:00
|
|
|
|
2013-11-12 20:53:38 +01:00
|
|
|
w.dirty = false
|
2013-10-15 21:53:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|