Skip to content

Commit faf8318

Browse files
author
MarcoFalke
committed
test: Split fundrawtx test into subtests
1 parent fa6fba3 commit faf8318

File tree

1 file changed

+61
-36
lines changed

1 file changed

+61
-36
lines changed

test/functional/rpc_fundrawtransaction.py

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,36 @@ def run_test(self):
6060
self.nodes[0].generate(121)
6161
self.sync_all()
6262

63+
self.test_change_position()
64+
self.test_simple()
65+
self.test_simple_two_coins()
66+
self.test_simple_two_outputs()
67+
self.test_change()
68+
self.test_no_change()
69+
self.test_invalid_option()
70+
self.test_invalid_change_address()
71+
self.test_valid_change_address()
72+
self.test_change_type()
73+
self.test_coin_selection()
74+
self.test_two_vin()
75+
self.test_two_vin_two_vout()
76+
self.test_invalid_input()
77+
self.test_fee_p2pkh()
78+
self.test_fee_p2pkh_multi_out()
79+
self.test_fee_p2sh()
80+
self.test_fee_4of5()
81+
self.test_spend_2of2()
82+
self.test_locked_wallet()
83+
self.test_many_inputs_fee()
84+
self.test_many_inputs_send()
85+
self.test_op_return()
86+
self.test_watchonly()
87+
self.test_all_watched_funds()
88+
self.test_option_feerate()
89+
self.test_address_reuse()
90+
self.test_option_subtract_fee_from_outputs()
91+
92+
def test_change_position(self):
6393
# ensure that setting changePosition in fundraw with an exact match is handled properly
6494
rawmatch = self.nodes[2].createrawtransaction([], {self.nodes[2].getnewaddress():50})
6595
rawmatch = self.nodes[2].fundrawtransaction(rawmatch, {"changePosition":1, "subtractFeeFromOutputs":[0]})
@@ -84,6 +114,7 @@ def run_test(self):
84114
self.nodes[0].generate(1)
85115
self.sync_all()
86116

117+
def test_simple(self):
87118
###############
88119
# simple test #
89120
###############
@@ -92,10 +123,10 @@ def run_test(self):
92123
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
93124
dec_tx = self.nodes[2].decoderawtransaction(rawtx)
94125
rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
95-
fee = rawtxfund['fee']
96126
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
97127
assert len(dec_tx['vin']) > 0 #test that we have enough inputs
98128

129+
def test_simple_two_coins(self):
99130
##############################
100131
# simple test with two coins #
101132
##############################
@@ -105,25 +136,11 @@ def run_test(self):
105136
dec_tx = self.nodes[2].decoderawtransaction(rawtx)
106137

107138
rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
108-
fee = rawtxfund['fee']
109139
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
110140
assert len(dec_tx['vin']) > 0 #test if we have enough inputs
111-
112-
##############################
113-
# simple test with two coins #
114-
##############################
115-
inputs = [ ]
116-
outputs = { self.nodes[0].getnewaddress() : 2.6 }
117-
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
118-
dec_tx = self.nodes[2].decoderawtransaction(rawtx)
119-
120-
rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
121-
fee = rawtxfund['fee']
122-
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
123-
assert len(dec_tx['vin']) > 0
124141
assert_equal(dec_tx['vin'][0]['scriptSig']['hex'], '')
125142

126-
143+
def test_simple_two_outputs(self):
127144
################################
128145
# simple test with two outputs #
129146
################################
@@ -133,7 +150,6 @@ def run_test(self):
133150
dec_tx = self.nodes[2].decoderawtransaction(rawtx)
134151

135152
rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
136-
fee = rawtxfund['fee']
137153
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
138154
totalOut = 0
139155
for out in dec_tx['vout']:
@@ -142,7 +158,7 @@ def run_test(self):
142158
assert len(dec_tx['vin']) > 0
143159
assert_equal(dec_tx['vin'][0]['scriptSig']['hex'], '')
144160

145-
161+
def test_change(self):
146162
#########################################################################
147163
# test a fundrawtransaction with a VIN greater than the required amount #
148164
#########################################################################
@@ -164,7 +180,7 @@ def run_test(self):
164180

165181
assert_equal(fee + totalOut, utx['amount']) #compare vin total and totalout+fee
166182

167-
183+
def test_no_change(self):
168184
#####################################################################
169185
# test a fundrawtransaction with which will not get a change output #
170186
#####################################################################
@@ -186,7 +202,7 @@ def run_test(self):
186202
assert_equal(rawtxfund['changepos'], -1)
187203
assert_equal(fee + totalOut, utx['amount']) #compare vin total and totalout+fee
188204

189-
205+
def test_invalid_option(self):
190206
####################################################
191207
# test a fundrawtransaction with an invalid option #
192208
####################################################
@@ -203,6 +219,7 @@ def run_test(self):
203219
# reserveChangeKey was deprecated and is now removed
204220
assert_raises_rpc_error(-3, "Unexpected key reserveChangeKey", lambda: self.nodes[2].fundrawtransaction(hexstring=rawtx, options={'reserveChangeKey': True}))
205221

