diff --git a/libs/gl-plugin/src/lsp.rs b/libs/gl-plugin/src/lsp.rs index c7adeb440..509211c98 100644 --- a/libs/gl-plugin/src/lsp.rs +++ b/libs/gl-plugin/src/lsp.rs @@ -54,27 +54,35 @@ struct HtlcAcceptedResponse { const TLV_FORWARD_AMT: u64 = 2; const TLV_PAYMENT_SECRET: u64 = 8; +/// A macro to break out of the current hook flow and return a `continue` +/// signal to core-lightning. This is to be used when we don't know how to +/// handle a given payload or as a shortcut in case we could identify that the +/// incoming htlc is not part of a LSP jit channel opening. +macro_rules! unwrap_or_continue { + ($res:expr) => { + match $res { + Ok(x) => x, + Err(e) => { + log::debug!("Lsp-plugin continue, reason: {}", e.to_string()); + return Ok(serde_json::to_value(HtlcAcceptedResponse { + result: "continue".to_string(), + ..Default::default() + }) + .expect("Could not serialize json value")); + } + } + }; +} + pub async fn on_htlc_accepted(plugin: Plugin, v: Value) -> Result { - let req: HtlcAcceptedRequest = serde_json::from_value(v).unwrap(); + let req: HtlcAcceptedRequest = unwrap_or_continue!(serde_json::from_value(v)); log::debug!("Decoded {:?}", &req); let htlc_amt = req.htlc.amount_msat; - let onion_amt = match req.onion.forward_msat { - Some(a) => a, - None => { - // An onion payload without an `amt_to_forward` is unorthodox and - // can not be processed by this plugin. Skip it. - log::debug!( - "lsp-plugin: got an onion payload={} without an amt forward_msat.", - req.onion.payload - ); - return Ok(serde_json::to_value(HtlcAcceptedResponse { - result: "continue".to_string(), - ..Default::default() - }) - .unwrap()); - } - }; + let onion_amt = unwrap_or_continue!(req.onion.forward_msat.ok_or(format!( + "payload={} is missing forward_msat", + &req.onion.payload + ))); let res = if htlc_amt.msat() < onion_amt.msat() { log::info!( @@ -85,7 +93,10 @@ pub async fn on_htlc_accepted(plugin: Plugin, v: Value) -> Result Result