-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathmod.rs
More file actions
1186 lines (1053 loc) · 44.3 KB
/
mod.rs
File metadata and controls
1186 lines (1053 loc) · 44.3 KB
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use crate::config::Config;
use crate::pb::{self, node_server::Node};
use crate::storage::StateStore;
use crate::{messages, Event};
use crate::{stager, tramp};
use anyhow::{Context, Error, Result};
use base64::{engine::general_purpose, Engine as _};
use bytes::BufMut;
use cln_rpc::Notification;
use gl_client::persist::{State, StateSketch};
use gl_client::metrics::{
signer_state_request_wire_bytes, savings_percent,
};
use governor::{
clock::MonotonicClock, state::direct::NotKeyed, state::InMemoryState, Quota, RateLimiter,
};
use lazy_static::lazy_static;
use log::{debug, error, info, trace, warn};
use std::path::{Path, PathBuf};
use std::sync::atomic::AtomicBool;
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
};
use std::time::Duration;
use tokio::sync::{broadcast, mpsc, Mutex, OnceCell};
use tokio_stream::wrappers::ReceiverStream;
use tonic::{transport::ServerTlsConfig, Code, Request, Response, Status};
mod wrapper;
use gl_client::bitcoin;
use std::borrow::Borrow;
use std::str::FromStr;
pub use wrapper::WrappedNodeServer;
static LIMITER: OnceCell<RateLimiter<NotKeyed, InMemoryState, MonotonicClock>> =
OnceCell::const_new();
static RPC_CLIENT: OnceCell<Arc<Mutex<cln_rpc::ClnRpc>>> = OnceCell::const_new();
static RPC_POLL_INTERVAL: Duration = Duration::from_millis(500);
#[allow(unused)]
const OPT_SUPPORTS_LSPS: usize = 729;
pub async fn get_rpc<P: AsRef<Path>>(path: P) -> Arc<Mutex<cln_rpc::ClnRpc>> {
RPC_CLIENT
.get_or_init(|| async {
loop {
match cln_rpc::ClnRpc::new(path.as_ref()).await {
Ok(client) => {
debug!("Connected to lightning-rpc.");
return Arc::new(Mutex::new(client));
}
Err(_) => {
debug!("Failed to connect to lightning-rpc. Retrying in {RPC_POLL_INTERVAL:?}...");
tokio::time::sleep(RPC_POLL_INTERVAL).await;
continue;
}
}
}
})
.await
.clone()
}
lazy_static! {
static ref HSM_ID_COUNT: AtomicUsize = AtomicUsize::new(0);
/// The number of signers that are currently connected (best guess
/// due to races). Allows us to determine whether we should
/// initiate operations that might require signatures.
static ref SIGNER_COUNT: AtomicUsize = AtomicUsize::new(0);
static ref RPC_BCAST: broadcast::Sender<super::Event> = broadcast::channel(4).0;
static ref SERIALIZED_CONFIGURE_REQUEST: Mutex<Option<String>> = Mutex::new(None);
static ref RPC_READY: AtomicBool = AtomicBool::new(false);
}
/// The PluginNodeServer is the interface that is exposed to client devices
/// and is in charge of coordinating the various user-controlled
/// entities. This includes dispatching incoming RPC calls to the JSON-RPC
/// interface, as well as staging requests from the HSM so that they can be
/// streamed and replied to by devices that have access to the signing keys.
#[derive(Clone)]
pub struct PluginNodeServer {
pub tls: ServerTlsConfig,
pub stage: Arc<stager::Stage>,
rpc_path: PathBuf,
events: tokio::sync::broadcast::Sender<super::Event>,
signer_state: Arc<Mutex<State>>,
grpc_binding: String,
signer_state_store: Arc<Mutex<Box<dyn StateStore>>>,
pub ctx: crate::context::Context,
notifications: tokio::sync::broadcast::Sender<Notification>,
}
impl PluginNodeServer {
pub async fn new(
stage: Arc<stager::Stage>,
config: Config,
events: tokio::sync::broadcast::Sender<super::Event>,
notifications: tokio::sync::broadcast::Sender<Notification>,
signer_state_store: Box<dyn StateStore>,
) -> Result<Self, Error> {
let tls = ServerTlsConfig::new()
.identity(config.identity.id)
.client_ca_root(config.identity.ca);
let mut rpc_path = std::env::current_dir().unwrap();
rpc_path.push("lightning-rpc");
info!("Connecting to lightning-rpc at {:?}", rpc_path);
// Bridge the RPC_BCAST into the events queue
let tx = events.clone();
tokio::spawn(async move {
let mut rx = RPC_BCAST.subscribe();
loop {
if let Ok(e) = rx.recv().await {
let _ = tx.send(e);
}
}
});
let signer_state = signer_state_store.read().await?;
let ctx = crate::context::Context::new();
let s = PluginNodeServer {
ctx,
tls,
stage,
events,
rpc_path: rpc_path.clone(),
signer_state: Arc::new(Mutex::new(signer_state)),
signer_state_store: Arc::new(Mutex::new(signer_state_store)),
grpc_binding: config.node_grpc_binding,
notifications,
};
tokio::spawn(async move {
let rpc_arc = get_rpc(&rpc_path).await.clone();
let mut rpc = rpc_arc.lock().await;
let list_datastore_req = cln_rpc::model::requests::ListdatastoreRequest {
key: Some(vec!["glconf".to_string(), "request".to_string()]),
};
let res = rpc.call_typed(&list_datastore_req).await;
match res {
Ok(list_datastore_res) => {
if list_datastore_res.datastore.len() > 0 {
let serialized_configure_request =
list_datastore_res.datastore[0].string.clone();
match serialized_configure_request {
Some(serialized_configure_request) => {
let mut cached_serialized_configure_request =
SERIALIZED_CONFIGURE_REQUEST.lock().await;
*cached_serialized_configure_request =
Some(serialized_configure_request);
}
None => {}
}
}
}
Err(_) => {}
}
});
Ok(s)
}
// Wait for the limiter to allow a new RPC call
pub async fn limit(&self) {
let limiter = LIMITER
.get_or_init(|| async {
let quota = Quota::per_minute(core::num::NonZeroU32::new(300).unwrap());
RateLimiter::direct_with_clock(quota, &MonotonicClock::default())
})
.await;
limiter.until_ready().await
}
}
#[tonic::async_trait]
impl Node for PluginNodeServer {
type StreamCustommsgStream = ReceiverStream<Result<pb::Custommsg, Status>>;
type StreamHsmRequestsStream = ReceiverStream<Result<pb::HsmRequest, Status>>;
type StreamLogStream = ReceiverStream<Result<pb::LogEntry, Status>>;
async fn lsp_invoice(
&self,
req: Request<pb::LspInvoiceRequest>,
) -> Result<Response<pb::LspInvoiceResponse>, Status> {
let req: pb::LspInvoiceRequest = req.into_inner();
let rpc_arc = get_rpc(&self.rpc_path).await;
let mut rpc = rpc_arc.lock().await;
// Check if we have sufficient incoming capacity to skip JIT channel negotiation.
// We require capacity + 5% buffer to account for fees and routing.
// Only check for specific amounts (not "any" amount invoices).
if req.amount_msat > 0 {
let receivable = self
.get_receivable_capacity(&mut rpc)
.await
.unwrap_or(0);
// Add 5% buffer: capacity >= amount * 1.05
// Equivalent to: capacity * 100 >= amount * 105
let has_sufficient_capacity = req.amount_msat
.saturating_mul(105)
.checked_div(100)
.map(|required| receivable >= required)
.unwrap_or(false);
if has_sufficient_capacity {
log::info!(
"Sufficient incoming capacity ({} msat) for invoice amount ({} msat), creating regular invoice",
receivable,
req.amount_msat
);
// Create a regular invoice without JIT channel negotiation
let invreq = cln_rpc::model::requests::InvoiceRequest {
amount_msat: cln_rpc::primitives::AmountOrAny::Amount(
cln_rpc::primitives::Amount::from_msat(req.amount_msat),
),
description: req.description.clone(),
label: req.label.clone(),
expiry: None,
fallbacks: None,
preimage: None,
cltv: Some(144),
deschashonly: None,
exposeprivatechannels: None,
};
let res = rpc
.call_typed(&invreq)
.await
.map_err(|e| Status::new(Code::Internal, e.to_string()))?;
return Ok(Response::new(pb::LspInvoiceResponse {
bolt11: res.bolt11,
created_index: res.created_index.unwrap_or(0) as u32,
expires_at: res.expires_at as u32,
payment_hash: <cln_rpc::primitives::Sha256 as Borrow<[u8]>>::borrow(&res.payment_hash).to_vec(),
payment_secret: res.payment_secret.to_vec(),
opening_fee_msat: 0,
}));
}
log::info!(
"Insufficient incoming capacity ({} msat) for invoice amount ({} msat), negotiating JIT channel",
receivable,
req.amount_msat
);
}
// Get the CLN version to determine which RPC method to use
let version = rpc
.call_typed(&cln_rpc::model::requests::GetinfoRequest {})
.await
.map_err(|e| Status::new(Code::Internal, format!("Failed to get version: {}", e)))?
.version;
// In case the client did not specify an LSP to work with,
// let's enumerate them, and select the best option ourselves.
let lsps = self.get_lsps_offers(&mut rpc).await.map_err(|_e| {
Status::not_found("Could not retrieve LSPS peers for invoice negotiation.")
})?;
if lsps.len() < 1 {
return Err(Status::not_found(
"Could not find an LSP peer to negotiate the LSPS2 channel for this invoice.",
));
}
let lsp = &lsps[0];
log::info!("Selecting {:?} for invoice negotiation", lsp);
// Compute the expected opening fee from the LSP's fee parameters.
let opening_fee_msat = lsp.params.first().map_or(0, |p| {
let min_fee: u64 = p.min_fee_msat.parse().unwrap_or(0);
let proportional_fee = req
.amount_msat
.saturating_mul(p.proportional)
.div_ceil(1_000_000);
std::cmp::max(min_fee, proportional_fee)
});
// Use the new RPC method name for versions > v25.05gl1
let mut res = if *version > *"v25.05gl1" {
let mut invreq: crate::requests::LspInvoiceRequestV2 = req.into();
invreq.lsp_id = lsp.node_id.to_owned();
rpc.call_typed(&invreq)
.await
.map_err(|e| Status::new(Code::Internal, e.to_string()))?
} else {
let mut invreq: crate::requests::LspInvoiceRequest = req.into();
invreq.lsp_id = lsp.node_id.to_owned();
rpc.call_typed(&invreq)
.await
.map_err(|e| Status::new(Code::Internal, e.to_string()))?
};
res.opening_fee_msat = opening_fee_msat;
Ok(Response::new(res.into()))
}
async fn stream_custommsg(
&self,
_: Request<pb::StreamCustommsgRequest>,
) -> Result<Response<Self::StreamCustommsgStream>, Status> {
log::debug!("Added a new listener for custommsg");
let (tx, rx) = mpsc::channel(1);
let mut stream = self.events.subscribe();
// TODO: We can do better by returning the broadcast receiver
// directly. Well really we should be filtering the events by
// type, so maybe a `.map()` on the stream can work?
tokio::spawn(async move {
while let Ok(msg) = stream.recv().await {
if let Event::CustomMsg(m) = msg {
log::trace!("Forwarding custommsg {:?} to listener", m);
if let Err(e) = tx.send(Ok(m)).await {
log::warn!("Unable to send custmmsg to listener: {:?}", e);
break;
}
}
}
panic!("stream.recv loop exited...");
});
return Ok(Response::new(ReceiverStream::new(rx)));
}
async fn stream_log(
&self,
_: Request<pb::StreamLogRequest>,
) -> Result<Response<Self::StreamLogStream>, Status> {
match async {
let (tx, rx) = mpsc::channel(1);
let mut lines = linemux::MuxedLines::new()?;
lines.add_file("/tmp/log").await?;
// TODO: Yes, this may produce duplicate lines, when new
// log entries are produced while we're streaming the
// backlog out, but do we care?
use tokio::io::{AsyncBufReadExt, BufReader};
// The nodelet uses its CWD, but CLN creates a network
// subdirectory
let file = tokio::fs::File::open("../log").await?;
let mut file = BufReader::new(file).lines();
tokio::spawn(async move {
match async {
while let Some(line) = file.next_line().await? {
tx.send(Ok(pb::LogEntry {
line: line.trim().to_owned(),
}))
.await?
}
while let Ok(Some(line)) = lines.next_line().await {
tx.send(Ok(pb::LogEntry {
line: line.line().trim().to_string(),
}))
.await?;
}
Ok(())
}
.await as Result<(), anyhow::Error>
{
Ok(()) => {}
Err(e) => {
warn!("error streaming logs to client: {}", e);
}
}
});
Ok(ReceiverStream::new(rx))
}
.await as Result<Self::StreamLogStream, anyhow::Error>
{
Ok(v) => Ok(Response::new(v)),
Err(e) => Err(Status::new(Code::Internal, e.to_string())),
}
}
async fn stream_hsm_requests(
&self,
_request: Request<pb::Empty>,
) -> Result<Response<Self::StreamHsmRequestsStream>, Status> {
let hsm_id = HSM_ID_COUNT.fetch_add(1, Ordering::SeqCst);
SIGNER_COUNT.fetch_add(1, Ordering::SeqCst);
info!(
"New signer with hsm_id={} attached, streaming requests",
hsm_id
);
let (tx, rx) = mpsc::channel(10);
let mut stream = self.stage.mystream().await;
let signer_state = self.signer_state.clone();
let ctx = self.ctx.clone();
tokio::spawn(async move {
trace!("hsmd hsm_id={} request processor started", hsm_id);
let mut last_sent_sketch = StateSketch::new();
{
// We start by immediately injecting a
// vls_protocol::Message::GetHeartbeat. This serves two
// purposes: already send the initial snapshot of the
// signer state to the signer as early as possible, and
// triggering a pruning on the signer, if enabled. In
// incremental mode this ensures that any subsequent,
// presumably time-critical messages, do not have to carry
// the large state with them.
let state_snapshot = signer_state.lock().await.clone();
let state_entries: Vec<gl_client::pb::SignerStateEntry> = state_snapshot
.omit_tombstones()
.into();
let state_wire_bytes = signer_state_request_wire_bytes(&state_entries);
let state_entries: Vec<pb::SignerStateEntry> = state_entries
.into_iter()
.map(|s| pb::SignerStateEntry {
key: s.key,
version: s.version,
value: s.value,
})
.collect();
trace!(
"Signer state heartbeat to hsm_id={} entries={}, wire_bytes={}",
hsm_id,
state_entries.len(),
state_wire_bytes
);
let msg = vls_protocol::msgs::GetHeartbeat {};
use vls_protocol::msgs::SerBolt;
let req = crate::pb::HsmRequest {
// Notice that the request_counter starts at 1000, to
// avoid collisions.
request_id: 0,
signer_state: state_entries,
raw: msg.as_vec(),
requests: vec![], // No pending requests yet, nothing to authorize.
context: None,
};
if let Err(e) = tx.send(Ok(req)).await {
log::warn!("Failed to send heartbeat message to signer: {}", e);
} else {
last_sent_sketch = state_snapshot.sketch();
}
}
loop {
let mut req = match stream.next().await {
Err(e) => {
error!(
"Could not get next request from stage: {:?} for hsm_id={}",
e, hsm_id
);
break;
}
Ok(r) => r,
};
trace!(
"Sending request={} to hsm_id={}",
req.request.request_id,
hsm_id
);
let state_snapshot = signer_state.lock().await.clone();
// Estimate the size of the full state to calculate the bandwidth savings of sending diffs
let full_entries: Vec<gl_client::pb::SignerStateEntry> =
state_snapshot.omit_tombstones().into();
let full_wire_bytes = signer_state_request_wire_bytes(&full_entries);
// Send only the changes since the last time we sent state to this signer.
let diff_state = last_sent_sketch.diff_state(&state_snapshot);
let outgoing_entries: Vec<gl_client::pb::SignerStateEntry> =
diff_state.clone().into();
let outgoing_wire_bytes = signer_state_request_wire_bytes(&outgoing_entries);
let outgoing_entry_count = outgoing_entries.len();
last_sent_sketch.apply_state(&diff_state);
// TODO Consolidate protos in `gl-client` and `gl-plugin`, then remove this map.
let outgoing_entries: Vec<pb::SignerStateEntry> = outgoing_entries
.into_iter()
.map(|s| pb::SignerStateEntry {
key: s.key,
version: s.version,
value: s.value,
})
.collect();
let saved_percent = savings_percent(full_wire_bytes, outgoing_wire_bytes);
trace!(
"Signer state diff to hsm_id={} request_id={} entries={}, wire_bytes={}, full_wire_bytes={}, saved {}% bandwidth syncing the state",
hsm_id,
req.request.request_id,
outgoing_entry_count,
outgoing_wire_bytes,
full_wire_bytes,
saved_percent
);
req.request.signer_state = outgoing_entries;
req.request.requests = ctx.snapshot().await.into_iter().map(|r| r.into()).collect();
let serialized_configure_request = SERIALIZED_CONFIGURE_REQUEST.lock().await;
match &(*serialized_configure_request) {
Some(serialized_configure_request) => {
let configure_request = serde_json::from_str::<crate::context::Request>(
serialized_configure_request,
)
.unwrap();
req.request.requests.push(configure_request.into());
}
None => {}
}
debug!(
"Sending signer requests with {} requests and {} state entries",
req.request.requests.len(),
req.request.signer_state.len()
);
eprintln!("WIRE: plugin -> signer: {:?}", req);
if let Err(e) = tx.send(Ok(req.request)).await {
warn!("Error streaming request {:?} to hsm_id={}", e, hsm_id);
break;
}
}
info!("Signer hsm_id={} exited", hsm_id);
SIGNER_COUNT.fetch_sub(1, Ordering::SeqCst);
});
trace!("Returning stream_hsm_request channel");
Ok(Response::new(ReceiverStream::new(rx)))
}
async fn respond_hsm_request(
&self,
request: Request<pb::HsmResponse>,
) -> Result<Response<pb::Empty>, Status> {
let req = request.into_inner();
if req.error != "" {
log::error!("Signer reports an error: {}", req.error);
log::warn!("The above error was returned instead of a response.");
return Ok(Response::new(pb::Empty::default()));
}
eprintln!("WIRE: signer -> plugin: {:?}", req);
// Merge diff entries returned by signer.
// Create a state from the key-value-version tuples. Need to
// convert here, since `pb` is duplicated in the two different
// crates.
let signer_state: Vec<gl_client::pb::SignerStateEntry> = req
.signer_state
.iter()
.map(|i| gl_client::pb::SignerStateEntry {
key: i.key.to_owned(),
value: i.value.to_owned(),
version: i.version,
})
.collect();
let new_state: gl_client::persist::State = signer_state.into();
// Apply state changes to the in-memory state
let mut state = self.signer_state.lock().await;
let merge_res = state.merge(&new_state).map_err(|e| {
Status::new(
Code::Internal,
format!("Error updating internal state: {e}"),
)
})?;
if merge_res.has_conflicts() {
debug!(
"State merge ignored stale versions (count={})",
merge_res.conflict_count
);
}
// Send changes to the signer_state_store for persistence
let store = self.signer_state_store.lock().await;
if let Err(e) = store.write(state.clone()).await {
log::warn!(
"The returned state could not be stored. Ignoring response for request_id={}, error={:?}",
req.request_id, e
);
/* Exit here so we don't end up committing the changes
* to CLN, but not to the state store. That'd cause
* drifts in states that are very hard to debug, and
* harder to correct. */
return Ok(Response::new(pb::Empty::default()));
}
if let Err(e) = self.stage.respond(req).await {
warn!("Suppressing error: {:?}", e);
}
Ok(Response::new(pb::Empty::default()))
}
type StreamIncomingStream = ReceiverStream<Result<pb::IncomingPayment, Status>>;
async fn stream_incoming(
&self,
_req: tonic::Request<pb::StreamIncomingFilter>,
) -> Result<Response<Self::StreamIncomingStream>, Status> {
// TODO See if we can just return the broadcast::Receiver
// instead of pulling off broadcast and into an mpsc.
let (tx, rx) = mpsc::channel(1);
let mut bcast = self.events.subscribe();
tokio::spawn(async move {
while let Ok(p) = bcast.recv().await {
match p {
super::Event::IncomingPayment(p) => {
let _ = tx.send(Ok(p)).await;
}
_ => {}
}
}
});
return Ok(Response::new(ReceiverStream::new(rx)));
}
type StreamNodeEventsStream = ReceiverStream<Result<pb::NodeEvent, Status>>;
async fn stream_node_events(
&self,
_req: tonic::Request<pb::NodeEventsRequest>,
) -> Result<Response<Self::StreamNodeEventsStream>, Status> {
let (tx, rx) = mpsc::channel(1);
let mut bcast = self.events.subscribe();
tokio::spawn(async move {
while let Ok(event) = bcast.recv().await {
// Convert Event to NodeEvent protobuf, if applicable
let node_event = match &event {
super::Event::IncomingPayment(p) => {
// Extract the offchain payment details
if let Some(crate::pb::incoming_payment::Details::Offchain(offchain)) =
&p.details
{
Some(pb::NodeEvent {
event: Some(pb::node_event::Event::InvoicePaid(pb::InvoicePaid {
payment_hash: offchain.payment_hash.clone(),
bolt11: offchain.bolt11.clone(),
preimage: offchain.preimage.clone(),
label: offchain.label.clone(),
amount_msat: offchain
.amount
.as_ref()
.and_then(|a| a.unit.as_ref())
.map(|u| match u {
pb::amount::Unit::Millisatoshi(m) => *m,
pb::amount::Unit::Satoshi(s) => s * 1000,
pb::amount::Unit::Bitcoin(b) => b * 100_000_000_000,
_ => 0,
})
.unwrap_or(0),
extratlvs: offchain.extratlvs.clone(),
})),
})
} else {
None
}
}
// Other event types are not exposed to clients
_ => None,
};
if let Some(event) = node_event {
if tx.send(Ok(event)).await.is_err() {
// Client disconnected
break;
}
}
}
});
Ok(Response::new(ReceiverStream::new(rx)))
}
async fn configure(
&self,
req: tonic::Request<pb::GlConfig>,
) -> Result<Response<pb::Empty>, Status> {
self.limit().await;
let gl_config = req.into_inner();
let rpc_arc = get_rpc(&self.rpc_path).await;
let mut rpc = rpc_arc.lock().await;
let res = rpc
.call_typed(&cln_rpc::model::requests::GetinfoRequest {})
.await;
let network = match res {
Ok(get_info_response) => match get_info_response.network.parse() {
Ok(v) => v,
Err(_) => Err(Status::new(
Code::Unknown,
format!("Failed to parse 'network' from 'getinfo' response"),
))?,
},
Err(e) => {
return Err(Status::new(
Code::Unknown,
format!("Failed to retrieve a response from 'getinfo' while setting the node's configuration: {}", e),
));
}
};
match bitcoin::Address::from_str(&gl_config.close_to_addr) {
Ok(address) => {
if let Err(e) = address.require_network(network) {
return Err(Status::new(
Code::Unknown,
format!(
"Network validation failed: {}",
e
),
));
}
}
Err(e) => {
return Err(Status::new(
Code::Unknown,
format!(
"The address {} is not valid: {}",
gl_config.close_to_addr, e
),
));
}
}
let requests: Vec<crate::context::Request> = self
.ctx
.snapshot()
.await
.into_iter()
.map(|r| r.into())
.collect();
let serialized_req = serde_json::to_string(&requests[0]).unwrap();
let datastore_res = rpc
.call_typed(&cln_rpc::model::requests::DatastoreRequest {
key: vec!["glconf".to_string(), "request".to_string()],
string: Some(serialized_req.clone()),
hex: None,
mode: None,
generation: None,
})
.await;
match datastore_res {
Ok(_) => {
let mut cached_gl_config = SERIALIZED_CONFIGURE_REQUEST.lock().await;
*cached_gl_config = Some(serialized_req);
Ok(Response::new(pb::Empty::default()))
}
Err(e) => {
return Err(Status::new(
Code::Unknown,
format!(
"Failed to store the raw configure request in the datastore: {}",
e
),
))
}
}
}
async fn trampoline_pay(
&self,
r: tonic::Request<pb::TrampolinePayRequest>,
) -> Result<tonic::Response<pb::TrampolinePayResponse>, Status> {
tramp::trampolinepay(r.into_inner(), self.rpc_path.clone())
.await
.map(cln_rpc::model::responses::PayResponse::into)
.map(|res: cln_grpc::pb::PayResponse| {
tonic::Response::new(pb::TrampolinePayResponse {
payment_preimage: res.payment_preimage,
payment_hash: res.payment_hash,
created_at: res.created_at,
parts: res.parts,
amount_msat: res.amount_msat.unwrap_or_default().msat,
amount_sent_msat: res.amount_sent_msat.unwrap_or_default().msat,
destination: res.destination.unwrap_or_default(),
})
})
.map_err(|err| {
debug!("Trampoline payment failed: {}", err);
err.into()
})
}
}
use cln_grpc::pb::node_server::NodeServer;
#[derive(Clone, Debug)]
struct Lsps2Offer {
node_id: String,
#[allow(unused)]
params: Vec<crate::responses::OpeningFeeParams>,
}
impl PluginNodeServer {
pub async fn run(self) -> Result<()> {
let addr = self.grpc_binding.parse().unwrap();
let cln_node = NodeServer::new(
WrappedNodeServer::new(self.clone())
.await
.context("creating NodeServer instance")?,
);
let router = tonic::transport::Server::builder()
.max_frame_size(4 * 1024 * 1024) // 4MB max request size
.tcp_keepalive(Some(tokio::time::Duration::from_secs(1)))
.tls_config(self.tls.clone())?
.layer(SignatureContextLayer {
ctx: self.ctx.clone(),
})
.add_service(RpcWaitService::new(cln_node, self.rpc_path.clone()))
.add_service(crate::pb::node_server::NodeServer::new(self.clone()));
router
.serve(addr)
.await
.context("grpc interface exited with error")
}
/// Reconnect all peers with whom we have a channel or previously
/// connected explicitly to.
pub async fn reconnect_peers(&self) -> Result<(), Error> {
if SIGNER_COUNT.load(Ordering::SeqCst) < 1 {
use anyhow::anyhow;
return Err(anyhow!(
"Cannot reconnect peers, no signer to complete the handshake"
));
}
log::info!("Reconnecting all peers (plugin)");
let peers = self.get_reconnect_peers().await?;
log::info!(
"Found {} peers to reconnect: {:?} (plugin)",
peers.len(),
peers.iter().map(|p| p.id.clone())
);
let rpc_arc = get_rpc(&self.rpc_path).await;
let mut rpc = rpc_arc.lock().await;
for r in peers {
trace!("Calling connect: {:?} (plugin)", &r.id);
let res = rpc.call_typed(&r).await;
trace!("Connect returned: {:?} -> {:?} (plugin)", &r.id, res);
match res {
Ok(r) => info!("Connection to {} established: {:?} (plugin)", &r.id, r),
Err(e) => warn!("Could not connect to {}: {:?} (plugin)", &r.id, e),
}
}
return Ok(());
}
async fn list_peers(
&self,
rpc: &mut cln_rpc::ClnRpc,
) -> Result<cln_rpc::model::responses::ListpeersResponse, Error> {
rpc.call_typed(&cln_rpc::model::requests::ListpeersRequest {
id: None,
level: None,
})
.await
.map_err(|e| e.into())
}
async fn get_lsps_offers(&self, rpc: &mut cln_rpc::ClnRpc) -> Result<Vec<Lsps2Offer>, Error> {
// Collect peers offering LSP functionality
let lpeers = self.list_peers(rpc).await?;
// Filter out the ones that do not announce the LSPs features.
// TODO: Re-enable the filtering once the cln-lsps-service plugin announces the features.
let _lsps: Vec<cln_rpc::model::responses::ListpeersPeers> = lpeers
.peers
.into_iter()
//.filter(|p| has_feature(
// hex::decode(p.features.clone().unwrap_or_default()).expect("featurebits are hex"),
// OPT_SUPPORTS_LSPS
//))
.collect();
// Query all peers for their LSPS offers, but with a brief
// timeout so the invoice creation isn't help up too long.
let futs: Vec<
tokio::task::JoinHandle<(
String,
Result<
Result<crate::responses::LspGetinfoResponse, cln_rpc::RpcError>,
tokio::time::error::Elapsed,
>,
)>,
> = _lsps
.into_iter()
.map(|peer| {
let rpc_path = self.rpc_path.clone();
tokio::spawn(async move {
let peer_id = format!("{:x}", peer.id);
let mut rpc = cln_rpc::ClnRpc::new(rpc_path.clone()).await.unwrap();
let req = crate::requests::LspGetinfoRequest {
lsp_id: peer_id.clone(),
token: None,
};
(
peer_id,
tokio::time::timeout(
tokio::time::Duration::from_secs(2),
rpc.call_typed(&req),
)
.await,
)
})
})
.collect();
let mut res = vec![];
for f in futs {
match f.await {
//TODO We need to drag the node_id along.
Ok((node_id, Ok(Ok(r)))) => res.push(Lsps2Offer {
node_id: node_id,
params: r.opening_fee_params_menu,
}),
Ok((node_id, Err(e))) => warn!(
"Error fetching LSPS menu items from peer_id={}: {:?}",
node_id, e
),
Ok((node_id, Ok(Err(e)))) => warn!(
"Error fetching LSPS menu items from peer_id={}: {:?}",
node_id, e
),
Err(_) => warn!("Timeout fetching LSPS menu items"),
}
}
log::info!("Gathered {} LSP menus", res.len());
log::trace!("LSP menus: {:?}", &res);
Ok(res)
}
/// Get the total receivable capacity across all active channels.
///
/// Returns the sum of `receivable_msat` for all channels in
/// `CHANNELD_NORMAL` state with a connected peer.
async fn get_receivable_capacity(&self, rpc: &mut cln_rpc::ClnRpc) -> Result<u64, Error> {
use cln_rpc::primitives::ChannelState;
let res = rpc
.call_typed(&cln_rpc::model::requests::ListpeerchannelsRequest { id: None })
.await?;
let total: u64 = res
.channels
.into_iter()
.filter(|c| c.peer_connected && c.state == ChannelState::CHANNELD_NORMAL)
.filter_map(|c| c.receivable_msat)
.map(|a| a.msat())
.sum();
log::debug!("Total receivable capacity: {} msat", total);
Ok(total)
}
async fn get_reconnect_peers(
&self,
) -> Result<Vec<cln_rpc::model::requests::ConnectRequest>, Error> {
let rpc_arc = get_rpc(&self.rpc_path).await;
let mut rpc = rpc_arc.lock().await;
let peers = self.list_peers(&mut rpc).await?;
let mut requests: Vec<cln_rpc::model::requests::ConnectRequest> = peers
.peers
.iter()
.filter(|&p| p.connected)
.map(|p| cln_rpc::model::requests::ConnectRequest {
id: p.id.to_string(),
host: None,
port: None,
})
.collect();