Skip to content

Commit 97b358f

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

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/rpc/blockchain.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -2750,6 +2750,10 @@ static RPCHelpMan loadtxoutset()
27502750
RPCArg::Type::STR,
27512751
RPCArg::Optional::NO,
27522752
"path to the snapshot file. If relative, will be prefixed by datadir."},
2753+
{"in_memory",
2754+
RPCArg::Type::BOOL,
2755+
RPCArg::Optional::OMITTED,
2756+
"should we load snapshot in memory."}
27532757
},
27542758
RPCResult{
27552759
RPCResult::Type::OBJ, "", "",
@@ -2761,13 +2765,15 @@ static RPCHelpMan loadtxoutset()
27612765
}
27622766
},
27632767
RPCExamples{
2764-
HelpExampleCli("loadtxoutset", "utxo.dat")
2768+
HelpExampleCli("loadtxoutset", "utxo.dat") +
2769+
HelpExampleCli("dumptxoutset", "\"utxo.dat\" \"true\""),
27652770
},
27662771
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
27672772
{
27682773
NodeContext& node = EnsureAnyNodeContext(request.context);
27692774
ChainstateManager& chainman = EnsureChainman(node);
27702775
fs::path path{AbsPathForConfigVal(EnsureArgsman(node), fs::u8path(request.params[0].get_str()))};
2776+
bool in_memory{request.params[1].isNull() ? false : request.params[1].get_bool()};
27712777

27722778
FILE* file{fsbridge::fopen(path, "rb")};
27732779
AutoFile afile{file};
@@ -2794,7 +2800,7 @@ static RPCHelpMan loadtxoutset()
27942800
strprintf("The base block header (%s) must appear in the headers chain. Make sure all headers are syncing, and call this RPC again.",
27952801
base_blockhash.ToString()));
27962802
}
2797-
if (!chainman.ActivateSnapshot(afile, metadata, false)) {
2803+
if (!chainman.ActivateSnapshot(afile, metadata, in_memory)) {
27982804
throw JSONRPCError(RPC_INTERNAL_ERROR, "Unable to load UTXO snapshot " + fs::PathToString(path));
27992805
}
28002806

test/functional/feature_assumeutxo.py

+20
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)