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.

prioritise_transaction.py 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2015-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 the prioritisetransaction mining RPC."""
  6. from test_framework.test_framework import BitcoinTestFramework
  7. from test_framework.util import *
  8. from test_framework.mininode import COIN, MAX_BLOCK_BASE_SIZE
  9. class PrioritiseTransactionTest(BitcoinTestFramework):
  10. def set_test_params(self):
  11. self.setup_clean_chain = True
  12. self.num_nodes = 2
  13. self.extra_args = [["-printpriority=1"], ["-printpriority=1"]]
  14. def run_test(self):
  15. self.txouts = gen_return_txouts()
  16. self.relayfee = self.nodes[0].getnetworkinfo()['relayfee']
  17. utxo_count = 90
  18. utxos = create_confirmed_utxos(self.relayfee, self.nodes[0], utxo_count)
  19. base_fee = self.relayfee*100 # our transactions are smaller than 100kb
  20. txids = []
  21. # Create 3 batches of transactions at 3 different fee rate levels
  22. range_size = utxo_count // 3
  23. for i in range(3):
  24. txids.append([])
  25. start_range = i * range_size
  26. end_range = start_range + range_size
  27. txids[i] = create_lots_of_big_transactions(self.nodes[0], self.txouts, utxos[start_range:end_range], end_range - start_range, (i+1)*base_fee)
  28. # Make sure that the size of each group of transactions exceeds
  29. # MAX_BLOCK_BASE_SIZE -- otherwise the test needs to be revised to create
  30. # more transactions.
  31. mempool = self.nodes[0].getrawmempool(True)
  32. sizes = [0, 0, 0]
  33. for i in range(3):
  34. for j in txids[i]:
  35. assert(j in mempool)
  36. sizes[i] += mempool[j]['size']
  37. assert(sizes[i] > MAX_BLOCK_BASE_SIZE) # Fail => raise utxo_count
  38. # add a fee delta to something in the cheapest bucket and make sure it gets mined
  39. # also check that a different entry in the cheapest bucket is NOT mined
  40. self.nodes[0].prioritisetransaction(txid=txids[0][0], fee_delta=int(3*base_fee*COIN))
  41. self.nodes[0].generate(1)
  42. mempool = self.nodes[0].getrawmempool()
  43. self.log.info("Assert that prioritised transaction was mined")
  44. assert(txids[0][0] not in mempool)
  45. assert(txids[0][1] in mempool)
  46. high_fee_tx = None
  47. for x in txids[2]:
  48. if x not in mempool:
  49. high_fee_tx = x
  50. # Something high-fee should have been mined!
  51. assert(high_fee_tx != None)
  52. # Add a prioritisation before a tx is in the mempool (de-prioritising a
  53. # high-fee transaction so that it's now low fee).
  54. self.nodes[0].prioritisetransaction(txid=high_fee_tx, fee_delta=-int(2*base_fee*COIN))
  55. # Add everything back to mempool
  56. self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash())
  57. # Check to make sure our high fee rate tx is back in the mempool
  58. mempool = self.nodes[0].getrawmempool()
  59. assert(high_fee_tx in mempool)
  60. # Now verify the modified-high feerate transaction isn't mined before
  61. # the other high fee transactions. Keep mining until our mempool has
  62. # decreased by all the high fee size that we calculated above.
  63. while (self.nodes[0].getmempoolinfo()['bytes'] > sizes[0] + sizes[1]):
  64. self.nodes[0].generate(1)
  65. # High fee transaction should not have been mined, but other high fee rate
  66. # transactions should have been.
  67. mempool = self.nodes[0].getrawmempool()
  68. self.log.info("Assert that de-prioritised transaction is still in mempool")
  69. assert(high_fee_tx in mempool)
  70. for x in txids[2]:
  71. if (x != high_fee_tx):
  72. assert(x not in mempool)
  73. # Create a free transaction. Should be rejected.
  74. utxo_list = self.nodes[0].listunspent()
  75. assert(len(utxo_list) > 0)
  76. utxo = utxo_list[0]
  77. inputs = []
  78. outputs = {}
  79. inputs.append({"txid" : utxo["txid"], "vout" : utxo["vout"]})
  80. outputs[self.nodes[0].getnewaddress()] = utxo["amount"]
  81. raw_tx = self.nodes[0].createrawtransaction(inputs, outputs)
  82. tx_hex = self.nodes[0].signrawtransaction(raw_tx)["hex"]
  83. tx_id = self.nodes[0].decoderawtransaction(tx_hex)["txid"]
  84. # This will raise an exception due to min relay fee not being met
  85. assert_raises_rpc_error(-26, "66: min relay fee not met", self.nodes[0].sendrawtransaction, tx_hex)
  86. assert(tx_id not in self.nodes[0].getrawmempool())
  87. # This is a less than 1000-byte transaction, so just set the fee
  88. # to be the minimum for a 1000 byte transaction and check that it is
  89. # accepted.
  90. self.nodes[0].prioritisetransaction(txid=tx_id, fee_delta=int(self.relayfee*COIN))
  91. self.log.info("Assert that prioritised free transaction is accepted to mempool")
  92. assert_equal(self.nodes[0].sendrawtransaction(tx_hex), tx_id)
  93. assert(tx_id in self.nodes[0].getrawmempool())
  94. # Test that calling prioritisetransaction is sufficient to trigger
  95. # getblocktemplate to (eventually) return a new block.
  96. mock_time = int(time.time())
  97. self.nodes[0].setmocktime(mock_time)
  98. template = self.nodes[0].getblocktemplate()
  99. self.nodes[0].prioritisetransaction(txid=tx_id, fee_delta=-int(self.relayfee*COIN))
  100. self.nodes[0].setmocktime(mock_time+10)
  101. new_template = self.nodes[0].getblocktemplate()
  102. assert(template != new_template)
  103. if __name__ == '__main__':
  104. PrioritiseTransactionTest().main()