X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning%2Futil%2Ferrors.rs;h=4c911a5f2ddd9ce3b6ddfde0e60886a0bbfe33a0;hb=a21177846ed75d66f68a1603f768410f07a65791;hp=526138297a7b1e7e38828c386591aa95ed14f49b;hpb=d9866e870bbe080412dc70b1421e28a756e4407e;p=ldk-c-bindings diff --git a/lightning-c-bindings/src/lightning/util/errors.rs b/lightning-c-bindings/src/lightning/util/errors.rs index 5261382..4c911a5 100644 --- a/lightning-c-bindings/src/lightning/util/errors.rs +++ b/lightning-c-bindings/src/lightning/util/errors.rs @@ -8,28 +8,32 @@ //! Error types live here. -use std::ffi::c_void; +use alloc::str::FromStr; +use core::ffi::c_void; +use core::convert::Infallible; use bitcoin::hashes::Hash; use crate::c_types::*; +#[cfg(feature="no-std")] +use alloc::{vec::Vec, boxed::Box}; /// Indicates an error on the client's part (usually some variant of attempting to use too-low or /// too-high values) -#[must_use] #[derive(Clone)] +#[must_use] #[repr(C)] pub enum APIError { /// Indicates the API was wholly misused (see err for more). Cases where these can be returned /// are documented, but generally indicates some precondition of a function was violated. APIMisuseError { /// A human-readable error message - err: crate::c_types::derived::CVec_u8Z, + err: crate::c_types::Str, }, /// Due to a high feerate, we were unable to complete the request. /// For example, this may be returned if the feerate implies we cannot open a channel at the /// requested value, but opening a larger channel would succeed. FeeRateTooHigh { /// A human-readable error message - err: crate::c_types::derived::CVec_u8Z, + err: crate::c_types::Str, /// The feerate which was too high. feerate: u32, }, @@ -44,13 +48,33 @@ pub enum APIError { /// peer, channel at capacity, channel shutting down, etc. ChannelUnavailable { /// A human-readable error message - err: crate::c_types::derived::CVec_u8Z, + err: crate::c_types::Str, + }, + /// An attempt to call [`chain::Watch::watch_channel`]/[`chain::Watch::update_channel`] + /// returned a [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a + /// monitor update is awaiting async resolution. Once it resolves the attempted action should + /// complete automatically. + /// + /// [`chain::Watch::watch_channel`]: crate::chain::Watch::watch_channel + /// [`chain::Watch::update_channel`]: crate::chain::Watch::update_channel + /// [`ChannelMonitorUpdateStatus::InProgress`]: crate::chain::ChannelMonitorUpdateStatus::InProgress + MonitorUpdateInProgress, + /// [`KeysInterface::get_shutdown_scriptpubkey`] returned a shutdown scriptpubkey incompatible + /// with the channel counterparty as negotiated in [`InitFeatures`]. + /// + /// Using a SegWit v0 script should resolve this issue. If you cannot, you won't be able to open + /// a channel or cooperatively close one with this peer (and will have to force-close instead). + /// + /// [`KeysInterface::get_shutdown_scriptpubkey`]: crate::chain::keysinterface::KeysInterface::get_shutdown_scriptpubkey + /// [`InitFeatures`]: crate::ln::features::InitFeatures + IncompatibleShutdownScript { + /// The incompatible shutdown script. + script: crate::lightning::ln::script::ShutdownScript, }, - /// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the - /// attempted action to fail. - MonitorUpdateFailed, } -use lightning::util::errors::APIError as nativeAPIError; +use lightning::util::errors::APIError as APIErrorImport; +pub(crate) type nativeAPIError = APIErrorImport; + impl APIError { #[allow(unused)] pub(crate) fn to_native(&self) -> nativeAPIError { @@ -58,30 +82,36 @@ impl APIError { APIError::APIMisuseError {ref err, } => { let mut err_nonref = (*err).clone(); nativeAPIError::APIMisuseError { - err: String::from_utf8(err_nonref.into_rust()).unwrap(), + err: err_nonref.into_string(), } }, APIError::FeeRateTooHigh {ref err, ref feerate, } => { let mut err_nonref = (*err).clone(); let mut feerate_nonref = (*feerate).clone(); nativeAPIError::FeeRateTooHigh { - err: String::from_utf8(err_nonref.into_rust()).unwrap(), + err: err_nonref.into_string(), feerate: feerate_nonref, } }, APIError::RouteError {ref err, } => { let mut err_nonref = (*err).clone(); nativeAPIError::RouteError { - err: err_nonref.into(), + err: err_nonref.into_str(), } }, APIError::ChannelUnavailable {ref err, } => { let mut err_nonref = (*err).clone(); nativeAPIError::ChannelUnavailable { - err: String::from_utf8(err_nonref.into_rust()).unwrap(), + err: err_nonref.into_string(), + } + }, + APIError::MonitorUpdateInProgress => nativeAPIError::MonitorUpdateInProgress, + APIError::IncompatibleShutdownScript {ref script, } => { + let mut script_nonref = (*script).clone(); + nativeAPIError::IncompatibleShutdownScript { + script: *unsafe { Box::from_raw(script_nonref.take_inner()) }, } }, - APIError::MonitorUpdateFailed => nativeAPIError::MonitorUpdateFailed, } } #[allow(unused)] @@ -89,26 +119,31 @@ impl APIError { match self { APIError::APIMisuseError {mut err, } => { nativeAPIError::APIMisuseError { - err: String::from_utf8(err.into_rust()).unwrap(), + err: err.into_string(), } }, APIError::FeeRateTooHigh {mut err, mut feerate, } => { nativeAPIError::FeeRateTooHigh { - err: String::from_utf8(err.into_rust()).unwrap(), + err: err.into_string(), feerate: feerate, } }, APIError::RouteError {mut err, } => { nativeAPIError::RouteError { - err: err.into(), + err: err.into_str(), } }, APIError::ChannelUnavailable {mut err, } => { nativeAPIError::ChannelUnavailable { - err: String::from_utf8(err.into_rust()).unwrap(), + err: err.into_string(), + } + }, + APIError::MonitorUpdateInProgress => nativeAPIError::MonitorUpdateInProgress, + APIError::IncompatibleShutdownScript {mut script, } => { + nativeAPIError::IncompatibleShutdownScript { + script: *unsafe { Box::from_raw(script.take_inner()) }, } }, - APIError::MonitorUpdateFailed => nativeAPIError::MonitorUpdateFailed, } } #[allow(unused)] @@ -117,14 +152,14 @@ impl APIError { nativeAPIError::APIMisuseError {ref err, } => { let mut err_nonref = (*err).clone(); APIError::APIMisuseError { - err: err_nonref.into_bytes().into(), + err: err_nonref.into(), } }, nativeAPIError::FeeRateTooHigh {ref err, ref feerate, } => { let mut err_nonref = (*err).clone(); let mut feerate_nonref = (*feerate).clone(); APIError::FeeRateTooHigh { - err: err_nonref.into_bytes().into(), + err: err_nonref.into(), feerate: feerate_nonref, } }, @@ -137,10 +172,16 @@ impl APIError { nativeAPIError::ChannelUnavailable {ref err, } => { let mut err_nonref = (*err).clone(); APIError::ChannelUnavailable { - err: err_nonref.into_bytes().into(), + err: err_nonref.into(), + } + }, + nativeAPIError::MonitorUpdateInProgress => APIError::MonitorUpdateInProgress, + nativeAPIError::IncompatibleShutdownScript {ref script, } => { + let mut script_nonref = (*script).clone(); + APIError::IncompatibleShutdownScript { + script: crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(script_nonref), is_owned: true }, } }, - nativeAPIError::MonitorUpdateFailed => APIError::MonitorUpdateFailed, } } #[allow(unused)] @@ -148,12 +189,12 @@ impl APIError { match native { nativeAPIError::APIMisuseError {mut err, } => { APIError::APIMisuseError { - err: err.into_bytes().into(), + err: err.into(), } }, nativeAPIError::FeeRateTooHigh {mut err, mut feerate, } => { APIError::FeeRateTooHigh { - err: err.into_bytes().into(), + err: err.into(), feerate: feerate, } }, @@ -164,10 +205,15 @@ impl APIError { }, nativeAPIError::ChannelUnavailable {mut err, } => { APIError::ChannelUnavailable { - err: err.into_bytes().into(), + err: err.into(), + } + }, + nativeAPIError::MonitorUpdateInProgress => APIError::MonitorUpdateInProgress, + nativeAPIError::IncompatibleShutdownScript {mut script, } => { + APIError::IncompatibleShutdownScript { + script: crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(script), is_owned: true }, } }, - nativeAPIError::MonitorUpdateFailed => APIError::MonitorUpdateFailed, } } } @@ -179,3 +225,49 @@ pub extern "C" fn APIError_free(this_ptr: APIError) { } pub extern "C" fn APIError_clone(orig: &APIError) -> APIError { orig.clone() } +#[no_mangle] +/// Utility method to constructs a new APIMisuseError-variant APIError +pub extern "C" fn APIError_apimisuse_error(err: crate::c_types::Str) -> APIError { + APIError::APIMisuseError { + err, + } +} +#[no_mangle] +/// Utility method to constructs a new FeeRateTooHigh-variant APIError +pub extern "C" fn APIError_fee_rate_too_high(err: crate::c_types::Str, feerate: u32) -> APIError { + APIError::FeeRateTooHigh { + err, + feerate, + } +} +#[no_mangle] +/// Utility method to constructs a new RouteError-variant APIError +pub extern "C" fn APIError_route_error(err: crate::c_types::Str) -> APIError { + APIError::RouteError { + err, + } +} +#[no_mangle] +/// Utility method to constructs a new ChannelUnavailable-variant APIError +pub extern "C" fn APIError_channel_unavailable(err: crate::c_types::Str) -> APIError { + APIError::ChannelUnavailable { + err, + } +} +#[no_mangle] +/// Utility method to constructs a new MonitorUpdateInProgress-variant APIError +pub extern "C" fn APIError_monitor_update_in_progress() -> APIError { + APIError::MonitorUpdateInProgress} +#[no_mangle] +/// Utility method to constructs a new IncompatibleShutdownScript-variant APIError +pub extern "C" fn APIError_incompatible_shutdown_script(script: crate::lightning::ln::script::ShutdownScript) -> APIError { + APIError::IncompatibleShutdownScript { + script, + } +} +/// Checks if two APIErrors contain equal inner contents. +/// This ignores pointers and is_owned flags and looks at the values in fields. +#[no_mangle] +pub extern "C" fn APIError_eq(a: &APIError, b: &APIError) -> bool { + if &a.to_native() == &b.to_native() { true } else { false } +}