2013-08-21 16:37:30 +02:00
|
|
|
/*
|
2014-01-03 19:34:37 +01:00
|
|
|
* Copyright (c) 2013, 2014 Conformal Systems LLC <info@conformal.com>
|
2013-08-21 16:37:30 +02:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-06-12 18:39:26 +02:00
|
|
|
"errors"
|
2013-11-19 18:21:54 +01:00
|
|
|
"io/ioutil"
|
2013-11-21 17:27:26 +01:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
_ "net/http/pprof"
|
2013-08-21 16:37:30 +02:00
|
|
|
"os"
|
2013-09-03 06:10:32 +02:00
|
|
|
"sync"
|
2013-08-21 16:37:30 +02:00
|
|
|
"time"
|
2014-06-12 18:39:26 +02:00
|
|
|
|
|
|
|
"github.com/conformal/btcutil"
|
|
|
|
"github.com/conformal/btcwallet/wallet"
|
|
|
|
"github.com/conformal/btcwire"
|
2013-08-21 16:37:30 +02:00
|
|
|
)
|
|
|
|
|
2013-09-03 16:21:14 +02:00
|
|
|
var (
|
2014-06-24 23:00:27 +02:00
|
|
|
cfg *config
|
|
|
|
server *rpcServer
|
|
|
|
shutdownChan = make(chan struct{})
|
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
|
|
|
}
|
2013-08-21 16:37:30 +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
|
|
|
// 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.
|
2014-05-28 06:54:50 +02:00
|
|
|
func GetCurBlock() (wallet.BlockStamp, error) {
|
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.RLock()
|
2014-05-28 06:54:50 +02:00
|
|
|
bs := curBlock.BlockStamp
|
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.RUnlock()
|
|
|
|
if bs.Height != int32(btcutil.BlockHeightUnknown) {
|
|
|
|
return bs, nil
|
|
|
|
}
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
var bbHash *btcwire.ShaHash
|
|
|
|
var bbHeight int32
|
|
|
|
client, err := accessClient()
|
|
|
|
if err == nil {
|
|
|
|
bbHash, bbHeight, err = client.GetBestBlock()
|
|
|
|
}
|
2014-06-04 02:52:04 +02:00
|
|
|
if err != nil {
|
|
|
|
unknown := wallet.BlockStamp{
|
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(btcutil.BlockHeightUnknown),
|
2014-06-04 02:52:04 +02:00
|
|
|
}
|
|
|
|
return unknown, err
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
2013-09-05 21:31:39 +02:00
|
|
|
|
2013-11-12 18:01:32 +01:00
|
|
|
curBlock.Lock()
|
2014-06-12 18:39:26 +02:00
|
|
|
if bbHeight > curBlock.BlockStamp.Height {
|
2013-11-12 18:01:32 +01:00
|
|
|
bs = wallet.BlockStamp{
|
2014-06-12 18:39:26 +02:00
|
|
|
Height: bbHeight,
|
|
|
|
Hash: *bbHash,
|
2013-11-12 18:01:32 +01:00
|
|
|
}
|
|
|
|
curBlock.BlockStamp = bs
|
|
|
|
}
|
|
|
|
curBlock.Unlock()
|
|
|
|
return bs, nil
|
2013-09-05 21:31:39 +02:00
|
|
|
}
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
var clientAccessChan = make(chan *rpcClient)
|
|
|
|
|
|
|
|
func clientAccess(newClient <-chan *rpcClient) {
|
|
|
|
var client *rpcClient
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case c := <-newClient:
|
|
|
|
client = c
|
|
|
|
case clientAccessChan <- client:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func accessClient() (*rpcClient, error) {
|
|
|
|
c := <-clientAccessChan
|
|
|
|
if c == nil {
|
|
|
|
return nil, errors.New("chain server disconnected")
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
2013-10-15 00:45:48 +02:00
|
|
|
|
2014-06-24 23:00:27 +02:00
|
|
|
func clientConnect(certs []byte, newClient chan<- *rpcClient) {
|
|
|
|
const initialWait = 5 * time.Second
|
|
|
|
wait := initialWait
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-server.quit:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := newRPCClient(certs)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Unable to open chain server client "+
|
|
|
|
"connection: %v", err)
|
|
|
|
time.Sleep(wait)
|
|
|
|
wait <<= 1
|
|
|
|
if wait > time.Minute {
|
|
|
|
wait = time.Minute
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
wait = initialWait
|
|
|
|
client.Start()
|
|
|
|
newClient <- client
|
|
|
|
|
|
|
|
client.WaitForShutdown()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 17:39:27 +02:00
|
|
|
func main() {
|
|
|
|
// Work around defer not working after os.Exit.
|
|
|
|
if err := walletMain(); err != nil {
|
|
|
|
os.Exit(1)
|
2013-10-15 00:45:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 17:39:27 +02:00
|
|
|
// walletMain is a work-around main function that is required since deferred
|
|
|
|
// functions (such as log flushing) are not called with calls to os.Exit.
|
|
|
|
// Instead, main runs this function and checks for a non-nil error, at which
|
|
|
|
// point any defers have already run, and if the error is non-nil, the program
|
|
|
|
// can be exited with an error exit status.
|
|
|
|
func walletMain() error {
|
2014-06-20 01:11:47 +02:00
|
|
|
// Load configuration and parse command line. This function also
|
|
|
|
// initializes logging and configures it accordingly.
|
2013-09-09 20:14:57 +02:00
|
|
|
tcfg, _, err := loadConfig()
|
|
|
|
if err != nil {
|
2014-06-21 17:39:27 +02:00
|
|
|
return err
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
cfg = tcfg
|
2014-06-20 01:11:47 +02:00
|
|
|
defer backendLog.Flush()
|
2013-10-29 15:38:51 +01:00
|
|
|
|
2013-11-21 17:27:26 +01:00
|
|
|
if cfg.Profile != "" {
|
|
|
|
go func() {
|
|
|
|
listenAddr := net.JoinHostPort("", cfg.Profile)
|
|
|
|
log.Infof("Profile server listening on %s", listenAddr)
|
|
|
|
profileRedirect := http.RedirectHandler("/debug/pprof",
|
|
|
|
http.StatusSeeOther)
|
|
|
|
http.Handle("/", profileRedirect)
|
|
|
|
log.Errorf("%v", http.ListenAndServe(listenAddr, nil))
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
// Read CA file to verify a btcd TLS connection.
|
|
|
|
certs, err := ioutil.ReadFile(cfg.CAFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("cannot open CA file: %v", err)
|
2014-06-21 17:39:27 +02:00
|
|
|
return err
|
2014-06-12 18:39:26 +02:00
|
|
|
}
|
|
|
|
|
Introduce new account file structure.
This changes the locations that account files (wallet.bin, utxo.bin,
and tx.bin) are searched for when opening or disk syncing accounts.
Previously, files were saved in the following layout:
~/.btcwallet/
- btcwallet/
- wallet.bin
- tx.bin
- utxo.bin
- btcwallet-AccountA/
- wallet.bin
- tx.bin
- utxo.bin
This format had two issues. First, each account would require its own
directory, causing a scalability issue on unix (and perhaps other)
platforms. Second, there was no distinction between testnet and
mainnet wallets, and if mainnet support was enabled, btcwallet would
attempt to open accounts with testnet wallets.
Instead, the following file structure is now used:
~/.btcwallet/
- testnet/
- wallet.bin
- tx.bin
- utxo.bin
- AccountA-wallet.bin
- AccountA-tx.bin
- AccountA-utxo.bin
This solves both of the previously-mentioned issues by requiring only
two subdirectories (one each for the testnet and mainnet bitcoin
networks), and by separating the locations to open and save testnet
and mainnet account files.
At startup, a check for the old account file structure is performed.
If found, files are moved to the new locations, and the old account
directories are removed. Account files are moved to the testnet
directory, as only testnet support is currently enabled.
The version has been bumped to 0.1.1 to reflect this change.
Fixes #16.
2013-12-05 02:16:50 +01:00
|
|
|
// Check and update any old file locations.
|
2014-06-21 17:39:27 +02:00
|
|
|
err = updateOldFileLocations()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
Introduce new account file structure.
This changes the locations that account files (wallet.bin, utxo.bin,
and tx.bin) are searched for when opening or disk syncing accounts.
Previously, files were saved in the following layout:
~/.btcwallet/
- btcwallet/
- wallet.bin
- tx.bin
- utxo.bin
- btcwallet-AccountA/
- wallet.bin
- tx.bin
- utxo.bin
This format had two issues. First, each account would require its own
directory, causing a scalability issue on unix (and perhaps other)
platforms. Second, there was no distinction between testnet and
mainnet wallets, and if mainnet support was enabled, btcwallet would
attempt to open accounts with testnet wallets.
Instead, the following file structure is now used:
~/.btcwallet/
- testnet/
- wallet.bin
- tx.bin
- utxo.bin
- AccountA-wallet.bin
- AccountA-tx.bin
- AccountA-utxo.bin
This solves both of the previously-mentioned issues by requiring only
two subdirectories (one each for the testnet and mainnet bitcoin
networks), and by separating the locations to open and save testnet
and mainnet account files.
At startup, a check for the old account file structure is performed.
If found, files are moved to the new locations, and the old account
directories are removed. Account files are moved to the testnet
directory, as only testnet support is currently enabled.
The version has been bumped to 0.1.1 to reflect this change.
Fixes #16.
2013-12-05 02:16:50 +01:00
|
|
|
|
2014-02-28 19:03:23 +01:00
|
|
|
// Start account manager and open accounts.
|
2014-03-25 05:59:24 +01:00
|
|
|
AcctMgr.Start()
|
2013-09-09 20:14:57 +02:00
|
|
|
|
2014-06-12 18:39:26 +02:00
|
|
|
server, err = newRPCServer(cfg.SvrListeners)
|
2013-11-19 18:21:54 +01:00
|
|
|
if err != nil {
|
2014-06-12 18:39:26 +02:00
|
|
|
log.Errorf("Unable to create HTTP server: %v", err)
|
2014-06-21 17:39:27 +02:00
|
|
|
return err
|
2013-11-19 18:21:54 +01:00
|
|
|
}
|
|
|
|
|
2014-06-12 19:54:58 +02:00
|
|
|
// Start HTTP server to serve wallet client connections.
|
2014-06-12 18:39:26 +02:00
|
|
|
server.Start()
|
2014-01-30 16:14:02 +01:00
|
|
|
|
2014-06-24 23:00:27 +02:00
|
|
|
// Shutdown the server if an interrupt signal is received.
|
|
|
|
addInterruptHandler(server.Stop)
|
|
|
|
|
2014-06-12 19:54:58 +02:00
|
|
|
// Start client connection to a btcd chain server. Attempt
|
|
|
|
// reconnections if the client could not be successfully connected.
|
2014-06-12 18:39:26 +02:00
|
|
|
clientChan := make(chan *rpcClient)
|
|
|
|
go clientAccess(clientChan)
|
2014-06-24 23:00:27 +02:00
|
|
|
go clientConnect(certs, clientChan)
|
2014-06-21 17:39:27 +02:00
|
|
|
|
2014-06-24 23:00:27 +02:00
|
|
|
// Wait for the server to shutdown either due to a stop RPC request
|
|
|
|
// or an interrupt.
|
|
|
|
server.WaitForShutdown()
|
|
|
|
log.Info("Shutdown complete")
|
|
|
|
return nil
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|