diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index c9997ae063894..a535bd71d3519 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -2746,10 +2746,8 @@ static RPCHelpMan loadtxoutset() "You can find more information on this process in the `assumeutxo` design " "document ().", { - {"path", - RPCArg::Type::STR, - RPCArg::Optional::NO, - "path to the snapshot file. If relative, will be prefixed by datadir."}, + {"path", RPCArg::Type::STR, RPCArg::Optional::NO, "path to the snapshot file. If relative, will be prefixed by datadir."}, + {"in_memory", RPCArg::Type::BOOL, RPCArg::Default{false}, "should we load snapshot in memory."}, }, RPCResult{ RPCResult::Type::OBJ, "", "", @@ -2768,6 +2766,10 @@ static RPCHelpMan loadtxoutset() NodeContext& node = EnsureAnyNodeContext(request.context); ChainstateManager& chainman = EnsureChainman(node); fs::path path{AbsPathForConfigVal(EnsureArgsman(node), fs::u8path(request.params[0].get_str()))}; + bool in_memory = true; + if (!request.params[1].isNull()) { + in_memory = request.params[1].get_bool(); + } FILE* file{fsbridge::fopen(path, "rb")}; AutoFile afile{file}; @@ -2794,7 +2796,7 @@ static RPCHelpMan loadtxoutset() strprintf("The base block header (%s) must appear in the headers chain. Make sure all headers are syncing, and call this RPC again.", base_blockhash.ToString())); } - if (!chainman.ActivateSnapshot(afile, metadata, false)) { + if (!chainman.ActivateSnapshot(afile, metadata, in_memory)) { throw JSONRPCError(RPC_INTERNAL_ERROR, "Unable to load UTXO snapshot " + fs::PathToString(path)); } diff --git a/test/functional/feature_assumeutxo.py b/test/functional/feature_assumeutxo.py index 58a5442f4e652..d2f7b335cc980 100755 --- a/test/functional/feature_assumeutxo.py +++ b/test/functional/feature_assumeutxo.py @@ -397,6 +397,26 @@ def check_tx_counts(final: bool) -> None: assert_equal(loaded['coins_loaded'], SNAPSHOT_BASE_HEIGHT) assert_equal(loaded['base_height'], SNAPSHOT_BASE_HEIGHT) + + normal, snapshot = n2.getchainstates()['chainstates'] + assert_equal(normal['blocks'], START_HEIGHT) + assert_equal(normal.get('snapshot_blockhash'), None) + assert_equal(normal['validated'], True) + assert_equal(snapshot['blocks'], SNAPSHOT_BASE_HEIGHT) + assert_equal(snapshot['snapshot_blockhash'], dump_output['base_hash']) + assert_equal(snapshot['validated'], False) + + for reindex_arg in ['-reindex=1', '-reindex-chainstate=1']: + self.log.info(f"Check that restarting with {reindex_arg} will delete the snapshot chainstate and load in_memory=true works") + self.restart_node(2, extra_args=[reindex_arg, *self.extra_args[2]]) + assert_equal(1, len(n2.getchainstates()["chainstates"])) + for i in range(1, 300): + block = n0.getblock(n0.getblockhash(i), 0) + n2.submitheader(block) + loaded = n2.loadtxoutset(dump_output['path'], True) + assert_equal(loaded['coins_loaded'], SNAPSHOT_BASE_HEIGHT) + assert_equal(loaded['base_height'], SNAPSHOT_BASE_HEIGHT) + normal, snapshot = n2.getchainstates()['chainstates'] assert_equal(normal['blocks'], START_HEIGHT) assert_equal(normal.get('snapshot_blockhash'), None)