11use super :: * ;
22use crate :: config:: WALLET_KEYS_SEED_LEN ;
33
4- use crate :: logger:: log_error;
4+ use crate :: logger:: { log_error, FilesystemLogger } ;
55use crate :: peer_store:: PeerStore ;
6- use crate :: sweep:: SpendableOutputInfo ;
6+ use crate :: sweep:: DeprecatedSpendableOutputInfo ;
7+ use crate :: types:: { Broadcaster , ChainSource , FeeEstimator , KeysManager , Sweeper } ;
78use crate :: { Error , EventQueue , PaymentDetails } ;
89
910use lightning:: routing:: gossip:: NetworkGraph ;
@@ -12,13 +13,16 @@ use lightning::util::logger::Logger;
1213use lightning:: util:: persist:: {
1314 KVStore , KVSTORE_NAMESPACE_KEY_ALPHABET , KVSTORE_NAMESPACE_KEY_MAX_LEN ,
1415 NETWORK_GRAPH_PERSISTENCE_KEY , NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE ,
15- NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE , SCORER_PERSISTENCE_KEY ,
16- SCORER_PERSISTENCE_PRIMARY_NAMESPACE , SCORER_PERSISTENCE_SECONDARY_NAMESPACE ,
16+ NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE , OUTPUT_SWEEPER_PERSISTENCE_KEY ,
17+ OUTPUT_SWEEPER_PERSISTENCE_PRIMARY_NAMESPACE , OUTPUT_SWEEPER_PERSISTENCE_SECONDARY_NAMESPACE ,
18+ SCORER_PERSISTENCE_KEY , SCORER_PERSISTENCE_PRIMARY_NAMESPACE ,
19+ SCORER_PERSISTENCE_SECONDARY_NAMESPACE ,
1720} ;
1821use lightning:: util:: ser:: { Readable , ReadableArgs , Writeable } ;
1922use lightning:: util:: string:: PrintableString ;
2023
2124use bip39:: Mnemonic ;
25+ use lightning:: util:: sweep:: { OutputSpendStatus , OutputSweeper } ;
2226use rand:: { thread_rng, RngCore } ;
2327
2428use std:: fs;
@@ -200,34 +204,107 @@ where
200204 Ok ( res)
201205}
202206
207+ /// Read `OutputSweeper` state from the store.
208+ pub ( crate ) fn read_output_sweeper < K : KVStore + Send + Sync > (
209+ broadcaster : Arc < Broadcaster > , fee_estimator : Arc < FeeEstimator > ,
210+ chain_data_source : Arc < ChainSource > , keys_manager : Arc < KeysManager > , kv_store : Arc < K > ,
211+ logger : Arc < FilesystemLogger > ,
212+ ) -> Result < Sweeper < K > , std:: io:: Error > {
213+ let mut reader = Cursor :: new ( kv_store. read (
214+ OUTPUT_SWEEPER_PERSISTENCE_PRIMARY_NAMESPACE ,
215+ OUTPUT_SWEEPER_PERSISTENCE_SECONDARY_NAMESPACE ,
216+ OUTPUT_SWEEPER_PERSISTENCE_KEY ,
217+ ) ?) ;
218+ let args = (
219+ broadcaster,
220+ fee_estimator,
221+ Some ( chain_data_source) ,
222+ Arc :: clone ( & keys_manager) ,
223+ keys_manager,
224+ kv_store,
225+ logger. clone ( ) ,
226+ ) ;
227+ OutputSweeper :: read ( & mut reader, args) . map_err ( |e| {
228+ log_error ! ( logger, "Failed to deserialize OutputSweeper: {}" , e) ;
229+ std:: io:: Error :: new ( std:: io:: ErrorKind :: InvalidData , "Failed to deserialize OutputSweeper" )
230+ } )
231+ }
232+
203233/// Read previously persisted spendable output information from the store.
204- pub ( crate ) fn read_spendable_outputs < K : KVStore + Sync + Send , L : Deref > (
205- kv_store : Arc < K > , logger : L ,
206- ) -> Result < Vec < SpendableOutputInfo > , std:: io:: Error >
234+ pub ( crate ) fn migrate_deprecated_spendable_outputs < K : KVStore + Sync + Send , L : Deref > (
235+ sweeper : Arc < Sweeper < K > > , kv_store : Arc < K > , logger : L ,
236+ ) -> Result < ( ) , std:: io:: Error >
207237where
208238 L :: Target : Logger ,
209239{
210- let mut res = Vec :: new ( ) ;
240+ let best_block = sweeper . current_best_block ( ) ;
211241
212242 for stored_key in kv_store. list (
213- SPENDABLE_OUTPUT_INFO_PERSISTENCE_PRIMARY_NAMESPACE ,
214- SPENDABLE_OUTPUT_INFO_PERSISTENCE_SECONDARY_NAMESPACE ,
243+ DEPRECATED_SPENDABLE_OUTPUT_INFO_PERSISTENCE_PRIMARY_NAMESPACE ,
244+ DEPRECATED_SPENDABLE_OUTPUT_INFO_PERSISTENCE_SECONDARY_NAMESPACE ,
215245 ) ? {
216246 let mut reader = Cursor :: new ( kv_store. read (
217- SPENDABLE_OUTPUT_INFO_PERSISTENCE_PRIMARY_NAMESPACE ,
218- SPENDABLE_OUTPUT_INFO_PERSISTENCE_SECONDARY_NAMESPACE ,
247+ DEPRECATED_SPENDABLE_OUTPUT_INFO_PERSISTENCE_PRIMARY_NAMESPACE ,
248+ DEPRECATED_SPENDABLE_OUTPUT_INFO_PERSISTENCE_SECONDARY_NAMESPACE ,
219249 & stored_key,
220250 ) ?) ;
221- let output = SpendableOutputInfo :: read ( & mut reader) . map_err ( |e| {
251+ let output = DeprecatedSpendableOutputInfo :: read ( & mut reader) . map_err ( |e| {
222252 log_error ! ( logger, "Failed to deserialize SpendableOutputInfo: {}" , e) ;
223253 std:: io:: Error :: new (
224254 std:: io:: ErrorKind :: InvalidData ,
225255 "Failed to deserialize SpendableOutputInfo" ,
226256 )
227257 } ) ?;
228- res. push ( output) ;
258+ let descriptors = vec ! [ output. descriptor. clone( ) ] ;
259+ let spend_delay = Some ( best_block. height + 2 ) ;
260+ sweeper. track_spendable_outputs ( descriptors, output. channel_id , true , spend_delay) ;
261+ if let Some ( tracked_spendable_output) =
262+ sweeper. tracked_spendable_outputs ( ) . iter ( ) . find ( |o| o. descriptor == output. descriptor )
263+ {
264+ match tracked_spendable_output. status {
265+ OutputSpendStatus :: PendingInitialBroadcast { delayed_until_height } => {
266+ if delayed_until_height == spend_delay {
267+ kv_store. remove (
268+ DEPRECATED_SPENDABLE_OUTPUT_INFO_PERSISTENCE_PRIMARY_NAMESPACE ,
269+ DEPRECATED_SPENDABLE_OUTPUT_INFO_PERSISTENCE_SECONDARY_NAMESPACE ,
270+ & stored_key,
271+ false ,
272+ ) ?;
273+ } else {
274+ debug_assert ! ( false , "Unexpected status in OutputSweeper migration." ) ;
275+ log_error ! ( logger, "Unexpected status in OutputSweeper migration." ) ;
276+ return Err ( std:: io:: Error :: new (
277+ std:: io:: ErrorKind :: Other ,
278+ "Failed to migrate OutputSweeper state." ,
279+ ) ) ;
280+ }
281+ } ,
282+ _ => {
283+ debug_assert ! ( false , "Unexpected status in OutputSweeper migration." ) ;
284+ log_error ! ( logger, "Unexpected status in OutputSweeper migration." ) ;
285+ return Err ( std:: io:: Error :: new (
286+ std:: io:: ErrorKind :: Other ,
287+ "Failed to migrate OutputSweeper state." ,
288+ ) ) ;
289+ } ,
290+ }
291+ } else {
292+ debug_assert ! (
293+ false ,
294+ "OutputSweeper failed to track and persist outputs during migration."
295+ ) ;
296+ log_error ! (
297+ logger,
298+ "OutputSweeper failed to track and persist outputs during migration."
299+ ) ;
300+ return Err ( std:: io:: Error :: new (
301+ std:: io:: ErrorKind :: Other ,
302+ "Failed to migrate OutputSweeper state." ,
303+ ) ) ;
304+ }
229305 }
230- Ok ( res)
306+
307+ Ok ( ( ) )
231308}
232309
233310pub ( crate ) fn read_latest_rgs_sync_timestamp < K : KVStore + Sync + Send , L : Deref > (
0 commit comments