From d3be02c94b393325982d755ebf4062c25d8f2d81 Mon Sep 17 00:00:00 2001 From: Gregory Hill Date: Mon, 21 Aug 2023 13:24:09 +0100 Subject: [PATCH] chore: convert subxt errors to a human readable format Signed-off-by: Gregory Hill --- runtime/src/error.rs | 12 ++++++ runtime/src/lib.rs | 1 - runtime/src/rpc.rs | 12 +++--- runtime/src/types.rs | 4 +- vault/src/connection_manager.rs | 6 +-- vault/src/error.rs | 9 +++++ vault/src/issue.rs | 10 ++--- vault/src/metrics.rs | 2 +- vault/src/redeem.rs | 4 +- vault/src/refund.rs | 68 --------------------------------- vault/src/replace.rs | 10 ++--- vault/src/system.rs | 9 +++-- 12 files changed, 50 insertions(+), 97 deletions(-) delete mode 100644 vault/src/refund.rs diff --git a/runtime/src/error.rs b/runtime/src/error.rs index 4ad5d6692..aec11e1f2 100644 --- a/runtime/src/error.rs +++ b/runtime/src/error.rs @@ -105,6 +105,18 @@ impl From for Error { } impl Error { + pub fn to_human(self) -> String { + match self { + Error::SubxtRuntimeError(SubxtError::Runtime(DispatchError::Module(module_error))) => { + match module_error.as_root_error::() { + Ok(root_error) => format!("{:?}", root_error), + Err(_) => format!("Unknown error: {:?}", module_error.raw()), + } + } + err => err.to_string(), + } + } + pub fn is_any_module_err(&self) -> bool { matches!( self, diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index d5dd78e0a..533c692a0 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -164,7 +164,6 @@ pub const DISABLE_DIFFICULTY_CHECK: &str = "DisableDifficultyCheck"; ), ) )] - pub mod metadata {} #[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Default, Clone, Decode, Encode)] diff --git a/runtime/src/rpc.rs b/runtime/src/rpc.rs index f9b7e43dc..1594c3347 100644 --- a/runtime/src/rpc.rs +++ b/runtime/src/rpc.rs @@ -392,14 +392,14 @@ impl InterBtcParachain { /// /// # Arguments /// * `on_error` - callback for decoding errors, is not allowed to take too long - pub async fn on_event_error(&self, on_error: E) -> Result<(), Error> { + pub async fn on_event_error(&self, on_error: E) -> Result<(), Error> { let mut sub = self.subscribe_events().await?; loop { match sub.next().await { - Some(Err(err)) => on_error(err), // report error - Some(Ok(_)) => {} // do nothing - None => break Ok(()), // end of stream + Some(Err(err)) => on_error(err.into()), // report error + Some(Ok(_)) => {} // do nothing + None => break Ok(()), // end of stream } } } @@ -420,7 +420,7 @@ impl InterBtcParachain { T: StaticEvent + core::fmt::Debug, F: FnMut(T) -> R, R: Future, - E: Fn(SubxtError), + E: Fn(Error), { let mut sub = self.subscribe_events().await?; let (tx, mut rx) = futures::channel::mpsc::channel::(32); @@ -442,7 +442,7 @@ impl InterBtcParachain { break; } } - Err(err) => on_error(err), + Err(err) => on_error(err.into()), } } } diff --git a/runtime/src/types.rs b/runtime/src/types.rs index 3c169bb15..edf0f4506 100644 --- a/runtime/src/types.rs +++ b/runtime/src/types.rs @@ -1,7 +1,7 @@ use crate::{ metadata, utils::{account_id::AccountId32, signer::PairSigner}, - Config, InterBtcRuntime, RuntimeCurrencyInfo, + Config, InterBtcRuntime, RuntimeCurrencyInfo, SS58_PREFIX, }; pub use currency_id::CurrencyIdExt; pub use h256_le::RichH256Le; @@ -211,7 +211,7 @@ mod account_id { use sp_core::crypto::Ss58Codec; impl PrettyPrint for AccountId { fn pretty_print(&self) -> String { - self.0.to_ss58check() + self.0.to_ss58check_with_version(SS58_PREFIX.into()) } } } diff --git a/vault/src/connection_manager.rs b/vault/src/connection_manager.rs index d24b69ca5..070edc9c8 100644 --- a/vault/src/connection_manager.rs +++ b/vault/src/connection_manager.rs @@ -136,12 +136,12 @@ impl ConnectionManager { ); match service.start().await { - Err(err @ backoff::Error::Permanent(_)) => { + Err(backoff::Error::Permanent(err)) => { tracing::warn!("Disconnected: {}", err); return Err(err.into()); } - Err(err) => { - tracing::warn!("Disconnected: {}", err); + Err(backoff::Error::Transient(err)) => { + tracing::warn!("Disconnected: {}", err.to_human()); } _ => { tracing::warn!("Disconnected"); diff --git a/vault/src/error.rs b/vault/src/error.rs index 6a067c129..315a5f4ee 100644 --- a/vault/src/error.rs +++ b/vault/src/error.rs @@ -62,6 +62,15 @@ pub enum Error { IoError(#[from] IoError), } +impl Error { + pub fn to_human(self) -> String { + match self { + Self::RuntimeError(runtime_error) => runtime_error.to_human(), + err => err.to_string(), + } + } +} + impl From> for Error { fn from(err: backoff::Error) -> Self { match err { diff --git a/vault/src/issue.rs b/vault/src/issue.rs index b5413418f..9bc8dea54 100644 --- a/vault/src/issue.rs +++ b/vault/src/issue.rs @@ -66,7 +66,7 @@ pub async fn process_issue_requests( random_delay.clone(), ) .map_err(|e| { - tracing::warn!("Failed to execute issue request: {}", e.to_string()); + tracing::warn!("Failed to execute issue request: {}", e.to_human()); }), ), Err(err) => return Err(err.into()), @@ -382,7 +382,7 @@ pub async fn listen_for_issue_requests( let _ = publish_expected_bitcoin_balance(&vault, btc_parachain.clone()).await; if let Err(e) = add_new_deposit_key(&vault.btc_rpc, *event.issue_id, event.vault_public_key).await { - tracing::error!("Failed to add new deposit key #{}: {}", *event.issue_id, e.to_string()); + tracing::error!("Failed to add new deposit key #{}: {}", *event.issue_id, e.to_human()); } } @@ -393,7 +393,7 @@ pub async fn listen_for_issue_requests( ); issue_set.insert(*event.issue_id, *event.vault_address).await; }, - |error| tracing::error!("Error reading request issue event: {}", error.to_string()), + |error| tracing::error!("Error reading request issue event: {}", error.to_human()), ) .await?; Ok(()) @@ -428,7 +428,7 @@ pub async fn listen_for_issue_executes( tracing::trace!("issue #{} executed, no longer watching", *event.issue_id); issue_set.remove(&event.issue_id).await; }, - |error| tracing::error!("Error reading execute issue event: {}", error.to_string()), + |error| tracing::error!("Error reading execute issue event: {}", error.to_human()), ) .await?; Ok(()) @@ -451,7 +451,7 @@ pub async fn listen_for_issue_cancels( tracing::trace!("issue #{} cancelled, no longer watching", *event.issue_id); issue_set.remove(&event.issue_id).await; }, - |error| tracing::error!("Error reading cancel issue event: {}", error.to_string()), + |error| tracing::error!("Error reading cancel issue event: {}", error.to_human()), ) .await?; Ok(()) diff --git a/vault/src/metrics.rs b/vault/src/metrics.rs index 169adee10..d98bd533a 100644 --- a/vault/src/metrics.rs +++ b/vault/src/metrics.rs @@ -580,7 +580,7 @@ pub async fn monitor_bridge_metrics( } } }, - |error| tracing::error!("Error reading SetExchangeRate event: {}", error.to_string()), + |error| tracing::error!("Error reading SetExchangeRate event: {}", error.to_human()), ) .await?; Ok(()) diff --git a/vault/src/redeem.rs b/vault/src/redeem.rs index 0f8e19bda..ae86729ca 100644 --- a/vault/src/redeem.rs +++ b/vault/src/redeem.rs @@ -64,12 +64,12 @@ pub async fn listen_for_redeem_requests( Err(e) => tracing::error!( "Failed to process redeem request #{}: {}", *event.redeem_id, - e.to_string() + e.to_human() ), } }); }, - |error| tracing::error!("Error reading redeem event: {}", error.to_string()), + |error| tracing::error!("Error reading redeem event: {}", error.to_human()), ) .await?; Ok(()) diff --git a/vault/src/refund.rs b/vault/src/refund.rs deleted file mode 100644 index cf8553b07..000000000 --- a/vault/src/refund.rs +++ /dev/null @@ -1,68 +0,0 @@ -use crate::{execution::*, system::VaultIdManager}; -use runtime::{InterBtcParachain, RequestRefundEvent}; -use crate::service::{spawn_cancelable, ShutdownSender}; - -/// Listen for RequestRefundEvent directed at this vault; upon reception, transfer -/// bitcoin and call execute_refund -/// -/// # Arguments -/// -/// * `parachain_rpc` - the parachain RPC handle -/// * `btc_rpc` - the bitcoin RPC handle -/// * `network` - network the bitcoin network used (i.e. regtest/testnet/mainnet) -/// * `num_confirmations` - the number of bitcoin confirmation to await -/// * `process_refunds` - if true, we will process refund requests -pub async fn listen_for_refund_requests( - shutdown_tx: ShutdownSender, - parachain_rpc: InterBtcParachain, - vault_id_manager: VaultIdManager, - num_confirmations: u32, - process_refunds: bool, - auto_rbf: bool, -) -> Result<(), Error> { - parachain_rpc - .on_event::( - |event| async { - let vault = match vault_id_manager.get_vault(&event.vault_id).await { - Some(x) => x, - None => return, // event not directed at this vault - }; - tracing::info!("Received refund request: {:?}", event); - - if !process_refunds { - tracing::info!("Not processing refund"); - return; - } - - // within this event callback, we captured the arguments of listen_for_refund_requests - // by reference. Since spawn requires static lifetimes, we will need to capture the - // arguments by value rather than by reference, so clone these: - let parachain_rpc = parachain_rpc.clone(); - // Spawn a new task so that we handle these events concurrently - spawn_cancelable(shutdown_tx.subscribe(), async move { - tracing::info!("Executing refund #{:?}", event.refund_id); - // prepare the action that will be executed after the bitcoin transfer - let request = Request::from_refund_request_event(&event); - let result = request - .pay_and_execute(parachain_rpc, vault, num_confirmations, auto_rbf) - .await; - - match result { - Ok(_) => tracing::info!( - "Completed refund request #{} with amount {}", - event.refund_id, - event.amount - ), - Err(e) => tracing::error!( - "Failed to process refund request #{}: {}", - event.refund_id, - e.to_string() - ), - } - }); - }, - |error| tracing::error!("Error reading refund event: {}", error.to_string()), - ) - .await?; - Ok(()) -} diff --git a/vault/src/replace.rs b/vault/src/replace.rs index 2631cc54e..36b31669f 100644 --- a/vault/src/replace.rs +++ b/vault/src/replace.rs @@ -73,12 +73,12 @@ pub async fn listen_for_accept_replace( Err(e) => tracing::error!( "Failed to process accept replace request #{}: {}", *event.replace_id, - e.to_string() + e.to_human() ), } }); }, - |error| tracing::error!("Error reading accept_replace_event: {}", error.to_string()), + |error| tracing::error!("Error reading accept_replace_event: {}", error.to_human()), ) .await?; Ok(()) @@ -133,13 +133,13 @@ pub async fn listen_for_replace_requests( "[{}] Failed to accept replace request from {}: {}", vault_id.pretty_print(), event.old_vault_id.pretty_print(), - e.to_string() + e.to_human() ), } } } }, - |error| tracing::error!("Error reading replace event: {}", error.to_string()), + |error| tracing::error!("Error reading replace event: {}", error.to_human()), ) .await?; Ok(()) @@ -201,7 +201,7 @@ pub async fn listen_for_execute_replace( let _ = event_channel.clone().send(Event::Executed(*event.replace_id)).await; } }, - |error| tracing::error!("Error reading redeem event: {}", error.to_string()), + |error| tracing::error!("Error reading redeem event: {}", error.to_human()), ) .await?; Ok(()) diff --git a/vault/src/system.rs b/vault/src/system.rs index 71c48bcff..156af48d0 100644 --- a/vault/src/system.rs +++ b/vault/src/system.rs @@ -140,7 +140,7 @@ async fn active_block_listener( let _ = issue_tx.clone().send(Event::ParachainBlock(event.block_number)).await; let _ = replace_tx.clone().send(Event::ParachainBlock(event.block_number)).await; }, - |err| tracing::error!("Error (UpdateActiveBlockEvent): {}", err.to_string()), + |err| tracing::error!("Error (UpdateActiveBlockEvent): {}", err.to_human()), ) .await?; Ok(()) @@ -159,7 +159,7 @@ async fn relay_block_listener( let _ = issue_tx.clone().send(Event::BitcoinBlock(event.block_height)).await; let _ = replace_tx.clone().send(Event::BitcoinBlock(event.block_height)).await; }, - |err| tracing::error!("Error (StoreMainChainHeaderEvent): {}", err.to_string()), + |err| tracing::error!("Error (StoreMainChainHeaderEvent): {}", err.to_human()), ) .await?; Ok(()) @@ -311,6 +311,7 @@ impl VaultIdManager { // issue keys should be imported separately but we need to iterate // through currency specific wallets to get change addresses for address in btc_rpc.list_addresses()? { + tracing::info!("Found {:?}", address); // get private key from currency specific wallet let private_key = btc_rpc.dump_private_key(&address)?; // import key into main wallet @@ -367,7 +368,7 @@ impl VaultIdManager { let _ = self.add_vault_id(vault_id).await; } }, - |err| tracing::error!("Error (RegisterVaultEvent): {}", err.to_string()), + |err| tracing::error!("Error (RegisterVaultEvent): {}", err.to_human()), ) .await?) } @@ -619,7 +620,7 @@ impl VaultService { // NOTE: this will block if subsequent errors do not trigger shutdown let err_listener = wait_or_shutdown(self.shutdown.clone(), async move { err_provider - .on_event_error(|e| tracing::debug!("Received error event: {}", e)) + .on_event_error(|e| tracing::debug!("Received error event: {}", e.to_human())) .await?; Ok::<_, Error>(()) });