Skip to content

Commit 3b740fa

Browse files
committed
feat: assumeutxo: enable loading snapshot in memory
1 parent 6f1d906 commit 3b740fa

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/rpc/blockchain.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2746,10 +2746,8 @@ static RPCHelpMan loadtxoutset()
27462746
"You can find more information on this process in the `assumeutxo` design "
27472747
"document (<https://github.com/bitcoin/bitcoin/blob/master/doc/design/assumeutxo.md>).",
27482748
{
2749-
{"path",
2750-
RPCArg::Type::STR,
2751-
RPCArg::Optional::NO,
2752-
"path to the snapshot file. If relative, will be prefixed by datadir."},
2749+
{"path", RPCArg::Type::STR, RPCArg::Optional::NO, "path to the snapshot file. If relative, will be prefixed by datadir."},
2750+
{"in_memory", RPCArg::Type::BOOL, RPCArg::Default{false}, "should we load snapshot in memory."},
27532751
},
27542752
RPCResult{
27552753
RPCResult::Type::OBJ, "", "",
@@ -2768,6 +2766,10 @@ static RPCHelpMan loadtxoutset()
27682766
NodeContext& node = EnsureAnyNodeContext(request.context);
27692767
ChainstateManager& chainman = EnsureChainman(node);
27702768
fs::path path{AbsPathForConfigVal(EnsureArgsman(node), fs::u8path(request.params[0].get_str()))};
2769+
bool in_memory = true;
2770+
if (!request.params[1].isNull()) {
2771+
in_memory = request.params[1].get_bool();
2772+
}
27712773

27722774
FILE* file{fsbridge::fopen(path, "rb")};
27732775
AutoFile afile{file};
@@ -2794,7 +2796,7 @@ static RPCHelpMan loadtxoutset()
27942796
strprintf("The base block header (%s) must appear in the headers chain. Make sure all headers are syncing, and call this RPC again.",
27952797
base_blockhash.ToString()));
27962798
}
2797-
if (!chainman.ActivateSnapshot(afile, metadata, false)) {
2799+
if (!chainman.ActivateSnapshot(afile, metadata, in_memory)) {
27982800
throw JSONRPCError(RPC_INTERNAL_ERROR, "Unable to load UTXO snapshot " + fs::PathToString(path));
27992801
}
28002802

test/functional/feature_assumeutxo.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,26 @@ def check_tx_counts(final: bool) -> None:
397397
assert_equal(loaded['coins_loaded'], SNAPSHOT_BASE_HEIGHT)
398398
assert_equal(loaded['base_height'], SNAPSHOT_BASE_HEIGHT)
399399

400+
401+
normal, snapshot = n2.getchainstates()['chainstates']
402+
assert_equal(normal['blocks'], START_HEIGHT)
403+
assert_equal(normal.get('snapshot_blockhash'), None)
404+
assert_equal(normal['validated'], True)
405+
assert_equal(snapshot['blocks'], SNAPSHOT_BASE_HEIGHT)
406+
assert_equal(snapshot['snapshot_blockhash'], dump_output['base_hash'])
407+
assert_equal(snapshot['validated'], False)
408+
409+
for reindex_arg in ['-reindex=1', '-reindex-chainstate=1']:
410+
self.log.info(f"Check that restarting with {reindex_arg} will delete the snapshot chainstate and load in_memory=true works")
411+
self.restart_node(2, extra_args=[reindex_arg, *self.extra_args[2]])
412+
assert_equal(1, len(n2.getchainstates()["chainstates"]))
413+
for i in range(1, 300):
414+
block = n0.getblock(n0.getblockhash(i), 0)
415+
n2.submitheader(block)
416+
loaded = n2.loadtxoutset(dump_output['path'], True)
417+
assert_equal(loaded['coins_loaded'], SNAPSHOT_BASE_HEIGHT)
418+
assert_equal(loaded['base_height'], SNAPSHOT_BASE_HEIGHT)
419+
400420
normal, snapshot = n2.getchainstates()['chainstates']
401421
assert_equal(normal['blocks'], START_HEIGHT)
402422
assert_equal(normal.get('snapshot_blockhash'), None)

0 commit comments

Comments
 (0)