Skip to content

Commit 4974e8f

Browse files
author
Fernando Ledesma
committed
add lightweight update_chain_tip() for fast invoice expiry refresh
1 parent 1179080 commit 4974e8f

4 files changed

Lines changed: 70 additions & 14 deletions

File tree

src/chain/esplora.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,34 @@ impl EsploraChainSource {
363363
Ok(())
364364
}
365365

366+
pub(super) async fn fetch_chain_tip(
367+
&self,
368+
) -> Result<(bitcoin::block::Header, u32), Error> {
369+
let tip_hash = self.esplora_client.get_tip_hash().await.map_err(|e| {
370+
log_error!(self.logger, "Failed to get chain tip hash: {}", e);
371+
Error::TxSyncFailed
372+
})?;
373+
374+
let header =
375+
self.esplora_client.get_header_by_hash(&tip_hash).await.map_err(|e| {
376+
log_error!(self.logger, "Failed to get block header: {}", e);
377+
Error::TxSyncFailed
378+
})?;
379+
380+
let status =
381+
self.esplora_client.get_block_status(&tip_hash).await.map_err(|e| {
382+
log_error!(self.logger, "Failed to get block status: {}", e);
383+
Error::TxSyncFailed
384+
})?;
385+
386+
let height = status.height.ok_or_else(|| {
387+
log_error!(self.logger, "Block not in best chain");
388+
Error::TxSyncFailed
389+
})?;
390+
391+
Ok((header, height))
392+
}
393+
366394
pub(crate) async fn process_broadcast_package(&self, package: Vec<Transaction>) {
367395
for tx in &package {
368396
let txid = tx.compute_txid();

src/chain/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,17 @@ impl ChainSource {
431431
}
432432
}
433433

434+
pub(crate) async fn fetch_chain_tip(
435+
&self,
436+
) -> Result<(bitcoin::block::Header, u32), Error> {
437+
match &self.kind {
438+
ChainSourceKind::Esplora(esplora_chain_source) => {
439+
esplora_chain_source.fetch_chain_tip().await
440+
},
441+
_ => Err(Error::TxSyncFailed),
442+
}
443+
}
444+
434445
pub(crate) async fn continuously_process_broadcast_queue(
435446
&self, mut stop_tx_bcast_receiver: tokio::sync::watch::Receiver<()>,
436447
) {

src/io/utils.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use crate::fee_estimator::OnchainFeeEstimator;
4545
use crate::io::{
4646
NODE_METRICS_KEY, NODE_METRICS_PRIMARY_NAMESPACE, NODE_METRICS_SECONDARY_NAMESPACE,
4747
};
48-
use crate::logger::{log_error, LdkLogger, Logger};
48+
use crate::logger::{log_error, log_info, LdkLogger, Logger};
4949
use crate::peer_store::PeerStore;
5050
use crate::types::{Broadcaster, DynStore, KeysManager, Sweeper, WordCount};
5151
use crate::wallet::ser::{ChangeSetDeserWrapper, ChangeSetSerWrapper};
@@ -161,21 +161,24 @@ pub(crate) async fn read_scorer<G: Deref<Target = NetworkGraph<L>>, L: Deref + C
161161
where
162162
L::Target: LdkLogger,
163163
{
164+
log_info!(logger, "[SCORER-LDK] read_scorer() - Attempting to read scorer from VSS store");
164165
let params = ProbabilisticScoringDecayParameters::default();
165-
let mut reader = Cursor::new(
166-
KVStore::read(
167-
&*kv_store,
168-
SCORER_PERSISTENCE_PRIMARY_NAMESPACE,
169-
SCORER_PERSISTENCE_SECONDARY_NAMESPACE,
170-
SCORER_PERSISTENCE_KEY,
171-
)
172-
.await?,
173-
);
166+
let data = KVStore::read(
167+
&*kv_store,
168+
SCORER_PERSISTENCE_PRIMARY_NAMESPACE,
169+
SCORER_PERSISTENCE_SECONDARY_NAMESPACE,
170+
SCORER_PERSISTENCE_KEY,
171+
)
172+
.await?;
173+
log_info!(logger, "[SCORER-LDK] read_scorer() - Read {} bytes from VSS store", data.len());
174+
let mut reader = Cursor::new(data);
174175
let args = (params, network_graph, logger.clone());
175-
ProbabilisticScorer::read(&mut reader, args).map_err(|e| {
176-
log_error!(logger, "Failed to deserialize scorer: {}", e);
176+
let scorer = ProbabilisticScorer::read(&mut reader, args).map_err(|e| {
177+
log_error!(logger, "[SCORER-LDK] read_scorer() - Failed to deserialize scorer: {}", e);
177178
std::io::Error::new(std::io::ErrorKind::InvalidData, "Failed to deserialize Scorer")
178-
})
179+
})?;
180+
log_info!(logger, "[SCORER-LDK] read_scorer() - Successfully deserialized scorer from VSS");
181+
Ok(scorer)
179182
}
180183

181184
/// Read previously persisted external pathfinding scores from the cache.

src/lib.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ use gossip::GossipSource;
133133
use graph::NetworkGraph;
134134
pub use io::utils::generate_entropy_mnemonic;
135135
use io::utils::write_node_metrics;
136-
use lightning::chain::BestBlock;
136+
use lightning::chain::{BestBlock, Confirm};
137137
use lightning::events::bump_transaction::{Input, Wallet as LdkWallet};
138138
use lightning::impl_writeable_tlv_based;
139139
use lightning::ln::chan_utils::{make_funding_redeemscript, FUNDING_TRANSACTION_WITNESS_WEIGHT};
@@ -1505,6 +1505,20 @@ impl Node {
15051505
})
15061506
}
15071507

1508+
/// Fetch the latest block header and update the ChannelManager's `highest_seen_timestamp`.
1509+
pub fn update_chain_tip(&self) -> Result<(), Error> {
1510+
let chain_source = Arc::clone(&self.chain_source);
1511+
let channel_manager = Arc::clone(&self.channel_manager);
1512+
let logger = Arc::clone(&self.logger);
1513+
1514+
self.runtime.block_on(async move {
1515+
let (header, height) = chain_source.fetch_chain_tip().await?;
1516+
channel_manager.best_block_updated(&header, height);
1517+
log_info!(logger, "Chain tip updated to height {}", height);
1518+
Ok(())
1519+
})
1520+
}
1521+
15081522
/// Manually sync the RGS snapshot.
15091523
///
15101524
/// If `do_full_sync` is true, the RGS snapshot will be updated from scratch. Otherwise, the

0 commit comments

Comments
 (0)