2013-08-21 16:37:30 +02:00
|
|
|
/*
|
2014-01-09 20:12:20 +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.
|
|
|
|
*/
|
|
|
|
|
2014-07-08 20:17:24 +02:00
|
|
|
package keystore
|
2013-08-21 16:37:30 +02:00
|
|
|
|
|
|
|
import (
|
2014-01-16 17:50:08 +01:00
|
|
|
"bytes"
|
2013-09-03 06:10:32 +02:00
|
|
|
"crypto/rand"
|
2014-05-23 04:16:50 +02:00
|
|
|
"math/big"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2014-01-16 17:50:08 +01:00
|
|
|
"github.com/conformal/btcec"
|
2014-05-23 04:16:50 +02:00
|
|
|
"github.com/conformal/btcnet"
|
2014-03-13 20:13:39 +01:00
|
|
|
"github.com/conformal/btcscript"
|
2014-01-20 18:56:27 +01:00
|
|
|
"github.com/conformal/btcutil"
|
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/btcwire"
|
2014-05-23 04:16:50 +02:00
|
|
|
|
2013-08-21 16:37:30 +02:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
const dummyDir = ""
|
|
|
|
|
2014-05-27 19:50:51 +02:00
|
|
|
var tstNetParams = &btcnet.MainNetParams
|
2013-09-03 06:10:32 +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 makeBS(height int32) *BlockStamp {
|
|
|
|
return &BlockStamp{
|
|
|
|
Hash: new(btcwire.ShaHash),
|
|
|
|
Height: height,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-21 16:37:30 +02:00
|
|
|
func TestBtcAddressSerializer(t *testing.T) {
|
2014-07-08 20:17:24 +02:00
|
|
|
fakeWallet := &Store{net: (*netParams)(tstNetParams)}
|
2013-09-03 06:10:32 +02:00
|
|
|
kdfp := &kdfParameters{
|
|
|
|
mem: 1024,
|
|
|
|
nIter: 5,
|
|
|
|
}
|
2013-11-15 18:03:52 +01:00
|
|
|
if _, err := rand.Read(kdfp.salt[:]); err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
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
|
|
|
key := kdf([]byte("banana"), kdfp)
|
2013-09-03 06:10:32 +02:00
|
|
|
privKey := make([]byte, 32)
|
2013-11-15 18:03:52 +01:00
|
|
|
if _, err := rand.Read(privKey); err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2014-04-09 02:18:52 +02:00
|
|
|
addr, err := newBtcAddress(fakeWallet, privKey, nil,
|
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
|
|
|
makeBS(0), true)
|
2013-09-03 06:10:32 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = addr.encrypt(key)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
2013-08-21 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
2014-03-14 16:22:37 +01:00
|
|
|
buf := new(bytes.Buffer)
|
2013-08-21 16:37:30 +02:00
|
|
|
|
2014-03-14 16:22:37 +01:00
|
|
|
if _, err := addr.WriteTo(buf); err != nil {
|
2013-08-21 16:37:30 +02:00
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var readAddr btcAddress
|
2014-07-08 20:17:24 +02:00
|
|
|
readAddr.store = fakeWallet
|
2014-03-14 16:22:37 +01:00
|
|
|
_, err = readAddr.ReadFrom(buf)
|
2013-08-21 16:37:30 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-11-11 21:30:50 +01:00
|
|
|
if _, err = readAddr.unlock(key); err != nil {
|
2013-09-03 06:10:32 +02:00
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(addr, &readAddr) {
|
2013-08-21 16:37:30 +02:00
|
|
|
t.Error("Original and read btcAddress differ.")
|
|
|
|
}
|
|
|
|
}
|
2013-09-03 06:10:32 +02:00
|
|
|
|
2014-03-13 20:13:39 +01:00
|
|
|
func TestScriptAddressSerializer(t *testing.T) {
|
2014-07-08 20:17:24 +02:00
|
|
|
fakeWallet := &Store{net: (*netParams)(tstNetParams)}
|
2014-03-13 20:13:39 +01:00
|
|
|
script := []byte{btcscript.OP_TRUE, btcscript.OP_DUP,
|
|
|
|
btcscript.OP_DROP}
|
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
|
|
|
addr, err := newScriptAddress(fakeWallet, script, makeBS(0))
|
2014-03-13 20:13:39 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-14 16:22:37 +01:00
|
|
|
buf := new(bytes.Buffer)
|
2014-03-13 20:13:39 +01:00
|
|
|
|
2014-03-14 16:22:37 +01:00
|
|
|
if _, err := addr.WriteTo(buf); err != nil {
|
2014-03-13 20:13:39 +01:00
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var readAddr scriptAddress
|
2014-07-08 20:17:24 +02:00
|
|
|
readAddr.store = fakeWallet
|
2014-03-14 16:22:37 +01:00
|
|
|
_, err = readAddr.ReadFrom(buf)
|
2014-03-13 20:13:39 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(addr, &readAddr) {
|
|
|
|
t.Error("Original and read btcAddress differ.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 06:10:32 +02:00
|
|
|
func TestWalletCreationSerialization(t *testing.T) {
|
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
|
|
|
createdAt := makeBS(0)
|
|
|
|
w1, err := New(dummyDir, "A wallet for testing.",
|
|
|
|
[]byte("banana"), tstNetParams, createdAt)
|
2013-09-03 06:10:32 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Error creating new wallet: " + err.Error())
|
2014-01-06 18:24:29 +01:00
|
|
|
return
|
2013-09-03 06:10:32 +02:00
|
|
|
}
|
|
|
|
|
2014-03-14 16:22:37 +01:00
|
|
|
buf := new(bytes.Buffer)
|
2013-09-03 06:10:32 +02:00
|
|
|
|
2014-03-14 16:22:37 +01:00
|
|
|
if _, err := w1.WriteTo(buf); err != nil {
|
2013-09-03 06:10:32 +02:00
|
|
|
t.Error("Error writing new wallet: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-08 20:17:24 +02:00
|
|
|
w2 := new(Store)
|
2014-03-14 16:22:37 +01:00
|
|
|
_, err = w2.ReadFrom(buf)
|
2013-09-03 06:10:32 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Error reading newly written wallet: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w1.Lock()
|
|
|
|
w2.Lock()
|
|
|
|
|
|
|
|
if err = w1.Unlock([]byte("banana")); err != nil {
|
|
|
|
t.Error("Decrypting original wallet failed: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = w2.Unlock([]byte("banana")); err != nil {
|
|
|
|
t.Error("Decrypting newly read wallet failed: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-09 02:18:52 +02:00
|
|
|
// if !reflect.DeepEqual(w1, w2) {
|
|
|
|
// t.Error("Created and read-in wallets do not match.")
|
|
|
|
// spew.Dump(w1, w2)
|
|
|
|
// return
|
|
|
|
// }
|
2013-09-03 06:10:32 +02:00
|
|
|
}
|
2014-01-16 17:50:08 +01:00
|
|
|
|
|
|
|
func TestChaining(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
cc []byte
|
|
|
|
origPrivateKey []byte
|
|
|
|
nextPrivateKeyUncompressed []byte
|
|
|
|
nextPrivateKeyCompressed []byte
|
|
|
|
nextPublicKeyUncompressed []byte
|
|
|
|
nextPublicKeyCompressed []byte
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "chaintest 1",
|
|
|
|
cc: []byte("3318959fff419ab8b556facb3c429a86"),
|
|
|
|
origPrivateKey: []byte("5ffc975976eaaa1f7b179f384ebbc053"),
|
|
|
|
nextPrivateKeyUncompressed: []byte{
|
|
|
|
0xd3, 0xfe, 0x2e, 0x96, 0x44, 0x12, 0x2d, 0xaa,
|
|
|
|
0x80, 0x8e, 0x36, 0x17, 0xb5, 0x9f, 0x8c, 0xd2,
|
|
|
|
0x72, 0x8c, 0xaf, 0xf1, 0xdb, 0xd6, 0x4a, 0x92,
|
|
|
|
0xd7, 0xc7, 0xee, 0x2b, 0x56, 0x34, 0xe2, 0x87,
|
|
|
|
},
|
|
|
|
nextPrivateKeyCompressed: []byte{
|
|
|
|
0x08, 0x56, 0x7a, 0x1b, 0x89, 0x56, 0x2e, 0xfa,
|
|
|
|
0xb4, 0x02, 0x59, 0x69, 0x10, 0xc3, 0x60, 0x1f,
|
|
|
|
0x34, 0xf0, 0x55, 0x02, 0x8a, 0xbf, 0x37, 0xf5,
|
|
|
|
0x22, 0x80, 0x9f, 0xd2, 0xe5, 0x42, 0x5b, 0x2d,
|
|
|
|
},
|
|
|
|
nextPublicKeyUncompressed: []byte{
|
|
|
|
0x04, 0xdd, 0x70, 0x31, 0xa5, 0xf9, 0x06, 0x70,
|
|
|
|
0xd3, 0x9a, 0x24, 0x5b, 0xd5, 0x73, 0xdd, 0xb6,
|
|
|
|
0x15, 0x81, 0x0b, 0x78, 0x19, 0xbc, 0xc8, 0x26,
|
|
|
|
0xc9, 0x16, 0x86, 0x73, 0xae, 0xe4, 0xc0, 0xed,
|
|
|
|
0x39, 0x81, 0xb4, 0x86, 0x2d, 0x19, 0x8c, 0x67,
|
|
|
|
0x9c, 0x93, 0x99, 0xf6, 0xd2, 0x3f, 0xd1, 0x53,
|
|
|
|
0x9e, 0xed, 0xbd, 0x07, 0xd6, 0x4f, 0xa9, 0x81,
|
|
|
|
0x61, 0x85, 0x46, 0x84, 0xb1, 0xa0, 0xed, 0xbc,
|
|
|
|
0xa7,
|
|
|
|
},
|
|
|
|
nextPublicKeyCompressed: []byte{
|
|
|
|
0x02, 0x2c, 0x48, 0x73, 0x37, 0x35, 0x74, 0x7f,
|
|
|
|
0x05, 0x58, 0xc1, 0x4e, 0x0d, 0x18, 0xc2, 0xbf,
|
|
|
|
0xcc, 0x83, 0xa2, 0x4d, 0x64, 0xab, 0xba, 0xea,
|
|
|
|
0xeb, 0x4c, 0xcd, 0x4c, 0x0c, 0x21, 0xc4, 0x30,
|
|
|
|
0x0f,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
// Create both uncompressed and compressed public keys for original
|
|
|
|
// private key.
|
|
|
|
origPubUncompressed := pubkeyFromPrivkey(test.origPrivateKey, false)
|
|
|
|
origPubCompressed := pubkeyFromPrivkey(test.origPrivateKey, true)
|
|
|
|
|
|
|
|
// Create next chained private keys, chained from both the uncompressed
|
|
|
|
// and compressed pubkeys.
|
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
|
|
|
nextPrivUncompressed, err := chainedPrivKey(test.origPrivateKey,
|
2014-01-16 17:50:08 +01:00
|
|
|
origPubUncompressed, test.cc)
|
|
|
|
if err != nil {
|
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
|
|
|
t.Errorf("%s: Uncompressed chainedPrivKey failed: %v", test.name, err)
|
2014-01-16 17:50:08 +01:00
|
|
|
return
|
|
|
|
}
|
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
|
|
|
nextPrivCompressed, err := chainedPrivKey(test.origPrivateKey,
|
2014-01-16 17:50:08 +01:00
|
|
|
origPubCompressed, test.cc)
|
|
|
|
if err != nil {
|
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
|
|
|
t.Errorf("%s: Compressed chainedPrivKey failed: %v", test.name, err)
|
2014-01-16 17:50:08 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the new private keys match the expected values
|
|
|
|
// in the test case.
|
|
|
|
if !bytes.Equal(nextPrivUncompressed, test.nextPrivateKeyUncompressed) {
|
|
|
|
t.Errorf("%s: Next private key (from uncompressed pubkey) does not match expected.\nGot: %s\nExpected: %s",
|
|
|
|
test.name, spew.Sdump(nextPrivUncompressed), spew.Sdump(test.nextPrivateKeyUncompressed))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !bytes.Equal(nextPrivCompressed, test.nextPrivateKeyCompressed) {
|
|
|
|
t.Errorf("%s: Next private key (from compressed pubkey) does not match expected.\nGot: %s\nExpected: %s",
|
|
|
|
test.name, spew.Sdump(nextPrivCompressed), spew.Sdump(test.nextPrivateKeyCompressed))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the next pubkeys generated from the next private keys.
|
|
|
|
nextPubUncompressedFromPriv := pubkeyFromPrivkey(nextPrivUncompressed, false)
|
|
|
|
nextPubCompressedFromPriv := pubkeyFromPrivkey(nextPrivCompressed, true)
|
|
|
|
|
|
|
|
// Create the next pubkeys by chaining directly off the original
|
|
|
|
// pubkeys (without using the original's private key).
|
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
|
|
|
nextPubUncompressedFromPub, err := chainedPubKey(origPubUncompressed, test.cc)
|
2014-01-16 17:50:08 +01:00
|
|
|
if err != nil {
|
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
|
|
|
t.Errorf("%s: Uncompressed chainedPubKey failed: %v", test.name, err)
|
2014-01-16 17:50:08 +01:00
|
|
|
return
|
|
|
|
}
|
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
|
|
|
nextPubCompressedFromPub, err := chainedPubKey(origPubCompressed, test.cc)
|
2014-01-16 17:50:08 +01:00
|
|
|
if err != nil {
|
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
|
|
|
t.Errorf("%s: Compressed chainedPubKey failed: %v", test.name, err)
|
2014-01-16 17:50:08 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Public keys (used to generate the bitcoin address) MUST match.
|
|
|
|
if !bytes.Equal(nextPubUncompressedFromPriv, nextPubUncompressedFromPub) {
|
|
|
|
t.Errorf("%s: Uncompressed public keys do not match.", test.name)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(nextPubCompressedFromPriv, nextPubCompressedFromPub) {
|
|
|
|
t.Errorf("%s: Compressed public keys do not match.", test.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that all generated public keys match the expected
|
|
|
|
// values in the test case.
|
|
|
|
if !bytes.Equal(nextPubUncompressedFromPub, test.nextPublicKeyUncompressed) {
|
|
|
|
t.Errorf("%s: Next uncompressed public keys do not match expected value.\nGot: %s\nExpected: %s",
|
|
|
|
test.name, spew.Sdump(nextPubUncompressedFromPub), spew.Sdump(test.nextPublicKeyUncompressed))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !bytes.Equal(nextPubCompressedFromPub, test.nextPublicKeyCompressed) {
|
|
|
|
t.Errorf("%s: Next compressed public keys do not match expected value.\nGot: %s\nExpected: %s",
|
|
|
|
test.name, spew.Sdump(nextPubCompressedFromPub), spew.Sdump(test.nextPublicKeyCompressed))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign data with the next private keys and verify signature with
|
|
|
|
// the next pubkeys.
|
|
|
|
pubkeyUncompressed, err := btcec.ParsePubKey(nextPubUncompressedFromPub, btcec.S256())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: Unable to parse next uncompressed pubkey: %v", test.name, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pubkeyCompressed, err := btcec.ParsePubKey(nextPubCompressedFromPub, btcec.S256())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: Unable to parse next compressed pubkey: %v", test.name, err)
|
|
|
|
return
|
|
|
|
}
|
2014-09-27 20:59:18 +02:00
|
|
|
privkeyUncompressed := &btcec.PrivateKey{
|
2014-04-09 02:18:52 +02:00
|
|
|
PublicKey: *pubkeyUncompressed.ToECDSA(),
|
2014-01-16 17:50:08 +01:00
|
|
|
D: new(big.Int).SetBytes(nextPrivUncompressed),
|
|
|
|
}
|
2014-09-27 20:59:18 +02:00
|
|
|
privkeyCompressed := &btcec.PrivateKey{
|
2014-04-09 02:18:52 +02:00
|
|
|
PublicKey: *pubkeyCompressed.ToECDSA(),
|
2014-01-16 17:50:08 +01:00
|
|
|
D: new(big.Int).SetBytes(nextPrivCompressed),
|
|
|
|
}
|
|
|
|
data := "String to sign."
|
2014-09-27 20:59:18 +02:00
|
|
|
sig, err := privkeyUncompressed.Sign([]byte(data))
|
2014-01-16 17:50:08 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: Unable to sign data with next private key (chained from uncompressed pubkey): %v",
|
|
|
|
test.name, err)
|
|
|
|
return
|
|
|
|
}
|
2014-09-27 20:59:18 +02:00
|
|
|
ok := sig.Verify([]byte(data), privkeyUncompressed.PubKey())
|
2014-01-16 17:50:08 +01:00
|
|
|
if !ok {
|
2014-09-27 20:59:18 +02:00
|
|
|
t.Errorf("%s: btcec signature verification failed for next keypair (chained from uncompressed pubkey).",
|
2014-01-16 17:50:08 +01:00
|
|
|
test.name)
|
|
|
|
return
|
|
|
|
}
|
2014-09-27 20:59:18 +02:00
|
|
|
sig, err = privkeyCompressed.Sign([]byte(data))
|
2014-01-16 17:50:08 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: Unable to sign data with next private key (chained from compressed pubkey): %v",
|
|
|
|
test.name, err)
|
|
|
|
return
|
|
|
|
}
|
2014-09-27 20:59:18 +02:00
|
|
|
ok = sig.Verify([]byte(data), privkeyCompressed.PubKey())
|
2014-01-16 17:50:08 +01:00
|
|
|
if !ok {
|
2014-09-27 20:59:18 +02:00
|
|
|
t.Errorf("%s: btcec signature verification failed for next keypair (chained from compressed pubkey).",
|
2014-01-16 17:50:08 +01:00
|
|
|
test.name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-17 01:28:34 +01:00
|
|
|
|
|
|
|
func TestWalletPubkeyChaining(t *testing.T) {
|
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
|
|
|
w, err := New(dummyDir, "A wallet for testing.",
|
|
|
|
[]byte("banana"), tstNetParams, makeBS(0))
|
2014-01-17 01:28:34 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Error creating new wallet: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !w.IsLocked() {
|
|
|
|
t.Error("New wallet is not locked.")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// Get next chained address. The wallet is locked, so this will chain
|
|
|
|
// off the last pubkey, not privkey.
|
|
|
|
addrWithoutPrivkey, err := w.NextChainedAddress(makeBS(0))
|
2014-01-17 01:28:34 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to extend address chain from pubkey: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup address info. This should succeed even without the private
|
|
|
|
// key available.
|
2014-04-09 02:18:52 +02:00
|
|
|
info, err := w.Address(addrWithoutPrivkey)
|
2014-01-17 01:28:34 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed to get info about address without private key: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-03-06 01:34:44 +01:00
|
|
|
|
2014-04-09 02:18:52 +02:00
|
|
|
pkinfo := info.(PubKeyAddress)
|
2014-01-17 01:28:34 +01:00
|
|
|
// sanity checks
|
2014-03-06 01:34:44 +01:00
|
|
|
if !info.Compressed() {
|
2014-01-17 01:28:34 +01:00
|
|
|
t.Errorf("Pubkey should be compressed.")
|
|
|
|
return
|
|
|
|
}
|
2014-03-06 01:34:44 +01:00
|
|
|
if info.Imported() {
|
2014-01-17 01:28:34 +01:00
|
|
|
t.Errorf("Should not be marked as imported.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-09 02:18:52 +02:00
|
|
|
pka := info.(PubKeyAddress)
|
|
|
|
|
2014-01-17 01:28:34 +01:00
|
|
|
// Try to lookup it's private key. This should fail.
|
2014-04-09 02:18:52 +02:00
|
|
|
_, err = pka.PrivKey()
|
2014-01-17 01:28:34 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Incorrectly returned nil error for looking up private key for address without one saved.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize w and serialize into a new wallet. The rest of the checks
|
|
|
|
// in this test test against both a fresh, as well as an "opened and closed"
|
|
|
|
// wallet with the missing private key.
|
|
|
|
serializedWallet := new(bytes.Buffer)
|
|
|
|
_, err = w.WriteTo(serializedWallet)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error writing wallet with missing private key: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-07-08 20:17:24 +02:00
|
|
|
w2 := new(Store)
|
2014-01-17 01:28:34 +01:00
|
|
|
_, err = w2.ReadFrom(serializedWallet)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Error reading wallet with missing private key: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock wallet. This should trigger creating the private key for
|
|
|
|
// the address.
|
|
|
|
if err = w.Unlock([]byte("banana")); err != nil {
|
|
|
|
t.Errorf("Can't unlock original wallet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = w2.Unlock([]byte("banana")); err != nil {
|
|
|
|
t.Errorf("Can't unlock re-read wallet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same address, better variable name.
|
|
|
|
addrWithPrivKey := addrWithoutPrivkey
|
|
|
|
|
|
|
|
// Try a private key lookup again. The private key should now be available.
|
2014-04-09 02:18:52 +02:00
|
|
|
key1, err := pka.PrivKey()
|
2014-01-17 01:28:34 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Private key for original wallet was not created! %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-04-09 02:18:52 +02:00
|
|
|
|
|
|
|
info2, err := w.Address(addrWithPrivKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("no address in re-read wallet")
|
|
|
|
}
|
|
|
|
pka2 := info2.(PubKeyAddress)
|
|
|
|
key2, err := pka2.PrivKey()
|
2014-01-17 01:28:34 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Private key for re-read wallet was not created! %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keys returned by both wallets must match.
|
|
|
|
if !reflect.DeepEqual(key1, key2) {
|
|
|
|
t.Errorf("Private keys for address originally created without one mismtach between original and re-read wallet.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign some data with the private key, then verify signature with the pubkey.
|
|
|
|
hash := []byte("hash to sign")
|
2014-09-27 20:59:18 +02:00
|
|
|
sig, err := key1.Sign(hash)
|
2014-01-17 01:28:34 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unable to sign hash with the created private key: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-04-09 02:18:52 +02:00
|
|
|
pubKey := pkinfo.PubKey()
|
2014-09-27 20:59:18 +02:00
|
|
|
ok := sig.Verify(hash, pubKey)
|
2014-01-17 01:28:34 +01:00
|
|
|
if !ok {
|
2014-09-27 20:59:18 +02:00
|
|
|
t.Errorf("btcec signature verification failed; address's pubkey mismatches the privkey.")
|
2014-01-17 01:28:34 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
|
|
nextAddr, err := w.NextChainedAddress(makeBS(0))
|
2014-01-17 01:28:34 +01:00
|
|
|
if err != nil {
|
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
|
|
|
t.Errorf("Unable to create next address after finding the privkey: %v", err)
|
2014-01-17 01:28:34 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-09 02:18:52 +02:00
|
|
|
nextInfo, err := w.Address(nextAddr)
|
2014-01-17 01:28:34 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Couldn't get info about the next address in the chain: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-04-09 02:18:52 +02:00
|
|
|
nextPkInfo := nextInfo.(PubKeyAddress)
|
|
|
|
nextKey, err := nextPkInfo.PrivKey()
|
2014-01-17 01:28:34 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Couldn't get private key for the next address in the chain: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-27 20:59:18 +02:00
|
|
|
// Do a signature check here as well, this time for the next
|
2014-01-17 01:28:34 +01:00
|
|
|
// address after the one made without the private key.
|
2014-09-27 20:59:18 +02:00
|
|
|
sig, err = nextKey.Sign(hash)
|
2014-01-17 01:28:34 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unable to sign hash with the created private key: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-04-09 02:18:52 +02:00
|
|
|
pubKey = nextPkInfo.PubKey()
|
2014-09-27 20:59:18 +02:00
|
|
|
ok = sig.Verify(hash, pubKey)
|
2014-01-17 01:28:34 +01:00
|
|
|
if !ok {
|
2014-09-27 20:59:18 +02:00
|
|
|
t.Errorf("btcec signature verification failed; next address's keypair does not match.")
|
2014-01-17 01:28:34 +01:00
|
|
|
return
|
|
|
|
}
|
2014-02-04 16:37:28 +01:00
|
|
|
|
|
|
|
// Check that the serialized wallet correctly unmarked the 'needs private
|
|
|
|
// keys later' flag.
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
w2.WriteTo(buf)
|
|
|
|
w2.ReadFrom(buf)
|
|
|
|
err = w2.Unlock([]byte("banana"))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unlock after serialize/deserialize failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-01-17 01:28:34 +01:00
|
|
|
}
|
2014-01-20 18:56:27 +01:00
|
|
|
|
|
|
|
func TestWatchingWalletExport(t *testing.T) {
|
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
|
|
|
createdAt := makeBS(0)
|
|
|
|
w, err := New(dummyDir, "A wallet for testing.",
|
|
|
|
[]byte("banana"), tstNetParams, createdAt)
|
2014-01-20 18:56:27 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Error creating new wallet: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maintain a set of the active addresses in the wallet.
|
2014-03-11 02:28:40 +01:00
|
|
|
activeAddrs := make(map[addressKey]struct{})
|
2014-01-20 18:56:27 +01:00
|
|
|
|
|
|
|
// Add root address.
|
2014-03-11 02:28:40 +01:00
|
|
|
activeAddrs[getAddressKey(w.LastChainedAddress())] = struct{}{}
|
2014-01-20 18:56:27 +01:00
|
|
|
|
|
|
|
// Create watching wallet from w.
|
|
|
|
ww, err := w.ExportWatchingWallet()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Could not create watching wallet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify correctness of wallet flags.
|
|
|
|
if ww.flags.useEncryption {
|
|
|
|
t.Errorf("Watching wallet marked as using encryption (but nothing to encrypt).")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !ww.flags.watchingOnly {
|
|
|
|
t.Errorf("Wallet should be watching-only but is not marked so.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that all flags are set as expected.
|
|
|
|
if ww.keyGenerator.flags.encrypted {
|
|
|
|
t.Errorf("Watching root address should not be encrypted (nothing to encrypt)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ww.keyGenerator.flags.hasPrivKey {
|
|
|
|
t.Errorf("Watching root address marked as having a private key.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !ww.keyGenerator.flags.hasPubKey {
|
|
|
|
t.Errorf("Watching root address marked as missing a public key.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ww.keyGenerator.flags.createPrivKeyNextUnlock {
|
|
|
|
t.Errorf("Watching root address marked as needing a private key to be generated later.")
|
|
|
|
return
|
|
|
|
}
|
2014-03-11 02:28:40 +01:00
|
|
|
for apkh, waddr := range ww.addrMap {
|
|
|
|
switch addr := waddr.(type) {
|
|
|
|
case *btcAddress:
|
|
|
|
if addr.flags.encrypted {
|
|
|
|
t.Errorf("Chained address should not be encrypted (nothing to encrypt)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if addr.flags.hasPrivKey {
|
|
|
|
t.Errorf("Chained address marked as having a private key.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !addr.flags.hasPubKey {
|
|
|
|
t.Errorf("Chained address marked as missing a public key.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if addr.flags.createPrivKeyNextUnlock {
|
|
|
|
t.Errorf("Chained address marked as needing a private key to be generated later.")
|
|
|
|
return
|
|
|
|
}
|
2014-03-13 20:13:39 +01:00
|
|
|
case *scriptAddress:
|
|
|
|
t.Errorf("Chained address was a script!")
|
|
|
|
return
|
2014-03-11 02:28:40 +01:00
|
|
|
default:
|
|
|
|
t.Errorf("Chained address unknown type!")
|
2014-01-20 18:56:27 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := activeAddrs[apkh]; !ok {
|
|
|
|
t.Errorf("Address from watching wallet not found in original wallet.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delete(activeAddrs, apkh)
|
|
|
|
}
|
|
|
|
if len(activeAddrs) != 0 {
|
|
|
|
t.Errorf("%v address(es) were not exported to watching wallet.", len(activeAddrs))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the new addresses created by each wallet match. The
|
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
|
|
|
// original wallet is unlocked so addresses are chained with privkeys.
|
2014-01-20 18:56:27 +01:00
|
|
|
if err := w.Unlock([]byte("banana")); err != nil {
|
|
|
|
t.Errorf("Unlocking original wallet failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-01-21 17:19:08 +01:00
|
|
|
// Test that ExtendActiveAddresses for the watching wallet match
|
|
|
|
// manually requested addresses of the original wallet.
|
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
|
|
|
var newAddrs []btcutil.Address
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
addr, err := w.NextChainedAddress(createdAt)
|
2014-01-21 17:19:08 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Cannot get next chained address for original wallet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
newAddrs = append(newAddrs, addr)
|
|
|
|
}
|
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
|
|
|
newWWAddrs, err := ww.ExtendActiveAddresses(10)
|
2014-01-21 17:19:08 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Cannot extend active addresses for watching wallet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := range newAddrs {
|
|
|
|
if newAddrs[i].EncodeAddress() != newWWAddrs[i].EncodeAddress() {
|
|
|
|
t.Errorf("Extended active addresses do not match manually requested addresses.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test ExtendActiveAddresses for the original wallet after manually
|
|
|
|
// requesting addresses for the watching wallet.
|
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
|
|
|
newWWAddrs = nil
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
addr, err := ww.NextChainedAddress(createdAt)
|
2014-01-21 17:19:08 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Cannot get next chained address for watching wallet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
newWWAddrs = append(newWWAddrs, addr)
|
|
|
|
}
|
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
|
|
|
newAddrs, err = w.ExtendActiveAddresses(10)
|
2014-01-21 17:19:08 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Cannot extend active addresses for original wallet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := range newAddrs {
|
|
|
|
if newAddrs[i].EncodeAddress() != newWWAddrs[i].EncodeAddress() {
|
|
|
|
t.Errorf("Extended active addresses do not match manually requested addresses.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-20 18:56:27 +01:00
|
|
|
// Test (de)serialization of watching wallet.
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
_, err = ww.WriteTo(buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Cannot write watching wallet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-07-08 20:17:24 +02:00
|
|
|
ww2 := new(Store)
|
2014-01-20 18:56:27 +01:00
|
|
|
_, err = ww2.ReadFrom(buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Cannot read watching wallet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that (de)serialized watching wallet matches the exported wallet.
|
|
|
|
if !reflect.DeepEqual(ww, ww2) {
|
|
|
|
t.Error("Exported and read-in watching wallets do not match.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that nonsensical functions fail with correct error.
|
2014-07-08 20:17:24 +02:00
|
|
|
if err := ww.Lock(); err != ErrWatchingOnly {
|
2014-01-20 18:56:27 +01:00
|
|
|
t.Errorf("Nonsensical func Lock returned no or incorrect error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-07-08 20:17:24 +02:00
|
|
|
if err := ww.Unlock([]byte("banana")); err != ErrWatchingOnly {
|
2014-01-20 18:56:27 +01:00
|
|
|
t.Errorf("Nonsensical func Unlock returned no or incorrect error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-04-09 02:18:52 +02:00
|
|
|
generator, err := ww.Address(w.keyGenerator.Address())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("generator isnt' present in wallet")
|
|
|
|
}
|
|
|
|
gpk := generator.(PubKeyAddress)
|
2014-07-08 20:17:24 +02:00
|
|
|
if _, err := gpk.PrivKey(); err != ErrWatchingOnly {
|
2014-01-20 18:56:27 +01:00
|
|
|
t.Errorf("Nonsensical func AddressKey returned no or incorrect error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-07-08 20:17:24 +02:00
|
|
|
if _, err := ww.ExportWatchingWallet(); err != ErrWatchingOnly {
|
2014-01-20 18:56:27 +01:00
|
|
|
t.Errorf("Nonsensical func ExportWatchingWallet returned no or incorrect error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-05-22 00:50:47 +02:00
|
|
|
pk, _ := btcec.PrivKeyFromBytes(btcec.S256(), make([]byte, 32))
|
2014-05-28 01:22:03 +02:00
|
|
|
wif, err := btcutil.NewWIF(pk, tstNetParams, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-07-08 20:17:24 +02:00
|
|
|
if _, err := ww.ImportPrivateKey(wif, createdAt); err != ErrWatchingOnly {
|
2014-01-20 18:56:27 +01:00
|
|
|
t.Errorf("Nonsensical func ImportPrivateKey returned no or incorrect error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2014-01-27 15:30:42 +01:00
|
|
|
|
2014-03-11 16:58:15 +01:00
|
|
|
func TestImportPrivateKey(t *testing.T) {
|
2014-03-17 15:24:14 +01:00
|
|
|
createHeight := int32(100)
|
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
|
|
|
createdAt := makeBS(createHeight)
|
|
|
|
w, err := New(dummyDir, "A wallet for testing.",
|
|
|
|
[]byte("banana"), tstNetParams, createdAt)
|
2014-03-11 16:58:15 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Error creating new wallet: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = w.Unlock([]byte("banana")); err != nil {
|
|
|
|
t.Errorf("Can't unlock original wallet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-27 20:59:18 +02:00
|
|
|
pk, err := btcec.NewPrivateKey(btcec.S256())
|
2014-03-11 16:58:15 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Error generating private key: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-17 15:24:14 +01:00
|
|
|
// verify that the entire wallet's sync height matches the
|
|
|
|
// expected createHeight.
|
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
|
|
|
if _, h := w.SyncedTo(); h != createHeight {
|
2014-05-30 22:34:39 +02:00
|
|
|
t.Errorf("Initial sync height %v does not match expected %v.", h, createHeight)
|
2014-03-17 15:24:14 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-11 16:58:15 +01:00
|
|
|
// import priv key
|
2014-05-28 01:22:03 +02:00
|
|
|
wif, err := btcutil.NewWIF((*btcec.PrivateKey)(pk), tstNetParams, false)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-03-17 15:24:14 +01:00
|
|
|
importHeight := int32(50)
|
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
|
|
|
importedAt := makeBS(importHeight)
|
2014-05-22 00:50:47 +02:00
|
|
|
address, err := w.ImportPrivateKey(wif, importedAt)
|
2014-03-11 16:58:15 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("importing private key: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-09 02:18:52 +02:00
|
|
|
addr, err := w.Address(address)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("privkey just imported missing: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pka := addr.(PubKeyAddress)
|
|
|
|
|
2014-03-11 16:58:15 +01:00
|
|
|
// lookup address
|
2014-04-09 02:18:52 +02:00
|
|
|
pk2, err := pka.PrivKey()
|
2014-03-11 16:58:15 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("error looking up key: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(pk, pk2) {
|
|
|
|
t.Error("original and looked-up private keys do not match.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-05-30 22:17:51 +02:00
|
|
|
// verify that the sync height now match the (smaller) import height.
|
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
|
|
|
if _, h := w.SyncedTo(); h != importHeight {
|
2014-03-17 15:24:14 +01:00
|
|
|
t.Errorf("After import sync height %v does not match expected %v.", h, importHeight)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-11 16:58:15 +01:00
|
|
|
// serialise and deseralise and check still there.
|
|
|
|
|
|
|
|
// Test (de)serialization of wallet.
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
_, err = w.WriteTo(buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Cannot write wallet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-07-08 20:17:24 +02:00
|
|
|
w2 := new(Store)
|
2014-03-11 16:58:15 +01:00
|
|
|
_, err = w2.ReadFrom(buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Cannot read wallet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-05-30 22:17:51 +02:00
|
|
|
// Verify that the sync height match expected after the reserialization.
|
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
|
|
|
if _, h := w2.SyncedTo(); h != importHeight {
|
2014-03-17 15:24:14 +01:00
|
|
|
t.Errorf("After reserialization sync height %v does not match expected %v.", h, importHeight)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-27 16:32:56 +01:00
|
|
|
// Mark imported address as partially synced with a block somewhere inbetween
|
|
|
|
// the import height and the chain height.
|
|
|
|
partialHeight := (createHeight-importHeight)/2 + importHeight
|
|
|
|
if err := w2.SetSyncStatus(address, PartialSync(partialHeight)); err != nil {
|
|
|
|
t.Errorf("Cannot mark address partially synced: %v", err)
|
|
|
|
return
|
|
|
|
}
|
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
|
|
|
if _, h := w2.SyncedTo(); h != partialHeight {
|
2014-03-27 16:32:56 +01:00
|
|
|
t.Errorf("After address partial sync, sync height %v does not match expected %v.", h, partialHeight)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test serialization with the partial sync.
|
|
|
|
buf.Reset()
|
|
|
|
_, err = w2.WriteTo(buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Cannot write wallet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-07-08 20:17:24 +02:00
|
|
|
w3 := new(Store)
|
2014-03-27 16:32:56 +01:00
|
|
|
_, err = w3.ReadFrom(buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Cannot read wallet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test correct partial height after serialization.
|
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
|
|
|
if _, h := w3.SyncedTo(); h != partialHeight {
|
2014-03-27 16:32:56 +01:00
|
|
|
t.Errorf("After address partial sync and reserialization, sync height %v does not match expected %v.",
|
|
|
|
h, partialHeight)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark imported address as not synced at all, and verify sync height is now
|
|
|
|
// the import height.
|
|
|
|
if err := w3.SetSyncStatus(address, Unsynced(0)); err != nil {
|
2014-03-17 15:24:14 +01:00
|
|
|
t.Errorf("Cannot mark address synced: %v", err)
|
|
|
|
return
|
|
|
|
}
|
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
|
|
|
if _, h := w3.SyncedTo(); h != importHeight {
|
2014-03-27 16:32:56 +01:00
|
|
|
t.Errorf("After address unsync, sync height %v does not match expected %v.", h, importHeight)
|
|
|
|
return
|
|
|
|
}
|
2014-03-17 15:24:14 +01:00
|
|
|
|
|
|
|
// Mark imported address as synced with the recently-seen blocks, and verify
|
|
|
|
// that the sync height now equals the most recent block (the one at wallet
|
|
|
|
// creation).
|
2014-03-27 16:32:56 +01:00
|
|
|
if err := w3.SetSyncStatus(address, FullSync{}); err != nil {
|
|
|
|
t.Errorf("Cannot mark address synced: %v", err)
|
|
|
|
return
|
|
|
|
}
|
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
|
|
|
if _, h := w3.SyncedTo(); h != createHeight {
|
2014-03-17 15:24:14 +01:00
|
|
|
t.Errorf("After address sync, sync height %v does not match expected %v.", h, createHeight)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-27 16:32:56 +01:00
|
|
|
if err = w3.Unlock([]byte("banana")); err != nil {
|
2014-03-11 16:58:15 +01:00
|
|
|
t.Errorf("Can't unlock deserialised wallet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-09 02:18:52 +02:00
|
|
|
addr3, err := w3.Address(address)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("privkey in deserialised wallet missing : " +
|
|
|
|
err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pka3 := addr3.(PubKeyAddress)
|
|
|
|
|
2014-03-11 16:58:15 +01:00
|
|
|
// lookup address
|
2014-04-09 02:18:52 +02:00
|
|
|
pk2, err = pka3.PrivKey()
|
2014-03-11 16:58:15 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("error looking up key in deserialized wallet: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(pk, pk2) {
|
|
|
|
t.Error("original and deserialized private keys do not match.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-13 20:13:39 +01:00
|
|
|
func TestImportScript(t *testing.T) {
|
2014-03-17 15:24:14 +01:00
|
|
|
createHeight := int32(100)
|
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
|
|
|
createdAt := makeBS(createHeight)
|
|
|
|
w, err := New(dummyDir, "A wallet for testing.",
|
|
|
|
[]byte("banana"), tstNetParams, createdAt)
|
2014-03-13 20:13:39 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Error creating new wallet: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = w.Unlock([]byte("banana")); err != nil {
|
|
|
|
t.Errorf("Can't unlock original wallet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-17 15:24:14 +01:00
|
|
|
// verify that the entire wallet's sync height matches the
|
|
|
|
// expected createHeight.
|
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
|
|
|
if _, h := w.SyncedTo(); h != createHeight {
|
2014-05-30 22:34:39 +02:00
|
|
|
t.Errorf("Initial sync height %v does not match expected %v.", h, createHeight)
|
2014-03-17 15:24:14 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-13 20:13:39 +01:00
|
|
|
script := []byte{btcscript.OP_TRUE, btcscript.OP_DUP,
|
|
|
|
btcscript.OP_DROP}
|
2014-03-17 15:24:14 +01:00
|
|
|
importHeight := int32(50)
|
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
|
|
|
stamp := makeBS(importHeight)
|
2014-03-13 20:13:39 +01:00
|
|
|
address, err := w.ImportScript(script, stamp)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("error importing script: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup address
|
2014-04-09 02:18:52 +02:00
|
|
|
ainfo, err := w.Address(address)
|
2014-03-13 20:13:39 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("error looking up script: " + err.Error())
|
|
|
|
}
|
|
|
|
|
2014-04-09 02:18:52 +02:00
|
|
|
sinfo := ainfo.(ScriptAddress)
|
2014-03-13 20:13:39 +01:00
|
|
|
|
2014-04-09 02:18:52 +02:00
|
|
|
if !bytes.Equal(script, sinfo.Script()) {
|
2014-03-13 20:13:39 +01:00
|
|
|
t.Error("original and looked-up script do not match.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-09 02:18:52 +02:00
|
|
|
if sinfo.ScriptClass() != btcscript.NonStandardTy {
|
2014-03-13 20:13:39 +01:00
|
|
|
t.Error("script type incorrect.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-09 02:18:52 +02:00
|
|
|
if sinfo.RequiredSigs() != 0 {
|
2014-03-13 20:13:39 +01:00
|
|
|
t.Error("required sigs funny number")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-09 02:18:52 +02:00
|
|
|
if len(sinfo.Addresses()) != 0 {
|
2014-03-13 20:13:39 +01:00
|
|
|
t.Error("addresses in bogus script.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if sinfo.Address().EncodeAddress() != address.EncodeAddress() {
|
|
|
|
t.Error("script address doesn't match entry.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(sinfo.Address().ScriptAddress()) != sinfo.AddrHash() {
|
|
|
|
t.Error("script hash doesn't match address.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-17 15:24:14 +01:00
|
|
|
if sinfo.FirstBlock() != importHeight {
|
2014-03-13 20:13:39 +01:00
|
|
|
t.Error("funny first block")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sinfo.Imported() {
|
|
|
|
t.Error("imported script info not imported.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if sinfo.Change() {
|
|
|
|
t.Error("imported script is change.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if sinfo.Compressed() {
|
|
|
|
t.Error("imported script is compressed.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-05-30 22:17:51 +02:00
|
|
|
// verify that the sync height now match the (smaller) import height.
|
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
|
|
|
if _, h := w.SyncedTo(); h != importHeight {
|
2014-03-17 15:24:14 +01:00
|
|
|
t.Errorf("After import sync height %v does not match expected %v.", h, importHeight)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-07 17:54:10 +02:00
|
|
|
// Check that it's included along with the active payment addresses.
|
|
|
|
found := false
|
|
|
|
for _, wa := range w.SortedActiveAddresses() {
|
|
|
|
if wa.Address() == address {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Errorf("Imported script address was not returned with sorted active payment addresses.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := w.ActiveAddresses()[address]; !ok {
|
|
|
|
t.Errorf("Imported script address was not returned with unsorted active payment addresses.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-13 20:13:39 +01:00
|
|
|
// serialise and deseralise and check still there.
|
|
|
|
|
|
|
|
// Test (de)serialization of wallet.
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
_, err = w.WriteTo(buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Cannot write wallet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-07-08 20:17:24 +02:00
|
|
|
w2 := new(Store)
|
2014-03-13 20:13:39 +01:00
|
|
|
_, err = w2.ReadFrom(buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Cannot read wallet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-05-30 22:17:51 +02:00
|
|
|
// Verify that the sync height matches expected after the reserialization.
|
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
|
|
|
if _, h := w2.SyncedTo(); h != importHeight {
|
2014-03-17 15:24:14 +01:00
|
|
|
t.Errorf("After reserialization sync height %v does not match expected %v.", h, importHeight)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-27 16:32:56 +01:00
|
|
|
// lookup address
|
2014-04-09 02:18:52 +02:00
|
|
|
ainfo2, err := w2.Address(address)
|
2014-03-27 16:32:56 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("error looking up info in deserialized wallet: " + err.Error())
|
|
|
|
}
|
|
|
|
|
2014-04-09 02:18:52 +02:00
|
|
|
sinfo2 := ainfo2.(ScriptAddress)
|
|
|
|
// Check all the same again. We can't use reflect.DeepEquals since
|
|
|
|
// the internals have pointers back to the wallet struct.
|
|
|
|
if sinfo2.Address().EncodeAddress() != address.EncodeAddress() {
|
|
|
|
t.Error("script address doesn't match entry.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(sinfo2.Address().ScriptAddress()) != sinfo2.AddrHash() {
|
|
|
|
t.Error("script hash doesn't match address.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if sinfo2.FirstBlock() != importHeight {
|
|
|
|
t.Error("funny first block")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sinfo2.Imported() {
|
|
|
|
t.Error("imported script info not imported.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if sinfo2.Change() {
|
|
|
|
t.Error("imported script is change.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if sinfo2.Compressed() {
|
|
|
|
t.Error("imported script is compressed.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(sinfo.Script(), sinfo2.Script()) {
|
|
|
|
t.Error("original and serailised scriptinfo scripts "+
|
|
|
|
"don't match %s != %s", spew.Sdump(sinfo.Script()),
|
|
|
|
spew.Sdump(sinfo2.Script()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if sinfo.ScriptClass() != sinfo2.ScriptClass() {
|
|
|
|
t.Error("original and serailised scriptinfo class "+
|
|
|
|
"don't match: %s != %s", sinfo.ScriptClass(),
|
|
|
|
sinfo2.ScriptClass())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(sinfo.Addresses(), sinfo2.Addresses()) {
|
|
|
|
t.Error("original and serailised scriptinfo addresses "+
|
|
|
|
"don't match (%s) != (%s)", spew.Sdump(sinfo.Addresses),
|
|
|
|
spew.Sdump(sinfo2.Addresses()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if sinfo.RequiredSigs() != sinfo.RequiredSigs() {
|
|
|
|
t.Errorf("original and serailised scriptinfo requiredsigs "+
|
|
|
|
"don't match %d != %d", sinfo.RequiredSigs(),
|
|
|
|
sinfo2.RequiredSigs())
|
2014-03-17 15:24:14 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-07 17:54:10 +02:00
|
|
|
// Check that it's included along with the active payment addresses.
|
|
|
|
found = false
|
|
|
|
for _, wa := range w.SortedActiveAddresses() {
|
|
|
|
if wa.Address() == address {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Errorf("After reserialiation, imported script address was not returned with sorted " +
|
|
|
|
"active payment addresses.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := w.ActiveAddresses()[address]; !ok {
|
|
|
|
t.Errorf("After reserialiation, imported script address was not returned with unsorted " +
|
|
|
|
"active payment addresses.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-27 16:32:56 +01:00
|
|
|
// Mark imported address as partially synced with a block somewhere inbetween
|
|
|
|
// the import height and the chain height.
|
|
|
|
partialHeight := (createHeight-importHeight)/2 + importHeight
|
|
|
|
if err := w2.SetSyncStatus(address, PartialSync(partialHeight)); err != nil {
|
|
|
|
t.Errorf("Cannot mark address partially synced: %v", err)
|
|
|
|
return
|
|
|
|
}
|
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
|
|
|
if _, h := w2.SyncedTo(); h != partialHeight {
|
2014-03-27 16:32:56 +01:00
|
|
|
t.Errorf("After address partial sync, sync height %v does not match expected %v.", h, partialHeight)
|
2014-03-17 15:24:14 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-27 16:32:56 +01:00
|
|
|
// Test serialization with the partial sync.
|
|
|
|
buf.Reset()
|
|
|
|
_, err = w2.WriteTo(buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Cannot write wallet: %v", err)
|
2014-03-13 20:13:39 +01:00
|
|
|
return
|
|
|
|
}
|
2014-07-08 20:17:24 +02:00
|
|
|
w3 := new(Store)
|
2014-03-27 16:32:56 +01:00
|
|
|
_, err = w3.ReadFrom(buf)
|
2014-03-13 20:13:39 +01:00
|
|
|
if err != nil {
|
2014-03-27 16:32:56 +01:00
|
|
|
t.Errorf("Cannot read wallet: %v", err)
|
|
|
|
return
|
2014-03-13 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
2014-03-27 16:32:56 +01:00
|
|
|
// Test correct partial height after serialization.
|
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
|
|
|
if _, h := w3.SyncedTo(); h != partialHeight {
|
2014-03-27 16:32:56 +01:00
|
|
|
t.Errorf("After address partial sync and reserialization, sync height %v does not match expected %v.",
|
|
|
|
h, partialHeight)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark imported address as not synced at all, and verify sync height is now
|
|
|
|
// the import height.
|
|
|
|
if err := w3.SetSyncStatus(address, Unsynced(0)); err != nil {
|
|
|
|
t.Errorf("Cannot mark address synced: %v", err)
|
|
|
|
return
|
|
|
|
}
|
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
|
|
|
if _, h := w3.SyncedTo(); h != importHeight {
|
2014-03-27 16:32:56 +01:00
|
|
|
t.Errorf("After address unsync, sync height %v does not match expected %v.", h, importHeight)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark imported address as synced with the recently-seen blocks, and verify
|
|
|
|
// that the sync height now equals the most recent block (the one at wallet
|
|
|
|
// creation).
|
|
|
|
if err := w3.SetSyncStatus(address, FullSync{}); err != nil {
|
|
|
|
t.Errorf("Cannot mark address synced: %v", err)
|
|
|
|
return
|
|
|
|
}
|
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
|
|
|
if _, h := w3.SyncedTo(); h != createHeight {
|
2014-03-27 16:32:56 +01:00
|
|
|
t.Errorf("After address sync, sync height %v does not match expected %v.", h, createHeight)
|
2014-03-13 20:13:39 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-03-27 16:32:56 +01:00
|
|
|
if err = w3.Unlock([]byte("banana")); err != nil {
|
|
|
|
t.Errorf("Can't unlock deserialised wallet: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2014-03-13 20:13:39 +01:00
|
|
|
}
|
|
|
|
|
2014-01-27 15:30:42 +01:00
|
|
|
func TestChangePassphrase(t *testing.T) {
|
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
|
|
|
createdAt := makeBS(0)
|
|
|
|
w, err := New(dummyDir, "A wallet for testing.",
|
|
|
|
[]byte("banana"), tstNetParams, createdAt)
|
2014-01-27 15:30:42 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Error creating new wallet: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Changing the passphrase with a locked wallet must fail with ErrWalletLocked.
|
2014-07-08 20:17:24 +02:00
|
|
|
if err := w.ChangePassphrase([]byte("potato")); err != ErrLocked {
|
2014-01-27 15:30:42 +01:00
|
|
|
t.Errorf("Changing passphrase on a locked wallet did not fail correctly: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock wallet so the passphrase can be changed.
|
|
|
|
if err := w.Unlock([]byte("banana")); err != nil {
|
|
|
|
t.Errorf("Cannot unlock: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get root address and its private key. This is compared to the private
|
|
|
|
// key post passphrase change.
|
|
|
|
rootAddr := w.LastChainedAddress()
|
2014-04-09 02:18:52 +02:00
|
|
|
|
|
|
|
rootAddrInfo, err := w.Address(rootAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("can't find root address: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rapka := rootAddrInfo.(PubKeyAddress)
|
|
|
|
|
|
|
|
rootPrivKey, err := rapka.PrivKey()
|
2014-01-27 15:30:42 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Cannot get root address' private key: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change passphrase.
|
|
|
|
if err := w.ChangePassphrase([]byte("potato")); err != nil {
|
|
|
|
t.Errorf("Changing passhprase failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wallet should still be unlocked.
|
|
|
|
if w.IsLocked() {
|
|
|
|
t.Errorf("Wallet should be unlocked after passphrase change.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock it.
|
|
|
|
if err := w.Lock(); err != nil {
|
|
|
|
t.Errorf("Cannot lock wallet after passphrase change: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock with old passphrase. This must fail with ErrWrongPassphrase.
|
|
|
|
if err := w.Unlock([]byte("banana")); err != ErrWrongPassphrase {
|
|
|
|
t.Errorf("Unlocking with old passphrases did not fail correctly: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock with new passphrase. This must succeed.
|
|
|
|
if err := w.Unlock([]byte("potato")); err != nil {
|
|
|
|
t.Errorf("Unlocking with new passphrase failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get root address' private key again.
|
2014-04-09 02:18:52 +02:00
|
|
|
rootAddrInfo2, err := w.Address(rootAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("can't find root address: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rapka2 := rootAddrInfo2.(PubKeyAddress)
|
|
|
|
|
|
|
|
rootPrivKey2, err := rapka2.PrivKey()
|
2014-01-27 15:30:42 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Cannot get root address' private key after passphrase change: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private keys must match.
|
|
|
|
if !reflect.DeepEqual(rootPrivKey, rootPrivKey2) {
|
|
|
|
t.Errorf("Private keys before and after unlock differ.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|