|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2021 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | +"""RPCs that handle raw transaction packages.""" |
| 6 | + |
| 7 | +from decimal import Decimal |
| 8 | +from io import BytesIO |
| 9 | +import random |
| 10 | + |
| 11 | +from test_framework.address import ADDRESS_BCRT1_P2WSH_OP_TRUE |
| 12 | +from test_framework.test_framework import BitcoinTestFramework |
| 13 | +from test_framework.messages import ( |
| 14 | + BIP125_SEQUENCE_NUMBER, |
| 15 | + COIN, |
| 16 | + CTransaction, |
| 17 | + CTxInWitness, |
| 18 | +) |
| 19 | +from test_framework.script import ( |
| 20 | + CScript, |
| 21 | + OP_TRUE, |
| 22 | +) |
| 23 | +from test_framework.util import ( |
| 24 | + assert_equal, |
| 25 | + hex_str_to_bytes, |
| 26 | +) |
| 27 | + |
| 28 | +class RPCPackagesTest(BitcoinTestFramework): |
| 29 | + def set_test_params(self): |
| 30 | + self.num_nodes = 1 |
| 31 | + self.setup_clean_chain = True |
| 32 | + |
| 33 | + def assert_testres_equal(self, package_hex, testres_expected): |
| 34 | + """Shuffle package_hex and assert that the testmempoolaccept result matches testres_expected. This should only |
| 35 | + be used to test packages where the order does not matter. The ordering of transactions in package_hex and |
| 36 | + testres_expected must match. |
| 37 | + """ |
| 38 | + shuffled_indeces = list(range(len(package_hex))) |
| 39 | + random.shuffle(shuffled_indeces) |
| 40 | + shuffled_package = [package_hex[i] for i in shuffled_indeces] |
| 41 | + shuffled_testres = [testres_expected[i] for i in shuffled_indeces] |
| 42 | + assert_equal(shuffled_testres, self.nodes[0].testmempoolaccept(shuffled_package)) |
| 43 | + |
| 44 | + def run_test(self): |
| 45 | + self.log.info("Generate blocks to create UTXOs") |
| 46 | + node = self.nodes[0] |
| 47 | + self.privkeys = [node.get_deterministic_priv_key().key] |
| 48 | + self.address = node.get_deterministic_priv_key().address |
| 49 | + self.coins = [] |
| 50 | + # The last 100 coinbase transactions are premature |
| 51 | + for b in node.generatetoaddress(200, self.address)[:100]: |
| 52 | + coinbase = node.getblock(blockhash=b, verbosity=2)["tx"][0] |
| 53 | + self.coins.append({ |
| 54 | + "txid": coinbase["txid"], |
| 55 | + "amount": coinbase["vout"][0]["value"], |
| 56 | + "scriptPubKey": coinbase["vout"][0]["scriptPubKey"], |
| 57 | + }) |
| 58 | + |
| 59 | + # Create some transactions that can be reused throughout the test. Never submit these to mempool. |
| 60 | + self.independent_txns_hex = [] |
| 61 | + self.independent_txns_testres = [] |
| 62 | + for _ in range(3): |
| 63 | + coin = self.coins.pop() |
| 64 | + rawtx = node.createrawtransaction([{"txid": coin["txid"], "vout": 0}], |
| 65 | + {self.address : coin["amount"] - Decimal("0.0001")}) |
| 66 | + signedtx = node.signrawtransactionwithkey(hexstring=rawtx, privkeys=self.privkeys) |
| 67 | + assert signedtx["complete"] |
| 68 | + testres = node.testmempoolaccept([signedtx["hex"]]) |
| 69 | + assert testres[0]["allowed"] |
| 70 | + self.independent_txns_hex.append(signedtx["hex"]) |
| 71 | + # testmempoolaccept returns a list of length one, avoid creating a 2D list |
| 72 | + self.independent_txns_testres.append(testres[0]) |
| 73 | + self.independent_txns_testres_blank = [{ |
| 74 | + "txid": res["txid"], "wtxid": res["wtxid"]} for res in self.independent_txns_testres] |
| 75 | + |
| 76 | + self.test_independent() |
| 77 | + self.test_chain() |
| 78 | + self.test_multiple_children() |
| 79 | + self.test_multiple_parents() |
| 80 | + self.test_conflicting() |
| 81 | + self.test_rbf() |
| 82 | + |
| 83 | + def chain_transaction(self, parent_txid, parent_value, n=0, parent_locking_script=None): |
| 84 | + """Build a transaction that spends parent_txid.vout[n] and produces one output with |
| 85 | + amount = parent_value with a fee deducted. |
| 86 | + Return tuple (CTransaction object, raw hex, nValue, scriptPubKey of the output created). |
| 87 | + """ |
| 88 | + node = self.nodes[0] |
| 89 | + inputs = [{"txid": parent_txid, "vout": n}] |
| 90 | + my_value = parent_value - Decimal("0.0001") |
| 91 | + outputs = {self.address : my_value} |
| 92 | + rawtx = node.createrawtransaction(inputs, outputs) |
| 93 | + prevtxs = [{ |
| 94 | + "txid": parent_txid, |
| 95 | + "vout": n, |
| 96 | + "scriptPubKey": parent_locking_script, |
| 97 | + "amount": parent_value, |
| 98 | + }] if parent_locking_script else None |
| 99 | + signedtx = node.signrawtransactionwithkey(hexstring=rawtx, privkeys=self.privkeys, prevtxs=prevtxs) |
| 100 | + tx = CTransaction() |
| 101 | + assert signedtx["complete"] |
| 102 | + tx.deserialize(BytesIO(hex_str_to_bytes(signedtx["hex"]))) |
| 103 | + return (tx, signedtx["hex"], my_value, tx.vout[0].scriptPubKey.hex()) |
| 104 | + |
| 105 | + def test_independent(self): |
| 106 | + self.log.info("Test multiple independent transactions in a package") |
| 107 | + node = self.nodes[0] |
| 108 | + # For independent transactions, order doesn't matter. |
| 109 | + self.assert_testres_equal(self.independent_txns_hex, self.independent_txns_testres) |
| 110 | + |
| 111 | + self.log.info("Test an otherwise valid package with an extra garbage tx appended") |
| 112 | + garbage_tx = node.createrawtransaction([{"txid": "00" * 32, "vout": 5}], {self.address: 1}) |
| 113 | + tx = CTransaction() |
| 114 | + tx.deserialize(BytesIO(hex_str_to_bytes(garbage_tx))) |
| 115 | + # Only the txid and wtxids are returned because validation is incomplete for the independent txns. |
| 116 | + # Package validation is atomic: if the node cannot find a UTXO for any single tx in the package, |
| 117 | + # it terminates immediately to avoid unnecessary, expensive signature verification. |
| 118 | + package_bad = self.independent_txns_hex + [garbage_tx] |
| 119 | + testres_bad = self.independent_txns_testres_blank + [{"txid": tx.rehash(), "wtxid": tx.getwtxid(), "allowed": False, "reject-reason": "missing-inputs"}] |
| 120 | + self.assert_testres_equal(package_bad, testres_bad) |
| 121 | + |
| 122 | + self.log.info("Check testmempoolaccept tells us when some transactions completed validation successfully") |
| 123 | + coin = self.coins.pop() |
| 124 | + tx_bad_sig_hex = node.createrawtransaction([{"txid": coin["txid"], "vout": 0}], |
| 125 | + {self.address : coin["amount"] - Decimal("0.0001")}) |
| 126 | + tx_bad_sig = CTransaction() |
| 127 | + tx_bad_sig.deserialize(BytesIO(hex_str_to_bytes(tx_bad_sig_hex))) |
| 128 | + testres_bad_sig = node.testmempoolaccept(self.independent_txns_hex + [tx_bad_sig_hex]) |
| 129 | + # By the time the signature for the last transaction is checked, all the other transactions |
| 130 | + # have been fully validated, which is why the node returns full validation results for all |
| 131 | + # transactions here but empty results in other cases. |
| 132 | + assert_equal(testres_bad_sig, self.independent_txns_testres + [{ |
| 133 | + "txid": tx_bad_sig.rehash(), |
| 134 | + "wtxid": tx_bad_sig.getwtxid(), "allowed": False, |
| 135 | + "reject-reason": "mandatory-script-verify-flag-failed (Operation not valid with the current stack size)" |
| 136 | + }]) |
| 137 | + |
| 138 | + self.log.info("Check testmempoolaccept reports txns in packages that exceed max feerate") |
| 139 | + coin = self.coins.pop() |
| 140 | + tx_high_fee_raw = node.createrawtransaction([{"txid": coin["txid"], "vout": 0}], |
| 141 | + {self.address : coin["amount"] - Decimal("0.999")}) |
| 142 | + tx_high_fee_signed = node.signrawtransactionwithkey(hexstring=tx_high_fee_raw, privkeys=self.privkeys) |
| 143 | + assert tx_high_fee_signed["complete"] |
| 144 | + tx_high_fee = CTransaction() |
| 145 | + tx_high_fee.deserialize(BytesIO(hex_str_to_bytes(tx_high_fee_signed["hex"]))) |
| 146 | + testres_high_fee = node.testmempoolaccept([tx_high_fee_signed["hex"]]) |
| 147 | + assert_equal(testres_high_fee, [ |
| 148 | + {"txid": tx_high_fee.rehash(), "wtxid": tx_high_fee.getwtxid(), "allowed": False, "reject-reason": "max-fee-exceeded"} |
| 149 | + ]) |
| 150 | + package_high_fee = [tx_high_fee_signed["hex"]] + self.independent_txns_hex |
| 151 | + testres_package_high_fee = node.testmempoolaccept(package_high_fee) |
| 152 | + assert_equal(testres_package_high_fee, testres_high_fee + self.independent_txns_testres_blank) |
| 153 | + |
| 154 | + def test_chain(self): |
| 155 | + node = self.nodes[0] |
| 156 | + first_coin = self.coins.pop() |
| 157 | + |
| 158 | + # Chain of 25 transactions |
| 159 | + parent_locking_script = None |
| 160 | + txid = first_coin["txid"] |
| 161 | + chain_hex = [] |
| 162 | + chain_txns = [] |
| 163 | + value = first_coin["amount"] |
| 164 | + |
| 165 | + for _ in range(25): |
| 166 | + (tx, txhex, value, parent_locking_script) = self.chain_transaction(txid, value, 0, parent_locking_script) |
| 167 | + txid = tx.rehash() |
| 168 | + chain_hex.append(txhex) |
| 169 | + chain_txns.append(tx) |
| 170 | + |
| 171 | + self.log.info("Check that testmempoolaccept requires packages to be sorted by dependency") |
| 172 | + testres_multiple_unsorted = node.testmempoolaccept(rawtxs=chain_hex[::-1]) |
| 173 | + assert_equal(testres_multiple_unsorted, |
| 174 | + [{"txid": chain_txns[-1].rehash(), "wtxid": chain_txns[-1].getwtxid(), "allowed": False, "reject-reason": "missing-inputs"}] |
| 175 | + + [{"txid": tx.rehash(), "wtxid": tx.getwtxid()} for tx in chain_txns[::-1]][1:]) |
| 176 | + |
| 177 | + self.log.info("Testmempoolaccept a chain of 25 transactions") |
| 178 | + testres_multiple = node.testmempoolaccept(rawtxs=chain_hex) |
| 179 | + |
| 180 | + testres_single = [] |
| 181 | + # Test accept and then submit each one individually, which should be identical to package test accept |
| 182 | + for rawtx in chain_hex: |
| 183 | + testres = node.testmempoolaccept([rawtx]) |
| 184 | + testres_single.append(testres[0]) |
| 185 | + # Submit the transaction now so its child should have no problem validating |
| 186 | + node.sendrawtransaction(rawtx) |
| 187 | + assert_equal(testres_single, testres_multiple) |
| 188 | + |
| 189 | + # Clean up by clearing the mempool |
| 190 | + node.generate(1) |
| 191 | + |
| 192 | + def test_multiple_children(self): |
| 193 | + node = self.nodes[0] |
| 194 | + |
| 195 | + self.log.info("Testmempoolaccept a package in which a transaction has two children within the package") |
| 196 | + first_coin = self.coins.pop() |
| 197 | + value = (first_coin["amount"] - Decimal("0.0002")) / 2 # Deduct reasonable fee and make 2 outputs |
| 198 | + inputs = [{"txid": first_coin["txid"], "vout": 0}] |
| 199 | + outputs = [{self.address : value}, {ADDRESS_BCRT1_P2WSH_OP_TRUE : value}] |
| 200 | + rawtx = node.createrawtransaction(inputs, outputs) |
| 201 | + |
| 202 | + parent_signed = node.signrawtransactionwithkey(hexstring=rawtx, privkeys=self.privkeys) |
| 203 | + parent_tx = CTransaction() |
| 204 | + assert parent_signed["complete"] |
| 205 | + parent_tx.deserialize(BytesIO(hex_str_to_bytes(parent_signed["hex"]))) |
| 206 | + parent_txid = parent_tx.rehash() |
| 207 | + assert node.testmempoolaccept([parent_signed["hex"]])[0]["allowed"] |
| 208 | + |
| 209 | + parent_locking_script_a = parent_tx.vout[0].scriptPubKey.hex() |
| 210 | + child_value = value - Decimal("0.0001") |
| 211 | + |
| 212 | + # Child A |
| 213 | + (_, tx_child_a_hex, _, _) = self.chain_transaction(parent_txid, child_value, 0, parent_locking_script_a) |
| 214 | + assert not node.testmempoolaccept([tx_child_a_hex])[0]["allowed"] |
| 215 | + |
| 216 | + # Child B |
| 217 | + rawtx_b = node.createrawtransaction([{"txid": parent_txid, "vout": 1}], {self.address : child_value}) |
| 218 | + tx_child_b = CTransaction() |
| 219 | + tx_child_b.deserialize(BytesIO(hex_str_to_bytes(rawtx_b))) |
| 220 | + tx_child_b.wit.vtxinwit = [CTxInWitness()] |
| 221 | + tx_child_b.wit.vtxinwit[0].scriptWitness.stack = [CScript([OP_TRUE])] |
| 222 | + tx_child_b_hex = tx_child_b.serialize().hex() |
| 223 | + assert not node.testmempoolaccept([tx_child_b_hex])[0]["allowed"] |
| 224 | + |
| 225 | + self.log.info("Testmempoolaccept with entire package, should work with children in either order") |
| 226 | + testres_multiple_ab = node.testmempoolaccept(rawtxs=[parent_signed["hex"], tx_child_a_hex, tx_child_b_hex]) |
| 227 | + testres_multiple_ba = node.testmempoolaccept(rawtxs=[parent_signed["hex"], tx_child_b_hex, tx_child_a_hex]) |
| 228 | + assert all([testres["allowed"] for testres in testres_multiple_ab + testres_multiple_ba]) |
| 229 | + |
| 230 | + testres_single = [] |
| 231 | + # Test accept and then submit each one individually, which should be identical to package testaccept |
| 232 | + for rawtx in [parent_signed["hex"], tx_child_a_hex, tx_child_b_hex]: |
| 233 | + testres = node.testmempoolaccept([rawtx]) |
| 234 | + testres_single.append(testres[0]) |
| 235 | + # Submit the transaction now so its child should have no problem validating |
| 236 | + node.sendrawtransaction(rawtx) |
| 237 | + assert_equal(testres_single, testres_multiple_ab) |
| 238 | + |
| 239 | + def create_child_with_parents(self, parents_tx, values, locking_scripts): |
| 240 | + """Creates a transaction that spends the first output of each parent in parents_tx.""" |
| 241 | + num_parents = len(parents_tx) |
| 242 | + total_value = sum(values) |
| 243 | + inputs = [{"txid": tx.rehash(), "vout": 0} for tx in parents_tx] |
| 244 | + outputs = {self.address : total_value - num_parents * Decimal("0.0001")} |
| 245 | + rawtx_child = self.nodes[0].createrawtransaction(inputs, outputs) |
| 246 | + prevtxs = [] |
| 247 | + for i in range(num_parents): |
| 248 | + prevtxs.append({"txid": parents_tx[i].rehash(), "vout": 0, "scriptPubKey": locking_scripts[i], "amount": values[i]}) |
| 249 | + signedtx_child = self.nodes[0].signrawtransactionwithkey(hexstring=rawtx_child, privkeys=self.privkeys, prevtxs=prevtxs) |
| 250 | + assert signedtx_child["complete"] |
| 251 | + return signedtx_child["hex"] |
| 252 | + |
| 253 | + def test_multiple_parents(self): |
| 254 | + node = self.nodes[0] |
| 255 | + |
| 256 | + self.log.info("Testmempoolaccept a package in which a transaction has multiple parents within the package") |
| 257 | + for num_parents in [2, 10, 24]: |
| 258 | + # Test a package with num_parents parents and 1 child transaction. |
| 259 | + package_hex = [] |
| 260 | + parents_tx = [] |
| 261 | + values = [] |
| 262 | + parent_locking_scripts = [] |
| 263 | + for _ in range(num_parents): |
| 264 | + parent_coin = self.coins.pop() |
| 265 | + value = parent_coin["amount"] |
| 266 | + (tx, txhex, value, parent_locking_script) = self.chain_transaction(parent_coin["txid"], value) |
| 267 | + package_hex.append(txhex) |
| 268 | + parents_tx.append(tx) |
| 269 | + values.append(value) |
| 270 | + parent_locking_scripts.append(parent_locking_script) |
| 271 | + child_hex = self.create_child_with_parents(parents_tx, values, parent_locking_scripts) |
| 272 | + # Package accept should work with the parents in any order (as long as parents come before child) |
| 273 | + for _ in range(10): |
| 274 | + random.shuffle(package_hex) |
| 275 | + testres_multiple = node.testmempoolaccept(rawtxs=package_hex + [child_hex]) |
| 276 | + assert all([testres["allowed"] for testres in testres_multiple]) |
| 277 | + |
| 278 | + testres_single = [] |
| 279 | + # Test accept and then submit each one individually, which should be identical to package testaccept |
| 280 | + for rawtx in package_hex + [child_hex]: |
| 281 | + testres_single.append(node.testmempoolaccept([rawtx])[0]) |
| 282 | + # Submit the transaction now so its child should have no problem validating |
| 283 | + node.sendrawtransaction(rawtx) |
| 284 | + assert_equal(testres_single, testres_multiple) |
| 285 | + |
| 286 | + def test_conflicting(self): |
| 287 | + node = self.nodes[0] |
| 288 | + prevtx = self.coins.pop() |
| 289 | + inputs = [{"txid": prevtx["txid"], "vout": 0}] |
| 290 | + output1 = {node.get_deterministic_priv_key().address: 50 - 0.00125} |
| 291 | + output2 = {ADDRESS_BCRT1_P2WSH_OP_TRUE: 50 - 0.00125} |
| 292 | + |
| 293 | + # tx1 and tx2 share the same inputs |
| 294 | + rawtx1 = node.createrawtransaction(inputs, output1) |
| 295 | + rawtx2 = node.createrawtransaction(inputs, output2) |
| 296 | + signedtx1 = node.signrawtransactionwithkey(hexstring=rawtx1, privkeys=self.privkeys) |
| 297 | + signedtx2 = node.signrawtransactionwithkey(hexstring=rawtx2, privkeys=self.privkeys) |
| 298 | + tx1 = CTransaction() |
| 299 | + tx1.deserialize(BytesIO(hex_str_to_bytes(signedtx1["hex"]))) |
| 300 | + tx2 = CTransaction() |
| 301 | + tx2.deserialize(BytesIO(hex_str_to_bytes(signedtx2["hex"]))) |
| 302 | + assert signedtx1["complete"] |
| 303 | + assert signedtx2["complete"] |
| 304 | + |
| 305 | + # Ensure tx1 and tx2 are valid by themselves |
| 306 | + assert node.testmempoolaccept([signedtx1["hex"]])[0]["allowed"] |
| 307 | + assert node.testmempoolaccept([signedtx2["hex"]])[0]["allowed"] |
| 308 | + |
| 309 | + self.log.info("Test duplicate transactions in the same package") |
| 310 | + testres = node.testmempoolaccept([signedtx1["hex"], signedtx1["hex"]]) |
| 311 | + assert_equal(testres, [ |
| 312 | + {"txid": tx1.rehash(), "wtxid": tx1.getwtxid(), "package-error": "conflict-in-package"}, |
| 313 | + {"txid": tx1.rehash(), "wtxid": tx1.getwtxid(), "package-error": "conflict-in-package"} |
| 314 | + ]) |
| 315 | + |
| 316 | + self.log.info("Test conflicting transactions in the same package") |
| 317 | + testres = node.testmempoolaccept([signedtx1["hex"], signedtx2["hex"]]) |
| 318 | + assert_equal(testres, [ |
| 319 | + {"txid": tx1.rehash(), "wtxid": tx1.getwtxid(), "package-error": "conflict-in-package"}, |
| 320 | + {"txid": tx2.rehash(), "wtxid": tx2.getwtxid(), "package-error": "conflict-in-package"} |
| 321 | + ]) |
| 322 | + |
| 323 | + def test_rbf(self): |
| 324 | + node = self.nodes[0] |
| 325 | + coin = self.coins.pop() |
| 326 | + inputs = [{"txid": coin["txid"], "vout": 0, "sequence": BIP125_SEQUENCE_NUMBER}] |
| 327 | + fee = Decimal('0.00125000') |
| 328 | + output = {node.get_deterministic_priv_key().address: 50 - fee} |
| 329 | + raw_replaceable_tx = node.createrawtransaction(inputs, output) |
| 330 | + signed_replaceable_tx = node.signrawtransactionwithkey(hexstring=raw_replaceable_tx, privkeys=self.privkeys) |
| 331 | + testres_replaceable = node.testmempoolaccept([signed_replaceable_tx["hex"]]) |
| 332 | + replaceable_tx = CTransaction() |
| 333 | + replaceable_tx.deserialize(BytesIO(hex_str_to_bytes(signed_replaceable_tx["hex"]))) |
| 334 | + assert_equal(testres_replaceable, [ |
| 335 | + {"txid": replaceable_tx.rehash(), "wtxid": replaceable_tx.getwtxid(), |
| 336 | + "allowed": True, "vsize": replaceable_tx.get_vsize(), "fees": { "base": fee }} |
| 337 | + ]) |
| 338 | + |
| 339 | + # Replacement transaction is identical except has double the fee |
| 340 | + replacement_tx = CTransaction() |
| 341 | + replacement_tx.deserialize(BytesIO(hex_str_to_bytes(signed_replaceable_tx["hex"]))) |
| 342 | + replacement_tx.vout[0].nValue -= int(fee * COIN) # Doubled fee |
| 343 | + signed_replacement_tx = node.signrawtransactionwithkey(replacement_tx.serialize().hex(), self.privkeys) |
| 344 | + replacement_tx.deserialize(BytesIO(hex_str_to_bytes(signed_replacement_tx["hex"]))) |
| 345 | + |
| 346 | + self.log.info("Test that transactions within a package cannot replace each other") |
| 347 | + testres_rbf_conflicting = node.testmempoolaccept([signed_replaceable_tx["hex"], signed_replacement_tx["hex"]]) |
| 348 | + assert_equal(testres_rbf_conflicting, [ |
| 349 | + {"txid": replaceable_tx.rehash(), "wtxid": replaceable_tx.getwtxid(), "package-error": "conflict-in-package"}, |
| 350 | + {"txid": replacement_tx.rehash(), "wtxid": replacement_tx.getwtxid(), "package-error": "conflict-in-package"} |
| 351 | + ]) |
| 352 | + |
| 353 | + self.log.info("Test that packages cannot conflict with mempool transactions, even if a valid BIP125 RBF") |
| 354 | + node.sendrawtransaction(signed_replaceable_tx["hex"]) |
| 355 | + testres_rbf_single = node.testmempoolaccept([signed_replacement_tx["hex"]]) |
| 356 | + # This transaction is a valid BIP125 replace-by-fee |
| 357 | + assert testres_rbf_single[0]["allowed"] |
| 358 | + testres_rbf_package = self.independent_txns_testres_blank + [{ |
| 359 | + "txid": replacement_tx.rehash(), "wtxid": replacement_tx.getwtxid(), "allowed": False, "reject-reason": "txn-mempool-conflict" |
| 360 | + }] |
| 361 | + self.assert_testres_equal(self.independent_txns_hex + [signed_replacement_tx["hex"]], testres_rbf_package) |
| 362 | + |
| 363 | +if __name__ == "__main__": |
| 364 | + RPCPackagesTest().main() |
0 commit comments