Skip to content

Commit 764974c

Browse files
committed
lsps: Add service implementation for LSPS0
Implements the LSPS0 service plugin for core lightning Changelog-Added: lsps-plugin: lsps0 service support Signed-off-by: Peter Neuroth <[email protected]>
1 parent 51016a5 commit 764974c

File tree

4 files changed

+134
-2
lines changed

4 files changed

+134
-2
lines changed

plugins/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ recklessrpc
2121
exposesecret
2222
cln-xpay
2323
cln-lsps-client
24+
cln-lsps-service

plugins/Makefile

+6-2
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,10 @@ plugins/clnrest: target/${RUST_PROFILE}/clnrest
149149
@cp $< $@
150150
plugins/cln-lsps-client: target/${RUST_PROFILE}/cln-lsps-client
151151
@cp $< $@
152+
plugins/cln-lsps-service: target/${RUST_PROFILE}/cln-lsps-service
153+
@cp $< $@
152154

153-
PLUGINS += plugins/cln-grpc plugins/clnrest plugins/cln-lsps-client
155+
PLUGINS += plugins/cln-grpc plugins/clnrest plugins/cln-lsps-client plugins/cln-lsps-service
154156
endif
155157

156158
PLUGIN_COMMON_OBJS := \
@@ -310,10 +312,12 @@ target/${RUST_PROFILE}/clnrest: ${CLN_REST_PLUGIN_SRC}
310312
cargo build ${CARGO_OPTS} --bin clnrest
311313
target/${RUST_PROFILE}/cln-lsps-client: ${CLN_LSPS_PLUGIN_SRC}
312314
cargo build ${CARGO_OPTS} --bin cln-lsps-client
315+
target/${RUST_PROFILE}/cln-lsps-service: ${CLN_LSPS_PLUGIN_SRC}
316+
cargo build ${CARGO_OPTS} --bin cln-lsps-service
313317

314318
ifneq ($(RUST),0)
315319
include plugins/rest-plugin/Makefile
316-
DEFAULT_TARGETS += $(CLN_PLUGIN_EXAMPLES) plugins/cln-grpc plugins/clnrest plugins/cln-lsps-client
320+
DEFAULT_TARGETS += $(CLN_PLUGIN_EXAMPLES) plugins/cln-grpc plugins/clnrest plugins/cln-lsps-client plugins/cln-lsps-service
317321
endif
318322

319323
clean: plugins-clean

plugins/lsps-plugin/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ edition = "2021"
77
name = "cln-lsps-client"
88
path = "src/client.rs"
99

10+
[[bin]]
11+
name = "cln-lsps-service"
12+
path = "src/service.rs"
13+
1014
[dependencies]
1115
anyhow = "1.0"
1216
async-trait = "0.1"

