You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mempool_packages.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2014-2016 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 code."""
  6. from test_framework.test_framework import BitcoinTestFramework
  7. from test_framework.util import *
  8. from test_framework.mininode import COIN
  9. MAX_ANCESTORS = 25
  10. MAX_DESCENDANTS = 25
  11. class MempoolPackagesTest(BitcoinTestFramework):
  12. def set_test_params(self):
  13. self.num_nodes = 2
  14. self.extra_args = [["-maxorphantx=1000"], ["-maxorphantx=1000", "-limitancestorcount=5"]]
  15. # Build a transaction that spends parent_txid:vout
  16. # Return amount sent
  17. def chain_transaction(self, node, parent_txid, vout, value, fee, num_outputs):
  18. send_value = satoshi_round((value - fee)/num_outputs)
  19. inputs = [ {'txid' : parent_txid, 'vout' : vout} ]
  20. outputs = {}
  21. for i in range(num_outputs):
  22. outputs[node.getnewaddress()] = send_value
  23. rawtx = node.createrawtransaction(inputs, outputs)
  24. signedtx = node.signrawtransaction(rawtx)
  25. txid = node.sendrawtransaction(signedtx['hex'])
  26. fulltx = node.getrawtransaction(txid, 1)
  27. assert(len(fulltx['vout']) == num_outputs) # make sure we didn't generate a change output
  28. return (txid, send_value)
  29. def run_test(self):
  30. ''' Mine some blocks and have them mature. '''
  31. self.nodes[0].generate(101)
  32. utxo = self.nodes[0].listunspent(10)
  33. txid = utxo[0]['txid']
  34. vout = utxo[0]['vout']
  35. value = utxo[0]['amount']
  36. fee = Decimal("0.0001")
  37. # MAX_ANCESTORS transactions off a confirmed tx should be fine
  38. chain = []
  39. for i in range(MAX_ANCESTORS):
  40. (txid, sent_value) = self.chain_transaction(self.nodes[0], txid, 0, value, fee, 1)
  41. value = sent_value
  42. chain.append(txid)
  43. # Check mempool has MAX_ANCESTORS transactions in it, and descendant
  44. # count and fees should look correct
  45. mempool = self.nodes[0].getrawmempool(True)
  46. assert_equal(len(mempool), MAX_ANCESTORS)
  47. descendant_count = 1
  48. descendant_fees = 0
  49. descendant_size = 0
  50. descendants = []
  51. ancestors = list(chain)
  52. for x in reversed(chain):
  53. # Check that getmempoolentry is consistent with getrawmempool
  54. entry = self.nodes[0].getmempoolentry(x)
  55. assert_equal(entry, mempool[x])
  56. # Check that the descendant calculations are correct
  57. assert_equal(mempool[x]['descendantcount'], descendant_count)
  58. descendant_fees += mempool[x]['fee']
  59. assert_equal(mempool[x]['modifiedfee'], mempool[x]['fee'])
  60. assert_equal(mempool[x]['descendantfees'], descendant_fees * COIN)
  61. descendant_size += mempool[x]['size']
  62. assert_equal(mempool[x]['descendantsize'], descendant_size)
  63. descendant_count += 1
  64. # Check that getmempooldescendants is correct
  65. assert_equal(sorted(descendants), sorted(self.nodes[0].getmempooldescendants(x)))
  66. descendants.append(x)
  67. # Check that getmempoolancestors is correct
  68. ancestors.remove(x)
  69. assert_equal(sorted(ancestors), sorted(self.nodes[0].getmempoolancestors(x)))
  70. # Check that getmempoolancestors/getmempooldescendants correctly handle verbose=true
  71. v_ancestors = self.nodes[0].getmempoolancestors(chain[-1], True)
  72. assert_equal(len(v_ancestors), len(chain)-1)
  73. for x in v_ancestors.keys():
  74. assert_equal(mempool[x], v_ancestors[x])
  75. assert(chain[-1] not in v_ancestors.keys())
  76. v_descendants = self.nodes[0].getmempooldescendants(chain[0], True)
  77. assert_equal(len(v_descendants), len(chain)-1)
  78. for x in v_descendants.keys():
  79. assert_equal(mempool[x], v_descendants[x])
  80. assert(chain[0] not in v_descendants.keys())
  81. # Check that ancestor modified fees includes fee deltas from
  82. # prioritisetransaction
  83. self.nodes[0].prioritisetransaction(txid=chain[0], fee_delta=1000)
  84. mempool = self.nodes[0].getrawmempool(True)
  85. ancestor_fees = 0
  86. for x in chain:
  87. ancestor_fees += mempool[x]['fee']
  88. assert_equal(mempool[x]['ancestorfees'], ancestor_fees * COIN + 1000)
  89. # Undo the prioritisetransaction for later tests
  90. self.nodes[0].prioritisetransaction(txid=chain[0], fee_delta=-1000)
  91. # Check that descendant modified fees includes fee deltas from
  92. # prioritisetransaction
  93. self.nodes[0].prioritisetransaction(txid=chain[-1], fee_delta=1000)
  94. mempool = self.nodes[0].getrawmempool(True)
  95. descendant_fees = 0
  96. for x in reversed(chain):
  97. descendant_fees += mempool[x]['fee']
  98. assert_equal(mempool[x]['descendantfees'], descendant_fees * COIN + 1000)
  99. # Adding one more transaction on to the chain should fail.
  100. assert_raises_rpc_error(-26, "too-long-mempool-chain", self.chain_transaction, self.nodes[0], txid, vout, value, fee, 1)
  101. # Check that prioritising a tx before it's added to the mempool works
  102. # First clear the mempool by mining a block.
  103. self.nodes[0].generate(1)
  104. sync_blocks(self.nodes)
  105. assert_equal(len(self.nodes[0].getrawmempool()), 0)
  106. # Prioritise a transaction that has been mined, then add it back to the
  107. # mempool by using invalidateblock.
  108. self.nodes[0].prioritisetransaction(txid=chain[-1], fee_delta=2000)
  109. self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
  110. # Keep node1's tip synced with node0
  111. self.nodes[1].invalidateblock(self.nodes[1].getbestblockhash())
  112. # Now check that the transaction is in the mempool, with the right modified fee
  113. mempool = self.nodes[0].getrawmempool(True)
  114. descendant_fees = 0
  115. for x in reversed(chain):
  116. descendant_fees += mempool[x]['fee']
  117. if (x == chain[-1]):
  118. assert_equal(mempool[x]['modifiedfee'], mempool[x]['fee']+satoshi_round(0.00002))
  119. assert_equal(mempool[x]['descendantfees'], descendant_fees * COIN + 2000)
  120. # TODO: check that node1's mempool is as expected
  121. # TODO: test ancestor size limits
  122. # Now test descendant chain limits
  123. txid = utxo[1]['txid']
  124. value = utxo[1]['amount']
  125. vout = utxo[1]['vout']
  126. transaction_package = []
  127. # First create one parent tx with 10 children
  128. (txid, sent_value) = self.chain_transaction(self.nodes[0], txid, vout, value, fee, 10)
  129. parent_transaction = txid
  130. for i in range(10):
  131. transaction_package.append({'txid': txid, 'vout': i, 'amount': sent_value})
  132. # Sign and send up to MAX_DESCENDANT transactions chained off the parent tx
  133. for i in range(MAX_DESCENDANTS - 1):
  134. utxo = transaction_package.pop(0)
  135. (txid, sent_value) = self.chain_transaction(self.nodes[0], utxo['txid'], utxo['vout'], utxo['amount'], fee, 10)
  136. for j in range(10):
  137. transaction_package.append({'txid': txid, 'vout': j, 'amount': sent_value})
  138. mempool = self.nodes[0].getrawmempool(True)
  139. assert_equal(mempool[parent_transaction]['descendantcount'], MAX_DESCENDANTS)
  140. # Sending one more chained transaction will fail
  141. utxo = transaction_package.pop(0)
  142. assert_raises_rpc_error(-26, "too-long-mempool-chain", self.chain_transaction, self.nodes[0], utxo['txid'], utxo['vout'], utxo['amount'], fee, 10)
  143. # TODO: check that node1's mempool is as expected
  144. # TODO: test descendant size limits
  145. # Test reorg handling
  146. # First, the basics:
  147. self.nodes[0].generate(1)
  148. sync_blocks(self.nodes)
  149. self.nodes[1].invalidateblock(self.nodes[0].getbestblockhash())
  150. self.nodes[1].reconsiderblock(self.nodes[0].getbestblockhash())
  151. # Now test the case where node1 has a transaction T in its mempool that
  152. # depends on transactions A and B which are in a mined block, and the
  153. # block containing A and B is disconnected, AND B is not accepted back
  154. # into node1's mempool because its ancestor count is too high.
  155. # Create 8 transactions, like so:
  156. # Tx0 -> Tx1 (vout0)
  157. # \--> Tx2 (vout1) -> Tx3 -> Tx4 -> Tx5 -> Tx6 -> Tx7
  158. #
  159. # Mine them in the next block, then generate a new tx8 that spends
  160. # Tx1 and Tx7, and add to node1's mempool, then disconnect the
  161. # last block.
  162. # Create tx0 with 2 outputs
  163. utxo = self.nodes[0].listunspent()
  164. txid = utxo[0]['txid']
  165. value = utxo[0]['amount']
  166. vout = utxo[0]['vout']
  167. send_value = satoshi_round((value - fee)/2)
  168. inputs = [ {'txid' : txid, 'vout' : vout} ]
  169. outputs = {}
  170. for i in range(2):
  171. outputs[self.nodes[0].getnewaddress()] = send_value
  172. rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
  173. signedtx = self.nodes[0].signrawtransaction(rawtx)
  174. txid = self.nodes[0].sendrawtransaction(signedtx['hex'])
  175. tx0_id = txid
  176. value = send_value
  177. # Create tx1
  178. (tx1_id, tx1_value) = self.chain_transaction(self.nodes[0], tx0_id, 0, value, fee, 1)
  179. # Create tx2-7
  180. vout = 1
  181. txid = tx0_id
  182. for i in range(6):
  183. (txid, sent_value) = self.chain_transaction(self.nodes[0], txid, vout, value, fee, 1)
  184. vout = 0
  185. value = sent_value
  186. # Mine these in a block
  187. self.nodes[0].generate(1)
  188. self.sync_all()
  189. # Now generate tx8, with a big fee
  190. inputs = [ {'txid' : tx1_id, 'vout': 0}, {'txid' : txid, 'vout': 0} ]
  191. outputs = { self.nodes[0].getnewaddress() : send_value + value - 4*fee }
  192. rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
  193. signedtx = self.nodes[0].signrawtransaction(rawtx)
  194. txid = self.nodes[0].sendrawtransaction(signedtx['hex'])
  195. sync_mempools(self.nodes)
  196. # Now try to disconnect the tip on each node...
  197. self.nodes[1].invalidateblock(self.nodes[1].getbestblockhash())
  198. self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
  199. sync_blocks(self.nodes)
  200. if __name__ == '__main__':
  201. MempoolPackagesTest().main()