PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/qa/rpc-tests/mempool_packages.py

http://github.com/bitcoin/bitcoin
Python | 243 lines | 154 code | 39 blank | 50 comment | 19 complexity | f8108536778193a41ce7ce8d5fc02e45 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, MIT
  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", "-relaypriority=0", "-debug"]))
  19. self.nodes.append(start_node(1, self.options.tmpdir, ["-maxorphantx=1000", "-relaypriority=0", "-limitancestorcount=5", "-debug"]))
  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], 0, 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. try:
  99. self.chain_transaction(self.nodes[0], txid, vout, value, fee, 1)
  100. except JSONRPCException as e:
  101. print("too-long-ancestor-chain successfully rejected")
  102. # Check that prioritising a tx before it's added to the mempool works
  103. # First clear the mempool by mining a block.
  104. self.nodes[0].generate(1)
  105. sync_blocks(self.nodes)
  106. assert_equal(len(self.nodes[0].getrawmempool()), 0)
  107. # Prioritise a transaction that has been mined, then add it back to the
  108. # mempool by using invalidateblock.
  109. self.nodes[0].prioritisetransaction(chain[-1], 0, 2000)
  110. self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
  111. # Keep node1's tip synced with node0
  112. self.nodes[1].invalidateblock(self.nodes[1].getbestblockhash())
  113. # Now check that the transaction is in the mempool, with the right modified fee
  114. mempool = self.nodes[0].getrawmempool(True)
  115. descendant_fees = 0
  116. for x in reversed(chain):
  117. descendant_fees += mempool[x]['fee']
  118. if (x == chain[-1]):
  119. assert_equal(mempool[x]['modifiedfee'], mempool[x]['fee']+satoshi_round(0.00002))
  120. assert_equal(mempool[x]['descendantfees'], descendant_fees * COIN + 2000)
  121. # TODO: check that node1's mempool is as expected
  122. # TODO: test ancestor size limits
  123. # Now test descendant chain limits
  124. txid = utxo[1]['txid']
  125. value = utxo[1]['amount']
  126. vout = utxo[1]['vout']
  127. transaction_package = []
  128. # First create one parent tx with 10 children
  129. (txid, sent_value) = self.chain_transaction(self.nodes[0], txid, vout, value, fee, 10)
  130. parent_transaction = txid
  131. for i in range(10):
  132. transaction_package.append({'txid': txid, 'vout': i, 'amount': sent_value})
  133. for i in range(MAX_DESCENDANTS):
  134. utxo = transaction_package.pop(0)
  135. try:
  136. (txid, sent_value) = self.chain_transaction(self.nodes[0], utxo['txid'], utxo['vout'], utxo['amount'], fee, 10)
  137. for j in range(10):
  138. transaction_package.append({'txid': txid, 'vout': j, 'amount': sent_value})
  139. if i == MAX_DESCENDANTS - 2:
  140. mempool = self.nodes[0].getrawmempool(True)
  141. assert_equal(mempool[parent_transaction]['descendantcount'], MAX_DESCENDANTS)
  142. except JSONRPCException as e:
  143. print(e.error['message'])
  144. assert_equal(i, MAX_DESCENDANTS - 1)
  145. print("tx that would create too large descendant package successfully rejected")
  146. # TODO: check that node1's mempool is as expected
  147. # TODO: test descendant size limits
  148. # Test reorg handling
  149. # First, the basics:
  150. self.nodes[0].generate(1)
  151. sync_blocks(self.nodes)
  152. self.nodes[1].invalidateblock(self.nodes[0].getbestblockhash())
  153. self.nodes[1].reconsiderblock(self.nodes[0].getbestblockhash())
  154. # Now test the case where node1 has a transaction T in its mempool that
  155. # depends on transactions A and B which are in a mined block, and the
  156. # block containing A and B is disconnected, AND B is not accepted back
  157. # into node1's mempool because its ancestor count is too high.
  158. # Create 8 transactions, like so:
  159. # Tx0 -> Tx1 (vout0)
  160. # \--> Tx2 (vout1) -> Tx3 -> Tx4 -> Tx5 -> Tx6 -> Tx7
  161. #
  162. # Mine them in the next block, then generate a new tx8 that spends
  163. # Tx1 and Tx7, and add to node1's mempool, then disconnect the
  164. # last block.
  165. # Create tx0 with 2 outputs
  166. utxo = self.nodes[0].listunspent()
  167. txid = utxo[0]['txid']
  168. value = utxo[0]['amount']
  169. vout = utxo[0]['vout']
  170. send_value = satoshi_round((value - fee)/2)
  171. inputs = [ {'txid' : txid, 'vout' : vout} ]
  172. outputs = {}
  173. for i in range(2):
  174. outputs[self.nodes[0].getnewaddress()] = send_value
  175. rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
  176. signedtx = self.nodes[0].signrawtransaction(rawtx)
  177. txid = self.nodes[0].sendrawtransaction(signedtx['hex'])
  178. tx0_id = txid
  179. value = send_value
  180. # Create tx1
  181. (tx1_id, tx1_value) = self.chain_transaction(self.nodes[0], tx0_id, 0, value, fee, 1)
  182. # Create tx2-7
  183. vout = 1
  184. txid = tx0_id
  185. for i in range(6):
  186. (txid, sent_value) = self.chain_transaction(self.nodes[0], txid, vout, value, fee, 1)
  187. vout = 0
  188. value = sent_value
  189. # Mine these in a block
  190. self.nodes[0].generate(1)
  191. self.sync_all()
  192. # Now generate tx8, with a big fee
  193. inputs = [ {'txid' : tx1_id, 'vout': 0}, {'txid' : txid, 'vout': 0} ]
  194. outputs = { self.nodes[0].getnewaddress() : send_value + value - 4*fee }
  195. rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
  196. signedtx = self.nodes[0].signrawtransaction(rawtx)
  197. txid = self.nodes[0].sendrawtransaction(signedtx['hex'])
  198. sync_mempools(self.nodes)
  199. # Now try to disconnect the tip on each node...
  200. self.nodes[1].invalidateblock(self.nodes[1].getbestblockhash())
  201. self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
  202. sync_blocks(self.nodes)
  203. if __name__ == '__main__':
  204. MempoolPackagesTest().main()