Skip to content

Commit 18ef772

Browse files
committed
Adjust MUP to read only old monitor height instead of deserializing whole monitor
1 parent 281a0ae commit 18ef772

File tree

1 file changed

+53
-21
lines changed

1 file changed

+53
-21
lines changed

lightning/src/util/persist.rs

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use core::ops::Deref;
1414
use bitcoin::hashes::hex::{FromHex, ToHex};
1515
use bitcoin::{BlockHash, Txid};
1616

17-
use crate::{io, log_error};
17+
use crate::{io, io::Read, log_error};
1818
use crate::alloc::string::ToString;
1919
use crate::prelude::*;
2020

@@ -538,6 +538,34 @@ where
538538
}
539539
}
540540

541+
/// Read a channel monitor's height only.
542+
fn read_monitor_height(
543+
&self, monitor_name: &MonitorName,
544+
) -> Result<u64, io::Error> {
545+
let mut monitor_cursor = io::Cursor::new(self.kv_store.read(
546+
CHANNEL_MONITOR_PERSISTENCE_PRIMARY_NAMESPACE,
547+
CHANNEL_MONITOR_PERSISTENCE_SECONDARY_NAMESPACE,
548+
monitor_name.as_str(),
549+
)?);
550+
// Discard the sentinel bytes if found.
551+
if monitor_cursor.get_ref().starts_with(MONITOR_UPDATING_PERSISTER_PREPEND_SENTINEL) {
552+
monitor_cursor.set_position(MONITOR_UPDATING_PERSISTER_PREPEND_SENTINEL.len() as u64);
553+
}
554+
555+
// This doesn't compile because of an error type mismatch
556+
// let height = 0u64;
557+
// decode_tlv_stream!(monitor_cursor, {
558+
// (0, height, required),
559+
// });
560+
561+
// Skip T, L, and get to V
562+
monitor_cursor.set_position(monitor_cursor.position() + 2);
563+
let mut buf = [0; 8];
564+
monitor_cursor.read_exact(&mut buf)?;
565+
let height: u64 = u64::from_ne_bytes(buf);
566+
Ok(height)
567+
}
568+
541569
/// Read a channel monitor update.
542570
fn read_monitor_update(
543571
&self, monitor_name: &MonitorName, update_name: &UpdateName,
@@ -610,24 +638,28 @@ where
610638
) -> chain::ChannelMonitorUpdateStatus {
611639
// Determine the proper key for this monitor
612640
let monitor_name = MonitorName::from(funding_txo);
613-
let maybe_old_monitor = self.read_monitor(&monitor_name);
614-
match maybe_old_monitor {
615-
Ok((_, ref old_monitor)) => {
616-
// Check that this key isn't already storing a monitor with a higher update_id
617-
// (collision)
618-
if old_monitor.get_latest_update_id() > monitor.get_latest_update_id() {
619-
log_error!(
620-
self.logger,
621-
"Tried to write a monitor at the same outpoint {} with a higher update_id!",
622-
monitor_name.as_str()
623-
);
624-
return chain::ChannelMonitorUpdateStatus::UnrecoverableError;
625-
}
626-
}
627-
// This means the channel monitor is new.
628-
Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
629-
_ => return chain::ChannelMonitorUpdateStatus::UnrecoverableError,
630-
}
641+
let maybe_old_monitor_height = self.read_monitor_height(&monitor_name);
642+
643+
// // Can this check be sacrificed?
644+
// let maybe_old_monitor = self.read_monitor(&monitor_name);
645+
// match maybe_old_monitor {
646+
// Ok((_, ref old_monitor)) => {
647+
// // Check that this key isn't already storing a monitor with a higher update_id
648+
// // (collision)
649+
// if old_monitor.get_latest_update_id() > monitor.get_latest_update_id() {
650+
// log_error!(
651+
// self.logger,
652+
// "Tried to write a monitor at the same outpoint {} with a higher update_id!",
653+
// monitor_name.as_str()
654+
// );
655+
// return chain::ChannelMonitorUpdateStatus::UnrecoverableError;
656+
// }
657+
// }
658+
// // This means the channel monitor is new.
659+
// Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
660+
// _ => return chain::ChannelMonitorUpdateStatus::UnrecoverableError,
661+
// }
662+
631663
// Serialize and write the new monitor
632664
let mut monitor_bytes = Vec::with_capacity(
633665
MONITOR_UPDATING_PERSISTER_PREPEND_SENTINEL.len() + monitor.serialized_length(),
@@ -643,8 +675,8 @@ where
643675
Ok(_) => {
644676
// Assess cleanup. Typically, we'll clean up only between the last two known full
645677
// monitors.
646-
if let Ok((_, old_monitor)) = maybe_old_monitor {
647-
let start = old_monitor.get_latest_update_id();
678+
if let Ok(old_monitor_height) = maybe_old_monitor_height {
679+
let start = old_monitor_height;
648680
let end = if monitor.get_latest_update_id() == CLOSED_CHANNEL_UPDATE_ID {
649681
// We don't want to clean the rest of u64, so just do possible pending
650682
// updates. Note that we never write updates at

0 commit comments

Comments
 (0)