Remove accidental stray semicolon
[bitcoinplatinum.git] / test / functional / mempool_persist.py
blob149a01870d7752e0e73de83c4db4631b861be922
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.
7 By default, bitcoind will dump mempool on shutdown and
8 then reload it on startup. This can be overridden with
9 the -persistmempool=0 command line option.
11 Test is as follows:
13 - start node0, node1 and node2. node1 has -persistmempool=0
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=0, the mempool is not
24 dumped to disk when the node is shut down.
25 - Restart node0 with -persistmempool=0. Verify that its mempool is
26 empty. Shutdown node0. This tests that with -persistmempool=0,
27 the mempool is not loaded from disk on start up.
28 - Restart node0 with -persistmempool. Verify that it has 5
29 transactions in its mempool. This tests that -persistmempool=0
30 does not overwrite a previously valid mempool stored on disk.
31 - Remove node0 mempool.dat and verify savemempool RPC recreates it
32 and verify that node1 can load it and has 5 transaction in its
33 mempool.
34 - Verify that savemempool throws when the RPC is called if
35 node1 can't write to disk.
37 """
38 import os
39 import time
41 from test_framework.test_framework import BitcoinTestFramework
42 from test_framework.util import *
44 class MempoolPersistTest(BitcoinTestFramework):
45 def set_test_params(self):
46 self.num_nodes = 3
47 self.extra_args = [[], ["-persistmempool=0"], []]
49 def run_test(self):
50 chain_height = self.nodes[0].getblockcount()
51 assert_equal(chain_height, 200)
53 self.log.debug("Mine a single block to get out of IBD")
54 self.nodes[0].generate(1)
55 self.sync_all()
57 self.log.debug("Send 5 transactions from node2 (to its own address)")
58 for i in range(5):
59 self.nodes[2].sendtoaddress(self.nodes[2].getnewaddress(), Decimal("10"))
60 self.sync_all()
62 self.log.debug("Verify that node0 and node1 have 5 transactions in their mempools")
63 assert_equal(len(self.nodes[0].getrawmempool()), 5)
64 assert_equal(len(self.nodes[1].getrawmempool()), 5)
66 self.log.debug("Stop-start node0 and node1. Verify that node0 has the transactions in its mempool and node1 does not.")
67 self.stop_nodes()
68 self.start_node(0)
69 self.start_node(1)
70 # Give bitcoind a second to reload the mempool
71 time.sleep(1)
72 wait_until(lambda: len(self.nodes[0].getrawmempool()) == 5)
73 assert_equal(len(self.nodes[1].getrawmempool()), 0)
75 self.log.debug("Stop-start node0 with -persistmempool=0. Verify that it doesn't load its mempool.dat file.")
76 self.stop_nodes()
77 self.start_node(0, extra_args=["-persistmempool=0"])
78 # Give bitcoind a second to reload the mempool
79 time.sleep(1)
80 assert_equal(len(self.nodes[0].getrawmempool()), 0)
82 self.log.debug("Stop-start node0. Verify that it has the transactions in its mempool.")
83 self.stop_nodes()
84 self.start_node(0)
85 wait_until(lambda: len(self.nodes[0].getrawmempool()) == 5)
87 mempooldat0 = os.path.join(self.options.tmpdir, 'node0', 'regtest', 'mempool.dat')
88 mempooldat1 = os.path.join(self.options.tmpdir, 'node1', 'regtest', 'mempool.dat')
89 self.log.debug("Remove the mempool.dat file. Verify that savemempool to disk via RPC re-creates it")
90 os.remove(mempooldat0)
91 self.nodes[0].savemempool()
92 assert os.path.isfile(mempooldat0)
94 self.log.debug("Stop nodes, make node1 use mempool.dat from node0. Verify it has 5 transactions")
95 os.rename(mempooldat0, mempooldat1)
96 self.stop_nodes()
97 self.start_node(1, extra_args=[])
98 wait_until(lambda: len(self.nodes[1].getrawmempool()) == 5)
100 self.log.debug("Prevent bitcoind from writing mempool.dat to disk. Verify that `savemempool` fails")
101 # to test the exception we are setting bad permissions on a tmp file called mempool.dat.new
102 # which is an implementation detail that could change and break this test
103 mempooldotnew1 = mempooldat1 + '.new'
104 with os.fdopen(os.open(mempooldotnew1, os.O_CREAT, 0o000), 'w'):
105 pass
106 assert_raises_jsonrpc(-1, "Unable to dump mempool to disk", self.nodes[1].savemempool)
107 os.remove(mempooldotnew1)
109 if __name__ == '__main__':
110 MempoolPersistTest().main()