1#!/usr/bin/env python3
2# Copyright (c) 2014-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 mempool limiting together/eviction with the wallet."""
6
7from decimal import Decimal
8
9from test_framework.test_framework import BitcoinTestFramework
10from test_framework.util import assert_equal, assert_greater_than, assert_raises_rpc_error, create_confirmed_utxos, create_lots_of_big_transactions, gen_return_txouts
11
12class MempoolLimitTest(BitcoinTestFramework):
13    def set_test_params(self):
14        self.setup_clean_chain = True
15        self.num_nodes = 1
16        self.extra_args = [[
17            "-acceptnonstdtxn=1",
18            "-maxmempool=5",
19            "-spendzeroconfchange=0",
20        ]]
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        txouts = gen_return_txouts()
28        relayfee = self.nodes[0].getnetworkinfo()['relayfee']
29
30        self.log.info('Check that mempoolminfee is minrelytxfee')
31        assert_equal(self.nodes[0].getmempoolinfo()['minrelaytxfee'], Decimal('0.00400000'))
32        assert_equal(self.nodes[0].getmempoolinfo()['mempoolminfee'], Decimal('0.00400000'))
33
34        txids = []
35        utxos = create_confirmed_utxos(relayfee, self.nodes[0], 91)
36
37        self.log.info('Create a mempool tx that will be evicted')
38        us0 = utxos.pop()
39        inputs = [{ "txid" : us0["txid"], "vout" : us0["vout"]}]
40        outputs = {self.nodes[0].getnewaddress() : 0.01}
41        tx = self.nodes[0].createrawtransaction(inputs, outputs)
42        self.nodes[0].settxfee(relayfee) # specifically fund this tx with low fee
43        txF = self.nodes[0].fundrawtransaction(tx)
44        self.nodes[0].settxfee(0) # return to automatic fee selection
45        txFS = self.nodes[0].signrawtransactionwithwallet(txF['hex'])
46        txid = self.nodes[0].sendrawtransaction(txFS['hex'])
47
48        relayfee = self.nodes[0].getnetworkinfo()['relayfee']
49        base_fee = relayfee*100
50        for i in range (3):
51            txids.append([])
52            txids[i] = create_lots_of_big_transactions(self.nodes[0], txouts, utxos[30*i:30*i+30], 30, (i+1)*base_fee)
53
54        self.log.info('The tx should be evicted by now')
55        assert txid not in self.nodes[0].getrawmempool()
56        txdata = self.nodes[0].gettransaction(txid)
57        assert txdata['confirmations'] ==  0  #confirmation should still be 0
58
59        self.log.info('Check that mempoolminfee is larger than minrelytxfee')
60        assert_equal(self.nodes[0].getmempoolinfo()['minrelaytxfee'], Decimal('0.00400000'))
61        assert_greater_than(self.nodes[0].getmempoolinfo()['mempoolminfee'], Decimal('0.00400000'))
62
63        self.log.info('Create a mempool tx that will not pass mempoolminfee')
64        us0 = utxos.pop()
65        inputs = [{ "txid" : us0["txid"], "vout" : us0["vout"]}]
66        outputs = {self.nodes[0].getnewaddress() : 0.01}
67        tx = self.nodes[0].createrawtransaction(inputs, outputs)
68        # specifically fund this tx with a fee < mempoolminfee, >= than minrelaytxfee
69        txF = self.nodes[0].fundrawtransaction(tx, {'feeRate': relayfee})
70        txFS = self.nodes[0].signrawtransactionwithwallet(txF['hex'])
71        assert_raises_rpc_error(-26, "mempool min fee not met", self.nodes[0].sendrawtransaction, txFS['hex'])
72
73if __name__ == '__main__':
74    MempoolLimitTest().main()
75