@@ -174,25 +174,7 @@ impl<Key : Send + cmp::Eq + hash::Hash> ChainListener for SimpleManyChannelMonit
174
174
for htlc in htlc_updated_infos. drain ( ..) {
175
175
match pending_htlc_updated. entry ( htlc. 2 ) {
176
176
hash_map:: Entry :: Occupied ( mut e) => {
177
- // In case of reorg we may have htlc outputs solved in a different way so
178
- // we prefer to keep claims but don't store duplicate updates for a given
179
- // (payment_hash, HTLCSource) pair.
180
- // TODO: Note that we currently don't really use this as ChannelManager
181
- // will fail/claim backwards after the first block. We really should delay
182
- // a few blocks before failing backwards (but can claim backwards
183
- // immediately) as long as we have a few blocks of headroom.
184
- let mut existing_claim = false ;
185
- e. get_mut ( ) . retain ( |htlc_data| {
186
- if htlc. 0 == htlc_data. 0 {
187
- if htlc_data. 1 . is_some ( ) {
188
- existing_claim = true ;
189
- true
190
- } else { false }
191
- } else { true }
192
- } ) ;
193
- if !existing_claim {
194
- e. get_mut ( ) . push ( ( htlc. 0 , htlc. 1 ) ) ;
195
- }
177
+ e. get_mut ( ) . push ( ( htlc. 0 , htlc. 1 ) ) ;
196
178
}
197
179
hash_map:: Entry :: Vacant ( e) => {
198
180
e. insert ( vec ! [ ( htlc. 0 , htlc. 1 ) ] ) ;
@@ -304,7 +286,6 @@ pub(crate) const HTLC_FAIL_TIMEOUT_BLOCKS: u32 = 3;
304
286
/// Number of blocks we wait on seeing a confirmed HTLC-Timeout or previous revoked commitment
305
287
/// transaction before we fail corresponding inbound HTLCs. This prevents us from failing backwards
306
288
/// and then getting a reorg resulting in us losing money.
307
- //TODO: We currently don't actually use this...we should
308
289
pub ( crate ) const HTLC_FAIL_ANTI_REORG_DELAY : u32 = 6 ;
309
290
310
291
#[ derive( Clone , PartialEq ) ]
@@ -390,6 +371,8 @@ pub struct ChannelMonitor {
390
371
391
372
destination_script : Script ,
392
373
374
+ htlc_updated_waiting_threshold_conf : HashMap < u32 , Vec < ( HTLCSource , Option < PaymentPreimage > , PaymentHash ) > > ,
375
+
393
376
// We simply modify last_block_hash in Channel's block_connected so that serialization is
394
377
// consistent but hopefully the users' copy handles block_connected in a consistent way.
395
378
// (we do *not*, however, update them in insert_combine to ensure any local user copies keep
@@ -419,7 +402,8 @@ impl PartialEq for ChannelMonitor {
419
402
self . current_remote_commitment_number != other. current_remote_commitment_number ||
420
403
self . current_local_signed_commitment_tx != other. current_local_signed_commitment_tx ||
421
404
self . payment_preimages != other. payment_preimages ||
422
- self . destination_script != other. destination_script
405
+ self . destination_script != other. destination_script ||
406
+ self . htlc_updated_waiting_threshold_conf != other. htlc_updated_waiting_threshold_conf
423
407
{
424
408
false
425
409
} else {
@@ -469,6 +453,8 @@ impl ChannelMonitor {
469
453
payment_preimages : HashMap :: new ( ) ,
470
454
destination_script : destination_script,
471
455
456
+ htlc_updated_waiting_threshold_conf : HashMap :: new ( ) ,
457
+
472
458
last_block_hash : Default :: default ( ) ,
473
459
secp_ctx : Secp256k1 :: new ( ) ,
474
460
logger,
@@ -946,6 +932,17 @@ impl ChannelMonitor {
946
932
self . last_block_hash . write ( writer) ?;
947
933
self . destination_script . write ( writer) ?;
948
934
935
+ writer. write_all ( & byte_utils:: be64_to_array ( self . htlc_updated_waiting_threshold_conf . len ( ) as u64 ) ) ?;
936
+ for ( ref target, ref updates) in self . htlc_updated_waiting_threshold_conf . iter ( ) {
937
+ writer. write_all ( & byte_utils:: be32_to_array ( * * target) ) ?;
938
+ writer. write_all ( & byte_utils:: be64_to_array ( updates. len ( ) as u64 ) ) ?;
939
+ for ref update in updates. iter ( ) {
940
+ update. 0 . write ( writer) ?;
941
+ update. 1 . write ( writer) ?;
942
+ update. 2 . write ( writer) ?;
943
+ }
944
+ }
945
+
949
946
Ok ( ( ) )
950
947
}
951
948
@@ -1009,13 +1006,12 @@ impl ChannelMonitor {
1009
1006
/// HTLC-Success/HTLC-Timeout transactions.
1010
1007
/// Return updates for HTLC pending in the channel and failed automatically by the broadcast of
1011
1008
/// revoked remote commitment tx
1012
- fn check_spend_remote_transaction ( & mut self , tx : & Transaction , height : u32 ) -> ( Vec < Transaction > , ( Sha256dHash , Vec < TxOut > ) , Vec < SpendableOutputDescriptor > , Vec < ( HTLCSource , Option < PaymentPreimage > , PaymentHash ) > ) {
1009
+ fn check_spend_remote_transaction ( & mut self , tx : & Transaction , height : u32 ) -> ( Vec < Transaction > , ( Sha256dHash , Vec < TxOut > ) , Vec < SpendableOutputDescriptor > ) {
1013
1010
// Most secp and related errors trying to create keys means we have no hope of constructing
1014
1011
// a spend transaction...so we return no transactions to broadcast
1015
1012
let mut txn_to_broadcast = Vec :: new ( ) ;
1016
1013
let mut watch_outputs = Vec :: new ( ) ;
1017
1014
let mut spendable_outputs = Vec :: new ( ) ;
1018
- let mut htlc_updated = Vec :: new ( ) ;
1019
1015
1020
1016
let commitment_txid = tx. txid ( ) ; //TODO: This is gonna be a performance bottleneck for watchtowers!
1021
1017
let per_commitment_option = self . remote_claimable_outpoints . get ( & commitment_txid) ;
@@ -1024,7 +1020,7 @@ impl ChannelMonitor {
1024
1020
( $thing : expr ) => {
1025
1021
match $thing {
1026
1022
Ok ( a) => a,
1027
- Err ( _) => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated )
1023
+ Err ( _) => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs)
1028
1024
}
1029
1025
} ;
1030
1026
}
@@ -1049,7 +1045,7 @@ impl ChannelMonitor {
1049
1045
} ;
1050
1046
let delayed_key = ignore_error ! ( chan_utils:: derive_public_key( & self . secp_ctx, & PublicKey :: from_secret_key( & self . secp_ctx, & per_commitment_key) , & self . their_delayed_payment_base_key. unwrap( ) ) ) ;
1051
1047
let a_htlc_key = match self . their_htlc_base_key {
1052
- None => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated ) ,
1048
+ None => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs) ,
1053
1049
Some ( their_htlc_base_key) => ignore_error ! ( chan_utils:: derive_public_key( & self . secp_ctx, & PublicKey :: from_secret_key( & self . secp_ctx, & per_commitment_key) , & their_htlc_base_key) ) ,
1054
1050
} ;
1055
1051
@@ -1129,7 +1125,7 @@ impl ChannelMonitor {
1129
1125
if transaction_output_index as usize >= tx. output . len ( ) ||
1130
1126
tx. output [ transaction_output_index as usize ] . value != htlc. amount_msat / 1000 ||
1131
1127
tx. output [ transaction_output_index as usize ] . script_pubkey != expected_script. to_v0_p2wsh ( ) {
1132
- return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated ) ; // Corrupted per_commitment_data, fuck this user
1128
+ return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs) ; // Corrupted per_commitment_data, fuck this user
1133
1129
}
1134
1130
let input = TxIn {
1135
1131
previous_output : BitcoinOutPoint {
@@ -1169,16 +1165,22 @@ impl ChannelMonitor {
1169
1165
watch_outputs. append ( & mut tx. output . clone ( ) ) ;
1170
1166
self . remote_commitment_txn_on_chain . insert ( commitment_txid, ( commitment_number, tx. output . iter ( ) . map ( |output| { output. script_pubkey . clone ( ) } ) . collect ( ) ) ) ;
1171
1167
1172
- // TODO: We really should only fail backwards after our revocation claims have been
1173
- // confirmed, but we also need to do more other tracking of in-flight pre-confirm
1174
- // on-chain claims, so we can do that at the same time.
1175
1168
macro_rules! check_htlc_fails {
1176
1169
( $txid: expr, $commitment_tx: expr) => {
1177
1170
if let Some ( ref outpoints) = self . remote_claimable_outpoints. get( $txid) {
1178
1171
for & ( ref htlc, ref source_option) in outpoints. iter( ) {
1179
1172
if let & Some ( ref source) = source_option {
1180
- log_trace!( self , "Failing HTLC with payment_hash {} from {} remote commitment tx due to broadcast of revoked remote commitment transaction" , log_bytes!( htlc. payment_hash. 0 ) , $commitment_tx) ;
1181
- htlc_updated. push( ( ( * * source) . clone( ) , None , htlc. payment_hash. clone( ) ) ) ;
1173
+ log_trace!( self , "Failing HTLC with payment_hash {} from {} remote commitment tx due to broadcast of revoked remote commitment transaction, waiting confirmation until {} height" , log_bytes!( htlc. payment_hash. 0 ) , $commitment_tx, height + HTLC_FAIL_ANTI_REORG_DELAY - 1 ) ;
1174
+ match self . htlc_updated_waiting_threshold_conf. entry( height + HTLC_FAIL_ANTI_REORG_DELAY - 1 ) {
1175
+ hash_map:: Entry :: Occupied ( mut entry) => {
1176
+ let e = entry. get_mut( ) ;
1177
+ e. retain( |ref update| update. 0 != * * source) ;
1178
+ e. push( ( ( * * source) . clone( ) , None , htlc. payment_hash. clone( ) ) ) ;
1179
+ }
1180
+ hash_map:: Entry :: Vacant ( entry) => {
1181
+ entry. insert( vec![ ( ( * * source) . clone( ) , None , htlc. payment_hash. clone( ) ) ] ) ;
1182
+ }
1183
+ }
1182
1184
}
1183
1185
}
1184
1186
}
@@ -1194,7 +1196,7 @@ impl ChannelMonitor {
1194
1196
}
1195
1197
// No need to check local commitment txn, symmetric HTLCSource must be present as per-htlc data on remote commitment tx
1196
1198
}
1197
- if inputs. is_empty ( ) { return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated ) ; } // Nothing to be done...probably a false positive/local tx
1199
+ if inputs. is_empty ( ) { return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs) ; } // Nothing to be done...probably a false positive/local tx
1198
1200
1199
1201
let outputs = vec ! ( TxOut {
1200
1202
script_pubkey: self . destination_script. clone( ) ,
@@ -1233,9 +1235,6 @@ impl ChannelMonitor {
1233
1235
1234
1236
log_trace ! ( self , "Got broadcast of non-revoked remote commitment transaction {}" , commitment_txid) ;
1235
1237
1236
- // TODO: We really should only fail backwards after our revocation claims have been
1237
- // confirmed, but we also need to do more other tracking of in-flight pre-confirm
1238
- // on-chain claims, so we can do that at the same time.
1239
1238
macro_rules! check_htlc_fails {
1240
1239
( $txid: expr, $commitment_tx: expr, $id: tt) => {
1241
1240
if let Some ( ref latest_outpoints) = self . remote_claimable_outpoints. get( $txid) {
@@ -1256,7 +1255,16 @@ impl ChannelMonitor {
1256
1255
}
1257
1256
}
1258
1257
log_trace!( self , "Failing HTLC with payment_hash {} from {} remote commitment tx due to broadcast of remote commitment transaction" , log_bytes!( htlc. payment_hash. 0 ) , $commitment_tx) ;
1259
- htlc_updated. push( ( ( * * source) . clone( ) , None , htlc. payment_hash. clone( ) ) ) ;
1258
+ match self . htlc_updated_waiting_threshold_conf. entry( height + HTLC_FAIL_ANTI_REORG_DELAY - 1 ) {
1259
+ hash_map:: Entry :: Occupied ( mut entry) => {
1260
+ let e = entry. get_mut( ) ;
1261
+ e. retain( |ref update| update. 0 != * * source) ;
1262
+ e. push( ( ( * * source) . clone( ) , None , htlc. payment_hash. clone( ) ) ) ;
1263
+ }
1264
+ hash_map:: Entry :: Vacant ( entry) => {
1265
+ entry. insert( vec![ ( ( * * source) . clone( ) , None , htlc. payment_hash. clone( ) ) ] ) ;
1266
+ }
1267
+ }
1260
1268
}
1261
1269
}
1262
1270
}
@@ -1289,7 +1297,7 @@ impl ChannelMonitor {
1289
1297
} ,
1290
1298
} ;
1291
1299
let a_htlc_key = match self . their_htlc_base_key {
1292
- None => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated ) ,
1300
+ None => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs) ,
1293
1301
Some ( their_htlc_base_key) => ignore_error ! ( chan_utils:: derive_public_key( & self . secp_ctx, revocation_point, & their_htlc_base_key) ) ,
1294
1302
} ;
1295
1303
@@ -1344,7 +1352,7 @@ impl ChannelMonitor {
1344
1352
if transaction_output_index as usize >= tx. output . len ( ) ||
1345
1353
tx. output [ transaction_output_index as usize ] . value != htlc. amount_msat / 1000 ||
1346
1354
tx. output [ transaction_output_index as usize ] . script_pubkey != expected_script. to_v0_p2wsh ( ) {
1347
- return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated ) ; // Corrupted per_commitment_data, fuck this user
1355
+ return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs) ; // Corrupted per_commitment_data, fuck this user
1348
1356
}
1349
1357
if let Some ( payment_preimage) = self . payment_preimages . get ( & htlc. payment_hash ) {
1350
1358
let input = TxIn {
@@ -1407,7 +1415,7 @@ impl ChannelMonitor {
1407
1415
}
1408
1416
}
1409
1417
1410
- if inputs. is_empty ( ) { return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated ) ; } // Nothing to be done...probably a false positive/local tx
1418
+ if inputs. is_empty ( ) { return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs) ; } // Nothing to be done...probably a false positive/local tx
1411
1419
1412
1420
let outputs = vec ! ( TxOut {
1413
1421
script_pubkey: self . destination_script. clone( ) ,
@@ -1437,7 +1445,7 @@ impl ChannelMonitor {
1437
1445
}
1438
1446
}
1439
1447
1440
- ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated )
1448
+ ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs)
1441
1449
}
1442
1450
1443
1451
/// Attempts to claim a remote HTLC-Success/HTLC-Timeout's outputs using the revocation key
@@ -1709,7 +1717,7 @@ impl ChannelMonitor {
1709
1717
}
1710
1718
} ;
1711
1719
if funding_txo. is_none ( ) || ( prevout. txid == funding_txo. as_ref ( ) . unwrap ( ) . 0 . txid && prevout. vout == funding_txo. as_ref ( ) . unwrap ( ) . 0 . index as u32 ) {
1712
- let ( remote_txn, new_outputs, mut spendable_output, mut updated ) = self . check_spend_remote_transaction ( tx, height) ;
1720
+ let ( remote_txn, new_outputs, mut spendable_output) = self . check_spend_remote_transaction ( tx, height) ;
1713
1721
txn = remote_txn;
1714
1722
spendable_outputs. append ( & mut spendable_output) ;
1715
1723
if !new_outputs. 1 . is_empty ( ) {
@@ -1728,9 +1736,6 @@ impl ChannelMonitor {
1728
1736
spendable_outputs. push ( spendable_output) ;
1729
1737
}
1730
1738
}
1731
- if updated. len ( ) > 0 {
1732
- htlc_updated. append ( & mut updated) ;
1733
- }
1734
1739
} else {
1735
1740
if let Some ( & ( commitment_number, _) ) = self . remote_commitment_txn_on_chain . get ( & prevout. txid ) {
1736
1741
let ( tx, spendable_output) = self . check_spend_remote_htlc ( tx, commitment_number) ;
@@ -1781,6 +1786,12 @@ impl ChannelMonitor {
1781
1786
}
1782
1787
}
1783
1788
}
1789
+ if let Some ( updates) = self . htlc_updated_waiting_threshold_conf . remove ( & height) {
1790
+ for update in updates {
1791
+ log_trace ! ( self , "HTLC {} failure update has get enough confirmation to be pass upstream" , log_bytes!( ( update. 2 ) . 0 ) ) ;
1792
+ htlc_updated. push ( update) ;
1793
+ }
1794
+ }
1784
1795
self . last_block_hash = block_hash. clone ( ) ;
1785
1796
( watch_outputs, spendable_outputs, htlc_updated)
1786
1797
}
@@ -2181,6 +2192,21 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
2181
2192
let last_block_hash: Sha256dHash = Readable :: read ( reader) ?;
2182
2193
let destination_script = Readable :: read ( reader) ?;
2183
2194
2195
+ let waiting_threshold_conf_len: u64 = Readable :: read ( reader) ?;
2196
+ let mut htlc_updated_waiting_threshold_conf = HashMap :: with_capacity ( cmp:: min ( waiting_threshold_conf_len as usize , MAX_ALLOC_SIZE / 128 ) ) ;
2197
+ for _ in 0 ..waiting_threshold_conf_len {
2198
+ let height_target = Readable :: read ( reader) ?;
2199
+ let updates_len: u64 = Readable :: read ( reader) ?;
2200
+ let mut updates = Vec :: with_capacity ( cmp:: min ( updates_len as usize , MAX_ALLOC_SIZE / 128 ) ) ;
2201
+ for _ in 0 ..updates_len {
2202
+ let htlc_source = Readable :: read ( reader) ?;
2203
+ let preimage = Readable :: read ( reader) ?;
2204
+ let hash = Readable :: read ( reader) ?;
2205
+ updates. push ( ( htlc_source, preimage, hash) ) ;
2206
+ }
2207
+ htlc_updated_waiting_threshold_conf. insert ( height_target, updates) ;
2208
+ }
2209
+
2184
2210
Ok ( ( last_block_hash. clone ( ) , ChannelMonitor {
2185
2211
commitment_transaction_number_obscure_factor,
2186
2212
@@ -2204,6 +2230,9 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
2204
2230
payment_preimages,
2205
2231
2206
2232
destination_script,
2233
+
2234
+ htlc_updated_waiting_threshold_conf,
2235
+
2207
2236
last_block_hash,
2208
2237
secp_ctx,
2209
2238
logger,
0 commit comments