|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2014-2017 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 persistence. |
| 6 | +
|
| 7 | +By default, bitcoind will dump mempool on shutdown and |
| 8 | +then reload it on startup. This can be overridden with |
| 9 | +the -persistmempool=false command line option. |
| 10 | +
|
| 11 | +Test is as follows: |
| 12 | +
|
| 13 | + - start node0, node1 and node2. node1 has -persistmempool=false |
| 14 | + - create 5 transactions on node2 to its own address. Note that these |
| 15 | + are not sent to node0 or node1 addresses because we don't want |
| 16 | + them to be saved in the wallet. |
| 17 | + - check that node0 and node1 have 5 transactions in their mempools |
| 18 | + - shutdown all nodes. |
| 19 | + - startup node0. Verify that it still has 5 transactions |
| 20 | + in its mempool. Shutdown node0. This tests that by default the |
| 21 | + mempool is persistent. |
| 22 | + - startup node1. Verify that its mempool is empty. Shutdown node1. |
| 23 | + This tests that with -persistmempool=false, the mempool is not |
| 24 | + dumped to disk when the node is shut down. |
| 25 | + - Restart node0 with -persistmempool=false. Verify that its mempool is |
| 26 | + empty. Shutdown node0. This tests that with -persistmempool=false, |
| 27 | + the mempool is not loaded from disk on start up. |
| 28 | + - Restart node0 with -persistmempool=true. Verify that it has 5 |
| 29 | + transactions in its mempool. This tests that -persistmempool=false |
| 30 | + does not overwrite a previously valid mempool stored on disk. |
| 31 | +
|
| 32 | +""" |
| 33 | + |
| 34 | +from test_framework.test_framework import BitcoinTestFramework |
| 35 | +from test_framework.util import * |
| 36 | + |
| 37 | +class MempoolPersistTest(BitcoinTestFramework): |
| 38 | + |
| 39 | + def __init__(self): |
| 40 | + super().__init__() |
| 41 | + self.num_nodes = 3 |
| 42 | + self.setup_clean_chain = False |
| 43 | + |
| 44 | + def setup_network(self): |
| 45 | + # We need 3 nodes for this test. Node1 does not have a persistent mempool. |
| 46 | + self.nodes = [] |
| 47 | + self.nodes.append(start_node(0, self.options.tmpdir)) |
| 48 | + self.nodes.append(start_node(1, self.options.tmpdir, ["-persistmempool=false"])) |
| 49 | + self.nodes.append(start_node(2, self.options.tmpdir)) |
| 50 | + connect_nodes_bi(self.nodes, 0, 2) |
| 51 | + connect_nodes_bi(self.nodes, 1, 2) |
| 52 | + self.is_network_split = False |
| 53 | + |
| 54 | + def run_test(self): |
| 55 | + chain_height = self.nodes[0].getblockcount() |
| 56 | + assert_equal(chain_height, 200) |
| 57 | + |
| 58 | + self.log.debug("Mine a single block to get out of IBD") |
| 59 | + self.nodes[0].generate(1) |
| 60 | + |
| 61 | + self.log.debug("Send 5 transactions from node2 (to its own address)") |
| 62 | + for i in range(5): |
| 63 | + self.nodes[2].sendtoaddress(self.nodes[2].getnewaddress(), Decimal("10")) |
| 64 | + self.sync_all() |
| 65 | + |
| 66 | + self.log.debug("Verify that node0 and node1 have 5 transactions in their mempools") |
| 67 | + assert_equal(len(self.nodes[0].getrawmempool()), 5) |
| 68 | + assert_equal(len(self.nodes[1].getrawmempool()), 5) |
| 69 | + |
| 70 | + self.log.debug("Stop-start node0 and node1. Verify that node0 has the transactions in its mempool and node1 does not.") |
| 71 | + stop_nodes(self.nodes) |
| 72 | + self.nodes = [] |
| 73 | + self.nodes.append(start_node(0, self.options.tmpdir)) |
| 74 | + self.nodes.append(start_node(1, self.options.tmpdir)) |
| 75 | + assert_equal(len(self.nodes[0].getrawmempool()), 5) |
| 76 | + assert_equal(len(self.nodes[1].getrawmempool()), 0) |
| 77 | + |
| 78 | + self.log.debug("Stop-start node0 with -persistmempool=false. Verify that it doesn't load its mempool.dat file.") |
| 79 | + stop_nodes(self.nodes) |
| 80 | + self.nodes = [] |
| 81 | + self.nodes.append(start_node(0, self.options.tmpdir, ["-persistmempool=false"])) |
| 82 | + assert_equal(len(self.nodes[0].getrawmempool()), 0) |
| 83 | + |
| 84 | + self.log.debug("Stop-start node0. Verify that it has the transactions in its mempool.") |
| 85 | + stop_nodes(self.nodes) |
| 86 | + self.nodes = [] |
| 87 | + self.nodes.append(start_node(0, self.options.tmpdir)) |
| 88 | + assert_equal(len(self.nodes[0].getrawmempool()), 5) |
| 89 | + |
| 90 | +if __name__ == '__main__': |
| 91 | + MempoolPersistTest().main() |
0 commit comments