plugins/lsps-plugin/src/service.rs

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
use anyhow::anyhow;
2+
use async_trait::async_trait;
3+
use cln_lsps::jsonrpc::server::{JsonRpcResponseWriter, RequestHandler};
4+
use cln_lsps::jsonrpc::{server::JsonRpcServer, JsonRpcRequest};
5+
use cln_lsps::jsonrpc::{JsonRpcResponse, RequestObject, RpcError, TransportError};
6+
use cln_lsps::lsps0;
7+
use cln_lsps::lsps0::model::{Lsps0listProtocolsRequest, Lsps0listProtocolsResponse};
8+
use cln_lsps::lsps0::transport::{self, CustomMsg};
9+
use cln_plugin::options::ConfigOption;
10+
use cln_plugin::{options, Plugin};
11+
use cln_rpc::notifications::CustomMsgNotification;
12+
use cln_rpc::primitives::PublicKey;
13+
use log::debug;
14+
use std::path::{Path, PathBuf};
15+
use std::str::FromStr;
16+
use std::sync::Arc;
17+
18+
/// An option to enable this service. It defaults to `false` as we don't want a
19+
/// node to be an LSP per default.
20+
/// If a user want's to run an LSP service on their node this has to explicitly
21+
/// set to true.
22+
const OPTION_ENABLED: options::DefaultBooleanConfigOption = ConfigOption::new_bool_with_default(
23+
"lsps-service",
24+
false,
25+
"Enables an LSPS service on the node.",
26+
);
27+
28+
#[derive(Clone)]
29+
struct State {
30+
lsps_service: JsonRpcServer,
31+
}
32+
33+
#[tokio::main]
34+
async fn main() -> Result<(), anyhow::Error> {
35+
let lsps_service = JsonRpcServer::builder()
36+
.with_handler(
37+
Lsps0listProtocolsRequest::METHOD.to_string(),
38+
Arc::new(Lsps0ListProtocolsHandler),
39+
)
40+
.build();
41+
let state = State { lsps_service };
42+
43+
if let Some(plugin) = cln_plugin::Builder::new(tokio::io::stdin(), tokio::io::stdout())
44+
.option(OPTION_ENABLED)
45+
.hook("custommsg", on_custommsg)
46+
.configure()
47+
.await?
48+
{
49+
if !plugin.option(&OPTION_ENABLED)? {
50+
return plugin.disable("`lsps-service` not enabled").await;
51+
}
52+
53+
let plugin = plugin.start(state).await?;
54+
plugin.join().await
55+
} else {
56+
Ok(())
57+
}
58+
}
59+
60+
async fn on_custommsg(
61+
p: Plugin<State>,
62+
v: serde_json::Value,
63+
) -> Result<serde_json::Value, anyhow::Error> {
64+
// All of this could be done async if needed.
65+
let continue_response = Ok(serde_json::json!({
66+
"result": "continue"
67+
}));
68+
let msg: CustomMsgNotification =
69+
serde_json::from_value(v).map_err(|e| anyhow!("invalid custommsg: {e}"))?;
70+
71+
let req = CustomMsg::from_str(&msg.payload).map_err(|e| anyhow!("invalid payload {e}"))?;
72+
if req.message_type != lsps0::transport::LSPS0_MESSAGE_TYPE {
73+
// We don't care if this is not for us!
74+
return continue_response;
75+
}
76+
77+
let dir = p.configuration().lightning_dir;
78+
let rpc_path = Path::new(&dir).join(&p.configuration().rpc_file);
79+
let mut writer = LspsResponseWriter {
80+
peer_id: msg.peer_id,
81+
rpc_path: rpc_path.try_into()?,
82+
};
83+
84+
let service = p.state().lsps_service.clone();
85+
match service.handle_message(&req.payload, &mut writer).await {
86+
Ok(_) => continue_response,
87+
Err(e) => {
88+
debug!("failed to handle lsps message: {}", e);
89+
continue_response
90+
}
91+
}
92+
}
93+
94+
pub struct LspsResponseWriter {
95+
peer_id: PublicKey,
96+
rpc_path: PathBuf,
97+
}
98+
99+
#[async_trait]
100+
impl JsonRpcResponseWriter for LspsResponseWriter {
101+
async fn write(&mut self, payload: &[u8]) -> cln_lsps::jsonrpc::Result<()> {
102+
let mut client = cln_rpc::ClnRpc::new(&self.rpc_path).await.map_err(|e| {
103+
cln_lsps::jsonrpc::Error::Transport(TransportError::Other(e.to_string()))
104+
})?;
105+
transport::send_custommsg(&mut client, payload.to_vec(), self.peer_id).await
106+
}
107+
}
108+
109+
pub struct Lsps0ListProtocolsHandler;
110+
111+
#[async_trait]
112+
impl RequestHandler for Lsps0ListProtocolsHandler {
113+
async fn handle(&self, payload: &[u8]) -> core::result::Result<Vec<u8>, RpcError> {
114+
let req: RequestObject<Lsps0listProtocolsRequest> =
115+
serde_json::from_slice(payload).unwrap();
116+
if let Some(id) = req.id {
117+
let res = Lsps0listProtocolsResponse { protocols: vec![] }.into_response(id);
118+
let res_vec = serde_json::to_vec(&res).unwrap();
119+
return Ok(res_vec);
120+
}
121+
Ok(vec![])
122+
}
123+
}

0 commit comments

Comments
 (0)