@@ -172,10 +172,6 @@ impl<Key : Send + cmp::Eq + hash::Hash> ChainListener for SimpleManyChannelMonit
172
172
// In case of reorg we may have htlc outputs solved in a different way so
173
173
// we prefer to keep claims but don't store duplicate updates for a given
174
174
// (payment_hash, HTLCSource) pair.
175
- // TODO: Note that we currently don't really use this as ChannelManager
176
- // will fail/claim backwards after the first block. We really should delay
177
- // a few blocks before failing backwards (but can claim backwards
178
- // immediately) as long as we have a few blocks of headroom.
179
175
let mut existing_claim = false ;
180
176
e. get_mut ( ) . retain ( |htlc_data| {
181
177
if htlc. 0 == htlc_data. 0 {
@@ -299,7 +295,6 @@ pub(crate) const HTLC_FAIL_TIMEOUT_BLOCKS: u32 = 3;
299
295
/// Number of blocks we wait on seeing a confirmed HTLC-Timeout or previous revoked commitment
300
296
/// transaction before we fail corresponding inbound HTLCs. This prevents us from failing backwards
301
297
/// and then getting a reorg resulting in us losing money.
302
- //TODO: We currently don't actually use this...we should
303
298
pub ( crate ) const HTLC_FAIL_ANTI_REORG_DELAY : u32 = 6 ;
304
299
305
300
#[ derive( Clone , PartialEq ) ]
@@ -385,6 +380,8 @@ pub struct ChannelMonitor {
385
380
386
381
destination_script : Script ,
387
382
383
+ htlc_updated_waiting_threshold_conf : HashMap < u32 , Vec < ( HTLCSource , Option < PaymentPreimage > , PaymentHash ) > > ,
384
+
388
385
// We simply modify last_block_hash in Channel's block_connected so that serialization is
389
386
// consistent but hopefully the users' copy handles block_connected in a consistent way.
390
387
// (we do *not*, however, update them in insert_combine to ensure any local user copies keep
@@ -414,7 +411,8 @@ impl PartialEq for ChannelMonitor {
414
411
self . current_remote_commitment_number != other. current_remote_commitment_number ||
415
412
self . current_local_signed_commitment_tx != other. current_local_signed_commitment_tx ||
416
413
self . payment_preimages != other. payment_preimages ||
417
- self . destination_script != other. destination_script
414
+ self . destination_script != other. destination_script ||
415
+ self . htlc_updated_waiting_threshold_conf != other. htlc_updated_waiting_threshold_conf
418
416
{
419
417
false
420
418
} else {
@@ -464,6 +462,8 @@ impl ChannelMonitor {
464
462
payment_preimages : HashMap :: new ( ) ,
465
463
destination_script : destination_script,
466
464
465
+ htlc_updated_waiting_threshold_conf : HashMap :: new ( ) ,
466
+
467
467
last_block_hash : Default :: default ( ) ,
468
468
secp_ctx : Secp256k1 :: new ( ) ,
469
469
logger,
@@ -951,6 +951,17 @@ impl ChannelMonitor {
951
951
self . last_block_hash . write ( writer) ?;
952
952
self . destination_script . write ( writer) ?;
953
953
954
+ writer. write_all ( & byte_utils:: be64_to_array ( self . htlc_updated_waiting_threshold_conf . len ( ) as u64 ) ) ?;
955
+ for ( ref target, ref updates) in self . htlc_updated_waiting_threshold_conf . iter ( ) {
956
+ writer. write_all ( & byte_utils:: be32_to_array ( * * target) ) ?;
957
+ writer. write_all ( & byte_utils:: be64_to_array ( updates. len ( ) as u64 ) ) ?;
958
+ for ref update in updates. iter ( ) {
959
+ update. 0 . write ( writer) ?;
960
+ update. 1 . write ( writer) ?;
961
+ update. 2 . write ( writer) ?;
962
+ }
963
+ }
964
+
954
965
Ok ( ( ) )
955
966
}
956
967
@@ -1014,13 +1025,12 @@ impl ChannelMonitor {
1014
1025
/// HTLC-Success/HTLC-Timeout transactions.
1015
1026
/// Return updates for HTLC pending in the channel and failed automatically by the broadcast of
1016
1027
/// revoked remote commitment tx
1017
- fn check_spend_remote_transaction ( & mut self , tx : & Transaction , height : u32 ) -> ( Vec < Transaction > , ( Sha256dHash , Vec < TxOut > ) , Vec < SpendableOutputDescriptor > , Vec < ( HTLCSource , Option < PaymentPreimage > , PaymentHash ) > ) {
1028
+ fn check_spend_remote_transaction ( & mut self , tx : & Transaction , height : u32 ) -> ( Vec < Transaction > , ( Sha256dHash , Vec < TxOut > ) , Vec < SpendableOutputDescriptor > ) {
1018
1029
// Most secp and related errors trying to create keys means we have no hope of constructing
1019
1030
// a spend transaction...so we return no transactions to broadcast
1020
1031
let mut txn_to_broadcast = Vec :: new ( ) ;
1021
1032
let mut watch_outputs = Vec :: new ( ) ;
1022
1033
let mut spendable_outputs = Vec :: new ( ) ;
1023
- let mut htlc_updated = Vec :: new ( ) ;
1024
1034
1025
1035
let commitment_txid = tx. txid ( ) ; //TODO: This is gonna be a performance bottleneck for watchtowers!
1026
1036
let per_commitment_option = self . remote_claimable_outpoints . get ( & commitment_txid) ;
@@ -1029,7 +1039,7 @@ impl ChannelMonitor {
1029
1039
( $thing : expr ) => {
1030
1040
match $thing {
1031
1041
Ok ( a) => a,
1032
- Err ( _) => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated )
1042
+ Err ( _) => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs)
1033
1043
}
1034
1044
} ;
1035
1045
}
@@ -1054,7 +1064,7 @@ impl ChannelMonitor {
1054
1064
} ;
1055
1065
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( ) ) ) ;
1056
1066
let a_htlc_key = match self . their_htlc_base_key {
1057
- None => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated ) ,
1067
+ None => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs) ,
1058
1068
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) ) ,
1059
1069
} ;
1060
1070
@@ -1134,7 +1144,7 @@ impl ChannelMonitor {
1134
1144
if transaction_output_index as usize >= tx. output . len ( ) ||
1135
1145
tx. output [ transaction_output_index as usize ] . value != htlc. amount_msat / 1000 ||
1136
1146
tx. output [ transaction_output_index as usize ] . script_pubkey != expected_script. to_v0_p2wsh ( ) {
1137
- return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated ) ; // Corrupted per_commitment_data, fuck this user
1147
+ return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs) ; // Corrupted per_commitment_data, fuck this user
1138
1148
}
1139
1149
let input = TxIn {
1140
1150
previous_output : BitcoinOutPoint {
@@ -1174,16 +1184,20 @@ impl ChannelMonitor {
1174
1184
watch_outputs. append ( & mut tx. output . clone ( ) ) ;
1175
1185
self . remote_commitment_txn_on_chain . insert ( commitment_txid, ( commitment_number, tx. output . iter ( ) . map ( |output| { output. script_pubkey . clone ( ) } ) . collect ( ) ) ) ;
1176
1186
1177
- // TODO: We really should only fail backwards after our revocation claims have been
1178
- // confirmed, but we also need to do more other tracking of in-flight pre-confirm
1179
- // on-chain claims, so we can do that at the same time.
1180
1187
macro_rules! check_htlc_fails {
1181
1188
( $txid: expr, $commitment_tx: expr) => {
1182
1189
if let Some ( ref outpoints) = self . remote_claimable_outpoints. get( & $txid) {
1183
1190
for & ( ref htlc, ref source_option) in outpoints. iter( ) {
1184
1191
if let & Some ( ref source) = source_option {
1185
- 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) ;
1186
- htlc_updated. push( ( ( * * source) . clone( ) , None , htlc. payment_hash. clone( ) ) ) ;
1192
+ 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 ) ;
1193
+ match self . htlc_updated_waiting_threshold_conf. entry( height + HTLC_FAIL_ANTI_REORG_DELAY - 1 ) {
1194
+ hash_map:: Entry :: Occupied ( mut entry) => {
1195
+ entry. get_mut( ) . push( ( ( * * source) . clone( ) , None , htlc. payment_hash. clone( ) ) ) ;
1196
+ }
1197
+ hash_map:: Entry :: Vacant ( entry) => {
1198
+ entry. insert( vec![ ( ( * * source) . clone( ) , None , htlc. payment_hash. clone( ) ) ] ) ;
1199
+ }
1200
+ }
1187
1201
}
1188
1202
}
1189
1203
}
@@ -1199,7 +1213,7 @@ impl ChannelMonitor {
1199
1213
}
1200
1214
// No need to check local commitment txn, symmetric HTLCSource must be present as per-htlc data on remote commitment tx
1201
1215
}
1202
- 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
1216
+ if inputs. is_empty ( ) { return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs) ; } // Nothing to be done...probably a false positive/local tx
1203
1217
1204
1218
let outputs = vec ! ( TxOut {
1205
1219
script_pubkey: self . destination_script. clone( ) ,
@@ -1238,9 +1252,6 @@ impl ChannelMonitor {
1238
1252
1239
1253
log_trace ! ( self , "Got broadcast of non-revoked remote commitment transaction {}" , commitment_txid) ;
1240
1254
1241
- // TODO: We really should only fail backwards after our revocation claims have been
1242
- // confirmed, but we also need to do more other tracking of in-flight pre-confirm
1243
- // on-chain claims, so we can do that at the same time.
1244
1255
macro_rules! check_htlc_fails {
1245
1256
( $txid: expr, $commitment_tx: expr, $id: tt) => {
1246
1257
if let Some ( ref latest_outpoints) = self . remote_claimable_outpoints. get( & $txid) {
@@ -1261,7 +1272,14 @@ impl ChannelMonitor {
1261
1272
}
1262
1273
}
1263
1274
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) ;
1264
- htlc_updated. push( ( ( * * source) . clone( ) , None , htlc. payment_hash. clone( ) ) ) ;
1275
+ match self . htlc_updated_waiting_threshold_conf. entry( height + HTLC_FAIL_ANTI_REORG_DELAY - 1 ) {
1276
+ hash_map:: Entry :: Occupied ( mut entry) => {
1277
+ entry. get_mut( ) . push( ( ( * * source) . clone( ) , None , htlc. payment_hash. clone( ) ) ) ;
1278
+ }
1279
+ hash_map:: Entry :: Vacant ( entry) => {
1280
+ entry. insert( vec![ ( ( * * source) . clone( ) , None , htlc. payment_hash. clone( ) ) ] ) ;
1281
+ }
1282
+ }
1265
1283
}
1266
1284
}
1267
1285
}
@@ -1294,7 +1312,7 @@ impl ChannelMonitor {
1294
1312
} ,
1295
1313
} ;
1296
1314
let a_htlc_key = match self . their_htlc_base_key {
1297
- None => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated ) ,
1315
+ None => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs) ,
1298
1316
Some ( their_htlc_base_key) => ignore_error ! ( chan_utils:: derive_public_key( & self . secp_ctx, revocation_point, & their_htlc_base_key) ) ,
1299
1317
} ;
1300
1318
@@ -1349,7 +1367,7 @@ impl ChannelMonitor {
1349
1367
if transaction_output_index as usize >= tx. output . len ( ) ||
1350
1368
tx. output [ transaction_output_index as usize ] . value != htlc. amount_msat / 1000 ||
1351
1369
tx. output [ transaction_output_index as usize ] . script_pubkey != expected_script. to_v0_p2wsh ( ) {
1352
- return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated ) ; // Corrupted per_commitment_data, fuck this user
1370
+ return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs) ; // Corrupted per_commitment_data, fuck this user
1353
1371
}
1354
1372
if let Some ( payment_preimage) = self . payment_preimages . get ( & htlc. payment_hash ) {
1355
1373
let input = TxIn {
@@ -1412,7 +1430,7 @@ impl ChannelMonitor {
1412
1430
}
1413
1431
}
1414
1432
1415
- 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
1433
+ if inputs. is_empty ( ) { return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs) ; } // Nothing to be done...probably a false positive/local tx
1416
1434
1417
1435
let outputs = vec ! ( TxOut {
1418
1436
script_pubkey: self . destination_script. clone( ) ,
@@ -1442,7 +1460,7 @@ impl ChannelMonitor {
1442
1460
}
1443
1461
}
1444
1462
1445
- ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated )
1463
+ ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs)
1446
1464
}
1447
1465
1448
1466
/// Attempts to claim a remote HTLC-Success/HTLC-Timeout's outputs using the revocation key
@@ -1714,7 +1732,7 @@ impl ChannelMonitor {
1714
1732
}
1715
1733
} ;
1716
1734
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 ) {
1717
- let ( remote_txn, new_outputs, mut spendable_output, mut updated ) = self . check_spend_remote_transaction ( tx, height) ;
1735
+ let ( remote_txn, new_outputs, mut spendable_output) = self . check_spend_remote_transaction ( tx, height) ;
1718
1736
txn = remote_txn;
1719
1737
spendable_outputs. append ( & mut spendable_output) ;
1720
1738
if !new_outputs. 1 . is_empty ( ) {
@@ -1733,9 +1751,6 @@ impl ChannelMonitor {
1733
1751
spendable_outputs. push ( spendable_output) ;
1734
1752
}
1735
1753
}
1736
- if updated. len ( ) > 0 {
1737
- htlc_updated. append ( & mut updated) ;
1738
- }
1739
1754
} else {
1740
1755
if let Some ( & ( commitment_number, _) ) = self . remote_commitment_txn_on_chain . get ( & prevout. txid ) {
1741
1756
let ( tx, spendable_output) = self . check_spend_remote_htlc ( tx, commitment_number) ;
@@ -1786,6 +1801,12 @@ impl ChannelMonitor {
1786
1801
}
1787
1802
}
1788
1803
}
1804
+ if let Some ( updates) = self . htlc_updated_waiting_threshold_conf . remove ( & height) {
1805
+ for update in updates {
1806
+ log_trace ! ( self , "HTLC {} failure update has get enough confirmation to be pass upstream" , log_bytes!( ( update. 2 ) . 0 ) ) ;
1807
+ htlc_updated. push ( update) ;
1808
+ }
1809
+ }
1789
1810
self . last_block_hash = block_hash. clone ( ) ;
1790
1811
( watch_outputs, spendable_outputs, htlc_updated)
1791
1812
}
@@ -2159,6 +2180,21 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
2159
2180
let last_block_hash: Sha256dHash = Readable :: read ( reader) ?;
2160
2181
let destination_script = Readable :: read ( reader) ?;
2161
2182
2183
+ let waiting_threshold_conf_len: u64 = Readable :: read ( reader) ?;
2184
+ let mut htlc_updated_waiting_threshold_conf = HashMap :: with_capacity ( cmp:: min ( waiting_threshold_conf_len as usize , MAX_ALLOC_SIZE / 128 ) ) ;
2185
+ for _ in 0 ..waiting_threshold_conf_len {
2186
+ let height_target = Readable :: read ( reader) ?;
2187
+ let updates_len: u64 = Readable :: read ( reader) ?;
2188
+ let mut updates = Vec :: with_capacity ( cmp:: min ( updates_len as usize , MAX_ALLOC_SIZE / 128 ) ) ;
2189
+ for _ in 0 ..updates_len {
2190
+ let htlc_source = Readable :: read ( reader) ?;
2191
+ let preimage = Readable :: read ( reader) ?;
2192
+ let hash = Readable :: read ( reader) ?;
2193
+ updates. push ( ( htlc_source, preimage, hash) ) ;
2194
+ }
2195
+ htlc_updated_waiting_threshold_conf. insert ( height_target, updates) ;
2196
+ }
2197
+
2162
2198
Ok ( ( last_block_hash. clone ( ) , ChannelMonitor {
2163
2199
commitment_transaction_number_obscure_factor,
2164
2200
@@ -2182,6 +2218,9 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
2182
2218
payment_preimages,
2183
2219
2184
2220
destination_script,
2221
+
2222
+ htlc_updated_waiting_threshold_conf,
2223
+
2185
2224
last_block_hash,
2186
2225
secp_ctx,
2187
2226
logger,
0 commit comments