@@ -29,6 +29,7 @@ use secp256k1;
29
29
use ln:: msgs:: DecodeError ;
30
30
use ln:: chan_utils;
31
31
use ln:: chan_utils:: HTLCOutputInCommitment ;
32
+ use ln:: channelmanager:: HTLCSource ;
32
33
use chain:: chaininterface:: { ChainListener , ChainWatchInterface , BroadcasterInterface } ;
33
34
use chain:: transaction:: OutPoint ;
34
35
use chain:: keysinterface:: SpendableOutputDescriptor ;
@@ -258,7 +259,7 @@ struct LocalSignedTx {
258
259
b_htlc_key : PublicKey ,
259
260
delayed_payment_key : PublicKey ,
260
261
feerate_per_kw : u64 ,
261
- htlc_outputs : Vec < ( HTLCOutputInCommitment , Signature , Signature ) > ,
262
+ htlc_outputs : Vec < ( HTLCOutputInCommitment , Signature , Signature , Option < HTLCSource > ) > ,
262
263
}
263
264
264
265
const SERIALIZATION_VERSION : u8 = 1 ;
@@ -283,7 +284,7 @@ pub struct ChannelMonitor {
283
284
their_to_self_delay : Option < u16 > ,
284
285
285
286
old_secrets : [ ( [ u8 ; 32 ] , u64 ) ; 49 ] ,
286
- remote_claimable_outpoints : HashMap < Sha256dHash , Vec < HTLCOutputInCommitment > > ,
287
+ remote_claimable_outpoints : HashMap < Sha256dHash , Vec < ( HTLCOutputInCommitment , Option < HTLCSource > ) > > ,
287
288
/// We cannot identify HTLC-Success or HTLC-Timeout transactions by themselves on the chain.
288
289
/// Nor can we figure out their commitment numbers without the commitment transaction they are
289
290
/// spending. Thus, in order to claim them via revocation key, we track all the remote
@@ -439,13 +440,13 @@ impl ChannelMonitor {
439
440
let remote_hash_commitment_number = & mut self . remote_hash_commitment_number ;
440
441
441
442
self . payment_preimages . retain ( |& k, _| {
442
- for & ( ref htlc, _, _) in & local_signed_commitment_tx. htlc_outputs {
443
+ for & ( ref htlc, _, _, _ ) in & local_signed_commitment_tx. htlc_outputs {
443
444
if k == htlc. payment_hash {
444
445
return true
445
446
}
446
447
}
447
448
if let Some ( prev_local_commitment_tx) = prev_local_signed_commitment_tx {
448
- for & ( ref htlc, _, _) in prev_local_commitment_tx. htlc_outputs . iter ( ) {
449
+ for & ( ref htlc, _, _, _ ) in prev_local_commitment_tx. htlc_outputs . iter ( ) {
449
450
if k == htlc. payment_hash {
450
451
return true
451
452
}
@@ -471,14 +472,21 @@ impl ChannelMonitor {
471
472
/// The monitor watches for it to be broadcasted and then uses the HTLC information (and
472
473
/// possibly future revocation/preimage information) to claim outputs where possible.
473
474
/// We cache also the mapping hash:commitment number to lighten pruning of old preimages by watchtowers.
474
- pub ( super ) fn provide_latest_remote_commitment_tx_info ( & mut self , unsigned_commitment_tx : & Transaction , htlc_outputs : Vec < HTLCOutputInCommitment > , commitment_number : u64 , their_revocation_point : PublicKey ) {
475
+ pub ( super ) fn provide_latest_remote_commitment_tx_info ( & mut self , unsigned_commitment_tx : & Transaction , htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < HTLCSource > ) > , commitment_number : u64 , their_revocation_point : PublicKey ) {
475
476
// TODO: Encrypt the htlc_outputs data with the single-hash of the commitment transaction
476
477
// so that a remote monitor doesn't learn anything unless there is a malicious close.
477
478
// (only maybe, sadly we cant do the same for local info, as we need to be aware of
478
479
// timeouts)
479
- for htlc in & htlc_outputs {
480
+ for & ( ref htlc, _ ) in & htlc_outputs {
480
481
self . remote_hash_commitment_number . insert ( htlc. payment_hash , commitment_number) ;
481
482
}
483
+ // We prune old claimable outpoints, useless to pass backward state when remote commitment
484
+ // tx get revoked, optimize for storage
485
+ for ( _, htlc_data) in self . remote_claimable_outpoints . iter_mut ( ) {
486
+ for & mut ( _, ref mut source) in htlc_data. iter_mut ( ) {
487
+ source. take ( ) ;
488
+ }
489
+ }
482
490
self . remote_claimable_outpoints . insert ( unsigned_commitment_tx. txid ( ) , htlc_outputs) ;
483
491
self . current_remote_commitment_number = commitment_number;
484
492
//TODO: Merge this into the other per-remote-transaction output storage stuff
@@ -509,7 +517,7 @@ impl ChannelMonitor {
509
517
/// Panics if set_their_to_self_delay has never been called.
510
518
/// Also update Storage with latest local per_commitment_point to derive local_delayedkey in
511
519
/// case of onchain HTLC tx
512
- pub ( super ) fn provide_latest_local_commitment_tx_info ( & mut self , signed_commitment_tx : Transaction , local_keys : chan_utils:: TxCreationKeys , feerate_per_kw : u64 , htlc_outputs : Vec < ( HTLCOutputInCommitment , Signature , Signature ) > ) {
520
+ pub ( super ) fn provide_latest_local_commitment_tx_info ( & mut self , signed_commitment_tx : Transaction , local_keys : chan_utils:: TxCreationKeys , feerate_per_kw : u64 , htlc_outputs : Vec < ( HTLCOutputInCommitment , Signature , Signature , Option < HTLCSource > ) > ) {
513
521
assert ! ( self . their_to_self_delay. is_some( ) ) ;
514
522
self . prev_local_signed_commitment_tx = self . current_local_signed_commitment_tx . take ( ) ;
515
523
self . current_local_signed_commitment_tx = Some ( LocalSignedTx {
@@ -753,12 +761,25 @@ impl ChannelMonitor {
753
761
}
754
762
}
755
763
764
+ macro_rules! serialize_htlc_source {
765
+ ( $htlc_source: expr) => {
766
+ if let & Some ( ref source) = $htlc_source {
767
+ writer. write_all( & [ 1 ; 1 ] ) ?;
768
+ source. write( writer) ?;
769
+ } else {
770
+ writer. write_all( & [ 0 ; 1 ] ) ?;
771
+ }
772
+ }
773
+ }
774
+
775
+
756
776
writer. write_all ( & byte_utils:: be64_to_array ( self . remote_claimable_outpoints . len ( ) as u64 ) ) ?;
757
- for ( ref txid, ref htlc_outputs ) in self . remote_claimable_outpoints . iter ( ) {
777
+ for ( ref txid, ref htlc_infos ) in self . remote_claimable_outpoints . iter ( ) {
758
778
writer. write_all ( & txid[ ..] ) ?;
759
- writer. write_all ( & byte_utils:: be64_to_array ( htlc_outputs . len ( ) as u64 ) ) ?;
760
- for htlc_output in htlc_outputs . iter ( ) {
779
+ writer. write_all ( & byte_utils:: be64_to_array ( htlc_infos . len ( ) as u64 ) ) ?;
780
+ for & ( ref htlc_output, ref htlc_source ) in htlc_infos . iter ( ) {
761
781
serialize_htlc_in_commitment ! ( htlc_output) ;
782
+ serialize_htlc_source ! ( htlc_source) ;
762
783
}
763
784
}
764
785
@@ -798,10 +819,11 @@ impl ChannelMonitor {
798
819
799
820
writer. write_all( & byte_utils:: be64_to_array( $local_tx. feerate_per_kw) ) ?;
800
821
writer. write_all( & byte_utils:: be64_to_array( $local_tx. htlc_outputs. len( ) as u64 ) ) ?;
801
- for & ( ref htlc_output, ref their_sig, ref our_sig) in $local_tx. htlc_outputs. iter( ) {
822
+ for & ( ref htlc_output, ref their_sig, ref our_sig, ref htlc_source ) in $local_tx. htlc_outputs. iter( ) {
802
823
serialize_htlc_in_commitment!( htlc_output) ;
803
824
writer. write_all( & their_sig. serialize_compact( & self . secp_ctx) ) ?;
804
825
writer. write_all( & our_sig. serialize_compact( & self . secp_ctx) ) ?;
826
+ serialize_htlc_source!( htlc_source) ;
805
827
}
806
828
}
807
829
}
@@ -985,7 +1007,7 @@ impl ChannelMonitor {
985
1007
let ( sig, redeemscript) = match self . key_storage {
986
1008
Storage :: Local { ref revocation_base_key, .. } => {
987
1009
let redeemscript = if $htlc_idx. is_none( ) { revokeable_redeemscript. clone( ) } else {
988
- let htlc = & per_commitment_option. unwrap( ) [ $htlc_idx. unwrap( ) ] ;
1010
+ let htlc = & per_commitment_option. unwrap( ) [ $htlc_idx. unwrap( ) ] . 0 ;
989
1011
chan_utils:: get_htlc_redeemscript_with_explicit_keys( htlc, & a_htlc_key, & b_htlc_key, & revocation_pubkey)
990
1012
} ;
991
1013
let sighash = ignore_error!( Message :: from_slice( & $sighash_parts. sighash_all( & $input, & redeemscript, $amount) [ ..] ) ) ;
@@ -1011,7 +1033,7 @@ impl ChannelMonitor {
1011
1033
if let Some ( per_commitment_data) = per_commitment_option {
1012
1034
inputs. reserve_exact ( per_commitment_data. len ( ) ) ;
1013
1035
1014
- for ( idx, htlc) in per_commitment_data. iter ( ) . enumerate ( ) {
1036
+ for ( idx, & ( ref htlc, _ ) ) in per_commitment_data. iter ( ) . enumerate ( ) {
1015
1037
let expected_script = chan_utils:: get_htlc_redeemscript_with_explicit_keys ( & htlc, & a_htlc_key, & b_htlc_key, & revocation_pubkey) ;
1016
1038
if htlc. transaction_output_index as usize >= tx. output . len ( ) ||
1017
1039
tx. output [ htlc. transaction_output_index as usize ] . value != htlc. amount_msat / 1000 ||
@@ -1140,7 +1162,7 @@ impl ChannelMonitor {
1140
1162
{
1141
1163
let ( sig, redeemscript) = match self . key_storage {
1142
1164
Storage :: Local { ref htlc_base_key, .. } => {
1143
- let htlc = & per_commitment_option. unwrap( ) [ $input. sequence as usize ] ;
1165
+ let htlc = & per_commitment_option. unwrap( ) [ $input. sequence as usize ] . 0 ;
1144
1166
let redeemscript = chan_utils:: get_htlc_redeemscript_with_explicit_keys( htlc, & a_htlc_key, & b_htlc_key, & revocation_pubkey) ;
1145
1167
let sighash = ignore_error!( Message :: from_slice( & $sighash_parts. sighash_all( & $input, & redeemscript, $amount) [ ..] ) ) ;
1146
1168
let htlc_key = ignore_error!( chan_utils:: derive_private_key( & self . secp_ctx, revocation_point, & htlc_base_key) ) ;
@@ -1158,7 +1180,7 @@ impl ChannelMonitor {
1158
1180
}
1159
1181
}
1160
1182
1161
- for ( idx, htlc) in per_commitment_data. iter ( ) . enumerate ( ) {
1183
+ for ( idx, & ( ref htlc, _ ) ) in per_commitment_data. iter ( ) . enumerate ( ) {
1162
1184
let expected_script = chan_utils:: get_htlc_redeemscript_with_explicit_keys ( & htlc, & a_htlc_key, & b_htlc_key, & revocation_pubkey) ;
1163
1185
if htlc. transaction_output_index as usize >= tx. output . len ( ) ||
1164
1186
tx. output [ htlc. transaction_output_index as usize ] . value != htlc. amount_msat / 1000 ||
@@ -1352,7 +1374,7 @@ impl ChannelMonitor {
1352
1374
}
1353
1375
}
1354
1376
1355
- for & ( ref htlc, ref their_sig, ref our_sig) in local_tx. htlc_outputs . iter ( ) {
1377
+ for & ( ref htlc, ref their_sig, ref our_sig, _ ) in local_tx. htlc_outputs . iter ( ) {
1356
1378
if htlc. offered {
1357
1379
let mut htlc_timeout_tx = chan_utils:: build_htlc_transaction ( & local_tx. txid , local_tx. feerate_per_kw , self . their_to_self_delay . unwrap ( ) , htlc, & local_tx. delayed_payment_key , & local_tx. revocation_key ) ;
1358
1380
@@ -1558,7 +1580,7 @@ impl ChannelMonitor {
1558
1580
1559
1581
pub ( super ) fn would_broadcast_at_height ( & self , height : u32 ) -> bool {
1560
1582
if let Some ( ref cur_local_tx) = self . current_local_signed_commitment_tx {
1561
- for & ( ref htlc, _, _) in cur_local_tx. htlc_outputs . iter ( ) {
1583
+ for & ( ref htlc, _, _, _ ) in cur_local_tx. htlc_outputs . iter ( ) {
1562
1584
// For inbound HTLCs which we know the preimage for, we have to ensure we hit the
1563
1585
// chain with enough room to claim the HTLC without our counterparty being able to
1564
1586
// time out the HTLC first.
@@ -1692,14 +1714,31 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
1692
1714
}
1693
1715
}
1694
1716
1717
+ macro_rules! read_htlc_source {
1718
+ ( ) => {
1719
+ {
1720
+ match <u8 as Readable <R >>:: read( reader) ? {
1721
+ 0 => None ,
1722
+ 1 => {
1723
+ let htlc_source: HTLCSource = Readable :: read( reader) ?;
1724
+ Some ( htlc_source)
1725
+ } ,
1726
+ _ => return Err ( DecodeError :: InvalidValue ) ,
1727
+ }
1728
+ }
1729
+ }
1730
+ }
1731
+
1695
1732
let remote_claimable_outpoints_len: u64 = Readable :: read ( reader) ?;
1696
1733
let mut remote_claimable_outpoints = HashMap :: with_capacity ( cmp:: min ( remote_claimable_outpoints_len as usize , MAX_ALLOC_SIZE / 64 ) ) ;
1697
1734
for _ in 0 ..remote_claimable_outpoints_len {
1698
1735
let txid: Sha256dHash = Readable :: read ( reader) ?;
1699
1736
let outputs_count: u64 = Readable :: read ( reader) ?;
1700
1737
let mut outputs = Vec :: with_capacity ( cmp:: min ( outputs_count as usize , MAX_ALLOC_SIZE / 32 ) ) ;
1701
1738
for _ in 0 ..outputs_count {
1702
- outputs. push ( read_htlc_in_commitment ! ( ) ) ;
1739
+ let out = read_htlc_in_commitment ! ( ) ;
1740
+ let source = read_htlc_source ! ( ) ;
1741
+ outputs. push ( ( out, source) ) ;
1703
1742
}
1704
1743
if let Some ( _) = remote_claimable_outpoints. insert ( txid, outputs) {
1705
1744
return Err ( DecodeError :: InvalidValue ) ;
@@ -1756,7 +1795,10 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
1756
1795
let htlc_outputs_len: u64 = Readable :: read( reader) ?;
1757
1796
let mut htlc_outputs = Vec :: with_capacity( cmp:: min( htlc_outputs_len as usize , MAX_ALLOC_SIZE / 128 ) ) ;
1758
1797
for _ in 0 ..htlc_outputs_len {
1759
- htlc_outputs. push( ( read_htlc_in_commitment!( ) , Readable :: read( reader) ?, Readable :: read( reader) ?) ) ;
1798
+ let out = read_htlc_in_commitment!( ) ;
1799
+ let sigs = ( Readable :: read( reader) ?, Readable :: read( reader) ?) ;
1800
+ let source = read_htlc_source!( ) ;
1801
+ htlc_outputs. push( ( out, sigs. 0 , sigs. 1 , source) ) ;
1760
1802
}
1761
1803
1762
1804
LocalSignedTx {
@@ -2245,13 +2287,13 @@ mod tests {
2245
2287
{
2246
2288
let mut res = Vec :: new( ) ;
2247
2289
for ( idx, preimage) in $preimages_slice. iter( ) . enumerate( ) {
2248
- res. push( HTLCOutputInCommitment {
2290
+ res. push( ( HTLCOutputInCommitment {
2249
2291
offered: true ,
2250
2292
amount_msat: 0 ,
2251
2293
cltv_expiry: 0 ,
2252
2294
payment_hash: preimage. 1 . clone( ) ,
2253
2295
transaction_output_index: idx as u32 ,
2254
- } ) ;
2296
+ } , None ) ) ;
2255
2297
}
2256
2298
res
2257
2299
}
@@ -2261,7 +2303,7 @@ mod tests {
2261
2303
( $preimages_slice: expr) => {
2262
2304
{
2263
2305
let mut inp = preimages_slice_to_htlc_outputs!( $preimages_slice) ;
2264
- let res: Vec <_> = inp. drain( ..) . map( |e| { ( e, dummy_sig. clone( ) , dummy_sig. clone( ) ) } ) . collect( ) ;
2306
+ let res: Vec <_> = inp. drain( ..) . map( |e| { ( e. 0 , dummy_sig. clone( ) , dummy_sig. clone( ) , e . 1 ) } ) . collect( ) ;
2265
2307
res
2266
2308
}
2267
2309
}
0 commit comments