plugins: lsps: replace owned types by references
What changed, and why it matters
This is a routine Rust code cleanup in a Core Lightning plugin. It changes several function signatures to accept string and byte references instead of owned values, reducing unnecessary memory copies. There is no security-relevant change visible in the diff.
No security action required. Review as normal code-quality change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors the LSPS plugin’s transport layer to use borrowed types (&str, &[u8]) rather than owned String and Vec
Changed components
plugins/lsps-plugin/src/core/transport.rsplugins/lsps-plugin/src/lsps0/transport.rsplugins/lsps-plugin/src/service.rsInspect captured patch +18 / −25
diff --git a/plugins/lsps-plugin/src/core/transport.rs b/plugins/lsps-plugin/src/core/transport.rs
index 5e5ce749..0d86afbf 100644
--- a/plugins/lsps-plugin/src/core/transport.rs
+++ b/plugins/lsps-plugin/src/core/transport.rs
@@ -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 {
diff --git a/plugins/lsps-plugin/src/lsps0/transport.rs b/plugins/lsps-plugin/src/lsps0/transport.rs
index f549ccf8..ffc98e92 100644
--- a/plugins/lsps-plugin/src/lsps0/transport.rs
+++ b/plugins/lsps-plugin/src/lsps0/transport.rs
@@ -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
}
}
diff --git a/plugins/lsps-plugin/src/service.rs b/plugins/lsps-plugin/src/service.rs
index 2b443045..ea0def45 100644
--- a/plugins/lsps-plugin/src/service.rs
+++ b/plugins/lsps-plugin/src/service.rs
@@ -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
}
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.