|
| 1 | +#!/usr/bin/env python2 |
| 2 | +# Copyright (c) 2014-2015 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 | + |
| 6 | +# Test mempool limiting together/eviction with the wallet |
| 7 | + |
| 8 | +from test_framework.test_framework import BitcoinTestFramework |
| 9 | +from test_framework.util import * |
| 10 | + |
| 11 | +class MempoolLimitTest(BitcoinTestFramework): |
| 12 | + |
| 13 | + def satoshi_round(self, amount): |
| 14 | + return Decimal(amount).quantize(Decimal('0.00000001'), rounding=ROUND_DOWN) |
| 15 | + |
| 16 | + def __init__(self): |
| 17 | + # Some pre-processing to create a bunch of OP_RETURN txouts to insert into transactions we create |
| 18 | + # So we have big transactions (and therefore can't fit very many into each block) |
| 19 | + # create one script_pubkey |
| 20 | + script_pubkey = "6a4d0200" #OP_RETURN OP_PUSH2 512 bytes |
| 21 | + for i in xrange (512): |
| 22 | + script_pubkey = script_pubkey + "01" |
| 23 | + # concatenate 128 txouts of above script_pubkey which we'll insert before the txout for change |
| 24 | + self.txouts = "81" |
| 25 | + for k in xrange(128): |
| 26 | + # add txout value |
| 27 | + self.txouts = self.txouts + "0000000000000000" |
| 28 | + # add length of script_pubkey |
| 29 | + self.txouts = self.txouts + "fd0402" |
| 30 | + # add script_pubkey |
| 31 | + self.txouts = self.txouts + script_pubkey |
| 32 | + |
| 33 | + def create_confirmed_utxos(self, count): |
| 34 | + self.nodes[0].generate(int(0.5*90)+102) |
| 35 | + utxos = self.nodes[0].listunspent() |
| 36 | + iterations = count - len(utxos) |
| 37 | + addr1 = self.nodes[0].getnewaddress() |
| 38 | + addr2 = self.nodes[0].getnewaddress() |
| 39 | + if iterations <= 0: |
| 40 | + return utxos |
| 41 | + for i in xrange(iterations): |
| 42 | + t = utxos.pop() |
| 43 | + fee = self.relayfee |
| 44 | + inputs = [] |
| 45 | + inputs.append({ "txid" : t["txid"], "vout" : t["vout"]}) |
| 46 | + outputs = {} |
| 47 | + send_value = t['amount'] - fee |
| 48 | + outputs[addr1] = self.satoshi_round(send_value/2) |
| 49 | + outputs[addr2] = self.satoshi_round(send_value/2) |
| 50 | + raw_tx = self.nodes[0].createrawtransaction(inputs, outputs) |
| 51 | + signed_tx = self.nodes[0].signrawtransaction(raw_tx)["hex"] |
| 52 | + txid = self.nodes[0].sendrawtransaction(signed_tx) |
| 53 | + |
| 54 | + while (self.nodes[0].getmempoolinfo()['size'] > 0): |
| 55 | + self.nodes[0].generate(1) |
| 56 | + |
| 57 | + utxos = self.nodes[0].listunspent() |
| 58 | + assert(len(utxos) >= count) |
| 59 | + return utxos |
| 60 | + |
| 61 | + def create_lots_of_big_transactions(self, utxos, fee): |
| 62 | + addr = self.nodes[0].getnewaddress() |
| 63 | + txids = [] |
| 64 | + for i in xrange(len(utxos)): |
| 65 | + t = utxos.pop() |
| 66 | + inputs = [] |
| 67 | + inputs.append({ "txid" : t["txid"], "vout" : t["vout"]}) |
| 68 | + outputs = {} |
| 69 | + send_value = t['amount'] - fee |
| 70 | + outputs[addr] = self.satoshi_round(send_value) |
| 71 | + rawtx = self.nodes[0].createrawtransaction(inputs, outputs) |
| 72 | + newtx = rawtx[0:92] |
| 73 | + newtx = newtx + self.txouts |
| 74 | + newtx = newtx + rawtx[94:] |
| 75 | + signresult = self.nodes[0].signrawtransaction(newtx, None, None, "NONE") |
| 76 | + txid = self.nodes[0].sendrawtransaction(signresult["hex"], True) |
| 77 | + txids.append(txid) |
| 78 | + return txids |
| 79 | + |
| 80 | + def setup_network(self): |
| 81 | + self.nodes = [] |
| 82 | + self.nodes.append(start_node(0, self.options.tmpdir, ["-maxmempool=5", "-spendzeroconfchange=0", "-debug"])) |
| 83 | + self.nodes.append(start_node(1, self.options.tmpdir, [])) |
| 84 | + connect_nodes(self.nodes[0], 1) |
| 85 | + self.is_network_split = False |
| 86 | + self.sync_all() |
| 87 | + self.relayfee = self.nodes[0].getnetworkinfo()['relayfee'] |
| 88 | + |
| 89 | + def setup_chain(self): |
| 90 | + print("Initializing test directory "+self.options.tmpdir) |
| 91 | + initialize_chain_clean(self.options.tmpdir, 2) |
| 92 | + |
| 93 | + def run_test(self): |
| 94 | + txids = [] |
| 95 | + utxos = self.create_confirmed_utxos(90) |
| 96 | + |
| 97 | + #create a mempool tx that will be evicted |
| 98 | + us0 = utxos.pop() |
| 99 | + inputs = [{ "txid" : us0["txid"], "vout" : us0["vout"]}] |
| 100 | + outputs = {self.nodes[1].getnewaddress() : 0.0001} |
| 101 | + tx = self.nodes[0].createrawtransaction(inputs, outputs) |
| 102 | + txF = self.nodes[0].fundrawtransaction(tx) |
| 103 | + txFS = self.nodes[0].signrawtransaction(txF['hex']) |
| 104 | + txid = self.nodes[0].sendrawtransaction(txFS['hex']) |
| 105 | + self.nodes[0].lockunspent(True, [us0]) |
| 106 | + |
| 107 | + relayfee = self.nodes[0].getnetworkinfo()['relayfee'] |
| 108 | + base_fee = relayfee*100 |
| 109 | + for i in xrange (4): |
| 110 | + txids.append([]) |
| 111 | + txids[i] = self.create_lots_of_big_transactions(utxos[30*i:30*i+30], (i+1)*base_fee) |
| 112 | + |
| 113 | + # by now, the tx should be evicted, check confirmation state |
| 114 | + assert(txid not in self.nodes[0].getrawmempool()) |
| 115 | + txdata = self.nodes[0].gettransaction(txid); |
| 116 | + assert(txdata['confirmations'] == 0) #confirmation should still be 0 |
| 117 | + |
| 118 | +if __name__ == '__main__': |
| 119 | + MempoolLimitTest().main() |
0 commit comments