2013-08-21 16:37:30 +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 (
|
2013-09-03 16:21:14 +02:00
|
|
|
"errors"
|
2013-08-21 16:37:30 +02:00
|
|
|
"fmt"
|
2013-09-04 01:05:59 +02:00
|
|
|
"github.com/conformal/btcjson"
|
2013-09-04 19:41:33 +02:00
|
|
|
"github.com/conformal/btcutil"
|
2013-09-03 06:10:32 +02:00
|
|
|
"github.com/conformal/btcwallet/tx"
|
2013-08-21 16:37:30 +02:00
|
|
|
"github.com/conformal/btcwallet/wallet"
|
2013-09-04 15:54:06 +02:00
|
|
|
"github.com/conformal/btcwire"
|
2013-11-06 17:23:30 +01:00
|
|
|
"github.com/conformal/btcws"
|
2013-08-21 16:37:30 +02:00
|
|
|
"os"
|
2013-09-03 06:10:32 +02:00
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
2013-08-21 16:37:30 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-09-03 16:21:14 +02:00
|
|
|
var (
|
2013-09-09 20:14:57 +02:00
|
|
|
// ErrNoWallet describes an error where a wallet does not exist and
|
|
|
|
// must be created first.
|
|
|
|
ErrNoWallet = errors.New("wallet file does not exist")
|
|
|
|
|
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
|
|
|
// ErrNoUtxos describes an error where the wallet file was successfully
|
|
|
|
// read, but the UTXO file was not. To properly handle this error,
|
|
|
|
// a rescan should be done since the wallet creation block.
|
|
|
|
ErrNoUtxos = errors.New("utxo file cannot be read")
|
|
|
|
|
|
|
|
// ErrNoTxs describes an error where the wallet and UTXO files were
|
|
|
|
// successfully read, but the TX history file was not. It is up to
|
|
|
|
// the caller whether this necessitates a rescan or not.
|
|
|
|
ErrNoTxs = errors.New("tx file cannot be read")
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
cfg *config
|
2013-09-03 16:21:14 +02:00
|
|
|
|
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
|
|
|
curBlock = struct {
|
2013-09-05 21:31:39 +02:00
|
|
|
sync.RWMutex
|
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
|
|
|
wallet.BlockStamp
|
2013-09-05 21:31:39 +02:00
|
|
|
}{
|
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
|
|
|
BlockStamp: wallet.BlockStamp{
|
|
|
|
Height: int32(btcutil.BlockHeightUnknown),
|
|
|
|
},
|
2013-09-05 21:31:39 +02:00
|
|
|
}
|
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
|
|
|
wallets = NewAccountStore()
|
2013-08-21 16:37:30 +02:00
|
|
|
)
|
|
|
|
|
2013-09-03 15:49:16 +02:00
|
|
|
// walletdir returns the directory path which holds the wallet, utxo,
|
|
|
|
// and tx files.
|
|
|
|
func walletdir(cfg *config, account string) string {
|
2013-09-03 06:10:32 +02:00
|
|
|
var wname string
|
|
|
|
if account == "" {
|
|
|
|
wname = "btcwallet"
|
|
|
|
} else {
|
|
|
|
wname = fmt.Sprintf("btcwallet-%s", account)
|
|
|
|
}
|
|
|
|
|
2013-09-03 15:49:16 +02:00
|
|
|
return filepath.Join(cfg.DataDir, wname)
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// OpenWallet opens a wallet described by account in the data
|
|
|
|
// directory specified by cfg. If the wallet does not exist, ErrNoWallet
|
|
|
|
// is returned as an error.
|
2013-10-17 17:16:45 +02:00
|
|
|
//
|
|
|
|
// Wallets opened from this function are not set to track against a
|
|
|
|
// btcd connection.
|
2013-11-14 18:15:16 +01:00
|
|
|
func OpenWallet(cfg *config, account string) (*Account, error) {
|
2013-09-03 15:49:16 +02:00
|
|
|
wdir := walletdir(cfg, account)
|
2013-09-03 06:10:32 +02:00
|
|
|
fi, err := os.Stat(wdir)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// Attempt data directory creation
|
|
|
|
if err = os.MkdirAll(wdir, 0700); err != nil {
|
2013-09-09 20:14:57 +02:00
|
|
|
return nil, fmt.Errorf("cannot create data directory: %s", err)
|
2013-09-03 06:10:32 +02:00
|
|
|
}
|
|
|
|
} else {
|
2013-09-09 20:14:57 +02:00
|
|
|
return nil, fmt.Errorf("error checking data directory: %s", err)
|
2013-09-03 06:10:32 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !fi.IsDir() {
|
2013-09-09 20:14:57 +02:00
|
|
|
return nil, fmt.Errorf("data directory '%s' is not a directory", cfg.DataDir)
|
2013-09-03 06:10:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wfilepath := filepath.Join(wdir, "wallet.bin")
|
|
|
|
utxofilepath := filepath.Join(wdir, "utxo.bin")
|
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
|
|
|
txfilepath := filepath.Join(wdir, "tx.bin")
|
|
|
|
var wfile, utxofile, txfile *os.File
|
|
|
|
|
|
|
|
// Read wallet file.
|
2013-09-03 06:10:32 +02:00
|
|
|
if wfile, err = os.Open(wfilepath); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2013-09-03 16:21:14 +02:00
|
|
|
// Must create and save wallet first.
|
|
|
|
return nil, ErrNoWallet
|
2013-09-03 06:10:32 +02:00
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
return nil, fmt.Errorf("cannot open wallet file: %s", err)
|
2013-09-03 06:10:32 +02:00
|
|
|
}
|
2013-09-03 23:05:22 +02:00
|
|
|
defer wfile.Close()
|
2013-09-03 06:10:32 +02:00
|
|
|
|
|
|
|
wlt := new(wallet.Wallet)
|
|
|
|
if _, err = wlt.ReadFrom(wfile); err != nil {
|
2013-09-09 20:14:57 +02:00
|
|
|
return nil, fmt.Errorf("cannot read wallet: %s", err)
|
2013-09-03 06:10:32 +02:00
|
|
|
}
|
|
|
|
|
2013-11-14 18:15:16 +01:00
|
|
|
w := &Account{
|
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
|
|
|
Wallet: wlt,
|
|
|
|
name: account,
|
2013-09-03 06:10:32 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
// Read utxo file. If this fails, return a ErrNoUtxos error so a
|
|
|
|
// rescan can be done since the wallet creation block.
|
2013-09-03 06:10:32 +02:00
|
|
|
var utxos tx.UtxoStore
|
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
|
|
|
if utxofile, err = os.Open(utxofilepath); err != nil {
|
|
|
|
log.Errorf("cannot open utxo file: %s", err)
|
|
|
|
return w, ErrNoUtxos
|
|
|
|
}
|
|
|
|
defer utxofile.Close()
|
2013-09-03 06:10:32 +02:00
|
|
|
if _, err = utxos.ReadFrom(utxofile); 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 read utxo file: %s", err)
|
|
|
|
return w, ErrNoUtxos
|
2013-09-03 06:10:32 +02:00
|
|
|
}
|
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
|
|
|
w.UtxoStore.s = utxos
|
2013-09-03 06:10:32 +02:00
|
|
|
|
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
|
|
|
// Read tx file. If this fails, return a ErrNoTxs error and let
|
|
|
|
// the caller decide if a rescan is necessary.
|
|
|
|
if txfile, err = os.Open(txfilepath); err != nil {
|
|
|
|
log.Errorf("cannot open tx file: %s", err)
|
|
|
|
return w, ErrNoTxs
|
|
|
|
}
|
|
|
|
defer txfile.Close()
|
|
|
|
var txs tx.TxStore
|
|
|
|
if _, err = txs.ReadFrom(txfile); err != nil {
|
|
|
|
log.Errorf("cannot read tx file: %s", err)
|
|
|
|
return w, ErrNoTxs
|
2013-09-03 06:10:32 +02:00
|
|
|
}
|
2013-09-04 01:05:59 +02:00
|
|
|
w.TxStore.s = txs
|
2013-09-03 06:10:32 +02:00
|
|
|
|
2013-09-03 23:16:07 +02:00
|
|
|
return w, 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
|
|
|
// GetCurBlock returns the blockchain height and SHA hash of the most
|
|
|
|
// recently seen block. If no blocks have been seen since btcd has
|
|
|
|
// connected, btcd is queried for the current block height and hash.
|
|
|
|
func GetCurBlock() (bs wallet.BlockStamp, err error) {
|
|
|
|
curBlock.RLock()
|
|
|
|
bs = curBlock.BlockStamp
|
|
|
|
curBlock.RUnlock()
|
|
|
|
if bs.Height != int32(btcutil.BlockHeightUnknown) {
|
|
|
|
return bs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a hack and may result in races, but we need to make
|
|
|
|
// sure that btcd is connected and sending a message will succeed,
|
|
|
|
// or this will block forever. A better solution is to return an
|
|
|
|
// error to the reply handler immediately if btcd is disconnected.
|
|
|
|
if !btcdConnected.b {
|
|
|
|
return wallet.BlockStamp{
|
|
|
|
Height: int32(btcutil.BlockHeightUnknown),
|
|
|
|
}, errors.New("current block unavailable")
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2013-09-05 21:31:39 +02:00
|
|
|
|
2013-10-15 16:40:23 +02:00
|
|
|
n := <-NewJSONID
|
2013-11-06 17:23:30 +01:00
|
|
|
cmd := btcws.NewGetBestBlockCmd(fmt.Sprintf("btcwallet(%v)", n))
|
|
|
|
mcmd, err := cmd.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
return wallet.BlockStamp{
|
|
|
|
Height: int32(btcutil.BlockHeightUnknown),
|
|
|
|
}, errors.New("cannot ask for best block")
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2013-09-05 21:31:39 +02:00
|
|
|
|
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
|
|
|
c := make(chan *struct {
|
|
|
|
hash *btcwire.ShaHash
|
|
|
|
height int32
|
|
|
|
})
|
2013-09-05 21:31:39 +02:00
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
replyHandlers.Lock()
|
|
|
|
replyHandlers.m[n] = func(result interface{}, e *btcjson.Error) bool {
|
|
|
|
if e != 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
|
|
|
c <- nil
|
2013-09-09 20:14:57 +02:00
|
|
|
return true
|
|
|
|
}
|
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
|
|
|
m, ok := result.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
c <- nil
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
hashBE, ok := m["hash"].(string)
|
|
|
|
if !ok {
|
|
|
|
c <- nil
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
hash, err := btcwire.NewShaHashFromStr(hashBE)
|
|
|
|
if err != nil {
|
|
|
|
c <- nil
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
fheight, ok := m["height"].(float64)
|
|
|
|
if !ok {
|
|
|
|
c <- nil
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
c <- &struct {
|
|
|
|
hash *btcwire.ShaHash
|
|
|
|
height int32
|
|
|
|
}{
|
|
|
|
hash: hash,
|
|
|
|
height: int32(fheight),
|
2013-09-05 21:31:39 +02:00
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
replyHandlers.Unlock()
|
2013-09-05 21:31:39 +02:00
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// send message
|
2013-11-06 17:23:30 +01:00
|
|
|
btcdMsgs <- mcmd
|
2013-09-09 20:14:57 +02:00
|
|
|
|
|
|
|
// Block until reply is ready.
|
2013-11-12 18:01:32 +01:00
|
|
|
reply, ok := <-c
|
|
|
|
if !ok || reply == nil {
|
|
|
|
return wallet.BlockStamp{
|
|
|
|
Height: int32(btcutil.BlockHeightUnknown),
|
|
|
|
}, errors.New("current block unavailable")
|
2013-09-05 21:31:39 +02:00
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
|
2013-11-12 18:01:32 +01:00
|
|
|
curBlock.Lock()
|
|
|
|
if reply.height > curBlock.BlockStamp.Height {
|
|
|
|
bs = wallet.BlockStamp{
|
|
|
|
Height: reply.height,
|
|
|
|
Hash: *reply.hash,
|
|
|
|
}
|
|
|
|
curBlock.BlockStamp = bs
|
|
|
|
}
|
|
|
|
curBlock.Unlock()
|
|
|
|
return bs, nil
|
2013-09-05 21:31:39 +02:00
|
|
|
}
|
|
|
|
|
2013-10-15 16:40:23 +02:00
|
|
|
// NewJSONID is used to receive the next unique JSON ID for btcd
|
2013-10-15 00:45:48 +02:00
|
|
|
// requests, starting from zero and incrementing by one after each
|
|
|
|
// read.
|
2013-10-15 16:40:23 +02:00
|
|
|
var NewJSONID = make(chan uint64)
|
2013-10-15 00:45:48 +02:00
|
|
|
|
2013-10-15 16:40:23 +02:00
|
|
|
// JSONIDGenerator sends incremental integers across a channel. This
|
2013-10-15 00:45:48 +02:00
|
|
|
// is meant to provide a unique value for the JSON ID field for btcd
|
|
|
|
// messages.
|
2013-10-15 16:40:23 +02:00
|
|
|
func JSONIDGenerator(c chan uint64) {
|
2013-10-15 00:45:48 +02:00
|
|
|
var n uint64
|
|
|
|
for {
|
|
|
|
c <- n
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
func main() {
|
2013-10-29 15:38:51 +01:00
|
|
|
// Initialize logging and setup deferred flushing to ensure all
|
|
|
|
// outstanding messages are written on shutdown
|
|
|
|
loggers := setLogLevel(defaultLogLevel)
|
|
|
|
defer func() {
|
|
|
|
for _, logger := range loggers {
|
|
|
|
logger.Flush()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
tcfg, _, err := loadConfig()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
cfg = tcfg
|
|
|
|
|
2013-10-29 15:38:51 +01:00
|
|
|
// Change the logging level if needed.
|
|
|
|
if cfg.DebugLevel != defaultLogLevel {
|
|
|
|
loggers = setLogLevel(cfg.DebugLevel)
|
|
|
|
}
|
|
|
|
|
2013-10-15 21:53:49 +02:00
|
|
|
// Open default wallet
|
2013-09-09 20:14:57 +02:00
|
|
|
w, err := OpenWallet(cfg, "")
|
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
|
|
|
switch err {
|
|
|
|
case ErrNoTxs:
|
|
|
|
// Do nothing special for now. This will be implemented when
|
|
|
|
// the tx history file is properly written.
|
|
|
|
wallets.Lock()
|
|
|
|
wallets.m[""] = w
|
|
|
|
wallets.Unlock()
|
|
|
|
|
|
|
|
case ErrNoUtxos:
|
|
|
|
// Add wallet, but mark wallet as needing a full rescan since
|
|
|
|
// the wallet creation block. This will take place when btcd
|
|
|
|
// connects.
|
2013-09-09 20:14:57 +02:00
|
|
|
wallets.Lock()
|
|
|
|
wallets.m[""] = w
|
|
|
|
wallets.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
|
|
|
w.fullRescan = true
|
|
|
|
|
|
|
|
case nil:
|
|
|
|
wallets.Lock()
|
|
|
|
wallets.m[""] = w
|
|
|
|
wallets.Unlock()
|
|
|
|
|
|
|
|
default:
|
|
|
|
log.Errorf("cannot open wallet: %v", err)
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
// Start wallet disk syncer goroutine.
|
|
|
|
go DirtyWalletSyncer()
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
go func() {
|
|
|
|
// Start HTTP server to listen and send messages to frontend and btcd
|
|
|
|
// backend. Try reconnection if connection failed.
|
|
|
|
for {
|
2013-10-30 04:53:20 +01:00
|
|
|
if err := FrontendListenAndServe(); err != nil {
|
|
|
|
log.Info("Unable to start frontend HTTP server: %v", err)
|
|
|
|
os.Exit(1)
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2013-10-15 00:45:48 +02:00
|
|
|
// Begin generating new IDs for JSON calls.
|
2013-10-15 16:40:23 +02:00
|
|
|
go JSONIDGenerator(NewJSONID)
|
2013-10-15 00:45:48 +02:00
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
for {
|
|
|
|
replies := make(chan error)
|
|
|
|
done := make(chan int)
|
|
|
|
go func() {
|
|
|
|
BtcdConnect(replies)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
selectLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
break selectLoop
|
|
|
|
case err := <-replies:
|
|
|
|
switch err {
|
|
|
|
case ErrConnRefused:
|
|
|
|
btcdConnected.c <- false
|
|
|
|
log.Info("btcd connection refused, retying in 5 seconds")
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
case ErrConnLost:
|
|
|
|
btcdConnected.c <- false
|
|
|
|
log.Info("btcd connection lost, retrying in 5 seconds")
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
case nil:
|
|
|
|
btcdConnected.c <- true
|
|
|
|
log.Info("Established connection to btcd.")
|
|
|
|
default:
|
|
|
|
log.Infof("Unhandled error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|