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
|
|
|
|
}
|
2013-12-04 01:22:47 +01:00
|
|
|
a := &Account{
|
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
|
|
|
|
}
|
|
|
|
}
|