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

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