Skip to content

Commit

Permalink
Control mempool persistence using a command line parameter.
Browse files Browse the repository at this point in the history
Summary:
Mempool persistence was added in
3f78562, and is always on. This commit
introduces a command-line parameter -persistmempool, which defaults to
true. When set to false:
- mempool.dat is not loaded when the node starts.
- mempool.dat is not written when the node stops.

Backport core's PR9966 and PR10342

Test Plan:
  make check
  ./test/functional/test_runner.py mempool_persist.py

Reviewers: #bitcoin_abc, dagurval

Reviewed By: #bitcoin_abc, dagurval

Differential Revision: https://reviews.bitcoinabc.org/D964
  • Loading branch information
jnewbery authored and deadalnix committed Jan 16, 2018
1 parent 9ee5639 commit f9d817b
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,10 @@ void Shutdown() {

StopTorControl();
UnregisterNodeSignals(GetNodeSignals());
if (fDumpMempoolLater) DumpMempool();
if (fDumpMempoolLater &&
GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
DumpMempool();
}

if (fFeeEstimatesInitialized) {
fs::path est_path = GetDataDir() / FEE_ESTIMATES_FILENAME;
Expand Down Expand Up @@ -372,6 +375,11 @@ std::string HelpMessage(HelpMessageMode mode) {
strprintf(_("Do not keep transactions in the mempool "
"longer than <n> hours (default: %u)"),
DEFAULT_MEMPOOL_EXPIRY));
strUsage +=
HelpMessageOpt("-persistmempool",
strprintf(_("Whether to save the mempool on shutdown "
"and load on restart (default: %u)"),
DEFAULT_PERSIST_MEMPOOL));
strUsage += HelpMessageOpt(
"-blockreconstructionextratxn=<n>",
strprintf(_("Extra transactions to keep in memory for compact block "
Expand Down Expand Up @@ -1043,8 +1051,10 @@ void ThreadImport(const Config &config, std::vector<fs::path> vImportFiles) {
StartShutdown();
}
} // End scope of CImportingNow
LoadMempool(config);
fDumpMempoolLater = !fRequestShutdown;
if (GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
LoadMempool(config);
fDumpMempoolLater = !fRequestShutdown;
}
}

/** Sanity checks
Expand Down
2 changes: 2 additions & 0 deletions src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ static const bool DEFAULT_CHECKPOINTS_ENABLED = true;
static const bool DEFAULT_TXINDEX = false;
static const unsigned int DEFAULT_BANSCORE_THRESHOLD = 100;

/** Default for -persistmempool */
static const bool DEFAULT_PERSIST_MEMPOOL = true;
/** Default for using fee filter */
static const bool DEFAULT_FEEFILTER = true;

Expand Down
98 changes: 98 additions & 0 deletions test/functional/mempool_persist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/env python3
# Copyright (c) 2014-2017 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test mempool persistence.
By default, bitcoind will dump mempool on shutdown and
then reload it on startup. This can be overridden with
the -persistmempool=0 command line option.
Test is as follows:
- start node0, node1 and node2. node1 has -persistmempool=0
- create 5 transactions on node2 to its own address. Note that these
are not sent to node0 or node1 addresses because we don't want
them to be saved in the wallet.
- check that node0 and node1 have 5 transactions in their mempools
- shutdown all nodes.
- startup node0. Verify that it still has 5 transactions
in its mempool. Shutdown node0. This tests that by default the
mempool is persistent.
- startup node1. Verify that its mempool is empty. Shutdown node1.
This tests that with -persistmempool=0, the mempool is not
dumped to disk when the node is shut down.
- Restart node0 with -persistmempool=0. Verify that its mempool is
empty. Shutdown node0. This tests that with -persistmempool=0,
the mempool is not loaded from disk on start up.
- Restart node0 with -persistmempool. Verify that it has 5
transactions in its mempool. This tests that -persistmempool=0
does not overwrite a previously valid mempool stored on disk.
"""
import time

from test_framework.mininode import wait_until
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import *


class MempoolPersistTest(BitcoinTestFramework):

def __init__(self):
super().__init__()
# We need 3 nodes for this test. Node1 does not have a persistent mempool.
self.num_nodes = 3
self.setup_clean_chain = False
self.extra_args = [[], ["-persistmempool=0"], []]

def run_test(self):
chain_height = self.nodes[0].getblockcount()
assert_equal(chain_height, 200)

self.log.debug("Mine a single block to get out of IBD")
self.nodes[0].generate(1)
self.sync_all()

self.log.debug("Send 5 transactions from node2 (to its own address)")
for i in range(5):
self.nodes[2].sendtoaddress(
self.nodes[2].getnewaddress(), Decimal("10"))
self.sync_all()

self.log.debug(
"Verify that node0 and node1 have 5 transactions in their mempools")
assert_equal(len(self.nodes[0].getrawmempool()), 5)
assert_equal(len(self.nodes[1].getrawmempool()), 5)

self.log.debug(
"Stop-start node0 and node1. Verify that node0 has the transactions in its mempool and node1 does not.")
stop_nodes(self.nodes)
self.nodes = []
self.nodes.append(start_node(0, self.options.tmpdir))
self.nodes.append(start_node(1, self.options.tmpdir))
# Give bitcoind a second to reload the mempool
time.sleep(1)
assert wait_until(lambda: len(self.nodes[0].getrawmempool()) == 5)
assert_equal(len(self.nodes[1].getrawmempool()), 0)

self.log.debug(
"Stop-start node0 with -persistmempool=0. Verify that it doesn't load its mempool.dat file.")
stop_nodes(self.nodes)
self.nodes = []
self.nodes.append(start_node(
0, self.options.tmpdir, ["-persistmempool=0"]))
# Give bitcoind a second to reload the mempool
time.sleep(1)
assert_equal(len(self.nodes[0].getrawmempool()), 0)

self.log.debug(
"Stop-start node0. Verify that it has the transactions in its mempool.")
stop_nodes(self.nodes)
self.nodes = []
self.nodes.append(start_node(0, self.options.tmpdir))
assert wait_until(lambda: len(self.nodes[0].getrawmempool()) == 5)


if __name__ == '__main__':
MempoolPersistTest().main()
1 change: 1 addition & 0 deletions test/functional/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
'rest.py',
'mempool_spendcoinbase.py',
'mempool_reorg.py',
'mempool_persist.py',
'httpbasics.py',
'multi_rpc.py',
'proxy_test.py',
Expand Down

0 comments on commit f9d817b

Please sign in to comment.