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 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env python2
  2. # Copyright (c) 2014-2015 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. def satoshi_round(amount):
  9. return Decimal(amount).quantize(Decimal('0.00000001'), rounding=ROUND_DOWN)
  10. class MempoolPackagesTest(BitcoinTestFramework):
  11. def setup_network(self):
  12. self.nodes = []
  13. self.nodes.append(start_node(0, self.options.tmpdir, ["-maxorphantx=1000", "-relaypriority=0"]))
  14. self.is_network_split = False
  15. self.sync_all()
  16. # Build a transaction that spends parent_txid:vout
  17. # Return amount sent
  18. def chain_transaction(self, parent_txid, vout, value, fee, num_outputs):
  19. send_value = satoshi_round((value - fee)/num_outputs)
  20. inputs = [ {'txid' : parent_txid, 'vout' : vout} ]
  21. outputs = {}
  22. for i in xrange(num_outputs):
  23. outputs[self.nodes[0].getnewaddress()] = send_value
  24. rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
  25. signedtx = self.nodes[0].signrawtransaction(rawtx)
  26. txid = self.nodes[0].sendrawtransaction(signedtx['hex'])
  27. fulltx = self.nodes[0].getrawtransaction(txid, 1)
  28. assert(len(fulltx['vout']) == num_outputs) # make sure we didn't generate a change output
  29. return (txid, send_value)
  30. def run_test(self):
  31. ''' Mine some blocks and have them mature. '''
  32. self.nodes[0].generate(101)
  33. utxo = self.nodes[0].listunspent(10)
  34. txid = utxo[0]['txid']
  35. vout = utxo[0]['vout']
  36. value = utxo[0]['amount']
  37. fee = Decimal("0.0001")
  38. # 100 transactions off a confirmed tx should be fine
  39. chain = []
  40. for i in xrange(100):
  41. (txid, sent_value) = self.chain_transaction(txid, 0, value, fee, 1)
  42. value = sent_value
  43. chain.append(txid)
  44. # Check mempool has 100 transactions in it, and descendant
  45. # count and fees should look correct
  46. mempool = self.nodes[0].getrawmempool(True)
  47. assert_equal(len(mempool), 100)
  48. descendant_count = 1
  49. descendant_fees = 0
  50. descendant_size = 0
  51. SATOSHIS = 100000000
  52. for x in reversed(chain):
  53. assert_equal(mempool[x]['descendantcount'], descendant_count)
  54. descendant_fees += mempool[x]['fee']
  55. assert_equal(mempool[x]['descendantfees'], SATOSHIS*descendant_fees)
  56. descendant_size += mempool[x]['size']
  57. assert_equal(mempool[x]['descendantsize'], descendant_size)
  58. descendant_count += 1
  59. # Adding one more transaction on to the chain should fail.
  60. try:
  61. self.chain_transaction(txid, vout, value, fee, 1)
  62. except JSONRPCException as e:
  63. print "too-long-ancestor-chain successfully rejected"
  64. # TODO: test ancestor size limits
  65. # Now test descendant chain limits
  66. txid = utxo[1]['txid']
  67. value = utxo[1]['amount']
  68. vout = utxo[1]['vout']
  69. transaction_package = []
  70. # First create one parent tx with 10 children
  71. (txid, sent_value) = self.chain_transaction(txid, vout, value, fee, 10)
  72. parent_transaction = txid
  73. for i in xrange(10):
  74. transaction_package.append({'txid': txid, 'vout': i, 'amount': sent_value})
  75. for i in xrange(1000):
  76. utxo = transaction_package.pop(0)
  77. try:
  78. (txid, sent_value) = self.chain_transaction(utxo['txid'], utxo['vout'], utxo['amount'], fee, 10)
  79. for j in xrange(10):
  80. transaction_package.append({'txid': txid, 'vout': j, 'amount': sent_value})
  81. if i == 998:
  82. mempool = self.nodes[0].getrawmempool(True)
  83. assert_equal(mempool[parent_transaction]['descendantcount'], 1000)
  84. except JSONRPCException as e:
  85. print e.error['message']
  86. assert_equal(i, 999)
  87. print "tx that would create too large descendant package successfully rejected"
  88. # TODO: test descendant size limits
  89. if __name__ == '__main__':
  90. MempoolPackagesTest().main()