222+
def test_invalid_change_address(self):
206223
############################################################
207224
# test a fundrawtransaction with an invalid change address #
208225
############################################################
@@ -216,6 +233,7 @@ def run_test(self):
216233

217234
assert_raises_rpc_error(-5, "changeAddress must be a valid bitcoin address", self.nodes[2].fundrawtransaction, rawtx, {'changeAddress':'foobar'})
218235

236+
def test_valid_change_address(self):
219237
############################################################
220238
# test a fundrawtransaction with a provided change address #
221239
############################################################
@@ -234,6 +252,7 @@ def run_test(self):
234252
out = dec_tx['vout'][0]
235253
assert_equal(change, out['scriptPubKey']['addresses'][0])
236254

255+
def test_change_type(self):
237256
#########################################################
238257
# test a fundrawtransaction with a provided change type #
239258
#########################################################
@@ -248,6 +267,7 @@ def run_test(self):
248267
dec_tx = self.nodes[2].decoderawtransaction(rawtx['hex'])
249268
assert_equal('witness_v0_keyhash', dec_tx['vout'][rawtx['changepos']]['scriptPubKey']['type'])
250269

270+
def test_coin_selection(self):
251271
#########################################################################
252272
# test a fundrawtransaction with a VIN smaller than the required amount #
253273
#########################################################################
@@ -265,7 +285,6 @@ def run_test(self):
265285
assert_equal("00", dec_tx['vin'][0]['scriptSig']['hex'])
266286

267287
rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
268-
fee = rawtxfund['fee']
269288
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
270289
totalOut = 0
271290
matchingOuts = 0
@@ -282,7 +301,7 @@ def run_test(self):
282301
assert_equal(matchingOuts, 1)
283302
assert_equal(len(dec_tx['vout']), 2)
284303

285-
304+
def test_two_vin(self):
286305
###########################################
287306
# test a fundrawtransaction with two VINs #
288307
###########################################
@@ -296,7 +315,6 @@ def run_test(self):
296315
assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
297316

298317
rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
299-
fee = rawtxfund['fee']
300318
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
301319
totalOut = 0
302320
matchingOuts = 0
@@ -316,6 +334,7 @@ def run_test(self):
316334

317335
assert_equal(matchingIns, 2) #we now must see two vins identical to vins given as params
318336

337+
def test_two_vin_two_vout(self):
319338
#########################################################
320339
# test a fundrawtransaction with two VINs and two vOUTs #
321340
#########################################################
@@ -329,7 +348,6 @@ def run_test(self):
329348
assert_equal(utx['txid'], dec_tx['vin'][0]['txid'])
330349

331350
rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
332-
fee = rawtxfund['fee']
333351
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
334352
totalOut = 0
335353
matchingOuts = 0
@@ -341,16 +359,16 @@ def run_test(self):
341359
assert_equal(matchingOuts, 2)
342360
assert_equal(len(dec_tx['vout']), 3)
343361

362+
def test_invalid_input(self):
344363
##############################################
345364
# test a fundrawtransaction with invalid vin #
346365
##############################################
347366
inputs = [ {'txid' : "1c7f966dab21119bac53213a2bc7532bff1fa844c124fd750a7d0b1332440bd1", 'vout' : 0} ] #invalid vin!
348367
outputs = { self.nodes[0].getnewaddress() : 1.0}
349368
rawtx = self.nodes[2].createrawtransaction(inputs, outputs)
350-
dec_tx = self.nodes[2].decoderawtransaction(rawtx)
351-
352369
assert_raises_rpc_error(-4, "Insufficient funds", self.nodes[2].fundrawtransaction, rawtx)
353370

371+
def test_fee_p2pkh(self):
354372
############################################################
355373
#compare fee of a standard pubkeyhash transaction
356374
inputs = []
@@ -367,6 +385,7 @@ def run_test(self):
367385
assert feeDelta >= 0 and feeDelta <= self.fee_tolerance
368386
############################################################
369387

388+
def test_fee_p2pkh_multi_out(self):
370389
############################################################
371390
#compare fee of a standard pubkeyhash transaction with multiple outputs
372391
inputs = []
@@ -382,7 +401,7 @@ def run_test(self):
382401
assert feeDelta >= 0 and feeDelta <= self.fee_tolerance
383402
############################################################
384403

385-
404+
def test_fee_p2sh(self):
386405
############################################################
387406
#compare fee of a 2of2 multisig p2sh transaction
388407

@@ -409,7 +428,7 @@ def run_test(self):
409428
assert feeDelta >= 0 and feeDelta <= self.fee_tolerance
410429
############################################################
411430

412-
431+
def test_fee_4of5(self):
413432
############################################################
414433
#compare fee of a standard pubkeyhash transaction
415434

@@ -442,7 +461,7 @@ def run_test(self):
442461
assert feeDelta >= 0 and feeDelta <= self.fee_tolerance
443462
############################################################
444463

445-
464+
def test_spend_2of2(self):
446465
############################################################
447466
# spend a 2of2 multisig transaction over fundraw
448467

