]>
Commit | Line | Data |
---|---|---|
4ca5def6 | 1 | #!/usr/bin/env python |
9e565329 GA |
2 | # Copyright (c) 2014 The Bitcoin Core developers |
3 | # Distributed under the MIT software license, see the accompanying | |
bc909a7a | 4 | # file COPYING or https://www.opensource.org/licenses/mit-license.php . |
9e565329 GA |
5 | |
6 | # | |
7 | # Test spending coinbase transactions. | |
8 | # The coinbase transaction in block N can appear in block | |
9 | # N+100... so is valid in the mempool when the best block | |
10 | # height is N+99. | |
11 | # This test makes sure coinbase spends that will be mature | |
12 | # in the next block are accepted into the memory pool, | |
13 | # but less mature coinbase spends are NOT. | |
14 | # | |
15 | ||
4ca5def6 | 16 | import sys; assert sys.version_info < (3,), ur"This script does not run under Python 3. Please use Python 2.7.x." |
17 | ||
64937fe5 | 18 | from test_framework.test_framework import BitcoinTestFramework |
aff0bf7f DH |
19 | from test_framework.authproxy import JSONRPCException |
20 | from test_framework.util import assert_equal, assert_greater_than, assert_raises, \ | |
21 | start_node | |
22 | ||
9e565329 GA |
23 | |
24 | # Create one-input, one-output, no-fee transaction: | |
25 | class MempoolSpendCoinbaseTest(BitcoinTestFramework): | |
26 | ||
27 | def setup_network(self): | |
28 | # Just need one node for this test | |
29 | args = ["-checkmempool", "-debug=mempool"] | |
30 | self.nodes = [] | |
31 | self.nodes.append(start_node(0, self.options.tmpdir, args)) | |
32 | self.is_network_split = False | |
33 | ||
34 | def create_tx(self, from_txid, to_address, amount): | |
35 | inputs = [{ "txid" : from_txid, "vout" : 0}] | |
36 | outputs = { to_address : amount } | |
37 | rawtx = self.nodes[0].createrawtransaction(inputs, outputs) | |
38 | signresult = self.nodes[0].signrawtransaction(rawtx) | |
39 | assert_equal(signresult["complete"], True) | |
40 | return signresult["hex"] | |
41 | ||
42 | def run_test(self): | |
43 | chain_height = self.nodes[0].getblockcount() | |
44 | assert_equal(chain_height, 200) | |
45 | node0_address = self.nodes[0].getnewaddress() | |
46 | ||
47 | # Coinbase at height chain_height-100+1 ok in mempool, should | |
48 | # get mined. Coinbase at height chain_height-100+2 is | |
49 | # is too immature to spend. | |
50 | b = [ self.nodes[0].getblockhash(n) for n in range(101, 103) ] | |
51 | coinbase_txids = [ self.nodes[0].getblock(h)['tx'][0] for h in b ] | |
196cf25d | 52 | spends_raw = [ self.create_tx(txid, node0_address, 10) for txid in coinbase_txids ] |
9e565329 GA |
53 | |
54 | spend_101_id = self.nodes[0].sendrawtransaction(spends_raw[0]) | |
55 | ||
56 | # coinbase at height 102 should be too immature to spend | |
57 | assert_raises(JSONRPCException, self.nodes[0].sendrawtransaction, spends_raw[1]) | |
58 | ||
59 | # mempool should have just spend_101: | |
542a7a40 JG |
60 | mempoolinfo = self.nodes[0].getmempoolinfo() |
61 | assert_equal(mempoolinfo['size'], 1) | |
9e565329 GA |
62 | assert_equal(self.nodes[0].getrawmempool(), [ spend_101_id ]) |
63 | ||
542a7a40 JG |
64 | # the size of the memory pool should be greater than 1x ~100 bytes |
65 | assert_greater_than(mempoolinfo['bytes'], 100) | |
66 | # the actual memory usage should be strictly greater than the size | |
67 | # of the memory pool | |
68 | assert_greater_than(mempoolinfo['usage'], mempoolinfo['bytes']) | |
69 | ||
9e565329 | 70 | # mine a block, spend_101 should get confirmed |
6b04508e | 71 | self.nodes[0].generate(1) |
542a7a40 JG |
72 | mempoolinfo = self.nodes[0].getmempoolinfo() |
73 | assert_equal(mempoolinfo['size'], 0) | |
74 | assert_equal(mempoolinfo['bytes'], 0) | |
75 | assert_equal(mempoolinfo['usage'], 0) | |
9e565329 GA |
76 | assert_equal(set(self.nodes[0].getrawmempool()), set()) |
77 | ||
78 | # ... and now height 102 can be spent: | |
79 | spend_102_id = self.nodes[0].sendrawtransaction(spends_raw[1]) | |
542a7a40 JG |
80 | mempoolinfo = self.nodes[0].getmempoolinfo() |
81 | assert_equal(mempoolinfo['size'], 1) | |
9e565329 | 82 | assert_equal(self.nodes[0].getrawmempool(), [ spend_102_id ]) |
542a7a40 JG |
83 | assert_greater_than(mempoolinfo['bytes'], 100) |
84 | assert_greater_than(mempoolinfo['usage'], mempoolinfo['bytes']) | |
9e565329 GA |
85 | |
86 | if __name__ == '__main__': | |
87 | MempoolSpendCoinbaseTest().main() |