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