@@ -24,7 +24,7 @@ use ln::chan_utils;
24
24
use chain:: chaininterface:: { FeeEstimator , ConfirmationTarget } ;
25
25
use chain:: transaction:: OutPoint ;
26
26
use chain:: keysinterface:: { ChannelKeys , KeysInterface } ;
27
- use util:: transaction_utils;
27
+ use util:: { byte_utils , transaction_utils} ;
28
28
use util:: ser:: { Readable , Writeable , Writer } ;
29
29
use util:: logger:: Logger ;
30
30
use util:: errors:: APIError ;
@@ -34,6 +34,7 @@ use std;
34
34
use std:: default:: Default ;
35
35
use std:: { cmp, mem, fmt} ;
36
36
use std:: ops:: Deref ;
37
+ use std:: collections:: HashMap ;
37
38
use bitcoin:: hashes:: hex:: ToHex ;
38
39
39
40
#[ cfg( test) ]
@@ -100,11 +101,8 @@ enum InboundHTLCState {
100
101
/// Note that we have to keep an eye on the HTLC until we've received a broadcastable
101
102
/// commitment transaction without it as otherwise we'll have to force-close the channel to
102
103
/// claim it before the timeout (obviously doesn't apply to revoked HTLCs that we can't claim
103
- /// anyway). That said, ChannelMonitor does this for us (see
104
- /// ChannelMonitor::would_broadcast_at_height) so we actually remove the HTLC from our own
105
- /// local state before then, once we're sure that the next commitment_signed and
106
- /// ChannelMonitor::provide_latest_local_commitment_tx_info will not include this HTLC.
107
- LocalRemoved ( InboundHTLCRemovalReason ) ,
104
+ /// anyway).
105
+ LocalRemoved ( bool , InboundHTLCRemovalReason ) ,
108
106
}
109
107
110
108
struct InboundHTLCOutput {
@@ -284,6 +282,7 @@ pub(super) struct Channel<ChanSigner: ChannelKeys> {
284
282
pending_inbound_htlcs : Vec < InboundHTLCOutput > ,
285
283
pending_outbound_htlcs : Vec < OutboundHTLCOutput > ,
286
284
holding_cell_htlc_updates : Vec < HTLCUpdateAwaitingACK > ,
285
+ payment_preimages : HashMap < PaymentHash , PaymentPreimage > ,
287
286
288
287
/// When resending CS/RAA messages on channel monitor restoration or on reconnect, we always
289
288
/// need to ensure we resend them in the order we originally generated them. Note that because
@@ -501,6 +500,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
501
500
pending_inbound_htlcs : Vec :: new ( ) ,
502
501
pending_outbound_htlcs : Vec :: new ( ) ,
503
502
holding_cell_htlc_updates : Vec :: new ( ) ,
503
+ payment_preimages : HashMap :: new ( ) ,
504
504
pending_update_fee : None ,
505
505
holding_cell_update_fee : None ,
506
506
next_local_htlc_id : 0 ,
@@ -729,6 +729,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
729
729
pending_inbound_htlcs : Vec :: new ( ) ,
730
730
pending_outbound_htlcs : Vec :: new ( ) ,
731
731
holding_cell_htlc_updates : Vec :: new ( ) ,
732
+ payment_preimages : HashMap :: new ( ) ,
732
733
pending_update_fee : None ,
733
734
holding_cell_update_fee : None ,
734
735
next_local_htlc_id : 0 ,
@@ -786,6 +787,71 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
786
787
Ok ( chan)
787
788
}
788
789
790
+ pub ( super ) fn monitor_would_broadcast_at_height < L : Deref > ( & self , height : u32 , logger : & L ) -> bool where L :: Target : Logger {
791
+ macro_rules! add_htlc_output {
792
+ ( $htlc: expr, $offered: expr, $list: expr) => {
793
+ $list. push( HTLCOutputInCommitment {
794
+ offered: $offered,
795
+ amount_msat: $htlc. amount_msat,
796
+ cltv_expiry: $htlc. cltv_expiry,
797
+ payment_hash: $htlc. payment_hash,
798
+ transaction_output_index: None
799
+ } ) ;
800
+ }
801
+ }
802
+
803
+ let cap = self . pending_inbound_htlcs . len ( ) + self . pending_outbound_htlcs . len ( ) ;
804
+ let mut remote_outputs = Vec :: with_capacity ( cap) ;
805
+ let mut local_outputs = Vec :: with_capacity ( cap) ;
806
+ let awaiting_raa = ( self . channel_state & ChannelState :: AwaitingRemoteRevoke as u32 ) != 0 ;
807
+ for ref htlc in self . pending_inbound_htlcs . iter ( ) {
808
+ match htlc. state {
809
+ InboundHTLCState :: AwaitingAnnouncedRemoteRevoke ( _) => {
810
+ add_htlc_output ! ( htlc, false , local_outputs) ;
811
+ add_htlc_output ! ( htlc, true , remote_outputs) ;
812
+ } ,
813
+ InboundHTLCState :: Committed => {
814
+ add_htlc_output ! ( htlc, false , local_outputs) ;
815
+ add_htlc_output ! ( htlc, true , remote_outputs) ;
816
+ } ,
817
+ InboundHTLCState :: LocalRemoved ( revoked, _) => {
818
+ add_htlc_output ! ( htlc, false , local_outputs) ;
819
+ if awaiting_raa && !revoked {
820
+ add_htlc_output ! ( htlc, true , remote_outputs)
821
+ }
822
+ } ,
823
+ _ => { } ,
824
+ }
825
+ }
826
+ for ref htlc in self . pending_outbound_htlcs . iter ( ) {
827
+ match htlc. state {
828
+ OutboundHTLCState :: LocalAnnounced ( _) => {
829
+ add_htlc_output ! ( htlc, true , local_outputs) ;
830
+ add_htlc_output ! ( htlc, false , remote_outputs) ;
831
+ } ,
832
+ OutboundHTLCState :: Committed => {
833
+ add_htlc_output ! ( htlc, true , local_outputs) ;
834
+ add_htlc_output ! ( htlc, false , remote_outputs) ;
835
+ } ,
836
+ OutboundHTLCState :: RemoteRemoved ( _) => {
837
+ add_htlc_output ! ( htlc, true , local_outputs) ;
838
+ add_htlc_output ! ( htlc, false , remote_outputs)
839
+ } ,
840
+ OutboundHTLCState :: AwaitingRemoteRevokeToRemove ( _) => {
841
+ add_htlc_output ! ( htlc, true , local_outputs) ;
842
+ add_htlc_output ! ( htlc, false , remote_outputs) ;
843
+ } ,
844
+ OutboundHTLCState :: AwaitingRemovedRemoteRevoke ( _) => {
845
+ if awaiting_raa {
846
+ add_htlc_output ! ( htlc, false , remote_outputs)
847
+ }
848
+ } ,
849
+ }
850
+ }
851
+
852
+ ChannelMonitor :: < ChanSigner > :: would_broadcast_at_height_given_htlcs ( local_outputs. iter ( ) , remote_outputs. iter ( ) , height, & self . payment_preimages , logger)
853
+ }
854
+
789
855
// Utilities to build transactions:
790
856
791
857
fn get_commitment_transaction_number_obscure_factor ( & self ) -> u64 {
@@ -899,7 +965,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
899
965
InboundHTLCState :: AwaitingRemoteRevokeToAnnounce ( _) => ( !generated_by_local, "AwaitingRemoteRevokeToAnnounce" ) ,
900
966
InboundHTLCState :: AwaitingAnnouncedRemoteRevoke ( _) => ( true , "AwaitingAnnouncedRemoteRevoke" ) ,
901
967
InboundHTLCState :: Committed => ( true , "Committed" ) ,
902
- InboundHTLCState :: LocalRemoved ( _) => ( !generated_by_local, "LocalRemoved" ) ,
968
+ InboundHTLCState :: LocalRemoved ( revoked , _) => ( !generated_by_local && !revoked , "LocalRemoved" ) ,
903
969
} ;
904
970
905
971
if include {
@@ -908,7 +974,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
908
974
} else {
909
975
log_trace ! ( logger, " ...not including inbound HTLC {} (hash {}) with value {} due to state ({})" , htlc. htlc_id, log_bytes!( htlc. payment_hash. 0 ) , htlc. amount_msat, state_name) ;
910
976
match & htlc. state {
911
- & InboundHTLCState :: LocalRemoved ( ref reason) => {
977
+ & InboundHTLCState :: LocalRemoved ( false , ref reason) => {
912
978
if generated_by_local {
913
979
if let & InboundHTLCRemovalReason :: Fulfill ( _) = reason {
914
980
value_to_self_msat_offset += htlc. amount_msat as i64 ;
@@ -1181,7 +1247,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1181
1247
assert_eq ! ( htlc. payment_hash, payment_hash_calc) ;
1182
1248
match htlc. state {
1183
1249
InboundHTLCState :: Committed => { } ,
1184
- InboundHTLCState :: LocalRemoved ( ref reason) => {
1250
+ InboundHTLCState :: LocalRemoved ( _ , ref reason) => {
1185
1251
if let & InboundHTLCRemovalReason :: Fulfill ( _) = reason {
1186
1252
} else {
1187
1253
log_warn ! ( logger, "Have preimage and want to fulfill HTLC with payment hash {} we already failed against channel {}" , log_bytes!( htlc. payment_hash. 0 ) , log_bytes!( self . channel_id( ) ) ) ;
@@ -1207,6 +1273,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1207
1273
// We have to put the payment_preimage in the channel_monitor right away here to ensure we
1208
1274
// can claim it even if the channel hits the chain before we see their next commitment.
1209
1275
self . latest_monitor_update_id += 1 ;
1276
+ self . payment_preimages . insert ( payment_hash_calc, payment_preimage_arg. clone ( ) ) ;
1210
1277
let monitor_update = ChannelMonitorUpdate {
1211
1278
update_id : self . latest_monitor_update_id ,
1212
1279
updates : vec ! [ ChannelMonitorUpdateStep :: PaymentPreimage {
@@ -1253,7 +1320,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1253
1320
return Ok ( ( None , Some ( monitor_update) ) ) ;
1254
1321
}
1255
1322
log_trace ! ( logger, "Upgrading HTLC {} to LocalRemoved with a Fulfill!" , log_bytes!( htlc. payment_hash. 0 ) ) ;
1256
- htlc. state = InboundHTLCState :: LocalRemoved ( InboundHTLCRemovalReason :: Fulfill ( payment_preimage_arg. clone ( ) ) ) ;
1323
+ htlc. state = InboundHTLCState :: LocalRemoved ( false , InboundHTLCRemovalReason :: Fulfill ( payment_preimage_arg. clone ( ) ) ) ;
1257
1324
}
1258
1325
1259
1326
Ok ( ( Some ( msgs:: UpdateFulfillHTLC {
@@ -1303,7 +1370,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1303
1370
if htlc. htlc_id == htlc_id_arg {
1304
1371
match htlc. state {
1305
1372
InboundHTLCState :: Committed => { } ,
1306
- InboundHTLCState :: LocalRemoved ( _) => {
1373
+ InboundHTLCState :: LocalRemoved ( _, _ ) => {
1307
1374
debug_assert ! ( false , "Tried to fail an HTLC that was already fail/fulfilled" ) ;
1308
1375
return Ok ( None ) ;
1309
1376
} ,
@@ -1347,7 +1414,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1347
1414
1348
1415
{
1349
1416
let htlc = & mut self . pending_inbound_htlcs [ pending_idx] ;
1350
- htlc. state = InboundHTLCState :: LocalRemoved ( InboundHTLCRemovalReason :: FailRelay ( err_packet. clone ( ) ) ) ;
1417
+ htlc. state = InboundHTLCState :: LocalRemoved ( false , InboundHTLCRemovalReason :: FailRelay ( err_packet. clone ( ) ) ) ;
1351
1418
}
1352
1419
1353
1420
Ok ( Some ( msgs:: UpdateFailHTLC {
@@ -2001,6 +2068,17 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2001
2068
return Err ( ( None , ChannelError :: Close ( format ! ( "Got wrong number of HTLC signatures ({}) from remote. It must be {}" , msg. htlc_signatures. len( ) , local_commitment_tx. 1 ) ) ) ) ;
2002
2069
}
2003
2070
2071
+ // A LocalRemoved HTLC need to be monitored for expiration until we receive a
2072
+ // broadcastable commitment tx without said HTLC. Now that we've confirmed that
2073
+ // this commitment signed message provides said commitment tx, we can drop the
2074
+ // LocalRemoved HTLCs we were previously watching for.
2075
+ self . pending_inbound_htlcs . retain ( |htlc| {
2076
+ log_trace ! ( logger, "Removing inbound LocalRemoved {}" , log_bytes!( htlc. payment_hash. 0 ) ) ;
2077
+ if let & InboundHTLCState :: LocalRemoved ( true , _) = & htlc. state {
2078
+ false
2079
+ } else { true }
2080
+ } ) ;
2081
+
2004
2082
// TODO: Merge these two, sadly they are currently both required to be passed separately to
2005
2083
// ChannelMonitor:
2006
2084
let mut htlcs_without_source = Vec :: with_capacity ( local_commitment_tx. 2 . len ( ) ) ;
@@ -2309,30 +2387,16 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2309
2387
// Take references explicitly so that we can hold multiple references to self.
2310
2388
let pending_inbound_htlcs: & mut Vec < _ > = & mut self . pending_inbound_htlcs ;
2311
2389
let pending_outbound_htlcs: & mut Vec < _ > = & mut self . pending_outbound_htlcs ;
2312
-
2313
- // We really shouldnt have two passes here, but retain gives a non-mutable ref (Rust bug)
2314
- pending_inbound_htlcs. retain ( |htlc| {
2315
- if let & InboundHTLCState :: LocalRemoved ( ref reason) = & htlc. state {
2316
- log_trace ! ( logger, " ...removing inbound LocalRemoved {}" , log_bytes!( htlc. payment_hash. 0 ) ) ;
2390
+ for htlc in pending_inbound_htlcs. iter_mut ( ) {
2391
+ if let & mut InboundHTLCState :: LocalRemoved ( ref mut revoked, ref reason) = & mut htlc. state {
2317
2392
if let & InboundHTLCRemovalReason :: Fulfill ( _) = reason {
2318
- value_to_self_msat_diff += htlc. amount_msat as i64 ;
2319
- }
2320
- false
2321
- } else { true }
2322
- } ) ;
2323
- pending_outbound_htlcs. retain ( |htlc| {
2324
- if let & OutboundHTLCState :: AwaitingRemovedRemoteRevoke ( ref fail_reason) = & htlc. state {
2325
- log_trace ! ( logger, " ...removing outbound AwaitingRemovedRemoteRevoke {}" , log_bytes!( htlc. payment_hash. 0 ) ) ;
2326
- if let Some ( reason) = fail_reason. clone ( ) { // We really want take() here, but, again, non-mut ref :(
2327
- revoked_htlcs. push ( ( htlc. source . clone ( ) , htlc. payment_hash , reason) ) ;
2328
- } else {
2329
- // They fulfilled, so we sent them money
2330
- value_to_self_msat_diff -= htlc. amount_msat as i64 ;
2393
+ if !* revoked {
2394
+ value_to_self_msat_diff += htlc. amount_msat as i64 ;
2395
+ }
2331
2396
}
2332
- false
2333
- } else { true }
2334
- } ) ;
2335
- for htlc in pending_inbound_htlcs. iter_mut ( ) {
2397
+ * revoked = true ;
2398
+ continue
2399
+ }
2336
2400
let swap = if let & InboundHTLCState :: AwaitingRemoteRevokeToAnnounce ( _) = & htlc. state {
2337
2401
log_trace ! ( logger, " ...promoting inbound AwaitingRemoteRevokeToAnnounce {} to Committed" , log_bytes!( htlc. payment_hash. 0 ) ) ;
2338
2402
true
@@ -2353,11 +2417,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2353
2417
require_commitment = true ;
2354
2418
match fail_msg {
2355
2419
HTLCFailureMsg :: Relay ( msg) => {
2356
- htlc. state = InboundHTLCState :: LocalRemoved ( InboundHTLCRemovalReason :: FailRelay ( msg. reason . clone ( ) ) ) ;
2420
+ htlc. state = InboundHTLCState :: LocalRemoved ( false , InboundHTLCRemovalReason :: FailRelay ( msg. reason . clone ( ) ) ) ;
2357
2421
update_fail_htlcs. push ( msg)
2358
2422
} ,
2359
2423
HTLCFailureMsg :: Malformed ( msg) => {
2360
- htlc. state = InboundHTLCState :: LocalRemoved ( InboundHTLCRemovalReason :: FailMalformed ( ( msg. sha256_of_onion , msg. failure_code ) ) ) ;
2424
+ htlc. state = InboundHTLCState :: LocalRemoved ( false , InboundHTLCRemovalReason :: FailMalformed ( ( msg. sha256_of_onion , msg. failure_code ) ) ) ;
2361
2425
update_fail_malformed_htlcs. push ( msg)
2362
2426
} ,
2363
2427
}
@@ -2370,6 +2434,19 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2370
2434
}
2371
2435
}
2372
2436
}
2437
+ // We really shouldnt have two passes here, but retain gives a non-mutable ref (Rust bug)
2438
+ pending_outbound_htlcs. retain ( |htlc| {
2439
+ if let & OutboundHTLCState :: AwaitingRemovedRemoteRevoke ( ref fail_reason) = & htlc. state {
2440
+ log_trace ! ( logger, " ...removing outbound AwaitingRemovedRemoteRevoke {}" , log_bytes!( htlc. payment_hash. 0 ) ) ;
2441
+ if let Some ( reason) = fail_reason. clone ( ) { // We really want take() here, but, again, non-mut ref :(
2442
+ revoked_htlcs. push ( ( htlc. source . clone ( ) , htlc. payment_hash , reason) ) ;
2443
+ } else {
2444
+ // They fulfilled, so we sent them money
2445
+ value_to_self_msat_diff -= htlc. amount_msat as i64 ;
2446
+ }
2447
+ false
2448
+ } else { true }
2449
+ } ) ;
2373
2450
for htlc in pending_outbound_htlcs. iter_mut ( ) {
2374
2451
if let OutboundHTLCState :: LocalAnnounced ( _) = htlc. state {
2375
2452
log_trace ! ( logger, " ...promoting outbound LocalAnnounced {} to Committed" , log_bytes!( htlc. payment_hash. 0 ) ) ;
@@ -2539,7 +2616,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2539
2616
true
2540
2617
} ,
2541
2618
InboundHTLCState :: Committed => true ,
2542
- InboundHTLCState :: LocalRemoved ( _) => {
2619
+ InboundHTLCState :: LocalRemoved ( _, _ ) => {
2543
2620
// We (hopefully) sent a commitment_signed updating this HTLC (which we can
2544
2621
// re-transmit if needed) and they may have even sent a revoke_and_ack back
2545
2622
// (that we missed). Keep this around for now and if they tell us they missed
@@ -2689,7 +2766,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
2689
2766
}
2690
2767
2691
2768
for htlc in self . pending_inbound_htlcs . iter ( ) {
2692
- if let & InboundHTLCState :: LocalRemoved ( ref reason) = & htlc. state {
2769
+ if let & InboundHTLCState :: LocalRemoved ( false , ref reason) = & htlc. state {
2693
2770
match reason {
2694
2771
& InboundHTLCRemovalReason :: FailRelay ( ref err_packet) => {
2695
2772
update_fail_htlcs. push ( msgs:: UpdateFailHTLC {
@@ -3098,14 +3175,6 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3098
3175
self . user_id
3099
3176
}
3100
3177
3101
- /// May only be called after funding has been initiated (ie is_funding_initiated() is true)
3102
- pub fn channel_monitor ( & mut self ) -> & mut ChannelMonitor < ChanSigner > {
3103
- if self . channel_state < ChannelState :: FundingSent as u32 {
3104
- panic ! ( "Can't get a channel monitor until funding has been created" ) ;
3105
- }
3106
- self . channel_monitor . as_mut ( ) . unwrap ( )
3107
- }
3108
-
3109
3178
/// Guaranteed to be Some after both FundingLocked messages have been exchanged (and, thus,
3110
3179
/// is_usable() returns true).
3111
3180
/// Allowed in any state (including after shutdown)
@@ -3801,7 +3870,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3801
3870
if have_updates { break ; }
3802
3871
}
3803
3872
for htlc in self . pending_inbound_htlcs . iter ( ) {
3804
- if let InboundHTLCState :: LocalRemoved ( _) = htlc. state {
3873
+ if let InboundHTLCState :: LocalRemoved ( false , _) = htlc. state {
3805
3874
have_updates = true ;
3806
3875
}
3807
3876
if have_updates { break ; }
@@ -4094,8 +4163,9 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
4094
4163
& InboundHTLCState :: Committed => {
4095
4164
3u8 . write ( writer) ?;
4096
4165
} ,
4097
- & InboundHTLCState :: LocalRemoved ( ref removal_reason) => {
4166
+ & InboundHTLCState :: LocalRemoved ( ref revoked , ref removal_reason) => {
4098
4167
4u8 . write ( writer) ?;
4168
+ revoked. write ( writer) ?;
4099
4169
removal_reason. write ( writer) ?;
4100
4170
} ,
4101
4171
}
@@ -4131,6 +4201,11 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
4131
4201
}
4132
4202
}
4133
4203
4204
+ writer. write_all ( & byte_utils:: be64_to_array ( self . payment_preimages . len ( ) as u64 ) ) ?;
4205
+ for payment_preimage in self . payment_preimages . values ( ) {
4206
+ writer. write_all ( & payment_preimage. 0 [ ..] ) ?;
4207
+ }
4208
+
4134
4209
( self . holding_cell_htlc_updates . len ( ) as u64 ) . write ( writer) ?;
4135
4210
for update in self . holding_cell_htlc_updates . iter ( ) {
4136
4211
match update {
@@ -4228,6 +4303,8 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for Channel<ChanSigner> {
4228
4303
}
4229
4304
}
4230
4305
4306
+ const MAX_ALLOC_SIZE : usize = 64 * 1024 ;
4307
+
4231
4308
impl < ChanSigner : ChannelKeys + Readable > Readable for Channel < ChanSigner > {
4232
4309
fn read < R : :: std:: io:: Read > ( reader : & mut R ) -> Result < Self , DecodeError > {
4233
4310
let _ver: u8 = Readable :: read ( reader) ?;
@@ -4266,7 +4343,7 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for Channel<ChanSigner> {
4266
4343
1 => InboundHTLCState :: AwaitingRemoteRevokeToAnnounce ( Readable :: read ( reader) ?) ,
4267
4344
2 => InboundHTLCState :: AwaitingAnnouncedRemoteRevoke ( Readable :: read ( reader) ?) ,
4268
4345
3 => InboundHTLCState :: Committed ,
4269
- 4 => InboundHTLCState :: LocalRemoved ( Readable :: read ( reader) ?) ,
4346
+ 4 => InboundHTLCState :: LocalRemoved ( Readable :: read ( reader) ?, Readable :: read ( reader ) ? ) ,
4270
4347
_ => return Err ( DecodeError :: InvalidValue ) ,
4271
4348
} ,
4272
4349
} ) ;
@@ -4292,6 +4369,16 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for Channel<ChanSigner> {
4292
4369
} ) ;
4293
4370
}
4294
4371
4372
+ let payment_preimages_len: u64 = Readable :: read ( reader) ?;
4373
+ let mut payment_preimages = HashMap :: with_capacity ( cmp:: min ( payment_preimages_len as usize , MAX_ALLOC_SIZE / 32 ) ) ;
4374
+ for _ in 0 ..payment_preimages_len {
4375
+ let preimage: PaymentPreimage = Readable :: read ( reader) ?;
4376
+ let hash = PaymentHash ( Sha256 :: hash ( & preimage. 0 [ ..] ) . into_inner ( ) ) ;
4377
+ if let Some ( _) = payment_preimages. insert ( hash, preimage) {
4378
+ return Err ( DecodeError :: InvalidValue ) ;
4379
+ }
4380
+ }
4381
+
4295
4382
let holding_cell_htlc_update_count: u64 = Readable :: read ( reader) ?;
4296
4383
let mut holding_cell_htlc_updates = Vec :: with_capacity ( cmp:: min ( holding_cell_htlc_update_count as usize , OUR_MAX_HTLCS as usize * 2 ) ) ;
4297
4384
for _ in 0 ..holding_cell_htlc_update_count {
@@ -4408,6 +4495,7 @@ impl<ChanSigner: ChannelKeys + Readable> Readable for Channel<ChanSigner> {
4408
4495
pending_inbound_htlcs,
4409
4496
pending_outbound_htlcs,
4410
4497
holding_cell_htlc_updates,
4498
+ payment_preimages,
4411
4499
4412
4500
resend_order,
4413
4501
0 commit comments