/ test / functional / mempool_package_onemore.py
mempool_package_onemore.py
 1  #!/usr/bin/env python3
 2  # Copyright (c) 2014-2022 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 test_framework.messages import (
11      DEFAULT_ANCESTOR_LIMIT,
12  )
13  from test_framework.test_framework import BitcoinTestFramework
14  from test_framework.util import (
15      assert_equal,
16      assert_raises_rpc_error,
17  )
18  from test_framework.wallet import MiniWallet
19  
20  
21  class MempoolPackagesTest(BitcoinTestFramework):
22      def set_test_params(self):
23          self.num_nodes = 1
24  
25      def chain_tx(self, utxos_to_spend, *, num_outputs=1):
26          return self.wallet.send_self_transfer_multi(
27              from_node=self.nodes[0],
28              utxos_to_spend=utxos_to_spend,
29              num_outputs=num_outputs)['new_utxos']
30  
31      def run_test(self):
32          self.wallet = MiniWallet(self.nodes[0])
33  
34          # DEFAULT_ANCESTOR_LIMIT transactions off a confirmed tx should be fine
35          chain = []
36          utxo = self.wallet.get_utxo()
37          for _ in range(4):
38              utxo, utxo2 = self.chain_tx([utxo], num_outputs=2)
39              chain.append(utxo2)
40          for _ in range(DEFAULT_ANCESTOR_LIMIT - 4):
41              utxo, = self.chain_tx([utxo])
42              chain.append(utxo)
43          second_chain, = self.chain_tx([self.wallet.get_utxo(confirmed_only=True)])
44  
45          # Check mempool has DEFAULT_ANCESTOR_LIMIT + 1 transactions in it
46          assert_equal(len(self.nodes[0].getrawmempool()), DEFAULT_ANCESTOR_LIMIT + 1)
47  
48          # Adding one more transaction on to the chain should fail.
49          assert_raises_rpc_error(-26, "too-long-mempool-chain, too many unconfirmed ancestors [limit: 25]", self.chain_tx, [utxo])
50          # ... or if it chains on from some point in the middle of the chain.
51          assert_raises_rpc_error(-26, "too-long-mempool-chain, too many descendants", self.chain_tx, [chain[2]])
52          assert_raises_rpc_error(-26, "too-long-mempool-chain, too many descendants", self.chain_tx, [chain[1]])
53          # ...even if it chains on to two parent transactions with one in the chain.
54          assert_raises_rpc_error(-26, "too-long-mempool-chain, too many descendants", self.chain_tx, [chain[0], second_chain])
55          # ...especially if its > 40k weight
56          assert_raises_rpc_error(-26, "too-long-mempool-chain, too many descendants", self.chain_tx, [chain[0]], num_outputs=350)
57          # ...even if it's submitted with other transactions
58          replaceable_tx = self.wallet.create_self_transfer_multi(utxos_to_spend=[chain[0]])
59          txns = [replaceable_tx["tx"], self.wallet.create_self_transfer_multi(utxos_to_spend=replaceable_tx["new_utxos"])["tx"]]
60          txns_hex = [tx.serialize().hex() for tx in txns]
61          assert_equal(self.nodes[0].testmempoolaccept(txns_hex)[0]["reject-reason"], "too-long-mempool-chain")
62          pkg_result = self.nodes[0].submitpackage(txns_hex)
63          assert "too-long-mempool-chain" in pkg_result["tx-results"][txns[0].getwtxid()]["error"]
64          assert_equal(pkg_result["tx-results"][txns[1].getwtxid()]["error"], "bad-txns-inputs-missingorspent")
65          # But not if it chains directly off the first transaction
66          self.nodes[0].sendrawtransaction(replaceable_tx["hex"])
67          # and the second chain should work just fine
68          self.chain_tx([second_chain])
69  
70          # Ensure an individual transaction with single direct conflict can RBF the chain which used our carve-out rule
71          replacement_tx = replaceable_tx["tx"]
72          replacement_tx.vout[0].nValue -= 1000000
73          self.nodes[0].sendrawtransaction(replacement_tx.serialize().hex())
74  
75          # Finally, check that we added two transactions
76          assert_equal(len(self.nodes[0].getrawmempool()), DEFAULT_ANCESTOR_LIMIT + 3)
77  
78  
79  if __name__ == '__main__':
80      MempoolPackagesTest(__file__).main()