@@ -385,6 +385,9 @@ pub struct ChannelMonitor {
385
385
386
386
destination_script : Script ,
387
387
388
+ htlc_updated_waiting_outpoint_solving : HashMap < BitcoinOutPoint , ( HTLCSource , Option < PaymentPreimage > , PaymentHash ) > ,
389
+ htlc_updated_waiting_threshold_conf : HashMap < u32 , Vec < ( Option < BitcoinOutPoint > , ( HTLCSource , Option < PaymentPreimage > , PaymentHash ) ) > > ,
390
+
388
391
// We simply modify last_block_hash in Channel's block_connected so that serialization is
389
392
// consistent but hopefully the users' copy handles block_connected in a consistent way.
390
393
// (we do *not*, however, update them in insert_combine to ensure any local user copies keep
@@ -414,7 +417,9 @@ impl PartialEq for ChannelMonitor {
414
417
self . current_remote_commitment_number != other. current_remote_commitment_number ||
415
418
self . current_local_signed_commitment_tx != other. current_local_signed_commitment_tx ||
416
419
self . payment_preimages != other. payment_preimages ||
417
- self . destination_script != other. destination_script
420
+ self . destination_script != other. destination_script ||
421
+ self . htlc_updated_waiting_outpoint_solving != other. htlc_updated_waiting_outpoint_solving ||
422
+ self . htlc_updated_waiting_threshold_conf != other. htlc_updated_waiting_threshold_conf
418
423
{
419
424
false
420
425
} else {
@@ -464,6 +469,9 @@ impl ChannelMonitor {
464
469
payment_preimages : HashMap :: new ( ) ,
465
470
destination_script : destination_script,
466
471
472
+ htlc_updated_waiting_outpoint_solving : HashMap :: new ( ) ,
473
+ htlc_updated_waiting_threshold_conf : HashMap :: new ( ) ,
474
+
467
475
last_block_hash : Default :: default ( ) ,
468
476
secp_ctx : Secp256k1 :: new ( ) ,
469
477
logger,
@@ -951,6 +959,26 @@ impl ChannelMonitor {
951
959
self . last_block_hash . write ( writer) ?;
952
960
self . destination_script . write ( writer) ?;
953
961
962
+ writer. write_all ( & byte_utils:: be64_to_array ( self . htlc_updated_waiting_outpoint_solving . len ( ) as u64 ) ) ?;
963
+ for ( ref outpoint, ref update) in self . htlc_updated_waiting_outpoint_solving . iter ( ) {
964
+ outpoint. write ( writer) ?;
965
+ update. 0 . write ( writer) ?;
966
+ update. 1 . write ( writer) ?;
967
+ update. 2 . write ( writer) ?;
968
+ }
969
+
970
+ writer. write_all ( & byte_utils:: be64_to_array ( self . htlc_updated_waiting_threshold_conf . len ( ) as u64 ) ) ?;
971
+ for ( ref target, ref updates) in self . htlc_updated_waiting_threshold_conf . iter ( ) {
972
+ writer. write_all ( & byte_utils:: be32_to_array ( * * target) ) ?;
973
+ writer. write_all ( & byte_utils:: be64_to_array ( updates. len ( ) as u64 ) ) ?;
974
+ for & ( ref outpoint, ref update) in updates. iter ( ) {
975
+ outpoint. write ( writer) ?;
976
+ update. 0 . write ( writer) ?;
977
+ update. 1 . write ( writer) ?;
978
+ update. 2 . write ( writer) ?;
979
+ }
980
+ }
981
+
954
982
Ok ( ( ) )
955
983
}
956
984
@@ -1014,13 +1042,12 @@ impl ChannelMonitor {
1014
1042
/// HTLC-Success/HTLC-Timeout transactions.
1015
1043
/// Return updates for HTLC pending in the channel and failed automatically by the broadcast of
1016
1044
/// 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 ) > ) {
1045
+ fn check_spend_remote_transaction ( & mut self , tx : & Transaction , height : u32 ) -> ( Vec < Transaction > , ( Sha256dHash , Vec < TxOut > ) , Vec < SpendableOutputDescriptor > ) {
1018
1046
// Most secp and related errors trying to create keys means we have no hope of constructing
1019
1047
// a spend transaction...so we return no transactions to broadcast
1020
1048
let mut txn_to_broadcast = Vec :: new ( ) ;
1021
1049
let mut watch_outputs = Vec :: new ( ) ;
1022
1050
let mut spendable_outputs = Vec :: new ( ) ;
1023
- let mut htlc_updated = Vec :: new ( ) ;
1024
1051
1025
1052
let commitment_txid = tx. txid ( ) ; //TODO: This is gonna be a performance bottleneck for watchtowers!
1026
1053
let per_commitment_option = self . remote_claimable_outpoints . get ( & commitment_txid) ;
@@ -1029,7 +1056,7 @@ impl ChannelMonitor {
1029
1056
( $thing : expr ) => {
1030
1057
match $thing {
1031
1058
Ok ( a) => a,
1032
- Err ( _) => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated )
1059
+ Err ( _) => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs)
1033
1060
}
1034
1061
} ;
1035
1062
}
@@ -1054,7 +1081,7 @@ impl ChannelMonitor {
1054
1081
} ;
1055
1082
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
1083
let a_htlc_key = match self . their_htlc_base_key {
1057
- None => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated ) ,
1084
+ None => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs) ,
1058
1085
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
1086
} ;
1060
1087
@@ -1134,7 +1161,7 @@ impl ChannelMonitor {
1134
1161
if transaction_output_index as usize >= tx. output . len ( ) ||
1135
1162
tx. output [ transaction_output_index as usize ] . value != htlc. amount_msat / 1000 ||
1136
1163
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
1164
+ return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs) ; // Corrupted per_commitment_data, fuck this user
1138
1165
}
1139
1166
let input = TxIn {
1140
1167
previous_output : BitcoinOutPoint {
@@ -1182,8 +1209,15 @@ impl ChannelMonitor {
1182
1209
if let Some ( ref outpoints) = self . remote_claimable_outpoints. get( & $txid) {
1183
1210
for & ( ref htlc, ref source_option) in outpoints. iter( ) {
1184
1211
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( ) ) ) ;
1212
+ log_info!( 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 ) ;
1213
+ match self . htlc_updated_waiting_threshold_conf. entry( height + HTLC_FAIL_ANTI_REORG_DELAY - 1 ) {
1214
+ hash_map:: Entry :: Occupied ( mut entry) => {
1215
+ entry. get_mut( ) . push( ( None , ( ( * * source) . clone( ) , None , htlc. payment_hash. clone( ) ) ) ) ;
1216
+ }
1217
+ hash_map:: Entry :: Vacant ( entry) => {
1218
+ entry. insert( vec![ ( None , ( ( * * source) . clone( ) , None , htlc. payment_hash. clone( ) ) ) ] ) ;
1219
+ }
1220
+ }
1187
1221
}
1188
1222
}
1189
1223
}
@@ -1199,7 +1233,7 @@ impl ChannelMonitor {
1199
1233
}
1200
1234
// No need to check local commitment txn, symmetric HTLCSource must be present as per-htlc data on remote commitment tx
1201
1235
}
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
1236
+ 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
1237
1204
1238
let outputs = vec ! ( TxOut {
1205
1239
script_pubkey: self . destination_script. clone( ) ,
@@ -1257,11 +1291,19 @@ impl ChannelMonitor {
1257
1291
// need to here.
1258
1292
for & ( ref broadcast_htlc, ref broadcast_source) in per_commitment_data. iter( ) {
1259
1293
if broadcast_htlc. transaction_output_index. is_some( ) && Some ( source) == broadcast_source. as_ref( ) {
1294
+ self . htlc_updated_waiting_outpoint_solving. insert( BitcoinOutPoint { txid: commitment_txid. clone( ) , vout: broadcast_htlc. transaction_output_index. unwrap( ) } , ( ( * * source) . clone( ) , None , htlc. payment_hash. clone( ) ) ) ;
1260
1295
continue $id;
1261
1296
}
1262
1297
}
1263
1298
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( ) ) ) ;
1299
+ match self . htlc_updated_waiting_threshold_conf. entry( height + HTLC_FAIL_ANTI_REORG_DELAY - 1 ) {
1300
+ hash_map:: Entry :: Occupied ( mut entry) => {
1301
+ entry. get_mut( ) . push( ( None , ( ( * * source) . clone( ) , None , htlc. payment_hash. clone( ) ) ) ) ;
1302
+ }
1303
+ hash_map:: Entry :: Vacant ( entry) => {
1304
+ entry. insert( vec![ ( None , ( ( * * source) . clone( ) , None , htlc. payment_hash. clone( ) ) ) ] ) ;
1305
+ }
1306
+ }
1265
1307
}
1266
1308
}
1267
1309
}
@@ -1294,7 +1336,7 @@ impl ChannelMonitor {
1294
1336
} ,
1295
1337
} ;
1296
1338
let a_htlc_key = match self . their_htlc_base_key {
1297
- None => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated ) ,
1339
+ None => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs) ,
1298
1340
Some ( their_htlc_base_key) => ignore_error ! ( chan_utils:: derive_public_key( & self . secp_ctx, revocation_point, & their_htlc_base_key) ) ,
1299
1341
} ;
1300
1342
@@ -1349,7 +1391,7 @@ impl ChannelMonitor {
1349
1391
if transaction_output_index as usize >= tx. output . len ( ) ||
1350
1392
tx. output [ transaction_output_index as usize ] . value != htlc. amount_msat / 1000 ||
1351
1393
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
1394
+ return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs) ; // Corrupted per_commitment_data, fuck this user
1353
1395
}
1354
1396
if let Some ( payment_preimage) = self . payment_preimages . get ( & htlc. payment_hash ) {
1355
1397
let input = TxIn {
@@ -1412,7 +1454,7 @@ impl ChannelMonitor {
1412
1454
}
1413
1455
}
1414
1456
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
1457
+ 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
1458
1417
1459
let outputs = vec ! ( TxOut {
1418
1460
script_pubkey: self . destination_script. clone( ) ,
@@ -1442,7 +1484,7 @@ impl ChannelMonitor {
1442
1484
}
1443
1485
}
1444
1486
1445
- ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated )
1487
+ ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs)
1446
1488
}
1447
1489
1448
1490
/// Attempts to claim a remote HTLC-Success/HTLC-Timeout's outputs using the revocation key
@@ -1697,6 +1739,7 @@ impl ChannelMonitor {
1697
1739
let mut watch_outputs = Vec :: new ( ) ;
1698
1740
let mut spendable_outputs = Vec :: new ( ) ;
1699
1741
let mut htlc_updated = Vec :: new ( ) ;
1742
+ //log_info!(self, "Height at {}", height);
1700
1743
for tx in txn_matched {
1701
1744
if tx. input . len ( ) == 1 {
1702
1745
// Assuming our keys were not leaked (in which case we're screwed no matter what),
@@ -1714,7 +1757,7 @@ impl ChannelMonitor {
1714
1757
}
1715
1758
} ;
1716
1759
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) ;
1760
+ let ( remote_txn, new_outputs, mut spendable_output) = self . check_spend_remote_transaction ( tx, height) ;
1718
1761
txn = remote_txn;
1719
1762
spendable_outputs. append ( & mut spendable_output) ;
1720
1763
if !new_outputs. 1 . is_empty ( ) {
@@ -1733,9 +1776,6 @@ impl ChannelMonitor {
1733
1776
spendable_outputs. push ( spendable_output) ;
1734
1777
}
1735
1778
}
1736
- if updated. len ( ) > 0 {
1737
- htlc_updated. append ( & mut updated) ;
1738
- }
1739
1779
} else {
1740
1780
if let Some ( & ( commitment_number, _) ) = self . remote_commitment_txn_on_chain . get ( & prevout. txid ) {
1741
1781
let ( tx, spendable_output) = self . check_spend_remote_htlc ( tx, commitment_number) ;
@@ -1758,6 +1798,22 @@ impl ChannelMonitor {
1758
1798
if updated. len ( ) > 0 {
1759
1799
htlc_updated. append ( & mut updated) ;
1760
1800
}
1801
+ for inp in tx. input . iter ( ) {
1802
+ if let Some ( htlc_update) = self . htlc_updated_waiting_outpoint_solving . remove ( & inp. previous_output ) {
1803
+ if inp. witness . len ( ) == 5 && inp. witness [ 4 ] . len ( ) == ACCEPTED_HTLC_SCRIPT_WEIGHT && inp. witness . len ( ) == 3 && inp. witness [ 2 ] . len ( ) == OFFERED_HTLC_SCRIPT_WEIGHT {
1804
+ htlc_updated. append ( & mut vec ! [ htlc_update] ) ;
1805
+ } else {
1806
+ match self . htlc_updated_waiting_threshold_conf . entry ( height + HTLC_FAIL_ANTI_REORG_DELAY - 1 ) {
1807
+ hash_map:: Entry :: Occupied ( mut entry) => {
1808
+ entry. get_mut ( ) . push ( ( Some ( inp. previous_output . clone ( ) ) , htlc_update) ) ;
1809
+ }
1810
+ hash_map:: Entry :: Vacant ( entry) => {
1811
+ entry. insert ( vec ! [ ( Some ( inp. previous_output. clone( ) ) , htlc_update) ] ) ;
1812
+ }
1813
+ }
1814
+ }
1815
+ }
1816
+ }
1761
1817
}
1762
1818
if let Some ( ref cur_local_tx) = self . current_local_signed_commitment_tx {
1763
1819
if self . would_broadcast_at_height ( height) {
@@ -1786,6 +1842,15 @@ impl ChannelMonitor {
1786
1842
}
1787
1843
}
1788
1844
}
1845
+ if let Some ( updates) = self . htlc_updated_waiting_threshold_conf . remove ( & height) {
1846
+ for ( outpoint, update) in updates {
1847
+ if let Some ( oup) = outpoint {
1848
+ self . htlc_updated_waiting_outpoint_solving . remove ( & oup) ;
1849
+ }
1850
+ log_info ! ( self , "HTLC {} failure update has get enough confirmation to be pass upstream" , log_bytes!( ( update. 2 ) . 0 ) ) ;
1851
+ htlc_updated. push ( update) ;
1852
+ }
1853
+ }
1789
1854
self . last_block_hash = block_hash. clone ( ) ;
1790
1855
( watch_outputs, spendable_outputs, htlc_updated)
1791
1856
}
@@ -2159,6 +2224,34 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
2159
2224
let last_block_hash: Sha256dHash = Readable :: read ( reader) ?;
2160
2225
let destination_script = Readable :: read ( reader) ?;
2161
2226
2227
+ let waiting_outpoint_solving_len: u64 = Readable :: read ( reader) ?;
2228
+ let mut htlc_updated_waiting_outpoint_solving = HashMap :: with_capacity ( cmp:: min ( waiting_outpoint_solving_len as usize , MAX_ALLOC_SIZE / 128 ) ) ;
2229
+ for _ in 0 ..waiting_outpoint_solving_len {
2230
+ let outpoint = Readable :: read ( reader) ?;
2231
+ let htlc_source = Readable :: read ( reader) ?;
2232
+ let preimage = Readable :: read ( reader) ?;
2233
+ let hash = Readable :: read ( reader) ?;
2234
+ if let Some ( _) = htlc_updated_waiting_outpoint_solving. insert ( outpoint, ( htlc_source, preimage, hash) ) {
2235
+ return Err ( DecodeError :: InvalidValue ) ;
2236
+ }
2237
+ }
2238
+
2239
+ let waiting_threshold_conf_len: u64 = Readable :: read ( reader) ?;
2240
+ let mut htlc_updated_waiting_threshold_conf = HashMap :: with_capacity ( cmp:: min ( waiting_threshold_conf_len as usize , MAX_ALLOC_SIZE / 128 ) ) ;
2241
+ for _ in 0 ..waiting_threshold_conf_len {
2242
+ let height_target = Readable :: read ( reader) ?;
2243
+ let updates_len: u64 = Readable :: read ( reader) ?;
2244
+ let mut updates = Vec :: with_capacity ( cmp:: min ( updates_len as usize , MAX_ALLOC_SIZE / 128 ) ) ;
2245
+ for _ in 0 ..updates_len {
2246
+ let outpoint = Readable :: read ( reader) ?;
2247
+ let htlc_source = Readable :: read ( reader) ?;
2248
+ let preimage = Readable :: read ( reader) ?;
2249
+ let hash = Readable :: read ( reader) ?;
2250
+ updates. push ( ( outpoint, ( htlc_source, preimage, hash) ) ) ;
2251
+ }
2252
+ htlc_updated_waiting_threshold_conf. insert ( height_target, updates) ;
2253
+ }
2254
+
2162
2255
Ok ( ( last_block_hash. clone ( ) , ChannelMonitor {
2163
2256
commitment_transaction_number_obscure_factor,
2164
2257
@@ -2182,6 +2275,10 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
2182
2275
payment_preimages,
2183
2276
2184
2277
destination_script,
2278
+
2279
+ htlc_updated_waiting_outpoint_solving,
2280
+ htlc_updated_waiting_threshold_conf,
2281
+
2185
2282
last_block_hash,
2186
2283
secp_ctx,
2187
2284
logger,
0 commit comments