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 (
|
Perform smarter UTXO tracking.
This change fixes many issues with the tracking of unspent transaction
outputs. First, notifications for when UTXOs arse spent are now
requested from btcd, and when spent, will be removed from the
UtxoStore.
Second, when transactions are created, the unconfirmed (not yet in a
block) Utxo (with block height -1 and zeroed block hash) is added to
the wallet's UtxoStore. Notifications for when this UTXO is spent are
also requested from btcd. After the tx appears in a block, because
the UTXO has a pkScript to be spent by another owned wallet address, a
notification with the UTXO will be sent to btcwallet. We already
store the unconfirmed UTXO, so at this point the actual block height
and hash are filled in.
Finally, when calculating the balance, if confirmations is zero,
unconfirmed UTXOs (block height -1) will be included in the balance.
Otherwise, they are ignored.
2013-10-22 15:55:53 +02:00
|
|
|
"bytes"
|
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-09-09 20:14:57 +02:00
|
|
|
wallets = NewBtcWalletStore()
|
2013-08-21 16:37:30 +02:00
|
|
|
)
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// BtcWallet is a structure containing all the components for a
|
|
|
|
// complete wallet. It contains the Armory-style wallet (to store
|
|
|
|
// addresses and keys), and tx and utxo data stores, along with locks
|
|
|
|
// to prevent against incorrect multiple access.
|
2013-09-03 06:10:32 +02:00
|
|
|
type BtcWallet struct {
|
|
|
|
*wallet.Wallet
|
Perform smarter UTXO tracking.
This change fixes many issues with the tracking of unspent transaction
outputs. First, notifications for when UTXOs arse spent are now
requested from btcd, and when spent, will be removed from the
UtxoStore.
Second, when transactions are created, the unconfirmed (not yet in a
block) Utxo (with block height -1 and zeroed block hash) is added to
the wallet's UtxoStore. Notifications for when this UTXO is spent are
also requested from btcd. After the tx appears in a block, because
the UTXO has a pkScript to be spent by another owned wallet address, a
notification with the UTXO will be sent to btcwallet. We already
store the unconfirmed UTXO, so at this point the actual block height
and hash are filled in.
Finally, when calculating the balance, if confirmations is zero,
unconfirmed UTXOs (block height -1) will be included in the balance.
Otherwise, they are ignored.
2013-10-22 15:55:53 +02:00
|
|
|
mtx sync.RWMutex
|
|
|
|
name string
|
|
|
|
dirty 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
|
|
|
fullRescan bool
|
Perform smarter UTXO tracking.
This change fixes many issues with the tracking of unspent transaction
outputs. First, notifications for when UTXOs arse spent are now
requested from btcd, and when spent, will be removed from the
UtxoStore.
Second, when transactions are created, the unconfirmed (not yet in a
block) Utxo (with block height -1 and zeroed block hash) is added to
the wallet's UtxoStore. Notifications for when this UTXO is spent are
also requested from btcd. After the tx appears in a block, because
the UTXO has a pkScript to be spent by another owned wallet address, a
notification with the UTXO will be sent to btcwallet. We already
store the unconfirmed UTXO, so at this point the actual block height
and hash are filled in.
Finally, when calculating the balance, if confirmations is zero,
unconfirmed UTXOs (block height -1) will be included in the balance.
Otherwise, they are ignored.
2013-10-22 15:55:53 +02:00
|
|
|
NewBlockTxSeqN uint64
|
|
|
|
SpentOutpointSeqN uint64
|
|
|
|
UtxoStore struct {
|
2013-09-04 01:05:59 +02:00
|
|
|
sync.RWMutex
|
|
|
|
dirty bool
|
|
|
|
s tx.UtxoStore
|
|
|
|
}
|
|
|
|
TxStore struct {
|
|
|
|
sync.RWMutex
|
|
|
|
dirty bool
|
|
|
|
s tx.TxStore
|
|
|
|
}
|
2013-09-03 06:10:32 +02:00
|
|
|
}
|
|
|
|
|
2013-10-14 22:39:15 +02:00
|
|
|
// BtcWalletStore stores all wallets currently being handled by
|
|
|
|
// btcwallet. Wallet are stored in a map with the account name as the
|
|
|
|
// key. A RWMutex is used to protect against incorrect concurrent
|
|
|
|
// access.
|
2013-09-09 20:14:57 +02:00
|
|
|
type BtcWalletStore struct {
|
2013-10-29 07:19:40 +01:00
|
|
|
sync.Mutex
|
2013-09-09 20:14:57 +02:00
|
|
|
m map[string]*BtcWallet
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBtcWalletStore returns an initialized and empty BtcWalletStore.
|
|
|
|
func NewBtcWalletStore() *BtcWalletStore {
|
|
|
|
return &BtcWalletStore{
|
|
|
|
m: make(map[string]*BtcWallet),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-14 22:39:15 +02:00
|
|
|
// Rollback rolls back each BtcWallet saved in the store.
|
2013-10-28 21:58:35 +01:00
|
|
|
//
|
|
|
|
// TODO(jrick): This must also roll back the UTXO and TX stores, and notify
|
|
|
|
// all wallets of new account balances.
|
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
|
|
|
func (s *BtcWalletStore) Rollback(height int32, hash *btcwire.ShaHash) {
|
2013-09-09 20:14:57 +02:00
|
|
|
for _, w := range s.m {
|
|
|
|
w.Rollback(height, hash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-14 22:39:15 +02:00
|
|
|
// Rollback reverts each stored BtcWallet to a state before the block
|
|
|
|
// with the passed chainheight and block hash was connected to the main
|
|
|
|
// chain. This is used to remove transactions and utxos for each wallet
|
|
|
|
// that occured on a chain no longer considered to be the main chain.
|
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
|
|
|
func (w *BtcWallet) Rollback(height int32, hash *btcwire.ShaHash) {
|
2013-09-09 20:14:57 +02:00
|
|
|
w.UtxoStore.Lock()
|
|
|
|
w.UtxoStore.dirty = w.UtxoStore.dirty || w.UtxoStore.s.Rollback(height, hash)
|
|
|
|
w.UtxoStore.Unlock()
|
|
|
|
|
|
|
|
w.TxStore.Lock()
|
|
|
|
w.TxStore.dirty = w.TxStore.dirty || w.TxStore.s.Rollback(height, hash)
|
|
|
|
w.TxStore.Unlock()
|
2013-10-15 22:55:28 +02:00
|
|
|
|
|
|
|
if err := w.writeDirtyToDisk(); err != nil {
|
|
|
|
log.Errorf("cannot sync dirty wallet: %v", err)
|
|
|
|
}
|
2013-09-09 20:14:57 +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-09-03 16:21:14 +02:00
|
|
|
func OpenWallet(cfg *config, account string) (*BtcWallet, 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
|
|
|
}
|
|
|
|
|
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 := &BtcWallet{
|
|
|
|
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-09-09 20:14:57 +02:00
|
|
|
// CalculateBalance sums the amounts of all unspent transaction
|
|
|
|
// outputs to addresses of a wallet and returns the balance as a
|
|
|
|
// float64.
|
2013-10-28 21:58:35 +01:00
|
|
|
//
|
|
|
|
// If confirmations is 0, all UTXOs, even those not present in a
|
|
|
|
// block (height -1), will be used to get the balance. Otherwise,
|
|
|
|
// a UTXO must be in a block. If confirmations is 1 or greater,
|
|
|
|
// the balance will be calculated based on how many how many blocks
|
|
|
|
// include a UTXO.
|
2013-10-29 07:19:40 +01:00
|
|
|
func (w *BtcWallet) CalculateBalance(confirms int) float64 {
|
2013-09-09 20:14:57 +02:00
|
|
|
var bal uint64 // Measured in satoshi
|
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
|
|
|
bs, err := GetCurBlock()
|
|
|
|
if bs.Height == int32(btcutil.BlockHeightUnknown) || err != nil {
|
2013-09-05 21:31:39 +02:00
|
|
|
return 0.
|
|
|
|
}
|
|
|
|
|
|
|
|
w.UtxoStore.RLock()
|
2013-09-09 20:14:57 +02:00
|
|
|
for _, u := range w.UtxoStore.s {
|
2013-10-28 21:58:35 +01:00
|
|
|
// Utxos not yet in blocks (height -1) should only be
|
|
|
|
// added if confirmations is 0.
|
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 confirms == 0 || (u.Height != -1 && int(bs.Height-u.Height+1) >= confirms) {
|
2013-10-28 21:58:35 +01:00
|
|
|
bal += u.Amt
|
2013-09-05 21:31:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
w.UtxoStore.RUnlock()
|
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
|
|
|
return float64(bal) / float64(btcutil.SatoshiPerBitcoin)
|
2013-09-05 21:31:39 +02:00
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// Track requests btcd to send notifications of new transactions for
|
|
|
|
// each address stored in a wallet and sets up a new reply handler for
|
|
|
|
// these notifications.
|
2013-09-03 23:16:07 +02:00
|
|
|
func (w *BtcWallet) Track() {
|
2013-10-15 16:40:23 +02:00
|
|
|
n := <-NewJSONID
|
2013-09-05 17:51:33 +02:00
|
|
|
w.mtx.Lock()
|
|
|
|
w.NewBlockTxSeqN = n
|
|
|
|
w.mtx.Unlock()
|
2013-09-04 01:05:59 +02:00
|
|
|
|
2013-09-05 17:19:48 +02:00
|
|
|
replyHandlers.Lock()
|
2013-11-05 00:58:41 +01:00
|
|
|
replyHandlers.m[n] = w.newBlockTxOutHandler
|
2013-09-05 17:19:48 +02:00
|
|
|
replyHandlers.Unlock()
|
2013-09-04 01:05:59 +02:00
|
|
|
for _, addr := range w.GetActiveAddresses() {
|
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.ReqNewTxsForAddress(addr.Address)
|
2013-09-04 01:05:59 +02:00
|
|
|
}
|
Perform smarter UTXO tracking.
This change fixes many issues with the tracking of unspent transaction
outputs. First, notifications for when UTXOs arse spent are now
requested from btcd, and when spent, will be removed from the
UtxoStore.
Second, when transactions are created, the unconfirmed (not yet in a
block) Utxo (with block height -1 and zeroed block hash) is added to
the wallet's UtxoStore. Notifications for when this UTXO is spent are
also requested from btcd. After the tx appears in a block, because
the UTXO has a pkScript to be spent by another owned wallet address, a
notification with the UTXO will be sent to btcwallet. We already
store the unconfirmed UTXO, so at this point the actual block height
and hash are filled in.
Finally, when calculating the balance, if confirmations is zero,
unconfirmed UTXOs (block height -1) will be included in the balance.
Otherwise, they are ignored.
2013-10-22 15:55:53 +02:00
|
|
|
|
|
|
|
n = <-NewJSONID
|
|
|
|
w.mtx.Lock()
|
|
|
|
w.SpentOutpointSeqN = n
|
|
|
|
w.mtx.Unlock()
|
|
|
|
|
|
|
|
replyHandlers.Lock()
|
|
|
|
replyHandlers.m[n] = w.spentUtxoHandler
|
|
|
|
replyHandlers.Unlock()
|
|
|
|
w.UtxoStore.RLock()
|
|
|
|
for _, utxo := range w.UtxoStore.s {
|
|
|
|
w.ReqSpentUtxoNtfn(utxo)
|
|
|
|
}
|
|
|
|
w.UtxoStore.RUnlock()
|
2013-09-04 01:05:59 +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
|
|
|
// RescanToBestBlock requests btcd to rescan the blockchain for new
|
|
|
|
// transactions to all wallet addresses. This is needed for making
|
|
|
|
// btcwallet catch up to a long-running btcd process, as otherwise
|
|
|
|
// it would have missed notifications as blocks are attached to the
|
|
|
|
// main chain.
|
|
|
|
func (w *BtcWallet) RescanToBestBlock() {
|
|
|
|
beginBlock := int32(0)
|
|
|
|
|
|
|
|
if w.fullRescan {
|
|
|
|
// Need to perform a complete rescan since the wallet creation
|
|
|
|
// block.
|
|
|
|
beginBlock = w.CreatedAt()
|
|
|
|
log.Debugf("Rescanning account '%v' for new transactions since block height %v",
|
|
|
|
w.name, beginBlock)
|
|
|
|
} else {
|
|
|
|
// The last synced block height should be used the starting
|
|
|
|
// point for block rescanning. Grab the block stamp here.
|
|
|
|
bs := w.SyncedWith()
|
|
|
|
|
|
|
|
log.Debugf("Rescanning account '%v' for new transactions since block height %v hash %v",
|
|
|
|
w.name, bs.Height, bs.Hash)
|
|
|
|
|
|
|
|
// If we're synced with block x, must scan the blocks x+1 to best block.
|
|
|
|
beginBlock = bs.Height + 1
|
2013-09-04 01:05:59 +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
|
|
|
|
|
|
|
n := <-NewJSONID
|
2013-11-06 17:23:30 +01:00
|
|
|
cmd, err := btcws.NewRescanCmd(fmt.Sprintf("btcwallet(%v)", n),
|
|
|
|
beginBlock, w.ActivePaymentAddresses())
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("cannot create rescan request: %v", err)
|
|
|
|
return
|
2013-09-04 01:05:59 +02:00
|
|
|
}
|
2013-11-06 17:23:30 +01:00
|
|
|
mcmd, err := cmd.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("cannot create rescan request: %v", err)
|
|
|
|
return
|
2013-09-04 01:05:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
replyHandlers.Lock()
|
2013-09-09 20:14:57 +02:00
|
|
|
replyHandlers.m[n] = func(result interface{}, e *btcjson.Error) 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
|
|
|
// Rescan is compatible with new txs from connected block
|
|
|
|
// notifications, so use that handler.
|
2013-11-05 00:58:41 +01:00
|
|
|
_ = w.newBlockTxOutHandler(result, e)
|
2013-09-04 01:05:59 +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
|
|
|
if result != nil {
|
|
|
|
// Notify frontends of new account balance.
|
|
|
|
confirmed := w.CalculateBalance(1)
|
|
|
|
unconfirmed := w.CalculateBalance(0) - confirmed
|
|
|
|
NotifyWalletBalance(frontendNotificationMaster, w.name, confirmed)
|
|
|
|
NotifyWalletBalanceUnconfirmed(frontendNotificationMaster, w.name, unconfirmed)
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if bs, err := GetCurBlock(); err == nil {
|
|
|
|
w.SetSyncedWith(&bs)
|
|
|
|
w.dirty = true
|
|
|
|
if err = w.writeDirtyToDisk(); err != nil {
|
|
|
|
log.Errorf("cannot sync dirty wallet: %v",
|
|
|
|
err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If result is nil, the rescan has completed. Returning
|
|
|
|
// true removes this handler.
|
|
|
|
return true
|
2013-09-04 01:05:59 +02:00
|
|
|
}
|
|
|
|
replyHandlers.Unlock()
|
|
|
|
|
2013-11-06 17:23:30 +01:00
|
|
|
btcdMsgs <- mcmd
|
2013-09-04 01:05:59 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 17:23:30 +01:00
|
|
|
// SortedActivePaymentAddresses returns a slice of all active payment
|
|
|
|
// addresses in an account.
|
|
|
|
func (w *BtcWallet) SortedActivePaymentAddresses() []string {
|
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.mtx.RLock()
|
|
|
|
defer w.mtx.RUnlock()
|
|
|
|
|
2013-11-06 17:23:30 +01:00
|
|
|
infos := w.GetSortedActiveAddresses()
|
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
|
|
|
addrs := make([]string, len(infos))
|
|
|
|
|
2013-11-06 17:23:30 +01:00
|
|
|
for i, addr := range infos {
|
|
|
|
addrs[i] = addr.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
|
|
|
// ActivePaymentAddresses returns a set of all active pubkey hashes
|
|
|
|
// in an account.
|
|
|
|
func (w *BtcWallet) ActivePaymentAddresses() map[string]struct{} {
|
|
|
|
w.mtx.RLock()
|
|
|
|
defer w.mtx.RUnlock()
|
|
|
|
|
|
|
|
infos := w.GetActiveAddresses()
|
|
|
|
addrs := make(map[string]struct{}, len(infos))
|
|
|
|
|
|
|
|
for _, info := range infos {
|
|
|
|
addrs[info.Address] = 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
|
|
|
}
|
|
|
|
|
|
|
|
return addrs
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// ReqNewTxsForAddress sends a message to btcd to request tx updates
|
|
|
|
// for addr for each new block that is added to the blockchain.
|
2013-09-04 19:41:33 +02:00
|
|
|
func (w *BtcWallet) ReqNewTxsForAddress(addr string) {
|
2013-10-17 17:16:45 +02:00
|
|
|
log.Debugf("Requesting notifications of TXs sending to address %v", addr)
|
|
|
|
|
2013-09-05 17:19:48 +02:00
|
|
|
w.mtx.RLock()
|
|
|
|
n := w.NewBlockTxSeqN
|
|
|
|
w.mtx.RUnlock()
|
2013-09-04 01:05:59 +02:00
|
|
|
|
2013-11-06 17:23:30 +01:00
|
|
|
cmd := btcws.NewNotifyNewTXsCmd(fmt.Sprintf("btcwallet(%d)", n),
|
|
|
|
[]string{addr})
|
|
|
|
mcmd, err := cmd.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("cannot request transaction notifications: %v", err)
|
2013-09-04 01:05:59 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 17:23:30 +01:00
|
|
|
btcdMsgs <- mcmd
|
2013-09-05 17:19:48 +02:00
|
|
|
}
|
2013-09-04 01:05:59 +02:00
|
|
|
|
Perform smarter UTXO tracking.
This change fixes many issues with the tracking of unspent transaction
outputs. First, notifications for when UTXOs arse spent are now
requested from btcd, and when spent, will be removed from the
UtxoStore.
Second, when transactions are created, the unconfirmed (not yet in a
block) Utxo (with block height -1 and zeroed block hash) is added to
the wallet's UtxoStore. Notifications for when this UTXO is spent are
also requested from btcd. After the tx appears in a block, because
the UTXO has a pkScript to be spent by another owned wallet address, a
notification with the UTXO will be sent to btcwallet. We already
store the unconfirmed UTXO, so at this point the actual block height
and hash are filled in.
Finally, when calculating the balance, if confirmations is zero,
unconfirmed UTXOs (block height -1) will be included in the balance.
Otherwise, they are ignored.
2013-10-22 15:55:53 +02:00
|
|
|
// ReqSpentUtxoNtfn sends a message to btcd to request updates for when
|
|
|
|
// a stored UTXO has been spent.
|
|
|
|
func (w *BtcWallet) ReqSpentUtxoNtfn(u *tx.Utxo) {
|
|
|
|
log.Debugf("Requesting spent UTXO notifications for Outpoint hash %s index %d",
|
|
|
|
u.Out.Hash, u.Out.Index)
|
|
|
|
|
|
|
|
w.mtx.RLock()
|
|
|
|
n := w.SpentOutpointSeqN
|
|
|
|
w.mtx.RUnlock()
|
|
|
|
|
2013-11-06 17:23:30 +01:00
|
|
|
cmd := btcws.NewNotifySpentCmd(fmt.Sprintf("btcwallet(%d)", n),
|
|
|
|
(*btcwire.OutPoint)(&u.Out))
|
|
|
|
mcmd, err := cmd.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("cannot create spent request: %v", err)
|
|
|
|
return
|
Perform smarter UTXO tracking.
This change fixes many issues with the tracking of unspent transaction
outputs. First, notifications for when UTXOs arse spent are now
requested from btcd, and when spent, will be removed from the
UtxoStore.
Second, when transactions are created, the unconfirmed (not yet in a
block) Utxo (with block height -1 and zeroed block hash) is added to
the wallet's UtxoStore. Notifications for when this UTXO is spent are
also requested from btcd. After the tx appears in a block, because
the UTXO has a pkScript to be spent by another owned wallet address, a
notification with the UTXO will be sent to btcwallet. We already
store the unconfirmed UTXO, so at this point the actual block height
and hash are filled in.
Finally, when calculating the balance, if confirmations is zero,
unconfirmed UTXOs (block height -1) will be included in the balance.
Otherwise, they are ignored.
2013-10-22 15:55:53 +02:00
|
|
|
}
|
|
|
|
|
2013-11-06 17:23:30 +01:00
|
|
|
btcdMsgs <- mcmd
|
Perform smarter UTXO tracking.
This change fixes many issues with the tracking of unspent transaction
outputs. First, notifications for when UTXOs arse spent are now
requested from btcd, and when spent, will be removed from the
UtxoStore.
Second, when transactions are created, the unconfirmed (not yet in a
block) Utxo (with block height -1 and zeroed block hash) is added to
the wallet's UtxoStore. Notifications for when this UTXO is spent are
also requested from btcd. After the tx appears in a block, because
the UTXO has a pkScript to be spent by another owned wallet address, a
notification with the UTXO will be sent to btcwallet. We already
store the unconfirmed UTXO, so at this point the actual block height
and hash are filled in.
Finally, when calculating the balance, if confirmations is zero,
unconfirmed UTXOs (block height -1) will be included in the balance.
Otherwise, they are ignored.
2013-10-22 15:55:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// spentUtxoHandler is the handler function for btcd spent UTXO notifications
|
|
|
|
// resulting from transactions in newly-attached blocks.
|
|
|
|
func (w *BtcWallet) spentUtxoHandler(result interface{}, e *btcjson.Error) bool {
|
|
|
|
if e != nil {
|
|
|
|
log.Errorf("Spent UTXO Handler: Error %d received from btcd: %s",
|
|
|
|
e.Code, e.Message)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
v, ok := result.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
txHashBE, ok := v["txhash"].(string)
|
|
|
|
if !ok {
|
|
|
|
log.Error("Spent UTXO Handler: Unspecified transaction hash.")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
txHash, err := btcwire.NewShaHashFromStr(txHashBE)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Spent UTXO Handler: Bad transaction hash: %s", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
index, ok := v["index"].(float64)
|
|
|
|
if !ok {
|
|
|
|
log.Error("Spent UTXO Handler: Unspecified index.")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = txHash, index
|
|
|
|
|
|
|
|
// Never remove this handler.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2013-11-05 00:58:41 +01:00
|
|
|
// newBlockTxOutHandler is the handler function for btcd transaction
|
2013-09-09 20:14:57 +02:00
|
|
|
// notifications resulting from newly-attached blocks.
|
2013-11-05 00:58:41 +01:00
|
|
|
func (w *BtcWallet) newBlockTxOutHandler(result interface{}, e *btcjson.Error) bool {
|
2013-09-05 21:31:39 +02:00
|
|
|
if e != nil {
|
2013-09-09 20:14:57 +02:00
|
|
|
log.Errorf("Tx Handler: Error %d received from btcd: %s",
|
|
|
|
e.Code, e.Message)
|
|
|
|
return false
|
2013-09-05 21:31:39 +02:00
|
|
|
}
|
|
|
|
|
2013-09-05 17:19:48 +02:00
|
|
|
v, ok := result.(map[string]interface{})
|
|
|
|
if !ok {
|
2013-09-05 21:31:39 +02:00
|
|
|
// The first result sent from btcd is nil. This could be used to
|
|
|
|
// indicate that the request for notifications succeeded.
|
|
|
|
if result != nil {
|
|
|
|
log.Errorf("Tx Handler: Unexpected result type %T.", result)
|
|
|
|
}
|
2013-09-05 17:19:48 +02:00
|
|
|
return false
|
|
|
|
}
|
2013-10-11 00:44:44 +02:00
|
|
|
sender, ok := v["sender"].(string)
|
2013-09-05 17:19:48 +02:00
|
|
|
if !ok {
|
|
|
|
log.Error("Tx Handler: Unspecified sender.")
|
|
|
|
return false
|
|
|
|
}
|
2013-10-11 00:44:44 +02:00
|
|
|
receiver, ok := v["receiver"].(string)
|
2013-09-05 17:19:48 +02:00
|
|
|
if !ok {
|
|
|
|
log.Error("Tx Handler: Unspecified receiver.")
|
|
|
|
return false
|
|
|
|
}
|
2013-09-09 20:14:57 +02:00
|
|
|
blockhashBE, ok := v["blockhash"].(string)
|
|
|
|
if !ok {
|
|
|
|
log.Error("Tx Handler: Unspecified block hash.")
|
|
|
|
return false
|
|
|
|
}
|
2013-09-05 17:19:48 +02:00
|
|
|
height, ok := v["height"].(float64)
|
|
|
|
if !ok {
|
|
|
|
log.Error("Tx Handler: Unspecified height.")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
txhashBE, ok := v["txhash"].(string)
|
|
|
|
if !ok {
|
|
|
|
log.Error("Tx Handler: Unspecified transaction hash.")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
index, ok := v["index"].(float64)
|
|
|
|
if !ok {
|
|
|
|
log.Error("Tx Handler: Unspecified transaction index.")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
amt, ok := v["amount"].(float64)
|
|
|
|
if !ok {
|
|
|
|
log.Error("Tx Handler: Unspecified amount.")
|
|
|
|
return false
|
|
|
|
}
|
2013-10-11 19:01:15 +02:00
|
|
|
pkscript58, ok := v["pkscript"].(string)
|
|
|
|
if !ok {
|
|
|
|
log.Error("Tx Handler: Unspecified pubkey script.")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
pkscript := btcutil.Base58Decode(pkscript58)
|
2013-11-05 00:58:41 +01:00
|
|
|
spent := false
|
|
|
|
if tspent, ok := v["spent"].(bool); ok {
|
|
|
|
spent = tspent
|
2013-09-05 17:19:48 +02:00
|
|
|
}
|
2013-09-04 01:05:59 +02:00
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
// btcd sends the block and tx hashes as BE strings. Convert both
|
|
|
|
// to a LE ShaHash.
|
|
|
|
blockhash, err := btcwire.NewShaHashFromStr(blockhashBE)
|
|
|
|
if err != nil {
|
2013-11-05 00:58:41 +01:00
|
|
|
log.Errorf("Tx Handler: Block hash string cannot be parsed: %v", err)
|
2013-09-09 20:14:57 +02:00
|
|
|
return false
|
|
|
|
}
|
2013-09-05 17:19:48 +02:00
|
|
|
txhash, err := btcwire.NewShaHashFromStr(txhashBE)
|
|
|
|
if err != nil {
|
2013-11-05 00:58:41 +01:00
|
|
|
log.Errorf("Tx Handler: Tx hash string cannot be parsed: %v", err)
|
2013-09-04 01:05:59 +02:00
|
|
|
return false
|
|
|
|
}
|
2013-10-11 00:44:44 +02:00
|
|
|
// TODO(jrick): btcd does not find the sender yet.
|
|
|
|
senderHash, _, _ := btcutil.DecodeAddress(sender)
|
|
|
|
receiverHash, _, err := btcutil.DecodeAddress(receiver)
|
|
|
|
if err != nil {
|
2013-11-05 00:58:41 +01:00
|
|
|
log.Errorf("Tx Handler: receiver address can not be decoded: %v", err)
|
2013-10-11 00:44:44 +02:00
|
|
|
return false
|
|
|
|
}
|
2013-09-05 17:19:48 +02:00
|
|
|
|
2013-11-05 00:58:41 +01:00
|
|
|
// Add to TxStore.
|
2013-10-29 07:19:40 +01:00
|
|
|
t := &tx.RecvTx{
|
|
|
|
Amt: uint64(amt),
|
|
|
|
}
|
|
|
|
copy(t.TxHash[:], txhash[:])
|
|
|
|
copy(t.BlockHash[:], blockhash[:])
|
|
|
|
copy(t.SenderAddr[:], senderHash)
|
|
|
|
copy(t.ReceiverAddr[:], receiverHash)
|
2013-09-05 17:19:48 +02:00
|
|
|
|
2013-10-29 07:19:40 +01:00
|
|
|
w.TxStore.Lock()
|
|
|
|
txs := w.TxStore.s
|
|
|
|
w.TxStore.s = append(txs, t)
|
|
|
|
w.TxStore.dirty = true
|
|
|
|
w.TxStore.Unlock()
|
2013-10-15 22:55:28 +02:00
|
|
|
|
2013-10-29 07:19:40 +01:00
|
|
|
if err = w.writeDirtyToDisk(); err != nil {
|
|
|
|
log.Errorf("cannot sync dirty wallet: %v", err)
|
|
|
|
}
|
2013-09-05 17:19:48 +02:00
|
|
|
|
2013-10-29 07:19:40 +01:00
|
|
|
// Add to UtxoStore if unspent.
|
2013-09-09 20:14:57 +02:00
|
|
|
if !spent {
|
2013-10-29 07:19:40 +01:00
|
|
|
// First, iterate through all stored utxos. If an unconfirmed utxo
|
|
|
|
// (not present in a block) has the same outpoint as this utxo,
|
|
|
|
// update the block height and hash.
|
|
|
|
w.UtxoStore.RLock()
|
|
|
|
for _, u := range w.UtxoStore.s {
|
|
|
|
if bytes.Equal(u.Out.Hash[:], txhash[:]) && u.Out.Index == uint32(index) {
|
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
|
|
|
// Found a either a duplicate, or a change UTXO. If not change,
|
|
|
|
// ignore it.
|
2013-11-06 20:12:52 +01:00
|
|
|
w.UtxoStore.RUnlock()
|
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 u.Height != -1 {
|
|
|
|
return false
|
|
|
|
}
|
2013-10-29 07:19:40 +01:00
|
|
|
|
|
|
|
w.UtxoStore.Lock()
|
|
|
|
copy(u.BlockHash[:], blockhash[:])
|
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
|
|
|
u.Height = int32(height)
|
2013-10-29 07:19:40 +01:00
|
|
|
w.UtxoStore.dirty = true
|
|
|
|
w.UtxoStore.Unlock()
|
|
|
|
|
|
|
|
if err = w.writeDirtyToDisk(); err != nil {
|
|
|
|
log.Errorf("cannot sync dirty wallet: %v", err)
|
Perform smarter UTXO tracking.
This change fixes many issues with the tracking of unspent transaction
outputs. First, notifications for when UTXOs arse spent are now
requested from btcd, and when spent, will be removed from the
UtxoStore.
Second, when transactions are created, the unconfirmed (not yet in a
block) Utxo (with block height -1 and zeroed block hash) is added to
the wallet's UtxoStore. Notifications for when this UTXO is spent are
also requested from btcd. After the tx appears in a block, because
the UTXO has a pkScript to be spent by another owned wallet address, a
notification with the UTXO will be sent to btcwallet. We already
store the unconfirmed UTXO, so at this point the actual block height
and hash are filled in.
Finally, when calculating the balance, if confirmations is zero,
unconfirmed UTXOs (block height -1) will be included in the balance.
Otherwise, they are ignored.
2013-10-22 15:55:53 +02:00
|
|
|
}
|
2013-10-29 07:19:40 +01:00
|
|
|
return false
|
Perform smarter UTXO tracking.
This change fixes many issues with the tracking of unspent transaction
outputs. First, notifications for when UTXOs arse spent are now
requested from btcd, and when spent, will be removed from the
UtxoStore.
Second, when transactions are created, the unconfirmed (not yet in a
block) Utxo (with block height -1 and zeroed block hash) is added to
the wallet's UtxoStore. Notifications for when this UTXO is spent are
also requested from btcd. After the tx appears in a block, because
the UTXO has a pkScript to be spent by another owned wallet address, a
notification with the UTXO will be sent to btcwallet. We already
store the unconfirmed UTXO, so at this point the actual block height
and hash are filled in.
Finally, when calculating the balance, if confirmations is zero,
unconfirmed UTXOs (block height -1) will be included in the balance.
Otherwise, they are ignored.
2013-10-22 15:55:53 +02:00
|
|
|
}
|
2013-10-29 07:19:40 +01:00
|
|
|
}
|
|
|
|
w.UtxoStore.RUnlock()
|
Perform smarter UTXO tracking.
This change fixes many issues with the tracking of unspent transaction
outputs. First, notifications for when UTXOs arse spent are now
requested from btcd, and when spent, will be removed from the
UtxoStore.
Second, when transactions are created, the unconfirmed (not yet in a
block) Utxo (with block height -1 and zeroed block hash) is added to
the wallet's UtxoStore. Notifications for when this UTXO is spent are
also requested from btcd. After the tx appears in a block, because
the UTXO has a pkScript to be spent by another owned wallet address, a
notification with the UTXO will be sent to btcwallet. We already
store the unconfirmed UTXO, so at this point the actual block height
and hash are filled in.
Finally, when calculating the balance, if confirmations is zero,
unconfirmed UTXOs (block height -1) will be included in the balance.
Otherwise, they are ignored.
2013-10-22 15:55:53 +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
|
|
|
// After iterating through all UTXOs, it was not a duplicate or
|
|
|
|
// change UTXO appearing in a block. Append a new Utxo to the end.
|
|
|
|
|
2013-10-29 07:19:40 +01:00
|
|
|
u := &tx.Utxo{
|
|
|
|
Amt: uint64(amt),
|
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
|
|
|
Height: int32(height),
|
2013-10-29 07:19:40 +01:00
|
|
|
Subscript: pkscript,
|
|
|
|
}
|
|
|
|
copy(u.Out.Hash[:], txhash[:])
|
|
|
|
u.Out.Index = uint32(index)
|
|
|
|
copy(u.AddrHash[:], receiverHash)
|
|
|
|
copy(u.BlockHash[:], blockhash[:])
|
|
|
|
w.UtxoStore.Lock()
|
|
|
|
w.UtxoStore.s = append(w.UtxoStore.s, u)
|
|
|
|
w.UtxoStore.dirty = true
|
|
|
|
w.UtxoStore.Unlock()
|
|
|
|
if err = w.writeDirtyToDisk(); err != nil {
|
|
|
|
log.Errorf("cannot sync dirty wallet: %v", err)
|
|
|
|
}
|
2013-10-15 22:55:28 +02:00
|
|
|
|
2013-10-29 07:19:40 +01:00
|
|
|
confirmed := w.CalculateBalance(1)
|
|
|
|
unconfirmed := w.CalculateBalance(0) - confirmed
|
|
|
|
NotifyWalletBalance(frontendNotificationMaster, w.name, confirmed)
|
|
|
|
NotifyWalletBalanceUnconfirmed(frontendNotificationMaster, w.name, unconfirmed)
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2013-09-05 17:19:48 +02:00
|
|
|
|
|
|
|
// Never remove this handler.
|
|
|
|
return false
|
2013-09-03 06:10:32 +02:00
|
|
|
}
|
2013-09-09 20:14:57 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|