cln-rpc: allow id to be any token.

Suggested-by: @cdecker
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2022-09-13 06:48:55 +09:30
parent bd18fbc488
commit ce0b765c96
4 changed files with 13 additions and 12 deletions

View File

@ -9,7 +9,7 @@ use std::fmt::Debug;
#[derive(Debug)] #[derive(Debug)]
pub enum JsonRpc<N, R> { pub enum JsonRpc<N, R> {
Request(usize, R), Request(serde_json::Value, R),
Notification(N), Notification(N),
} }
@ -38,7 +38,7 @@ where
{ {
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
struct IdHelper { struct IdHelper {
id: Option<usize>, id: Option<serde_json::Value>,
} }
let v = Value::deserialize(deserializer)?; let v = Value::deserialize(deserializer)?;

View File

@ -5,6 +5,7 @@ use anyhow::Result;
use futures_util::sink::SinkExt; use futures_util::sink::SinkExt;
use futures_util::StreamExt; use futures_util::StreamExt;
use log::{debug, trace}; use log::{debug, trace};
use serde_json::json;
use std::path::Path; use std::path::Path;
use std::sync::atomic::AtomicUsize; use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering; use std::sync::atomic::Ordering;
@ -60,7 +61,7 @@ impl ClnRpc {
// Wrap the raw request in a well-formed JSON-RPC outer dict. // Wrap the raw request in a well-formed JSON-RPC outer dict.
let id = self.next_id.fetch_add(1, Ordering::SeqCst); let id = self.next_id.fetch_add(1, Ordering::SeqCst);
let req: JsonRpc<Notification, Request> = JsonRpc::Request(id, req); let req: JsonRpc<Notification, Request> = JsonRpc::Request(json!(id), req);
let req = serde_json::to_value(req).map_err(|e| RpcError { let req = serde_json::to_value(req).map_err(|e| RpcError {
code: None, code: None,
message: format!("Error parsing request: {}", e), message: format!("Error parsing request: {}", e),
@ -137,7 +138,7 @@ mod test {
let read_req = dbg!(read.next().await.unwrap().unwrap()); let read_req = dbg!(read.next().await.unwrap().unwrap());
assert_eq!( assert_eq!(
json!({"id": 1, "method": "getinfo", "params": {}, "jsonrpc": "2.0"}), json!({"id": "1", "method": "getinfo", "params": {}, "jsonrpc": "2.0"}),
read_req read_req
); );
} }

View File

@ -364,7 +364,7 @@ pub struct ConfiguredPlugin<S, I, O>
where where
S: Clone + Send, S: Clone + Send,
{ {
init_id: usize, init_id: serde_json::Value,
input: FramedRead<I, JsonRpcCodec>, input: FramedRead<I, JsonRpcCodec>,
output: Arc<Mutex<FramedWrite<O, JsonCodec>>>, output: Arc<Mutex<FramedWrite<O, JsonCodec>>>,
plugin: Plugin<S>, plugin: Plugin<S>,
@ -543,7 +543,7 @@ where
self.dispatch_notification(n, plugin).await self.dispatch_notification(n, plugin).await
} }
messages::JsonRpc::CustomRequest(id, p) => { messages::JsonRpc::CustomRequest(id, p) => {
match self.dispatch_custom_request(id, p, plugin).await { match self.dispatch_custom_request(id.clone(), p, plugin).await {
Ok(v) => plugin Ok(v) => plugin
.sender .sender
.send(json!({ .send(json!({
@ -575,7 +575,7 @@ where
} }
async fn dispatch_request( async fn dispatch_request(
_id: usize, _id: serde_json::Value,
_request: messages::Request, _request: messages::Request,
_plugin: &Plugin<S>, _plugin: &Plugin<S>,
) -> Result<(), Error> { ) -> Result<(), Error> {
@ -595,7 +595,7 @@ where
async fn dispatch_custom_request( async fn dispatch_custom_request(
&self, &self,
_id: usize, _id: serde_json::Value,
request: serde_json::Value, request: serde_json::Value,
plugin: &Plugin<S>, plugin: &Plugin<S>,
) -> Result<serde_json::Value, Error> { ) -> Result<serde_json::Value, Error> {

View File

@ -94,9 +94,9 @@ pub struct ProxyInfo {
#[derive(Debug)] #[derive(Debug)]
pub enum JsonRpc<N, R> { pub enum JsonRpc<N, R> {
Request(usize, R), Request(serde_json::Value, R),
Notification(N), Notification(N),
CustomRequest(usize, Value), CustomRequest(serde_json::Value, Value),
CustomNotification(Value), CustomNotification(Value),
} }
@ -125,7 +125,7 @@ where
{ {
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
struct IdHelper { struct IdHelper {
id: Option<usize>, id: Option<serde_json::Value>,
} }
let v = Value::deserialize(deserializer)?; let v = Value::deserialize(deserializer)?;
@ -203,7 +203,7 @@ mod test {
"always_use_proxy": false "always_use_proxy": false
} }
}, },
"id": 10, "id": "10",
}); });
let req: JsonRpc<Notification, Request> = serde_json::from_value(value).unwrap(); let req: JsonRpc<Notification, Request> = serde_json::from_value(value).unwrap();
match req { match req {