@@ -118,7 +118,7 @@ use types::{
118118pub use types:: { ChannelDetails , ChannelId , PeerDetails , UserChannelId } ;
119119use wallet:: Wallet ;
120120
121- use logger:: { log_error, log_info, FilesystemLogger , Logger } ;
121+ use logger:: { log_error, log_info, log_trace , FilesystemLogger , Logger } ;
122122
123123use lightning:: chain:: keysinterface:: EntropySource ;
124124use lightning:: chain:: { chainmonitor, BestBlock , Confirm , Watch } ;
@@ -173,7 +173,9 @@ const DEFAULT_STORAGE_DIR_PATH: &str = "/tmp/ldk_node/";
173173const DEFAULT_NETWORK : Network = Network :: Bitcoin ;
174174const DEFAULT_LISTENING_ADDR : & str = "0.0.0.0:9735" ;
175175const DEFAULT_CLTV_EXPIRY_DELTA : u32 = 144 ;
176- const DEFAULT_ESPLORA_SERVER_URL : & str = "https://blockstream.info/api" ;
176+ const DEFAULT_BDK_WALLET_SYNC_INTERVAL_SECS : u64 = 60 ;
177+ const DEFAULT_LDK_WALLET_SYNC_INTERVAL_SECS : u64 = 20 ;
178+ const DEFAULT_FEE_RATE_CACHE_UPDATE_INTERVAL_SECS : u64 = 60 * 60 ;
177179const DEFAULT_LOG_LEVEL : LogLevel = LogLevel :: Debug ;
178180
179181// The 'stop gap' parameter used by BDK's wallet sync. This seems to configure the threshold
@@ -183,6 +185,9 @@ const BDK_CLIENT_STOP_GAP: usize = 20;
183185// The number of concurrent requests made against the API provider.
184186const BDK_CLIENT_CONCURRENCY : u8 = 8 ;
185187
188+ // The default Esplora server we're using.
189+ const DEFAULT_ESPLORA_SERVER_URL : & str = "https://blockstream.info/api" ;
190+
186191// The timeout after which we abandon retrying failed payments.
187192const LDK_PAYMENT_RETRY_TIMEOUT : Duration = Duration :: from_secs ( 10 ) ;
188193
@@ -203,13 +208,16 @@ const WALLET_KEYS_SEED_LEN: usize = 64;
203208///
204209/// ### Defaults
205210///
206- /// | Parameter | Value |
207- /// |-----------------------------|------------------|
208- /// | `storage_dir_path` | /tmp/ldk_node/ |
209- /// | `network` | Network::Bitcoin |
210- /// | `listening_address` | 0.0.0.0:9735 |
211- /// | `default_cltv_expiry_delta` | 144 |
212- /// | `log_level` | `Debug` |
211+ /// | Parameter | Value |
212+ /// |----------------------------------------|------------------|
213+ /// | `storage_dir_path` | /tmp/ldk_node/ |
214+ /// | `network` | Network::Bitcoin |
215+ /// | `listening_address` | 0.0.0.0:9735 |
216+ /// | `default_cltv_expiry_delta` | 144 |
217+ /// | `onchain_wallet_sync_interval_secs` | 60 |
218+ /// | `wallet_sync_interval_secs` | 20 |
219+ /// | `fee_rate_cache_update_interval_secs` | 3600 |
220+ /// | `log_level` | `Debug` |
213221///
214222pub struct Config {
215223 /// The path where the underlying LDK and BDK persist their data.
@@ -220,6 +228,18 @@ pub struct Config {
220228 pub listening_address : Option < NetAddress > ,
221229 /// The default CLTV expiry delta to be used for payments.
222230 pub default_cltv_expiry_delta : u32 ,
231+ /// The time in-between background sync attempts of the onchain wallet, in seconds.
232+ ///
233+ /// **Note:** A minimum of 10 seconds is always enforced.
234+ pub onchain_wallet_sync_interval_secs : u64 ,
235+ /// The time in-between background sync attempts of the LDK wallet, in seconds.
236+ ///
237+ /// **Note:** A minimum of 10 seconds is always enforced.
238+ pub wallet_sync_interval_secs : u64 ,
239+ /// The time in-between background update attempts to our fee rate cache, in seconds.
240+ ///
241+ /// **Note:** A minimum of 10 seconds is always enforced.
242+ pub fee_rate_cache_update_interval_secs : u64 ,
223243 /// The level at which we log messages.
224244 ///
225245 /// Any messages below this level will be excluded from the logs.
@@ -233,6 +253,9 @@ impl Default for Config {
233253 network : DEFAULT_NETWORK ,
234254 listening_address : Some ( DEFAULT_LISTENING_ADDR . parse ( ) . unwrap ( ) ) ,
235255 default_cltv_expiry_delta : DEFAULT_CLTV_EXPIRY_DELTA ,
256+ onchain_wallet_sync_interval_secs : DEFAULT_BDK_WALLET_SYNC_INTERVAL_SECS ,
257+ wallet_sync_interval_secs : DEFAULT_LDK_WALLET_SYNC_INTERVAL_SECS ,
258+ fee_rate_cache_update_interval_secs : DEFAULT_FEE_RATE_CACHE_UPDATE_INTERVAL_SECS ,
236259 log_level : DEFAULT_LOG_LEVEL ,
237260 }
238261 }
@@ -766,40 +789,57 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
766789
767790 let runtime = tokio:: runtime:: Builder :: new_multi_thread ( ) . enable_all ( ) . build ( ) . unwrap ( ) ;
768791
769- let event_handler = Arc :: new ( EventHandler :: new (
770- Arc :: clone ( & self . wallet ) ,
771- Arc :: clone ( & self . event_queue ) ,
772- Arc :: clone ( & self . channel_manager ) ,
773- Arc :: clone ( & self . network_graph ) ,
774- Arc :: clone ( & self . keys_manager ) ,
775- Arc :: clone ( & self . payment_store ) ,
776- Arc :: clone ( & self . runtime ) ,
777- Arc :: clone ( & self . logger ) ,
778- Arc :: clone ( & self . config ) ,
779- ) ) ;
792+ // Block to ensure we update our fee rate cache once on startup
793+ let wallet = Arc :: clone ( & self . wallet ) ;
794+ let sync_logger = Arc :: clone ( & self . logger ) ;
795+ runtime. block_on ( async move {
796+ let now = Instant :: now ( ) ;
797+ match wallet. update_fee_estimates ( ) . await {
798+ Ok ( ( ) ) => {
799+ log_info ! (
800+ sync_logger,
801+ "Initial fee rate cache update finished in {}ms." ,
802+ now. elapsed( ) . as_millis( )
803+ ) ;
804+ Ok ( ( ) )
805+ }
806+ Err ( e) => {
807+ log_error ! ( sync_logger, "Initial fee rate cache update failed: {}" , e, ) ;
808+ Err ( e)
809+ }
810+ }
811+ } ) ?;
780812
781813 // Setup wallet sync
782814 let wallet = Arc :: clone ( & self . wallet ) ;
783- let tx_sync = Arc :: clone ( & self . tx_sync ) ;
784- let sync_cman = Arc :: clone ( & self . channel_manager ) ;
785- let sync_cmon = Arc :: clone ( & self . chain_monitor ) ;
786815 let sync_logger = Arc :: clone ( & self . logger ) ;
787816 let mut stop_sync = self . stop_receiver . clone ( ) ;
788-
817+ let onchain_wallet_sync_interval_secs =
818+ self . config . onchain_wallet_sync_interval_secs . max ( 10 ) ;
819+ let fee_rate_cache_update_interval_secs =
820+ self . config . fee_rate_cache_update_interval_secs . max ( 10 ) ;
789821 std:: thread:: spawn ( move || {
790822 tokio:: runtime:: Builder :: new_current_thread ( ) . enable_all ( ) . build ( ) . unwrap ( ) . block_on (
791823 async move {
792- let mut interval = tokio:: time:: interval ( Duration :: from_secs ( 30 ) ) ;
793- interval. set_missed_tick_behavior ( tokio:: time:: MissedTickBehavior :: Skip ) ;
824+ let mut onchain_wallet_sync_interval = tokio:: time:: interval (
825+ Duration :: from_secs ( onchain_wallet_sync_interval_secs) ,
826+ ) ;
827+ onchain_wallet_sync_interval
828+ . set_missed_tick_behavior ( tokio:: time:: MissedTickBehavior :: Skip ) ;
829+ let mut fee_rate_update_interval = tokio:: time:: interval ( Duration :: from_secs (
830+ fee_rate_cache_update_interval_secs,
831+ ) ) ;
832+ // We just blocked on updating, so skip the first tick.
833+ fee_rate_update_interval. reset ( ) ;
794834 loop {
795835 let now = Instant :: now ( ) ;
796836 tokio:: select! {
797837 _ = stop_sync. changed( ) => {
798838 return ;
799839 }
800- _ = interval . tick( ) => {
840+ _ = onchain_wallet_sync_interval . tick( ) => {
801841 match wallet. sync( ) . await {
802- Ok ( ( ) ) => log_info !(
842+ Ok ( ( ) ) => log_trace !(
803843 sync_logger,
804844 "Background sync of on-chain wallet finished in {}ms." ,
805845 now. elapsed( ) . as_millis( )
@@ -813,12 +853,64 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
813853 }
814854 }
815855 }
856+ _ = fee_rate_update_interval. tick( ) => {
857+ match wallet. update_fee_estimates( ) . await {
858+ Ok ( ( ) ) => log_trace!(
859+ sync_logger,
860+ "Background update of fee rate cache finished in {}ms." ,
861+ now. elapsed( ) . as_millis( )
862+ ) ,
863+ Err ( err) => {
864+ log_error!(
865+ sync_logger,
866+ "Background update of fee rate cache failed: {}" ,
867+ err
868+ )
869+ }
870+ }
871+ }
816872 }
817873 }
818874 } ,
819875 ) ;
820876 } ) ;
821877
878+ let tx_sync = Arc :: clone ( & self . tx_sync ) ;
879+ let sync_cman = Arc :: clone ( & self . channel_manager ) ;
880+ let sync_cmon = Arc :: clone ( & self . chain_monitor ) ;
881+ let sync_logger = Arc :: clone ( & self . logger ) ;
882+ let mut stop_sync = self . stop_receiver . clone ( ) ;
883+ let wallet_sync_interval_secs = self . config . wallet_sync_interval_secs . max ( 10 ) ;
884+ runtime. spawn ( async move {
885+ let mut wallet_sync_interval =
886+ tokio:: time:: interval ( Duration :: from_secs ( wallet_sync_interval_secs) ) ;
887+ wallet_sync_interval. set_missed_tick_behavior ( tokio:: time:: MissedTickBehavior :: Skip ) ;
888+ loop {
889+ let now = Instant :: now ( ) ;
890+ tokio:: select! {
891+ _ = stop_sync. changed( ) => {
892+ return ;
893+ }
894+ _ = wallet_sync_interval. tick( ) => {
895+ let confirmables = vec![
896+ & * sync_cman as & ( dyn Confirm + Sync + Send ) ,
897+ & * sync_cmon as & ( dyn Confirm + Sync + Send ) ,
898+ ] ;
899+ match tx_sync. sync( confirmables) . await {
900+ Ok ( ( ) ) => log_trace!(
901+ sync_logger,
902+ "Background sync of Lightning wallet finished in {}ms." ,
903+ now. elapsed( ) . as_millis( )
904+ ) ,
905+ Err ( e) => {
906+ log_error!( sync_logger, "Background sync of Lightning wallet failed: {}" , e)
907+ }
908+ }
909+ }
910+ }
911+ }
912+ } ) ;
913+
822914 if self . gossip_source . is_rgs ( ) {
823915 let gossip_source = Arc :: clone ( & self . gossip_source ) ;
824916 let gossip_sync_store = Arc :: clone ( & self . kv_store ) ;
@@ -836,7 +928,7 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
836928 let now = Instant :: now( ) ;
837929 match gossip_source. update_rgs_snapshot( ) . await {
838930 Ok ( updated_timestamp) => {
839- log_info !(
931+ log_trace !(
840932 gossip_sync_logger,
841933 "Background sync of RGS gossip data finished in {}ms." ,
842934 now. elapsed( ) . as_millis( )
@@ -860,37 +952,6 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
860952 } ) ;
861953 }
862954
863- let sync_logger = Arc :: clone ( & self . logger ) ;
864- let mut stop_sync = self . stop_receiver . clone ( ) ;
865- runtime. spawn ( async move {
866- let mut interval = tokio:: time:: interval ( Duration :: from_secs ( 10 ) ) ;
867- interval. set_missed_tick_behavior ( tokio:: time:: MissedTickBehavior :: Skip ) ;
868- loop {
869- let now = Instant :: now ( ) ;
870- tokio:: select! {
871- _ = stop_sync. changed( ) => {
872- return ;
873- }
874- _ = interval. tick( ) => {
875- let confirmables = vec![
876- & * sync_cman as & ( dyn Confirm + Sync + Send ) ,
877- & * sync_cmon as & ( dyn Confirm + Sync + Send ) ,
878- ] ;
879- match tx_sync. sync( confirmables) . await {
880- Ok ( ( ) ) => log_info!(
881- sync_logger,
882- "Background sync of Lightning wallet finished in {}ms." ,
883- now. elapsed( ) . as_millis( )
884- ) ,
885- Err ( e) => {
886- log_error!( sync_logger, "Background sync of Lightning wallet failed: {}" , e)
887- }
888- }
889- }
890- }
891- }
892- } ) ;
893-
894955 if let Some ( listening_address) = & self . config . listening_address {
895956 // Setup networking
896957 let peer_manager_connection_handler = Arc :: clone ( & self . peer_manager ) ;
@@ -1024,6 +1085,18 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
10241085 }
10251086 } ) ;
10261087
1088+ let event_handler = Arc :: new ( EventHandler :: new (
1089+ Arc :: clone ( & self . wallet ) ,
1090+ Arc :: clone ( & self . event_queue ) ,
1091+ Arc :: clone ( & self . channel_manager ) ,
1092+ Arc :: clone ( & self . network_graph ) ,
1093+ Arc :: clone ( & self . keys_manager ) ,
1094+ Arc :: clone ( & self . payment_store ) ,
1095+ Arc :: clone ( & self . runtime ) ,
1096+ Arc :: clone ( & self . logger ) ,
1097+ Arc :: clone ( & self . config ) ,
1098+ ) ) ;
1099+
10271100 // Setup background processing
10281101 let background_persister = Arc :: clone ( & self . kv_store ) ;
10291102 let background_event_handler = Arc :: clone ( & event_handler) ;
0 commit comments