v0.2.6-pre.007

This commit is contained in:
2026-08-21 10:50:36 +02:00
parent 4c9086e2f0
commit f11f0921b0
25 changed files with 871 additions and 42 deletions

View File

@@ -1,5 +1,5 @@
// file: crates/ksp-app-wallet-desk/src/app_state.rs
// version: 7
// version: 8
//! Shared backend state owned by the Wallet Desk Tauri application.
@@ -9,6 +9,7 @@ pub(crate) struct AppState {
logging_runtime: std::sync::Mutex<LoggingRuntimeState>,
splash_settings: crate::SplashSettings,
splash_sequence_started: std::sync::atomic::AtomicBool,
transport_runtime: crate::TransportRuntime,
wallet_config_startup: crate::WalletConfigStartup,
wallet_session: std::sync::Mutex<crate::WalletSession>,
}
@@ -31,6 +32,14 @@ impl AppState {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let transport_runtime = crate::initialize_transport(&config_management);
let transport_runtime = match transport_runtime {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => {
ksp_logging_lib::error!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_BOOTSTRAP, error_domain = error.code().domain(), error_code = error.code().code(), "Wallet Desk Config/Transport bootstrap failed");
return std::result::Result::Err(error);
},
};
let wallet_config_startup = crate::initialize_wallet_config(&config_management);
let wallet_config_startup = match wallet_config_startup {
std::result::Result::Ok(value) => value,
@@ -58,6 +67,7 @@ impl AppState {
}),
splash_settings,
splash_sequence_started: std::sync::atomic::AtomicBool::new(false),
transport_runtime,
wallet_config_startup,
wallet_session: std::sync::Mutex::new(crate::WalletSession::no_selection()),
});
@@ -90,20 +100,37 @@ impl AppState {
},
};
let _keep_guard_alive = &runtime.guard;
let transport_snapshot = self.transport_runtime.pool().snapshot();
let transport_available_endpoint_count = count_to_u32(transport_snapshot.available_endpoint_count(), "available Transport endpoints");
let transport_available_endpoint_count = match transport_available_endpoint_count {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let transport_endpoint_count = count_to_u32(transport_snapshot.endpoint_count(), "Transport endpoints");
let transport_endpoint_count = match transport_endpoint_count {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let resolved = self.wallet_config_startup.resolved();
let wallets_subdirectory = resolved.wallets_subdirectory().map(|value| return value.to_string_lossy().into_owned());
return std::result::Result::Ok(crate::RuntimeStatusDto {
application_version: env!("CARGO_PKG_VERSION").to_owned(),
active_composite_profile: self.wallet_config_startup.composite_profile_id().to_owned(),
active_logging_profile: runtime.active_profile_id.clone(),
active_transport_profile: self.transport_runtime.profile_id().to_owned(),
active_wallet_profile: resolved.profile_id().to_owned(),
config_document_count: document_count,
effective_wallets_directory: resolved.effective_wallets_directory().to_string_lossy().into_owned(),
effective_wallets_directory_created_on_startup: self.wallet_config_startup.effective_directory_created_on_startup(),
fallback_logging_active: runtime.fallback_active,
root_wallets_directory_created_on_startup: self.wallet_config_startup.root_directory_created_on_startup(),
shell_phase: "pre.006-wallet-unlock".to_owned(),
shell_phase: "pre.007-wallet-balance".to_owned(),
startup_diagnostic: runtime.startup_diagnostic.clone(),
transport_available_endpoint_count,
transport_clusters: self.transport_runtime.clusters(),
transport_endpoint_count,
transport_providers: self.transport_runtime.providers(),
transport_role: self.transport_runtime.role().as_str().to_owned(),
wallets_directory: resolved.wallets_directory().to_string_lossy().into_owned(),
wallets_subdirectory,
});
@@ -524,6 +551,87 @@ impl AppState {
};
}
/// Refreshes the native SOL balance for the currently authorized VIEW/OWNER Wallet.
pub(crate) async fn refresh_wallet_balance(&self) -> ksp_core_lib::Result<crate::WalletBalanceDto> {
let context = self.authorized_balance_context();
let context = match context {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => return std::result::Result::Err(error),
};
let config = ksp_onchain_transport_lib::GetBalanceConfig::new(
std::option::Option::Some(ksp_onchain_transport_lib::SolanaCommitment::Confirmed),
std::option::Option::None,
);
ksp_logging_lib::debug!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_TRANSPORT, wallet_id = context.wallet_id.as_str(), capability = context.capability, transport_profile = self.transport_runtime.profile_id(), transport_role = self.transport_runtime.role().as_str(), "Wallet balance refresh started");
let result = self.transport_runtime.pool().get_balance(self.transport_runtime.role(), &context.pubkey, std::option::Option::Some(&config)).await;
let result = match result {
std::result::Result::Ok(value) => value,
std::result::Result::Err(error) => {
ksp_logging_lib::warn!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_TRANSPORT, wallet_id = context.wallet_id.as_str(), capability = context.capability, error_domain = error.code().domain(), error_code = error.code().code(), "Wallet balance refresh failed");
return std::result::Result::Err(error);
},
};
if !self.balance_context_is_current(&context) {
return std::result::Result::Err(ksp_core_lib::Error::new(
crate::ERROR_CODE_WALLET_SESSION_INVALID,
"Wallet balance refresh completed after the authorized session changed",
));
}
let lamports = result.value();
let dto = crate::WalletBalanceDto {
api_version: result.context().api_version().map(str::to_owned),
lamports: lamports.to_string(),
slot: result.context().slot(),
sol: crate::format_lamports_as_sol(lamports),
transport_profile: self.transport_runtime.profile_id().to_owned(),
transport_role: self.transport_runtime.role().as_str().to_owned(),
wallet_id: context.wallet_id.clone(),
};
ksp_logging_lib::info!(target: crate::TRACING_TARGET, domain = crate::TRACING_DOMAIN_TRANSPORT, wallet_id = context.wallet_id.as_str(), capability = context.capability, lamports, slot = dto.slot, "Wallet balance refresh succeeded");
return std::result::Result::Ok(dto);
}
fn authorized_balance_context(&self) -> ksp_core_lib::Result<WalletBalanceContext> {
let session = self.wallet_session.lock();
let session = match session {
std::result::Result::Ok(value) => value,
std::result::Result::Err(_) => return std::result::Result::Err(session_lock_error()),
};
return match &*session {
crate::WalletSession::View { wallet_id, wallet, .. } => {
std::result::Result::Ok(WalletBalanceContext { capability: "view", pubkey: wallet.pubkey().to_owned(), wallet_id: wallet_id.clone() })
},
crate::WalletSession::Owner { wallet_id, wallet, .. } => {
std::result::Result::Ok(WalletBalanceContext { capability: "owner", pubkey: wallet.pubkey().to_owned(), wallet_id: wallet_id.clone() })
},
crate::WalletSession::PrivilegedOperation { .. } => std::result::Result::Err(ksp_core_lib::Error::new(
crate::ERROR_CODE_WALLET_SESSION_INVALID,
"Wallet balance is unavailable while a privileged Wallet operation is running",
)),
crate::WalletSession::Locked { .. } | crate::WalletSession::NoSelection => std::result::Result::Err(ksp_core_lib::Error::new(
crate::ERROR_CODE_WALLET_AUTHORIZATION_REQUIRED,
"Wallet balance requires an authorized VIEW or OWNER session",
)),
};
}
fn balance_context_is_current(&self, context: &WalletBalanceContext) -> bool {
let session = self.wallet_session.lock();
let session = match session {
std::result::Result::Ok(value) => value,
std::result::Result::Err(_) => return false,
};
return match &*session {
crate::WalletSession::View { wallet_id, wallet, .. } => {
context.capability == "view" && wallet_id == &context.wallet_id && wallet.pubkey() == &context.pubkey
},
crate::WalletSession::Owner { wallet_id, wallet, .. } => {
context.capability == "owner" && wallet_id == &context.wallet_id && wallet.pubkey() == &context.pubkey
},
crate::WalletSession::NoSelection | crate::WalletSession::Locked { .. } | crate::WalletSession::PrivilegedOperation { .. } => false,
};
}
/// Returns the resolved common splash timings captured during bootstrap.
#[must_use]
pub(crate) const fn splash_settings(&self) -> crate::SplashSettings {
@@ -562,6 +670,24 @@ fn configured_secret_missing_error() -> ksp_core_lib::Error {
return ksp_core_lib::Error::new(crate::ERROR_CODE_WALLET_SECRET_CANDIDATES_MISSING, "No effective Config-owned Wallet password candidate is available");
}
struct WalletBalanceContext {
capability: &'static str,
pubkey: ksp_core_lib::Pubkey,
wallet_id: String,
}
fn count_to_u32(value: usize, label: &'static str) -> ksp_core_lib::Result<u32> {
let converted = u32::try_from(value);
return match converted {
std::result::Result::Ok(value) => std::result::Result::Ok(value),
std::result::Result::Err(error) => std::result::Result::Err(
ksp_core_lib::Error::new(crate::ERROR_CODE_APP_STATE_INVALID, "Wallet Desk runtime count exceeds DTO range")
.with_context("count_kind", label)
.with_source(error),
),
};
}
struct LoggingRuntimeState {
guard: ksp_logging_lib::LoggingGuard,
active_profile_id: std::option::Option<String>,