Skip to content

Commit 5b073d9

Browse files
UdjinM6claude
andcommitted
fix: check index sync status before querying and show sync progress
Check sync status upfront for address, timestamp, and spent index RPCs. Include current sync height in error messages to help users understand progress, following the pattern used by coinstatsindex. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent bc7af3a commit 5b073d9

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

src/rpc/blockchain.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,12 +556,17 @@ static RPCHelpMan getblockhashes()
556556
throw JSONRPCError(RPC_MISC_ERROR, "Timestamp index is not enabled. Start with -timestampindex to enable.");
557557
}
558558

559+
const IndexSummary summary = g_timestampindex->GetSummary();
560+
if (!summary.synced) {
561+
throw JSONRPCError(RPC_MISC_ERROR, strprintf("Timestamp index is syncing. Current height: %d", summary.best_block_height));
562+
}
563+
559564
unsigned int high = request.params[0].getInt<int>();
560565
unsigned int low = request.params[1].getInt<int>();
561566
std::vector<uint256> blockHashes;
562567

563568
if (!g_timestampindex->GetBlockHashes(high, low, blockHashes)) {
564-
throw JSONRPCError(RPC_MISC_ERROR, "Timestamp index is still syncing. Try again later.");
569+
throw JSONRPCError(RPC_MISC_ERROR, "Failed to read timestamp index.");
565570
}
566571

567572
UniValue result(UniValue::VARR);

src/rpc/node.cpp

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,11 @@ static RPCHelpMan getaddressutxos()
500500
throw JSONRPCError(RPC_MISC_ERROR, "Address index is not enabled. Start with -addressindex to enable.");
501501
}
502502

503+
const IndexSummary summary = g_addressindex->GetSummary();
504+
if (!summary.synced) {
505+
throw JSONRPCError(RPC_MISC_ERROR, strprintf("Address index is syncing. Current height: %d", summary.best_block_height));
506+
}
507+
503508
std::vector<CAddressUnspentIndexEntry> unspentOutputs;
504509

505510
for (const auto& address : addresses) {
@@ -587,6 +592,12 @@ static RPCHelpMan getaddressdeltas()
587592
if (!g_addressindex) {
588593
throw JSONRPCError(RPC_MISC_ERROR, "Address index is not enabled. Start with -addressindex to enable.");
589594
}
595+
596+
const IndexSummary summary = g_addressindex->GetSummary();
597+
if (!summary.synced) {
598+
throw JSONRPCError(RPC_MISC_ERROR, strprintf("Address index is syncing. Current height: %d", summary.best_block_height));
599+
}
600+
590601
for (const auto& address : addresses) {
591602
if (start <= 0 || end <= 0) { start = 0; end = 0; }
592603
if (!g_addressindex->GetAddressIndex(address.first, address.second,
@@ -654,21 +665,22 @@ static RPCHelpMan getaddressbalance()
654665
if (!g_addressindex) {
655666
throw JSONRPCError(RPC_MISC_ERROR, "Address index is not enabled. Start with -addressindex to enable.");
656667
}
668+
669+
// Check sync status first to return a clear error message
670+
// Use the index's best block height instead of chain tip to avoid inconsistency
671+
const IndexSummary summary = g_addressindex->GetSummary();
672+
if (!summary.synced) {
673+
throw JSONRPCError(RPC_MISC_ERROR, strprintf("Address index is syncing. Current height: %d", summary.best_block_height));
674+
}
675+
int nHeight = summary.best_block_height;
676+
657677
for (const auto& address : addresses) {
658678
if (!g_addressindex->GetAddressIndex(address.first, address.second, addressIndex,
659679
/*start=*/0, /*end=*/0)) {
660680
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No information available for address");
661681
}
662682
}
663683

664-
// Use the index's best block height instead of chain tip to avoid inconsistency
665-
// The async index may be behind the chain tip during sync
666-
IndexSummary summary = g_addressindex->GetSummary();
667-
if (!summary.synced) {
668-
throw JSONRPCError(RPC_INTERNAL_ERROR, "Address index not yet synced");
669-
}
670-
int nHeight = summary.best_block_height;
671-
672684

673685
CAmount balance = 0;
674686
CAmount balance_spendable = 0;
@@ -742,6 +754,12 @@ static RPCHelpMan getaddresstxids()
742754
if (!g_addressindex) {
743755
throw JSONRPCError(RPC_MISC_ERROR, "Address index is not enabled. Start with -addressindex to enable.");
744756
}
757+
758+
const IndexSummary summary = g_addressindex->GetSummary();
759+
if (!summary.synced) {
760+
throw JSONRPCError(RPC_MISC_ERROR, strprintf("Address index is syncing. Current height: %d", summary.best_block_height));
761+
}
762+
745763
for (const auto& address : addresses) {
746764
if (start <= 0 || end <= 0) { start = 0; end = 0; }
747765
if (!g_addressindex->GetAddressIndex(address.first, address.second,
@@ -824,7 +842,7 @@ static RPCHelpMan getspentinfo()
824842
CSpentIndexValue value;
825843
bool found = false;
826844

827-
// Check the async index first
845+
// Check the async index first (may return false if still syncing)
828846
if (g_spentindex->GetSpentInfo(key, value)) {
829847
found = true;
830848
}
@@ -844,6 +862,10 @@ static RPCHelpMan getspentinfo()
844862
}
845863

846864
if (!found) {
865+
const IndexSummary summary = g_spentindex->GetSummary();
866+
if (!summary.synced) {
867+
throw JSONRPCError(RPC_MISC_ERROR, strprintf("Unable to get spent info. Spent index is syncing, current height: %d", summary.best_block_height));
868+
}
847869
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unable to get spent info");
848870
}
849871

0 commit comments

Comments
 (0)