forked from btcsuite/btcwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreatetx_test.go
301 lines (261 loc) · 7.54 KB
/
createtx_test.go
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright (c) 2018 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wallet
import (
"bytes"
"testing"
"time"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwallet/waddrmgr"
"github.com/btcsuite/btcwallet/wallet/txauthor"
"github.com/btcsuite/btcwallet/walletdb"
_ "github.com/btcsuite/btcwallet/walletdb/bdb"
"github.com/btcsuite/btcwallet/wtxmgr"
"github.com/stretchr/testify/require"
)
var (
testBlockHash, _ = chainhash.NewHashFromStr(
"00000000000000017188b968a371bab95aa43522665353b646e41865abae" +
"02a4",
)
testBlockHeight int32 = 276425
)
// TestTxToOutput checks that no new address is added to he database if we
// request a dry run of the txToOutputs call. It also makes sure a subsequent
// non-dry run call produces a similar transaction to the dry-run.
func TestTxToOutputsDryRun(t *testing.T) {
w, cleanup := testWallet(t)
defer cleanup()
// Create an address we can use to send some coins to.
keyScope := waddrmgr.KeyScopeBIP0049Plus
addr, err := w.CurrentAddress(0, keyScope)
if err != nil {
t.Fatalf("unable to get current address: %v", addr)
}
p2shAddr, err := txscript.PayToAddrScript(addr)
if err != nil {
t.Fatalf("unable to convert wallet address to p2sh: %v", err)
}
// Add an output paying to the wallet's address to the database.
txOut := wire.NewTxOut(100000, p2shAddr)
incomingTx := &wire.MsgTx{
TxIn: []*wire.TxIn{
{},
},
TxOut: []*wire.TxOut{
txOut,
},
}
addUtxo(t, w, incomingTx)
// Now tell the wallet to create a transaction paying to the specified
// outputs.
txOuts := []*wire.TxOut{
{
PkScript: p2shAddr,
Value: 10000,
},
{
PkScript: p2shAddr,
Value: 20000,
},
}
// First do a few dry-runs, making sure the number of addresses in the
// database us not inflated.
dryRunTx, err := w.txToOutputs(
txOuts, nil, 0, 1, 1000, CoinSelectionLargest, true,
)
if err != nil {
t.Fatalf("unable to author tx: %v", err)
}
change := dryRunTx.Tx.TxOut[dryRunTx.ChangeIndex]
addresses, err := w.AccountAddresses(0)
if err != nil {
t.Fatalf("unable to get addresses: %v", err)
}
if len(addresses) != 1 {
t.Fatalf("expected 1 address, found %v", len(addresses))
}
dryRunTx2, err := w.txToOutputs(
txOuts, nil, 0, 1, 1000, CoinSelectionLargest, true,
)
if err != nil {
t.Fatalf("unable to author tx: %v", err)
}
change2 := dryRunTx2.Tx.TxOut[dryRunTx2.ChangeIndex]
addresses, err = w.AccountAddresses(0)
if err != nil {
t.Fatalf("unable to get addresses: %v", err)
}
if len(addresses) != 1 {
t.Fatalf("expected 1 address, found %v", len(addresses))
}
// The two dry-run TXs should be invalid, since they don't have
// signatures.
err = validateMsgTx(
dryRunTx.Tx, dryRunTx.PrevScripts, dryRunTx.PrevInputValues,
)
if err == nil {
t.Fatalf("Expected tx to be invalid")
}
err = validateMsgTx(
dryRunTx2.Tx, dryRunTx2.PrevScripts, dryRunTx2.PrevInputValues,
)
if err == nil {
t.Fatalf("Expected tx to be invalid")
}
// Now we do a proper, non-dry run. This should add a change address
// to the database.
tx, err := w.txToOutputs(
txOuts, nil, 0, 1, 1000, CoinSelectionLargest, false,
)
if err != nil {
t.Fatalf("unable to author tx: %v", err)
}
change3 := tx.Tx.TxOut[tx.ChangeIndex]
addresses, err = w.AccountAddresses(0)
if err != nil {
t.Fatalf("unable to get addresses: %v", err)
}
if len(addresses) != 2 {
t.Fatalf("expected 2 addresses, found %v", len(addresses))
}
err = validateMsgTx(tx.Tx, tx.PrevScripts, tx.PrevInputValues)
if err != nil {
t.Fatalf("Expected tx to be valid: %v", err)
}
// Finally, we check that all the transaction were using the same
// change address.
if !bytes.Equal(change.PkScript, change2.PkScript) {
t.Fatalf("first dry-run using different change address " +
"than second")
}
if !bytes.Equal(change2.PkScript, change3.PkScript) {
t.Fatalf("dry-run using different change address " +
"than wet run")
}
}
// addUtxo add the given transaction to the wallet's database marked as a
// confirmed UTXO .
func addUtxo(t *testing.T, w *Wallet, incomingTx *wire.MsgTx) {
var b bytes.Buffer
if err := incomingTx.Serialize(&b); err != nil {
t.Fatalf("unable to serialize tx: %v", err)
}
txBytes := b.Bytes()
rec, err := wtxmgr.NewTxRecord(txBytes, time.Now())
if err != nil {
t.Fatalf("unable to create tx record: %v", err)
}
// The block meta will be inserted to tell the wallet this is a
// confirmed transaction.
block := &wtxmgr.BlockMeta{
Block: wtxmgr.Block{
Hash: *testBlockHash,
Height: testBlockHeight,
},
Time: time.Unix(1387737310, 0),
}
if err := walletdb.Update(w.db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(wtxmgrNamespaceKey)
err = w.TxStore.InsertTx(ns, rec, block)
if err != nil {
return err
}
// Add all tx outputs as credits.
for i := 0; i < len(incomingTx.TxOut); i++ {
err = w.TxStore.AddCredit(
ns, rec, block, uint32(i), false,
)
if err != nil {
return err
}
}
return nil
}); err != nil {
t.Fatalf("failed inserting tx: %v", err)
}
}
// TestInputYield verifies the functioning of the inputYieldsPositively.
func TestInputYield(t *testing.T) {
addr, _ := btcutil.DecodeAddress("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4", &chaincfg.MainNetParams)
pkScript, err := txscript.PayToAddrScript(addr)
require.NoError(t, err)
credit := &wtxmgr.Credit{
Amount: 1000,
PkScript: pkScript,
}
// At 10 sat/b this input is yielding positively.
require.True(t, inputYieldsPositively(credit, 10000))
// At 20 sat/b this input is yielding negatively.
require.False(t, inputYieldsPositively(credit, 20000))
}
// TestTxToOutputsRandom tests random coin selection.
func TestTxToOutputsRandom(t *testing.T) {
w, cleanup := testWallet(t)
defer cleanup()
// Create an address we can use to send some coins to.
keyScope := waddrmgr.KeyScopeBIP0049Plus
addr, err := w.CurrentAddress(0, keyScope)
if err != nil {
t.Fatalf("unable to get current address: %v", addr)
}
p2shAddr, err := txscript.PayToAddrScript(addr)
if err != nil {
t.Fatalf("unable to convert wallet address to p2sh: %v", err)
}
// Add a set of utxos to the wallet.
incomingTx := &wire.MsgTx{
TxIn: []*wire.TxIn{
{},
},
TxOut: []*wire.TxOut{},
}
for amt := int64(5000); amt <= 125000; amt += 10000 {
incomingTx.AddTxOut(wire.NewTxOut(amt, p2shAddr))
}
addUtxo(t, w, incomingTx)
// Now tell the wallet to create a transaction paying to the specified
// outputs.
txOuts := []*wire.TxOut{
{
PkScript: p2shAddr,
Value: 50000,
},
{
PkScript: p2shAddr,
Value: 100000,
},
}
const (
feeSatPerKb = 100000
maxIterations = 100
)
createTx := func() *txauthor.AuthoredTx {
tx, err := w.txToOutputs(
txOuts, nil, 0, 1, feeSatPerKb, CoinSelectionRandom, true,
)
require.NoError(t, err)
return tx
}
firstTx := createTx()
var isRandom bool
for iteration := 0; iteration < maxIterations; iteration++ {
tx := createTx()
// Check to see if we are getting a total input value.
// We consider this proof that the randomization works.
if tx.TotalInput != firstTx.TotalInput {
isRandom = true
}
// At the used fee rate of 100 sat/b, the 5000 sat input is
// negatively yielding. We don't expect it to ever be selected.
for _, inputValue := range tx.PrevInputValues {
require.NotEqual(t, inputValue, btcutil.Amount(5000))
}
}
require.True(t, isRandom)
}