Skip to content

Commit afbe962

Browse files
committed
Skip LSPS2ServiceHandler persistence if unnecessary
.. we only persist the service handler if necessary.
1 parent cad4659 commit afbe962

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

lightning-liquidity/src/lsps2/service.rs

Lines changed: 40 additions & 2 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 {
@@ -823,6 +829,9 @@ where
823829
match outer_state_lock.get(counterparty_node_id) {
824830
Some(inner_state_lock) => {
825831
let mut peer_state = inner_state_lock.lock().unwrap();
832+
peer_state.needs_persist |= peer_state
833+
.outbound_channels_by_intercept_scid
834+
.contains_key(&intercept_scid);
826835
if let Some(jit_channel) =
827836
peer_state.outbound_channels_by_intercept_scid.get_mut(&intercept_scid)
828837
{
@@ -910,6 +919,8 @@ where
910919
match outer_state_lock.get(counterparty_node_id) {
911920
Some(inner_state_lock) => {
912921
let mut peer_state = inner_state_lock.lock().unwrap();
922+
peer_state.needs_persist |=
923+
peer_state.intercept_scid_by_channel_id.contains_key(&channel_id);
913924
if let Some(intercept_scid) =
914925
peer_state.intercept_scid_by_channel_id.get(&channel_id).copied()
915926
{
@@ -978,6 +989,8 @@ where
978989
match outer_state_lock.get(counterparty_node_id) {
979990
Some(inner_state_lock) => {
980991
let mut peer_state = inner_state_lock.lock().unwrap();
992+
peer_state.needs_persist |=
993+
peer_state.intercept_scid_by_channel_id.contains_key(&next_channel_id);
981994
if let Some(intercept_scid) =
982995
peer_state.intercept_scid_by_channel_id.get(&next_channel_id).copied()
983996
{
@@ -1082,6 +1095,7 @@ where
10821095
peer_state.intercept_scid_by_user_channel_id.remove(&user_channel_id);
10831096
peer_state.outbound_channels_by_intercept_scid.remove(&intercept_scid);
10841097
peer_state.intercept_scid_by_channel_id.retain(|_, &mut scid| scid != intercept_scid);
1098+
peer_state.needs_persist |= true;
10851099

10861100
Ok(())
10871101
}
@@ -1113,6 +1127,8 @@ where
11131127
err: format!("Could not find a channel with user_channel_id {}", user_channel_id),
11141128
})?;
11151129

1130+
peer_state.needs_persist |=
1131+
peer_state.outbound_channels_by_intercept_scid.contains_key(&intercept_scid);
11161132
let jit_channel = peer_state
11171133
.outbound_channels_by_intercept_scid
11181134
.get_mut(&intercept_scid)
@@ -1162,6 +1178,8 @@ where
11621178
match outer_state_lock.get(counterparty_node_id) {
11631179
Some(inner_state_lock) => {
11641180
let mut peer_state = inner_state_lock.lock().unwrap();
1181+
peer_state.needs_persist |=
1182+
peer_state.intercept_scid_by_user_channel_id.contains_key(&user_channel_id);
11651183
if let Some(intercept_scid) =
11661184
peer_state.intercept_scid_by_user_channel_id.get(&user_channel_id).copied()
11671185
{
@@ -1484,7 +1502,16 @@ where
14841502
);
14851503
return Err(err);
14861504
},
1487-
Some(entry) => entry.lock().unwrap().encode(),
1505+
Some(entry) => {
1506+
let mut peer_state_lock = entry.lock().unwrap();
1507+
if !peer_state_lock.needs_persist {
1508+
// We already have persisted otherwise by now.
1509+
return Ok(());
1510+
} else {
1511+
peer_state_lock.needs_persist = false;
1512+
peer_state_lock.encode()
1513+
}
1514+
},
14881515
}
14891516
};
14901517

@@ -1498,6 +1525,14 @@ where
14981525
encoded,
14991526
)
15001527
.await
1528+
.map_err(|e| {
1529+
self.per_peer_state
1530+
.read()
1531+
.unwrap()
1532+
.get(&counterparty_node_id)
1533+
.map(|p| p.lock().unwrap().needs_persist = true);
1534+
e
1535+
})
15011536
}
15021537

15031538
pub(crate) async fn persist(&self) -> Result<(), lightning::io::Error> {
@@ -1506,7 +1541,10 @@ where
15061541
// time.
15071542
let need_persist: Vec<PublicKey> = {
15081543
let outer_state_lock = self.per_peer_state.read().unwrap();
1509-
outer_state_lock.iter().filter_map(|(k, v)| Some(*k)).collect()
1544+
outer_state_lock
1545+
.iter()
1546+
.filter_map(|(k, v)| if v.lock().unwrap().needs_persist { Some(*k) } else { None })
1547+
.collect()
15101548
};
15111549

15121550
for counterparty_node_id in need_persist.into_iter() {

0 commit comments

Comments
 (0)