Skip to content

Commit f8253d6

Browse files
committed
extract/rename helper functions from rpc_packages.py
MOVEONLY; no change in behavior. Rename because there is another helper funciton in chain_transaction in test_framework.util.py
1 parent 3cd663a commit f8253d6

File tree

2 files changed

+62
-50
lines changed

2 files changed

+62
-50
lines changed

test/functional/rpc_packages.py

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
from 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

2631
class 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)

test/functional/test_framework/wallet.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
CTxIn,
1717
CTxInWitness,
1818
CTxOut,
19+
tx_from_hex,
1920
)
2021
from test_framework.script import (
2122
CScript,
@@ -176,3 +177,55 @@ def create_self_transfer(self, *, fee_rate=Decimal("0.003"), from_node, utxo_to_
176177
def sendrawtransaction(self, *, from_node, tx_hex):
177178
from_node.sendrawtransaction(tx_hex)
178179
self.scan_tx(from_node.decoderawtransaction(tx_hex))
180+
181+
def make_chain(node, address, privkeys, parent_txid, parent_value, n=0, parent_locking_script=None):
182+
"""Build a transaction that spends parent_txid.vout[n] and produces one output with
183+
amount = parent_value with a fee deducted.
184+
Return tuple (CTransaction object, raw hex, nValue, scriptPubKey of the output created).
185+
"""
186+
inputs = [{"txid": parent_txid, "vout": n}]
187+
my_value = parent_value - Decimal("0.0001")
188+
outputs = {address : my_value}
189+
rawtx = node.createrawtransaction(inputs, outputs)
190+
prevtxs = [{
191+
"txid": parent_txid,
192+
"vout": n,
193+
"scriptPubKey": parent_locking_script,
194+
"amount": parent_value,
195+
}] if parent_locking_script else None
196+
signedtx = node.signrawtransactionwithkey(hexstring=rawtx, privkeys=privkeys, prevtxs=prevtxs)
197+
assert signedtx["complete"]
198+
tx = tx_from_hex(signedtx["hex"])
199+
return (tx, signedtx["hex"], my_value, tx.vout[0].scriptPubKey.hex())
200+
201+
def create_child_with_parents(node, address, privkeys, parents_tx, values, locking_scripts):
202+
"""Creates a transaction that spends the first output of each parent in parents_tx."""
203+
num_parents = len(parents_tx)
204+
total_value = sum(values)
205+
inputs = [{"txid": tx.rehash(), "vout": 0} for tx in parents_tx]
206+
outputs = {address : total_value - num_parents * Decimal("0.0001")}
207+
rawtx_child = node.createrawtransaction(inputs, outputs)
208+
prevtxs = []
209+
for i in range(num_parents):
210+
prevtxs.append({"txid": parents_tx[i].rehash(), "vout": 0, "scriptPubKey": locking_scripts[i], "amount": values[i]})
211+
signedtx_child = node.signrawtransactionwithkey(hexstring=rawtx_child, privkeys=privkeys, prevtxs=prevtxs)
212+
assert signedtx_child["complete"]
213+
return signedtx_child["hex"]
214+
215+
def create_raw_chain(node, first_coin, address, privkeys, chain_length=25):
216+
"""Helper function: create a "chain" of chain_length transactions. The nth transaction in the
217+
chain is a child of the n-1th transaction and parent of the n+1th transaction.
218+
"""
219+
parent_locking_script = None
220+
txid = first_coin["txid"]
221+
chain_hex = []
222+
chain_txns = []
223+
value = first_coin["amount"]
224+
225+
for _ in range(chain_length):
226+
(tx, txhex, value, parent_locking_script) = make_chain(node, address, privkeys, txid, value, 0, parent_locking_script)
227+
txid = tx.rehash()
228+
chain_hex.append(txhex)
229+
chain_txns.append(tx)
230+
231+
return (chain_hex, chain_txns)

0 commit comments

Comments
 (0)