Skip to content

Commit 2a183de

Browse files
committed
Merge #9966: Control mempool persistence using a command line parameter
a750d77 Add tests for mempool persistence (John Newbery) 91c91e1 Control mempool persistence using a command line parameter. (John Newbery) Tree-SHA512: 157d01cefd1903b8bfc5cbab42a3cc5e9c1094179bf4b64b3d34c0d4d9b976d593755bfea5c41c631cb758e1de17c6c2058c130d487d20560b7c0bafcddfa520
2 parents d3dce0e + a750d77 commit 2a183de

File tree

4 files changed

+101
-4
lines changed

4 files changed

+101
-4
lines changed

src/init.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,9 @@ void Shutdown()
208208

209209
StopTorControl();
210210
UnregisterNodeSignals(GetNodeSignals());
211-
if (fDumpMempoolLater)
211+
if (fDumpMempoolLater && GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
212212
DumpMempool();
213+
}
213214

214215
if (fFeeEstimatesInitialized)
215216
{
@@ -354,6 +355,7 @@ std::string HelpMessage(HelpMessageMode mode)
354355
strUsage += HelpMessageOpt("-maxorphantx=<n>", strprintf(_("Keep at most <n> unconnectable transactions in memory (default: %u)"), DEFAULT_MAX_ORPHAN_TRANSACTIONS));
355356
strUsage += HelpMessageOpt("-maxmempool=<n>", strprintf(_("Keep the transaction memory pool below <n> megabytes (default: %u)"), DEFAULT_MAX_MEMPOOL_SIZE));
356357
strUsage += HelpMessageOpt("-mempoolexpiry=<n>", strprintf(_("Do not keep transactions in the mempool longer than <n> hours (default: %u)"), DEFAULT_MEMPOOL_EXPIRY));
358+
strUsage += HelpMessageOpt("-persistmempool", strprintf(_("Whether to save the mempool on shutdown and load on restart (default: %u)"), DEFAULT_PERSIST_MEMPOOL));
357359
strUsage += HelpMessageOpt("-blockreconstructionextratxn=<n>", strprintf(_("Extra transactions to keep in memory for compact block reconstructions (default: %u)"), DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN));
358360
strUsage += HelpMessageOpt("-par=<n>", strprintf(_("Set the number of script verification threads (%u to %d, 0 = auto, <0 = leave that many cores free, default: %d)"),
359361
-GetNumCores(), MAX_SCRIPTCHECK_THREADS, DEFAULT_SCRIPTCHECK_THREADS));
@@ -679,8 +681,10 @@ void ThreadImport(std::vector<fs::path> vImportFiles)
679681
StartShutdown();
680682
}
681683
} // End scope of CImportingNow
682-
LoadMempool();
683-
fDumpMempoolLater = !fRequestShutdown;
684+
if (GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
685+
LoadMempool();
686+
fDumpMempoolLater = !fRequestShutdown;
687+
}
684688
}
685689

686690
/** Sanity checks

src/validation.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ static const bool DEFAULT_PERMIT_BAREMULTISIG = true;
131131
static const bool DEFAULT_CHECKPOINTS_ENABLED = true;
132132
static const bool DEFAULT_TXINDEX = false;
133133
static const unsigned int DEFAULT_BANSCORE_THRESHOLD = 100;
134-
134+
/** Default for -persistmempool */
135+
static const bool DEFAULT_PERSIST_MEMPOOL = true;
135136
/** Default for -mempoolreplacement */
136137
static const bool DEFAULT_ENABLE_REPLACEMENT = true;
137138
/** Default for using fee filter */

test/functional/mempool_persist.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
'rest.py',
8686
'mempool_spendcoinbase.py',
8787
'mempool_reorg.py',
88+
'mempool_persist.py',
8889
'httpbasics.py',
8990
'multi_rpc.py',
9091
'proxy_test.py',

0 commit comments

Comments
 (0)