77
88use lightning:: util:: ser:: { Readable , Writeable } ;
99use lightning:: { impl_writeable_tlv_based, log_error} ;
10- use lightning:: ln:: channelmanager:: InterceptId ;
11- use lightning:: ln:: types:: ChannelId ;
12-
1310use lightning:: util:: logger:: Logger ;
1411use lightning:: util:: persist:: KVStoreSync ;
15- use lightning_types:: payment:: PaymentHash ;
1612
1713use bitcoin:: secp256k1:: PublicKey ;
1814
1915use lightning:: io:: { self , Cursor } ;
2016
2117use std:: collections:: HashMap ;
2218use 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
2722use 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