Skip to content

Commit 3e287fa

Browse files
committed
lsp_plugin: use concrete type to avoid unwrap
This commit adds a custom request type for the on_openchannel hook to avoid calling unwrap() during runtime. We now return cleanly from the hook in any case. Signed-off-by: Peter Neuroth <[email protected]>
1 parent 1abbddd commit 3e287fa

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

plugins/lsps-plugin/src/client.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use cln_lsps::lsps0::{
99
};
1010
use cln_lsps::lsps2::cln::tlv::encode_tu64;
1111
use cln_lsps::lsps2::cln::{
12-
HtlcAcceptedRequest, HtlcAcceptedResponse, InvoicePaymentRequest, TLV_FORWARD_AMT,
13-
TLV_PAYMENT_SECRET,
12+
HtlcAcceptedRequest, HtlcAcceptedResponse, InvoicePaymentRequest, OpenChannelRequest,
13+
TLV_FORWARD_AMT, TLV_PAYMENT_SECRET,
1414
};
1515
use cln_lsps::lsps2::model::{
1616
compute_opening_fee, Lsps2BuyRequest, Lsps2BuyResponse, Lsps2GetInfoRequest,
@@ -636,15 +636,11 @@ async fn on_openchannel(
636636
p: cln_plugin::Plugin<State>,
637637
v: serde_json::Value,
638638
) -> Result<serde_json::Value, anyhow::Error> {
639-
#[derive(Deserialize)]
640-
struct Request {
641-
id: String,
642-
}
639+
let req: OpenChannelRequest = ok_or_continue!(
640+
serde_json::from_value(v).context("failed to deserialize open_channel request JSON")
641+
);
643642

644-
let req: Request = ok_or_continue!(serde_json::from_value(
645-
v.get("openchannel").unwrap().clone()
646-
)
647-
.context("failed to deserialize open_channel request JSON"));
643+
// Fixme: Check that channel parameters are as negotiated.
648644

649645
let dir = p.configuration().lightning_dir;
650646
let rpc_path = Path::new(&dir).join(&p.configuration().rpc_file);
@@ -656,7 +652,7 @@ async fn on_openchannel(
656652
key: Some(vec![
657653
"lsps".to_string(),
658654
"client".to_string(),
659-
req.id.clone(),
655+
req.openchannel.id.clone(),
660656
]),
661657
};
662658
let ds_res = ok_or_continue!(cln_client
@@ -665,10 +661,17 @@ async fn on_openchannel(
665661
.context("failed to get datastore record"));
666662

667663
if let Some(_rec) = ds_res.datastore.iter().next() {
668-
info!("Allowing zero-conf channel from LSP {}", &req.id);
664+
info!(
665+
"Allowing zero-conf channel from LSP {}",
666+
&req.openchannel.id
667+
);
669668
let ds_req = DeldatastoreRequest {
670669
generation: None,
671-
key: vec!["lsps".to_string(), "client".to_string(), req.id.clone()],
670+
key: vec![
671+
"lsps".to_string(),
672+
"client".to_string(),
673+
req.openchannel.id.clone(),
674+
],
672675
};
673676
if let Some(err) = cln_client.call_typed(&ds_req).await.err() {
674677
// We can do nothing but report that there was an issue deleting the

plugins/lsps-plugin/src/lsps2/cln.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,27 @@ pub struct InvoicePaymentRequestPayment {
128128
pub preimage: String,
129129
pub msat: u64,
130130
}
131+
132+
#[derive(Debug, Deserialize)]
133+
pub struct OpenChannelRequest {
134+
pub openchannel: OpenChannelRequestOpenChannel,
135+
}
136+
137+
#[derive(Debug, Deserialize)]
138+
pub struct OpenChannelRequestOpenChannel {
139+
pub id: String,
140+
pub funding_msat: u64,
141+
pub push_msat: u64,
142+
pub dust_limit_msat: u64,
143+
pub max_htlc_value_in_flight_msat: u64,
144+
pub channel_reserve_msat: u64,
145+
pub htlc_minimum_msat: u64,
146+
pub feerate_per_kw: u32,
147+
pub to_self_delay: u32,
148+
pub max_accepted_htlcs: u32,
149+
pub channel_flags: u64,
150+
}
151+
131152
/// Deserializes a lowercase hex string to a `Vec<u8>`.
132153
pub fn from_hex<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
133154
where

0 commit comments

Comments
 (0)