Skip to content
Draft
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
13 changes: 6 additions & 7 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::backend_task::{BackendTask, BackendTaskSuccessResult};
use crate::components::core_zmq_listener::{CoreZMQListener, ZMQMessage};
use crate::context::AppContext;
use crate::database::Database;
use crate::lock_helper::{MutexExt, RwLockExt};
use crate::logging::initialize_logger;
use crate::model::settings::Settings;
use crate::ui::contracts_documents::contracts_documents_screen::DocumentQueryScreen;
Expand Down Expand Up @@ -391,8 +392,7 @@ impl AppState {

let mainnet_core_zmq_endpoint = mainnet_app_context
.config
.read()
.unwrap()
.read_or_recover()
.core_zmq_endpoint
.clone()
.unwrap_or_else(|| "tcp://127.0.0.1:23708".to_string());
Expand Down Expand Up @@ -422,7 +422,7 @@ impl AppState {

let testnet_core_zmq_endpoint = testnet_app_context
.as_ref()
.and_then(|ctx| ctx.config.read().unwrap().core_zmq_endpoint.clone())
.and_then(|ctx| ctx.config.read_or_recover().core_zmq_endpoint.clone())
.unwrap_or_else(|| "tcp://127.0.0.1:23709".to_string());
let testnet_disable_zmq = testnet_app_context
.as_ref()
Expand All @@ -449,7 +449,7 @@ impl AppState {

let devnet_core_zmq_endpoint = devnet_app_context
.as_ref()
.and_then(|ctx| ctx.config.read().unwrap().core_zmq_endpoint.clone())
.and_then(|ctx| ctx.config.read_or_recover().core_zmq_endpoint.clone())
.unwrap_or_else(|| "tcp://127.0.0.1:23710".to_string());
let devnet_disable_zmq = devnet_app_context
.as_ref()
Expand All @@ -476,7 +476,7 @@ impl AppState {

let local_core_zmq_endpoint = local_app_context
.as_ref()
.and_then(|ctx| ctx.config.read().unwrap().core_zmq_endpoint.clone())
.and_then(|ctx| ctx.config.read_or_recover().core_zmq_endpoint.clone())
.unwrap_or_else(|| "tcp://127.0.0.1:20302".to_string());
let local_disable_zmq = local_app_context
.as_ref()
Expand Down Expand Up @@ -964,8 +964,7 @@ impl App for AppState {
screen.scheduled_vote_cast_in_progress = true;
if let Some((_, s)) = screen
.scheduled_votes
.lock()
.unwrap()
.lock_or_recover()
.iter_mut()
.find(|(v, _)| v == &vote)
{
Expand Down
11 changes: 5 additions & 6 deletions src/backend_task/core/create_asset_lock.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::backend_task::BackendTaskSuccessResult;
use crate::context::AppContext;
use crate::lock_helper::{MutexExt, RwLockExt};
use crate::model::wallet::Wallet;
use dash_sdk::dashcore_rpc::RpcApi;
use dash_sdk::dpp::balances::credits::CREDITS_PER_DUFF;
Expand Down Expand Up @@ -34,14 +35,13 @@ impl AppContext {

// Insert the transaction into waiting for finality
{
let mut proofs = self.transactions_waiting_for_finality.lock().unwrap();
let mut proofs = self.transactions_waiting_for_finality.lock_or_recover();
proofs.insert(tx_id, None);
}

// Broadcast the transaction
self.core_client
.read()
.expect("Core client lock was poisoned")
.read_or_recover()
.send_raw_transaction(&asset_lock_transaction)
.map_err(|e| format!("Failed to broadcast asset lock transaction: {}", e))?;

Expand Down Expand Up @@ -96,14 +96,13 @@ impl AppContext {

// Insert the transaction into waiting for finality
{
let mut proofs = self.transactions_waiting_for_finality.lock().unwrap();
let mut proofs = self.transactions_waiting_for_finality.lock_or_recover();
proofs.insert(tx_id, None);
}

// Broadcast the transaction
self.core_client
.read()
.expect("Core client lock was poisoned")
.read_or_recover()
.send_raw_transaction(&asset_lock_transaction)
.map_err(|e| format!("Failed to broadcast asset lock transaction: {}", e))?;

Expand Down
7 changes: 3 additions & 4 deletions src/backend_task/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::app_dir::core_cookie_path;
use crate::backend_task::BackendTaskSuccessResult;
use crate::config::{Config, NetworkConfig};
use crate::context::AppContext;
use crate::lock_helper::RwLockExt;
use crate::model::wallet::Wallet;
use crate::model::wallet::single_key::SingleKeyWallet;
use crate::spv::CoreBackendMode;
Expand Down Expand Up @@ -151,8 +152,7 @@ impl AppContext {
match task {
CoreTask::GetBestChainLock => self
.core_client
.read()
.expect("Core client lock was poisoned")
.read_or_recover()
.get_best_chain_lock()
.map(|chain_lock| {
BackendTaskSuccessResult::CoreItem(CoreItem::ChainLock(
Expand Down Expand Up @@ -353,8 +353,7 @@ impl AppContext {

let txid = self
.core_client
.read()
.expect("Core client lock was poisoned")
.read_or_recover()
.send_raw_transaction(&tx)
.map_err(|e| format!("Failed to broadcast transaction: {e}"))?;

Expand Down
6 changes: 2 additions & 4 deletions src/backend_task/core/recover_asset_locks.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::backend_task::BackendTaskSuccessResult;
use crate::context::AppContext;
use crate::lock_helper::RwLockExt;
use crate::model::wallet::Wallet;
use dash_sdk::dashcore_rpc::RpcApi;
use dash_sdk::dpp::dashcore::hashes::Hash;
Expand Down Expand Up @@ -42,10 +43,7 @@ impl AppContext {
});
}

let client = self
.core_client
.read()
.expect("Core client lock was poisoned");
let client = self.core_client.read_or_recover();

let mut recovered_count = 0;
let mut total_amount = 0u64;
Expand Down
11 changes: 3 additions & 8 deletions src/backend_task/core/refresh_single_key_wallet_info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Refresh Single Key Wallet Info - Reload UTXOs and balances for a single key wallet

use crate::context::AppContext;
use crate::lock_helper::RwLockExt;
use crate::model::wallet::single_key::SingleKeyWallet;
use dash_sdk::dashcore_rpc::RpcApi;
use dash_sdk::dpp::dashcore::{OutPoint, TxOut};
Expand All @@ -21,10 +22,7 @@ impl AppContext {

// Step 2: Import address to Core (needed for UTXO queries)
{
let client = self
.core_client
.read()
.expect("Core client lock was poisoned");
let client = self.core_client.read_or_recover();

if let Err(e) = client.import_address(&address, None, Some(false)) {
tracing::debug!(?e, address = %address, "import_address failed during single key refresh");
Expand All @@ -33,10 +31,7 @@ impl AppContext {

// Step 3: Get UTXOs for this address
let utxo_map = {
let client = self
.core_client
.read()
.expect("Core client lock was poisoned");
let client = self.core_client.read_or_recover();

let utxos = client
.list_unspent(Some(0), None, Some(&[&address]), None, None)
Expand Down
21 changes: 5 additions & 16 deletions src/backend_task/core/refresh_wallet_info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::backend_task::BackendTaskSuccessResult;
use crate::context::AppContext;
use crate::lock_helper::RwLockExt;
use crate::model::wallet::{DerivationPathHelpers, Wallet};
use dash_sdk::dashcore_rpc::RpcApi;
use dash_sdk::dpp::dashcore::hashes::Hash;
Expand Down Expand Up @@ -37,10 +38,7 @@ impl AppContext {

// Step 2: Import addresses to Core (no wallet lock needed)
{
let client = self
.core_client
.read()
.expect("Core client lock was poisoned");
let client = self.core_client.read_or_recover();

for address in &addresses {
if let Err(e) = client.import_address(address, None, Some(false)) {
Expand All @@ -51,10 +49,7 @@ impl AppContext {

// Step 3: Fetch UTXOs from Core RPC (no wallet lock needed)
let utxo_map: HashMap<OutPoint, TxOut> = {
let client = self
.core_client
.read()
.expect("Core client lock was poisoned");
let client = self.core_client.read_or_recover();

// Get UTXOs for all addresses
let utxos = if addresses.is_empty() {
Expand Down Expand Up @@ -96,10 +91,7 @@ impl AppContext {
// Step 5: Fetch total received for each address from Core RPC (no wallet lock)
let mut total_received_map: HashMap<Address, u64> = HashMap::new();
{
let client = self
.core_client
.read()
.expect("Core client lock was poisoned");
let client = self.core_client.read_or_recover();

for address in &addresses {
match client.get_received_by_address(address, None) {
Expand All @@ -119,10 +111,7 @@ impl AppContext {

// Step 6: Check which asset locks are stale (no wallet lock needed)
let stale_txids: Vec<_> = {
let client = self
.core_client
.read()
.expect("Core client lock was poisoned");
let client = self.core_client.read_or_recover();

asset_lock_txs
.iter()
Expand Down
4 changes: 2 additions & 2 deletions src/backend_task/core/send_single_key_wallet_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::backend_task::BackendTaskSuccessResult;
use crate::backend_task::core::WalletPaymentRequest;
use crate::context::AppContext;
use crate::lock_helper::RwLockExt;
use crate::model::wallet::single_key::SingleKeyWallet;
use dash_sdk::dashcore_rpc::RpcApi;
use dash_sdk::dashcore_rpc::dashcore::{Address, OutPoint, ScriptBuf, Transaction, TxIn, TxOut};
Expand Down Expand Up @@ -197,8 +198,7 @@ impl AppContext {
// Broadcast transaction
let txid = self
.core_client
.read()
.expect("Core client lock was poisoned")
.read_or_recover()
.send_raw_transaction(&tx)
.map_err(|e| format!("Failed to broadcast transaction: {}", e))?;

Expand Down
11 changes: 6 additions & 5 deletions src/backend_task/identity/load_identity.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::BackendTaskSuccessResult;
use crate::backend_task::identity::{IdentityInputToLoad, verify_key_input};
use crate::context::AppContext;
use crate::lock_helper::RwLockExt;
use crate::model::qualified_identity::PrivateKeyTarget::{
self, PrivateKeyOnMainIdentity, PrivateKeyOnVoterIdentity,
};
Expand Down Expand Up @@ -80,7 +81,7 @@ impl AppContext {

let mut encrypted_private_keys = BTreeMap::new();

let wallets = self.wallets.read().unwrap().clone();
let wallets = self.wallets.read_or_recover().clone();

if identity_type == IdentityType::User
&& derive_keys_from_wallets
Expand Down Expand Up @@ -286,7 +287,7 @@ impl AppContext {
};

let sdk_guard = {
let guard = self.sdk.read().unwrap();
let guard = self.sdk.read_or_recover();
guard.clone()
};

Expand Down Expand Up @@ -339,7 +340,7 @@ impl AppContext {
dpns_names: maybe_owned_dpns_names,
associated_wallets: wallets
.values()
.map(|wallet| (wallet.read().unwrap().seed_hash(), wallet.clone()))
.map(|wallet| (wallet.read_or_recover().seed_hash(), wallet.clone()))
.collect(),
wallet_index: None, //todo
top_ups: Default::default(),
Expand All @@ -355,7 +356,7 @@ impl AppContext {
if let Some((wallet_seed_hash, identity_index)) = wallet_info
&& let Some(wallet_arc) = wallets.get(&wallet_seed_hash)
{
let mut wallet = wallet_arc.write().unwrap();
let mut wallet = wallet_arc.write_or_recover();
wallet
.identities
.insert(identity_index, qualified_identity.identity.clone());
Expand All @@ -377,7 +378,7 @@ impl AppContext {
if wallet_filter.is_some_and(|filter| filter != wallet_seed_hash) {
continue;
}
let mut wallet = wallet_arc.write().unwrap();
let mut wallet = wallet_arc.write_or_recover();
if !wallet.is_open() {
continue;
}
Expand Down
5 changes: 3 additions & 2 deletions src/backend_task/identity/load_identity_by_dpns_name.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::BackendTaskSuccessResult;
use crate::context::AppContext;
use crate::lock_helper::RwLockExt;
use crate::model::qualified_identity::{
DPNSNameInfo, IdentityStatus, IdentityType, QualifiedIdentity,
};
Expand Down Expand Up @@ -138,7 +139,7 @@ impl AppContext {
})
.map_err(|e| format!("Error fetching DPNS names: {}", e))?;

let wallets = self.wallets.read().unwrap().clone();
let wallets = self.wallets.read_or_recover().clone();

// Try to derive keys from wallets if requested
let mut encrypted_private_keys = std::collections::BTreeMap::new();
Expand All @@ -162,7 +163,7 @@ impl AppContext {
dpns_names: owned_dpns_names,
associated_wallets: wallets
.values()
.map(|wallet| (wallet.read().unwrap().seed_hash(), wallet.clone()))
.map(|wallet| (wallet.read_or_recover().seed_hash(), wallet.clone()))
.collect(),
wallet_index: None,
top_ups: Default::default(),
Expand Down
11 changes: 6 additions & 5 deletions src/backend_task/identity/load_identity_from_wallet.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{BackendTaskSuccessResult, IdentityIndex};
use crate::app::TaskResult;
use crate::context::AppContext;
use crate::lock_helper::RwLockExt;
use crate::model::qualified_identity::encrypted_key_storage::{
PrivateKeyData, WalletDerivationPath,
};
Expand Down Expand Up @@ -38,7 +39,7 @@ impl AppContext {

for key_index in 0..AUTH_KEY_LOOKUP_WINDOW {
let public_key = {
let wallet = wallet_arc_ref.wallet.write().unwrap();
let wallet = wallet_arc_ref.wallet.write_or_recover();
wallet.identity_authentication_ecdsa_public_key(
self.network,
identity_index,
Expand Down Expand Up @@ -116,7 +117,7 @@ impl AppContext {
};

let sdk_guard = {
let guard = self.sdk.read().unwrap();
let guard = self.sdk.read_or_recover();
guard.clone()
};

Expand Down Expand Up @@ -162,7 +163,7 @@ impl AppContext {

let wallet_seed_hash;
let (public_key_result_map, public_key_hash_result_map) = {
let mut wallet = wallet_arc_ref.wallet.write().unwrap();
let mut wallet = wallet_arc_ref.wallet.write_or_recover();
wallet_seed_hash = wallet.seed_hash();
wallet.identity_authentication_ecdsa_public_keys_data_map(
self.network,
Expand Down Expand Up @@ -224,7 +225,7 @@ impl AppContext {

let private_keys = private_keys_map.into();

let wallet_seed_hash = wallet_arc_ref.wallet.read().unwrap().seed_hash();
let wallet_seed_hash = wallet_arc_ref.wallet.read_or_recover().seed_hash();

let mut qualified_identity = QualifiedIdentity {
identity: identity.clone(),
Expand Down Expand Up @@ -259,7 +260,7 @@ impl AppContext {
.map_err(|e| format!("Database error: {}", e))?;

{
let mut wallet = wallet_arc_ref.wallet.write().unwrap();
let mut wallet = wallet_arc_ref.wallet.write_or_recover();
wallet
.identities
.insert(identity_index, qualified_identity.identity.clone());
Expand Down
Loading
Loading