2013-10-15 21:53:49 +02:00
|
|
|
/*
|
2014-01-09 20:12:20 +01:00
|
|
|
* Copyright (c) 2013, 2014 Conformal Systems LLC <info@conformal.com>
|
2013-10-15 21:53:49 +02:00
|
|
|
*
|
|
|
|
* 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"
|
2013-12-05 02:55:56 +01:00
|
|
|
"github.com/conformal/btcwire"
|
2014-01-15 16:55:09 +01:00
|
|
|
"io/ioutil"
|
2013-10-15 21:53:49 +02:00
|
|
|
"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
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2014-01-27 15:30:42 +01:00
|
|
|
// networkDir returns the directory name of a network directory to hold account
|
|
|
|
// files.
|
2013-12-05 02:55:56 +01:00
|
|
|
func networkDir(net btcwire.BitcoinNet) string {
|
|
|
|
var netname string
|
|
|
|
if net == btcwire.MainNet {
|
|
|
|
netname = "mainnet"
|
|
|
|
} else {
|
|
|
|
netname = "testnet"
|
|
|
|
}
|
|
|
|
return filepath.Join(cfg.DataDir, netname)
|
|
|
|
}
|
|
|
|
|
2014-01-27 15:30:42 +01:00
|
|
|
// tmpNetworkDir returns the temporary directory name for a given network.
|
|
|
|
func tmpNetworkDir(net btcwire.BitcoinNet) string {
|
|
|
|
return networkDir(net) + "_tmp"
|
|
|
|
}
|
|
|
|
|
2013-12-05 02:55:56 +01:00
|
|
|
// checkCreateDir checks that the path exists and is a directory.
|
|
|
|
// If path does not exist, it is created.
|
|
|
|
func checkCreateDir(path string) error {
|
|
|
|
if fi, err := os.Stat(path); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// Attempt data directory creation
|
|
|
|
if err = os.MkdirAll(path, 0700); err != nil {
|
|
|
|
return fmt.Errorf("cannot create directory: %s", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("error checking directory: %s", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return fmt.Errorf("path '%s' is not a directory", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-05 02:16:50 +01:00
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-27 15:30:42 +01:00
|
|
|
// freshDir creates a new directory specified by path if it does not
|
|
|
|
// exist. If the directory already exists, all files contained in the
|
|
|
|
// directory are removed.
|
|
|
|
func freshDir(path string) error {
|
|
|
|
if err := checkCreateDir(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all files in the directory.
|
|
|
|
fd, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
names, err := fd.Readdirnames(0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, name := range names {
|
|
|
|
if err := os.RemoveAll(name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeAllToFreshDir writes all account files to the specified directory.
|
|
|
|
// If dir already exists, any old files are removed. If dir does not
|
|
|
|
// exist, it is created.
|
|
|
|
//
|
|
|
|
// It is a runtime error to call this function while not holding each
|
|
|
|
// wallet, tx store, and utxo store writer lock.
|
|
|
|
func (a *Account) writeAllToFreshDir(dir string) error {
|
|
|
|
if err := freshDir(dir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
wfilepath := accountFilename("wallet.bin", a.name, dir)
|
|
|
|
txfilepath := accountFilename("tx.bin", a.name, dir)
|
|
|
|
utxofilepath := accountFilename("utxo.bin", a.name, dir)
|
|
|
|
|
|
|
|
wfile, err := os.Create(wfilepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer wfile.Close()
|
|
|
|
txfile, err := os.Create(txfilepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer txfile.Close()
|
|
|
|
utxofile, err := os.Create(utxofilepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer utxofile.Close()
|
|
|
|
|
|
|
|
if _, err := a.Wallet.WriteTo(wfile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
a.dirty = false
|
|
|
|
|
|
|
|
if _, err := a.TxStore.s.WriteTo(txfile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
a.TxStore.dirty = false
|
|
|
|
|
|
|
|
if _, err := a.UtxoStore.s.WriteTo(utxofile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
a.UtxoStore.dirty = false
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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.
|
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-05 02:16:50 +01:00
|
|
|
func (a *Account) writeDirtyToDisk() error {
|
|
|
|
netdir := networkDir(cfg.Net())
|
|
|
|
if err := checkCreateDir(netdir); err != nil {
|
2013-11-21 15:24:16 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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-05 02:16:50 +01:00
|
|
|
wfilepath := accountFilename("wallet.bin", a.name, netdir)
|
|
|
|
txfilepath := accountFilename("tx.bin", a.name, netdir)
|
|
|
|
utxofilepath := accountFilename("utxo.bin", a.name, netdir)
|
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
|
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-05 02:16:50 +01:00
|
|
|
a.UtxoStore.RLock()
|
|
|
|
dirty := a.TxStore.dirty
|
|
|
|
a.UtxoStore.RUnlock()
|
2013-11-21 19:45:14 +01:00
|
|
|
if dirty {
|
2014-01-15 16:55:09 +01:00
|
|
|
netdir, filename := filepath.Split(utxofilepath)
|
|
|
|
tmpfile, err := ioutil.TempFile(netdir, filename)
|
2013-10-15 21:53:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-15 16:55:09 +01:00
|
|
|
|
|
|
|
a.UtxoStore.Lock()
|
|
|
|
defer a.UtxoStore.Unlock()
|
|
|
|
|
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-05 02:16:50 +01:00
|
|
|
if _, err = a.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
|
|
|
|
2014-01-15 16:55:09 +01:00
|
|
|
if err = Rename(tmpfile.Name(), utxofilepath); err != nil {
|
2013-10-15 21:53:49 +02:00
|
|
|
return err
|
|
|
|
}
|
2013-10-15 22:55:28 +02:00
|
|
|
|
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-05 02:16:50 +01:00
|
|
|
a.UtxoStore.dirty = false
|
2013-10-15 21:53:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transactions
|
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-05 02:16:50 +01:00
|
|
|
a.TxStore.RLock()
|
|
|
|
dirty = a.TxStore.dirty
|
|
|
|
a.TxStore.RUnlock()
|
2013-11-21 19:45:14 +01:00
|
|
|
if dirty {
|
2014-01-15 16:55:09 +01:00
|
|
|
netdir, filename := filepath.Split(txfilepath)
|
|
|
|
tmpfile, err := ioutil.TempFile(netdir, filename)
|
2013-10-15 21:53:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-15 16:55:09 +01:00
|
|
|
|
|
|
|
a.TxStore.Lock()
|
|
|
|
defer a.TxStore.Unlock()
|
|
|
|
|
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-05 02:16:50 +01:00
|
|
|
if _, err = a.TxStore.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
|
|
|
|
2014-01-15 16:55:09 +01:00
|
|
|
if err = Rename(tmpfile.Name(), txfilepath); err != nil {
|
2013-10-15 21:53:49 +02:00
|
|
|
return err
|
|
|
|
}
|
2013-10-15 22:55:28 +02:00
|
|
|
|
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-05 02:16:50 +01:00
|
|
|
a.TxStore.dirty = false
|
2013-10-15 21:53:49 +02:00
|
|
|
}
|
|
|
|
|
2013-11-12 20:53:38 +01:00
|
|
|
// Wallet
|
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-05 02:16:50 +01:00
|
|
|
a.mtx.RLock()
|
|
|
|
dirty = a.dirty
|
|
|
|
a.mtx.RUnlock()
|
2013-11-21 19:45:14 +01:00
|
|
|
if dirty {
|
2014-01-15 16:55:09 +01:00
|
|
|
netdir, filename := filepath.Split(wfilepath)
|
|
|
|
tmpfile, err := ioutil.TempFile(netdir, filename)
|
2013-10-15 21:53:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-15 16:55:09 +01:00
|
|
|
|
|
|
|
a.mtx.Lock()
|
|
|
|
defer a.mtx.Unlock()
|
|
|
|
|
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-05 02:16:50 +01:00
|
|
|
if _, err = a.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
|
|
|
|
2014-01-15 16:55:09 +01:00
|
|
|
if err = Rename(tmpfile.Name(), wfilepath); err != nil {
|
2013-10-15 21:53:49 +02:00
|
|
|
return err
|
|
|
|
}
|
2013-10-15 22:55:28 +02:00
|
|
|
|
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-05 02:16:50 +01:00
|
|
|
a.dirty = false
|
2013-10-15 21:53:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
Implement exporting a watching-only wallet.
This change allows for the use of watching-only wallets. Unlike
normal, "hot" wallets, watching-only wallets do not contain any
private keys, and can be used in situations where you want to keep one
wallet online to create new receiving addresses and watch for received
transactions, while keeping the hot wallet offline (possibly on an
air-gapped computer).
Two (websocket) extension RPC calls have been added:
First, exportwatchingwallet, which will export the current hot wallet
to a watching-only wallet, saving either to disk or returning the
base64-encoded wallet files to the caller.
Second, recoveraddresses, which is used to recover the next n
addresses from the address chain. This is used to "sync" a watching
wallet with the hot wallet, or vice versa.
2014-01-21 20:45:28 +01:00
|
|
|
|
|
|
|
// WriteExport writes an account to a special export directory named
|
|
|
|
// by dirName. Any previous files are overwritten.
|
|
|
|
func (a *Account) WriteExport(dirName string) error {
|
|
|
|
exportPath := filepath.Join(networkDir(cfg.Net()), dirName)
|
|
|
|
if err := checkCreateDir(exportPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
aname := a.Name()
|
|
|
|
wfilepath := accountFilename("wallet.bin", aname, exportPath)
|
|
|
|
txfilepath := accountFilename("tx.bin", aname, exportPath)
|
|
|
|
utxofilepath := accountFilename("utxo.bin", aname, exportPath)
|
|
|
|
|
|
|
|
a.UtxoStore.RLock()
|
|
|
|
defer a.UtxoStore.RUnlock()
|
|
|
|
utxofile, err := os.Create(utxofilepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer utxofile.Close()
|
|
|
|
if _, err := a.UtxoStore.s.WriteTo(utxofile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
a.TxStore.RLock()
|
|
|
|
defer a.TxStore.RUnlock()
|
|
|
|
txfile, err := os.Create(txfilepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer txfile.Close()
|
|
|
|
if _, err := a.TxStore.s.WriteTo(txfile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
a.mtx.RLock()
|
|
|
|
defer a.mtx.RUnlock()
|
|
|
|
wfile, err := os.Create(wfilepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer wfile.Close()
|
|
|
|
if _, err := a.Wallet.WriteTo(wfile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|