|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2014-2019 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 | +"""Test descendant package tracking carve-out allowing one final transaction in |
| 6 | + an otherwise-full package as long as it has only one parent and is <= 10k in |
| 7 | + size. |
| 8 | +""" |
| 9 | + |
| 10 | +from decimal import Decimal |
| 11 | + |
| 12 | +from test_framework.test_framework import BitcoinTestFramework |
| 13 | +from test_framework.util import assert_equal, assert_raises_rpc_error, satoshi_round |
| 14 | + |
| 15 | +MAX_ANCESTORS = 25 |
| 16 | +MAX_DESCENDANTS = 25 |
| 17 | + |
| 18 | +class MempoolPackagesTest(BitcoinTestFramework): |
| 19 | + def set_test_params(self): |
| 20 | + self.num_nodes = 1 |
| 21 | + self.extra_args = [["-maxorphantx=1000"]] |
| 22 | + |
| 23 | + def skip_test_if_missing_module(self): |
| 24 | + self.skip_if_no_wallet() |
| 25 | + |
| 26 | + # Build a transaction that spends parent_txid:vout |
| 27 | + # Return amount sent |
| 28 | + def chain_transaction(self, node, parent_txids, vouts, value, fee, num_outputs): |
| 29 | + send_value = satoshi_round((value - fee)/num_outputs) |
| 30 | + inputs = [] |
| 31 | + for (txid, vout) in zip(parent_txids, vouts): |
| 32 | + inputs.append({'txid' : txid, 'vout' : vout}) |
| 33 | + outputs = {} |
| 34 | + for i in range(num_outputs): |
| 35 | + outputs[node.getnewaddress()] = send_value |
| 36 | + rawtx = node.createrawtransaction(inputs, outputs) |
| 37 | + signedtx = node.signrawtransactionwithwallet(rawtx) |
| 38 | + txid = node.sendrawtransaction(signedtx['hex']) |
| 39 | + fulltx = node.getrawtransaction(txid, 1) |
| 40 | + assert len(fulltx['vout']) == num_outputs # make sure we didn't generate a change output |
| 41 | + return (txid, send_value) |
| 42 | + |
| 43 | + def run_test(self): |
| 44 | + # Mine some blocks and have them mature. |
| 45 | + self.nodes[0].generate(101) |
| 46 | + utxo = self.nodes[0].listunspent(10) |
| 47 | + txid = utxo[0]['txid'] |
| 48 | + vout = utxo[0]['vout'] |
| 49 | + value = utxo[0]['amount'] |
| 50 | + |
| 51 | + fee = Decimal("0.0002") |
| 52 | + # MAX_ANCESTORS transactions off a confirmed tx should be fine |
| 53 | + chain = [] |
| 54 | + for _ in range(4): |
| 55 | + (txid, sent_value) = self.chain_transaction(self.nodes[0], [txid], [vout], value, fee, 2) |
| 56 | + vout = 0 |
| 57 | + value = sent_value |
| 58 | + chain.append([txid, value]) |
| 59 | + for _ in range(MAX_ANCESTORS - 4): |
| 60 | + (txid, sent_value) = self.chain_transaction(self.nodes[0], [txid], [0], value, fee, 1) |
| 61 | + value = sent_value |
| 62 | + chain.append([txid, value]) |
| 63 | + (second_chain, second_chain_value) = self.chain_transaction(self.nodes[0], [utxo[1]['txid']], [utxo[1]['vout']], utxo[1]['amount'], fee, 1) |
| 64 | + |
| 65 | + # Check mempool has MAX_ANCESTORS + 1 transactions in it |
| 66 | + assert_equal(len(self.nodes[0].getrawmempool(True)), MAX_ANCESTORS + 1) |
| 67 | + |
| 68 | + # Adding one more transaction on to the chain should fail. |
| 69 | + assert_raises_rpc_error(-26, "too-long-mempool-chain", self.chain_transaction, self.nodes[0], [txid], [0], value, fee, 1) |
| 70 | + # ...even if it chains on from some point in the middle of the chain. |
| 71 | + assert_raises_rpc_error(-26, "too-long-mempool-chain", self.chain_transaction, self.nodes[0], [chain[2][0]], [1], chain[2][1], fee, 1) |
| 72 | + assert_raises_rpc_error(-26, "too-long-mempool-chain", self.chain_transaction, self.nodes[0], [chain[1][0]], [1], chain[1][1], fee, 1) |
| 73 | + # ...even if it chains on to two parent transactions with one in the chain. |
| 74 | + assert_raises_rpc_error(-26, "too-long-mempool-chain", self.chain_transaction, self.nodes[0], [chain[0][0], second_chain], [1, 0], chain[0][1] + second_chain_value, fee, 1) |
| 75 | + # ...especially if its > 40k weight |
| 76 | + assert_raises_rpc_error(-26, "too-long-mempool-chain", self.chain_transaction, self.nodes[0], [chain[0][0]], [1], chain[0][1], fee, 350) |
| 77 | + # But not if it chains directly off the first transaction |
| 78 | + self.chain_transaction(self.nodes[0], [chain[0][0]], [1], chain[0][1], fee, 1) |
| 79 | + # and the second chain should work just fine |
| 80 | + self.chain_transaction(self.nodes[0], [second_chain], [0], second_chain_value, fee, 1) |
| 81 | + |
| 82 | + # Finally, check that we added two transactions |
| 83 | + assert_equal(len(self.nodes[0].getrawmempool(True)), MAX_ANCESTORS + 3) |
| 84 | + |
| 85 | +if __name__ == '__main__': |
| 86 | + MempoolPackagesTest().main() |
0 commit comments