2014-02-24 20:35:30 +01:00
|
|
|
// TODO(jrick) Due to the extra encapsulation added during the switch
|
|
|
|
// to the new txstore, structures can no longer be mocked due to private
|
|
|
|
// members. Since all members for RecvTxOut and SignedTx are private, the
|
|
|
|
// simplist solution would be to make RecvTxOut an interface and create
|
|
|
|
// our own types satisifying the interface for this test package. Until
|
|
|
|
// then, disable this test.
|
|
|
|
//
|
|
|
|
// +build ignore
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-07-03 13:45:40 +02:00
|
|
|
"testing"
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
"github.com/conformal/btcscript"
|
|
|
|
"github.com/conformal/btcutil"
|
|
|
|
"github.com/conformal/btcwallet/tx"
|
|
|
|
"github.com/conformal/btcwallet/wallet"
|
|
|
|
"github.com/conformal/btcwire"
|
|
|
|
)
|
|
|
|
|
2014-01-16 18:04:47 +01:00
|
|
|
func init() {
|
|
|
|
cfg = &config{
|
|
|
|
KeypoolSize: 100,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-04 01:22:47 +01:00
|
|
|
type allowFreeTest struct {
|
|
|
|
name string
|
|
|
|
inputs []*tx.Utxo
|
|
|
|
curHeight int32
|
|
|
|
txSize int
|
|
|
|
free bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var allowFreeTests = []allowFreeTest{
|
2014-04-11 20:52:50 +02:00
|
|
|
{
|
2013-12-04 01:22:47 +01:00
|
|
|
name: "priority < 57,600,000",
|
|
|
|
inputs: []*tx.Utxo{
|
2014-04-11 20:52:50 +02:00
|
|
|
{
|
2014-07-08 18:22:09 +02:00
|
|
|
Amt: btcutil.SatoshiPerBitcoin,
|
2013-12-04 01:22:47 +01:00
|
|
|
Height: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
curHeight: 142, // 143 confirmations
|
|
|
|
txSize: 250,
|
|
|
|
free: false,
|
|
|
|
},
|
2014-04-11 20:52:50 +02:00
|
|
|
{
|
2013-12-04 01:22:47 +01:00
|
|
|
name: "priority == 57,600,000",
|
|
|
|
inputs: []*tx.Utxo{
|
2014-04-11 20:52:50 +02:00
|
|
|
{
|
2014-07-08 18:22:09 +02:00
|
|
|
Amt: btcutil.SatoshiPerBitcoin,
|
2013-12-04 01:22:47 +01:00
|
|
|
Height: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
curHeight: 143, // 144 confirmations
|
|
|
|
txSize: 250,
|
|
|
|
free: false,
|
|
|
|
},
|
2014-04-11 20:52:50 +02:00
|
|
|
{
|
2013-12-04 01:22:47 +01:00
|
|
|
name: "priority > 57,600,000",
|
|
|
|
inputs: []*tx.Utxo{
|
2014-04-11 20:52:50 +02:00
|
|
|
{
|
2014-07-08 18:22:09 +02:00
|
|
|
Amt: btcutil.SatoshiPerBitcoin,
|
2013-12-04 01:22:47 +01:00
|
|
|
Height: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
curHeight: 144, // 145 confirmations
|
|
|
|
txSize: 250,
|
|
|
|
free: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAllowFree(t *testing.T) {
|
|
|
|
for _, test := range allowFreeTests {
|
|
|
|
calcFree := allowFree(test.curHeight, test.inputs, test.txSize)
|
|
|
|
if calcFree != test.free {
|
|
|
|
t.Errorf("Allow free test '%v' failed.", test.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-09 20:14:57 +02:00
|
|
|
func TestFakeTxs(t *testing.T) {
|
|
|
|
// First we need a wallet.
|
2014-07-08 20:17:24 +02:00
|
|
|
w, err := keystore.NewStore("banana wallet", "", []byte("banana"),
|
|
|
|
btcwire.MainNet, &keystore.BlockStamp{}, 100)
|
2013-09-09 20:14:57 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Can not create encrypted wallet: %s", 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
|
|
|
a := &Wallet{
|
2014-06-23 23:59:57 +02:00
|
|
|
Wallet: w,
|
|
|
|
lockedOutpoints: map[btcwire.OutPoint]struct{}{},
|
2013-09-09 20:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
w.Unlock([]byte("banana"))
|
|
|
|
|
|
|
|
// Create and add a fake Utxo so we have some funds to spend.
|
|
|
|
//
|
|
|
|
// This will pass validation because btcscript is unaware of invalid
|
|
|
|
// tx inputs, however, this example would fail in btcd.
|
|
|
|
utxo := &tx.Utxo{}
|
2014-07-08 20:17:24 +02:00
|
|
|
addr, err := w.NextChainedAddress(&keystore.BlockStamp{}, 100)
|
2013-09-09 20:14:57 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Cannot get next address: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2014-01-06 18:24:29 +01:00
|
|
|
copy(utxo.AddrHash[:], addr.ScriptAddress())
|
2013-09-09 20:14:57 +02:00
|
|
|
ophash := (btcwire.ShaHash)([...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
|
|
|
|
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
|
|
|
|
28, 29, 30, 31, 32})
|
|
|
|
out := btcwire.NewOutPoint(&ophash, 0)
|
|
|
|
utxo.Out = tx.OutPoint(*out)
|
2014-01-06 18:24:29 +01:00
|
|
|
ss, err := btcscript.PayToAddrScript(addr)
|
2013-09-09 20:14:57 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Could not create utxo PkScript: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
utxo.Subscript = tx.PkScript(ss)
|
2013-12-04 01:22:47 +01:00
|
|
|
utxo.Amt = 1000000
|
2013-09-09 20:14:57 +02:00
|
|
|
utxo.Height = 12345
|
2014-01-30 16:14:02 +01:00
|
|
|
a.UtxoStore = append(a.UtxoStore, utxo)
|
2013-09-09 20:14:57 +02:00
|
|
|
|
|
|
|
// Fake our current block height so btcd doesn't need to be queried.
|
Implement address rescanning.
When a wallet is opened, a rescan request will be sent to btcd with
all active addresses from the wallet, to rescan from the last synced
block (now saved to the wallet file) and the current best block.
As multi-account support is further explored, rescan requests should
be batched together to send a single request for all addresses from
all wallets.
This change introduces several changes to the wallet, tx, and utxo
files. Wallet files are still compatible, however, a rescan will try
to start at the genesis block since no correct "last synced to" or
"created at block X" was saved. The tx and utxo files, however, are
not compatible and should be deleted (or an error will occur on read).
If any errors occur opening the utxo file, a rescan will start
beginning at the creation block saved in the wallet.
2013-10-30 02:22:14 +01:00
|
|
|
curBlock.BlockStamp.Height = 12346
|
2013-09-09 20:14:57 +02:00
|
|
|
|
|
|
|
// Create the transaction.
|
2013-11-12 18:08:10 +01:00
|
|
|
pairs := map[string]int64{
|
2013-09-09 20:14:57 +02:00
|
|
|
"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH": 5000,
|
|
|
|
}
|
2014-02-04 17:50:33 +01:00
|
|
|
_, err = a.txToPairs(pairs, 1)
|
2013-09-09 20:14:57 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Tx creation failed: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|