Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2350,6 +2350,15 @@ impl App for AppState {
// TaskError Debug output is shown to users, deliberately.
// Ensure inner error types don't expose secrets.
handle.with_details(&err);
match &err {
TaskError::WalletStorageNotReady => {
self.migration.track_storage_startup_error(handle);
}
TaskError::MasternodeListNotReady { .. } => {
self.connection_banner.track_quorum_startup_error(handle);
}
_ => {}
}
if !is_database_clear {
self.visible_screen_mut()
.display_message(&msg, MessageType::Error);
Expand Down
121 changes: 121 additions & 0 deletions src/app/reconcilers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,27 +226,57 @@ impl SpvBlockReconciler {
}
}

#[derive(Default)]
struct TransientBanner(Option<BannerHandle>);

impl TransientBanner {
fn track(&mut self, handle: BannerHandle) {
self.0 = Some(handle);
}

fn reset(&mut self) {
if let Some(handle) = self.0.take() {
handle.clear();
}
}

fn clear_if(&mut self, condition: bool) {
if condition {
self.reset();
}
}
}

/// Reconciles the connection-status banner with the overall connection state.
pub(super) struct ConnectionBanner {
/// Previous state, to detect transitions. `None` forces re-evaluation.
previous_state: Option<OverallConnectionState>,
/// Handle to the current connection banner, if displayed.
handle: Option<BannerHandle>,
/// Startup proof error cleared once quorum keys become available.
quorum_startup_error: TransientBanner,
}

impl ConnectionBanner {
pub(super) fn new() -> Self {
Self {
previous_state: None,
handle: None,
quorum_startup_error: TransientBanner::default(),
}
}

/// Adopt a quorum-not-ready error banner from the generic task fallback.
pub(super) fn track_quorum_startup_error(&mut self, handle: BannerHandle) {
self.quorum_startup_error.track(handle);
}

/// Clear the banner and force re-evaluation next frame (network switch).
pub(super) fn reset(&mut self) {
if let Some(handle) = self.handle.take() {
handle.clear();
}
self.quorum_startup_error.reset();
self.previous_state = None;
}

Expand All @@ -263,6 +293,8 @@ impl ConnectionBanner {
onboarding_active: bool,
) -> Option<BackendTask> {
let connection_status = app_context.connection_status();
self.quorum_startup_error
.clear_if(connection_status.masternodes_ready());
let current_state = connection_status.overall_state();
let state_changed = self.previous_state != Some(current_state);

Expand Down Expand Up @@ -361,6 +393,8 @@ impl ConnectionBanner {
pub(super) struct MigrationReconciler {
/// Handle to the current migration banner, if displayed.
banner_handle: Option<BannerHandle>,
/// Wallet-storage error cleared once migration is terminal and its run guard is free.
storage_startup_error: TransientBanner,
/// Last-seen migration state so reconciliation fires only on change.
last_state: Option<MigrationState>,
/// Networks whose cold-start `FinishUnwire` has been dispatched this process.
Expand All @@ -379,6 +413,7 @@ impl MigrationReconciler {
pub(super) fn new() -> Self {
Self {
banner_handle: None,
storage_startup_error: TransientBanner::default(),
last_state: None,
dispatched: BTreeSet::new(),
backend_wait_since: BTreeMap::new(),
Expand All @@ -388,13 +423,19 @@ impl MigrationReconciler {
}
}

/// Adopt a storage-not-ready error banner from the generic task fallback.
pub(super) fn track_storage_startup_error(&mut self, handle: BannerHandle) {
self.storage_startup_error.track(handle);
}

/// Clear the migration banner and force re-evaluation (network switch). The
/// per-network dispatch guard is intentionally NOT reset — it is scoped per
/// network so a return to a seen network never re-drains.
pub(super) fn reset_for_switch(&mut self) {
if let Some(handle) = self.banner_handle.take() {
handle.clear();
}
self.storage_startup_error.reset();
self.last_state = None;
self.wallet_unlock_popup.close();
self.prompt_wallet = None;
Expand Down Expand Up @@ -487,6 +528,13 @@ impl MigrationReconciler {
frame_state: &MigrationState,
) {
let state = frame_state.clone();
let storage_guard_resolved = !matches!(
&state,
MigrationState::Idle
| MigrationState::Running { .. }
| MigrationState::AwaitingWalletPasswords { .. }
) && app_context.migration_run.try_lock().is_ok();
self.storage_startup_error.clear_if(storage_guard_resolved);
self.update_password_prompt(ctx, app_context, &state);
if self.last_state.as_ref() == Some(&state) {
return;
Expand Down Expand Up @@ -932,6 +980,79 @@ mod tests {
);
}

#[test]
fn connection_banner_clears_quorum_startup_error_when_masternodes_become_ready() {
let tmp = tempfile::tempdir().expect("tempdir");
let app_context = test_app_context(tmp.path());
let message = "The network is still syncing. Please wait a moment and try again.";
let mut reconciler = ConnectionBanner::new();
let mut harness = Harness::builder()
.with_size(egui::vec2(700.0, 260.0))
.build_ui(MessageBanner::show_global);

let handle = MessageBanner::set_global(&harness.ctx, message, MessageType::Error);
handle.disable_auto_dismiss();
reconciler.track_quorum_startup_error(handle);
reconciler.update(&harness.ctx, &app_context, false, false);
harness.run();
assert!(harness.query_by_label(message).is_some());

app_context.connection_status().set_masternodes_ready(true);
reconciler.update(&harness.ctx, &app_context, false, false);
harness.run();
assert!(
harness.query_by_label(message).is_none(),
"the startup error must clear when quorum keys become available",
);
}

#[test]
fn migration_reconciler_waits_for_storage_guard_before_clearing_startup_error() {
let tmp = tempfile::tempdir().expect("tempdir");
let app_context = test_app_context(tmp.path());
let message = TaskError::WalletStorageNotReady.to_string();
let mut reconciler = MigrationReconciler::new();
let mut harness = Harness::builder()
.with_size(egui::vec2(700.0, 260.0))
.build_ui(MessageBanner::show_global);

app_context
.migration_status()
.set_state(MigrationState::Running {
step: crate::context::migration_status::MigrationStep::Detecting,
});
let handle = MessageBanner::set_global(&harness.ctx, &message, MessageType::Error);
handle.disable_auto_dismiss();
reconciler.track_storage_startup_error(handle);
let state = app_context.migration_status().state();
reconciler.update_banner(&harness.ctx, &app_context, state.as_ref());
harness.run();
assert!(harness.query_by_label(&message).is_some());

let migration_guard = app_context
.migration_run
.try_lock()
.expect("migration guard");
app_context
.migration_status()
.set_state(MigrationState::Ready);
let state = app_context.migration_status().state();
reconciler.update_banner(&harness.ctx, &app_context, state.as_ref());
harness.run();
assert!(
harness.query_by_label(&message).is_some(),
"terminal state must not clear the startup error while storage remains locked",
);

drop(migration_guard);
reconciler.update_banner(&harness.ctx, &app_context, state.as_ref());
harness.run();
assert!(
harness.query_by_label(&message).is_none(),
"the startup error must clear when the storage update is ready and unlocked",
);
}

#[test]
fn connection_banner_restores_recurring_error_after_onboarding_suppression() {
let tmp = tempfile::tempdir().expect("tempdir");
Expand Down
41 changes: 41 additions & 0 deletions src/backend_task/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,13 @@ pub enum TaskError {
source_error: Box<SdkError>,
},

/// Proof verification ran before SPV had synchronized quorum keys.
#[error("The network is still syncing. Please wait a moment and try again.")]
MasternodeListNotReady {
#[source]
source_error: Box<SdkError>,
},

// ──────────────────────────────────────────────────────────────────────────
// Wallet / platform-address operation errors
// ──────────────────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -2732,6 +2739,12 @@ impl From<dashcore_rpc::Error> for TaskError {

impl From<SdkError> for TaskError {
fn from(error: SdkError) -> Self {
if sdk_error_is_masternode_list_not_ready(&error) {
return TaskError::MasternodeListNotReady {
source_error: Box::new(error),
};
}

// Check DapiClientError for domain errors carried as gRPC Internal status.
// The SDK's From<DapiClientError> decodes `dash-serialized-consensus-error-bin`
// metadata, but some platform errors arrive as plain Internal with descriptive
Expand Down Expand Up @@ -3032,6 +3045,16 @@ impl From<SdkError> for TaskError {
}
}

fn sdk_error_is_masternode_list_not_ready(error: &SdkError) -> bool {
// Matches the `SpvProvider::get_quorum_public_key` payload from `context_provider`.
matches!(
error,
SdkError::Proof(dash_sdk::ProofVerifierError::ContextProviderError(
dash_sdk::error::ContextProviderError::Config(detail)
)) if detail == crate::context_provider::MASTERNODE_LIST_NOT_READY_DETAIL
)
}

fn sdk_error_is_dapi_reachability_failure(error: &SdkError) -> bool {
match error {
SdkError::DapiClientError(DapiClientError::Transport(TransportError::Grpc(status))) => {
Expand Down Expand Up @@ -3608,6 +3631,24 @@ mod tests {
);
}

#[test]
fn quorum_startup_error_maps_to_transient_task_error() {
// Recheck this upstream-format contract on every dash-sdk bump.
let sdk_detail = "masternode list not yet synced (quorums unavailable)";
assert_eq!(
crate::context_provider::MASTERNODE_LIST_NOT_READY_DETAIL,
sdk_detail
);
let sdk_error = SdkError::Proof(dash_sdk::ProofVerifierError::ContextProviderError(
dash_sdk::error::ContextProviderError::Config(sdk_detail.to_string()),
));

assert!(matches!(
TaskError::from(sdk_error),
TaskError::MasternodeListNotReady { .. }
));
}

#[test]
fn from_sdk_error_broadcast_cause_none_message_duplicate_remains_unconfirmed() {
let broadcast_err = dash_sdk::error::StateTransitionBroadcastError {
Expand Down
7 changes: 6 additions & 1 deletion src/context_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ use std::sync::Arc;
/// will reject a mismatch, catching forgotten additions at build time.
pub(crate) const SYSTEM_CONTRACT_COUNT: usize = 5;

/// Diagnostic carried by the SDK while quorum keys are unavailable at startup.
pub(crate) const MASTERNODE_LIST_NOT_READY_DETAIL: &str =
"masternode list not yet synced (quorums unavailable)";

/// Resolve a data contract by ID: check cached system contracts first, then DB.
///
/// All system contracts are listed in `cached` — adding a new one is a single
Expand Down Expand Up @@ -138,8 +142,9 @@ impl ContextProvider for SpvProvider {
// pre-unlock guard below uses. Any stray early proof call thus degrades
// gracefully instead of triggering the self-ban storm.
if !app_ctx.connection_status().masternodes_ready() {
// Consumed by `sdk_error_is_masternode_list_not_ready` in `backend_task::error`.
return Err(ContextProviderError::Config(
"masternode list not yet synced (quorums unavailable)".to_string(),
MASTERNODE_LIST_NOT_READY_DETAIL.to_string(),
));
}

Expand Down
Loading