Skip to content

Commit b8e5e3f

Browse files
committed
Use piecewise fn for partition factor to account for small nodes startup time.
1 parent 2f29569 commit b8e5e3f

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,11 @@ where C::Target: chain::Filter,
261261
{
262262
let err_str = "ChannelMonitor[Update] persistence failed unrecoverably. This indicates we cannot continue normal operation and must shut down.";
263263
let funding_outpoints = hash_set_from_iter(self.monitors.read().unwrap().keys().cloned());
264+
let channel_count = funding_outpoints.len();
264265
for funding_outpoint in funding_outpoints.iter() {
265266
let monitor_lock = self.monitors.read().unwrap();
266267
if let Some(monitor_state) = monitor_lock.get(funding_outpoint) {
267-
if self.update_monitor_with_chain_data(header, best_height, txdata, &process, funding_outpoint, &monitor_state).is_err() {
268+
if self.update_monitor_with_chain_data(header, best_height, txdata, &process, funding_outpoint, &monitor_state, channel_count).is_err() {
268269
// Take the monitors lock for writing so that we poison it and any future
269270
// operations going forward fail immediately.
270271
core::mem::drop(monitor_lock);
@@ -279,7 +280,7 @@ where C::Target: chain::Filter,
279280
let monitor_states = self.monitors.write().unwrap();
280281
for (funding_outpoint, monitor_state) in monitor_states.iter() {
281282
if !funding_outpoints.contains(funding_outpoint) {
282-
if self.update_monitor_with_chain_data(header, best_height, txdata, &process, funding_outpoint, &monitor_state).is_err() {
283+
if self.update_monitor_with_chain_data(header, best_height, txdata, &process, funding_outpoint, &monitor_state, channel_count).is_err() {
283284
log_error!(self.logger, "{}", err_str);
284285
panic!("{}", err_str);
285286
}
@@ -299,7 +300,7 @@ where C::Target: chain::Filter,
299300

300301
fn update_monitor_with_chain_data<FN>(
301302
&self, header: &Header, best_height: Option<u32>, txdata: &TransactionData, process: FN, funding_outpoint: &OutPoint,
302-
monitor_state: &MonitorHolder<ChannelSigner>,
303+
monitor_state: &MonitorHolder<ChannelSigner>, channel_count: usize,
303304
) -> Result<(), ()> where FN: Fn(&ChannelMonitor<ChannelSigner>, &TransactionData) -> Vec<TransactionOutputs> {
304305
let monitor = &monitor_state.monitor;
305306
let logger = WithChannelMonitor::from(&self.logger, &monitor, None);
@@ -312,9 +313,15 @@ where C::Target: chain::Filter,
312313
let funding_txid_u32 = u32::from_be_bytes([funding_txid_hash_bytes[0], funding_txid_hash_bytes[1], funding_txid_hash_bytes[2], funding_txid_hash_bytes[3]]);
313314
funding_txid_u32.wrapping_add(best_height.unwrap_or_default())
314315
};
315-
const CHAINSYNC_MONITOR_PARTITION_FACTOR: u32 = 50; // ~ 8hours
316+
317+
let partition_factor = if channel_count < 15 {
318+
5
319+
} else {
320+
50 // ~ 8hours
321+
};
322+
316323
let has_pending_claims = monitor_state.monitor.has_pending_claims();
317-
if has_pending_claims || get_partition_key(funding_outpoint) % CHAINSYNC_MONITOR_PARTITION_FACTOR == 0 {
324+
if has_pending_claims || get_partition_key(funding_outpoint) % partition_factor == 0 {
318325
log_trace!(logger, "Syncing Channel Monitor for channel {}", log_funding_info!(monitor));
319326
match self.persister.update_persisted_channel(*funding_outpoint, None, monitor) {
320327
ChannelMonitorUpdateStatus::Completed =>
@@ -889,7 +896,7 @@ mod tests {
889896
use crate::ln::functional_test_utils::*;
890897
use crate::ln::msgs::ChannelMessageHandler;
891898

892-
const CHAINSYNC_MONITOR_PARTITION_FACTOR: u32 = 50;
899+
const CHAINSYNC_MONITOR_PARTITION_FACTOR: u32 = 5;
893900

894901
#[test]
895902
fn test_async_ooo_offchain_updates() {

0 commit comments

Comments
 (0)