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.1KB

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