Merge bitcoin/bitcoin#29345: rpc: Do not wait for headers inside loadtxoutset

faa30a4c56 rpc: Do not wait for headers inside loadtxoutset (MarcoFalke)

Pull request description:

  While the `loadtxoutset` default 10 minute timeout is convenient when it is sufficient, it may cause hassle where it is not. For example:

  * When P2P connections are missing, it seems better to abort early than wait for the timeout.
  * When the 10 minute timeout is not sufficient, the RPC will have to be called again, so a check or loop is needed outside the RPC either way. So might as well remove the loop inside the RPC.

ACKs for top commit:
  fjahr:
    ACK faa30a4c56
  theStack:
    Code-review ACK faa30a4c56
  pablomartin4btc:
    tACK faa30a4c56
  TheCharlatan:
    ACK faa30a4c56

Tree-SHA512: 9167c7d8b2889bb3fd369de4acd2cc4d24a2fe225018d82bd9568ecd737093f6e19be7cc62815b574137b61076a6f773c29bff75398991b5cd702423aab2322b
This commit is contained in:
fanquake 2024-02-26 10:56:45 +00:00
commit ba90b058bd
No known key found for this signature in database
GPG key ID: 2EEB9F5CC09526C1
2 changed files with 7 additions and 27 deletions

View file

@ -183,8 +183,8 @@ echo "-- Initial state of the client:"
client_rpc getchainstates
echo
echo "-- Loading UTXO snapshot into client..."
client_rpc loadtxoutset "$UTXO_DAT_FILE"
echo "-- Loading UTXO snapshot into client. Calling RPC in a loop..."
while ! client_rpc loadtxoutset "$UTXO_DAT_FILE" ; do sleep 10; done
watch -n 0.3 "( tail -n 14 $CLIENT_DATADIR/debug.log ; echo ; ./src/bitcoin-cli -rpcport=$CLIENT_RPC_PORT -datadir=$CLIENT_DATADIR getchainstates) | cat"

View file

@ -2571,7 +2571,7 @@ static RPCHelpMan dumptxoutset()
{
return RPCHelpMan{
"dumptxoutset",
"Write the serialized UTXO set to disk.",
"Write the serialized UTXO set to a file.",
{
{"path", RPCArg::Type::STR, RPCArg::Optional::NO, "Path to the output file. If relative, will be prefixed by datadir."},
},
@ -2699,7 +2699,7 @@ static RPCHelpMan loadtxoutset()
{
return RPCHelpMan{
"loadtxoutset",
"Load the serialized UTXO set from disk.\n"
"Load the serialized UTXO set from a file.\n"
"Once this snapshot is loaded, its contents will be "
"deserialized into a second chainstate data structure, which is then used to sync to "
"the network's tip. "
@ -2753,34 +2753,14 @@ static RPCHelpMan loadtxoutset()
throw JSONRPCError(RPC_INTERNAL_ERROR, strprintf("Unable to load UTXO snapshot, "
"assumeutxo block hash in snapshot metadata not recognized (%s)", base_blockhash.ToString()));
}
int max_secs_to_wait_for_headers = 60 * 10;
CBlockIndex* snapshot_start_block = nullptr;
LogPrintf("[snapshot] waiting to see blockheader %s in headers chain before snapshot activation\n",
base_blockhash.ToString());
while (max_secs_to_wait_for_headers > 0) {
snapshot_start_block = WITH_LOCK(::cs_main,
CBlockIndex* snapshot_start_block = WITH_LOCK(::cs_main,
return chainman.m_blockman.LookupBlockIndex(base_blockhash));
max_secs_to_wait_for_headers -= 1;
if (!IsRPCRunning()) {
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Shutting down");
}
if (!snapshot_start_block) {
std::this_thread::sleep_for(std::chrono::seconds(1));
} else {
break;
}
}
if (!snapshot_start_block) {
LogPrintf("[snapshot] timed out waiting for snapshot start blockheader %s\n",
base_blockhash.ToString());
throw JSONRPCError(
RPC_INTERNAL_ERROR,
"Timed out waiting for base block header to appear in headers chain");
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)) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Unable to load UTXO snapshot " + fs::PathToString(path));