diff --git a/src/event.rs b/src/event.rs index 6197dc111..759ed05a1 100644 --- a/src/event.rs +++ b/src/event.rs @@ -548,7 +548,8 @@ where } LdkEvent::SpendableOutputs { outputs } => { // TODO: We should eventually remember the outputs and supply them to the wallet's coin selection, once BDK allows us to do so. - let destination_address = self.wallet.get_new_address().unwrap(); + let destination_address = + self.wallet.get_new_address().expect("Failed to get destination address"); let output_descriptors = &outputs.iter().collect::>(); let tx_feerate = self.wallet.get_est_sat_per_1000_weight(ConfirmationTarget::Normal); diff --git a/src/lib.rs b/src/lib.rs index 0d6213512..94453ead8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -839,7 +839,9 @@ impl Node { /// /// **Note:** This **MUST** be called after each event has been handled. pub fn event_handled(&self) { - self.event_queue.event_handled().unwrap(); + self.event_queue + .event_handled() + .expect("Couldn't mark event handled due to persistence failure"); } /// Returns our own node id @@ -922,23 +924,12 @@ impl Node { let con_peer_pubkey = peer_info.pubkey; let con_peer_addr = peer_info.address; - let con_success = Arc::new(AtomicBool::new(false)); - let con_success_cloned = Arc::clone(&con_success); let con_logger = Arc::clone(&self.logger); let con_pm = Arc::clone(&self.peer_manager); - tokio::task::block_in_place(move || { - runtime.block_on(async move { - let res = - connect_peer_if_necessary(con_peer_pubkey, con_peer_addr, con_pm, con_logger) - .await; - con_success_cloned.store(res.is_ok(), Ordering::Release); - }) - }); - - if !con_success.load(Ordering::Acquire) { - return Err(Error::ConnectionFailed); - } + runtime.block_on(async move { + connect_peer_if_necessary(con_peer_pubkey, con_peer_addr, con_pm, con_logger).await + })?; log_info!(self.logger, "Connected to peer {}@{}. ", peer_info.pubkey, peer_info.address,); @@ -1001,23 +992,12 @@ impl Node { let con_peer_pubkey = peer_info.pubkey; let con_peer_addr = peer_info.address; - let con_success = Arc::new(AtomicBool::new(false)); - let con_success_cloned = Arc::clone(&con_success); let con_logger = Arc::clone(&self.logger); let con_pm = Arc::clone(&self.peer_manager); - tokio::task::block_in_place(move || { - runtime.block_on(async move { - let res = - connect_peer_if_necessary(con_peer_pubkey, con_peer_addr, con_pm, con_logger) - .await; - con_success_cloned.store(res.is_ok(), Ordering::Release); - }) - }); - - if !con_success.load(Ordering::Acquire) { - return Err(Error::ConnectionFailed); - } + runtime.block_on(async move { + connect_peer_if_necessary(con_peer_pubkey, con_peer_addr, con_pm, con_logger).await + })?; let user_config = UserConfig { channel_handshake_limits: ChannelHandshakeLimits { @@ -1070,56 +1050,56 @@ impl Node { let wallet = Arc::clone(&self.wallet); let tx_sync = Arc::clone(&self.tx_sync); - let sync_cman = Arc::clone(&self.channel_manager); - let sync_cmon = Arc::clone(&self.chain_monitor); let sync_logger = Arc::clone(&self.logger); - let confirmables = vec![ - &*sync_cman as &(dyn Confirm + Sync + Send), - &*sync_cmon as &(dyn Confirm + Sync + Send), - ]; - tokio::task::block_in_place(move || { - tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on( - async move { - let now = Instant::now(); - match wallet.sync().await { - Ok(()) => { - log_info!( - sync_logger, - "Sync of on-chain wallet finished in {}ms.", - now.elapsed().as_millis() - ); - Ok(()) - } - Err(e) => { - log_error!(sync_logger, "Sync of on-chain wallet failed: {}", e); - Err(e) - } - } - }, - ) + let local_runtime = + tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap(); + local_runtime.block_on(async move { + let now = Instant::now(); + match wallet.sync().await { + Ok(()) => { + log_info!( + sync_logger, + "Sync of on-chain wallet finished in {}ms.", + now.elapsed().as_millis() + ); + Ok(()) + } + Err(e) => { + log_error!(sync_logger, "Sync of on-chain wallet failed: {}", e); + Err(e) + } + } })?; + let sync_cman = Arc::clone(&self.channel_manager); + let sync_cmon = Arc::clone(&self.chain_monitor); let sync_logger = Arc::clone(&self.logger); - tokio::task::block_in_place(move || { - runtime.block_on(async move { - let now = Instant::now(); - match tx_sync.sync(confirmables).await { - Ok(()) => { - log_info!( - sync_logger, - "Sync of Lightning wallet finished in {}ms.", - now.elapsed().as_millis() - ); - Ok(()) - } - Err(e) => { - log_error!(sync_logger, "Sync of Lightning wallet failed: {}", e); - Err(e) - } + let (sender, receiver) = std::sync::mpsc::sync_channel(1); + + runtime.spawn(async move { + let confirmables = vec![ + &*sync_cman as &(dyn Confirm + Sync + Send), + &*sync_cmon as &(dyn Confirm + Sync + Send), + ]; + let now = Instant::now(); + match tx_sync.sync(confirmables).await { + Ok(()) => { + log_info!( + sync_logger, + "Sync of Lightning wallet finished in {}ms.", + now.elapsed().as_millis() + ); + let _ = sender.send(Ok(())); } - }) - })?; + Err(e) => { + log_error!(sync_logger, "Sync of Lightning wallet failed: {}", e); + let _ = sender.send(Err(e)); + } + } + }); + + receiver.recv().map_err(|_| Error::TxSyncFailed)??; Ok(()) } diff --git a/src/logger.rs b/src/logger.rs index 6ae4cc4bd..f00ea8878 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -15,7 +15,7 @@ pub(crate) struct FilesystemLogger { impl FilesystemLogger { pub(crate) fn new(file_path: String) -> Self { if let Some(parent_dir) = Path::new(&file_path).parent() { - fs::create_dir_all(parent_dir).unwrap(); + fs::create_dir_all(parent_dir).expect("Failed to create log parent directory"); } Self { file_path } } @@ -35,8 +35,8 @@ impl Logger for FilesystemLogger { .create(true) .append(true) .open(self.file_path.clone()) - .unwrap() + .expect("Failed to open log file") .write_all(log.as_bytes()) - .unwrap(); + .expect("Failed to write to log file") } } diff --git a/src/types.rs b/src/types.rs index 8aa77dbbd..7aca197fa 100644 --- a/src/types.rs +++ b/src/types.rs @@ -283,7 +283,7 @@ impl UniffiCustomTypeConverter for Network { impl UniffiCustomTypeConverter for Txid { type Builtin = String; fn into_custom(val: Self::Builtin) -> uniffi::Result { - Ok(Txid::from_str(&val).unwrap()) + Ok(Txid::from_str(&val)?) } fn from_custom(obj: Self) -> Self::Builtin {