@@ -14,7 +14,7 @@ use core::ops::Deref;
14
14
use bitcoin:: hashes:: hex:: { FromHex , ToHex } ;
15
15
use bitcoin:: { BlockHash , Txid } ;
16
16
17
- use crate :: { io, log_error} ;
17
+ use crate :: { io, io :: Read , log_error} ;
18
18
use crate :: alloc:: string:: ToString ;
19
19
use crate :: prelude:: * ;
20
20
@@ -538,6 +538,34 @@ where
538
538
}
539
539
}
540
540
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
+
541
569
/// Read a channel monitor update.
542
570
fn read_monitor_update (
543
571
& self , monitor_name : & MonitorName , update_name : & UpdateName ,
@@ -610,24 +638,28 @@ where
610
638
) -> chain:: ChannelMonitorUpdateStatus {
611
639
// Determine the proper key for this monitor
612
640
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
+
631
663
// Serialize and write the new monitor
632
664
let mut monitor_bytes = Vec :: with_capacity (
633
665
MONITOR_UPDATING_PERSISTER_PREPEND_SENTINEL . len ( ) + monitor. serialized_length ( ) ,
@@ -643,8 +675,8 @@ where
643
675
Ok ( _) => {
644
676
// Assess cleanup. Typically, we'll clean up only between the last two known full
645
677
// 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 ;
648
680
let end = if monitor. get_latest_update_id ( ) == CLOSED_CHANNEL_UPDATE_ID {
649
681
// We don't want to clean the rest of u64, so just do possible pending
650
682
// updates. Note that we never write updates at
0 commit comments