forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
556 lines (500 loc) · 18.6 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! # A Core Lightning RPC-client
//!
//! Core Lightning exposes a JSON-RPC interface over unix-domain sockets.
//! The unix-domain socket appears like file and located by default in
//! `~/.lightning/<network>/lightning-rpc`.
//!
//! This crate contains an RPC-client called [ClnRpc] and models
//! for most [requests](crate::model::requests) and [responses](crate::model::responses).
//!
//! The example below shows how to initiate the client and celss the `getinfo`-rpc method.
//!
//! ```no_run
//! use std::path::Path;
//! use tokio_test;
//! use cln_rpc::{ClnRpc, TypedRequest};
//! use cln_rpc::model::requests::GetinfoRequest;
//! use cln_rpc::model::responses::GetinfoResponse;
//!
//! tokio_test::block_on( async {
//! let path = Path::new("path_to_lightning_dir");
//! let mut rpc = ClnRpc::new(path).await.unwrap();
//! let request = GetinfoRequest {};
//! let response : GetinfoResponse = rpc.call_typed(&request).await.unwrap();
//! });
//! ```
//!
//! If the required model is not available you can implement [`TypedRequest`]
//! and use [`ClnRpc::call_typed`] without a problem.
//!
//! ```no_run
//! use std::path::Path;
//! use tokio_test;
//! use cln_rpc::{ClnRpc, TypedRequest};
//! use serde::{Serialize, Deserialize};
//!
//! #[derive(Serialize, Debug)]
//! struct CustomMethodRequest {
//! param_a : String
//! };
//! #[derive(Deserialize, Debug)]
//! struct CustomMethodResponse {
//! field_a : String
//! };
//!
//! impl TypedRequest for CustomMethodRequest {
//! type Response = CustomMethodResponse;
//!
//! fn method(&self) -> &str {
//! "custommethod"
//! }
//! }
//!
//! tokio_test::block_on( async {
//! let path = Path::new("path_to_lightning_dir");
//! let mut rpc = ClnRpc::new(path).await.unwrap();
//!
//! let request = CustomMethodRequest { param_a : String::from("example")};
//! let response = rpc.call_typed(&request).await.unwrap();
//! })
//! ```
//!
//! An alternative is to use [`ClnRpc::call_raw`].
//!
//! ```no_run
//! use std::path::Path;
//! use tokio_test;
//! use cln_rpc::{ClnRpc, TypedRequest};
//!
//! tokio_test::block_on( async {
//! let path = Path::new("path_to_lightning_dir");
//! let mut rpc = ClnRpc::new(path).await.unwrap();
//! let method = "custommethod";
//! let request = serde_json::json!({"param_a" : "example"});
//! let response : serde_json::Value = rpc.call_raw(method, &request).await.unwrap();
//! })
//! ```
//!
use crate::codec::JsonCodec;
pub use anyhow::Error;
use anyhow::Result;
use core::fmt::Debug;
use futures_util::sink::SinkExt;
use futures_util::StreamExt;
use log::{debug, trace};
use serde::{de::DeserializeOwned, Serialize};
use std::path::Path;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use tokio::net::unix::{OwnedReadHalf, OwnedWriteHalf};
use tokio::net::UnixStream;
use tokio_util::codec::{FramedRead, FramedWrite};
pub mod codec;
pub mod jsonrpc;
pub mod model;
pub mod notifications;
pub mod primitives;
pub use crate::model::TypedRequest;
pub use crate::{
model::{Request, Response},
notifications::Notification,
primitives::RpcError,
};
/// An RPC-client for Core Lightning
///
///
///
pub struct ClnRpc {
next_id: AtomicUsize,
#[allow(dead_code)]
read: FramedRead<OwnedReadHalf, JsonCodec>,
write: FramedWrite<OwnedWriteHalf, JsonCodec>,
}
impl ClnRpc {
pub async fn new<P>(path: P) -> Result<ClnRpc>
where
P: AsRef<Path>,
{
debug!(
"Connecting to socket at {}",
path.as_ref().to_string_lossy()
);
ClnRpc::from_stream(UnixStream::connect(path).await?)
}
fn from_stream(stream: UnixStream) -> Result<ClnRpc> {
let (read, write) = stream.into_split();
Ok(ClnRpc {
next_id: AtomicUsize::new(1),
read: FramedRead::new(read, JsonCodec::default()),
write: FramedWrite::new(write, JsonCodec::default()),
})
}
/// Low-level API to call the rpc.
///
/// An interesting choice of `R` and `P` is [`serde_json::Value`] because it allows
/// ad-hoc calls to custom RPC-methods
///
/// If you are using a model such as [`crate::model::requests::GetinfoRequest`] you'd
/// probably want to use [`Self::call_typed`] instead.
///
/// Example:
/// ```no_run
/// use cln_rpc::ClnRpc;
/// use cln_rpc::model::{requests::GetinfoRequest, responses::GetinfoResponse, responses::ListfundsResponse};
/// use std::path::Path;
/// use tokio_test;
/// tokio_test::block_on( async {
///
/// // Call using json-values
/// let mut cln = ClnRpc::new(Path::new("./lightningd/rpc")).await.unwrap();
/// let request = serde_json::json!({});
/// let response : serde_json::Value = cln.call_raw("getinfo", &request).await.unwrap();
///
/// // Using a model
/// // Prefer to use call_typed instead
/// let request = GetinfoRequest {};
/// let response : GetinfoResponse = cln.call_raw("getinfo", &request).await.unwrap();
/// })
/// ```
pub async fn call_raw<R, P>(&mut self, method: &str, params: &P) -> Result<R, RpcError>
where
P: Serialize + Debug,
R: DeserializeOwned + Debug,
{
trace!("Sending request {} with params {:?}", method, ¶ms);
let id = self.next_id.fetch_add(1, Ordering::SeqCst);
// TODO: Can we make this nicer
// I don't want to have this json_rpc : 2.0 floating everywhere
let req = serde_json::json!({
"jsonrpc" : "2.0",
"id" : id,
"method" : method,
"params" : params,
});
let response: serde_json::Value = self.call_raw_request(req).await?;
serde_json::from_value(response).map_err(|e| RpcError {
code: None,
message: format!("Failed to parse response {:?}", e),
data: None,
})
}
/// A low level method to call raw requests
///
/// This method is private by intention.
/// The caller is (implicitly) providing the `id` of the JsonRpcRequest.
/// This is dangerous because the caller might pick a non-unique id.
///
/// The request should serialize to a valid JsonRpcMessage.
/// If the response is succesful the content of the "result" field is returned
/// If the response is an error the content of the "error" field is returned
///
/// ```no_run
/// use std::path::Path;
/// use cln_rpc::ClnRpc;
/// use tokio_test;
/// tokio_test::block_on( async {
/// let request = serde_json::json!({
/// "id" : 1,
/// "jsonrpc" : "2.0",
/// "method" : "some_method",
/// "params" : {}
/// }
/// );
/// let rpc = ClnRpc::new(Path::new("my_path_to_rpc_file"));
/// // let resp : serde_json::Value = rpc.call_raw_request(request).await.unwrap();
/// })
/// ```
///
async fn call_raw_request(
&mut self,
request: serde_json::Value,
) -> Result<serde_json::Value, RpcError>
where {
trace!("Sending request {:?}", request);
self.write.send(request).await.map_err(|e| RpcError {
code: None,
message: format!("Error passing request to lightningd: {}", e),
data: None,
})?;
let mut response: serde_json::Value = self
.read
.next()
.await
.ok_or_else(|| RpcError {
code: None,
message: "no response from lightningd".to_string(),
data: None,
})?
.map_err(|_| RpcError {
code: None,
message: "reading response from socket".to_string(),
data: None,
})?;
match response.get("result") {
Some(_) => Ok(response["result"].take()),
None => {
let _ = response.get("error").ok_or(
RpcError {
code : None,
message : "Invalid response from lightningd. Neither `result` or `error` field is present".to_string(),
data : None
})?;
let rpc_error: RpcError = serde_json::from_value(response["error"].take())
.map_err(|e| RpcError {
code: None,
message: format!(
"Invalid response from lightningd. Failed to parse `error`. {:?}",
e
),
data: None,
})?;
Err(rpc_error)
}
}
}
pub async fn call(&mut self, req: Request) -> Result<Response, RpcError> {
self.call_enum(req).await
}
/// Performs an rpc-call
pub async fn call_enum(&mut self, req: Request) -> Result<Response, RpcError> {
trace!("call : Serialize and deserialize request {:?}", req);
// A little bit hacky. But serialize the request to get the method name
let mut ser = serde_json::to_value(&req).unwrap();
let method: String = if let serde_json::Value::String(method) = ser["method"].take() {
method
} else {
panic!("Method should be string")
};
let params: serde_json::Value = ser["params"].take();
let response: serde_json::Value = self.call_raw(&method, ¶ms).await?;
let response = serde_json::json!({
"method" : method,
"result" : response
});
// Parse the response
// We add the `method` here because the Response-enum uses it to determine the type
serde_json::from_value(response).map_err(|e| RpcError {
code: None,
message: format!("Failed to deserialize response : {}", e),
data: None,
})
}
/// Performs an rpc-call and performs type-checking.
///
/// ```no_run
/// use cln_rpc::ClnRpc;
/// use cln_rpc::model::requests::GetinfoRequest;
/// use std::path::Path;
/// use tokio_test;
/// tokio_test::block_on( async {
/// let mut rpc = ClnRpc::new(Path::new("path_to_rpc")).await.unwrap();
/// let request = GetinfoRequest {};
/// let response = rpc.call_typed(&request);
/// })
/// ```
pub async fn call_typed<R>(&mut self, request: &R) -> Result<R::Response, RpcError>
where
R: TypedRequest + Serialize + std::fmt::Debug,
R::Response: DeserializeOwned + std::fmt::Debug,
{
let method = request.method();
self.call_raw::<R::Response, R>(method, request).await
}
}
/// Used to skip optional arrays when serializing requests.
fn is_none_or_empty<T>(f: &Option<Vec<T>>) -> bool
where
T: Clone,
{
f.as_ref().map_or(true, |value| value.is_empty())
}
#[cfg(test)]
mod test {
use super::*;
use crate::model::*;
use crate::primitives::PublicKey;
use futures_util::StreamExt;
use serde_json::json;
use std::str::FromStr;
use tokio_util::codec::{Framed, FramedRead};
#[tokio::test]
async fn call_raw_request() {
// Set up a pair of unix-streams
// The frame is a mock rpc-server
let (uds1, uds2) = UnixStream::pair().unwrap();
let mut cln = ClnRpc::from_stream(uds1).unwrap();
let mut frame = Framed::new(uds2, JsonCodec::default());
// Define the request and response send in the RPC-message
let rpc_request = serde_json::json!({
"id" : 1,
"jsonrpc" : "2.0",
"params" : {},
"method" : "some_method"
});
let rpc_request2 = rpc_request.clone();
let rpc_response = serde_json::json!({
"jsonrpc" : "2.0",
"id" : "1",
"result" : {"field_6" : 6}
});
// Spawn the task that performs the RPC-call
// Check that it reads the response correctly
let handle = tokio::task::spawn(async move { cln.call_raw_request(rpc_request2).await });
// Verify that our emulated server received a request
// and sendt the response
let read_req = dbg!(frame.next().await.unwrap().unwrap());
assert_eq!(&rpc_request, &read_req);
frame.send(rpc_response).await.unwrap();
// Get the result from `call_raw_request` and verify
let actual_response: Result<serde_json::Value, RpcError> = handle.await.unwrap();
let actual_response = actual_response.unwrap();
assert_eq!(actual_response, json!({"field_6" : 6}));
}
#[tokio::test]
async fn call_raw() {
let req = serde_json::json!({});
let (uds1, uds2) = UnixStream::pair().unwrap();
let mut cln = ClnRpc::from_stream(uds1).unwrap();
let mut read = FramedRead::new(uds2, JsonCodec::default());
tokio::task::spawn(async move {
let _: serde_json::Value = cln.call_raw("getinfo", &req).await.unwrap();
});
let read_req = dbg!(read.next().await.unwrap().unwrap());
assert_eq!(
json!({"id": 1, "method": "getinfo", "params": {}, "jsonrpc": "2.0"}),
read_req
);
}
#[tokio::test]
async fn test_call_enum_remote_error() {
// Set up the rpc-connection
// The frame represents a Mock rpc-server
let (uds1, uds2) = UnixStream::pair().unwrap();
let mut cln = ClnRpc::from_stream(uds1).unwrap();
let mut frame = Framed::new(uds2, JsonCodec::default());
// Construct the request and response
let req = Request::Ping(requests::PingRequest {
id: PublicKey::from_str(
"0364aeb75519be29d1af7b8cc6232dbda9fdabb79b66e4e1f6a223750954db210b",
)
.unwrap(),
len: None,
pongbytes: None,
});
let mock_resp = json!({
"id" : 1,
"jsonrpc" : "2.0",
"error" : {
"code" : 666,
"message" : "MOCK_ERROR"
}
});
// Spawn the task which calls the rpc
let handle = tokio::task::spawn(async move { cln.call(req).await });
// Ensure the mock receives the request and returns a response
let _ = dbg!(frame.next().await.unwrap().unwrap());
frame.send(mock_resp).await.unwrap();
let rpc_response: Result<_, RpcError> = handle.await.unwrap();
let rpc_error: RpcError = rpc_response.unwrap_err();
println!("RPC_ERROR : {:?}", rpc_error);
assert_eq!(rpc_error.code.unwrap(), 666);
assert_eq!(rpc_error.message, "MOCK_ERROR");
}
#[tokio::test]
async fn test_call_enum() {
// Set up the rpc-connection
// The frame represents a Mock rpc-server
let (uds1, uds2) = UnixStream::pair().unwrap();
let mut cln = ClnRpc::from_stream(uds1).unwrap();
let mut frame = Framed::new(uds2, JsonCodec::default());
// We'll use the Ping request here because both the request
// and response have few arguments
let req = Request::Ping(requests::PingRequest {
id: PublicKey::from_str(
"0364aeb75519be29d1af7b8cc6232dbda9fdabb79b66e4e1f6a223750954db210b",
)
.unwrap(),
len: None,
pongbytes: None,
});
let mock_resp = json!({
"id" : 1,
"jsonrpc" : "2.0",
"result" : { "totlen" : 123 }
});
// we create a task that sends the response and returns the response
let handle = tokio::task::spawn(async move { cln.call(req).await });
// Ensure our mock receives the request and sends the response
let read_req = dbg!(frame.next().await.unwrap().unwrap());
assert_eq!(
read_req,
json!({"id" : 1, "jsonrpc" : "2.0", "method" : "ping", "params" : {"id" : "0364aeb75519be29d1af7b8cc6232dbda9fdabb79b66e4e1f6a223750954db210b"}})
);
frame.send(mock_resp).await.unwrap();
// Verify that the error response is correct
let rpc_response: Result<_, RpcError> = handle.await.unwrap();
match rpc_response.unwrap() {
Response::Ping(ping) => {
assert_eq!(ping.totlen, 123);
}
_ => panic!("A Request::Getinfo should return Response::Getinfo"),
}
}
#[tokio::test]
async fn test_call_typed() {
// Set up the rpc-connection
// The frame represents a Mock rpc-server
let (uds1, uds2) = UnixStream::pair().unwrap();
let mut cln = ClnRpc::from_stream(uds1).unwrap();
let mut frame = Framed::new(uds2, JsonCodec::default());
// We'll use the Ping request here because both the request
// and response have few arguments
let req = requests::PingRequest {
id: PublicKey::from_str(
"0364aeb75519be29d1af7b8cc6232dbda9fdabb79b66e4e1f6a223750954db210b",
)
.unwrap(),
len: None,
pongbytes: None,
};
let mock_resp = json!({
"id" : 1,
"jsonrpc" : "2.0",
"result" : { "totlen" : 123 }
});
// we create a task that sends the response and returns the response
let handle = tokio::task::spawn(async move { cln.call_typed(&req).await });
// Ensure our mock receives the request and sends the response
_ = dbg!(frame.next().await.unwrap().unwrap());
frame.send(mock_resp).await.unwrap();
// Verify that the error response is correct
let rpc_response: Result<_, RpcError> = handle.await.unwrap();
let ping_response = rpc_response.unwrap();
assert_eq!(ping_response.totlen, 123);
}
#[tokio::test]
async fn test_call_typed_remote_error() {
// Create a dummy rpc-request
let req = requests::GetinfoRequest {};
// Create a dummy error response
let response = json!({
"id" : 1,
"jsonrpc" : "2.0",
"error" : {
"code" : 666,
"message" : "MOCK_ERROR",
}});
let (uds1, uds2) = UnixStream::pair().unwrap();
let mut cln = ClnRpc::from_stream(uds1).unwrap();
// Send out the request
let mut frame = Framed::new(uds2, JsonCodec::default());
let handle = tokio::task::spawn(async move { cln.call_typed(&req).await });
// Dummy-server ensures the request has been received and send the error response
let _ = dbg!(frame.next().await.unwrap().unwrap());
frame.send(response).await.unwrap();
let rpc_response = handle.await.unwrap();
let rpc_error = rpc_response.expect_err("Must be an RPC-error response");
assert_eq!(rpc_error.code.unwrap(), 666);
assert_eq!(rpc_error.message, "MOCK_ERROR");
}
}