Skip to content

Commit fa32465

Browse files
author
MarcoFalke
committed
[qa] fundrawtransaction: Create get_unspent()
1 parent fa8ce3b commit fa32465

File tree

1 file changed

+19
-74
lines changed

1 file changed

+19
-74
lines changed

qa/rpc-tests/fundrawtransaction.py

Lines changed: 19 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
from test_framework.test_framework import BitcoinTestFramework
77
from test_framework.util import *
88

9-
# Create one-input, one-output, no-fee transaction:
9+
10+
def get_unspent(listunspent, amount):
11+
for utx in listunspent:
12+
if utx['amount'] == amount:
13+
return utx
14+
raise AssertionError('Could not find unspent with amount={}'.format(amount))
15+
16+
1017
class RawTransactionsTest(BitcoinTestFramework):
1118

1219
def __init__(self):
@@ -71,7 +78,7 @@ def run_test(self):
7178
rawtxfund = self.nodes[2].fundrawtransaction(rawtx)
7279
fee = rawtxfund['fee']
7380
dec_tx = self.nodes[2].decoderawtransaction(rawtxfund['hex'])
74-
assert(len(dec_tx['vin']) > 0) #test if we have enought inputs
81+
assert(len(dec_tx['vin']) > 0) #test that we have enough inputs
7582

7683
##############################
7784
# simple test with two coins #
@@ -123,14 +130,7 @@ def run_test(self):
123130
#########################################################################
124131
# test a fundrawtransaction with a VIN greater than the required amount #
125132
#########################################################################
126-
utx = False
127-
listunspent = self.nodes[2].listunspent()
128-
for aUtx in listunspent:
129-
if aUtx['amount'] == 5.0:
130-
utx = aUtx
131-
break
132-
133-
assert(utx!=False)
133+
utx = get_unspent(self.nodes[2].listunspent(), 5)
134134

135135
inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']}]
136136
outputs = { self.nodes[0].getnewaddress() : 1.0 }
@@ -151,14 +151,7 @@ def run_test(self):
151151
#####################################################################
152152
# test a fundrawtransaction with which will not get a change output #
153153
#####################################################################
154-
utx = False
155-
listunspent = self.nodes[2].listunspent()
156-
for aUtx in listunspent:
157-
if aUtx['amount'] == 5.0:
158-
utx = aUtx
159-
break
160-
161-
assert(utx!=False)
154+
utx = get_unspent(self.nodes[2].listunspent(), 5)
162155

163156
inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']}]
164157
outputs = { self.nodes[0].getnewaddress() : Decimal(5.0) - fee - feeTolerance }
@@ -180,14 +173,7 @@ def run_test(self):
180173
####################################################
181174
# test a fundrawtransaction with an invalid option #
182175
####################################################
183-
utx = False
184-
listunspent = self.nodes[2].listunspent()
185-
for aUtx in listunspent:
186-
if aUtx['amount'] == 5.0:
187-
utx = aUtx
188-
break
189-
190-
assert_equal(utx!=False, True)
176+
utx = get_unspent(self.nodes[2].listunspent(), 5)
191177

192178
inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ]
193179
outputs = { self.nodes[0].getnewaddress() : Decimal(4.0) }
@@ -205,14 +191,7 @@ def run_test(self):
205191
############################################################
206192
# test a fundrawtransaction with an invalid change address #
207193
############################################################
208-
utx = False
209-
listunspent = self.nodes[2].listunspent()
210-
for aUtx in listunspent:
211-
if aUtx['amount'] == 5.0:
212-
utx = aUtx
213-
break
214-
215-
assert_equal(utx!=False, True)
194+
utx = get_unspent(self.nodes[2].listunspent(), 5)
216195

217196
inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ]
218197
outputs = { self.nodes[0].getnewaddress() : Decimal(4.0) }
@@ -227,18 +206,10 @@ def run_test(self):
227206
assert("changeAddress must be a valid bitcoin address" in e.error['message'])
228207

229208

230-
231209
############################################################
232210
# test a fundrawtransaction with a provided change address #
233211
############################################################
234-
utx = False
235-
listunspent = self.nodes[2].listunspent()
236-
for aUtx in listunspent:
237-
if aUtx['amount'] == 5.0:
238-
utx = aUtx
239-
break
240-
241-
assert_equal(utx!=False, True)
212+
utx = get_unspent(self.nodes[2].listunspent(), 5)
242213

243214
inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']} ]
244215
outputs = { self.nodes[0].getnewaddress() : Decimal(4.0) }
@@ -259,18 +230,10 @@ def run_test(self):
259230
assert_equal(change, out['scriptPubKey']['addresses'][0])
260231

261232

262-
263233
#########################################################################
264234
# test a fundrawtransaction with a VIN smaller than the required amount #
265235
#########################################################################
266-
utx = False
267-
listunspent = self.nodes[2].listunspent()
268-
for aUtx in listunspent:
269-
if aUtx['amount'] == 1.0:
270-
utx = aUtx
271-
break
272-
273-
assert(utx!=False)
236+
utx = get_unspent(self.nodes[2].listunspent(), 1)
274237

275238
inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']}]
276239
outputs = { self.nodes[0].getnewaddress() : 1.0 }
@@ -305,17 +268,8 @@ def run_test(self):
305268
###########################################
306269
# test a fundrawtransaction with two VINs #
307270
###########################################
308-
utx = False
309-
utx2 = False
310-
listunspent = self.nodes[2].listunspent()
311-
for aUtx in listunspent:
312-
if aUtx['amount'] == 1.0:
313-
utx = aUtx
314-
if aUtx['amount'] == 5.0:
315-
utx2 = aUtx
316-
317-
318-
assert(utx!=False)
271+
utx = get_unspent(self.nodes[2].listunspent(), 1)
272+
utx2 = get_unspent(self.nodes[2].listunspent(), 5)
319273

320274
inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']},{'txid' : utx2['txid'], 'vout' : utx2['vout']} ]
321275
outputs = { self.nodes[0].getnewaddress() : 6.0 }
@@ -347,17 +301,8 @@ def run_test(self):
347301
#########################################################
348302
# test a fundrawtransaction with two VINs and two vOUTs #
349303
#########################################################
350-
utx = False
351-
utx2 = False
352-
listunspent = self.nodes[2].listunspent()
353-
for aUtx in listunspent:
354-
if aUtx['amount'] == 1.0:
355-
utx = aUtx
356-
if aUtx['amount'] == 5.0:
357-
utx2 = aUtx
358-
359-
360-
assert(utx!=False)
304+
utx = get_unspent(self.nodes[2].listunspent(), 1)
305+
utx2 = get_unspent(self.nodes[2].listunspent(), 5)
361306

362307
inputs = [ {'txid' : utx['txid'], 'vout' : utx['vout']},{'txid' : utx2['txid'], 'vout' : utx2['vout']} ]
363308
outputs = { self.nodes[0].getnewaddress() : 6.0, self.nodes[0].getnewaddress() : 1.0 }

0 commit comments

Comments
 (0)