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"
|
2014-01-15 16:55:09 +01:00
|
|
|
"io/ioutil"
|
2013-10-15 21:53:49 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-02-04 21:48:20 +01:00
|
|
|
"time"
|
2014-05-23 04:16:50 +02:00
|
|
|
|
|
|
|
"github.com/conformal/btcnet"
|
|
|
|
"github.com/conformal/btcwire"
|
2013-10-15 21:53:49 +02:00
|
|
|
)
|
|
|
|
|
2014-01-27 15:30:42 +01:00
|
|
|
// networkDir returns the directory name of a network directory to hold account
|
|
|
|
// files.
|
2014-05-23 04:16:50 +02:00
|
|
|
func networkDir(net *btcnet.Params) string {
|
|
|
|
netname := net.Name
|
|
|
|
|
|
|
|
// For now, we must always name the testnet data directory as "testnet"
|
|
|
|
// and not "testnet3" or any other version, as the btcnet testnet3
|
|
|
|
// paramaters will likely be switched to being named "testnet3" in the
|
|
|
|
// future. This is done to future proof that change, and an upgrade
|
|
|
|
// plan to move the testnet3 data directory can be worked out later.
|
|
|
|
if net.Net == btcwire.TestNet3 {
|
2013-12-05 02:55:56 +01:00
|
|
|
netname = "testnet"
|
|
|
|
}
|
2014-05-23 04:16:50 +02:00
|
|
|
|
2013-12-05 02:55:56 +01:00
|
|
|
return filepath.Join(cfg.DataDir, netname)
|
|
|
|
}
|
|
|
|
|
2014-01-27 15:30:42 +01:00
|
|
|
// tmpNetworkDir returns the temporary directory name for a given network.
|
2014-05-23 04:16:50 +02:00
|
|
|
func tmpNetworkDir(net *btcnet.Params) string {
|
2014-01-27 15:30:42 +01:00
|
|
|
return networkDir(net) + "_tmp"
|
|
|
|
}
|
|
|
|
|
2014-01-29 05:04:10 +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
|
|
|
|
}
|
2014-05-28 06:54:50 +02:00
|
|
|
defer func() {
|
|
|
|
if err := fd.Close(); err != nil {
|
|
|
|
log.Warnf("Cannot close directory: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2014-01-29 05:04:10 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
2014-02-24 20:35:30 +01:00
|
|
|
// filename suffix ("wallet.bin", or "tx.bin"), account name and the
|
|
|
|
// network directory holding the file.
|
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 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))
|
|
|
|
}
|
|
|
|
|
2014-01-29 05:04:10 +01:00
|
|
|
// syncSchedule references the account files which have been
|
|
|
|
// scheduled to be written and the directory to write to.
|
|
|
|
type syncSchedule struct {
|
|
|
|
dir string
|
|
|
|
wallets map[*Account]struct{}
|
|
|
|
txs map[*Account]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
|
|
|
}
|
|
|
|
|
2014-01-29 05:04:10 +01:00
|
|
|
func newSyncSchedule(dir string) *syncSchedule {
|
|
|
|
s := &syncSchedule{
|
|
|
|
dir: dir,
|
|
|
|
wallets: make(map[*Account]struct{}),
|
|
|
|
txs: make(map[*Account]struct{}),
|
2014-01-27 15:30:42 +01:00
|
|
|
}
|
2014-01-29 05:04:10 +01:00
|
|
|
return s
|
|
|
|
}
|
2014-01-27 15:30:42 +01:00
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
// flushAccount writes all scheduled account files to disk for
|
2014-01-29 05:04:10 +01:00
|
|
|
// a single account and removes them from the schedule.
|
2014-01-30 16:14:02 +01:00
|
|
|
func (s *syncSchedule) flushAccount(a *Account) error {
|
2014-01-29 05:04:10 +01:00
|
|
|
if _, ok := s.txs[a]; ok {
|
|
|
|
if err := a.writeTxStore(s.dir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
delete(s.txs, a)
|
2014-01-27 15:30:42 +01:00
|
|
|
}
|
2014-01-29 05:04:10 +01:00
|
|
|
if _, ok := s.wallets[a]; ok {
|
|
|
|
if err := a.writeWallet(s.dir); err != nil {
|
2014-01-27 15:30:42 +01:00
|
|
|
return err
|
|
|
|
}
|
2014-01-29 05:04:10 +01:00
|
|
|
delete(s.wallets, a)
|
2014-01-27 15:30:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
// flush writes all scheduled account files and removes each
|
2014-01-29 05:04:10 +01:00
|
|
|
// from the schedule.
|
2014-01-30 16:14:02 +01:00
|
|
|
func (s *syncSchedule) flush() error {
|
2014-01-29 05:04:10 +01:00
|
|
|
for a := range s.txs {
|
|
|
|
if err := a.writeTxStore(s.dir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
delete(s.txs, a)
|
2014-01-27 15:30:42 +01:00
|
|
|
}
|
|
|
|
|
2014-01-29 05:04:10 +01:00
|
|
|
for a := range s.wallets {
|
|
|
|
if err := a.writeWallet(s.dir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
delete(s.wallets, a)
|
2014-01-27 15:30:42 +01:00
|
|
|
}
|
|
|
|
|
2014-01-29 05:04:10 +01:00
|
|
|
return nil
|
|
|
|
}
|
2014-01-27 15:30:42 +01:00
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
type flushAccountRequest struct {
|
2014-01-29 05:04:10 +01:00
|
|
|
a *Account
|
|
|
|
err chan error
|
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
type writeBatchRequest struct {
|
2014-01-29 05:04:10 +01:00
|
|
|
a []*Account
|
|
|
|
err chan error
|
2014-01-27 15:30:42 +01:00
|
|
|
}
|
|
|
|
|
2014-01-29 05:04:10 +01:00
|
|
|
type exportRequest struct {
|
|
|
|
dir string
|
|
|
|
a *Account
|
|
|
|
err chan error
|
|
|
|
}
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
// DiskSyncer manages all disk write operations for a collection of accounts.
|
|
|
|
type DiskSyncer struct {
|
|
|
|
// Flush scheduled account writes.
|
2014-02-04 21:48:20 +01:00
|
|
|
flushAccount chan *flushAccountRequest
|
2014-01-30 16:14:02 +01:00
|
|
|
|
|
|
|
// Schedule file writes for an account.
|
2014-02-24 20:35:30 +01:00
|
|
|
scheduleWallet chan *Account
|
|
|
|
scheduleTxStore chan *Account
|
2014-01-30 16:14:02 +01:00
|
|
|
|
|
|
|
// Write a collection of accounts all at once.
|
|
|
|
writeBatch chan *writeBatchRequest
|
|
|
|
|
|
|
|
// Write an account export.
|
|
|
|
exportAccount chan *exportRequest
|
|
|
|
|
|
|
|
// Account manager for this DiskSyncer. This is only
|
|
|
|
// needed to grab the account manager semaphore.
|
|
|
|
am *AccountManager
|
2014-06-24 23:00:27 +02:00
|
|
|
|
|
|
|
quit chan struct{}
|
|
|
|
shutdown chan struct{}
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDiskSyncer creates a new DiskSyncer.
|
|
|
|
func NewDiskSyncer(am *AccountManager) *DiskSyncer {
|
|
|
|
return &DiskSyncer{
|
2014-02-24 20:35:30 +01:00
|
|
|
flushAccount: make(chan *flushAccountRequest),
|
|
|
|
scheduleWallet: make(chan *Account),
|
|
|
|
scheduleTxStore: make(chan *Account),
|
|
|
|
writeBatch: make(chan *writeBatchRequest),
|
|
|
|
exportAccount: make(chan *exportRequest),
|
|
|
|
am: am,
|
2014-06-24 23:00:27 +02:00
|
|
|
quit: make(chan struct{}),
|
|
|
|
shutdown: make(chan struct{}),
|
2014-01-30 16:14:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-25 05:59:24 +01:00
|
|
|
// Start starts the goroutines required to run the DiskSyncer.
|
|
|
|
func (ds *DiskSyncer) Start() {
|
|
|
|
go ds.handler()
|
|
|
|
}
|
|
|
|
|
2014-06-24 23:00:27 +02:00
|
|
|
func (ds *DiskSyncer) Stop() {
|
|
|
|
close(ds.quit)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *DiskSyncer) WaitForShutdown() {
|
|
|
|
<-ds.shutdown
|
|
|
|
}
|
|
|
|
|
2014-03-25 05:59:24 +01:00
|
|
|
// handler runs the disk syncer. It manages a set of "dirty" account files
|
2014-01-30 16:14:02 +01:00
|
|
|
// which must be written to disk, and synchronizes all writes in a single
|
|
|
|
// goroutine. Periodic flush operations may be signaled by an AccountManager.
|
2014-01-29 05:04:10 +01:00
|
|
|
//
|
2014-01-30 16:14:02 +01:00
|
|
|
// This never returns and is should be called from a new goroutine.
|
2014-03-25 05:59:24 +01:00
|
|
|
func (ds *DiskSyncer) handler() {
|
2014-05-23 04:16:50 +02:00
|
|
|
netdir := networkDir(activeNet.Params)
|
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 := checkCreateDir(netdir); err != nil {
|
2014-01-29 05:04:10 +01:00
|
|
|
log.Errorf("Unable to create or write to account directory: %v", err)
|
2013-11-21 15:24:16 +01:00
|
|
|
}
|
2014-05-23 04:16:50 +02:00
|
|
|
tmpnetdir := tmpNetworkDir(activeNet.Params)
|
2013-11-21 15:24:16 +01:00
|
|
|
|
2014-02-04 21:48:20 +01:00
|
|
|
const wait = 10 * time.Second
|
|
|
|
var timer <-chan time.Time
|
|
|
|
var sem chan struct{}
|
2014-01-29 05:04:10 +01:00
|
|
|
schedule := newSyncSchedule(netdir)
|
2014-06-24 23:00:27 +02:00
|
|
|
out:
|
2014-01-29 05:04:10 +01:00
|
|
|
for {
|
|
|
|
select {
|
2014-02-04 21:48:20 +01:00
|
|
|
case <-sem: // Now have exclusive access of the account manager
|
2014-02-04 00:15:36 +01:00
|
|
|
err := schedule.flush()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Cannot write accounts: %v", err)
|
|
|
|
}
|
2014-01-30 16:14:02 +01:00
|
|
|
|
2014-02-04 21:48:20 +01:00
|
|
|
timer = nil
|
|
|
|
|
|
|
|
// Do not grab semaphore again until another flush is needed.
|
|
|
|
sem = nil
|
|
|
|
|
|
|
|
// Release semaphore.
|
|
|
|
ds.am.bsem <- struct{}{}
|
|
|
|
|
|
|
|
case <-timer:
|
|
|
|
// Grab AccountManager semaphore when ready so flush can occur.
|
|
|
|
sem = ds.am.bsem
|
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
case fr := <-ds.flushAccount:
|
|
|
|
fr.err <- schedule.flushAccount(fr.a)
|
|
|
|
|
|
|
|
case a := <-ds.scheduleWallet:
|
2014-01-29 05:04:10 +01:00
|
|
|
schedule.wallets[a] = struct{}{}
|
2014-02-04 21:48:20 +01:00
|
|
|
if timer == nil {
|
|
|
|
timer = time.After(wait)
|
|
|
|
}
|
2013-10-15 21:53:49 +02:00
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
case a := <-ds.scheduleTxStore:
|
2014-01-29 05:04:10 +01:00
|
|
|
schedule.txs[a] = struct{}{}
|
2014-02-04 21:48:20 +01:00
|
|
|
if timer == nil {
|
|
|
|
timer = time.After(wait)
|
|
|
|
}
|
2013-11-12 20:53:38 +01:00
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
case sr := <-ds.writeBatch:
|
2014-01-29 05:04:10 +01:00
|
|
|
err := batchWriteAccounts(sr.a, tmpnetdir, netdir)
|
|
|
|
if err == nil {
|
|
|
|
// All accounts have been synced, old schedule
|
|
|
|
// can be discarded.
|
|
|
|
schedule = newSyncSchedule(netdir)
|
2014-02-04 21:48:20 +01:00
|
|
|
timer = nil
|
2014-01-29 05:04:10 +01:00
|
|
|
}
|
|
|
|
sr.err <- err
|
2013-10-15 22:55:28 +02:00
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
case er := <-ds.exportAccount:
|
2014-01-29 05:04:10 +01:00
|
|
|
a := er.a
|
|
|
|
dir := er.dir
|
|
|
|
er.err <- a.writeAll(dir)
|
2014-06-24 23:00:27 +02:00
|
|
|
|
|
|
|
case <-ds.quit:
|
|
|
|
err := schedule.flush()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Cannot write accounts: %v", err)
|
|
|
|
}
|
|
|
|
break out
|
2013-10-15 21:53:49 +02:00
|
|
|
}
|
2014-01-29 05:04:10 +01:00
|
|
|
}
|
2014-06-24 23:00:27 +02:00
|
|
|
close(ds.shutdown)
|
2014-01-29 05:04:10 +01:00
|
|
|
}
|
2014-01-15 16:55:09 +01:00
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
// FlushAccount writes all scheduled account files to disk for a single
|
|
|
|
// account.
|
|
|
|
func (ds *DiskSyncer) FlushAccount(a *Account) error {
|
|
|
|
err := make(chan error)
|
|
|
|
ds.flushAccount <- &flushAccountRequest{a: a, err: err}
|
|
|
|
return <-err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScheduleWalletWrite schedules an account's wallet to be written to disk.
|
|
|
|
func (ds *DiskSyncer) ScheduleWalletWrite(a *Account) {
|
|
|
|
ds.scheduleWallet <- a
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScheduleTxStoreWrite schedules an account's transaction store to be
|
|
|
|
// written to disk.
|
|
|
|
func (ds *DiskSyncer) ScheduleTxStoreWrite(a *Account) {
|
|
|
|
ds.scheduleTxStore <- a
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteBatch safely replaces all account files in the network directory
|
|
|
|
// with new files created from all accounts in a.
|
|
|
|
func (ds *DiskSyncer) WriteBatch(a []*Account) error {
|
|
|
|
err := make(chan error)
|
|
|
|
ds.writeBatch <- &writeBatchRequest{
|
|
|
|
a: a,
|
|
|
|
err: err,
|
2014-01-29 05:04:10 +01:00
|
|
|
}
|
2014-01-30 16:14:02 +01:00
|
|
|
return <-err
|
|
|
|
}
|
2013-10-15 21:53:49 +02:00
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
// ExportAccount writes all account files for a to a new directory.
|
|
|
|
func (ds *DiskSyncer) ExportAccount(a *Account, dir string) error {
|
|
|
|
err := make(chan error)
|
|
|
|
er := &exportRequest{
|
|
|
|
dir: dir,
|
|
|
|
a: a,
|
2014-01-29 05:04:10 +01:00
|
|
|
err: err,
|
|
|
|
}
|
2014-01-30 16:14:02 +01:00
|
|
|
ds.exportAccount <- er
|
2014-01-29 05:04:10 +01:00
|
|
|
return <-err
|
|
|
|
}
|
|
|
|
|
|
|
|
func batchWriteAccounts(accts []*Account, tmpdir, netdir string) error {
|
|
|
|
if err := freshDir(tmpdir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, a := range accts {
|
|
|
|
if err := a.writeAll(tmpdir); err != nil {
|
2013-10-15 21:53:49 +02:00
|
|
|
return err
|
|
|
|
}
|
2014-01-29 05:04:10 +01:00
|
|
|
}
|
|
|
|
// This is technically NOT an atomic operation, but at startup, if the
|
|
|
|
// network directory is missing but the temporary network directory
|
|
|
|
// exists, the temporary is moved before accounts are opened.
|
|
|
|
if err := os.RemoveAll(netdir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := Rename(tmpdir, netdir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2013-10-15 22:55:28 +02:00
|
|
|
|
2014-01-29 05:04:10 +01:00
|
|
|
func (a *Account) writeAll(dir string) error {
|
|
|
|
if err := a.writeTxStore(dir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := a.writeWallet(dir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2014-01-29 05:04:10 +01:00
|
|
|
func (a *Account) writeWallet(dir string) error {
|
|
|
|
wfilepath := accountFilename("wallet.bin", a.name, dir)
|
|
|
|
_, filename := filepath.Split(wfilepath)
|
|
|
|
tmpfile, err := ioutil.TempFile(dir, filename)
|
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
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-29 05:04:10 +01:00
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
if _, err = a.Wallet.WriteTo(tmpfile); err != 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
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-02-05 17:21:35 +01:00
|
|
|
tmppath := tmpfile.Name()
|
2014-05-28 06:54:50 +02:00
|
|
|
if err := tmpfile.Close(); err != nil {
|
|
|
|
log.Warnf("Cannot close temporary wallet file: %v", err)
|
2014-01-29 05:04:10 +01:00
|
|
|
}
|
|
|
|
|
2014-05-28 06:54:50 +02:00
|
|
|
return Rename(tmppath, wfilepath)
|
2014-01-29 05:04:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Account) writeTxStore(dir string) error {
|
|
|
|
txfilepath := accountFilename("tx.bin", a.name, dir)
|
|
|
|
_, filename := filepath.Split(txfilepath)
|
|
|
|
tmpfile, err := ioutil.TempFile(dir, filename)
|
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
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-01-29 05:04:10 +01:00
|
|
|
|
2014-01-30 16:14:02 +01:00
|
|
|
if _, err = a.TxStore.WriteTo(tmpfile); err != 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
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-02-05 17:21:35 +01:00
|
|
|
tmppath := tmpfile.Name()
|
2014-05-28 06:54:50 +02:00
|
|
|
if err := tmpfile.Close(); err != nil {
|
|
|
|
log.Warnf("Cannot close temporary txstore file: %v", err)
|
2014-01-29 05:04:10 +01:00
|
|
|
}
|
|
|
|
|
2014-05-28 06:54:50 +02:00
|
|
|
return Rename(tmppath, txfilepath)
|
2014-01-29 05:04:10 +01:00
|
|
|
}
|