6
6
from test_framework .test_framework import BitcoinTestFramework
7
7
from test_framework .util import *
8
8
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
+
10
17
class RawTransactionsTest (BitcoinTestFramework ):
11
18
12
19
def __init__ (self ):
@@ -71,7 +78,7 @@ def run_test(self):
71
78
rawtxfund = self .nodes [2 ].fundrawtransaction (rawtx )
72
79
fee = rawtxfund ['fee' ]
73
80
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
75
82
76
83
##############################
77
84
# simple test with two coins #
@@ -123,14 +130,7 @@ def run_test(self):
123
130
#########################################################################
124
131
# test a fundrawtransaction with a VIN greater than the required amount #
125
132
#########################################################################
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 )
134
134
135
135
inputs = [ {'txid' : utx ['txid' ], 'vout' : utx ['vout' ]}]
136
136
outputs = { self .nodes [0 ].getnewaddress () : 1.0 }
@@ -151,14 +151,7 @@ def run_test(self):
151
151
#####################################################################
152
152
# test a fundrawtransaction with which will not get a change output #
153
153
#####################################################################
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 )
162
155
163
156
inputs = [ {'txid' : utx ['txid' ], 'vout' : utx ['vout' ]}]
164
157
outputs = { self .nodes [0 ].getnewaddress () : Decimal (5.0 ) - fee - feeTolerance }
@@ -180,14 +173,7 @@ def run_test(self):
180
173
####################################################
181
174
# test a fundrawtransaction with an invalid option #
182
175
####################################################
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 )
191
177
192
178
inputs = [ {'txid' : utx ['txid' ], 'vout' : utx ['vout' ]} ]
193
179
outputs = { self .nodes [0 ].getnewaddress () : Decimal (4.0 ) }
@@ -205,14 +191,7 @@ def run_test(self):
205
191
############################################################
206
192
# test a fundrawtransaction with an invalid change address #
207
193
############################################################
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 )
216
195
217
196
inputs = [ {'txid' : utx ['txid' ], 'vout' : utx ['vout' ]} ]
218
197
outputs = { self .nodes [0 ].getnewaddress () : Decimal (4.0 ) }
@@ -227,18 +206,10 @@ def run_test(self):
227
206
assert ("changeAddress must be a valid bitcoin address" in e .error ['message' ])
228
207
229
208
230
-
231
209
############################################################
232
210
# test a fundrawtransaction with a provided change address #
233
211
############################################################
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 )
242
213
243
214
inputs = [ {'txid' : utx ['txid' ], 'vout' : utx ['vout' ]} ]
244
215
outputs = { self .nodes [0 ].getnewaddress () : Decimal (4.0 ) }
@@ -247,24 +218,22 @@ def run_test(self):
247
218
assert_equal (utx ['txid' ], dec_tx ['vin' ][0 ]['txid' ])
248
219
249
220
change = self .nodes [2 ].getnewaddress ()
221
+ try :
222
+ rawtxfund = self .nodes [2 ].fundrawtransaction (rawtx , {'changeAddress' : change , 'changePosition' : 2 })
223
+ except JSONRPCException as e :
224
+ assert ('changePosition out of bounds' == e .error ['message' ])
225
+ else :
226
+ assert (False )
250
227
rawtxfund = self .nodes [2 ].fundrawtransaction (rawtx , {'changeAddress' : change , 'changePosition' : 0 })
251
228
dec_tx = self .nodes [2 ].decoderawtransaction (rawtxfund ['hex' ])
252
229
out = dec_tx ['vout' ][0 ];
253
230
assert_equal (change , out ['scriptPubKey' ]['addresses' ][0 ])
254
231
255
232
256
-
257
233
#########################################################################
258
234
# test a fundrawtransaction with a VIN smaller than the required amount #
259
235
#########################################################################
260
- utx = False
261
- listunspent = self .nodes [2 ].listunspent ()
262
- for aUtx in listunspent :
263
- if aUtx ['amount' ] == 1.0 :
264
- utx = aUtx
265
- break
266
-
267
- assert (utx != False )
236
+ utx = get_unspent (self .nodes [2 ].listunspent (), 1 )
268
237
269
238
inputs = [ {'txid' : utx ['txid' ], 'vout' : utx ['vout' ]}]
270
239
outputs = { self .nodes [0 ].getnewaddress () : 1.0 }
@@ -299,17 +268,8 @@ def run_test(self):
299
268
###########################################
300
269
# test a fundrawtransaction with two VINs #
301
270
###########################################
302
- utx = False
303
- utx2 = False
304
- listunspent = self .nodes [2 ].listunspent ()
305
- for aUtx in listunspent :
306
- if aUtx ['amount' ] == 1.0 :
307
- utx = aUtx
308
- if aUtx ['amount' ] == 5.0 :
309
- utx2 = aUtx
310
-
311
-
312
- assert (utx != False )
271
+ utx = get_unspent (self .nodes [2 ].listunspent (), 1 )
272
+ utx2 = get_unspent (self .nodes [2 ].listunspent (), 5 )
313
273
314
274
inputs = [ {'txid' : utx ['txid' ], 'vout' : utx ['vout' ]},{'txid' : utx2 ['txid' ], 'vout' : utx2 ['vout' ]} ]
315
275
outputs = { self .nodes [0 ].getnewaddress () : 6.0 }
@@ -341,17 +301,8 @@ def run_test(self):
341
301
#########################################################
342
302
# test a fundrawtransaction with two VINs and two vOUTs #
343
303
#########################################################
344
- utx = False
345
- utx2 = False
346
- listunspent = self .nodes [2 ].listunspent ()
347
- for aUtx in listunspent :
348
- if aUtx ['amount' ] == 1.0 :
349
- utx = aUtx
350
- if aUtx ['amount' ] == 5.0 :
351
- utx2 = aUtx
352
-
353
-
354
- assert (utx != False )
304
+ utx = get_unspent (self .nodes [2 ].listunspent (), 1 )
305
+ utx2 = get_unspent (self .nodes [2 ].listunspent (), 5 )
355
306
356
307
inputs = [ {'txid' : utx ['txid' ], 'vout' : utx ['vout' ]},{'txid' : utx2 ['txid' ], 'vout' : utx2 ['vout' ]} ]
357
308
outputs = { self .nodes [0 ].getnewaddress () : 6.0 , self .nodes [0 ].getnewaddress () : 1.0 }
0 commit comments