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
15 self
.extra_args
= [["-printpriority=1"], ["-printpriority=1"]]
18 # Test `prioritisetransaction` required parameters
19 assert_raises_rpc_error(-1, "prioritisetransaction", self
.nodes
[0].prioritisetransaction
)
20 assert_raises_rpc_error(-1, "prioritisetransaction", self
.nodes
[0].prioritisetransaction
, '')
21 assert_raises_rpc_error(-1, "prioritisetransaction", self
.nodes
[0].prioritisetransaction
, '', 0)
23 # Test `prioritisetransaction` invalid extra parameters
24 assert_raises_rpc_error(-1, "prioritisetransaction", self
.nodes
[0].prioritisetransaction
, '', 0, 0, 0)
26 # Test `prioritisetransaction` invalid `txid`
27 assert_raises_rpc_error(-1, "txid must be hexadecimal string", self
.nodes
[0].prioritisetransaction
, txid
='foo', fee_delta
=0)
29 # Test `prioritisetransaction` invalid `dummy`
30 txid
= '1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000'
31 assert_raises_rpc_error(-1, "JSON value is not a number as expected", self
.nodes
[0].prioritisetransaction
, txid
, 'foo', 0)
32 assert_raises_rpc_error(-8, "Priority is no longer supported, dummy argument to prioritisetransaction must be 0.", self
.nodes
[0].prioritisetransaction
, txid
, 1, 0)
34 # Test `prioritisetransaction` invalid `fee_delta`
35 assert_raises_rpc_error(-1, "JSON value is not an integer as expected", self
.nodes
[0].prioritisetransaction
, txid
=txid
, fee_delta
='foo')
37 self
.txouts
= gen_return_txouts()
38 self
.relayfee
= self
.nodes
[0].getnetworkinfo()['relayfee']
41 utxos
= create_confirmed_utxos(self
.relayfee
, self
.nodes
[0], utxo_count
)
42 base_fee
= self
.relayfee
*100 # our transactions are smaller than 100kb
45 # Create 3 batches of transactions at 3 different fee rate levels
46 range_size
= utxo_count
// 3
49 start_range
= i
* range_size
50 end_range
= start_range
+ range_size
51 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
)
53 # Make sure that the size of each group of transactions exceeds
54 # MAX_BLOCK_BASE_SIZE -- otherwise the test needs to be revised to create
56 mempool
= self
.nodes
[0].getrawmempool(True)
61 sizes
[i
] += mempool
[j
]['size']
62 assert(sizes
[i
] > MAX_BLOCK_BASE_SIZE
) # Fail => raise utxo_count
64 # add a fee delta to something in the cheapest bucket and make sure it gets mined
65 # also check that a different entry in the cheapest bucket is NOT mined
66 self
.nodes
[0].prioritisetransaction(txid
=txids
[0][0], fee_delta
=int(3*base_fee
*COIN
))
68 self
.nodes
[0].generate(1)
70 mempool
= self
.nodes
[0].getrawmempool()
71 self
.log
.info("Assert that prioritised transaction was mined")
72 assert(txids
[0][0] not in mempool
)
73 assert(txids
[0][1] in mempool
)
80 # Something high-fee should have been mined!
81 assert(high_fee_tx
!= None)
83 # Add a prioritisation before a tx is in the mempool (de-prioritising a
84 # high-fee transaction so that it's now low fee).
85 self
.nodes
[0].prioritisetransaction(txid
=high_fee_tx
, fee_delta
=-int(2*base_fee
*COIN
))
87 # Add everything back to mempool
88 self
.nodes
[0].invalidateblock(self
.nodes
[0].getbestblockhash())
90 # Check to make sure our high fee rate tx is back in the mempool
91 mempool
= self
.nodes
[0].getrawmempool()
92 assert(high_fee_tx
in mempool
)
94 # Now verify the modified-high feerate transaction isn't mined before
95 # the other high fee transactions. Keep mining until our mempool has
96 # decreased by all the high fee size that we calculated above.
97 while (self
.nodes
[0].getmempoolinfo()['bytes'] > sizes
[0] + sizes
[1]):
98 self
.nodes
[0].generate(1)
100 # High fee transaction should not have been mined, but other high fee rate
101 # transactions should have been.
102 mempool
= self
.nodes
[0].getrawmempool()
103 self
.log
.info("Assert that de-prioritised transaction is still in mempool")
104 assert(high_fee_tx
in mempool
)
106 if (x
!= high_fee_tx
):
107 assert(x
not in mempool
)
109 # Create a free transaction. Should be rejected.
110 utxo_list
= self
.nodes
[0].listunspent()
111 assert(len(utxo_list
) > 0)
116 inputs
.append({"txid" : utxo
["txid"], "vout" : utxo
["vout"]})
117 outputs
[self
.nodes
[0].getnewaddress()] = utxo
["amount"]
118 raw_tx
= self
.nodes
[0].createrawtransaction(inputs
, outputs
)
119 tx_hex
= self
.nodes
[0].signrawtransaction(raw_tx
)["hex"]
120 tx_id
= self
.nodes
[0].decoderawtransaction(tx_hex
)["txid"]
122 # This will raise an exception due to min relay fee not being met
123 assert_raises_rpc_error(-26, "66: min relay fee not met", self
.nodes
[0].sendrawtransaction
, tx_hex
)
124 assert(tx_id
not in self
.nodes
[0].getrawmempool())
126 # This is a less than 1000-byte transaction, so just set the fee
127 # to be the minimum for a 1000 byte transaction and check that it is
129 self
.nodes
[0].prioritisetransaction(txid
=tx_id
, fee_delta
=int(self
.relayfee
*COIN
))
131 self
.log
.info("Assert that prioritised free transaction is accepted to mempool")
132 assert_equal(self
.nodes
[0].sendrawtransaction(tx_hex
), tx_id
)
133 assert(tx_id
in self
.nodes
[0].getrawmempool())
135 # Test that calling prioritisetransaction is sufficient to trigger
136 # getblocktemplate to (eventually) return a new block.
137 mock_time
= int(time
.time())
138 self
.nodes
[0].setmocktime(mock_time
)
139 template
= self
.nodes
[0].getblocktemplate()
140 self
.nodes
[0].prioritisetransaction(txid
=tx_id
, fee_delta
=-int(self
.relayfee
*COIN
))
141 self
.nodes
[0].setmocktime(mock_time
+10)
142 new_template
= self
.nodes
[0].getblocktemplate()
144 assert(template
!= new_template
)
146 if __name__
== '__main__':
147 PrioritiseTransactionTest().main()