Skip to content

Commit afb8fa9

Browse files
committed
Skip LSPS2ServiceHandler persistence if unnecessary
.. we only persist the service handler if necessary.
1 parent 2c2bcfd commit afb8fa9

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

lightning-liquidity/src/lsps2/service.rs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ pub(crate) struct PeerState {
470470
intercept_scid_by_user_channel_id: HashMap<u128, u64>,
471471
intercept_scid_by_channel_id: HashMap<ChannelId, u64>,
472472
pending_requests: HashMap<LSPSRequestId, LSPS2Request>,
473+
needs_persist: bool,
473474
}
474475

475476
impl PeerState {
@@ -478,16 +479,19 @@ impl PeerState {
478479
let pending_requests = new_hash_map();
479480
let intercept_scid_by_user_channel_id = new_hash_map();
480481
let intercept_scid_by_channel_id = new_hash_map();
482+
let needs_persist = true;
481483
Self {
482484
outbound_channels_by_intercept_scid,
483485
pending_requests,
484486
intercept_scid_by_user_channel_id,
485487
intercept_scid_by_channel_id,
488+
needs_persist,
486489
}
487490
}
488491

489492
fn insert_outbound_channel(&mut self, intercept_scid: u64, channel: OutboundJITChannel) {
490493
self.outbound_channels_by_intercept_scid.insert(intercept_scid, channel);
494+
self.needs_persist |= true;
491495
}
492496

493497
fn prune_expired_request_state(&mut self) {
@@ -506,6 +510,7 @@ impl PeerState {
506510
// We abort the flow, and prune any data kept.
507511
self.intercept_scid_by_channel_id.retain(|_, iscid| intercept_scid != iscid);
508512
self.intercept_scid_by_user_channel_id.retain(|_, iscid| intercept_scid != iscid);
513+
// TODO: Remove peer state entry from the KVStore
509514
return false;
510515
}
511516
true
@@ -533,6 +538,7 @@ impl_writeable_tlv_based!(PeerState, {
533538
(2, intercept_scid_by_user_channel_id, required),
534539
(4, intercept_scid_by_channel_id, required),
535540
(_unused, pending_requests, (static_value, new_hash_map())),
541+
(_unused, needs_persist, (static_value, false)),
536542
});
537543

538544
macro_rules! get_or_insert_peer_state_entry {
@@ -831,6 +837,9 @@ where
831837
match outer_state_lock.get(counterparty_node_id) {
832838
Some(inner_state_lock) => {
833839
let mut peer_state = inner_state_lock.lock().unwrap();
840+
peer_state.needs_persist |= peer_state
841+
.outbound_channels_by_intercept_scid
842+
.contains_key(&intercept_scid);
834843
if let Some(jit_channel) =
835844
peer_state.outbound_channels_by_intercept_scid.get_mut(&intercept_scid)
836845
{
@@ -918,6 +927,8 @@ where
918927
match outer_state_lock.get(counterparty_node_id) {
919928
Some(inner_state_lock) => {
920929
let mut peer_state = inner_state_lock.lock().unwrap();
930+
peer_state.needs_persist |=
931+
peer_state.intercept_scid_by_channel_id.contains_key(&channel_id);
921932
if let Some(intercept_scid) =
922933
peer_state.intercept_scid_by_channel_id.get(&channel_id).copied()
923934
{
@@ -986,6 +997,8 @@ where
986997
match outer_state_lock.get(counterparty_node_id) {
987998
Some(inner_state_lock) => {
988999
let mut peer_state = inner_state_lock.lock().unwrap();
1000+
peer_state.needs_persist |=
1001+
peer_state.intercept_scid_by_channel_id.contains_key(&next_channel_id);
9891002
if let Some(intercept_scid) =
9901003
peer_state.intercept_scid_by_channel_id.get(&next_channel_id).copied()
9911004
{
@@ -1090,6 +1103,7 @@ where
10901103
peer_state.intercept_scid_by_user_channel_id.remove(&user_channel_id);
10911104
peer_state.outbound_channels_by_intercept_scid.remove(&intercept_scid);
10921105
peer_state.intercept_scid_by_channel_id.retain(|_, &mut scid| scid != intercept_scid);
1106+
peer_state.needs_persist |= true;
10931107

10941108
Ok(())
10951109
}
@@ -1121,6 +1135,8 @@ where
11211135
err: format!("Could not find a channel with user_channel_id {}", user_channel_id),
11221136
})?;
11231137

1138+
peer_state.needs_persist |=
1139+
peer_state.outbound_channels_by_intercept_scid.contains_key(&intercept_scid);
11241140
let jit_channel = peer_state
11251141
.outbound_channels_by_intercept_scid
11261142
.get_mut(&intercept_scid)
@@ -1170,6 +1186,8 @@ where
11701186
match outer_state_lock.get(counterparty_node_id) {
11711187
Some(inner_state_lock) => {
11721188
let mut peer_state = inner_state_lock.lock().unwrap();
1189+
peer_state.needs_persist |=
1190+
peer_state.intercept_scid_by_user_channel_id.contains_key(&user_channel_id);
11731191
if let Some(intercept_scid) =
11741192
peer_state.intercept_scid_by_user_channel_id.get(&user_channel_id).copied()
11751193
{
@@ -1486,13 +1504,19 @@ where
14861504
let outer_state_lock = self.per_peer_state.read().unwrap();
14871505
let encoded = match outer_state_lock.get(&counterparty_node_id) {
14881506
None => {
1489-
let err = lightning::io::Error::new(
1490-
lightning::io::ErrorKind::Other,
1491-
"Failed to get peer entry",
1492-
);
1493-
return Err(err);
1507+
// We dropped the peer state by now.
1508+
return Ok(());
1509+
},
1510+
Some(entry) => {
1511+
let mut peer_state_lock = entry.lock().unwrap();
1512+
if !peer_state_lock.needs_persist {
1513+
// We already have persisted otherwise by now.
1514+
return Ok(());
1515+
} else {
1516+
peer_state_lock.needs_persist = false;
1517+
peer_state_lock.encode()
1518+
}
14941519
},
1495-
Some(entry) => entry.lock().unwrap().encode(),
14961520
};
14971521
let key = counterparty_node_id.to_string();
14981522

@@ -1504,7 +1528,14 @@ where
15041528
)
15051529
};
15061530

1507-
fut.await
1531+
fut.await.map_err(|e| {
1532+
self.per_peer_state
1533+
.read()
1534+
.unwrap()
1535+
.get(&counterparty_node_id)
1536+
.map(|p| p.lock().unwrap().needs_persist = true);
1537+
e
1538+
})
15081539
}
15091540

15101541
pub(crate) async fn persist(&self) -> Result<(), lightning::io::Error> {
@@ -1513,7 +1544,10 @@ where
15131544
// time.
15141545
let need_persist: Vec<PublicKey> = {
15151546
let outer_state_lock = self.per_peer_state.read().unwrap();
1516-
outer_state_lock.iter().filter_map(|(k, v)| Some(*k)).collect()
1547+
outer_state_lock
1548+
.iter()
1549+
.filter_map(|(k, v)| if v.lock().unwrap().needs_persist { Some(*k) } else { None })
1550+
.collect()
15171551
};
15181552

15191553
for counterparty_node_id in need_persist.into_iter() {

0 commit comments

Comments
 (0)