@@ -385,6 +385,8 @@ pub struct ChannelMonitor {
385
385
386
386
destination_script : Script ,
387
387
388
+ htlc_updated_waiting_threshold_conf : HashMap < u32 , Vec < ( HTLCSource , Option < PaymentPreimage > , PaymentHash ) > > ,
389
+
388
390
// We simply modify last_block_hash in Channel's block_connected so that serialization is
389
391
// consistent but hopefully the users' copy handles block_connected in a consistent way.
390
392
// (we do *not*, however, update them in insert_combine to ensure any local user copies keep
@@ -414,7 +416,8 @@ impl PartialEq for ChannelMonitor {
414
416
self . current_remote_commitment_number != other. current_remote_commitment_number ||
415
417
self . current_local_signed_commitment_tx != other. current_local_signed_commitment_tx ||
416
418
self . payment_preimages != other. payment_preimages ||
417
- self . destination_script != other. destination_script
419
+ self . destination_script != other. destination_script ||
420
+ self . htlc_updated_waiting_threshold_conf != other. htlc_updated_waiting_threshold_conf
418
421
{
419
422
false
420
423
} else {
@@ -464,6 +467,8 @@ impl ChannelMonitor {
464
467
payment_preimages : HashMap :: new ( ) ,
465
468
destination_script : destination_script,
466
469
470
+ htlc_updated_waiting_threshold_conf : HashMap :: new ( ) ,
471
+
467
472
last_block_hash : Default :: default ( ) ,
468
473
secp_ctx : Secp256k1 :: new ( ) ,
469
474
logger,
@@ -951,6 +956,17 @@ impl ChannelMonitor {
951
956
self . last_block_hash . write ( writer) ?;
952
957
self . destination_script . write ( writer) ?;
953
958
959
+ writer. write_all ( & byte_utils:: be64_to_array ( self . htlc_updated_waiting_threshold_conf . len ( ) as u64 ) ) ?;
960
+ for ( ref target, ref updates) in self . htlc_updated_waiting_threshold_conf . iter ( ) {
961
+ writer. write_all ( & byte_utils:: be32_to_array ( * * target) ) ?;
962
+ writer. write_all ( & byte_utils:: be64_to_array ( updates. len ( ) as u64 ) ) ?;
963
+ for ref update in updates. iter ( ) {
964
+ update. 0 . write ( writer) ?;
965
+ update. 1 . write ( writer) ?;
966
+ update. 2 . write ( writer) ?;
967
+ }
968
+ }
969
+
954
970
Ok ( ( ) )
955
971
}
956
972
@@ -1014,13 +1030,12 @@ impl ChannelMonitor {
1014
1030
/// HTLC-Success/HTLC-Timeout transactions.
1015
1031
/// Return updates for HTLC pending in the channel and failed automatically by the broadcast of
1016
1032
/// 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 ) > ) {
1033
+ fn check_spend_remote_transaction ( & mut self , tx : & Transaction , height : u32 ) -> ( Vec < Transaction > , ( Sha256dHash , Vec < TxOut > ) , Vec < SpendableOutputDescriptor > ) {
1018
1034
// Most secp and related errors trying to create keys means we have no hope of constructing
1019
1035
// a spend transaction...so we return no transactions to broadcast
1020
1036
let mut txn_to_broadcast = Vec :: new ( ) ;
1021
1037
let mut watch_outputs = Vec :: new ( ) ;
1022
1038
let mut spendable_outputs = Vec :: new ( ) ;
1023
- let mut htlc_updated = Vec :: new ( ) ;
1024
1039
1025
1040
let commitment_txid = tx. txid ( ) ; //TODO: This is gonna be a performance bottleneck for watchtowers!
1026
1041
let per_commitment_option = self . remote_claimable_outpoints . get ( & commitment_txid) ;
@@ -1029,7 +1044,7 @@ impl ChannelMonitor {
1029
1044
( $thing : expr ) => {
1030
1045
match $thing {
1031
1046
Ok ( a) => a,
1032
- Err ( _) => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated )
1047
+ Err ( _) => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs)
1033
1048
}
1034
1049
} ;
1035
1050
}
@@ -1054,7 +1069,7 @@ impl ChannelMonitor {
1054
1069
} ;
1055
1070
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
1071
let a_htlc_key = match self . their_htlc_base_key {
1057
- None => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated ) ,
1072
+ None => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs) ,
1058
1073
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
1074
} ;
1060
1075
@@ -1134,7 +1149,7 @@ impl ChannelMonitor {
1134
1149
if transaction_output_index as usize >= tx. output . len ( ) ||
1135
1150
tx. output [ transaction_output_index as usize ] . value != htlc. amount_msat / 1000 ||
1136
1151
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
1152
+ return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs) ; // Corrupted per_commitment_data, fuck this user
1138
1153
}
1139
1154
let input = TxIn {
1140
1155
previous_output : BitcoinOutPoint {
@@ -1182,8 +1197,15 @@ impl ChannelMonitor {
1182
1197
if let Some ( ref outpoints) = self . remote_claimable_outpoints. get( & $txid) {
1183
1198
for & ( ref htlc, ref source_option) in outpoints. iter( ) {
1184
1199
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( ) ) ) ;
1200
+ 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 ) ;
1201
+ match self . htlc_updated_waiting_threshold_conf. entry( height + HTLC_FAIL_ANTI_REORG_DELAY - 1 ) {
1202
+ hash_map:: Entry :: Occupied ( mut entry) => {
1203
+ entry. get_mut( ) . push( ( ( * * source) . clone( ) , None , htlc. payment_hash. clone( ) ) ) ;
1204
+ }
1205
+ hash_map:: Entry :: Vacant ( entry) => {
1206
+ entry. insert( vec![ ( ( * * source) . clone( ) , None , htlc. payment_hash. clone( ) ) ] ) ;
1207
+ }
1208
+ }
1187
1209
}
1188
1210
}
1189
1211
}
@@ -1199,7 +1221,7 @@ impl ChannelMonitor {
1199
1221
}
1200
1222
// No need to check local commitment txn, symmetric HTLCSource must be present as per-htlc data on remote commitment tx
1201
1223
}
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
1224
+ 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
1225
1204
1226
let outputs = vec ! ( TxOut {
1205
1227
script_pubkey: self . destination_script. clone( ) ,
@@ -1261,7 +1283,14 @@ impl ChannelMonitor {
1261
1283
}
1262
1284
}
1263
1285
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( ) ) ) ;
1286
+ match self . htlc_updated_waiting_threshold_conf. entry( height + HTLC_FAIL_ANTI_REORG_DELAY - 1 ) {
1287
+ hash_map:: Entry :: Occupied ( mut entry) => {
1288
+ entry. get_mut( ) . push( ( ( * * source) . clone( ) , None , htlc. payment_hash. clone( ) ) ) ;
1289
+ }
1290
+ hash_map:: Entry :: Vacant ( entry) => {
1291
+ entry. insert( vec![ ( ( * * source) . clone( ) , None , htlc. payment_hash. clone( ) ) ] ) ;
1292
+ }
1293
+ }
1265
1294
}
1266
1295
}
1267
1296
}
@@ -1294,7 +1323,7 @@ impl ChannelMonitor {
1294
1323
} ,
1295
1324
} ;
1296
1325
let a_htlc_key = match self . their_htlc_base_key {
1297
- None => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated ) ,
1326
+ None => return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs) ,
1298
1327
Some ( their_htlc_base_key) => ignore_error ! ( chan_utils:: derive_public_key( & self . secp_ctx, revocation_point, & their_htlc_base_key) ) ,
1299
1328
} ;
1300
1329
@@ -1349,7 +1378,7 @@ impl ChannelMonitor {
1349
1378
if transaction_output_index as usize >= tx. output . len ( ) ||
1350
1379
tx. output [ transaction_output_index as usize ] . value != htlc. amount_msat / 1000 ||
1351
1380
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
1381
+ return ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs) ; // Corrupted per_commitment_data, fuck this user
1353
1382
}
1354
1383
if let Some ( payment_preimage) = self . payment_preimages . get ( & htlc. payment_hash ) {
1355
1384
let input = TxIn {
@@ -1412,7 +1441,7 @@ impl ChannelMonitor {
1412
1441
}
1413
1442
}
1414
1443
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
1444
+ 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
1445
1417
1446
let outputs = vec ! ( TxOut {
1418
1447
script_pubkey: self . destination_script. clone( ) ,
@@ -1442,7 +1471,7 @@ impl ChannelMonitor {
1442
1471
}
1443
1472
}
1444
1473
1445
- ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs, htlc_updated )
1474
+ ( txn_to_broadcast, ( commitment_txid, watch_outputs) , spendable_outputs)
1446
1475
}
1447
1476
1448
1477
/// Attempts to claim a remote HTLC-Success/HTLC-Timeout's outputs using the revocation key
@@ -1714,7 +1743,7 @@ impl ChannelMonitor {
1714
1743
}
1715
1744
} ;
1716
1745
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) ;
1746
+ let ( remote_txn, new_outputs, mut spendable_output) = self . check_spend_remote_transaction ( tx, height) ;
1718
1747
txn = remote_txn;
1719
1748
spendable_outputs. append ( & mut spendable_output) ;
1720
1749
if !new_outputs. 1 . is_empty ( ) {
@@ -1733,9 +1762,6 @@ impl ChannelMonitor {
1733
1762
spendable_outputs. push ( spendable_output) ;
1734
1763
}
1735
1764
}
1736
- if updated. len ( ) > 0 {
1737
- htlc_updated. append ( & mut updated) ;
1738
- }
1739
1765
} else {
1740
1766
if let Some ( & ( commitment_number, _) ) = self . remote_commitment_txn_on_chain . get ( & prevout. txid ) {
1741
1767
let ( tx, spendable_output) = self . check_spend_remote_htlc ( tx, commitment_number) ;
@@ -1786,6 +1812,12 @@ impl ChannelMonitor {
1786
1812
}
1787
1813
}
1788
1814
}
1815
+ if let Some ( updates) = self . htlc_updated_waiting_threshold_conf . remove ( & height) {
1816
+ for update in updates {
1817
+ log_trace ! ( self , "HTLC {} failure update has get enough confirmation to be pass upstream" , log_bytes!( ( update. 2 ) . 0 ) ) ;
1818
+ htlc_updated. push ( update) ;
1819
+ }
1820
+ }
1789
1821
self . last_block_hash = block_hash. clone ( ) ;
1790
1822
( watch_outputs, spendable_outputs, htlc_updated)
1791
1823
}
@@ -2159,6 +2191,21 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
2159
2191
let last_block_hash: Sha256dHash = Readable :: read ( reader) ?;
2160
2192
let destination_script = Readable :: read ( reader) ?;
2161
2193
2194
+ let waiting_threshold_conf_len: u64 = Readable :: read ( reader) ?;
2195
+ let mut htlc_updated_waiting_threshold_conf = HashMap :: with_capacity ( cmp:: min ( waiting_threshold_conf_len as usize , MAX_ALLOC_SIZE / 128 ) ) ;
2196
+ for _ in 0 ..waiting_threshold_conf_len {
2197
+ let height_target = Readable :: read ( reader) ?;
2198
+ let updates_len: u64 = Readable :: read ( reader) ?;
2199
+ let mut updates = Vec :: with_capacity ( cmp:: min ( updates_len as usize , MAX_ALLOC_SIZE / 128 ) ) ;
2200
+ for _ in 0 ..updates_len {
2201
+ let htlc_source = Readable :: read ( reader) ?;
2202
+ let preimage = Readable :: read ( reader) ?;
2203
+ let hash = Readable :: read ( reader) ?;
2204
+ updates. push ( ( htlc_source, preimage, hash) ) ;
2205
+ }
2206
+ htlc_updated_waiting_threshold_conf. insert ( height_target, updates) ;
2207
+ }
2208
+
2162
2209
Ok ( ( last_block_hash. clone ( ) , ChannelMonitor {
2163
2210
commitment_transaction_number_obscure_factor,
2164
2211
@@ -2182,6 +2229,9 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
2182
2229
payment_preimages,
2183
2230
2184
2231
destination_script,
2232
+
2233
+ htlc_updated_waiting_threshold_conf,
2234
+
2185
2235
last_block_hash,
2186
2236
secp_ctx,
2187
2237
logger,
0 commit comments