1#!/usr/bin/env python3 2# Copyright (c) 2015-2019 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 7import time 8 9from test_framework.messages import COIN, MAX_BLOCK_BASE_SIZE 10from test_framework.test_framework import BitcoinTestFramework 11from test_framework.util import assert_equal, assert_raises_rpc_error, create_confirmed_utxos, create_lots_of_big_transactions, gen_return_txouts 12 13class PrioritiseTransactionTest(BitcoinTestFramework): 14 def set_test_params(self): 15 self.setup_clean_chain = True 16 self.num_nodes = 2 17 self.extra_args = [[ 18 "-printpriority=1", 19 "-acceptnonstdtxn=1", 20 ]] * self.num_nodes 21 self.supports_cli = False 22 23 def skip_test_if_missing_module(self): 24 self.skip_if_no_wallet() 25 26 def run_test(self): 27 # Test `prioritisetransaction` required parameters 28 assert_raises_rpc_error(-1, "prioritisetransaction", self.nodes[0].prioritisetransaction) 29 assert_raises_rpc_error(-1, "prioritisetransaction", self.nodes[0].prioritisetransaction, '') 30 assert_raises_rpc_error(-1, "prioritisetransaction", self.nodes[0].prioritisetransaction, '', 0) 31 32 # Test `prioritisetransaction` invalid extra parameters 33 assert_raises_rpc_error(-1, "prioritisetransaction", self.nodes[0].prioritisetransaction, '', 0, 0, 0) 34 35 # Test `prioritisetransaction` invalid `txid` 36 assert_raises_rpc_error(-8, "txid must be of length 64 (not 3, for 'foo')", self.nodes[0].prioritisetransaction, txid='foo', fee_delta=0) 37 assert_raises_rpc_error(-8, "txid must be hexadecimal string (not 'Zd1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000')", self.nodes[0].prioritisetransaction, txid='Zd1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000', fee_delta=0) 38 39 # Test `prioritisetransaction` invalid `dummy` 40 txid = '1d1d4e24ed99057e84c3f80fd8fbec79ed9e1acee37da269356ecea000000000' 41 assert_raises_rpc_error(-1, "JSON value is not a number as expected", self.nodes[0].prioritisetransaction, txid, 'foo', 0) 42 assert_raises_rpc_error(-8, "Priority is no longer supported, dummy argument to prioritisetransaction must be 0.", self.nodes[0].prioritisetransaction, txid, 1, 0) 43 44 # Test `prioritisetransaction` invalid `fee_delta` 45 assert_raises_rpc_error(-1, "JSON value is not an integer as expected", self.nodes[0].prioritisetransaction, txid=txid, fee_delta='foo') 46 47 self.txouts = gen_return_txouts() 48 self.relayfee = self.nodes[0].getnetworkinfo()['relayfee'] 49 50 utxo_count = 90 51 utxos = create_confirmed_utxos(self.relayfee, self.nodes[0], utxo_count) 52 base_fee = self.relayfee*100 # our transactions are smaller than 100kb 53 txids = [] 54 55 # Create 3 batches of transactions at 3 different fee rate levels 56 range_size = utxo_count // 3 57 for i in range(3): 58 txids.append([]) 59 start_range = i * range_size 60 end_range = start_range + range_size 61 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) 62 63 # Make sure that the size of each group of transactions exceeds 64 # MAX_BLOCK_BASE_SIZE -- otherwise the test needs to be revised to create 65 # more transactions. 66 mempool = self.nodes[0].getrawmempool(True) 67 sizes = [0, 0, 0] 68 for i in range(3): 69 for j in txids[i]: 70 assert j in mempool 71 sizes[i] += mempool[j]['vsize'] 72 assert sizes[i] > MAX_BLOCK_BASE_SIZE # Fail => raise utxo_count 73 74 # add a fee delta to something in the cheapest bucket and make sure it gets mined 75 # also check that a different entry in the cheapest bucket is NOT mined 76 self.nodes[0].prioritisetransaction(txid=txids[0][0], fee_delta=int(3*base_fee*COIN)) 77 78 self.nodes[0].generate(1) 79 80 mempool = self.nodes[0].getrawmempool() 81 self.log.info("Assert that prioritised transaction was mined") 82 assert txids[0][0] not in mempool 83 assert txids[0][1] in mempool 84 85 high_fee_tx = None 86 for x in txids[2]: 87 if x not in mempool: 88 high_fee_tx = x 89 90 # Something high-fee should have been mined! 91 assert high_fee_tx is not None 92 93 # Add a prioritisation before a tx is in the mempool (de-prioritising a 94 # high-fee transaction so that it's now low fee). 95 self.nodes[0].prioritisetransaction(txid=high_fee_tx, fee_delta=-int(2*base_fee*COIN)) 96 97 # Add everything back to mempool 98 self.nodes[0].invalidateblock(self.nodes[0].getbestblockhash()) 99 100 # Check to make sure our high fee rate tx is back in the mempool 101 mempool = self.nodes[0].getrawmempool() 102 assert high_fee_tx in mempool 103 104 # Now verify the modified-high feerate transaction isn't mined before 105 # the other high fee transactions. Keep mining until our mempool has 106 # decreased by all the high fee size that we calculated above. 107 while (self.nodes[0].getmempoolinfo()['bytes'] > sizes[0] + sizes[1]): 108 self.nodes[0].generate(1) 109 110 # High fee transaction should not have been mined, but other high fee rate 111 # transactions should have been. 112 mempool = self.nodes[0].getrawmempool() 113 self.log.info("Assert that de-prioritised transaction is still in mempool") 114 assert high_fee_tx in mempool 115 for x in txids[2]: 116 if (x != high_fee_tx): 117 assert x not in mempool 118 119 # Create a free transaction. Should be rejected. 120 utxo_list = self.nodes[0].listunspent() 121 assert len(utxo_list) > 0 122 utxo = utxo_list[0] 123 124 inputs = [] 125 outputs = {} 126 inputs.append({"txid" : utxo["txid"], "vout" : utxo["vout"]}) 127 outputs[self.nodes[0].getnewaddress()] = utxo["amount"] 128 raw_tx = self.nodes[0].createrawtransaction(inputs, outputs) 129 tx_hex = self.nodes[0].signrawtransactionwithwallet(raw_tx)["hex"] 130 tx_id = self.nodes[0].decoderawtransaction(tx_hex)["txid"] 131 132 # This will raise an exception due to min relay fee not being met 133 assert_raises_rpc_error(-26, "min relay fee not met", self.nodes[0].sendrawtransaction, tx_hex) 134 assert tx_id not in self.nodes[0].getrawmempool() 135 136 # This is a less than 1000-byte transaction, so just set the fee 137 # to be the minimum for a 1000-byte transaction and check that it is 138 # accepted. 139 self.nodes[0].prioritisetransaction(txid=tx_id, fee_delta=int(self.relayfee*COIN)) 140 141 self.log.info("Assert that prioritised free transaction is accepted to mempool") 142 assert_equal(self.nodes[0].sendrawtransaction(tx_hex), tx_id) 143 assert tx_id in self.nodes[0].getrawmempool() 144 145 # Test that calling prioritisetransaction is sufficient to trigger 146 # getblocktemplate to (eventually) return a new block. 147 mock_time = int(time.time()) 148 self.nodes[0].setmocktime(mock_time) 149 template = self.nodes[0].getblocktemplate({'rules': ['segwit']}) 150 self.nodes[0].prioritisetransaction(txid=tx_id, fee_delta=-int(self.relayfee*COIN)) 151 self.nodes[0].setmocktime(mock_time+10) 152 new_template = self.nodes[0].getblocktemplate({'rules': ['segwit']}) 153 154 assert template != new_template 155 156if __name__ == '__main__': 157 PrioritiseTransactionTest().main() 158