lbcwallet/disksync.go
Josh Rickmar ce23523ed7 Introduce new account file structure.
This changes the locations that account files (wallet.bin, utxo.bin,
and tx.bin) are searched for when opening or disk syncing accounts.
Previously, files were saved in the following layout:

  ~/.btcwallet/
    - btcwallet/
      - wallet.bin
      - tx.bin
      - utxo.bin
    - btcwallet-AccountA/
      - wallet.bin
      - tx.bin
      - utxo.bin

This format had two issues.  First, each account would require its own
directory, causing a scalability issue on unix (and perhaps other)
platforms.  Second, there was no distinction between testnet and
mainnet wallets, and if mainnet support was enabled, btcwallet would
attempt to open accounts with testnet wallets.

Instead, the following file structure is now used:

  ~/.btcwallet/
    - testnet/
      - wallet.bin
      - tx.bin
      - utxo.bin
      - AccountA-wallet.bin
      - AccountA-tx.bin
      - AccountA-utxo.bin

This solves both of the previously-mentioned issues by requiring only
two subdirectories (one each for the testnet and mainnet bitcoin
networks), and by separating the locations to open and save testnet
and mainnet account files.

At startup, a check for the old account file structure is performed.
If found, files are moved to the new locations, and the old account
directories are removed.  Account files are moved to the testnet
directory, as only testnet support is currently enabled.

The version has been bumped to 0.1.1 to reflect this change.

Fixes #16.
2013-12-04 20:25:13 -05:00

179 lines
4.8 KiB
Go

/*
* 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"
"sync"
"time"
)
var (
// dirtyAccounts holds a set of accounts that include dirty components.
dirtyAccounts = struct {
sync.Mutex
m map[*Account]bool
}{
m: make(map[*Account]bool),
}
)
// accountFilename returns the filepath of an account file given the
// filename suffix ("wallet.bin", "tx.bin", or "utxo.bin"), account
// name and the network directory holding the file.
func accountFilename(suffix, account, netdir string) string {
if account == "" {
// default account
return filepath.Join(netdir, suffix)
}
// non-default account
return filepath.Join(netdir, fmt.Sprintf("%v-%v", account, suffix))
}
// 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
// seconds by this function.
//
// This never returns and is meant to be called from a goroutine.
func DirtyAccountSyncer() {
ticker := time.Tick(10 * time.Second)
for {
select {
case <-ticker:
dirtyAccounts.Lock()
for a := range dirtyAccounts.m {
log.Debugf("Syncing account '%v' to disk",
a.Wallet.Name())
if err := a.writeDirtyToDisk(); err != nil {
log.Errorf("cannot sync dirty wallet: %v",
err)
} else {
delete(dirtyAccounts.m, a)
}
}
dirtyAccounts.Unlock()
}
}
}
// writeDirtyToDisk checks for the dirty flag on an account's wallet,
// txstore, and utxostore, writing them to disk if any are dirty.
func (a *Account) 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())
netdir := networkDir(cfg.Net())
if err := checkCreateDir(netdir); err != nil {
return err
}
wfilepath := accountFilename("wallet.bin", a.name, netdir)
txfilepath := accountFilename("tx.bin", a.name, netdir)
utxofilepath := accountFilename("utxo.bin", a.name, netdir)
// 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
a.UtxoStore.RLock()
dirty := a.TxStore.dirty
a.UtxoStore.RUnlock()
if dirty {
a.UtxoStore.Lock()
defer a.UtxoStore.Unlock()
tmpfilepath := utxofilepath + "-" + timeStr
tmpfile, err := os.Create(tmpfilepath)
if err != nil {
return err
}
if _, err = a.UtxoStore.s.WriteTo(tmpfile); err != nil {
return err
}
tmpfile.Close()
// 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
}
a.UtxoStore.dirty = false
}
// Transactions
a.TxStore.RLock()
dirty = a.TxStore.dirty
a.TxStore.RUnlock()
if dirty {
a.TxStore.Lock()
defer a.TxStore.Unlock()
tmpfilepath := txfilepath + "-" + timeStr
tmpfile, err := os.Create(tmpfilepath)
if err != nil {
return err
}
if _, err = a.TxStore.s.WriteTo(tmpfile); err != nil {
return err
}
tmpfile.Close()
// 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
}
a.TxStore.dirty = false
}
// Wallet
a.mtx.RLock()
dirty = a.dirty
a.mtx.RUnlock()
if dirty {
a.mtx.Lock()
defer a.mtx.Unlock()
tmpfilepath := wfilepath + "-" + timeStr
tmpfile, err := os.Create(tmpfilepath)
if err != nil {
return err
}
if _, err = a.WriteTo(tmpfile); err != nil {
return err
}
tmpfile.Close()
// 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
}
a.dirty = false
}
return nil
}