@@ -26,7 +26,7 @@ use secp256k1::{Secp256k1,Message,Signature};
26
26
use secp256k1:: key:: { SecretKey , PublicKey } ;
27
27
use secp256k1;
28
28
29
- use ln:: msgs:: { DecodeError , HandleError } ;
29
+ use ln:: msgs:: DecodeError ;
30
30
use ln:: chan_utils;
31
31
use ln:: chan_utils:: HTLCOutputInCommitment ;
32
32
use chain:: chaininterface:: { ChainListener , ChainWatchInterface , BroadcasterInterface } ;
@@ -74,6 +74,14 @@ pub enum ChannelMonitorUpdateErr {
74
74
PermanentFailure ,
75
75
}
76
76
77
+ /// General Err type for ChannelMonitor actions. Generally, this implies that the data provided is
78
+ /// inconsistent with the ChannelMonitor being called. eg for ChannelMonitor::insert_combine this
79
+ /// means you tried to merge two monitors for different channels or for a channel which was
80
+ /// restored from a backup and then generated new commitment updates.
81
+ /// Contains a human-readable error message.
82
+ #[ derive( Debug ) ]
83
+ pub struct MonitorUpdateError ( pub & ' static str ) ;
84
+
77
85
/// Simple trait indicating ability to track a set of ChannelMonitors and multiplex events between
78
86
/// them. Generally should be implemented by keeping a local SimpleManyChannelMonitor and passing
79
87
/// events to it, while also taking any add_update_monitor events and passing them to some remote
@@ -158,7 +166,7 @@ impl<Key : Send + cmp::Eq + hash::Hash + 'static> SimpleManyChannelMonitor<Key>
158
166
}
159
167
160
168
/// Adds or udpates the monitor which monitors the channel referred to by the given key.
161
- pub fn add_update_monitor_by_key ( & self , key : Key , monitor : ChannelMonitor ) -> Result < ( ) , HandleError > {
169
+ pub fn add_update_monitor_by_key ( & self , key : Key , monitor : ChannelMonitor ) -> Result < ( ) , MonitorUpdateError > {
162
170
let mut monitors = self . monitors . lock ( ) . unwrap ( ) ;
163
171
match monitors. get_mut ( & key) {
164
172
Some ( orig_monitor) => {
@@ -408,12 +416,12 @@ impl ChannelMonitor {
408
416
/// Inserts a revocation secret into this channel monitor. Prunes old preimages if neither
409
417
/// needed by local commitment transactions HTCLs nor by remote ones. Unless we haven't already seen remote
410
418
/// commitment transaction's secret, they are de facto pruned (we can use revocation key).
411
- pub ( super ) fn provide_secret ( & mut self , idx : u64 , secret : [ u8 ; 32 ] ) -> Result < ( ) , HandleError > {
419
+ pub ( super ) fn provide_secret ( & mut self , idx : u64 , secret : [ u8 ; 32 ] ) -> Result < ( ) , MonitorUpdateError > {
412
420
let pos = ChannelMonitor :: place_secret ( idx) ;
413
421
for i in 0 ..pos {
414
422
let ( old_secret, old_idx) = self . old_secrets [ i as usize ] ;
415
423
if ChannelMonitor :: derive_secret ( secret, pos, old_idx) != old_secret {
416
- return Err ( HandleError { err : "Previous secret did not match new one" , action : None } )
424
+ return Err ( MonitorUpdateError ( "Previous secret did not match new one" ) ) ;
417
425
}
418
426
}
419
427
self . old_secrets [ pos as usize ] = ( secret, idx) ;
@@ -537,12 +545,12 @@ impl ChannelMonitor {
537
545
/// Combines this ChannelMonitor with the information contained in the other ChannelMonitor.
538
546
/// After a successful call this ChannelMonitor is up-to-date and is safe to use to monitor the
539
547
/// chain for new blocks/transactions.
540
- pub fn insert_combine ( & mut self , mut other : ChannelMonitor ) -> Result < ( ) , HandleError > {
548
+ pub fn insert_combine ( & mut self , mut other : ChannelMonitor ) -> Result < ( ) , MonitorUpdateError > {
541
549
if self . funding_txo . is_some ( ) {
542
550
// We should be able to compare the entire funding_txo, but in fuzztarget its trivially
543
551
// easy to collide the funding_txo hash and have a different scriptPubKey.
544
552
if other. funding_txo . is_some ( ) && other. funding_txo . as_ref ( ) . unwrap ( ) . 0 != self . funding_txo . as_ref ( ) . unwrap ( ) . 0 {
545
- return Err ( HandleError { err : "Funding transaction outputs are not identical!" , action : None } ) ;
553
+ return Err ( MonitorUpdateError ( "Funding transaction outputs are not identical!" ) ) ;
546
554
}
547
555
} else {
548
556
self . funding_txo = other. funding_txo . take ( ) ;
@@ -830,14 +838,14 @@ impl ChannelMonitor {
830
838
//we want to leave out (eg funding_txo, etc).
831
839
832
840
/// Can only fail if idx is < get_min_seen_secret
833
- pub ( super ) fn get_secret ( & self , idx : u64 ) -> Result < [ u8 ; 32 ] , HandleError > {
841
+ pub ( super ) fn get_secret ( & self , idx : u64 ) -> Option < [ u8 ; 32 ] > {
834
842
for i in 0 ..self . old_secrets . len ( ) {
835
843
if ( idx & ( !( ( 1 << i) - 1 ) ) ) == self . old_secrets [ i] . 1 {
836
- return Ok ( ChannelMonitor :: derive_secret ( self . old_secrets [ i] . 0 , i as u8 , idx) )
844
+ return Some ( ChannelMonitor :: derive_secret ( self . old_secrets [ i] . 0 , i as u8 , idx) )
837
845
}
838
846
}
839
847
assert ! ( idx < self . get_min_seen_secret( ) ) ;
840
- Err ( HandleError { err : "idx too low" , action : None } )
848
+ None
841
849
}
842
850
843
851
pub ( super ) fn get_min_seen_secret ( & self ) -> u64 {
@@ -1215,7 +1223,7 @@ impl ChannelMonitor {
1215
1223
} ;
1216
1224
}
1217
1225
1218
- let secret = ignore_error ! ( self . get_secret( commitment_number) ) ;
1226
+ let secret = if let Some ( secret ) = self . get_secret ( commitment_number) { secret } else { return ( None , None ) ; } ;
1219
1227
let per_commitment_key = ignore_error ! ( SecretKey :: from_slice( & self . secp_ctx, & secret) ) ;
1220
1228
let per_commitment_point = PublicKey :: from_secret_key ( & self . secp_ctx , & per_commitment_key) ;
1221
1229
let revocation_pubkey = match self . key_storage {
@@ -1808,7 +1816,7 @@ mod tests {
1808
1816
idx -= 1 ;
1809
1817
}
1810
1818
assert_eq!( monitor. get_min_seen_secret( ) , idx + 1 ) ;
1811
- assert!( monitor. get_secret( idx) . is_err ( ) ) ;
1819
+ assert!( monitor. get_secret( idx) . is_none ( ) ) ;
1812
1820
} ;
1813
1821
}
1814
1822
@@ -1870,7 +1878,7 @@ mod tests {
1870
1878
1871
1879
secrets. push ( [ 0 ; 32 ] ) ;
1872
1880
secrets. last_mut ( ) . unwrap ( ) [ 0 ..32 ] . clone_from_slice ( & hex:: decode ( "c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964" ) . unwrap ( ) ) ;
1873
- assert_eq ! ( monitor. provide_secret( 281474976710654 , secrets. last( ) . unwrap( ) . clone( ) ) . unwrap_err( ) . err ,
1881
+ assert_eq ! ( monitor. provide_secret( 281474976710654 , secrets. last( ) . unwrap( ) . clone( ) ) . unwrap_err( ) . 0 ,
1874
1882
"Previous secret did not match new one" ) ;
1875
1883
}
1876
1884
@@ -1896,7 +1904,7 @@ mod tests {
1896
1904
1897
1905
secrets. push ( [ 0 ; 32 ] ) ;
1898
1906
secrets. last_mut ( ) . unwrap ( ) [ 0 ..32 ] . clone_from_slice ( & hex:: decode ( "27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116" ) . unwrap ( ) ) ;
1899
- assert_eq ! ( monitor. provide_secret( 281474976710652 , secrets. last( ) . unwrap( ) . clone( ) ) . unwrap_err( ) . err ,
1907
+ assert_eq ! ( monitor. provide_secret( 281474976710652 , secrets. last( ) . unwrap( ) . clone( ) ) . unwrap_err( ) . 0 ,
1900
1908
"Previous secret did not match new one" ) ;
1901
1909
}
1902
1910
@@ -1922,7 +1930,7 @@ mod tests {
1922
1930
1923
1931
secrets. push ( [ 0 ; 32 ] ) ;
1924
1932
secrets. last_mut ( ) . unwrap ( ) [ 0 ..32 ] . clone_from_slice ( & hex:: decode ( "27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116" ) . unwrap ( ) ) ;
1925
- assert_eq ! ( monitor. provide_secret( 281474976710652 , secrets. last( ) . unwrap( ) . clone( ) ) . unwrap_err( ) . err ,
1933
+ assert_eq ! ( monitor. provide_secret( 281474976710652 , secrets. last( ) . unwrap( ) . clone( ) ) . unwrap_err( ) . 0 ,
1926
1934
"Previous secret did not match new one" ) ;
1927
1935
}
1928
1936
@@ -1968,7 +1976,7 @@ mod tests {
1968
1976
1969
1977
secrets. push ( [ 0 ; 32 ] ) ;
1970
1978
secrets. last_mut ( ) . unwrap ( ) [ 0 ..32 ] . clone_from_slice ( & hex:: decode ( "05cde6323d949933f7f7b78776bcc1ea6d9b31447732e3802e1f7ac44b650e17" ) . unwrap ( ) ) ;
1971
- assert_eq ! ( monitor. provide_secret( 281474976710648 , secrets. last( ) . unwrap( ) . clone( ) ) . unwrap_err( ) . err ,
1979
+ assert_eq ! ( monitor. provide_secret( 281474976710648 , secrets. last( ) . unwrap( ) . clone( ) ) . unwrap_err( ) . 0 ,
1972
1980
"Previous secret did not match new one" ) ;
1973
1981
}
1974
1982
@@ -2004,7 +2012,7 @@ mod tests {
2004
2012
2005
2013
secrets. push ( [ 0 ; 32 ] ) ;
2006
2014
secrets. last_mut ( ) . unwrap ( ) [ 0 ..32 ] . clone_from_slice ( & hex:: decode ( "969660042a28f32d9be17344e09374b379962d03db1574df5a8a5a47e19ce3f2" ) . unwrap ( ) ) ;
2007
- assert_eq ! ( monitor. provide_secret( 281474976710650 , secrets. last( ) . unwrap( ) . clone( ) ) . unwrap_err( ) . err ,
2015
+ assert_eq ! ( monitor. provide_secret( 281474976710650 , secrets. last( ) . unwrap( ) . clone( ) ) . unwrap_err( ) . 0 ,
2008
2016
"Previous secret did not match new one" ) ;
2009
2017
}
2010
2018
@@ -2050,7 +2058,7 @@ mod tests {
2050
2058
2051
2059
secrets. push ( [ 0 ; 32 ] ) ;
2052
2060
secrets. last_mut ( ) . unwrap ( ) [ 0 ..32 ] . clone_from_slice ( & hex:: decode ( "05cde6323d949933f7f7b78776bcc1ea6d9b31447732e3802e1f7ac44b650e17" ) . unwrap ( ) ) ;
2053
- assert_eq ! ( monitor. provide_secret( 281474976710648 , secrets. last( ) . unwrap( ) . clone( ) ) . unwrap_err( ) . err ,
2061
+ assert_eq ! ( monitor. provide_secret( 281474976710648 , secrets. last( ) . unwrap( ) . clone( ) ) . unwrap_err( ) . 0 ,
2054
2062
"Previous secret did not match new one" ) ;
2055
2063
}
2056
2064
@@ -2096,7 +2104,7 @@ mod tests {
2096
2104
2097
2105
secrets. push ( [ 0 ; 32 ] ) ;
2098
2106
secrets. last_mut ( ) . unwrap ( ) [ 0 ..32 ] . clone_from_slice ( & hex:: decode ( "05cde6323d949933f7f7b78776bcc1ea6d9b31447732e3802e1f7ac44b650e17" ) . unwrap ( ) ) ;
2099
- assert_eq ! ( monitor. provide_secret( 281474976710648 , secrets. last( ) . unwrap( ) . clone( ) ) . unwrap_err( ) . err ,
2107
+ assert_eq ! ( monitor. provide_secret( 281474976710648 , secrets. last( ) . unwrap( ) . clone( ) ) . unwrap_err( ) . 0 ,
2100
2108
"Previous secret did not match new one" ) ;
2101
2109
}
2102
2110
@@ -2142,7 +2150,7 @@ mod tests {
2142
2150
2143
2151
secrets. push ( [ 0 ; 32 ] ) ;
2144
2152
secrets. last_mut ( ) . unwrap ( ) [ 0 ..32 ] . clone_from_slice ( & hex:: decode ( "a7efbc61aac46d34f77778bac22c8a20c6a46ca460addc49009bda875ec88fa4" ) . unwrap ( ) ) ;
2145
- assert_eq ! ( monitor. provide_secret( 281474976710648 , secrets. last( ) . unwrap( ) . clone( ) ) . unwrap_err( ) . err ,
2153
+ assert_eq ! ( monitor. provide_secret( 281474976710648 , secrets. last( ) . unwrap( ) . clone( ) ) . unwrap_err( ) . 0 ,
2146
2154
"Previous secret did not match new one" ) ;
2147
2155
}
2148
2156
}
0 commit comments