From 8b9a498eee6178af40760ae19ff40c24525324f6 Mon Sep 17 00:00:00 2001 From: DanS Date: Fri, 9 Oct 2026 11:06:34 -0500 Subject: [PATCH] perf(net): one balance scan per poll, filtered opstatus, a guarded merge scope The Core poll ran z_gettotalbalance twice (minconf 0 and 1), each a whole- wallet scan under dragonxd's cs_main. The spendable (minconf=1) call now runs only after the display call succeeded, and only when something is unconfirmed or pending or the exact total moved: about 95% of polls make one scan instead of two. A forced post-send balance can no longer be lost to a refused enqueue, and pending-send debits aren't re-applied each tick. z_getoperationstatus is filtered to our opids with its real argument form ([[id,...]]; the old ["id"] was rejected), falling back to the unfiltered call if a daemon refuses the filter. The Shield/Merge dialog's own polls are filtered and session-guarded too, and finally reach Done; the dead RPCClient wrappers are deleted. The Merge/Consolidate scope scan becomes a tested state machine: one scan at a time, failures latch behind a Retry button, only "node not ready" retries by itself (10/30/60 s, at most 3), Lite never scans. Its 120 s calls are maintenance calls: every main-thread disconnect aborts one in flight first, so a lifecycle action no longer freezes the UI behind it. An aborted merge that was already sent says it may still go through. Co-Authored-By: Claude Opus 5.5 --- res/lang/de.json | 5 + res/lang/es.json | 5 + res/lang/fr.json | 5 + res/lang/ja.json | 5 + res/lang/ko.json | 5 + res/lang/pt.json | 5 + res/lang/ru.json | 5 + res/lang/zh.json | 5 + src/app.cpp | 27 +- src/app.h | 61 +++- src/app_network.cpp | 64 +++- src/app_wizard.cpp | 1 + src/rpc/rpc_client.cpp | 28 -- src/rpc/rpc_client.h | 5 - src/services/network_refresh_service.cpp | 108 ++++++- src/services/network_refresh_service.h | 38 ++- src/services/note_consolidation.cpp | 111 +++++++ src/services/note_consolidation.h | 66 ++++- src/ui/pages/settings_page.cpp | 15 +- src/ui/windows/shield_dialog.cpp | 244 ++++++++++++---- src/ui/windows/shield_dialog.h | 6 + src/util/i18n.cpp | 5 + tests/test_phase4.cpp | 353 ++++++++++++++++++++++- 23 files changed, 1044 insertions(+), 128 deletions(-) diff --git a/res/lang/de.json b/res/lang/de.json index 0a4a69b..f6bb536 100644 --- a/res/lang/de.json +++ b/res/lang/de.json @@ -1178,10 +1178,12 @@ "merge_keep_each_status_failed": "%s – fehlgeschlagen: %s", "merge_keep_each_status_not_started": "%s – nicht gestartet: %s", "merge_keep_each_status_running": "%s – %d Notes werden zusammengeführt (%d von %d Durchgängen fertig)…", + "merge_keep_each_status_unconfirmed": "%s – Status konnte nicht bestätigt werden", "merge_keep_each_too_small": "Zu klein zum Konsolidieren: Die Gebühr würde diese Mittel aufbrauchen.", "merge_max_inputs": "Max. Inputs pro Batch", "merge_no_spendable": "Noch keine verfügbaren Inputs zum Konsolidieren.", "merge_progress": "Konsolidierung läuft… dies kann einige Minuten dauern. Sie können dieses Fenster schließen.", + "merge_scope_failed_fmt": "Ihre Inputs konnten nicht gelesen werden: %s", "merge_scope_fmt": "%d transparente + %d geschützte Inputs · ~%s DRGX verfügbar", "merge_scope_loading": "Ihre Inputs werden geprüft…", "merge_send_failed": "Zusammenführen fehlgeschlagen: ", @@ -1190,7 +1192,10 @@ "merge_src_shielded": "Geschützt", "merge_src_transparent": "Transparent", "merge_started": "Zusammenführung gestartet", + "merge_submit_uncertain": "Der Node hat das Zusammenführen nicht bestätigt, bevor die Verbindung unterbrochen wurde. Es kann trotzdem ausgeführt werden – prüfen Sie nach dem erneuten Verbinden den Verlauf, bevor Sie erneut zusammenführen.", "merge_title": "An Adresse zusammenführen", + "merge_wait_locked": "Die Wallet ist gesperrt. Entsperren Sie sie, um fortzufahren.", + "merge_waiting_node": "Warten auf den Node…", "merge_where": "Wohin sollen die Mittel gehen?", "mig_adopt": "Dies zu meiner Wallet machen", "mig_adopt_now": "Seed-Wallet übernehmen", diff --git a/res/lang/es.json b/res/lang/es.json index 3857954..e07c2e6 100644 --- a/res/lang/es.json +++ b/res/lang/es.json @@ -1178,10 +1178,12 @@ "merge_keep_each_status_failed": "%s: falló: %s", "merge_keep_each_status_not_started": "%s: no iniciado: %s", "merge_keep_each_status_running": "%s: fusionando %d notas (%d de %d lotes listos)…", + "merge_keep_each_status_unconfirmed": "%s: no se pudo confirmar el estado", "merge_keep_each_too_small": "Demasiado poco para consolidar: la comisión se llevaría estos fondos.", "merge_max_inputs": "Máximo de entradas por lote", "merge_no_spendable": "Aún no hay entradas disponibles para consolidar.", "merge_progress": "Consolidando… esto puede tardar varios minutos. Puedes cerrar esta ventana.", + "merge_scope_failed_fmt": "No se pudieron leer tus entradas: %s", "merge_scope_fmt": "%d entradas transparentes + %d blindadas · ~%s DRGX disponibles", "merge_scope_loading": "Comprobando tus entradas…", "merge_send_failed": "Error al fusionar: ", @@ -1190,7 +1192,10 @@ "merge_src_shielded": "Blindadas", "merge_src_transparent": "Transparentes", "merge_started": "Operación de fusión iniciada", + "merge_submit_uncertain": "El nodo no confirmó la fusión antes de que se interrumpiera la conexión. Puede que aún se complete: revisa el Historial tras reconectar antes de fusionar de nuevo.", "merge_title": "Fusionar a Dirección", + "merge_wait_locked": "La cartera está bloqueada. Desbloquéala para continuar.", + "merge_waiting_node": "Esperando al nodo…", "merge_where": "¿Adónde deben ir los fondos?", "mig_adopt": "Hacer que esta sea mi cartera", "mig_adopt_now": "Adoptar cartera con frase", diff --git a/res/lang/fr.json b/res/lang/fr.json index bb25d3a..edb322a 100644 --- a/res/lang/fr.json +++ b/res/lang/fr.json @@ -1178,10 +1178,12 @@ "merge_keep_each_status_failed": "%s — échec : %s", "merge_keep_each_status_not_started": "%s — non démarré : %s", "merge_keep_each_status_running": "%s — fusion de %d notes (%d lots sur %d terminés)…", + "merge_keep_each_status_unconfirmed": "%s — statut non confirmé", "merge_keep_each_too_small": "Trop faible pour être consolidé : les frais absorberaient ces fonds.", "merge_max_inputs": "Nombre max d'entrées par lot", "merge_no_spendable": "Aucune entrée dépensable à consolider pour l'instant.", "merge_progress": "Consolidation en cours… cela peut prendre quelques minutes. Vous pouvez fermer cette fenêtre.", + "merge_scope_failed_fmt": "Impossible de lire vos entrées : %s", "merge_scope_fmt": "%d entrées transparentes + %d blindées · ~%s DRGX dépensables", "merge_scope_loading": "Vérification de vos entrées…", "merge_send_failed": "Échec de la fusion : ", @@ -1190,7 +1192,10 @@ "merge_src_shielded": "Blindées", "merge_src_transparent": "Transparentes", "merge_started": "Opération de fusion démarrée", + "merge_submit_uncertain": "Le nœud n'a pas confirmé la fusion avant l'interruption de la connexion. Elle peut tout de même aboutir — vérifiez l'Historique après la reconnexion avant de fusionner à nouveau.", "merge_title": "Fusionner vers une adresse", + "merge_wait_locked": "Le portefeuille est verrouillé. Déverrouillez-le pour continuer.", + "merge_waiting_node": "En attente du nœud…", "merge_where": "Où doivent aller les fonds ?", "mig_adopt": "Faire de ceci mon portefeuille", "mig_adopt_now": "Adopter le portefeuille à phrase", diff --git a/res/lang/ja.json b/res/lang/ja.json index 2e3f362..19a7130 100644 --- a/res/lang/ja.json +++ b/res/lang/ja.json @@ -1178,10 +1178,12 @@ "merge_keep_each_status_failed": "%s — 失敗:%s", "merge_keep_each_status_not_started": "%s — 開始されませんでした:%s", "merge_keep_each_status_running": "%s — %d 件のノートを統合中(%d / %d 回完了)…", + "merge_keep_each_status_unconfirmed": "%s — ステータスを確認できませんでした", "merge_keep_each_too_small": "少なすぎて統合できません。手数料でこの資金がなくなります。", "merge_max_inputs": "バッチあたりの最大入力数", "merge_no_spendable": "統合できる使用可能な入力がまだありません。", "merge_progress": "統合中… 数分かかることがあります。このウィンドウは閉じても構いません。", + "merge_scope_failed_fmt": "入力を読み取れませんでした: %s", "merge_scope_fmt": "透明%d件 + シールド%d件の入力 · 使用可能 約%s DRGX", "merge_scope_loading": "入力を確認中…", "merge_send_failed": "マージに失敗しました: ", @@ -1190,7 +1192,10 @@ "merge_src_shielded": "シールド", "merge_src_transparent": "透明", "merge_started": "統合操作を開始しました", + "merge_submit_uncertain": "接続が中断される前に、ノードはマージを確認しませんでした。マージは実行される可能性があります — 再接続後、もう一度マージする前に「履歴」を確認してください。", "merge_title": "アドレスに統合", + "merge_wait_locked": "ウォレットがロックされています。続行するにはロックを解除してください。", + "merge_waiting_node": "ノードを待っています…", "merge_where": "資金の移動先", "mig_adopt": "これを自分のウォレットにする", "mig_adopt_now": "シードウォレットを採用", diff --git a/res/lang/ko.json b/res/lang/ko.json index 50065c1..7d02462 100644 --- a/res/lang/ko.json +++ b/res/lang/ko.json @@ -1178,10 +1178,12 @@ "merge_keep_each_status_failed": "%s — 실패: %s", "merge_keep_each_status_not_started": "%s — 시작되지 않음: %s", "merge_keep_each_status_running": "%s — 노트 %d개 병합 중(%d/%d 배치 완료)…", + "merge_keep_each_status_unconfirmed": "%s — 상태를 확인할 수 없음", "merge_keep_each_too_small": "너무 적어서 통합할 수 없습니다. 수수료가 이 자금을 모두 소모합니다.", "merge_max_inputs": "배치당 최대 입력 수", "merge_no_spendable": "아직 통합할 사용 가능한 입력이 없습니다.", "merge_progress": "통합 중… 몇 분 정도 걸릴 수 있습니다. 이 창을 닫아도 됩니다.", + "merge_scope_failed_fmt": "입력을 읽을 수 없습니다: %s", "merge_scope_fmt": "투명 입력 %d개 + 보호 입력 %d개 · 사용 가능 약 %s DRGX", "merge_scope_loading": "입력을 확인하는 중…", "merge_send_failed": "통합 실패: ", @@ -1190,7 +1192,10 @@ "merge_src_shielded": "보호", "merge_src_transparent": "투명", "merge_started": "통합 작업이 시작되었습니다", + "merge_submit_uncertain": "연결이 끊기기 전에 노드가 통합을 확인하지 않았습니다. 그래도 처리될 수 있습니다 — 다시 연결한 후 다시 통합하기 전에 내역을 확인하세요.", "merge_title": "주소로 통합", + "merge_wait_locked": "지갑이 잠겨 있습니다. 계속하려면 잠금을 해제하세요.", + "merge_waiting_node": "노드를 기다리는 중…", "merge_where": "자금을 어디로 보낼까요?", "mig_adopt": "이 지갑을 내 지갑으로 사용", "mig_adopt_now": "시드 지갑 사용", diff --git a/res/lang/pt.json b/res/lang/pt.json index 29b16ad..14cdcf2 100644 --- a/res/lang/pt.json +++ b/res/lang/pt.json @@ -1178,10 +1178,12 @@ "merge_keep_each_status_failed": "%s — falhou: %s", "merge_keep_each_status_not_started": "%s — não iniciado: %s", "merge_keep_each_status_running": "%s — mesclando %d notas (%d de %d lotes concluídos)…", + "merge_keep_each_status_unconfirmed": "%s — não foi possível confirmar o status", "merge_keep_each_too_small": "Pequeno demais para consolidar: a taxa consumiria esses fundos.", "merge_max_inputs": "Máx. de entradas por lote", "merge_no_spendable": "Ainda não há entradas disponíveis para consolidar.", "merge_progress": "Consolidando… isso pode levar alguns minutos. Você pode fechar esta janela.", + "merge_scope_failed_fmt": "Não foi possível ler suas entradas: %s", "merge_scope_fmt": "%d entradas transparentes + %d blindadas · ~%s DRGX disponíveis", "merge_scope_loading": "Verificando suas entradas…", "merge_send_failed": "Falha na fusão: ", @@ -1190,7 +1192,10 @@ "merge_src_shielded": "Blindado", "merge_src_transparent": "Transparente", "merge_started": "Operação de fusão iniciada", + "merge_submit_uncertain": "O nó não confirmou a fusão antes de a conexão ser interrompida. Ela ainda pode ser concluída — verifique o Histórico após reconectar antes de fundir novamente.", "merge_title": "Fundir para Endereço", + "merge_wait_locked": "A carteira está bloqueada. Desbloqueie-a para continuar.", + "merge_waiting_node": "Aguardando o nó…", "merge_where": "Para onde devem ir os fundos?", "mig_adopt": "Tornar esta minha carteira", "mig_adopt_now": "Adotar carteira semente", diff --git a/res/lang/ru.json b/res/lang/ru.json index 5b0e217..148f6f4 100644 --- a/res/lang/ru.json +++ b/res/lang/ru.json @@ -1178,10 +1178,12 @@ "merge_keep_each_status_failed": "%s — ошибка: %s", "merge_keep_each_status_not_started": "%s — не запущено: %s", "merge_keep_each_status_running": "%s — объединение %d заметок (готово партий: %d из %d)…", + "merge_keep_each_status_unconfirmed": "%s — статус не удалось подтвердить", "merge_keep_each_too_small": "Слишком мало для объединения: комиссия съест эти средства.", "merge_max_inputs": "Макс. входов за партию", "merge_no_spendable": "Пока нет доступных для расходования входов для объединения.", "merge_progress": "Объединение… это может занять несколько минут. Вы можете закрыть это окно.", + "merge_scope_failed_fmt": "Не удалось прочитать ваши входы: %s", "merge_scope_fmt": "%d прозрачных + %d защищённых входов · ~%s DRGX доступно", "merge_scope_loading": "Проверка ваших входов…", "merge_send_failed": "Ошибка объединения: ", @@ -1190,7 +1192,10 @@ "merge_src_shielded": "Защищённые", "merge_src_transparent": "Прозрачные", "merge_started": "Операция объединения начата", + "merge_submit_uncertain": "Узел не подтвердил объединение до разрыва соединения. Оно всё ещё может выполниться — после переподключения проверьте Историю, прежде чем объединять снова.", "merge_title": "Объединить на адрес", + "merge_wait_locked": "Кошелёк заблокирован. Разблокируйте его, чтобы продолжить.", + "merge_waiting_node": "Ожидание узла…", "merge_where": "Куда направить средства?", "mig_adopt": "Сделать это моим кошельком", "mig_adopt_now": "Принять сид-кошелёк", diff --git a/res/lang/zh.json b/res/lang/zh.json index c91879f..50da100 100644 --- a/res/lang/zh.json +++ b/res/lang/zh.json @@ -1178,10 +1178,12 @@ "merge_keep_each_status_failed": "%s——失败:%s", "merge_keep_each_status_not_started": "%s——未开始:%s", "merge_keep_each_status_running": "%s——正在合并 %d 张票据(已完成 %d/%d 批)…", + "merge_keep_each_status_unconfirmed": "%s——无法确认状态", "merge_keep_each_too_small": "金额太小,无法整合:手续费会耗尽这些资金。", "merge_max_inputs": "每批最大输入数", "merge_no_spendable": "暂无可合并的可用输入。", "merge_progress": "正在合并…这可能需要几分钟。您可以关闭此窗口。", + "merge_scope_failed_fmt": "无法读取您的输入:%s", "merge_scope_fmt": "%d 笔透明输入 + %d 笔屏蔽输入 ・ 约 %s DRGX 可用", "merge_scope_loading": "正在检查您的输入…", "merge_send_failed": "合并失败:", @@ -1190,7 +1192,10 @@ "merge_src_shielded": "屏蔽", "merge_src_transparent": "透明", "merge_started": "合并操作已开始", + "merge_submit_uncertain": "连接中断前节点未确认合并。合并仍可能完成——重新连接后,请先在“历史”中确认,再重新合并。", "merge_title": "合并到地址", + "merge_wait_locked": "钱包已锁定。请解锁后继续。", + "merge_waiting_node": "正在等待节点…", "merge_where": "资金应转到哪里?", "mig_adopt": "将其设为我的钱包", "mig_adopt_now": "采用助记词钱包", diff --git a/src/app.cpp b/src/app.cpp index 8a0b2b0..cde9ffa 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -325,6 +325,7 @@ public: void beginBackgroundStop(const char* reason) override { + app_.abortMaintenanceCalls(); // a long call holds the curl lock disconnect() needs if (app_.rpc_ && app_.rpc_->isConnected()) app_.rpc_->disconnect(); app_.onDisconnected(reason ? reason : "Daemon lifecycle"); App& app = app_; @@ -1289,13 +1290,28 @@ void App::update() if (!rpc) return [this](){ opid_poll_in_progress_ = false; }; json result; services::NetworkRefreshService::OperationStatusPollResult parsed; + const auto filter = services::NetworkRefreshService::buildOperationFilterParams(opids); + if (!filter) return [this](){ opid_poll_in_progress_ = false; }; try { rpc::RPCClient::TraceScope trace("Send tab / Operation status"); - // No per-opid filter: this daemon rejects z_getoperationstatus(["opid"]) with - // "JSON value is not an array as expected", which left completed sends stuck on - // "Waiting for operation". The no-arg form returns ALL operations; - // parseOperationStatusPoll() filters down to the opids we're tracking. - result = rpc->call("z_getoperationstatus", json::array()); + // Filtered to our opids: the call takes cs_main + cs_wallet and builds a status object for + // every op it reports, and the daemon retains finished ops, so the unfiltered form grew + // with every send, shield and chat message. Its one argument is an ARRAY of ids, + // [[id, ...]]; the old ["opid"] attempt failed with "JSON value is not an array as + // expected" because params[0] was a string. If a daemon still answers the filter with an + // error, fall back to the unfiltered form for this connection rather than wedge sends on + // "Waiting for operation"; parseOperationStatusPoll() ignores ops we don't track. + if (!opid_filter_unsupported_.load(std::memory_order_relaxed)) { + try { + result = rpc->call("z_getoperationstatus", *filter); + } catch (const rpc::RpcError& e) { + if (e.kind() == rpc::RpcFailureKind::Warmup) throw; + result = rpc->call("z_getoperationstatus", json::array()); + opid_filter_unsupported_.store(true, std::memory_order_relaxed); + } + } else { + result = rpc->call("z_getoperationstatus", json::array()); + } // Parse INSIDE the guard: a malformed/type-anomalous element must never abort the // poll and leave opid_poll_in_progress_ stuck true for the whole connected session. parsed = services::NetworkRefreshService::parseOperationStatusPoll(result, opids); @@ -7496,6 +7512,7 @@ void App::restartDaemon() // Disconnect RPC so the loading overlay appears if (decision.disconnectRpc && rpc_ && rpc_->isConnected()) { + abortMaintenanceCalls(); // a long call holds the curl lock disconnect() needs rpc_->disconnect(); } onDisconnected("Daemon restart"); diff --git a/src/app.h b/src/app.h index 8d3b753..c8f8e82 100644 --- a/src/app.h +++ b/src/app.h @@ -15,6 +15,8 @@ #include #include #include +#include +#include #include #include "data/transaction_history_cache.h" #include "data/address_book.h" @@ -432,6 +434,42 @@ public: // richer pending-send path internally; this is for operations with no optimistic // transaction row of their own. void trackOperation(const std::string& opid); + // The connected daemon rejected the opid-filtered z_getoperationstatus, so polls use the unfiltered + // form (every retained op) for the rest of this connection. + bool opidFilterUnsupported() const { return opid_filter_unsupported_.load(std::memory_order_relaxed); } + + // RAII around one long ("maintenance") rpc_ call made on a worker thread with a timeout above the + // default 30 s, e.g. a merge or a whole-wallet scan given 120 s. rpc_ holds its curl lock for the + // whole transfer and disconnect() takes the same lock, so these are counted for + // abortMaintenanceCalls(). + class MaintenanceCallScope { + public: + explicit MaintenanceCallScope(App& app) : app_(app) + { + // Count first, then read the flag; abortMaintenanceCalls() sets the flag, then reads the count. + // Either this sees the abort and never sends, or the abort sees this call and cuts it off. (A + // call queued behind the one disconnect() waits on could otherwise take the curl lock next + // and hold disconnect() for its full 120 s.) + app_.maint_calls_.fetch_add(1); + if (app_.maint_abort_.load()) { + app_.maint_calls_.fetch_sub(1); + throw rpc::RpcTransportError(rpc::RpcFailureKind::Aborted, "RPC aborted: disconnecting"); + } + } + ~MaintenanceCallScope() { app_.maint_calls_.fetch_sub(1); } + MaintenanceCallScope(const MaintenanceCallScope&) = delete; + MaintenanceCallScope& operator=(const MaintenanceCallScope&) = delete; + private: + App& app_; + }; + // Main thread, right before rpc_->disconnect(): abort rpc_'s in-flight call if it is a maintenance + // call, so the disconnect doesn't freeze the UI for up to its 120 s, and refuse maintenance calls that + // start before the next connect (onConnected() lifts that). Ordinary calls are left to finish (at most + // 30 s, as before), so a user's own send is never cut off mid-call. + void abortMaintenanceCalls(); + // Whether a whole-wallet scan the user didn't ask for may be sent now: a connected, unlocked, synced + // full node that is past warmup, not rescanning and not busy. + bool walletScanReady() const; // Force refresh void refreshNow(); @@ -738,14 +776,18 @@ private: // t_addresses for `fromAddress` and clamp its balance at >=0. A positive `signedAmount` // restores a debit; a negative one applies it. When `includeAggregates` is set, adjust the // private/transparent bucket (chosen by the address's leading 'z') and totalBalance with the - // same clamp. Shared by the three pending-send delta sites so they can't drift. + // same clamp. Shared by the three pending-send delta sites so they can't drift. `includeAddress` + // off leaves the per-address balances alone (the Core poll refreshes only the aggregates). void applyPendingSendDelta(const std::string& fromAddress, double signedAmount, - bool includeAggregates); + bool includeAggregates, bool includeAddress = true); // Deliver a deferred z_sendmany result to its waiting UI callback once the opid // reaches a terminal status. Returns true if a callback was registered (and fired). bool invokeSendResultCallback(const std::string& opid, bool ok, const std::string& result); - void applyPendingSendBalanceDeltas(bool includeAggregateBalances); + // Re-apply every pending send's debit on top of freshly fetched balances: the aggregates after a + // Core poll that carried a balance, the per-address balances after an address scan. Only onto + // fresh values, or a debit applied again on every tick would drain them. + void applyPendingSendBalanceDeltas(bool includeAggregateBalances, bool includeAddressBalances = true); std::string transactionHistoryCacheWalletIdentity() const; bool ensureTransactionHistoryCacheUnlockedFor(const std::string& walletIdentity); // Record the active wallet's cached metadata (balance, address count, identity, size) into the @@ -1302,6 +1344,16 @@ private: std::time_t sync_settle_until_ = 0; // hold the sync-throttle until this wall-clock time (0 = not settling) double last_balance_scan_ms_ = 0.0; // measured cost of the last z_gettotalbalance scan bool force_balance_refresh_ = false; // a wallet mutation forces the next balance poll through the throttle + // Bumped with each forced-refresh request, so the poll that carried the forced balance clears the + // flag only if no newer request came in meanwhile (see requestForcedBalanceRefresh()). + std::uint32_t force_balance_seq_ = 0; + // The exact minconf=0 total of the last balance poll: the minconf=1 call is skipped while it holds + // still and nothing is pending (NetworkRefreshService::CoreRefreshPlan). + std::optional last_total_balance_zat_; + // Force the next balance poll through the throttle (the user's own send). Cleared by the Core poll + // that actually sent the forced balance call, not at dispatch, so a dispatch the job queue refuses + // (a Core job already in flight) or one that skips the balance (node behind or busy) keeps it. + void requestForcedBalanceRefresh() { force_balance_refresh_ = true; ++force_balance_seq_; } // Same adaptive back-off applied to the other two O(mapWallet) scans that hold the daemon's cs_main: // the address scan (z_listunspent) and the history scan (z_listreceivedbyaddress). Without this, a // large shielded wallet re-scans them every tab cadence (~seconds each), saturating cs_main and @@ -1490,6 +1542,9 @@ private: std::unordered_set witness_seen_txids_; int witness_total_txs_ = 0; bool opid_poll_in_progress_ = false; + std::atomic opid_filter_unsupported_{false}; // see opidFilterUnsupported(); set on the worker + std::atomic maint_calls_{0}; // maintenance calls in flight (MaintenanceCallScope) + std::atomic maint_abort_{false}; // set by abortMaintenanceCalls(), cleared on connect // Consecutive Core-refresh cycles where BOTH core RPCs failed → likely a dead // connection. After kCoreFailuresBeforeDisconnect, tear down and reconnect. int consecutive_core_failures_ = 0; diff --git a/src/app_network.cpp b/src/app_network.cpp index 383a8e7..20e7609 100644 --- a/src/app_network.cpp +++ b/src/app_network.cpp @@ -687,7 +687,9 @@ void App::tryConnect() void App::onConnected() { + maint_abort_.store(false); // maintenance calls queued before this connect have already failed (FIFO) state_.connected = true; + ui::ShieldDialog::invalidateScope(); // inputs read before this connect may be another wallet's const bool completedSwitch = wallet_switch_pending_confirm_.exchange(false); // this connect confirms a switch // A successful connect completes a switch — close/clear the progress modal (kept open through the // stop → start → reconnect sequence, or already hidden via "continue in background"). @@ -837,6 +839,9 @@ void App::onDisconnected(const std::string& reason) network_refresh_.resetJobs(); rescan_status_poll_in_progress_ = false; opid_poll_in_progress_ = false; + opid_filter_unsupported_ = false; // a reconnect may be to a different daemon + last_total_balance_zat_.reset(); + ui::ShieldDialog::invalidateScope(); address_validation_cache_dirty_ = true; resetTransactionHistoryCacheSession(); @@ -937,6 +942,7 @@ void App::handleLostConnection(const std::string& reason) DEBUG_LOGF("[Connection] %s — tearing down for reconnect\n", reason.c_str()); // Flip the main client's connected_ flag so update()'s else-branch re-enters // tryConnect(). onDisconnected() alone only tears down the fast lane. + abortMaintenanceCalls(); // a long call holds the curl lock disconnect() needs if (rpc_) rpc_->disconnect(); onDisconnected(reason); } @@ -989,6 +995,24 @@ bool App::effectivelySyncing() const return std::time(nullptr) < sync_settle_until_; } +void App::abortMaintenanceCalls() +{ + // Both stick until the next connect, which is fine here: every caller disconnects next. + maint_abort_.store(true); // refuses maintenance calls still queued on the worker + if (rpc_ && maint_calls_.load() > 0) rpc_->requestAbort(); // cuts off the one in flight +} + +bool App::walletScanReady() const +{ + if (isLiteBuild() || capture_mode_ || shutting_down_.load()) return false; + // state_.warming_up, not rpc_->isWarmingUp(): the client's flag is set when a connect lands in warmup + // and stays set until the next reconnect, which kept this false for the whole session. + if (!rpc_ || !rpc_->isConnected()) return false; + if (!state_.connected || state_.warming_up || state_.isLocked()) return false; + if (state_.sync.rescanning || runtime_rescan_active_ || effectivelySyncing()) return false; + return !rpc_busy_gate_->busy(); +} + // Adaptive throttle: the next balance poll must wait at least (lastScanCost / kBalanceDutyCycle) since // the last one, so balance scanning can never occupy more than ~kBalanceDutyCycle of wall-clock. A // cheap wallet (sub-cadence cost) is unaffected — the tab's Core timer stays the real cadence; a ~20s @@ -1194,7 +1218,7 @@ void App::removePendingSendTransactions(const std::vector& opids, } void App::applyPendingSendDelta(const std::string& fromAddress, double signedAmount, - bool includeAggregates) + bool includeAggregates, bool includeAddress) { // For a debit (signedAmount < 0) this clamps at >=0 exactly as the debit sites did. For a // restore (signedAmount > 0) the clamp is a no-op — max(0, bal+amt) == bal+amt when bal,amt>=0 — @@ -1211,7 +1235,7 @@ void App::applyPendingSendDelta(const std::string& fromAddress, double signedAmo } return false; }; - if (!applyToAddress(state_.z_addresses)) applyToAddress(state_.t_addresses); + if (includeAddress && !applyToAddress(state_.z_addresses)) applyToAddress(state_.t_addresses); if (includeAggregates) { if (!fromAddress.empty() && fromAddress[0] == 'z') { state_.spendablePrivateBalance = std::max(0.0, state_.spendablePrivateBalance + signedAmount); @@ -1244,11 +1268,11 @@ bool App::invokeSendResultCallback(const std::string& opid, bool ok, return true; } -void App::applyPendingSendBalanceDeltas(bool includeAggregateBalances) +void App::applyPendingSendBalanceDeltas(bool includeAggregateBalances, bool includeAddressBalances) { for (const auto& [opid, pending] : pending_send_info_) { (void)opid; - applyPendingSendDelta(pending.from, -pending.amount, includeAggregateBalances); + applyPendingSendDelta(pending.from, -pending.amount, includeAggregateBalances, includeAddressBalances); } } @@ -1458,6 +1482,7 @@ void App::switchToWallet(const std::string& walletFile, bool stopDaemonConfirmed // start, so the daemon comes up on -wallet=. WalletState clears on disconnect and the // per-wallet caches re-key off the new identity (P1), so no previous-wallet data leaks through. daemon_restarting_ = true; + abortMaintenanceCalls(); // a long call holds the curl lock disconnect() needs if (rpc_ && rpc_->isConnected()) rpc_->disconnect(); onDisconnected("Switching wallet"); // The loaded wallet is changing — drop the old wallet's chat identity + decrypted store so it @@ -1885,15 +1910,31 @@ void App::refreshCoreData(bool allowBalance) // pending for after the unlock. const bool includeBalance = allowBalance && !effectivelySyncing() && (force_balance_refresh_ || balanceRefreshDue()); - if (includeBalance) force_balance_refresh_ = false; + // The forced request this poll carries. The result callback clears the flag only if the balance + // call was really sent and no newer request came in; clearing it here lost it whenever the enqueue + // was refused (a Core job already in flight) or the worker skipped the balance (behind, busy). + const std::uint32_t forcedSeq = (includeBalance && force_balance_refresh_) ? force_balance_seq_ : 0; + NetworkRefreshService::CoreRefreshPlan plan; + plan.includeBalance = includeBalance; + plan.needSpendable = NetworkRefreshService::spendableBalanceNeeded( + state_, !pending_send_info_.empty() || !pending_opids_.empty() || !send_txids_.empty(), + force_balance_refresh_); + plan.prevTotalZat = last_total_balance_zat_; - auto enqueued = network_refresh_.enqueue(services::NetworkRefreshService::Job::Core, *w, [this, rpc, tracePage, includeBalance]() -> rpc::RPCWorker::MainCb { + auto enqueued = network_refresh_.enqueue(services::NetworkRefreshService::Job::Core, *w, [this, rpc, tracePage, plan, forcedSeq]() -> rpc::RPCWorker::MainCb { AppRefreshRpcGateway refreshRpc(*rpc, traceSource(tracePage, "Core refresh")); - auto result = NetworkRefreshService::collectCoreRefreshResult(refreshRpc, includeBalance); - return [this, result]() { + auto result = NetworkRefreshService::collectCoreRefreshResult(refreshRpc, plan); + return [this, result, forcedSeq]() { try { NetworkRefreshService::applyCoreRefreshResult(state_, result, std::time(nullptr)); - applyPendingSendBalanceDeltas(true); + if (result.balanceOk) { + // Fresh aggregates: re-apply the pending sends' debits to them. The per-address + // balances are the address scan's to refresh and re-debit. + applyPendingSendBalanceDeltas(true, false); + } + last_total_balance_zat_ = NetworkRefreshService::nextTotalBaseline(result, last_total_balance_zat_); + if (forcedSeq != 0 && result.balanceAttempted && forcedSeq == force_balance_seq_) + force_balance_refresh_ = false; // Feed the adaptive balance throttle + sync-settle hysteresis. Record the last scan's // cost (0 when balance was skipped), and arm the settle window only on the @@ -5182,6 +5223,7 @@ void App::beginAdoptSeedWallet() { std::lock_guard lk(seed_migration_mutex_); seed_migration_adopt_done_ = false; } // Gate the main-loop reconnect so tryConnect can't start the daemon while we swap wallet.dat. daemon_restarting_ = true; + abortMaintenanceCalls(); // a long call holds the curl lock disconnect() needs if (rpc_ && rpc_->isConnected()) rpc_->disconnect(); onDisconnected("Adopting seed wallet"); // Adopting swaps in a brand-new seed wallet — drop the legacy wallet's chat identity + store so @@ -5302,6 +5344,7 @@ void App::restoreOriginalWallet() { std::lock_guard lk(wallet_restore_mutex_); wallet_restore_done_ = false; } daemon_restarting_ = true; // gate the reconnect loop while we swap files connection_status_ = TR("sb_restarting_daemon"); + abortMaintenanceCalls(); // a long call holds the curl lock disconnect() needs if (rpc_ && rpc_->isConnected()) rpc_->disconnect(); onDisconnected("Restoring original wallet"); ui::Notifications::instance().info(TR("wallet_restore_started"), 12.0f); @@ -5472,6 +5515,7 @@ void App::rebuildWalletDatabase() { std::lock_guard lk(wallet_restore_mutex_); wallet_restore_done_ = false; } daemon_restarting_ = true; connection_status_ = TR("sb_restarting_daemon"); + abortMaintenanceCalls(); // a long call holds the curl lock disconnect() needs if (rpc_ && rpc_->isConnected()) rpc_->disconnect(); onDisconnected("Rebuilding wallet database"); ui::Notifications::instance().info(TR("wallet_rebuild_started"), 15.0f); @@ -5813,7 +5857,7 @@ void App::submitZSendMany(const std::string& from, const std::string& to, double // Force transaction list refresh so the sent tx appears immediately transactions_dirty_ = true; last_tx_block_height_ = -1; - force_balance_refresh_ = true; // the user's own send must update the balance now, past the throttle + requestForcedBalanceRefresh(); // the user's own send must update the balance now, past the throttle network_refresh_.markWalletMutationRefresh(); // z_sendmany only returned an opid: the transaction is built/signed/ // broadcast asynchronously by the daemon. Defer the user-facing diff --git a/src/app_wizard.cpp b/src/app_wizard.cpp index 1a41b73..c03ebb9 100644 --- a/src/app_wizard.cpp +++ b/src/app_wizard.cpp @@ -87,6 +87,7 @@ void App::restartWizard() // Disconnect RPC if (rpc_ && rpc_->isConnected()) { + abortMaintenanceCalls(); // a long call holds the curl lock disconnect() needs rpc_->disconnect(); } onDisconnected("Wizard restart"); diff --git a/src/rpc/rpc_client.cpp b/src/rpc/rpc_client.cpp index 27bd92f..2e48f44 100644 --- a/src/rpc/rpc_client.cpp +++ b/src/rpc/rpc_client.cpp @@ -832,24 +832,6 @@ void RPCClient::z_mergeToAddress(const std::vector& fromAddrs, cons doRPC("z_mergetoaddress", {addrs, toAddr, fee, 0, limit}, cb, err); } -void RPCClient::z_getOperationStatus(const std::vector& opids, Callback cb, ErrorCallback err) -{ - json ids = json::array(); - for (const auto& id : opids) { - ids.push_back(id); - } - doRPC("z_getoperationstatus", {ids}, cb, err); -} - -void RPCClient::z_getOperationResult(const std::vector& opids, Callback cb, ErrorCallback err) -{ - json ids = json::array(); - for (const auto& id : opids) { - ids.push_back(id); - } - doRPC("z_getoperationresult", {ids}, cb, err); -} - void RPCClient::z_listReceivedByAddress(const std::string& address, int minconf, Callback cb, ErrorCallback err) { doRPC("z_listreceivedbyaddress", {address, minconf}, cb, err); @@ -902,16 +884,6 @@ void RPCClient::z_mergeToAddress(const std::vector& fromAddrs, cons doRPC("z_mergetoaddress", {addrs, toAddr, fee, 0, limit}, handlers.first, handlers.second); } -void RPCClient::z_getOperationStatus(const std::vector& opids, UnifiedCallback cb) -{ - json ids = json::array(); - for (const auto& id : opids) { - ids.push_back(id); - } - auto handlers = splitUnified(std::move(cb)); - doRPC("z_getoperationstatus", {ids}, handlers.first, handlers.second); -} - void RPCClient::getBlock(int height, UnifiedCallback cb) { // First get block hash, then get block diff --git a/src/rpc/rpc_client.h b/src/rpc/rpc_client.h index e22fc38..5e13d4e 100644 --- a/src/rpc/rpc_client.h +++ b/src/rpc/rpc_client.h @@ -257,10 +257,6 @@ public: void z_mergeToAddress(const std::vector& fromAddrs, const std::string& toAddr, double fee, int limit, Callback cb, ErrorCallback err = nullptr); - // Operation status monitoring - void z_getOperationStatus(const std::vector& opids, Callback cb, ErrorCallback err = nullptr); - void z_getOperationResult(const std::vector& opids, Callback cb, ErrorCallback err = nullptr); - // Received transactions void z_listReceivedByAddress(const std::string& address, int minconf, Callback cb, ErrorCallback err = nullptr); @@ -272,7 +268,6 @@ public: double fee, int limit, UnifiedCallback cb); void z_mergeToAddress(const std::vector& fromAddrs, const std::string& toAddr, double fee, int limit, UnifiedCallback cb); - void z_getOperationStatus(const std::vector& opids, UnifiedCallback cb); void getBlock(int height, UnifiedCallback cb); private: diff --git a/src/services/network_refresh_service.cpp b/src/services/network_refresh_service.cpp index 96a190c..be5aaf6 100644 --- a/src/services/network_refresh_service.cpp +++ b/src/services/network_refresh_service.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -271,6 +272,9 @@ NetworkRefreshService::CoreRefreshResult NetworkRefreshService::parseCoreRefresh result.shieldedBalance = readBalanceString(totalBalance, "private"); result.transparentBalance = readBalanceString(totalBalance, "transparent"); result.totalBalance = readBalanceString(totalBalance, "total"); + const auto total = totalBalance.find("total"); + if (total != totalBalance.end() && total->is_string()) + result.totalBalanceZat = parseMoneyZat(total->get()); } if (spendableBalance.is_object()) { // confirmed totals (minconf=1); left unset on old daemons result.spendableShieldedBalance = readBalanceString(spendableBalance, "private"); @@ -290,7 +294,8 @@ NetworkRefreshService::CoreRefreshResult NetworkRefreshService::parseCoreRefresh return result; } -NetworkRefreshService::CoreRefreshResult NetworkRefreshService::collectCoreRefreshResult(RefreshRpcGateway& rpc, bool includeBalance) +NetworkRefreshService::CoreRefreshResult NetworkRefreshService::collectCoreRefreshResult(RefreshRpcGateway& rpc, + const CoreRefreshPlan& plan) { json totalBalance; json spendableBalance; @@ -328,7 +333,9 @@ NetworkRefreshService::CoreRefreshResult NetworkRefreshService::collectCoreRefre // Don't follow a chain-info call that found the node busy (a reply timeout or a full queue) with two // balance scans: they need the same cs_main, and each would only take another of its few RPC slots. - if (includeBalance && !behind && !rpc::rpcKindMeansBusy(blockErrKind)) { + bool balanceAttempted = false; + bool spendableQueried = false; + if (plan.includeBalance && !behind && !rpc::rpcKindMeansBusy(blockErrKind)) { // z_gettotalbalance is O(mapWallet) and holds the daemon's cs_main for its whole duration — // seconds on a large shielded wallet. Time it so the caller can throttle how often it polls // (balanceRefreshDue()), keeping balance scans from starving block connection. @@ -340,9 +347,25 @@ NetworkRefreshService::CoreRefreshResult NetworkRefreshService::collectCoreRefre balanceErrKind = rpc::rpcFailureKindOf(e); DEBUG_LOGF("Balance error: %s\n", e.what()); } - try { // SPENDABLE total: minconf=1 (confirmed). If absent, spendable degrades to display in apply. - spendableBalance = rpc.call("z_gettotalbalance", json::array({1})); - } catch (...) {} + // A Deferred call was held back by the busy gate and never reached the daemon. + balanceAttempted = balanceErrKind != rpc::RpcFailureKind::Deferred; + // SPENDABLE total: minconf=1 (confirmed), a second whole-wallet scan. Only after the display call + // succeeded (its result is dropped otherwise), and only when it can differ from the display total: + // something is unconfirmed or pending (needSpendable), or the exact total moved since the last + // poll, which is how a new incoming 0-conf payment first shows. Skipped, spendable equals display + // in applyCoreRefreshResult and nothing reads as unconfirmed. + if (balanceOk) { + std::optional totalZat; + const auto total = totalBalance.is_object() ? totalBalance.find("total") : totalBalance.end(); + if (total != totalBalance.end() && total->is_string()) totalZat = parseMoneyZat(total->get()); + const bool totalMoved = !totalZat || !plan.prevTotalZat || *totalZat != *plan.prevTotalZat; + if (plan.needSpendable || totalMoved) { + spendableQueried = true; + try { // If it fails, spendable degrades to display in apply. + spendableBalance = rpc.call("z_gettotalbalance", json::array({1})); + } catch (...) {} + } + } balanceScanMs = std::chrono::duration( std::chrono::steady_clock::now() - balanceStart).count(); } @@ -351,9 +374,65 @@ NetworkRefreshService::CoreRefreshResult NetworkRefreshService::collectCoreRefre result.balanceScanMs = balanceScanMs; result.blockchainErrKind = blockErrKind; result.balanceErrKind = balanceErrKind; + result.balanceAttempted = balanceAttempted; + result.spendableQueried = spendableQueried; return result; } +std::optional NetworkRefreshService::parseMoneyZat(const std::string& text) +{ + constexpr std::int64_t kCoin = 100000000; + // Leaves room for up to 99999999 zatoshis of fraction on top of whole * 1e8. + constexpr std::int64_t kMaxWhole = std::numeric_limits::max() / kCoin - 1; + std::size_t i = 0; + const bool negative = !text.empty() && text[0] == '-'; + if (negative) ++i; + std::int64_t whole = 0; + std::size_t wholeDigits = 0; + for (; i < text.size() && text[i] >= '0' && text[i] <= '9'; ++i, ++wholeDigits) { + const int digit = text[i] - '0'; + if (whole > (kMaxWhole - digit) / 10) return std::nullopt; // the amount would overflow + whole = whole * 10 + digit; + } + if (wholeDigits == 0) return std::nullopt; + std::int64_t fraction = 0; + if (i < text.size() && text[i] == '.') { + ++i; + std::size_t fractionDigits = 0; + for (; i < text.size() && text[i] >= '0' && text[i] <= '9'; ++i, ++fractionDigits) { + if (fractionDigits == 8) return std::nullopt; // finer than a zatoshi: not a daemon amount + fraction = fraction * 10 + (text[i] - '0'); + } + if (fractionDigits == 0) return std::nullopt; + for (; fractionDigits < 8; ++fractionDigits) fraction *= 10; + } + if (i != text.size()) return std::nullopt; + const std::int64_t zat = whole * kCoin + fraction; + return negative ? -zat : zat; +} + +std::optional NetworkRefreshService::nextTotalBaseline(const CoreRefreshResult& result, + std::optional previous) +{ + if (!result.balanceOk) return previous; // no fresh total + if (result.spendableQueried && !result.spendableTotalBalance) return std::nullopt; // re-measure next poll + return result.totalBalanceZat; +} + +bool NetworkRefreshService::spendableBalanceNeeded(const WalletState& state, bool pendingSendWork, bool forced) +{ + if (forced || pendingSendWork) return true; + if (state.last_balance_update == 0) return true; + if (state.unconfirmedBalance > 1e-9) return true; + // The address scan saw unconfirmed funds (z_listunspent/listunspent at minconf=0 with confirmations). + auto anyUnconfirmed = [](const std::vector& addresses) { + return std::any_of(addresses.begin(), addresses.end(), [](const AddressInfo& address) { + return address.balance - address.spendableBalance > 1e-9; + }); + }; + return anyUnconfirmed(state.z_addresses) || anyUnconfirmed(state.t_addresses); +} + NetworkRefreshService::MiningRefreshResult NetworkRefreshService::parseMiningRefreshResult( const json& miningInfo, bool miningOk, const json& localHashrate, bool hashrateOk, double daemonMemoryMb) { @@ -1327,9 +1406,10 @@ NetworkRefreshService::OperationStatusPollResult NetworkRefreshService::parseOpe OperationStatusPollResult parsed; if (!result.is_array()) return parsed; - // We poll z_getoperationstatus with no filter (this daemon rejects the per-opid filtered form), - // so the result lists ALL operations. Only act on the ones we're tracking — otherwise an - // unrelated failed/old operation would fire a spurious error toast or pollute send state. + // The poll is filtered to our opids (buildOperationFilterParams), but falls back to the unfiltered + // form, which lists ALL operations, if a daemon rejects the filter. Only act on the ones we're + // tracking — otherwise an unrelated failed/old operation would fire a spurious error toast or + // pollute send state. const std::set requested(requestedOpids.begin(), requestedOpids.end()); std::set reported; @@ -1379,6 +1459,18 @@ NetworkRefreshService::OperationStatusPollResult NetworkRefreshService::parseOpe return parsed; } +std::optional NetworkRefreshService::buildOperationFilterParams(const std::vector& opids) +{ + json ids = json::array(); + for (const auto& opid : opids) { + if (!opid.empty()) ids.push_back(opid); + } + if (ids.empty()) return std::nullopt; + json params = json::array(); + params.push_back(std::move(ids)); + return params; +} + void NetworkRefreshService::applyConnectionInfoResult(WalletState& state, const ConnectionInfoResult& result) { if (!result.ok) return; diff --git a/src/services/network_refresh_service.h b/src/services/network_refresh_service.h index ce918a3..2e5a885 100644 --- a/src/services/network_refresh_service.h +++ b/src/services/network_refresh_service.h @@ -120,6 +120,20 @@ public: // node (ReplyTimeout / QueueFull) from a gone one (NotListening) without reading what(). rpc::RpcFailureKind blockchainErrKind = rpc::RpcFailureKind::None; rpc::RpcFailureKind balanceErrKind = rpc::RpcFailureKind::None; + // The minconf=0 call reached the daemon (it may still have failed). False when the balance was + // skipped (behind, busy chain read) or held back by the busy gate (Deferred) before it was sent. + bool balanceAttempted = false; + bool spendableQueried = false; // the minconf=1 call was made + std::optional totalBalanceZat; // the minconf=0 "total" in zatoshis, exact + }; + + // What one Core refresh fetches. The minconf=1 (spendable) z_gettotalbalance is a second whole-wallet + // scan under cs_main; it only tells something the minconf=0 call doesn't while funds are unconfirmed + // or pending, so it runs only when needSpendable or the exact total moved since prevTotalZat. + struct CoreRefreshPlan { + bool includeBalance = true; + bool needSpendable = true; + std::optional prevTotalZat; }; struct MiningRefreshResult { @@ -271,7 +285,24 @@ public: // includeBalance=false skips z_gettotalbalance (which takes the wallet lock + cs_main) and only // fetches getblockchaininfo — used while syncing, where the balance is incomplete anyway and the // wallet should minimise lock contention with block connection. - static CoreRefreshResult collectCoreRefreshResult(RefreshRpcGateway& rpc, bool includeBalance = true); + static CoreRefreshResult collectCoreRefreshResult(RefreshRpcGateway& rpc, const CoreRefreshPlan& plan); + static CoreRefreshResult collectCoreRefreshResult(RefreshRpcGateway& rpc, bool includeBalance = true) + { + CoreRefreshPlan plan; + plan.includeBalance = includeBalance; + return collectCoreRefreshResult(rpc, plan); + } + // An amount as the daemon formats it ("[-]digits[.up to 8 digits]") in zatoshis; nullopt for anything + // else or anything that would overflow. Exact, so "did the total move" never trips on rounding. + static std::optional parseMoneyZat(const std::string& text); + // Whether the next balance poll needs the minconf=1 (spendable) call whatever the total does: never + // fetched, unconfirmed or pending funds, or a forced refresh after the user's own send. + static bool spendableBalanceNeeded(const WalletState& state, bool pendingSendWork, bool forced); + // The total the next poll compares against (CoreRefreshPlan::prevTotalZat). None after a poll whose + // minconf=1 call was made but brought back no total (timeout, held back, error): that left spendable + // degraded to display, so the next poll must count the total as moved and measure it again. + static std::optional nextTotalBaseline(const CoreRefreshResult& result, + std::optional previous); static MiningRefreshResult parseMiningRefreshResult(const nlohmann::json& miningInfo, bool miningOk, const nlohmann::json& localHashrate, @@ -340,6 +371,11 @@ public: int pageSize = 100); static OperationStatusPollResult parseOperationStatusPoll(const nlohmann::json& result, const std::vector& requestedOpids); + // Params for z_getoperationstatus/result filtered to these opids: ONE argument, an array of ids, so + // [[id, ...]]. nullopt when no non-empty id remains: an empty filter means every op the node has + // retained. Built explicitly, because a one-element braced list of a json ({ids}) collapses to the + // inner array, which the daemon rejects ("JSON value is not an array as expected"). + static std::optional buildOperationFilterParams(const std::vector& opids); struct DaemonRescanScan { bool foundRescan = false; diff --git a/src/services/note_consolidation.cpp b/src/services/note_consolidation.cpp index 811f2cb..6a6caa3 100644 --- a/src/services/note_consolidation.cpp +++ b/src/services/note_consolidation.cpp @@ -5,6 +5,7 @@ #include "note_consolidation.h" #include +#include #include namespace dragonx { @@ -20,6 +21,7 @@ std::vector groupSpendableNotes(const nlohmann::json& zListUnspent) if (address.empty()) continue; if (n.value("confirmations", 0) < 1) continue; // z_mergetoaddress spends confirmed notes only if (!n.value("spendable", true)) continue; // watch-only: no key to spend it + if (noteIsLocked(n)) continue; // held by an operation in flight NoteGroup& g = byAddress[address]; g.address = address; ++g.notes; @@ -36,6 +38,13 @@ std::vector groupSpendableNotes(const nlohmann::json& zListUnspent) return out; } +bool noteIsLocked(const nlohmann::json& zListUnspentEntry) +{ + if (!zListUnspentEntry.is_object()) return false; + const auto it = zListUnspentEntry.find("locked"); + return it != zListUnspentEntry.end() && it->is_boolean() && it->get(); +} + bool worthConsolidating(const NoteGroup& group, double fee) { // The daemon rejects a merge whose fee is more than what it nets (merged value < 2 x fee). @@ -67,5 +76,107 @@ AddressRun consolidateInPlace(const std::string& address, const MergeCall& call, return run; } +ScopeWant scopeScanWant(const ScopeScan& scan, const ScopeInputs& in) +{ + if (!in.mergeMode || scan.phase == ScopePhase::Loaded) return ScopeWant::Nothing; + if (scan.inFlight || in.busyWithOp) return ScopeWant::Nothing; + if (scan.phase == ScopePhase::Failed && !scan.autoRetry) return ScopeWant::Nothing; // waits for Retry + if (in.now < scan.retryAt) return ScopeWant::WaitBackoff; + if (!in.nodeReady) return ScopeWant::WaitNode; + return ScopeWant::Post; +} + +std::uint32_t scopeScanBegin(ScopeScan& scan, double now) +{ + scan.phase = ScopePhase::Loading; + scan.inFlight = true; + scan.inFlightGen = scan.gen; + scan.startedAt = now; + return scan.gen; +} + +bool scopeScanFinish(ScopeScan& scan, std::uint32_t gen, const std::string& error, rpc::RpcFailureKind kind, + double now) +{ + if (scan.inFlight && gen == scan.inFlightGen) scan.inFlight = false; + if (gen != scan.gen) return false; // a scan from an earlier session: its data is not this one's + if (error.empty()) { + scan.phase = ScopePhase::Loaded; + scan.failures = 0; + scan.autoRetry = false; + scan.retryAt = 0.0; + scan.error.clear(); + return true; + } + scan.phase = ScopePhase::Failed; + ++scan.failures; + scan.error = error; + scan.autoRetry = scopeFailureRetries(kind, error) && scan.failures <= kScopeMaxAutoRetries; + scan.retryAt = scan.autoRetry ? now + scopeRetryDelaySec(scan.failures) : 0.0; + return false; +} + +void scopeScanOpen(ScopeScan& scan, double now) +{ + if (scan.inFlight && now - scan.startedAt > kScopeInFlightMaxSec) scan.inFlight = false; // lost + if (scan.inFlight && scan.phase == ScopePhase::Loading) return; // still loading for the last session + ++scan.gen; + scan.phase = ScopePhase::Idle; + scan.failures = 0; + scan.autoRetry = false; + scan.retryAt = 0.0; + scan.error.clear(); +} + +void scopeScanInvalidate(ScopeScan& scan) +{ + ++scan.gen; + scan.phase = ScopePhase::Idle; + scan.failures = 0; + scan.autoRetry = false; + scan.retryAt = 0.0; + scan.error.clear(); +} + +void scopeScanRetry(ScopeScan& scan) +{ + if (scan.phase != ScopePhase::Failed) return; + scan.phase = ScopePhase::Idle; + scan.failures = 0; + scan.autoRetry = false; + scan.retryAt = 0.0; + scan.error.clear(); +} + +double scopeRetryDelaySec(int failures) +{ + if (failures <= 1) return 10.0; + if (failures == 2) return 30.0; + return 60.0; +} + +bool scopeFailureRetries(rpc::RpcFailureKind kind, const std::string& message) +{ + using K = rpc::RpcFailureKind; + switch (kind) { + case K::NotListening: + case K::ConnectTimeout: + case K::Warmup: + case K::Deferred: + case K::ShuttingDown: + case K::Unhandled: + case K::Aborted: + return true; + case K::Other: { + std::string lower = message; + std::transform(lower.begin(), lower.end(), lower.begin(), + [](unsigned char c) { return static_cast(std::tolower(c)); }); + return lower.find("not connected") != std::string::npos; + } + default: + return false; // ReplyTimeout, QueueFull, AppError, Auth, parse/type errors + } +} + } // namespace services } // namespace dragonx diff --git a/src/services/note_consolidation.h b/src/services/note_consolidation.h index bae82df..06b6a49 100644 --- a/src/services/note_consolidation.h +++ b/src/services/note_consolidation.h @@ -10,8 +10,12 @@ // Shielded only: a transparent address can't receive a non-coinbase output on DragonX (private chain). // Pure apart from the merge call it is handed, so the test target can drive it. +#include "rpc/rpc_failure.h" + #include +#include +#include #include #include #include @@ -26,10 +30,14 @@ struct NoteGroup { double amount = 0.0; }; -// Spendable shielded notes grouped by address, most notes first. Transparent UTXOs and unconfirmed or -// watch-only notes are left out (z_mergetoaddress only spends confirmed notes it has keys for). +// Spendable shielded notes grouped by address, most notes first. Transparent UTXOs and unconfirmed, +// watch-only or locked notes are left out (z_mergetoaddress only spends confirmed, unlocked notes it has +// keys for). std::vector groupSpendableNotes(const nlohmann::json& zListUnspent); +// A z_listunspent entry the wallet has locked, e.g. for an operation in flight ("locked": true). +bool noteIsLocked(const nlohmann::json& zListUnspentEntry); + // Worth consolidating in place: at least two notes, and more than one batch's fee to work with. bool worthConsolidating(const NoteGroup& group, double fee); @@ -59,5 +67,59 @@ struct AddressRun { // locks a batch's notes before the call returns, so each call takes the next batch. At most maxBatches. AddressRun consolidateInPlace(const std::string& address, const MergeCall& call, int maxBatches = 20); +// ── Scope scan (the Merge/Consolidate dialog's listunspent + z_listunspent) ───────────────────────── +// Both calls are whole-wallet scans under the daemon's cs_main. The dialog used to post the pair again on +// the frame after any failure, every frame for as long as it stayed open (and forever on Lite, which has +// no node). This decides when a scan may be posted; the dialog owns one ScopeScan. + +inline double steadySeconds() // ImGui::GetTime() stands still while the window is minimized +{ + return std::chrono::duration(std::chrono::steady_clock::now().time_since_epoch()).count(); +} + +inline constexpr long kScopeCallTimeoutSec = 120; // each call; a large wallet can take minutes +inline constexpr double kScopeInFlightMaxSec = 1800; // a scan outstanding this long is presumed lost +inline constexpr int kScopeMaxAutoRetries = 3; + +enum class ScopePhase { Idle, Loading, Loaded, Failed }; + +struct ScopeScan { + ScopePhase phase = ScopePhase::Idle; + std::uint32_t gen = 0; // bumped per dialog session; a result from another session is dropped + bool inFlight = false; // a scan is posted and its result not yet in (survives close/reopen) + std::uint32_t inFlightGen = 0; // the session that posted it: only its result clears inFlight + double startedAt = 0.0; + int failures = 0; + bool autoRetry = false; // the last failure was "node not ready yet": retry by itself + double retryAt = 0.0; + std::string error; +}; + +struct ScopeInputs { + bool mergeMode = false; + bool busyWithOp = false; // a merge was submitted or is in flight + bool nodeReady = false; // App::walletScanReady() + double now = 0.0; +}; + +enum class ScopeWant { Nothing, Post, WaitNode, WaitBackoff }; + +ScopeWant scopeScanWant(const ScopeScan& scan, const ScopeInputs& in); +// Mark a scan posted: Loading, in flight for this session. Returns the generation to finish it with. +std::uint32_t scopeScanBegin(ScopeScan& scan, double now); +// Record a scan's result. True when it belongs to the current session and succeeded: apply its data. +bool scopeScanFinish(ScopeScan& scan, std::uint32_t gen, const std::string& error, rpc::RpcFailureKind kind, + double now); +// The dialog opened: start a new session, unless a scan of the last one is still loading (adopt it). +void scopeScanOpen(ScopeScan& scan, double now); +// The data went stale (reconnect): the next want is a fresh scan. An in-flight scan stays guarded. +void scopeScanInvalidate(ScopeScan& scan); +// The user pressed Retry after a failure. +void scopeScanRetry(ScopeScan& scan); +double scopeRetryDelaySec(int failures); // 10, 30, then 60 s +// Retry by itself only when the node wasn't ready (refused, warming up, held back as busy, aborted): +// not after a reply timeout or a real error, each of which would cost another pair of full scans. +bool scopeFailureRetries(rpc::RpcFailureKind kind, const std::string& message); + } // namespace services } // namespace dragonx diff --git a/src/ui/pages/settings_page.cpp b/src/ui/pages/settings_page.cpp index 96e7a13..a55ea52 100644 --- a/src/ui/pages/settings_page.cpp +++ b/src/ui/pages/settings_page.cpp @@ -1293,12 +1293,15 @@ void RenderSettingsPage(App* app) { if (BTN("##wreq", TR("settings_request_payment"), ICON_MD_QR_CODE)) RequestPaymentDialog::show(); if (ImGui::IsItemHovered()) material::Tooltip("%s", TR("tt_request_payment")); - if (BTN("##wshield", TR("settings_shield_mining"), ICON_MD_SHIELD)) - ShieldDialog::show(ShieldDialog::Mode::ShieldCoinbase); - if (ImGui::IsItemHovered()) material::Tooltip("%s", TR("tt_shield_mining")); - if (BTN("##wmerge", TR("settings_merge_to_address"), ICON_MD_CALL_MERGE)) - ShieldDialog::showMerge(); - if (ImGui::IsItemHovered()) material::Tooltip("%s", TR("tt_merge")); + // z_shieldcoinbase / z_mergetoaddress are full-node RPCs: Lite has no node to send them to. + if (app->supportsFullNodeLifecycleActions()) { + if (BTN("##wshield", TR("settings_shield_mining"), ICON_MD_SHIELD)) + ShieldDialog::show(ShieldDialog::Mode::ShieldCoinbase); + if (ImGui::IsItemHovered()) material::Tooltip("%s", TR("tt_shield_mining")); + if (BTN("##wmerge", TR("settings_merge_to_address"), ICON_MD_CALL_MERGE)) + ShieldDialog::showMerge(); + if (ImGui::IsItemHovered()) material::Tooltip("%s", TR("tt_merge")); + } if (BTN("##wclear", TR("settings_clear_ztx"), ICON_MD_DELETE_SWEEP)) s_settingsState.confirm_clear_ztx = true; if (ImGui::IsItemHovered()) material::Tooltip("%s", TR("tt_clear_ztx")); diff --git a/src/ui/windows/shield_dialog.cpp b/src/ui/windows/shield_dialog.cpp index c9f5124..e09d7d8 100644 --- a/src/ui/windows/shield_dialog.cpp +++ b/src/ui/windows/shield_dialog.cpp @@ -16,7 +16,9 @@ #include "imgui.h" #include +#include #include +#include #include #include #include @@ -41,9 +43,8 @@ static bool s_op_terminal = false; // async op reached success/fa static std::string s_operation_id; static std::string s_status_message; static double s_last_poll = 0.0; // live-progress self-poll timer (ImGui::GetTime seconds) -// Scope of what can be consolidated (fetched once on open, merge mode only). -static bool s_scope_loading = false; -static bool s_scope_loaded = false; +// Scope of what can be consolidated (merge mode only). services::ScopeScan decides when to scan. +static services::ScopeScan s_scan; static int s_t_count = 0, s_z_count = 0; static double s_t_amount = 0.0, s_z_amount = 0.0; static bool s_creating_addr = false; // z_getnewaddress in flight (empty state) @@ -52,16 +53,22 @@ static bool s_creating_addr = false; // z_getnewaddress in flight ( static bool s_per_address = false; static std::vector s_groups; // spendable shielded notes by address (scope load) static std::set s_selected; // addresses ticked for per-address consolidation +static std::set s_unticked; // unticked by the user this opening: kept across a re-scan struct PerAddressRun { services::AddressRun run; - std::vector state; // per opid: 0 = running, 1 = done, 2 = failed + std::vector state; // per opid: 0 = running, 1 = done, 2 = failed, 3 = gone from the daemon (unknown) std::string failure; // first failed op's message }; static std::vector s_runs; // after submit static bool s_runs_submitting = false; +// The op-status polls: one in flight at a time, cleared only by its reply, even across a close and +// reopen; a reply for an earlier opening (another session) changes nothing else. +static bool s_poll_in_flight = false; +static unsigned s_poll_session = 0; static void resetTransient() { + ++s_poll_session; s_operation_pending = false; s_op_terminal = false; s_confirm = false; @@ -73,33 +80,47 @@ static void resetTransient() } // Count + sum spendable transparent UTXOs and shielded notes so the user can see the scope of a -// consolidation (and how many batches it may take). Read-only; runs off the UI thread. +// consolidation (and how many batches it may take). Read-only; runs off the UI thread. Only when +// services::scopeScanWant() says so: both calls are whole-wallet scans under the daemon's cs_main. static void loadScope(App* app) { if (!app || !app->worker()) return; - s_scope_loading = true; s_scope_loaded = false; - app->worker()->post([app, rpc = app->rpc()]() -> rpc::RPCWorker::MainCb { + const std::uint32_t gen = services::scopeScanBegin(s_scan, services::steadySeconds()); + app->worker()->post([app, rpc = app->rpc(), gen]() -> rpc::RPCWorker::MainCb { int tC = 0, zC = 0; double tA = 0.0, zA = 0.0; std::string error; + rpc::RpcFailureKind kind = rpc::RpcFailureKind::None; std::vector groups; try { + if (!rpc) throw std::runtime_error("Not connected"); + // On a large wallet each scan can outlast the default 30 s; give them 120 s, counted as + // maintenance calls so a disconnect aborts them rather than waiting. + App::MaintenanceCallScope maint(*app); rpc::RPCClient::TraceScope trace("Shield dialog / Scope count"); - nlohmann::json us = rpc->call("listunspent", nlohmann::json::array({0})); + nlohmann::json us = rpc->call("listunspent", nlohmann::json::array({0}), services::kScopeCallTimeoutSec); if (us.is_array()) for (const auto& u : us) { if (u.value("confirmations", 0) >= 1 && u.value("spendable", true)) { ++tC; tA += u.value("amount", 0.0); } } - nlohmann::json zs = rpc->call("z_listunspent", nlohmann::json::array({0})); + nlohmann::json zs = rpc->call("z_listunspent", nlohmann::json::array({0}), services::kScopeCallTimeoutSec); if (zs.is_array()) for (const auto& z : zs) { - if (z.value("confirmations", 0) >= 1) { ++zC; zA += z.value("amount", 0.0); } + if (z.value("confirmations", 0) >= 1 && !services::noteIsLocked(z)) { ++zC; zA += z.value("amount", 0.0); } } groups = services::groupSpendableNotes(zs); - } catch (const std::exception& e) { error = e.what(); } - return [tC, zC, tA, zA, error, groups]() { - s_scope_loading = false; s_scope_loaded = error.empty(); + } catch (const std::exception& e) { + error = e.what(); + kind = rpc::rpcFailureKindOf(e); + if (error.empty()) error = "error"; + } catch (...) { + error = "unknown error"; + kind = rpc::RpcFailureKind::Other; + } + return [tC, zC, tA, zA, error, kind, gen, groups]() { + if (!services::scopeScanFinish(s_scan, gen, error, kind, services::steadySeconds())) return; s_t_count = tC; s_z_count = zC; s_t_amount = tA; s_z_amount = zA; s_groups = groups; - s_selected.clear(); // every address worth it, ticked by default + s_selected.clear(); // every address worth it, ticked by default, except ones the user unticked for (const auto& g : s_groups) - if (services::worthConsolidating(g, s_fee)) s_selected.insert(g.address); + if (services::worthConsolidating(g, s_fee) && !s_unticked.count(g.address)) + s_selected.insert(g.address); // Clamp the source to what actually has inputs (unless the user is mid-op). const bool tOk = tC > 0, zOk = zC > 0; if (!s_operation_pending) { @@ -128,12 +149,13 @@ void ShieldDialog::show(Mode mode) s_utxo_limit = (int)schema::UI().drawElement("business", "utxo-limit").size; if (s_utxo_limit < 1) s_utxo_limit = 50; s_advanced = false; - s_scope_loaded = false; s_scope_loading = false; + services::scopeScanOpen(s_scan, services::steadySeconds()); s_t_count = s_z_count = 0; s_t_amount = s_z_amount = 0.0; s_last_poll = 0.0; s_per_address = false; s_groups.clear(); s_selected.clear(); + s_unticked.clear(); } void ShieldDialog::showShieldCoinbase(const std::string& fromAddress) @@ -163,6 +185,15 @@ void ShieldDialog::hide() resetTransient(); } +void ShieldDialog::invalidateScope() +{ + s_confirm = false; // what the user was confirming was read on the old connection + services::scopeScanInvalidate(s_scan); + s_t_count = s_z_count = 0; s_t_amount = s_z_amount = 0.0; + s_groups.clear(); + s_selected.clear(); +} + // Relevant count/amount for the currently-selected merge source. static int srcCount() { return s_src == 0 ? s_t_count : s_src == 1 ? s_z_count : (s_t_count + s_z_count); } static double srcAmount() { return s_src == 0 ? s_t_amount : s_src == 1 ? s_z_amount : (s_t_amount + s_z_amount); } @@ -241,17 +272,34 @@ static void submitOperation(App* app) nlohmann::json addrs = nlohmann::json::array(); for (const auto& a : fromAddrs) addrs.push_back(a); nlohmann::json result; std::string error; + bool uncertain = false; try { + if (!rpc) throw std::runtime_error("Not connected"); + // The daemon scans the whole wallet before it returns the opid; on a large wallet that can + // outlast 30 s, which reported a failure for a merge that had started. 120 s, as a maintenance + // call so a disconnect aborts it instead of waiting. + App::MaintenanceCallScope maint(*app); rpc::RPCClient::TraceScope trace("Shield dialog / Consolidate"); // fromaddrs, toaddr, fee, transparent_limit, shielded_limit — cap both to the batch size. - result = rpc->call("z_mergetoaddress", {addrs, to, fee, limit, limit}); - } catch (const std::exception& e) { error = e.what(); } - return [app, result, error]() { + result = rpc->call("z_mergetoaddress", {addrs, to, fee, limit, limit}, services::kScopeCallTimeoutSec); + } catch (const std::exception& e) { + error = e.what(); + // Sent, then cut off with no reply: our own abort on a lifecycle disconnect, the reply timeout + // or a dropped connection. The daemon keeps running the handler and may still queue the merge. + if (const auto* t = dynamic_cast(&e)) + uncertain = t->requestSent && t->httpStatus == 0; + } + return [app, result, error, uncertain]() { if (error.empty()) { s_operation_id = result.value("opid", ""); s_status_message = TR("merge_progress"); Notifications::instance().success(TR("merge_started")); app->trackOperation(s_operation_id); + } else if (uncertain) { + s_operation_pending = false; s_op_terminal = true; + s_confirm = false; // don't leave Confirm armed for a merge that may still be running + s_status_message = TR("merge_submit_uncertain"); + Notifications::instance().warning(TR("merge_submit_uncertain")); } else { s_operation_pending = false; s_op_terminal = true; s_status_message = std::string(TR("shield_error_prefix")) + error; @@ -325,86 +373,142 @@ static void submitPerAddress(App* app) }); } -// Progress of a per-address run: poll every ~2s until each batch's operation is done or failed. Unfiltered, -// like the shared poller (some daemons reject the per-opid form); only our operations are read. +// Progress of a per-address run: poll every ~2s until each batch's operation is done or failed. Filtered +// to the batches still running, as the shared poller does (unfiltered if the daemon rejected the filter). static void pollPerAddress(App* app) { - if (s_runs.empty() || s_op_terminal || !app->worker()) return; + if (s_runs.empty() || s_op_terminal || s_poll_in_flight || !app->worker()) return; const double now = ImGui::GetTime(); if (now - s_last_poll < 2.0) return; + std::vector running; + for (const auto& r : s_runs) + for (size_t i = 0; i < r.run.opids.size(); ++i) + if (r.state[i] == 0) running.push_back(r.run.opids[i]); + const auto filter = services::NetworkRefreshService::buildOperationFilterParams(running); + if (!filter) return; s_last_poll = now; - app->worker()->post([rpc = app->rpc()]() -> rpc::RPCWorker::MainCb { + s_poll_in_flight = true; + const unsigned session = s_poll_session; + nlohmann::json params = app->opidFilterUnsupported() ? nlohmann::json::array() : *filter; + app->worker()->post([rpc = app->rpc(), params = std::move(params), session]() -> rpc::RPCWorker::MainCb { nlohmann::json result; std::string error; try { + if (!rpc) throw std::runtime_error("Not connected"); rpc::RPCClient::TraceScope trace("Shield dialog / Consolidate in place status"); - result = rpc->call("z_getoperationstatus", nlohmann::json::array()); + result = rpc->call("z_getoperationstatus", params); } catch (const std::exception& e) { error = e.what(); } - return [result, error]() { + return [result, error, session]() { + s_poll_in_flight = false; + if (session != s_poll_session) return; // a reply for an earlier opening if (!error.empty() || !result.is_array()) return; // transient — retry next tick + std::set reported; for (const auto& op : result) { if (!op.is_object() || !op.contains("id") || !op["id"].is_string()) continue; const std::string id = op["id"].get(); + reported.insert(id); const std::string status = (op.contains("status") && op["status"].is_string()) ? op["status"].get() : std::string(); for (auto& r : s_runs) { for (size_t i = 0; i < r.run.opids.size(); ++i) { if (r.run.opids[i] != id || r.state[i] != 0) continue; if (status == "success") r.state[i] = 1; - else if (status == "failed" || status == "cancelled") { + else if (status == "failed" || status == "cancelled" || status == "canceled") { r.state[i] = 2; - if (r.failure.empty() && op.contains("error") && op["error"].is_object()) - r.failure = op["error"].value("message", std::string()); + if (r.failure.empty() && op.contains("error") && op["error"].is_object() && + op["error"].contains("message") && op["error"]["message"].is_string()) + r.failure = op["error"]["message"].get(); } } } } + // A valid reply without one of our running ops: the daemon no longer holds it (restart, or + // popped by z_getoperationresult); its outcome can't be confirmed. One poll at a time, and + // states change only here, so the running set now is the set that was asked for. + for (auto& r : s_runs) + for (size_t i = 0; i < r.run.opids.size(); ++i) + if (r.state[i] == 0 && !reported.count(r.run.opids[i])) r.state[i] = 3; bool allDone = true; - int failed = 0; + int failed = 0, unknown = 0; for (const auto& r : s_runs) - for (int st : r.state) { if (st == 0) allDone = false; if (st == 2) ++failed; } + for (int st : r.state) { if (st == 0) allDone = false; if (st == 2) ++failed; if (st == 3) ++unknown; } if (!allDone) return; s_operation_pending = false; s_op_terminal = true; - if (failed == 0) { - s_status_message = TR("shield_completed"); - Notifications::instance().success(TR("shield_merge_done")); - } else { + if (failed > 0) { s_status_message = TR("merge_keep_each_some_failed"); Notifications::instance().error(TR("merge_keep_each_some_failed")); + } else if (unknown > 0) { + s_status_message = TR("send_status_unconfirmed"); + Notifications::instance().warning(TR("send_status_unconfirmed")); + } else { + s_status_message = TR("shield_completed"); + Notifications::instance().success(TR("shield_merge_done")); } }; }); } // Live-progress self-poll: while an op is in flight, poll z_getoperationstatus every ~2s so the modal -// shows "Consolidating… → Done/Failed" without a manual button. (The shared poller also tracks it for -// balance refresh; this drives only the inline display.) +// shows "Consolidating… → Done/Failed" without a manual button. (The shared poller also tracks it: it +// refreshes balances and raises the failure toast; this drives the inline display and the success toast.) static void pollOperation(App* app) { - if (s_operation_id.empty() || s_op_terminal || !app->worker()) return; + if (s_operation_id.empty() || s_op_terminal || s_poll_in_flight || !app->worker()) return; const double now = ImGui::GetTime(); if (now - s_last_poll < 2.0) return; + const std::string opid = s_operation_id; + // [[opid]]: the argument is an ARRAY of ids. The old {json::array({opid})} collapsed to ["opid"] + // (C++17 brace init of a json from one json copies it), which the daemon rejects, so this poll failed + // every time and the dialog never saw its op finish. + const auto filter = services::NetworkRefreshService::buildOperationFilterParams({opid}); + if (!filter) return; s_last_poll = now; - std::string opid = s_operation_id; - app->worker()->post([rpc = app->rpc(), opid]() -> rpc::RPCWorker::MainCb { + s_poll_in_flight = true; + const unsigned session = s_poll_session; + nlohmann::json params = app->opidFilterUnsupported() ? nlohmann::json::array() : *filter; + app->worker()->post([rpc = app->rpc(), params = std::move(params), opid, session]() -> rpc::RPCWorker::MainCb { nlohmann::json result; std::string error; try { + if (!rpc) throw std::runtime_error("Not connected"); rpc::RPCClient::TraceScope trace("Shield dialog / Op status"); - result = rpc->call("z_getoperationstatus", {nlohmann::json::array({opid})}); + result = rpc->call("z_getoperationstatus", params); } catch (const std::exception& e) { error = e.what(); } - return [result, error]() { - if (!error.empty() || !result.is_array() || result.empty()) return; // transient — retry next tick - const auto& op = result[0]; - const std::string status = op.value("status", ""); + return [result, error, opid, session]() { + s_poll_in_flight = false; + if (session != s_poll_session || opid != s_operation_id) return; // the dialog moved on + if (!error.empty() || !result.is_array()) return; // transient — retry next tick + const nlohmann::json* found = nullptr; + for (const auto& o : result) { + if (o.is_object() && o.contains("id") && o["id"].is_string() && o["id"].get() == opid) { + found = &o; + break; + } + } + if (!found) { + // A valid reply without our op: the daemon no longer holds it (it restarted, or another + // client popped it with z_getoperationresult). It registers an op before returning its id, + // so this is no race. The outcome can't be confirmed; stop polling. + s_operation_pending = false; s_op_terminal = true; + s_status_message = TR("send_status_unconfirmed"); + Notifications::instance().warning(TR("send_status_unconfirmed")); + return; + } + const nlohmann::json& op = *found; + const std::string status = (op.contains("status") && op["status"].is_string()) + ? op["status"].get() : std::string(); if (status == "success") { s_operation_pending = false; s_op_terminal = true; s_status_message = TR("shield_completed"); Notifications::instance().success(TR("shield_merge_done")); - } else if (status == "failed") { - std::string msg = op.value("error", nlohmann::json{}).value("message", std::string(TR("shield_unknown_error"))); + } else if (status == "failed" || status == "cancelled" || status == "canceled") { + std::string msg = TR("shield_unknown_error"); + if (op.contains("error") && op["error"].is_object() && op["error"].contains("message") && + op["error"]["message"].is_string()) + msg = op["error"]["message"].get(); s_operation_pending = false; s_op_terminal = true; s_status_message = std::string(TR("shield_op_failed")) + msg; - Notifications::instance().error(std::string(TR("shield_op_failed")) + msg); + // No toast: the op is tracked (App::trackOperation) and the shared opid poller raises the + // one failure toast, whether this dialog is open or not. } // queued / executing → leave the "Consolidating…" message and keep polling. }; @@ -426,6 +530,9 @@ void ShieldDialog::render(App* app) auto cancelBtn = S.button("dialogs.shield", "cancel-button"); const float dp = Layout::dpiScale(); const bool isMerge = (s_mode == Mode::MergeToAddress); + // Merging needs the full node's z_mergetoaddress. Lite has no node: its scope scan used to fail and be + // re-posted every frame for as long as the dialog stayed open. (Shield coinbase keeps its own gating.) + if (isMerge && !app->supportsFullNodeLifecycleActions()) { s_open = false; return; } const char* title = s_consolidate ? TR("consolidate_title") : isMerge ? TR("merge_title") @@ -441,7 +548,14 @@ void ShieldDialog::render(App* app) autoSelectDestination(state); pollOperation(app); pollPerAddress(app); - if (isMerge && !s_scope_loaded && !s_scope_loading && s_operation_id.empty()) loadScope(app); + if (isMerge) { + services::ScopeInputs in; + in.mergeMode = true; + in.busyWithOp = !s_operation_id.empty() || s_operation_pending || !s_runs.empty(); + in.nodeReady = app->walletScanReady(); + in.now = services::steadySeconds(); + if (services::scopeScanWant(s_scan, in) == services::ScopeWant::Post) loadScope(app); + } // ── Description ────────────────────────────────────────────────────────────────────────────── ImGui::TextWrapped("%s", s_consolidate ? TR("consolidate_desc") @@ -455,14 +569,24 @@ void ShieldDialog::render(App* app) // ── Merge: scope + source selector ────────────────────────────────────────────────────────── if (isMerge && !opInFlight) { - if (s_scope_loading) { + if (s_scan.phase == services::ScopePhase::Loading) { material::Type().textColored(material::TypeStyle::Caption, material::OnSurfaceMedium(), TR("merge_scope_loading")); - } else if (s_scope_loaded) { + } else if (s_scan.phase == services::ScopePhase::Loaded) { char buf[160]; std::snprintf(buf, sizeof(buf), TR("merge_scope_fmt"), s_t_count, s_z_count, fmtAmt(s_t_amount + s_z_amount).c_str()); material::Type().textColored(material::TypeStyle::Caption, material::OnSurfaceMedium(), buf); + } else if (s_scan.phase == services::ScopePhase::Failed) { + const std::string reason = s_scan.error.size() > 160 ? s_scan.error.substr(0, 160) + "..." : s_scan.error; + char buf[320]; + std::snprintf(buf, sizeof(buf), TR("merge_scope_failed_fmt"), reason.c_str()); + wrappedCaption(material::Warning(), buf); + if (material::TactileButton(TR("retry"), ImVec2(0, 0), S.resolveFont(cancelBtn.font))) + services::scopeScanRetry(s_scan); + } else { + material::Type().textColored(material::TypeStyle::Caption, material::OnSurfaceMedium(), + state.isLocked() ? TR("merge_wait_locked") : TR("merge_waiting_node")); } ImGui::Spacing(); @@ -493,7 +617,7 @@ void ShieldDialog::render(App* app) ImGui::Spacing(); int listed = 0; for (const auto& g : s_groups) if (g.notes >= 2) ++listed; - if (s_scope_loaded && listed == 0) { + if (s_scan.phase == services::ScopePhase::Loaded && listed == 0) { wrappedCaption(material::Warning(), TR("merge_keep_each_none")); } else if (listed > 0) { const float rowH = ImGui::GetFrameHeightWithSpacing(); @@ -511,8 +635,8 @@ void ShieldDialog::render(App* app) ImGui::PushID(g.address.c_str()); ImGui::BeginDisabled(!worth); if (ImGui::Checkbox(row, &ticked)) { - if (ticked) s_selected.insert(g.address); - else s_selected.erase(g.address); + if (ticked) { s_selected.insert(g.address); s_unticked.erase(g.address); } + else { s_selected.erase(g.address); s_unticked.insert(g.address); } } ImGui::EndDisabled(); if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled)) @@ -618,7 +742,7 @@ void ShieldDialog::render(App* app) ImGui::Spacing(); wrappedCaption(material::Warning(), hb); } - } else if (isMerge && s_scope_loaded && srcCount() > s_utxo_limit) { + } else if (isMerge && s_scan.phase == services::ScopePhase::Loaded && srcCount() > s_utxo_limit) { char hb[160]; std::snprintf(hb, sizeof(hb), TR("merge_batch_fmt"), s_utxo_limit); ImGui::Spacing(); @@ -634,8 +758,8 @@ void ShieldDialog::render(App* app) const std::string label = app->getAddressLabel(r.run.address); const char* name = label.empty() ? nullptr : label.c_str(); const std::string shortA = shortAddr(r.run.address); - int done = 0, failed = 0; - for (int st : r.state) { if (st == 1) ++done; if (st == 2) ++failed; } + int done = 0, failed = 0, unknown = 0; + for (int st : r.state) { if (st == 1) ++done; if (st == 2) ++failed; if (st == 3) ++unknown; } const int batches = static_cast(r.run.opids.size()); char line[320]; ImU32 col = material::OnSurfaceMedium(); @@ -647,6 +771,9 @@ void ShieldDialog::render(App* app) std::snprintf(line, sizeof(line), TR("merge_keep_each_status_failed"), name ? name : shortA.c_str(), r.failure.c_str()); col = material::Error(); + } else if (unknown > 0 && done + unknown == batches) { + std::snprintf(line, sizeof(line), TR("merge_keep_each_status_unconfirmed"), name ? name : shortA.c_str()); + col = material::Warning(); } else if (done == batches) { std::snprintf(line, sizeof(line), TR("merge_keep_each_status_done"), name ? name : shortA.c_str(), r.run.notesMerged, batches); @@ -717,8 +844,9 @@ void ShieldDialog::render(App* app) } bool can_submit = haveDest && connected && !syncing; - if (isMerge && s_scope_loaded && srcCount() == 0) can_submit = false; - if (perAddress) can_submit = connected && !syncing && s_scope_loaded && !selectedGroups().empty(); + const bool scopeLoaded = s_scan.phase == services::ScopePhase::Loaded; + if (isMerge && scopeLoaded && srcCount() == 0) can_submit = false; + if (perAddress) can_submit = connected && !syncing && scopeLoaded && !selectedGroups().empty(); float footerBtnW = shieldBtn.width + cancelBtn.width + ImGui::GetStyle().ItemSpacing.x; material::BeginOverlayDialogFooter(footerBtnW, /*drawSeparator=*/false); diff --git a/src/ui/windows/shield_dialog.h b/src/ui/windows/shield_dialog.h index e37cd86..601b20c 100644 --- a/src/ui/windows/shield_dialog.h +++ b/src/ui/windows/shield_dialog.h @@ -52,6 +52,12 @@ public: * @brief Close the dialog (used by the UI sweep teardown) */ static void hide(); + + /** + * @brief Forget the scope (inputs) read for the Merge/Consolidate view: the node connection changed, + * so it may be a different wallet. The next render reads it again once the node is ready. + */ + static void invalidateScope(); }; } // namespace ui diff --git a/src/util/i18n.cpp b/src/util/i18n.cpp index d58232b..dcff5e1 100644 --- a/src/util/i18n.cpp +++ b/src/util/i18n.cpp @@ -2493,6 +2493,7 @@ void I18n::loadBuiltinEnglish() strings_["shield_unknown_error"] = "Unknown error"; strings_["shield_send_failed"] = "Shield failed: "; strings_["merge_send_failed"] = "Merge failed: "; + strings_["merge_submit_uncertain"] = "The node didn't confirm the merge before the connection was interrupted. It may still go through \xE2\x80\x94 check History after reconnecting before merging again."; strings_["merge_description"] = "Merge multiple UTXOs into a single shielded address. This can help reduce wallet size and improve privacy."; strings_["merge_funds"] = "Merge Funds"; strings_["merge_started"] = "Merge operation started"; @@ -2503,6 +2504,9 @@ void I18n::loadBuiltinEnglish() strings_["consolidate_funds_btn"] = "Consolidate"; strings_["merge_scope_loading"] = "Checking your inputs\xE2\x80\xA6"; strings_["merge_scope_fmt"] = "%d transparent + %d shielded inputs \xC2\xB7 ~%s DRGX spendable"; + strings_["merge_waiting_node"] = "Waiting for the node\xE2\x80\xA6"; + strings_["merge_wait_locked"] = "The wallet is locked. Unlock it to continue."; + strings_["merge_scope_failed_fmt"] = "Couldn't read your inputs: %s"; strings_["merge_source"] = "Consolidate"; strings_["merge_src_transparent"] = "Transparent"; strings_["merge_src_shielded"] = "Shielded"; @@ -2535,6 +2539,7 @@ void I18n::loadBuiltinEnglish() strings_["merge_keep_each_status_failed"] = "%s — failed: %s"; strings_["merge_keep_each_status_done"] = "%s — done: %d notes merged in %d batch(es)"; strings_["merge_keep_each_status_running"] = "%s — merging %d notes (%d of %d batches done)…"; + strings_["merge_keep_each_status_unconfirmed"] = "%s \xE2\x80\x94 status could not be confirmed"; strings_["merge_keep_each_run_again"] = "Addresses merged in several batches now hold one note per batch. Run this again after those confirm to merge them into one."; strings_["done"] = "Done"; diff --git a/tests/test_phase4.cpp b/tests/test_phase4.cpp index b0be774..e798832 100644 --- a/tests/test_phase4.cpp +++ b/tests/test_phase4.cpp @@ -1652,9 +1652,10 @@ void testNetworkRefreshRpcCollectors() coreFallbackRpc.addResponse("getblockchaininfo", json{{"blocks", 8}, {"headers", 9}}); auto partialCore = Refresh::collectCoreRefreshResult(coreFallbackRpc); // No longestchain in the response → not classified as "behind" → balance is still attempted (here it - // fails). getblockchaininfo is still issued first. + // fails). getblockchaininfo is still issued first. The minconf=1 call follows only a minconf=0 call + // that succeeded (its result would be dropped anyway). EXPECT_TRUE(coreFallbackRpc.methodNames() == std::vector({ - "getblockchaininfo", "z_gettotalbalance", "z_gettotalbalance" + "getblockchaininfo", "z_gettotalbalance" })); EXPECT_FALSE(partialCore.balanceOk); EXPECT_TRUE(partialCore.blockchainOk); @@ -4504,6 +4505,8 @@ void testConsolidationStringsTranslated() "merge_keep_each_confirm_fmt", "merge_keep_each_select", "merge_keep_each_nothing_submitted", "merge_keep_each_some_failed", "merge_keep_each_status_not_started", "merge_keep_each_status_failed", "merge_keep_each_status_done", "merge_keep_each_status_running", "merge_keep_each_run_again", + "merge_waiting_node", "merge_wait_locked", "merge_scope_failed_fmt", "retry", + "merge_keep_each_status_unconfirmed", "merge_submit_uncertain", }); } @@ -9496,6 +9499,347 @@ void testBalanceDisplayLerp() } } +// C47: the spendable (minconf=1) z_gettotalbalance is a second whole-wallet scan under cs_main. It runs +// only after the minconf=0 call succeeded, and only when its answer can differ from it: something is +// unconfirmed or pending (needSpendable), or the exact total moved since the last poll. +void testCoreRefreshBalancePlan() +{ + using Refresh = dragonx::services::NetworkRefreshService; + using json = nlohmann::json; + using dragonx::rpc::RpcFailureKind; + using dragonx::rpc::RpcTransportError; + const json chain = json{{"blocks", 150}, {"headers", 150}, {"longestchain", 150}, {"bestblockhash", "tip-150"}}; + const json total = json{{"private", "3.00000000"}, {"transparent", "1.25000000"}, {"total", "4.25000000"}}; + const json spendable = json{{"private", "2.00000000"}, {"transparent", "1.25000000"}, {"total", "3.25000000"}}; + const std::vector oneCall = {"getblockchaininfo", "z_gettotalbalance"}; + const std::vector twoCalls = {"getblockchaininfo", "z_gettotalbalance", "z_gettotalbalance"}; + + { // Nothing pending and the total unchanged: one balance call, and spendable is the display total. + MockRefreshRpc rpc; + rpc.addResponse("getblockchaininfo", chain); + rpc.addResponse("z_gettotalbalance", total); + Refresh::CoreRefreshPlan plan; + plan.needSpendable = false; + plan.prevTotalZat = 425000000; + auto result = Refresh::collectCoreRefreshResult(rpc, plan); + EXPECT_TRUE(rpc.methodNames() == oneCall); + EXPECT_EQ(rpc.calls[1].params, json::array({0})); + EXPECT_TRUE(result.balanceOk); + EXPECT_TRUE(result.balanceAttempted); + EXPECT_FALSE(result.spendableQueried); + EXPECT_TRUE(result.totalBalanceZat == std::optional(425000000)); + EXPECT_TRUE(Refresh::nextTotalBaseline(result, 425000000) == std::optional(425000000)); + dragonx::WalletState state; + Refresh::applyCoreRefreshResult(state, result, 1000); + EXPECT_NEAR(state.spendableTotalBalance, 4.25, 1e-9); + EXPECT_NEAR(state.unconfirmedBalance, 0.0, 1e-9); + } + { // The total moved (how a new 0-conf receive first shows): the spendable call runs. + MockRefreshRpc rpc; + rpc.addResponse("getblockchaininfo", chain); + rpc.addResponse("z_gettotalbalance", total); + rpc.addResponse("z_gettotalbalance", spendable); + Refresh::CoreRefreshPlan plan; + plan.needSpendable = false; + plan.prevTotalZat = 325000000; + auto result = Refresh::collectCoreRefreshResult(rpc, plan); + EXPECT_TRUE(rpc.methodNames() == twoCalls); + EXPECT_EQ(rpc.calls[2].params, json::array({1})); + EXPECT_TRUE(result.spendableQueried); + dragonx::WalletState state; + Refresh::applyCoreRefreshResult(state, result, 1000); + EXPECT_NEAR(state.spendableTotalBalance, 3.25, 1e-9); + EXPECT_NEAR(state.unconfirmedBalance, 1.0, 1e-9); + } + { // Something unconfirmed or pending: the spendable call runs although the total held still. + MockRefreshRpc rpc; + rpc.addResponse("getblockchaininfo", chain); + rpc.addResponse("z_gettotalbalance", total); + rpc.addResponse("z_gettotalbalance", spendable); + Refresh::CoreRefreshPlan plan; + plan.needSpendable = true; + plan.prevTotalZat = 425000000; + Refresh::collectCoreRefreshResult(rpc, plan); + EXPECT_TRUE(rpc.methodNames() == twoCalls); + } + { // No previous total (first poll, or after a reconnect): the spendable call runs. + MockRefreshRpc rpc; + rpc.addResponse("getblockchaininfo", chain); + rpc.addResponse("z_gettotalbalance", total); + rpc.addResponse("z_gettotalbalance", spendable); + Refresh::CoreRefreshPlan plan; + plan.needSpendable = false; + Refresh::collectCoreRefreshResult(rpc, plan); + EXPECT_TRUE(rpc.methodNames() == twoCalls); + } + { // The display call failed: its result is dropped, so the spendable scan isn't run either. + MockRefreshRpc rpc; + rpc.addResponse("getblockchaininfo", chain); + rpc.addFailure("z_gettotalbalance", "boom"); + auto result = Refresh::collectCoreRefreshResult(rpc, Refresh::CoreRefreshPlan{}); + EXPECT_TRUE(rpc.methodNames() == oneCall); + EXPECT_FALSE(result.balanceOk); + EXPECT_TRUE(result.balanceAttempted); // it reached the daemon + EXPECT_FALSE(result.spendableQueried); + EXPECT_FALSE(result.totalBalanceZat.has_value()); + } + { // Held back by the busy gate: never reached the daemon, so a forced refresh must not count it. + MockRefreshRpc rpc; + rpc.addResponse("getblockchaininfo", chain); + rpc.addException("z_gettotalbalance", std::make_exception_ptr( + RpcTransportError(RpcFailureKind::Deferred, "held back: the node is busy"))); + auto result = Refresh::collectCoreRefreshResult(rpc, Refresh::CoreRefreshPlan{}); + EXPECT_FALSE(result.balanceOk); + EXPECT_FALSE(result.balanceAttempted); + EXPECT_TRUE(result.balanceErrKind == RpcFailureKind::Deferred); + } + { // Behind, or no balance asked for: the chain call only, nothing attempted. + MockRefreshRpc rpc; + rpc.addResponse("getblockchaininfo", json{{"blocks", 100}, {"headers", 150}, {"longestchain", 150}}); + auto behind = Refresh::collectCoreRefreshResult(rpc, Refresh::CoreRefreshPlan{}); + EXPECT_TRUE(rpc.methodNames() == std::vector({"getblockchaininfo"})); + EXPECT_FALSE(behind.balanceAttempted); + MockRefreshRpc chainOnly; + chainOnly.addResponse("getblockchaininfo", chain); + Refresh::CoreRefreshPlan plan; + plan.includeBalance = false; + auto skipped = Refresh::collectCoreRefreshResult(chainOnly, plan); + EXPECT_TRUE(chainOnly.methodNames() == std::vector({"getblockchaininfo"})); + EXPECT_FALSE(skipped.balanceAttempted); + EXPECT_TRUE(Refresh::nextTotalBaseline(skipped, 425000000) == std::optional(425000000)); + } + // The total moved but the minconf=1 call brought nothing back (an error, or held back by the busy gate): + // spendable degrades to display, so the baseline is dropped and the next poll measures it again + // instead of skipping it while the total holds still. + for (int heldBack = 0; heldBack < 2; ++heldBack) { + MockRefreshRpc rpc; + rpc.addResponse("getblockchaininfo", chain); + rpc.addResponse("z_gettotalbalance", total); + if (heldBack) { + rpc.addException("z_gettotalbalance", std::make_exception_ptr( + RpcTransportError(RpcFailureKind::Deferred, "held back: the node is busy"))); + } else { + rpc.addFailure("z_gettotalbalance", "boom"); + } + Refresh::CoreRefreshPlan plan; + plan.needSpendable = false; + plan.prevTotalZat = 325000000; + auto result = Refresh::collectCoreRefreshResult(rpc, plan); + EXPECT_TRUE(rpc.methodNames() == twoCalls); + EXPECT_TRUE(result.balanceOk); + EXPECT_TRUE(result.spendableQueried); + EXPECT_FALSE(result.spendableTotalBalance.has_value()); + dragonx::WalletState state; + Refresh::applyCoreRefreshResult(state, result, 1000); + EXPECT_NEAR(state.spendableTotalBalance, 4.25, 1e-9); + EXPECT_NEAR(state.unconfirmedBalance, 0.0, 1e-9); + const auto baseline = Refresh::nextTotalBaseline(result, plan.prevTotalZat); + EXPECT_FALSE(baseline.has_value()); + + // The next poll: no baseline, so the spendable call runs again and finds the unconfirmed funds. + MockRefreshRpc retry; + retry.addResponse("getblockchaininfo", chain); + retry.addResponse("z_gettotalbalance", total); + retry.addResponse("z_gettotalbalance", spendable); + Refresh::CoreRefreshPlan next; + next.needSpendable = false; + next.prevTotalZat = baseline; + auto healed = Refresh::collectCoreRefreshResult(retry, next); + EXPECT_TRUE(retry.methodNames() == twoCalls); + dragonx::WalletState healedState; + Refresh::applyCoreRefreshResult(healedState, healed, 1001); + EXPECT_NEAR(healedState.unconfirmedBalance, 1.0, 1e-9); + EXPECT_TRUE(Refresh::nextTotalBaseline(healed, baseline) == std::optional(425000000)); + } +} + +// Exact daemon amounts in zatoshis, so "did the total move" never trips on floating-point rounding. +void testParseMoneyZat() +{ + using Refresh = dragonx::services::NetworkRefreshService; + using Z = std::optional; + EXPECT_TRUE(Refresh::parseMoneyZat("4.25000000") == Z(425000000)); + EXPECT_TRUE(Refresh::parseMoneyZat("0.00000000") == Z(0)); + EXPECT_TRUE(Refresh::parseMoneyZat("12") == Z(1200000000)); + EXPECT_TRUE(Refresh::parseMoneyZat("0.1") == Z(10000000)); + EXPECT_TRUE(Refresh::parseMoneyZat("0.00000001") == Z(1)); + EXPECT_TRUE(Refresh::parseMoneyZat("-1.50000000") == Z(-150000000)); + EXPECT_TRUE(Refresh::parseMoneyZat("92233720367.99999999") == Z(9223372036799999999LL)); + EXPECT_FALSE(Refresh::parseMoneyZat("9999999999999999999").has_value()); // would overflow + EXPECT_FALSE(Refresh::parseMoneyZat("").has_value()); + EXPECT_FALSE(Refresh::parseMoneyZat("-").has_value()); + EXPECT_FALSE(Refresh::parseMoneyZat(".5").has_value()); + EXPECT_FALSE(Refresh::parseMoneyZat("1.").has_value()); + EXPECT_FALSE(Refresh::parseMoneyZat("1.123456789").has_value()); // finer than a zatoshi + EXPECT_FALSE(Refresh::parseMoneyZat("1e5").has_value()); + EXPECT_FALSE(Refresh::parseMoneyZat(" 1.0").has_value()); + EXPECT_FALSE(Refresh::parseMoneyZat("1.0 ").has_value()); +} + +void testSpendableBalanceNeeded() +{ + using Refresh = dragonx::services::NetworkRefreshService; + dragonx::WalletState state; + EXPECT_TRUE(Refresh::spendableBalanceNeeded(state, false, false)); // never fetched + state.last_balance_update = 1000; + EXPECT_FALSE(Refresh::spendableBalanceNeeded(state, false, false)); + EXPECT_TRUE(Refresh::spendableBalanceNeeded(state, true, false)); // a send in flight + EXPECT_TRUE(Refresh::spendableBalanceNeeded(state, false, true)); // forced (the user's own send) + state.unconfirmedBalance = 0.5; + EXPECT_TRUE(Refresh::spendableBalanceNeeded(state, false, false)); + state.unconfirmedBalance = 0.0; + dragonx::AddressInfo z; + z.address = "zs-one"; + z.balance = 2.0; + z.spendableBalance = 1.0; // the address scan saw unconfirmed funds + state.z_addresses.push_back(z); + EXPECT_TRUE(Refresh::spendableBalanceNeeded(state, false, false)); + state.z_addresses[0].spendableBalance = 2.0; + EXPECT_FALSE(Refresh::spendableBalanceNeeded(state, false, false)); + dragonx::AddressInfo t; + t.address = "R-one"; + t.balance = 1.0; + t.spendableBalance = 0.0; + state.t_addresses.push_back(t); + EXPECT_TRUE(Refresh::spendableBalanceNeeded(state, false, false)); +} + +// C34: z_getoperationstatus filtered to our opids. Its one argument is an ARRAY of ids, so the params are +// [[id, ...]]; the old ["id"] attempt was rejected. An empty filter would mean every op, so none at all. +void testOperationFilterParams() +{ + using Refresh = dragonx::services::NetworkRefreshService; + auto one = Refresh::buildOperationFilterParams({"opid-1"}); + EXPECT_TRUE(one.has_value()); + EXPECT_EQ(one->dump(), std::string("[[\"opid-1\"]]")); + auto two = Refresh::buildOperationFilterParams({"a", "", "b"}); + EXPECT_TRUE(two.has_value()); + EXPECT_EQ(two->dump(), std::string("[[\"a\",\"b\"]]")); + EXPECT_FALSE(Refresh::buildOperationFilterParams({}).has_value()); + EXPECT_FALSE(Refresh::buildOperationFilterParams({""}).has_value()); +} + +// C211: the Merge/Consolidate dialog's scope scan (listunspent + z_listunspent, two whole-wallet scans) +// used to be re-posted on the frame after any failure, forever on Lite. One scan at a time, a failure +// latches until Retry, and only "node not ready" failures retry by themselves (backed off, 3 times). +void testScopeScanAndLockedNotes() +{ + using namespace dragonx::services; + using dragonx::rpc::RpcFailureKind; + ScopeScan scan; + ScopeInputs in; + in.mergeMode = true; + in.nodeReady = true; + in.now = 100.0; + scopeScanOpen(scan, 100.0); + EXPECT_TRUE(scopeScanWant(scan, in) == ScopeWant::Post); + in.mergeMode = false; + EXPECT_TRUE(scopeScanWant(scan, in) == ScopeWant::Nothing); + in.mergeMode = true; + in.nodeReady = false; + EXPECT_TRUE(scopeScanWant(scan, in) == ScopeWant::WaitNode); + in.nodeReady = true; + in.busyWithOp = true; + EXPECT_TRUE(scopeScanWant(scan, in) == ScopeWant::Nothing); + in.busyWithOp = false; + + auto gen = scopeScanBegin(scan, 100.0); + EXPECT_TRUE(scan.phase == ScopePhase::Loading); + EXPECT_TRUE(scan.inFlight); + EXPECT_TRUE(scopeScanWant(scan, in) == ScopeWant::Nothing); // one scan at a time + + // A real error latches until Retry: no re-scan by itself, whenever the next frame comes. + EXPECT_FALSE(scopeScanFinish(scan, gen, "Insufficient permissions", RpcFailureKind::AppError, 101.0)); + EXPECT_TRUE(scan.phase == ScopePhase::Failed); + EXPECT_FALSE(scan.inFlight); + EXPECT_FALSE(scan.autoRetry); + in.now = 10000.0; + EXPECT_TRUE(scopeScanWant(scan, in) == ScopeWant::Nothing); + scopeScanRetry(scan); + EXPECT_TRUE(scopeScanWant(scan, in) == ScopeWant::Post); + // ...as does a reply timeout (the daemon is still running that scan). + gen = scopeScanBegin(scan, 10000.0); + EXPECT_FALSE(scopeScanFinish(scan, gen, "Timeout was reached", RpcFailureKind::ReplyTimeout, 10001.0)); + EXPECT_FALSE(scan.autoRetry); + in.now = 20000.0; + EXPECT_TRUE(scopeScanWant(scan, in) == ScopeWant::Nothing); + + // Node not ready: retries by itself after 10, 30 and 60 s, then latches. + scopeScanRetry(scan); + gen = scopeScanBegin(scan, 20000.0); + scopeScanFinish(scan, gen, "Could not connect", RpcFailureKind::NotListening, 20000.0); + EXPECT_TRUE(scan.autoRetry); + in.now = 20009.0; + EXPECT_TRUE(scopeScanWant(scan, in) == ScopeWant::WaitBackoff); + in.now = 20010.0; + EXPECT_TRUE(scopeScanWant(scan, in) == ScopeWant::Post); + gen = scopeScanBegin(scan, 20010.0); + scopeScanFinish(scan, gen, "Could not connect", RpcFailureKind::NotListening, 20010.0); + in.now = 20039.0; + EXPECT_TRUE(scopeScanWant(scan, in) == ScopeWant::WaitBackoff); + in.now = 20040.0; + EXPECT_TRUE(scopeScanWant(scan, in) == ScopeWant::Post); + gen = scopeScanBegin(scan, 20040.0); + scopeScanFinish(scan, gen, "Could not connect", RpcFailureKind::NotListening, 20040.0); + EXPECT_NEAR(scan.retryAt, 20100.0, 1e-9); + in.now = 20100.0; + gen = scopeScanBegin(scan, 20100.0); + scopeScanFinish(scan, gen, "Could not connect", RpcFailureKind::NotListening, 20100.0); + EXPECT_FALSE(scan.autoRetry); // a 4th failure: wait for Retry + in.now = 30000.0; + EXPECT_TRUE(scopeScanWant(scan, in) == ScopeWant::Nothing); + EXPECT_TRUE(scopeFailureRetries(RpcFailureKind::Other, "Not connected")); + EXPECT_FALSE(scopeFailureRetries(RpcFailureKind::Other, "[json.exception.type_error.302] type must be number")); + + // A result from an earlier session (the connection changed meanwhile) is dropped, but frees the guard. + ScopeScan old; + scopeScanOpen(old, 0.0); + const auto oldGen = scopeScanBegin(old, 0.0); + scopeScanInvalidate(old); + EXPECT_TRUE(old.inFlight); + in.now = 1.0; + EXPECT_TRUE(scopeScanWant(old, in) == ScopeWant::Nothing); // the old scan still runs in the daemon + EXPECT_FALSE(scopeScanFinish(old, oldGen, "", RpcFailureKind::None, 1.0)); + EXPECT_FALSE(old.inFlight); + EXPECT_TRUE(old.phase == ScopePhase::Idle); + EXPECT_TRUE(scopeScanWant(old, in) == ScopeWant::Post); + + // Closing and reopening while a scan loads adopts it (no second pair of scans)... + ScopeScan adopt; + scopeScanOpen(adopt, 0.0); + const auto adoptGen = scopeScanBegin(adopt, 0.0); + scopeScanOpen(adopt, 5.0); + EXPECT_TRUE(adopt.gen == adoptGen); + EXPECT_TRUE(adopt.inFlight); + EXPECT_TRUE(scopeScanFinish(adopt, adoptGen, "", RpcFailureKind::None, 6.0)); + EXPECT_TRUE(adopt.phase == ScopePhase::Loaded); + // ...unless it has been outstanding past the limit: presumed lost, a new session starts. + ScopeScan lost; + scopeScanOpen(lost, 0.0); + const auto lostGen = scopeScanBegin(lost, 0.0); + scopeScanOpen(lost, kScopeInFlightMaxSec + 1.0); + EXPECT_TRUE(lost.gen != lostGen); + EXPECT_FALSE(lost.inFlight); + const auto freshGen = scopeScanBegin(lost, kScopeInFlightMaxSec + 2.0); + EXPECT_FALSE(scopeScanFinish(lost, lostGen, "", RpcFailureKind::None, kScopeInFlightMaxSec + 3.0)); + EXPECT_TRUE(lost.inFlight); // the late result doesn't clear the fresh scan's guard + EXPECT_TRUE(scopeScanFinish(lost, freshGen, "", RpcFailureKind::None, kScopeInFlightMaxSec + 4.0)); + + // Locked notes (held by an operation in flight) can't be merged, so they aren't counted. + using json = nlohmann::json; + const json notes = json::array({ + json{{"address", "zs-a"}, {"confirmations", 3}, {"amount", 1.0}}, + json{{"address", "zs-a"}, {"confirmations", 3}, {"amount", 1.0}, {"locked", true}}, + json{{"address", "zs-a"}, {"confirmations", 3}, {"amount", 1.0}, {"locked", false}}, + }); + const auto groups = groupSpendableNotes(notes); + EXPECT_EQ(groups.size(), static_cast(1)); + EXPECT_EQ(groups[0].notes, 2); + EXPECT_TRUE(noteIsLocked(json{{"locked", true}})); + EXPECT_FALSE(noteIsLocked(json{{"locked", "yes"}})); + EXPECT_FALSE(noteIsLocked(json::array())); +} + int main() { testConnectionConfig(); @@ -9515,6 +9859,10 @@ int main() testNetworkRefreshSnapshotHelpers(); testParseDaemonRescanOutput(); testNetworkRefreshRpcCollectors(); + testCoreRefreshBalancePlan(); + testParseMoneyZat(); + testSpendableBalanceNeeded(); + testOperationFilterParams(); testNetworkRefreshResultModels(); testTrackedSendRetirement(); testBalanceDisplayLerp(); @@ -9530,6 +9878,7 @@ int main() testOwnershipRecord(); testDebugLogTailRules(); testNoteConsolidation(); + testScopeScanAndLockedNotes(); testStopDeliveryPolicy(); testProcStatParse(); testStopProgressTracker();