From 126bc61df605f91d0f00ecaa8f7d81beba902e2b Mon Sep 17 00:00:00 2001 From: Matthew Date: Wed, 17 Apr 2024 11:45:20 -0500 Subject: [PATCH] feat: add apply_update related error --- bdk-ffi/src/bdk.udl | 7 ++++++- bdk-ffi/src/error.rs | 15 ++++++++++++++- bdk-ffi/src/lib.rs | 1 + bdk-ffi/src/wallet.rs | 10 ++++++---- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/bdk-ffi/src/bdk.udl b/bdk-ffi/src/bdk.udl index a781d47..29db64c 100644 --- a/bdk-ffi/src/bdk.udl +++ b/bdk-ffi/src/bdk.udl @@ -130,6 +130,11 @@ interface ExtractTxError { OtherExtractTransactionError(); }; +[Error] +interface CannotConnectError { + Include(u32 height); +}; + // ------------------------------------------------------------------------ // bdk crate - types module // ------------------------------------------------------------------------ @@ -226,7 +231,7 @@ interface Wallet { Balance get_balance(); - [Throws=Alpha3Error] + [Throws=CannotConnectError] void apply_update(Update update); boolean is_mine([ByRef] Script script); diff --git a/bdk-ffi/src/error.rs b/bdk-ffi/src/error.rs index 622e9fb..a9d8359 100644 --- a/bdk-ffi/src/error.rs +++ b/bdk-ffi/src/error.rs @@ -120,6 +120,12 @@ pub enum TxidParseError { InvalidTxid { txid: String }, } +#[derive(Debug, thiserror::Error)] +pub enum CannotConnectError { + #[error("cannot include height: {height}")] + Include { height: u32 }, +} + #[derive(Debug, thiserror::Error)] pub enum AddressError { // Errors coming from the ParseError enum @@ -575,7 +581,7 @@ impl From for ExtractTxError { } #[cfg(test)] mod test { - use crate::error::{EsploraError, PersistenceError, WalletCreationError}; + use crate::error::{CannotConnectError, EsploraError, PersistenceError, WalletCreationError}; use crate::CalculateFeeError; use crate::OutPoint; use crate::SignerError; @@ -793,4 +799,11 @@ mod test { assert_eq!(error.to_string(), message); } } + + #[test] + fn test_cannot_connect_error_include() { + let error = CannotConnectError::Include { height: 42 }; + + assert_eq!(format!("{}", error), "cannot include height: 42"); + } } diff --git a/bdk-ffi/src/lib.rs b/bdk-ffi/src/lib.rs index f995e1d..d9c6ee8 100644 --- a/bdk-ffi/src/lib.rs +++ b/bdk-ffi/src/lib.rs @@ -16,6 +16,7 @@ use crate::descriptor::Descriptor; use crate::error::AddressError; use crate::error::Alpha3Error; use crate::error::CalculateFeeError; +use crate::error::CannotConnectError; use crate::error::DescriptorError; use crate::error::EsploraError; use crate::error::ExtractTxError; diff --git a/bdk-ffi/src/wallet.rs b/bdk-ffi/src/wallet.rs index a37c34d..7835c2c 100644 --- a/bdk-ffi/src/wallet.rs +++ b/bdk-ffi/src/wallet.rs @@ -1,8 +1,8 @@ use crate::bitcoin::{OutPoint, Psbt, Script, Transaction}; use crate::descriptor::Descriptor; use crate::error::{ - Alpha3Error, CalculateFeeError, PersistenceError, SignerError, TxidParseError, - WalletCreationError, + Alpha3Error, CalculateFeeError, CannotConnectError, PersistenceError, SignerError, + TxidParseError, WalletCreationError, }; use crate::types::{ AddressIndex, AddressInfo, Balance, CanonicalTx, FeeRate, LocalOutput, ScriptAmount, @@ -58,10 +58,12 @@ impl Wallet { .into() } - pub fn apply_update(&self, update: Arc) -> Result<(), Alpha3Error> { + pub fn apply_update(&self, update: Arc) -> Result<(), CannotConnectError> { self.get_wallet() .apply_update(update.0.clone()) - .map_err(|_| Alpha3Error::Generic) + .map_err(|e| CannotConnectError::Include { + height: e.try_include_height, + }) } // TODO: This is the fallible version of get_internal_address; should I rename it to get_internal_address?