@@ -457,7 +476,7 @@ def run_test(self):
457476

458477

459478
# send 1.2 BTC to msig addr
460-
txId = self.nodes[0].sendtoaddress(mSigObj, 1.2)
479+
self.nodes[0].sendtoaddress(mSigObj, 1.2)
461480
self.sync_all()
462481
self.nodes[1].generate(1)
463482
self.sync_all()
@@ -469,14 +488,15 @@ def run_test(self):
469488
fundedTx = self.nodes[2].fundrawtransaction(rawtx)
470489

471490
signedTx = self.nodes[2].signrawtransactionwithwallet(fundedTx['hex'])
472-
txId = self.nodes[2].sendrawtransaction(signedTx['hex'])
491+
self.nodes[2].sendrawtransaction(signedTx['hex'])
473492
self.sync_all()
474493
self.nodes[1].generate(1)
475494
self.sync_all()
476495

477496
# make sure funds are received at node1
478497
assert_equal(oldBalance+Decimal('1.10000000'), self.nodes[1].getbalance())
479498

499+
def test_locked_wallet(self):
480500
############################################################
481501
# locked wallet test
482502
self.nodes[1].encryptwallet("test")
@@ -524,14 +544,14 @@ def run_test(self):
524544
#now we need to unlock
525545
self.nodes[1].walletpassphrase("test", 600)
526546
signedTx = self.nodes[1].signrawtransactionwithwallet(fundedTx['hex'])
527-
txId = self.nodes[1].sendrawtransaction(signedTx['hex'])
547+
self.nodes[1].sendrawtransaction(signedTx['hex'])
528548
self.nodes[1].generate(1)
529549
self.sync_all()
530550

531551
# make sure funds are received at node1
532552
assert_equal(oldBalance+Decimal('51.10000000'), self.nodes[0].getbalance())
533553

534-
554+
def test_many_inputs_fee(self):
535555
###############################################
536556
# multiple (~19) inputs tx test | Compare fee #
537557
###############################################
@@ -561,7 +581,7 @@ def run_test(self):
561581
feeDelta = Decimal(fundedTx['fee']) - Decimal(signedFee)
562582
assert feeDelta >= 0 and feeDelta <= self.fee_tolerance * 19 #~19 inputs
563583

564-
584+
def test_many_inputs_send(self):
565585
#############################################
566586
# multiple (~19) inputs tx test | sign/send #
567587
#############################################
@@ -585,12 +605,13 @@ def run_test(self):
585605
rawtx = self.nodes[1].createrawtransaction(inputs, outputs)
586606
fundedTx = self.nodes[1].fundrawtransaction(rawtx)
587607
fundedAndSignedTx = self.nodes[1].signrawtransactionwithwallet(fundedTx['hex'])
588-
txId = self.nodes[1].sendrawtransaction(fundedAndSignedTx['hex'])
608+
self.nodes[1].sendrawtransaction(fundedAndSignedTx['hex'])
589609
self.sync_all()
590610
self.nodes[0].generate(1)
591611
self.sync_all()
592612
assert_equal(oldBalance+Decimal('50.19000000'), self.nodes[0].getbalance()) #0.19+block reward
593613

614+
def test_op_return(self):
594615
#####################################################
595616
# test fundrawtransaction with OP_RETURN and no vin #
596617
#####################################################
@@ -607,7 +628,7 @@ def run_test(self):
607628
assert_greater_than(len(dec_tx['vin']), 0) # at least one vin
608629
assert_equal(len(dec_tx['vout']), 2) # one change output added
609630

610-
631+
def test_watchonly(self):
611632
##################################################
612633
# test a fundrawtransaction using only watchonly #
613634
##################################################
@@ -624,6 +645,7 @@ def run_test(self):
624645
assert "fee" in result.keys()
625646
assert_greater_than(result["changepos"], -1)
626647

648+
def test_all_watched_funds(self):
627649
###############################################################
628650
# test fundrawtransaction using the entirety of watched funds #
629651
###############################################################
@@ -650,6 +672,7 @@ def run_test(self):
650672
self.nodes[0].generate(1)
651673
self.sync_all()
652674

675+
def test_option_feerate(self):
653676
#######################
654677
# Test feeRate option #
655678
#######################
@@ -668,6 +691,7 @@ def run_test(self):
668691
assert_fee_amount(result2['fee'], count_bytes(result2['hex']), 2 * result_fee_rate)
669692
assert_fee_amount(result3['fee'], count_bytes(result3['hex']), 10 * result_fee_rate)
670693

694+
def test_address_reuse(self):
671695
################################
672696
# Test no address reuse occurs #
673697
################################
@@ -684,6 +708,7 @@ def run_test(self):
684708
# Now the change address key should be removed from the keypool
685709
assert changeaddress != nextaddr
686710

711+
def test_option_subtract_fee_from_outputs(self):
687712
######################################
688713
# Test subtractFeeFromOutputs option #
689714
######################################

0 commit comments

Comments
 (0)