Skip to content

Commit bb8ae2c

Browse files
committed
rpc: Expose g_is_mempool_loaded via getmempoolinfo and /rest/mempool/info.json
And use it to fix a race condition in mempool_persist.py: https://travis-ci.org/Empact/bitcoin/jobs/487577243 Since e.g. getrawmempool returns errors based on this status, this enables users to test it for readiness.
1 parent 3515612 commit bb8ae2c

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

doc/REST-interface.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ $ curl localhost:18332/rest/getutxos/checkmempool/b2cdfd7b89def827ff8af7cd9bff76
101101

102102
Returns various information about the TX mempool.
103103
Only supports JSON as output format.
104+
* loaded : (boolean) if the mempool is fully loaded
104105
* size : (numeric) the number of transactions in the TX mempool
105106
* bytes : (numeric) size of the TX mempool in bytes
106107
* usage : (numeric) total TX mempool memory usage

src/rpc/blockchain.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,6 +1484,7 @@ static UniValue getchaintips(const JSONRPCRequest& request)
14841484
UniValue MempoolInfoToJSON(const CTxMemPool& pool)
14851485
{
14861486
UniValue ret(UniValue::VOBJ);
1487+
ret.pushKV("loaded", g_is_mempool_loaded);
14871488
ret.pushKV("size", (int64_t)pool.size());
14881489
ret.pushKV("bytes", (int64_t)pool.GetTotalTxSize());
14891490
ret.pushKV("usage", (int64_t)pool.DynamicMemoryUsage());
@@ -1504,6 +1505,7 @@ static UniValue getmempoolinfo(const JSONRPCRequest& request)
15041505
{},
15051506
RPCResult{
15061507
"{\n"
1508+
" \"loaded\": true|false (boolean) True if the mempool is fully loaded\n"
15071509
" \"size\": xxxxx, (numeric) Current tx count\n"
15081510
" \"bytes\": xxxxx, (numeric) Sum of all virtual transaction sizes as defined in BIP 141. Differs from actual serialized size because witness data is discounted\n"
15091511
" \"usage\": xxxxx, (numeric) Total memory usage for the mempool\n"

test/functional/mempool_persist.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"""
3838
from decimal import Decimal
3939
import os
40-
import time
4140

4241
from test_framework.test_framework import BitcoinTestFramework
4342
from test_framework.util import assert_equal, assert_raises_rpc_error, wait_until
@@ -83,9 +82,10 @@ def run_test(self):
8382
self.start_node(1, extra_args=["-persistmempool=0"])
8483
self.start_node(0)
8584
self.start_node(2)
86-
# Give bitcoind a second to reload the mempool
87-
wait_until(lambda: len(self.nodes[0].getrawmempool()) == 5, timeout=1)
88-
wait_until(lambda: len(self.nodes[2].getrawmempool()) == 5, timeout=1)
85+
wait_until(lambda: self.nodes[0].getmempoolinfo()["loaded"], timeout=1)
86+
wait_until(lambda: self.nodes[2].getmempoolinfo()["loaded"], timeout=1)
87+
assert_equal(len(self.nodes[0].getrawmempool()), 5)
88+
assert_equal(len(self.nodes[2].getrawmempool()), 5)
8989
# The others have loaded their mempool. If node_1 loaded anything, we'd probably notice by now:
9090
assert_equal(len(self.nodes[1].getrawmempool()), 0)
9191

@@ -100,14 +100,14 @@ def run_test(self):
100100
self.log.debug("Stop-start node0 with -persistmempool=0. Verify that it doesn't load its mempool.dat file.")
101101
self.stop_nodes()
102102
self.start_node(0, extra_args=["-persistmempool=0"])
103-
# Give bitcoind a second to reload the mempool
104-
time.sleep(1)
103+
wait_until(lambda: self.nodes[0].getmempoolinfo()["loaded"])
105104
assert_equal(len(self.nodes[0].getrawmempool()), 0)
106105

107106
self.log.debug("Stop-start node0. Verify that it has the transactions in its mempool.")
108107
self.stop_nodes()
109108
self.start_node(0)
110-
wait_until(lambda: len(self.nodes[0].getrawmempool()) == 5)
109+
wait_until(lambda: self.nodes[0].getmempoolinfo()["loaded"])
110+
assert_equal(len(self.nodes[0].getrawmempool()), 5)
111111

112112
mempooldat0 = os.path.join(self.nodes[0].datadir, 'regtest', 'mempool.dat')
113113
mempooldat1 = os.path.join(self.nodes[1].datadir, 'regtest', 'mempool.dat')
@@ -120,7 +120,8 @@ def run_test(self):
120120
os.rename(mempooldat0, mempooldat1)
121121
self.stop_nodes()
122122
self.start_node(1, extra_args=[])
123-
wait_until(lambda: len(self.nodes[1].getrawmempool()) == 5)
123+
wait_until(lambda: self.nodes[1].getmempoolinfo()["loaded"])
124+
assert_equal(len(self.nodes[1].getrawmempool()), 5)
124125

125126
self.log.debug("Prevent bitcoind from writing mempool.dat to disk. Verify that `savemempool` fails")
126127
# to test the exception we are creating a tmp folder called mempool.dat.new

0 commit comments

Comments
 (0)