@@ -244,6 +244,7 @@ pub struct NodeBuilder {
244244 async_payments_role : Option < AsyncPaymentsRole > ,
245245 runtime_handle : Option < tokio:: runtime:: Handle > ,
246246 pathfinding_scores_sync_config : Option < PathfindingScoresSyncConfig > ,
247+ recovery_mode : bool ,
247248}
248249
249250impl NodeBuilder {
@@ -261,6 +262,7 @@ impl NodeBuilder {
261262 let log_writer_config = None ;
262263 let runtime_handle = None ;
263264 let pathfinding_scores_sync_config = None ;
265+ let recovery_mode = false ;
264266 Self {
265267 config,
266268 chain_data_source_config,
@@ -270,6 +272,7 @@ impl NodeBuilder {
270272 runtime_handle,
271273 async_payments_role : None ,
272274 pathfinding_scores_sync_config,
275+ recovery_mode,
273276 }
274277 }
275278
@@ -544,6 +547,16 @@ impl NodeBuilder {
544547 Ok ( self )
545548 }
546549
550+ /// Configures the [`Node`] to resync chain data from genesis on first startup, recovering any
551+ /// historical wallet funds.
552+ ///
553+ /// This should only be set on first startup when importing an older wallet from a previously
554+ /// used [`NodeEntropy`].
555+ pub fn set_wallet_recovery_mode ( & mut self ) -> & mut Self {
556+ self . recovery_mode = true ;
557+ self
558+ }
559+
547560 /// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options
548561 /// previously configured.
549562 pub fn build ( & self , node_entropy : NodeEntropy ) -> Result < Node , BuildError > {
@@ -679,6 +692,7 @@ impl NodeBuilder {
679692 self . liquidity_source_config . as_ref ( ) ,
680693 self . pathfinding_scores_sync_config . as_ref ( ) ,
681694 self . async_payments_role ,
695+ self . recovery_mode ,
682696 seed_bytes,
683697 runtime,
684698 logger,
@@ -919,6 +933,15 @@ impl ArcedNodeBuilder {
919933 self . inner . write ( ) . unwrap ( ) . set_async_payments_role ( role) . map ( |_| ( ) )
920934 }
921935
936+ /// Configures the [`Node`] to resync chain data from genesis on first startup, recovering any
937+ /// historical wallet funds.
938+ ///
939+ /// This should only be set on first startup when importing an older wallet from a previously
940+ /// used [`NodeEntropy`].
941+ pub fn set_wallet_recovery_mode ( & self ) {
942+ self . inner . write ( ) . unwrap ( ) . set_wallet_recovery_mode ( ) ;
943+ }
944+
922945 /// Builds a [`Node`] instance with a [`SqliteStore`] backend and according to the options
923946 /// previously configured.
924947 pub fn build ( & self , node_entropy : Arc < NodeEntropy > ) -> Result < Arc < Node > , BuildError > {
@@ -1033,8 +1056,8 @@ fn build_with_store_internal(
10331056 gossip_source_config : Option < & GossipSourceConfig > ,
10341057 liquidity_source_config : Option < & LiquiditySourceConfig > ,
10351058 pathfinding_scores_sync_config : Option < & PathfindingScoresSyncConfig > ,
1036- async_payments_role : Option < AsyncPaymentsRole > , seed_bytes : [ u8 ; 64 ] , runtime : Arc < Runtime > ,
1037- logger : Arc < Logger > , kv_store : Arc < DynStore > ,
1059+ async_payments_role : Option < AsyncPaymentsRole > , recovery_mode : bool , seed_bytes : [ u8 ; 64 ] ,
1060+ runtime : Arc < Runtime > , logger : Arc < Logger > , kv_store : Arc < DynStore > ,
10381061) -> Result < Node , BuildError > {
10391062 optionally_install_rustls_cryptoprovider ( ) ;
10401063
@@ -1230,19 +1253,23 @@ fn build_with_store_internal(
12301253 BuildError :: WalletSetupFailed
12311254 } ) ?;
12321255
1233- if let Some ( best_block) = chain_tip_opt {
1234- // Insert the first checkpoint if we have it, to avoid resyncing from genesis.
1235- // TODO: Use a proper wallet birthday once BDK supports it.
1236- let mut latest_checkpoint = wallet. latest_checkpoint ( ) ;
1237- let block_id =
1238- bdk_chain:: BlockId { height : best_block. height , hash : best_block. block_hash } ;
1239- latest_checkpoint = latest_checkpoint. insert ( block_id) ;
1240- let update =
1241- bdk_wallet:: Update { chain : Some ( latest_checkpoint) , ..Default :: default ( ) } ;
1242- wallet. apply_update ( update) . map_err ( |e| {
1243- log_error ! ( logger, "Failed to apply checkpoint during wallet setup: {}" , e) ;
1244- BuildError :: WalletSetupFailed
1245- } ) ?;
1256+ if !recovery_mode {
1257+ if let Some ( best_block) = chain_tip_opt {
1258+ // Insert the first checkpoint if we have it, to avoid resyncing from genesis.
1259+ // TODO: Use a proper wallet birthday once BDK supports it.
1260+ let mut latest_checkpoint = wallet. latest_checkpoint ( ) ;
1261+ let block_id = bdk_chain:: BlockId {
1262+ height : best_block. height ,
1263+ hash : best_block. block_hash ,
1264+ } ;
1265+ latest_checkpoint = latest_checkpoint. insert ( block_id) ;
1266+ let update =
1267+ bdk_wallet:: Update { chain : Some ( latest_checkpoint) , ..Default :: default ( ) } ;
1268+ wallet. apply_update ( update) . map_err ( |e| {
1269+ log_error ! ( logger, "Failed to apply checkpoint during wallet setup: {}" , e) ;
1270+ BuildError :: WalletSetupFailed
1271+ } ) ?;
1272+ }
12461273 }
12471274 wallet
12481275 } ,
0 commit comments