Remove account support, fix races on btcd connect.
This commit is the result of several big changes being made to the
wallet. In particular, the "handshake" (initial sync to the chain
server) was quite racy and required proper synchronization. To make
fixing this race easier, several other changes were made to the
internal wallet data structures and much of the RPC server ended up
being rewritten.
First, all account support has been removed. The previous Account
struct has been replaced with a Wallet structure, which includes a
keystore for saving keys, and a txstore for storing relevant
transactions. This decision has been made since it is the opinion of
myself and other developers that bitcoind accounts are fundamentally
broken (as accounts implemented by bitcoind support both arbitrary
address groupings as well as moving balances between accounts -- these
are fundamentally incompatible features), and since a BIP0032 keystore
is soon planned to be implemented (at which point, "accounts" can
return as HD extended keys). With the keystore handling the grouping
of related keys, there is no reason have many different Account
structs, and the AccountManager has been removed as well. All RPC
handlers that take an account option will only work with "" (the
default account) or "*" if the RPC allows specifying all accounts.
Second, much of the RPC server has been cleaned up. The global
variables for the RPC server and chain server client have been moved
to part of the rpcServer struct, and the handlers for each RPC method
that are looked up change depending on which components have been set.
Passthrough requests are also no longer handled specially, but when
the chain server is set, a handler to perform the passthrough will be
returned if the method is not otherwise a wallet RPC. The
notification system for websocket clients has also been rewritten so
wallet components can send notifications through channels, rather than
requiring direct access to the RPC server itself, or worse still,
sending directly to a websocket client's send channel. In the future,
this will enable proper registration of notifications, rather than
unsolicited broadcasts to every connected websocket client (see
issue #84).
Finally, and the main reason why much of this cleanup was necessary,
the races during intial sync with the chain server have been fixed.
Previously, when the 'Handshake' was run, a rescan would occur which
would perform modifications to Account data structures as
notifications were received. Synchronization was provided with a
single binary semaphore which serialized all access to wallet and
account data. However, the Handshake itself was not able to run with
this lock (or else notifications would block), and many data races
would occur as both notifications were being handled. If GOMAXPROCS
was ever increased beyond 1, btcwallet would always immediately crash
due to invalid addresses caused by the data races on startup. To fix
this, the single lock for all wallet access has been replaced with
mutexes for both the keystore and txstore. Handling of btcd
notifications and client requests may now occur simultaneously.
GOMAXPROCS has also been set to the number of logical CPUs at the
beginning of main, since with the data races fixed, there's no reason
to prevent the extra parallelism gained by increasing it.
Closes #78.
Closes #101.
Closes #110.
2014-07-09 05:17:38 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013, 2014 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 chain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2015-01-17 00:32:30 +01:00
|
|
|
"github.com/btcsuite/btcnet"
|
2015-01-16 04:28:09 +01:00
|
|
|
"github.com/btcsuite/btcrpcclient"
|
2015-01-15 17:48:58 +01:00
|
|
|
"github.com/btcsuite/btcutil"
|
2015-01-16 23:03:04 +01:00
|
|
|
"github.com/btcsuite/btcwire"
|
2015-01-17 06:40:19 +01:00
|
|
|
"github.com/btcsuite/btcws"
|
Remove account support, fix races on btcd connect.
This commit is the result of several big changes being made to the
wallet. In particular, the "handshake" (initial sync to the chain
server) was quite racy and required proper synchronization. To make
fixing this race easier, several other changes were made to the
internal wallet data structures and much of the RPC server ended up
being rewritten.
First, all account support has been removed. The previous Account
struct has been replaced with a Wallet structure, which includes a
keystore for saving keys, and a txstore for storing relevant
transactions. This decision has been made since it is the opinion of
myself and other developers that bitcoind accounts are fundamentally
broken (as accounts implemented by bitcoind support both arbitrary
address groupings as well as moving balances between accounts -- these
are fundamentally incompatible features), and since a BIP0032 keystore
is soon planned to be implemented (at which point, "accounts" can
return as HD extended keys). With the keystore handling the grouping
of related keys, there is no reason have many different Account
structs, and the AccountManager has been removed as well. All RPC
handlers that take an account option will only work with "" (the
default account) or "*" if the RPC allows specifying all accounts.
Second, much of the RPC server has been cleaned up. The global
variables for the RPC server and chain server client have been moved
to part of the rpcServer struct, and the handlers for each RPC method
that are looked up change depending on which components have been set.
Passthrough requests are also no longer handled specially, but when
the chain server is set, a handler to perform the passthrough will be
returned if the method is not otherwise a wallet RPC. The
notification system for websocket clients has also been rewritten so
wallet components can send notifications through channels, rather than
requiring direct access to the RPC server itself, or worse still,
sending directly to a websocket client's send channel. In the future,
this will enable proper registration of notifications, rather than
unsolicited broadcasts to every connected websocket client (see
issue #84).
Finally, and the main reason why much of this cleanup was necessary,
the races during intial sync with the chain server have been fixed.
Previously, when the 'Handshake' was run, a rescan would occur which
would perform modifications to Account data structures as
notifications were received. Synchronization was provided with a
single binary semaphore which serialized all access to wallet and
account data. However, the Handshake itself was not able to run with
this lock (or else notifications would block), and many data races
would occur as both notifications were being handled. If GOMAXPROCS
was ever increased beyond 1, btcwallet would always immediately crash
due to invalid addresses caused by the data races on startup. To fix
this, the single lock for all wallet access has been replaced with
mutexes for both the keystore and txstore. Handling of btcd
notifications and client requests may now occur simultaneously.
GOMAXPROCS has also been set to the number of logical CPUs at the
beginning of main, since with the data races fixed, there's no reason
to prevent the extra parallelism gained by increasing it.
Closes #78.
Closes #101.
Closes #110.
2014-07-09 05:17:38 +02:00
|
|
|
"github.com/conformal/btcwallet/keystore"
|
|
|
|
"github.com/conformal/btcwallet/txstore"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
*btcrpcclient.Client
|
|
|
|
netParams *btcnet.Params
|
|
|
|
|
|
|
|
enqueueNotification chan interface{}
|
|
|
|
dequeueNotification chan interface{}
|
|
|
|
currentBlock chan *keystore.BlockStamp
|
|
|
|
|
2014-07-30 16:47:50 +02:00
|
|
|
// Notification channels regarding the state of the client. These exist
|
|
|
|
// so other components can listen in on chain activity. These are
|
|
|
|
// initialized as nil, and must be created by calling one of the Listen*
|
|
|
|
// methods.
|
|
|
|
connected chan bool
|
|
|
|
notificationLock sync.Locker
|
|
|
|
|
Remove account support, fix races on btcd connect.
This commit is the result of several big changes being made to the
wallet. In particular, the "handshake" (initial sync to the chain
server) was quite racy and required proper synchronization. To make
fixing this race easier, several other changes were made to the
internal wallet data structures and much of the RPC server ended up
being rewritten.
First, all account support has been removed. The previous Account
struct has been replaced with a Wallet structure, which includes a
keystore for saving keys, and a txstore for storing relevant
transactions. This decision has been made since it is the opinion of
myself and other developers that bitcoind accounts are fundamentally
broken (as accounts implemented by bitcoind support both arbitrary
address groupings as well as moving balances between accounts -- these
are fundamentally incompatible features), and since a BIP0032 keystore
is soon planned to be implemented (at which point, "accounts" can
return as HD extended keys). With the keystore handling the grouping
of related keys, there is no reason have many different Account
structs, and the AccountManager has been removed as well. All RPC
handlers that take an account option will only work with "" (the
default account) or "*" if the RPC allows specifying all accounts.
Second, much of the RPC server has been cleaned up. The global
variables for the RPC server and chain server client have been moved
to part of the rpcServer struct, and the handlers for each RPC method
that are looked up change depending on which components have been set.
Passthrough requests are also no longer handled specially, but when
the chain server is set, a handler to perform the passthrough will be
returned if the method is not otherwise a wallet RPC. The
notification system for websocket clients has also been rewritten so
wallet components can send notifications through channels, rather than
requiring direct access to the RPC server itself, or worse still,
sending directly to a websocket client's send channel. In the future,
this will enable proper registration of notifications, rather than
unsolicited broadcasts to every connected websocket client (see
issue #84).
Finally, and the main reason why much of this cleanup was necessary,
the races during intial sync with the chain server have been fixed.
Previously, when the 'Handshake' was run, a rescan would occur which
would perform modifications to Account data structures as
notifications were received. Synchronization was provided with a
single binary semaphore which serialized all access to wallet and
account data. However, the Handshake itself was not able to run with
this lock (or else notifications would block), and many data races
would occur as both notifications were being handled. If GOMAXPROCS
was ever increased beyond 1, btcwallet would always immediately crash
due to invalid addresses caused by the data races on startup. To fix
this, the single lock for all wallet access has been replaced with
mutexes for both the keystore and txstore. Handling of btcd
notifications and client requests may now occur simultaneously.
GOMAXPROCS has also been set to the number of logical CPUs at the
beginning of main, since with the data races fixed, there's no reason
to prevent the extra parallelism gained by increasing it.
Closes #78.
Closes #101.
Closes #110.
2014-07-09 05:17:38 +02:00
|
|
|
quit chan struct{}
|
|
|
|
wg sync.WaitGroup
|
|
|
|
started bool
|
|
|
|
quitMtx sync.Mutex
|
|
|
|
}
|
|
|
|
|
2015-01-09 10:20:59 +01:00
|
|
|
func NewClient(net *btcnet.Params, connect, user, pass string, certs []byte, disableTLS bool) (*Client, error) {
|
Remove account support, fix races on btcd connect.
This commit is the result of several big changes being made to the
wallet. In particular, the "handshake" (initial sync to the chain
server) was quite racy and required proper synchronization. To make
fixing this race easier, several other changes were made to the
internal wallet data structures and much of the RPC server ended up
being rewritten.
First, all account support has been removed. The previous Account
struct has been replaced with a Wallet structure, which includes a
keystore for saving keys, and a txstore for storing relevant
transactions. This decision has been made since it is the opinion of
myself and other developers that bitcoind accounts are fundamentally
broken (as accounts implemented by bitcoind support both arbitrary
address groupings as well as moving balances between accounts -- these
are fundamentally incompatible features), and since a BIP0032 keystore
is soon planned to be implemented (at which point, "accounts" can
return as HD extended keys). With the keystore handling the grouping
of related keys, there is no reason have many different Account
structs, and the AccountManager has been removed as well. All RPC
handlers that take an account option will only work with "" (the
default account) or "*" if the RPC allows specifying all accounts.
Second, much of the RPC server has been cleaned up. The global
variables for the RPC server and chain server client have been moved
to part of the rpcServer struct, and the handlers for each RPC method
that are looked up change depending on which components have been set.
Passthrough requests are also no longer handled specially, but when
the chain server is set, a handler to perform the passthrough will be
returned if the method is not otherwise a wallet RPC. The
notification system for websocket clients has also been rewritten so
wallet components can send notifications through channels, rather than
requiring direct access to the RPC server itself, or worse still,
sending directly to a websocket client's send channel. In the future,
this will enable proper registration of notifications, rather than
unsolicited broadcasts to every connected websocket client (see
issue #84).
Finally, and the main reason why much of this cleanup was necessary,
the races during intial sync with the chain server have been fixed.
Previously, when the 'Handshake' was run, a rescan would occur which
would perform modifications to Account data structures as
notifications were received. Synchronization was provided with a
single binary semaphore which serialized all access to wallet and
account data. However, the Handshake itself was not able to run with
this lock (or else notifications would block), and many data races
would occur as both notifications were being handled. If GOMAXPROCS
was ever increased beyond 1, btcwallet would always immediately crash
due to invalid addresses caused by the data races on startup. To fix
this, the single lock for all wallet access has been replaced with
mutexes for both the keystore and txstore. Handling of btcd
notifications and client requests may now occur simultaneously.
GOMAXPROCS has also been set to the number of logical CPUs at the
beginning of main, since with the data races fixed, there's no reason
to prevent the extra parallelism gained by increasing it.
Closes #78.
Closes #101.
Closes #110.
2014-07-09 05:17:38 +02:00
|
|
|
client := Client{
|
|
|
|
netParams: net,
|
|
|
|
enqueueNotification: make(chan interface{}),
|
|
|
|
dequeueNotification: make(chan interface{}),
|
|
|
|
currentBlock: make(chan *keystore.BlockStamp),
|
2014-07-30 16:47:50 +02:00
|
|
|
notificationLock: new(sync.Mutex),
|
Remove account support, fix races on btcd connect.
This commit is the result of several big changes being made to the
wallet. In particular, the "handshake" (initial sync to the chain
server) was quite racy and required proper synchronization. To make
fixing this race easier, several other changes were made to the
internal wallet data structures and much of the RPC server ended up
being rewritten.
First, all account support has been removed. The previous Account
struct has been replaced with a Wallet structure, which includes a
keystore for saving keys, and a txstore for storing relevant
transactions. This decision has been made since it is the opinion of
myself and other developers that bitcoind accounts are fundamentally
broken (as accounts implemented by bitcoind support both arbitrary
address groupings as well as moving balances between accounts -- these
are fundamentally incompatible features), and since a BIP0032 keystore
is soon planned to be implemented (at which point, "accounts" can
return as HD extended keys). With the keystore handling the grouping
of related keys, there is no reason have many different Account
structs, and the AccountManager has been removed as well. All RPC
handlers that take an account option will only work with "" (the
default account) or "*" if the RPC allows specifying all accounts.
Second, much of the RPC server has been cleaned up. The global
variables for the RPC server and chain server client have been moved
to part of the rpcServer struct, and the handlers for each RPC method
that are looked up change depending on which components have been set.
Passthrough requests are also no longer handled specially, but when
the chain server is set, a handler to perform the passthrough will be
returned if the method is not otherwise a wallet RPC. The
notification system for websocket clients has also been rewritten so
wallet components can send notifications through channels, rather than
requiring direct access to the RPC server itself, or worse still,
sending directly to a websocket client's send channel. In the future,
this will enable proper registration of notifications, rather than
unsolicited broadcasts to every connected websocket client (see
issue #84).
Finally, and the main reason why much of this cleanup was necessary,
the races during intial sync with the chain server have been fixed.
Previously, when the 'Handshake' was run, a rescan would occur which
would perform modifications to Account data structures as
notifications were received. Synchronization was provided with a
single binary semaphore which serialized all access to wallet and
account data. However, the Handshake itself was not able to run with
this lock (or else notifications would block), and many data races
would occur as both notifications were being handled. If GOMAXPROCS
was ever increased beyond 1, btcwallet would always immediately crash
due to invalid addresses caused by the data races on startup. To fix
this, the single lock for all wallet access has been replaced with
mutexes for both the keystore and txstore. Handling of btcd
notifications and client requests may now occur simultaneously.
GOMAXPROCS has also been set to the number of logical CPUs at the
beginning of main, since with the data races fixed, there's no reason
to prevent the extra parallelism gained by increasing it.
Closes #78.
Closes #101.
Closes #110.
2014-07-09 05:17:38 +02:00
|
|
|
quit: make(chan struct{}),
|
|
|
|
}
|
|
|
|
ntfnCallbacks := btcrpcclient.NotificationHandlers{
|
2014-07-28 16:33:00 +02:00
|
|
|
OnClientConnected: client.onClientConnect,
|
Remove account support, fix races on btcd connect.
This commit is the result of several big changes being made to the
wallet. In particular, the "handshake" (initial sync to the chain
server) was quite racy and required proper synchronization. To make
fixing this race easier, several other changes were made to the
internal wallet data structures and much of the RPC server ended up
being rewritten.
First, all account support has been removed. The previous Account
struct has been replaced with a Wallet structure, which includes a
keystore for saving keys, and a txstore for storing relevant
transactions. This decision has been made since it is the opinion of
myself and other developers that bitcoind accounts are fundamentally
broken (as accounts implemented by bitcoind support both arbitrary
address groupings as well as moving balances between accounts -- these
are fundamentally incompatible features), and since a BIP0032 keystore
is soon planned to be implemented (at which point, "accounts" can
return as HD extended keys). With the keystore handling the grouping
of related keys, there is no reason have many different Account
structs, and the AccountManager has been removed as well. All RPC
handlers that take an account option will only work with "" (the
default account) or "*" if the RPC allows specifying all accounts.
Second, much of the RPC server has been cleaned up. The global
variables for the RPC server and chain server client have been moved
to part of the rpcServer struct, and the handlers for each RPC method
that are looked up change depending on which components have been set.
Passthrough requests are also no longer handled specially, but when
the chain server is set, a handler to perform the passthrough will be
returned if the method is not otherwise a wallet RPC. The
notification system for websocket clients has also been rewritten so
wallet components can send notifications through channels, rather than
requiring direct access to the RPC server itself, or worse still,
sending directly to a websocket client's send channel. In the future,
this will enable proper registration of notifications, rather than
unsolicited broadcasts to every connected websocket client (see
issue #84).
Finally, and the main reason why much of this cleanup was necessary,
the races during intial sync with the chain server have been fixed.
Previously, when the 'Handshake' was run, a rescan would occur which
would perform modifications to Account data structures as
notifications were received. Synchronization was provided with a
single binary semaphore which serialized all access to wallet and
account data. However, the Handshake itself was not able to run with
this lock (or else notifications would block), and many data races
would occur as both notifications were being handled. If GOMAXPROCS
was ever increased beyond 1, btcwallet would always immediately crash
due to invalid addresses caused by the data races on startup. To fix
this, the single lock for all wallet access has been replaced with
mutexes for both the keystore and txstore. Handling of btcd
notifications and client requests may now occur simultaneously.
GOMAXPROCS has also been set to the number of logical CPUs at the
beginning of main, since with the data races fixed, there's no reason
to prevent the extra parallelism gained by increasing it.
Closes #78.
Closes #101.
Closes #110.
2014-07-09 05:17:38 +02:00
|
|
|
OnBlockConnected: client.onBlockConnected,
|
|
|
|
OnBlockDisconnected: client.onBlockDisconnected,
|
|
|
|
OnRecvTx: client.onRecvTx,
|
|
|
|
OnRedeemingTx: client.onRedeemingTx,
|
|
|
|
OnRescanFinished: client.onRescanFinished,
|
|
|
|
OnRescanProgress: client.onRescanProgress,
|
|
|
|
}
|
|
|
|
conf := btcrpcclient.ConnConfig{
|
|
|
|
Host: connect,
|
|
|
|
Endpoint: "ws",
|
|
|
|
User: user,
|
|
|
|
Pass: pass,
|
|
|
|
Certificates: certs,
|
|
|
|
DisableConnectOnNew: true,
|
2015-01-09 10:20:59 +01:00
|
|
|
DisableTLS: disableTLS,
|
Remove account support, fix races on btcd connect.
This commit is the result of several big changes being made to the
wallet. In particular, the "handshake" (initial sync to the chain
server) was quite racy and required proper synchronization. To make
fixing this race easier, several other changes were made to the
internal wallet data structures and much of the RPC server ended up
being rewritten.
First, all account support has been removed. The previous Account
struct has been replaced with a Wallet structure, which includes a
keystore for saving keys, and a txstore for storing relevant
transactions. This decision has been made since it is the opinion of
myself and other developers that bitcoind accounts are fundamentally
broken (as accounts implemented by bitcoind support both arbitrary
address groupings as well as moving balances between accounts -- these
are fundamentally incompatible features), and since a BIP0032 keystore
is soon planned to be implemented (at which point, "accounts" can
return as HD extended keys). With the keystore handling the grouping
of related keys, there is no reason have many different Account
structs, and the AccountManager has been removed as well. All RPC
handlers that take an account option will only work with "" (the
default account) or "*" if the RPC allows specifying all accounts.
Second, much of the RPC server has been cleaned up. The global
variables for the RPC server and chain server client have been moved
to part of the rpcServer struct, and the handlers for each RPC method
that are looked up change depending on which components have been set.
Passthrough requests are also no longer handled specially, but when
the chain server is set, a handler to perform the passthrough will be
returned if the method is not otherwise a wallet RPC. The
notification system for websocket clients has also been rewritten so
wallet components can send notifications through channels, rather than
requiring direct access to the RPC server itself, or worse still,
sending directly to a websocket client's send channel. In the future,
this will enable proper registration of notifications, rather than
unsolicited broadcasts to every connected websocket client (see
issue #84).
Finally, and the main reason why much of this cleanup was necessary,
the races during intial sync with the chain server have been fixed.
Previously, when the 'Handshake' was run, a rescan would occur which
would perform modifications to Account data structures as
notifications were received. Synchronization was provided with a
single binary semaphore which serialized all access to wallet and
account data. However, the Handshake itself was not able to run with
this lock (or else notifications would block), and many data races
would occur as both notifications were being handled. If GOMAXPROCS
was ever increased beyond 1, btcwallet would always immediately crash
due to invalid addresses caused by the data races on startup. To fix
this, the single lock for all wallet access has been replaced with
mutexes for both the keystore and txstore. Handling of btcd
notifications and client requests may now occur simultaneously.
GOMAXPROCS has also been set to the number of logical CPUs at the
beginning of main, since with the data races fixed, there's no reason
to prevent the extra parallelism gained by increasing it.
Closes #78.
Closes #101.
Closes #110.
2014-07-09 05:17:38 +02:00
|
|
|
}
|
|
|
|
c, err := btcrpcclient.New(&conf, &ntfnCallbacks)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
client.Client = c
|
|
|
|
return &client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Start() error {
|
|
|
|
err := c.Connect(5) // attempt connection 5 tries at most
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the server is running on the expected network.
|
|
|
|
net, err := c.GetCurrentNet()
|
|
|
|
if err != nil {
|
|
|
|
c.Disconnect()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if net != c.netParams.Net {
|
|
|
|
c.Disconnect()
|
|
|
|
return errors.New("mismatched networks")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.quitMtx.Lock()
|
|
|
|
c.started = true
|
|
|
|
c.quitMtx.Unlock()
|
|
|
|
|
|
|
|
c.wg.Add(1)
|
|
|
|
go c.handler()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Stop() {
|
|
|
|
c.quitMtx.Lock()
|
|
|
|
defer c.quitMtx.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-c.quit:
|
|
|
|
default:
|
|
|
|
close(c.quit)
|
|
|
|
c.Client.Shutdown()
|
|
|
|
|
|
|
|
if !c.started {
|
|
|
|
close(c.dequeueNotification)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) WaitForShutdown() {
|
|
|
|
c.Client.WaitForShutdown()
|
|
|
|
c.wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Notifications() <-chan interface{} {
|
|
|
|
return c.dequeueNotification
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) BlockStamp() (*keystore.BlockStamp, error) {
|
|
|
|
select {
|
|
|
|
case bs := <-c.currentBlock:
|
|
|
|
return bs, nil
|
|
|
|
case <-c.quit:
|
|
|
|
return nil, errors.New("disconnected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notification types. These are defined here and processed from from reading
|
|
|
|
// a notificationChan to avoid handling these notifications directly in
|
|
|
|
// btcrpcclient callbacks, which isn't very Go-like and doesn't allow
|
|
|
|
// blocking client calls.
|
|
|
|
type (
|
|
|
|
BlockConnected keystore.BlockStamp
|
|
|
|
BlockDisconnected keystore.BlockStamp
|
|
|
|
RecvTx struct {
|
|
|
|
Tx *btcutil.Tx // Index is guaranteed to be set.
|
|
|
|
Block *txstore.Block // nil if unmined
|
|
|
|
}
|
|
|
|
RedeemingTx struct {
|
|
|
|
Tx *btcutil.Tx // Index is guaranteed to be set.
|
|
|
|
Block *txstore.Block // nil if unmined
|
|
|
|
}
|
|
|
|
RescanProgress struct {
|
|
|
|
Hash *btcwire.ShaHash
|
|
|
|
Height int32
|
|
|
|
Time time.Time
|
|
|
|
}
|
|
|
|
RescanFinished struct {
|
|
|
|
Hash *btcwire.ShaHash
|
|
|
|
Height int32
|
|
|
|
Time time.Time
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// parseBlock parses a btcws definition of the block a tx is mined it to the
|
|
|
|
// Block structure of the txstore package, and the block index. This is done
|
|
|
|
// here since btcrpcclient doesn't parse this nicely for us.
|
|
|
|
func parseBlock(block *btcws.BlockDetails) (blk *txstore.Block, idx int, err error) {
|
|
|
|
if block == nil {
|
|
|
|
return nil, btcutil.TxIndexUnknown, nil
|
|
|
|
}
|
|
|
|
blksha, err := btcwire.NewShaHashFromStr(block.Hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, btcutil.TxIndexUnknown, err
|
|
|
|
}
|
|
|
|
blk = &txstore.Block{
|
|
|
|
Height: block.Height,
|
|
|
|
Hash: *blksha,
|
|
|
|
Time: time.Unix(block.Time, 0),
|
|
|
|
}
|
|
|
|
return blk, block.Index, nil
|
|
|
|
}
|
|
|
|
|
2014-07-28 16:33:00 +02:00
|
|
|
func (c *Client) onClientConnect() {
|
|
|
|
log.Info("Established websocket RPC connection to btcd")
|
2014-07-30 16:47:50 +02:00
|
|
|
c.notifyConnected(true)
|
2014-07-28 16:33:00 +02:00
|
|
|
}
|
|
|
|
|
Remove account support, fix races on btcd connect.
This commit is the result of several big changes being made to the
wallet. In particular, the "handshake" (initial sync to the chain
server) was quite racy and required proper synchronization. To make
fixing this race easier, several other changes were made to the
internal wallet data structures and much of the RPC server ended up
being rewritten.
First, all account support has been removed. The previous Account
struct has been replaced with a Wallet structure, which includes a
keystore for saving keys, and a txstore for storing relevant
transactions. This decision has been made since it is the opinion of
myself and other developers that bitcoind accounts are fundamentally
broken (as accounts implemented by bitcoind support both arbitrary
address groupings as well as moving balances between accounts -- these
are fundamentally incompatible features), and since a BIP0032 keystore
is soon planned to be implemented (at which point, "accounts" can
return as HD extended keys). With the keystore handling the grouping
of related keys, there is no reason have many different Account
structs, and the AccountManager has been removed as well. All RPC
handlers that take an account option will only work with "" (the
default account) or "*" if the RPC allows specifying all accounts.
Second, much of the RPC server has been cleaned up. The global
variables for the RPC server and chain server client have been moved
to part of the rpcServer struct, and the handlers for each RPC method
that are looked up change depending on which components have been set.
Passthrough requests are also no longer handled specially, but when
the chain server is set, a handler to perform the passthrough will be
returned if the method is not otherwise a wallet RPC. The
notification system for websocket clients has also been rewritten so
wallet components can send notifications through channels, rather than
requiring direct access to the RPC server itself, or worse still,
sending directly to a websocket client's send channel. In the future,
this will enable proper registration of notifications, rather than
unsolicited broadcasts to every connected websocket client (see
issue #84).
Finally, and the main reason why much of this cleanup was necessary,
the races during intial sync with the chain server have been fixed.
Previously, when the 'Handshake' was run, a rescan would occur which
would perform modifications to Account data structures as
notifications were received. Synchronization was provided with a
single binary semaphore which serialized all access to wallet and
account data. However, the Handshake itself was not able to run with
this lock (or else notifications would block), and many data races
would occur as both notifications were being handled. If GOMAXPROCS
was ever increased beyond 1, btcwallet would always immediately crash
due to invalid addresses caused by the data races on startup. To fix
this, the single lock for all wallet access has been replaced with
mutexes for both the keystore and txstore. Handling of btcd
notifications and client requests may now occur simultaneously.
GOMAXPROCS has also been set to the number of logical CPUs at the
beginning of main, since with the data races fixed, there's no reason
to prevent the extra parallelism gained by increasing it.
Closes #78.
Closes #101.
Closes #110.
2014-07-09 05:17:38 +02:00
|
|
|
func (c *Client) onBlockConnected(hash *btcwire.ShaHash, height int32) {
|
|
|
|
c.enqueueNotification <- BlockConnected{Hash: hash, Height: height}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) onBlockDisconnected(hash *btcwire.ShaHash, height int32) {
|
|
|
|
c.enqueueNotification <- BlockDisconnected{Hash: hash, Height: height}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) onRecvTx(tx *btcutil.Tx, block *btcws.BlockDetails) {
|
|
|
|
var blk *txstore.Block
|
|
|
|
index := btcutil.TxIndexUnknown
|
|
|
|
if block != nil {
|
|
|
|
var err error
|
|
|
|
blk, index, err = parseBlock(block)
|
|
|
|
if err != nil {
|
|
|
|
// Log and drop improper notification.
|
|
|
|
log.Errorf("recvtx notification bad block: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx.SetIndex(index)
|
|
|
|
c.enqueueNotification <- RecvTx{tx, blk}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) onRedeemingTx(tx *btcutil.Tx, block *btcws.BlockDetails) {
|
|
|
|
var blk *txstore.Block
|
|
|
|
index := btcutil.TxIndexUnknown
|
|
|
|
if block != nil {
|
|
|
|
var err error
|
|
|
|
blk, index, err = parseBlock(block)
|
|
|
|
if err != nil {
|
|
|
|
// Log and drop improper notification.
|
2014-08-14 22:27:14 +02:00
|
|
|
log.Errorf("redeemingtx notification bad block: %v", err)
|
Remove account support, fix races on btcd connect.
This commit is the result of several big changes being made to the
wallet. In particular, the "handshake" (initial sync to the chain
server) was quite racy and required proper synchronization. To make
fixing this race easier, several other changes were made to the
internal wallet data structures and much of the RPC server ended up
being rewritten.
First, all account support has been removed. The previous Account
struct has been replaced with a Wallet structure, which includes a
keystore for saving keys, and a txstore for storing relevant
transactions. This decision has been made since it is the opinion of
myself and other developers that bitcoind accounts are fundamentally
broken (as accounts implemented by bitcoind support both arbitrary
address groupings as well as moving balances between accounts -- these
are fundamentally incompatible features), and since a BIP0032 keystore
is soon planned to be implemented (at which point, "accounts" can
return as HD extended keys). With the keystore handling the grouping
of related keys, there is no reason have many different Account
structs, and the AccountManager has been removed as well. All RPC
handlers that take an account option will only work with "" (the
default account) or "*" if the RPC allows specifying all accounts.
Second, much of the RPC server has been cleaned up. The global
variables for the RPC server and chain server client have been moved
to part of the rpcServer struct, and the handlers for each RPC method
that are looked up change depending on which components have been set.
Passthrough requests are also no longer handled specially, but when
the chain server is set, a handler to perform the passthrough will be
returned if the method is not otherwise a wallet RPC. The
notification system for websocket clients has also been rewritten so
wallet components can send notifications through channels, rather than
requiring direct access to the RPC server itself, or worse still,
sending directly to a websocket client's send channel. In the future,
this will enable proper registration of notifications, rather than
unsolicited broadcasts to every connected websocket client (see
issue #84).
Finally, and the main reason why much of this cleanup was necessary,
the races during intial sync with the chain server have been fixed.
Previously, when the 'Handshake' was run, a rescan would occur which
would perform modifications to Account data structures as
notifications were received. Synchronization was provided with a
single binary semaphore which serialized all access to wallet and
account data. However, the Handshake itself was not able to run with
this lock (or else notifications would block), and many data races
would occur as both notifications were being handled. If GOMAXPROCS
was ever increased beyond 1, btcwallet would always immediately crash
due to invalid addresses caused by the data races on startup. To fix
this, the single lock for all wallet access has been replaced with
mutexes for both the keystore and txstore. Handling of btcd
notifications and client requests may now occur simultaneously.
GOMAXPROCS has also been set to the number of logical CPUs at the
beginning of main, since with the data races fixed, there's no reason
to prevent the extra parallelism gained by increasing it.
Closes #78.
Closes #101.
Closes #110.
2014-07-09 05:17:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx.SetIndex(index)
|
|
|
|
c.enqueueNotification <- RedeemingTx{tx, blk}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) onRescanProgress(hash *btcwire.ShaHash, height int32, blkTime time.Time) {
|
|
|
|
c.enqueueNotification <- &RescanProgress{hash, height, blkTime}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) onRescanFinished(hash *btcwire.ShaHash, height int32, blkTime time.Time) {
|
|
|
|
c.enqueueNotification <- &RescanFinished{hash, height, blkTime}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handler maintains a queue of notifications and the current state (best
|
|
|
|
// block) of the chain.
|
|
|
|
func (c *Client) handler() {
|
|
|
|
hash, height, err := c.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
close(c.quit)
|
|
|
|
c.wg.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
bs := &keystore.BlockStamp{Hash: hash, Height: height}
|
|
|
|
|
|
|
|
// TODO: Rather than leaving this as an unbounded queue for all types of
|
|
|
|
// notifications, try dropping ones where a later enqueued notification
|
|
|
|
// can fully invalidate one waiting to be processed. For example,
|
|
|
|
// blockconnected notifications for greater block heights can remove the
|
|
|
|
// need to process earlier blockconnected notifications still waiting
|
|
|
|
// here.
|
|
|
|
|
|
|
|
var notifications []interface{}
|
|
|
|
enqueue := c.enqueueNotification
|
|
|
|
var dequeue chan interface{}
|
|
|
|
var next interface{}
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case n, ok := <-enqueue:
|
|
|
|
if !ok {
|
|
|
|
// If no notifications are queued for handling,
|
|
|
|
// the queue is finished.
|
|
|
|
if len(notifications) == 0 {
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
// nil channel so no more reads can occur.
|
|
|
|
enqueue = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(notifications) == 0 {
|
|
|
|
next = n
|
|
|
|
dequeue = c.dequeueNotification
|
|
|
|
}
|
|
|
|
notifications = append(notifications, n)
|
|
|
|
|
|
|
|
case dequeue <- next:
|
|
|
|
if n, ok := next.(BlockConnected); ok {
|
|
|
|
bs = (*keystore.BlockStamp)(&n)
|
|
|
|
}
|
|
|
|
|
|
|
|
notifications[0] = nil
|
|
|
|
notifications = notifications[1:]
|
|
|
|
if len(notifications) != 0 {
|
|
|
|
next = notifications[0]
|
|
|
|
} else {
|
|
|
|
// If no more notifications can be enqueued, the
|
|
|
|
// queue is finished.
|
|
|
|
if enqueue == nil {
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
dequeue = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
case c.currentBlock <- bs:
|
|
|
|
|
|
|
|
case <-c.quit:
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(c.dequeueNotification)
|
|
|
|
c.wg.Done()
|
|
|
|
}
|
2014-07-30 16:47:50 +02:00
|
|
|
|
|
|
|
// ErrDuplicateListen is returned for any attempts to listen for the same
|
|
|
|
// notification more than once. If callers must pass along a notifiation to
|
|
|
|
// multiple places, they must broadcast it themself.
|
|
|
|
var ErrDuplicateListen = errors.New("duplicate listen")
|
|
|
|
|
|
|
|
type noopLocker struct{}
|
|
|
|
|
|
|
|
func (noopLocker) Lock() {}
|
|
|
|
func (noopLocker) Unlock() {}
|
|
|
|
|
|
|
|
// ListenConnected returns a channel that passes the current connection state
|
|
|
|
// of the client. This will be automatically sent to when the client is first
|
|
|
|
// connected, as well as the current state whenever NotifyConnected is
|
|
|
|
// forcibly called.
|
|
|
|
//
|
|
|
|
// If this is called twice, ErrDuplicateListen is returned.
|
|
|
|
func (c *Client) ListenConnected() (<-chan bool, error) {
|
|
|
|
c.notificationLock.Lock()
|
|
|
|
defer c.notificationLock.Unlock()
|
|
|
|
|
|
|
|
if c.connected != nil {
|
|
|
|
return nil, ErrDuplicateListen
|
|
|
|
}
|
|
|
|
c.connected = make(chan bool)
|
|
|
|
c.notificationLock = noopLocker{}
|
|
|
|
return c.connected, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) notifyConnected(connected bool) {
|
|
|
|
c.notificationLock.Lock()
|
|
|
|
if c.connected != nil {
|
|
|
|
c.connected <- connected
|
|
|
|
}
|
|
|
|
c.notificationLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyConnected sends the channel notification for a connected or
|
|
|
|
// disconnected client. This is exported so it can be called by other
|
|
|
|
// packages which require notifying the current connection state.
|
|
|
|
//
|
|
|
|
// TODO: This shouldn't exist, but the current notification API requires it.
|
|
|
|
func (c *Client) NotifyConnected() {
|
|
|
|
connected := !c.Client.Disconnected()
|
|
|
|
c.notifyConnected(connected)
|
|
|
|
}
|