lbcwallet/createtx_test.go

138 lines
3.2 KiB
Go
Raw Normal View History

// 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
package main
import (
2014-07-03 13:45:40 +02:00
"testing"
"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,
}
}
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
{
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,
Height: 0,
},
},
curHeight: 142, // 143 confirmations
txSize: 250,
free: false,
},
2014-04-11 20:52:50 +02: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,
Height: 0,
},
},
curHeight: 143, // 144 confirmations
txSize: 250,
free: false,
},
2014-04-11 20:52:50 +02: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,
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)
}
}
}
func TestFakeTxs(t *testing.T) {
// First we need a wallet.
w, err := keystore.NewStore("banana wallet", "", []byte("banana"),
btcwire.MainNet, &keystore.BlockStamp{}, 100)
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{
Wallet: w,
lockedOutpoints: map[btcwire.OutPoint]struct{}{},
}
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{}
addr, err := w.NextChainedAddress(&keystore.BlockStamp{}, 100)
if err != nil {
t.Errorf("Cannot get next address: %s", err)
return
}
copy(utxo.AddrHash[:], addr.ScriptAddress())
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)
ss, err := btcscript.PayToAddrScript(addr)
if err != nil {
t.Errorf("Could not create utxo PkScript: %s", err)
return
}
utxo.Subscript = tx.PkScript(ss)
utxo.Amt = 1000000
utxo.Height = 12345
a.UtxoStore = append(a.UtxoStore, utxo)
// Fake our current block height so btcd doesn't need to be queried.
curBlock.BlockStamp.Height = 12346
// Create the transaction.
2013-11-12 18:08:10 +01:00
pairs := map[string]int64{
"17XhEvq9Nahdj7Xe1nv6oRe1tEmaHUuynH": 5000,
}
_, err = a.txToPairs(pairs, 1)
if err != nil {
t.Errorf("Tx creation failed: %s", err)
return
}
}