Skip to content

Commit

Permalink
Add dumpmempool RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
greenaddress authored and jnewbery committed Sep 1, 2017
1 parent 609e818 commit 67d307f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/rpc/blockchain.cpp
Expand Up @@ -1532,9 +1532,28 @@ UniValue getchaintxstats(const JSONRPCRequest& request)
return ret;
}

UniValue dumpmempool(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() != 0)
throw std::runtime_error(
"dumpmempool\n"
"\nDumps the mempool to disk.\n"
"\nExamples:\n"
+ HelpExampleCli("dumpmempool", "")
+ HelpExampleRpc("dumpmempool", "")
);

if (!DumpMempool()) {
throw JSONRPCError(RPC_MISC_ERROR, "Unable to dump mempool to disk");
}

return true;
}

static const CRPCCommand commands[] =
{ // category name actor (function) okSafe argNames
// --------------------- ------------------------ ----------------------- ------ ----------
{ "blockchain", "dumpmempool", &dumpmempool, true, {} },
{ "blockchain", "getblockchaininfo", &getblockchaininfo, true, {} },
{ "blockchain", "getchaintxstats", &getchaintxstats, true, {"nblocks", "blockhash"} },
{ "blockchain", "getbestblockhash", &getbestblockhash, true, {} },
Expand Down
28 changes: 28 additions & 0 deletions test/functional/mempool_persist.py
Expand Up @@ -28,8 +28,14 @@
- 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.
- Remove node0 mempool.dat and verify dumpmempool RPC recreates it
and verify that node1 can load it and has 5 transaction in its
mempool.
- Verify that dumpmempool throws when the RPC is called if
node1 can't write to disk.
"""
import os
import time

from test_framework.test_framework import BitcoinTestFramework
Expand Down Expand Up @@ -78,5 +84,27 @@ def run_test(self):
self.start_node(0)
wait_until(lambda: len(self.nodes[0].getrawmempool()) == 5)

mempooldat0 = os.path.join(self.options.tmpdir, 'node0', 'regtest', 'mempool.dat')
mempooldat1 = os.path.join(self.options.tmpdir, 'node1', 'regtest', 'mempool.dat')
self.log.debug("Remove the mempool.dat file. Verify that dumpmempool to disk via RPC re-creates it")
os.remove(mempooldat0)
assert self.nodes[0].dumpmempool()
assert os.path.isfile(mempooldat0)

self.log.debug("Stop nodes, make node1 use mempool.dat from node0. Verify it has 5 transactions")
os.rename(mempooldat0, mempooldat1)
self.stop_nodes()
self.start_node(1, extra_args=[])
wait_until(lambda: len(self.nodes[1].getrawmempool()) == 5)

self.log.debug("Prevent bitcoind from writing mempool.dat to disk. Verify that `dumpmempool` fails")
# to test the exception we are setting bad permissions on a tmp file called mempool.dat.new
# which is an implementation detail that could change and break this test
mempooldotnew1 = mempooldat1 + '.new'
with os.fdopen(os.open(mempooldotnew1, os.O_CREAT, 0o000), 'w'):
pass
assert_raises_jsonrpc(-1, "Unable to dump mempool to disk", self.nodes[1].dumpmempool)
os.remove(mempooldotnew1)

if __name__ == '__main__':
MempoolPersistTest().main()

0 comments on commit 67d307f

Please sign in to comment.