2222from test_framework .util import (
2323 assert_equal ,
2424)
25+ from test_framework .wallet import (
26+ create_child_with_parents ,
27+ create_raw_chain ,
28+ make_chain ,
29+ )
2530
2631class RPCPackagesTest (BitcoinTestFramework ):
2732 def set_test_params (self ):
@@ -78,26 +83,6 @@ def run_test(self):
7883 self .test_conflicting ()
7984 self .test_rbf ()
8085
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 ())
10186
10287 def test_independent (self ):
10388 self .log .info ("Test multiple independent transactions in a package" )
@@ -148,20 +133,7 @@ def test_independent(self):
148133 def test_chain (self ):
149134 node = self .nodes [0 ]
150135 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 )
165137 self .log .info ("Check that testmempoolaccept requires packages to be sorted by dependency" )
166138 assert_equal (node .testmempoolaccept (rawtxs = chain_hex [::- 1 ]),
167139 [{"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):
201173 child_value = value - Decimal ("0.0001" )
202174
203175 # 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 )
205177 assert not node .testmempoolaccept ([tx_child_a_hex ])[0 ]["allowed" ]
206178
207179 # Child B
@@ -226,19 +198,6 @@ def test_multiple_children(self):
226198 node .sendrawtransaction (rawtx )
227199 assert_equal (testres_single , testres_multiple_ab )
228200
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" ]
242201
243202 def test_multiple_parents (self ):
244203 node = self .nodes [0 ]
@@ -253,12 +212,12 @@ def test_multiple_parents(self):
253212 for _ in range (num_parents ):
254213 parent_coin = self .coins .pop ()
255214 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 )
257216 package_hex .append (txhex )
258217 parents_tx .append (tx )
259218 values .append (value )
260219 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 )
262221 # Package accept should work with the parents in any order (as long as parents come before child)
263222 for _ in range (10 ):
264223 random .shuffle (package_hex )
0 commit comments