22
22
from test_framework .util import (
23
23
assert_equal ,
24
24
)
25
+ from test_framework .wallet import (
26
+ create_child_with_parents ,
27
+ create_raw_chain ,
28
+ make_chain ,
29
+ )
25
30
26
31
class RPCPackagesTest (BitcoinTestFramework ):
27
32
def set_test_params (self ):
@@ -78,26 +83,6 @@ def run_test(self):
78
83
self .test_conflicting ()
79
84
self .test_rbf ()
80
85
81
- def chain_transaction (self , parent_txid , parent_value , n = 0 , parent_locking_script = None ):
82
- """Build a transaction that spends parent_txid.vout[n] and produces one output with
83
- amount = parent_value with a fee deducted.
84
- Return tuple (CTransaction object, raw hex, nValue, scriptPubKey of the output created).
85
- """
86
- node = self .nodes [0 ]
87
- inputs = [{"txid" : parent_txid , "vout" : n }]
88
- my_value = parent_value - Decimal ("0.0001" )
89
- outputs = {self .address : my_value }
90
- rawtx = node .createrawtransaction (inputs , outputs )
91
- prevtxs = [{
92
- "txid" : parent_txid ,
93
- "vout" : n ,
94
- "scriptPubKey" : parent_locking_script ,
95
- "amount" : parent_value ,
96
- }] if parent_locking_script else None
97
- signedtx = node .signrawtransactionwithkey (hexstring = rawtx , privkeys = self .privkeys , prevtxs = prevtxs )
98
- assert signedtx ["complete" ]
99
- tx = tx_from_hex (signedtx ["hex" ])
100
- return (tx , signedtx ["hex" ], my_value , tx .vout [0 ].scriptPubKey .hex ())
101
86
102
87
def test_independent (self ):
103
88
self .log .info ("Test multiple independent transactions in a package" )
@@ -148,20 +133,7 @@ def test_independent(self):
148
133
def test_chain (self ):
149
134
node = self .nodes [0 ]
150
135
first_coin = self .coins .pop ()
151
-
152
- # Chain of 25 transactions
153
- parent_locking_script = None
154
- txid = first_coin ["txid" ]
155
- chain_hex = []
156
- chain_txns = []
157
- value = first_coin ["amount" ]
158
-
159
- for _ in range (25 ):
160
- (tx , txhex , value , parent_locking_script ) = self .chain_transaction (txid , value , 0 , parent_locking_script )
161
- txid = tx .rehash ()
162
- chain_hex .append (txhex )
163
- chain_txns .append (tx )
164
-
136
+ (chain_hex , chain_txns ) = create_raw_chain (node , first_coin , self .address , self .privkeys )
165
137
self .log .info ("Check that testmempoolaccept requires packages to be sorted by dependency" )
166
138
assert_equal (node .testmempoolaccept (rawtxs = chain_hex [::- 1 ]),
167
139
[{"txid" : tx .rehash (), "wtxid" : tx .getwtxid (), "package-error" : "package-not-sorted" } for tx in chain_txns [::- 1 ]])
@@ -201,7 +173,7 @@ def test_multiple_children(self):
201
173
child_value = value - Decimal ("0.0001" )
202
174
203
175
# Child A
204
- (_ , tx_child_a_hex , _ , _ ) = self .chain_transaction ( parent_txid , child_value , 0 , parent_locking_script_a )
176
+ (_ , tx_child_a_hex , _ , _ ) = make_chain ( node , self .address , self . privkeys , parent_txid , child_value , 0 , parent_locking_script_a )
205
177
assert not node .testmempoolaccept ([tx_child_a_hex ])[0 ]["allowed" ]
206
178
207
179
# Child B
@@ -226,19 +198,6 @@ def test_multiple_children(self):
226
198
node .sendrawtransaction (rawtx )
227
199
assert_equal (testres_single , testres_multiple_ab )
228
200
229
- def create_child_with_parents (self , parents_tx , values , locking_scripts ):
230
- """Creates a transaction that spends the first output of each parent in parents_tx."""
231
- num_parents = len (parents_tx )
232
- total_value = sum (values )
233
- inputs = [{"txid" : tx .rehash (), "vout" : 0 } for tx in parents_tx ]
234
- outputs = {self .address : total_value - num_parents * Decimal ("0.0001" )}
235
- rawtx_child = self .nodes [0 ].createrawtransaction (inputs , outputs )
236
- prevtxs = []
237
- for i in range (num_parents ):
238
- prevtxs .append ({"txid" : parents_tx [i ].rehash (), "vout" : 0 , "scriptPubKey" : locking_scripts [i ], "amount" : values [i ]})
239
- signedtx_child = self .nodes [0 ].signrawtransactionwithkey (hexstring = rawtx_child , privkeys = self .privkeys , prevtxs = prevtxs )
240
- assert signedtx_child ["complete" ]
241
- return signedtx_child ["hex" ]
242
201
243
202
def test_multiple_parents (self ):
244
203
node = self .nodes [0 ]
@@ -253,12 +212,12 @@ def test_multiple_parents(self):
253
212
for _ in range (num_parents ):
254
213
parent_coin = self .coins .pop ()
255
214
value = parent_coin ["amount" ]
256
- (tx , txhex , value , parent_locking_script ) = self .chain_transaction ( parent_coin ["txid" ], value )
215
+ (tx , txhex , value , parent_locking_script ) = make_chain ( node , self .address , self . privkeys , parent_coin ["txid" ], value )
257
216
package_hex .append (txhex )
258
217
parents_tx .append (tx )
259
218
values .append (value )
260
219
parent_locking_scripts .append (parent_locking_script )
261
- child_hex = self . create_child_with_parents (parents_tx , values , parent_locking_scripts )
220
+ child_hex = create_child_with_parents (node , self . address , self . privkeys , parents_tx , values , parent_locking_scripts )
262
221
# Package accept should work with the parents in any order (as long as parents come before child)
263
222
for _ in range (10 ):
264
223
random .shuffle (package_hex )
0 commit comments