plugins: lsps: replace owned types by references

Improves flexibility in transport implementations.

Signed-off-by: Peter Neuroth <pet.v.ne@gmail.com>
This commit is contained in:
Peter Neuroth
2025-12-02 14:47:19 +01:00
committed by madelinevibes
parent e08756cd57
commit 0927179fe1
3 changed files with 18 additions and 25 deletions

View File

@@ -39,8 +39,9 @@ pub type Result<T> = std::result::Result<T, Error>;
/// request over some transport mechanism (RPC, Bolt8, etc.)
#[async_trait]
pub trait Transport: Send + Sync {
async fn send(&self, peer: &PublicKey, request: String) -> Result<String>;
async fn notify(&self, peer: &PublicKey, request: String) -> Result<()>;
async fn send(&self, peer: &PublicKey, request: &str) -> Result<String>;
async fn notify(&self, peer: &PublicKey, request: &str) -> Result<()>;
}
}
/// A typed JSON-RPC client that works with any transport implementation.
@@ -152,7 +153,7 @@ impl<T: Transport> JsonRpcClient<T> {
method, id, &request_json
);
let start = tokio::time::Instant::now();
let res_str = self.transport.send(peer, request_json).await?;
let res_str = self.transport.send(peer, &request_json).await?;
let elapsed = start.elapsed();
debug!(
"Received response: method={}, id={}, response={}, elapsed={}ms",
@@ -176,7 +177,7 @@ impl<T: Transport> JsonRpcClient<T> {
let request_json = serde_json::to_string(&payload)?;
debug!("Sending notification: method={}", method);
let start = tokio::time::Instant::now();
self.transport.notify(peer_id, request_json).await?;
self.transport.notify(peer_id, &request_json).await?;
let elapsed = start.elapsed();
debug!(
"Sent notification: method={}, elapsed={}ms",
@@ -241,10 +242,10 @@ mod test_json_rpc {
async fn send(
&self,
_peer_id: &PublicKey,
req: String,
req: &str,
) -> core::result::Result<String, Error> {
// Store the request
let _ = self.req.set(req);
let _ = self.req.set(req.to_owned());
// Check for error first
if let Some(err) = &*self.err {
@@ -259,13 +260,9 @@ mod test_json_rpc {
panic!("TestTransport: neither result nor error is set.");
}
async fn notify(
&self,
_peer_id: &PublicKey,
req: String,
) -> core::result::Result<(), Error> {
async fn notify(&self, _peer_id: &PublicKey, req: &str) -> core::result::Result<(), Error> {
// Store the request
let _ = self.req.set(req);
let _ = self.req.set(req.to_owned());
// Check for error
if let Some(err) = &*self.err {

View File

@@ -208,7 +208,7 @@ impl Bolt8Transport {
&self,
client: &mut ClnRpc,
peer_id: &PublicKey,
payload: Vec<u8>,
payload: &[u8],
) -> Result<(), Error> {
send_custommsg(client, payload, peer_id).await
}
@@ -228,12 +228,12 @@ impl Bolt8Transport {
/// Sends a custom message to the destination node.
pub async fn send_custommsg(
client: &mut ClnRpc,
payload: Vec<u8>,
payload: &[u8],
peer: &PublicKey,
) -> Result<(), Error> {
let msg = CustomMsg {
message_type: LSPS0_MESSAGE_TYPE,
payload,
payload: payload.to_owned(),
};
let request = cln_rpc::model::requests::SendcustommsgRequest {
@@ -257,9 +257,9 @@ impl Transport for Bolt8Transport {
async fn send(
&self,
peer_id: &PublicKey,
request: String,
request: &str,
) -> core::result::Result<String, Error> {
let id = extract_message_id(&request)?;
let id = extract_message_id(request)?;
let mut client = self.connect_to_node().await?;
let (tx, rx) = mpsc::channel(1);
@@ -274,7 +274,7 @@ impl Transport for Bolt8Transport {
self.hook_watcher
.subscribe_hook_once(id, Arc::downgrade(&tx_arc))
.await;
self.send_custom_msg(&mut client, peer_id, request.into_bytes())
self.send_custom_msg(&mut client, peer_id, request.as_bytes())
.await?;
let res = self.wait_for_response(rx).await?;
@@ -297,13 +297,9 @@ impl Transport for Bolt8Transport {
}
/// Sends a notification without waiting for a response.
async fn notify(
&self,
peer_id: &PublicKey,
request: String,
) -> core::result::Result<(), Error> {
async fn notify(&self, peer_id: &PublicKey, request: &str) -> core::result::Result<(), Error> {
let mut client = self.connect_to_node().await?;
self.send_custom_msg(&mut client, peer_id, request.into_bytes())
self.send_custom_msg(&mut client, peer_id, request.as_bytes())
.await
}
}

View File

@@ -189,6 +189,6 @@ impl JsonRpcResponseWriter for LspsResponseWriter {
let mut client = cln_rpc::ClnRpc::new(&self.rpc_path)
.await
.map_err(|e| Error::Internal(e.to_string()))?;
transport::send_custommsg(&mut client, payload.to_vec(), &self.peer_id).await
transport::send_custommsg(&mut client, payload, &self.peer_id).await
}
}