diff --git a/test/functional/rpc_packages.py b/test/functional/rpc_packages.py --- a/test/functional/rpc_packages.py +++ b/test/functional/rpc_packages.py @@ -15,6 +15,11 @@ from test_framework.test_framework import BitcoinTestFramework from test_framework.txtools import pad_tx from test_framework.util import assert_equal +from test_framework.wallet import ( + create_child_with_parents, + create_raw_chain, + make_chain, +) class RPCPackagesTest(BitcoinTestFramework): @@ -77,31 +82,6 @@ self.test_multiple_parents() self.test_conflicting() - def chain_transaction(self, parent_txid, parent_value, n=0, - parent_locking_script=None): - """Build a transaction that spends parent_txid.vout[n] and produces one - output with amount = parent_value with a fee deducted. - Return tuple (CTransaction object, raw hex, nValue, scriptPubKey of the - output created). - """ - node = self.nodes[0] - inputs = [{"txid": parent_txid, "vout": n}] - my_value = parent_value - Decimal("100.00") - outputs = {self.address: my_value} - rawtx = node.createrawtransaction(inputs, outputs) - prevtxs = [ - { - "txid": parent_txid, - "vout": n, - "scriptPubKey": parent_locking_script, - "amount": parent_value, - }] if parent_locking_script else None - signedtx = node.signrawtransactionwithkey( - hexstring=rawtx, privkeys=self.privkeys, prevtxs=prevtxs) - tx = FromHex(CTransaction(), signedtx["hex"]) - assert signedtx["complete"] - return tx, signedtx["hex"], my_value, tx.vout[0].scriptPubKey.hex() - def test_independent(self): self.log.info("Test multiple independent transactions in a package") node = self.nodes[0] @@ -173,20 +153,8 @@ def test_chain(self): node = self.nodes[0] first_coin = self.coins.pop() - - # Chain of 50 transactions - parent_locking_script = None - txid = first_coin["txid"] - chain_hex = [] - chain_txns = [] - value = first_coin["amount"] - - for _ in range(50): - (tx, txhex, value, parent_locking_script) = self.chain_transaction( - txid, value, 0, parent_locking_script) - txid = tx.get_id() - chain_hex.append(txhex) - chain_txns.append(tx) + (chain_hex, chain_txns) = create_raw_chain( + node, first_coin, self.address, self.privkeys) self.log.info( "Check that testmempoolaccept requires packages to be sorted by dependency") @@ -235,8 +203,9 @@ child_value = value - Decimal("100.00") # Child A - (_, tx_child_a_hex, _, _) = self.chain_transaction( - parent_txid, value, 0, parent_locking_script_a) + (_, tx_child_a_hex, _, _) = make_chain( + node, self.address, self.privkeys, parent_txid, value, 0, + parent_locking_script_a) assert not node.testmempoolaccept([tx_child_a_hex])[0]["allowed"] # Child B @@ -269,23 +238,6 @@ node.sendrawtransaction(rawtx) assert_equal(testres_single, testres_multiple_ab) - def create_child_with_parents(self, parents_tx, values, locking_scripts): - """Creates a transaction that spends the first output of each parent in parents_tx.""" - num_parents = len(parents_tx) - total_value = sum(values) - inputs = [{"txid": tx.get_id(), "vout": 0} for tx in parents_tx] - outputs = {self.address: total_value - num_parents * Decimal("100.00")} - rawtx_child = self.nodes[0].createrawtransaction(inputs, outputs) - prevtxs = [] - for i in range(num_parents): - prevtxs.append( - {"txid": parents_tx[i].get_id(), "vout": 0, - "scriptPubKey": locking_scripts[i], "amount": values[i]}) - signedtx_child = self.nodes[0].signrawtransactionwithkey( - hexstring=rawtx_child, privkeys=self.privkeys, prevtxs=prevtxs) - assert signedtx_child["complete"] - return signedtx_child["hex"] - def test_multiple_parents(self): node = self.nodes[0] @@ -300,14 +252,16 @@ for _ in range(num_parents): parent_coin = self.coins.pop() value = parent_coin["amount"] - (tx, txhex, value, parent_locking_script) = self.chain_transaction( - parent_coin["txid"], value) + (tx, txhex, value, parent_locking_script) = make_chain( + node, self.address, self.privkeys, parent_coin["txid"], + value) package_hex.append(txhex) parents_tx.append(tx) values.append(value) parent_locking_scripts.append(parent_locking_script) - child_hex = self.create_child_with_parents( - parents_tx, values, parent_locking_scripts) + child_hex = create_child_with_parents( + node, self.address, self.privkeys, parents_tx, values, + parent_locking_scripts) # Package accept should work with the parents in any order # (as long as parents come before child) for _ in range(10): diff --git a/test/functional/test_framework/wallet.py b/test/functional/test_framework/wallet.py --- a/test/functional/test_framework/wallet.py +++ b/test/functional/test_framework/wallet.py @@ -11,7 +11,14 @@ ADDRESS_ECREG_P2SH_OP_TRUE, SCRIPTSIG_OP_TRUE, ) -from test_framework.messages import XEC, COutPoint, CTransaction, CTxIn, CTxOut +from test_framework.messages import ( + XEC, + COutPoint, + CTransaction, + CTxIn, + CTxOut, + FromHex, +) from test_framework.txtools import pad_tx from test_framework.util import assert_equal, satoshi_round @@ -81,3 +88,67 @@ assert_equal(tx_info['size'], size) assert_equal(tx_info['fees']['base'], fee) return {'txid': tx_info['txid'], 'hex': tx_hex} + + +def make_chain(node, address, privkeys, parent_txid, parent_value, n=0, + parent_locking_script=None): + """Build a transaction that spends parent_txid.vout[n] and produces one + output with amount = parent_value with a fee deducted. + Return tuple (CTransaction object, raw hex, nValue, scriptPubKey of the + output created). + """ + inputs = [{"txid": parent_txid, "vout": n}] + my_value = parent_value - Decimal("100.00") + outputs = {address: my_value} + rawtx = node.createrawtransaction(inputs, outputs) + prevtxs = [{ + "txid": parent_txid, + "vout": n, + "scriptPubKey": parent_locking_script, + "amount": parent_value, + }] if parent_locking_script else None + signedtx = node.signrawtransactionwithkey( + hexstring=rawtx, privkeys=privkeys, prevtxs=prevtxs) + assert signedtx["complete"] + tx = FromHex(CTransaction(), signedtx["hex"]) + return (tx, signedtx["hex"], my_value, tx.vout[0].scriptPubKey.hex()) + + +def create_child_with_parents(node, address, privkeys, parents_tx, values, + locking_scripts): + """Creates a transaction that spends the first output of each parent in parents_tx.""" + num_parents = len(parents_tx) + total_value = sum(values) + inputs = [{"txid": tx.get_id(), "vout": 0} for tx in parents_tx] + outputs = {address: total_value - num_parents * Decimal("100.00")} + rawtx_child = node.createrawtransaction(inputs, outputs) + prevtxs = [] + for i in range(num_parents): + prevtxs.append( + {"txid": parents_tx[i].get_id(), "vout": 0, + "scriptPubKey": locking_scripts[i], "amount": values[i]}) + signedtx_child = node.signrawtransactionwithkey( + hexstring=rawtx_child, privkeys=privkeys, prevtxs=prevtxs) + assert signedtx_child["complete"] + return signedtx_child["hex"] + + +def create_raw_chain(node, first_coin, address, privkeys, chain_length=50): + """Helper function: create a "chain" of chain_length transactions. + The nth transaction in the chain is a child of the n-1th transaction and + parent of the n+1th transaction. + """ + parent_locking_script = None + txid = first_coin["txid"] + chain_hex = [] + chain_txns = [] + value = first_coin["amount"] + + for _ in range(chain_length): + (tx, txhex, value, parent_locking_script) = make_chain( + node, address, privkeys, txid, value, 0, parent_locking_script) + txid = tx.get_id() + chain_hex.append(txhex) + chain_txns.append(tx) + + return (chain_hex, chain_txns)