Skip to content

Commit 0c393ef

Browse files
Fernando Ledesmaamackillop
authored andcommitted
Migrate LSPS4 to async/sync persistence
1 parent d426cee commit 0c393ef

6 files changed

Lines changed: 308 additions & 141 deletions

File tree

lightning-background-processor/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@ pub const NO_LIQUIDITY_MANAGER: Option<
438438
+ Send
439439
+ Sync,
440440
T = &(dyn BroadcasterInterface + Send + Sync),
441+
Logger = dyn lightning::util::logger::Logger + Send + Sync,
442+
L = &(dyn lightning::util::logger::Logger + Send + Sync),
441443
> + Send
442444
+ Sync,
443445
>,

lightning-liquidity/src/lsps4/htlc_store.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
use lightning::util::ser::{Readable, Writeable};
99
use lightning::{impl_writeable_tlv_based, log_error};
1010
use lightning::ln::channelmanager::InterceptId;
11-
use lightning::ln::types::ChannelId;
12-
1311
use lightning::util::logger::Logger;
1412
use lightning::util::persist::KVStoreSync;
1513
use lightning_types::payment::PaymentHash;
@@ -112,15 +110,18 @@ where L::Target: Logger, KV::Target: KVStoreSync {
112110
) -> Result<Self, io::Error> {
113111
let mut htlcs = Vec::new();
114112

115-
for stored_key in kv_store.list(
113+
let stored_keys = kv_store.list(
116114
INTERCEPTED_HTLC_STORE_PERSISTENCE_PRIMARY_NAMESPACE,
117115
INTERCEPTED_HTLC_STORE_PERSISTENCE_SECONDARY_NAMESPACE,
118-
)? {
119-
let mut reader = Cursor::new(kv_store.read(
116+
)?;
117+
118+
for stored_key in stored_keys {
119+
let data = kv_store.read(
120120
INTERCEPTED_HTLC_STORE_PERSISTENCE_PRIMARY_NAMESPACE,
121121
INTERCEPTED_HTLC_STORE_PERSISTENCE_SECONDARY_NAMESPACE,
122122
&stored_key,
123-
)?);
123+
)?;
124+
let mut reader = Cursor::new(data);
124125
let htlc = InterceptedHtlc::read(&mut reader).map_err(|e| {
125126
log_error!(logger, "Failed to deserialize InterceptedHtlc: {}", e);
126127
io::Error::new(
@@ -138,13 +139,19 @@ where L::Target: Logger, KV::Target: KVStoreSync {
138139
}
139140

140141
pub(crate) fn insert(&self, htlc: InterceptedHtlc) -> Result<bool, io::Error> {
141-
let mut locked_htlcs = self.htlcs.lock().unwrap();
142-
143-
if locked_htlcs.contains_key(&htlc.id()) {
144-
return Ok(false);
142+
// Check if already exists
143+
{
144+
let locked_htlcs = self.htlcs.lock().unwrap();
145+
if locked_htlcs.contains_key(&htlc.id()) {
146+
return Ok(false);
147+
}
145148
}
146149

150+
// Persist first (outside the lock)
147151
self.persist(&htlc)?;
152+
153+
// Then insert into the map
154+
let mut locked_htlcs = self.htlcs.lock().unwrap();
148155
let updated = locked_htlcs.insert(htlc.id(), htlc).is_some();
149156
Ok(updated)
150157
}

lightning-liquidity/src/lsps4/scid_store.rs

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,16 @@
77

88
use lightning::util::ser::{Readable, Writeable};
99
use lightning::{impl_writeable_tlv_based, log_error};
10-
use lightning::ln::channelmanager::InterceptId;
11-
use lightning::ln::types::ChannelId;
12-
1310
use lightning::util::logger::Logger;
1411
use lightning::util::persist::KVStoreSync;
15-
use lightning_types::payment::PaymentHash;
1612

1713
use bitcoin::secp256k1::PublicKey;
1814

1915
use lightning::io::{self, Cursor};
2016

2117
use std::collections::HashMap;
2218
use std::ops::Deref;
23-
use std::time::{SystemTime, UNIX_EPOCH};
24-
use crate::sync::{Arc, Mutex, MutexGuard, RwLock};
19+
use crate::sync::RwLock;
2520

2621

2722
use crate::lsps4::utils;
@@ -81,15 +76,18 @@ where L::Target: Logger, KV::Target: KVStoreSync {
8176
) -> Result<Self, io::Error> {
8277
let mut scids = Vec::new();
8378

84-
for stored_key in kv_store.list(
79+
let stored_keys = kv_store.list(
8580
INTERCEPT_SCID_STORE_PERSISTENCE_PRIMARY_NAMESPACE,
8681
INTERCEPT_SCID_STORE_PERSISTENCE_SECONDARY_NAMESPACE,
87-
)? {
88-
let mut reader = Cursor::new(kv_store.read(
82+
)?;
83+
84+
for stored_key in stored_keys {
85+
let data = kv_store.read(
8986
INTERCEPT_SCID_STORE_PERSISTENCE_PRIMARY_NAMESPACE,
9087
INTERCEPT_SCID_STORE_PERSISTENCE_SECONDARY_NAMESPACE,
9188
&stored_key,
92-
)?);
89+
)?;
90+
let mut reader = Cursor::new(data);
9391
let scid = ScidWithPeer::read(&mut reader).map_err(|e| {
9492
log_error!(logger, "Failed to deserialize InterceptScid: {}", e);
9593
io::Error::new(
@@ -110,12 +108,26 @@ where L::Target: Logger, KV::Target: KVStoreSync {
110108
}
111109

112110
pub(crate) fn insert(&self, scid: ScidWithPeer) -> Result<bool, io::Error> {
113-
let mut locked_peer_by_scid = self.peer_by_scid.write().unwrap();
114-
let mut locked_scid_by_peer = self.scid_by_peer.write().unwrap();
111+
use lightning::log_info;
112+
log_info!(self.logger, "[LSPS4 ScidStore] Inserting SCID {} for peer {}", scid.scid(), scid.peer_id());
115113

114+
// Persist first
116115
self.persist(&scid)?;
116+
117+
// Then insert into the maps
118+
let mut locked_peer_by_scid = self.peer_by_scid.write().unwrap();
119+
let mut locked_scid_by_peer = self.scid_by_peer.write().unwrap();
117120
let updated = locked_peer_by_scid.insert(scid.scid(), scid.peer_id().clone()).is_some();
118121
locked_scid_by_peer.insert(scid.peer_id().clone(), scid.scid());
122+
123+
log_info!(
124+
self.logger,
125+
"[LSPS4 ScidStore] Successfully inserted SCID {} for peer {} (was_update: {})",
126+
scid.scid(),
127+
scid.peer_id(),
128+
updated
129+
);
130+
119131
Ok(updated)
120132
}
121133

@@ -170,10 +182,26 @@ where L::Target: Logger, KV::Target: KVStoreSync {
170182
}
171183

172184
pub fn get_peer(&self, scid: u64) -> Option<PublicKey> {
173-
self.peer_by_scid.read().unwrap().get(&scid).cloned()
185+
use lightning::log_debug;
186+
let result = self.peer_by_scid.read().unwrap().get(&scid).cloned();
187+
log_debug!(
188+
self.logger,
189+
"[LSPS4 ScidStore] get_peer({}) = {:?}",
190+
scid,
191+
result
192+
);
193+
result
174194
}
175195

176196
pub fn get_scid(&self, peer_id: &PublicKey) -> Option<u64> {
177-
self.scid_by_peer.read().unwrap().get(peer_id).cloned()
197+
use lightning::log_debug;
198+
let result = self.scid_by_peer.read().unwrap().get(peer_id).cloned();
199+
log_debug!(
200+
self.logger,
201+
"[LSPS4 ScidStore] get_scid({}) = {:?}",
202+
peer_id,
203+
result
204+
);
205+
result
178206
}
179207
}

0 commit comments

Comments
 (0)