@@ -34,7 +34,7 @@ use ln::chan_utils;
34
34
use ln:: chan_utils:: HTLCOutputInCommitment ;
35
35
use ln:: channelmanager:: { HTLCSource , PaymentPreimage , PaymentHash } ;
36
36
use ln:: channel:: { ACCEPTED_HTLC_SCRIPT_WEIGHT , OFFERED_HTLC_SCRIPT_WEIGHT } ;
37
- use chain:: chaininterface:: { ChainListener , ChainWatchInterface , BroadcasterInterface } ;
37
+ use chain:: chaininterface:: { ChainListener , ChainWatchInterface , BroadcasterInterface , FeeEstimator , ConfirmationTarget } ;
38
38
use chain:: transaction:: OutPoint ;
39
39
use chain:: keysinterface:: SpendableOutputDescriptor ;
40
40
use util:: logger:: Logger ;
@@ -143,6 +143,7 @@ pub struct SimpleManyChannelMonitor<Key> {
143
143
pending_events : Mutex < Vec < events:: Event > > ,
144
144
pending_htlc_updated : Mutex < HashMap < PaymentHash , Vec < ( HTLCSource , Option < PaymentPreimage > ) > > > ,
145
145
logger : Arc < Logger > ,
146
+ fee_estimator : Arc < FeeEstimator >
146
147
}
147
148
148
149
impl < Key : Send + cmp:: Eq + hash:: Hash > ChainListener for SimpleManyChannelMonitor < Key > {
@@ -153,7 +154,7 @@ impl<Key : Send + cmp::Eq + hash::Hash> ChainListener for SimpleManyChannelMonit
153
154
{
154
155
let mut monitors = self . monitors . lock ( ) . unwrap ( ) ;
155
156
for monitor in monitors. values_mut ( ) {
156
- let ( txn_outputs, spendable_outputs, mut htlc_updated) = monitor. block_connected ( txn_matched, height, & block_hash, & * self . broadcaster ) ;
157
+ let ( txn_outputs, spendable_outputs, mut htlc_updated) = monitor. block_connected ( txn_matched, height, & block_hash, & * self . broadcaster , & * self . fee_estimator ) ;
157
158
if spendable_outputs. len ( ) > 0 {
158
159
new_events. push ( events:: Event :: SpendableOutputs {
159
160
outputs : spendable_outputs,
@@ -210,14 +211,15 @@ impl<Key : Send + cmp::Eq + hash::Hash> ChainListener for SimpleManyChannelMonit
210
211
impl < Key : Send + cmp:: Eq + hash:: Hash + ' static > SimpleManyChannelMonitor < Key > {
211
212
/// Creates a new object which can be used to monitor several channels given the chain
212
213
/// interface with which to register to receive notifications.
213
- pub fn new ( chain_monitor : Arc < ChainWatchInterface > , broadcaster : Arc < BroadcasterInterface > , logger : Arc < Logger > ) -> Arc < SimpleManyChannelMonitor < Key > > {
214
+ pub fn new ( chain_monitor : Arc < ChainWatchInterface > , broadcaster : Arc < BroadcasterInterface > , logger : Arc < Logger > , feeest : Arc < FeeEstimator > ) -> Arc < SimpleManyChannelMonitor < Key > > {
214
215
let res = Arc :: new ( SimpleManyChannelMonitor {
215
216
monitors : Mutex :: new ( HashMap :: new ( ) ) ,
216
217
chain_monitor,
217
218
broadcaster,
218
219
pending_events : Mutex :: new ( Vec :: new ( ) ) ,
219
220
pending_htlc_updated : Mutex :: new ( HashMap :: new ( ) ) ,
220
221
logger,
222
+ fee_estimator : feeest,
221
223
} ) ;
222
224
let weak_res = Arc :: downgrade ( & res) ;
223
225
res. chain_monitor . register_listener ( weak_res) ;
@@ -340,6 +342,14 @@ struct LocalSignedTx {
340
342
htlc_outputs : Vec < ( HTLCOutputInCommitment , Option < ( Signature , Signature ) > , Option < HTLCSource > ) > ,
341
343
}
342
344
345
+ enum InputDescriptors {
346
+ RevokedOfferedHTLC ,
347
+ RevokedReceivedHTLC ,
348
+ OfferedHTLC ,
349
+ ReceivedHTLC ,
350
+ RevokedOutput , // either a revoked to_local output on commitment tx, a revoked HTLC-Timeout output or a revoked HTLC-Success output
351
+ }
352
+
343
353
const SERIALIZATION_VERSION : u8 = 1 ;
344
354
const MIN_SERIALIZATION_VERSION : u8 = 1 ;
345
355
@@ -475,6 +485,36 @@ impl ChannelMonitor {
475
485
}
476
486
}
477
487
488
+ fn get_witnesses_weight ( inputs : & Vec < InputDescriptors > ) -> u64 {
489
+ let mut tx_weight = 0 ;
490
+ for inp in inputs {
491
+ // We use expected weight (and not actual) as signatures and time lock delays may vary
492
+ tx_weight += match inp {
493
+ // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
494
+ InputDescriptors :: RevokedOfferedHTLC => {
495
+ 1 + 1 + 73 + 1 + 33 + 1 + 133
496
+ } ,
497
+ // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
498
+ InputDescriptors :: RevokedReceivedHTLC => {
499
+ 1 + 1 + 73 + 1 + 33 + 1 + 139
500
+ } ,
501
+ // number_of_witness_elements + sig_length + remotehtlc_sig + preimage_length + preimage + witness_script_length + witness_script
502
+ InputDescriptors :: OfferedHTLC => {
503
+ 1 + 1 + 73 + 1 + 32 + 1 + 133
504
+ } ,
505
+ // number_of_witness_elements + sig_length + revocation_sig + pubkey_length + revocationpubkey + witness_script_length + witness_script
506
+ InputDescriptors :: ReceivedHTLC => {
507
+ 1 + 1 + 73 + 1 + 1 + 1 + 139
508
+ } ,
509
+ // number_of_witness_elements + sig_length + revocation_sig + true_length + op_true + witness_script_length + witness_script
510
+ InputDescriptors :: RevokedOutput => {
511
+ 1 + 1 + 73 + 1 + 1 + 1 + 77
512
+ } ,
513
+ } ;
514
+ }
515
+ tx_weight
516
+ }
517
+
478
518
#[ inline]
479
519
fn place_secret ( idx : u64 ) -> u8 {
480
520
for i in 0 ..48 {
@@ -1019,7 +1059,7 @@ impl ChannelMonitor {
1019
1059
/// HTLC-Success/HTLC-Timeout transactions.
1020
1060
/// Return updates for HTLC pending in the channel and failed automatically by the broadcast of
1021
1061
/// revoked remote commitment tx
1022
- fn check_spend_remote_transaction ( & mut self , tx : & Transaction , height : u32 ) -> ( Vec < Transaction > , ( Sha256dHash , Vec < TxOut > ) , Vec < SpendableOutputDescriptor > , Vec < ( HTLCSource , Option < PaymentPreimage > , PaymentHash ) > ) {
1062
+ fn check_spend_remote_transaction ( & mut self , tx : & Transaction , height : u32 , fee_estimator : & FeeEstimator ) -> ( Vec < Transaction > , ( Sha256dHash , Vec < TxOut > ) , Vec < SpendableOutputDescriptor > , Vec < ( HTLCSource , Option < PaymentPreimage > , PaymentHash ) > ) {
1023
1063
// Most secp and related errors trying to create keys means we have no hope of constructing
1024
1064
// a spend transaction...so we return no transactions to broadcast
1025
1065
let mut txn_to_broadcast = Vec :: new ( ) ;
@@ -1077,6 +1117,7 @@ impl ChannelMonitor {
1077
1117
let mut values = Vec :: new ( ) ;
1078
1118
let mut inputs = Vec :: new ( ) ;
1079
1119
let mut htlc_idxs = Vec :: new ( ) ;
1120
+ let mut input_descriptors = Vec :: new ( ) ;
1080
1121
1081
1122
for ( idx, outp) in tx. output . iter ( ) . enumerate ( ) {
1082
1123
if outp. script_pubkey == revokeable_p2wsh {
@@ -1092,6 +1133,7 @@ impl ChannelMonitor {
1092
1133
htlc_idxs. push ( None ) ;
1093
1134
values. push ( outp. value ) ;
1094
1135
total_value += outp. value ;
1136
+ input_descriptors. push ( InputDescriptors :: RevokedOutput ) ;
1095
1137
} else if Some ( & outp. script_pubkey ) == local_payment_p2wpkh. as_ref ( ) {
1096
1138
spendable_outputs. push ( SpendableOutputDescriptor :: DynamicOutputP2WPKH {
1097
1139
outpoint : BitcoinOutPoint { txid : commitment_txid, vout : idx as u32 } ,
@@ -1155,6 +1197,7 @@ impl ChannelMonitor {
1155
1197
htlc_idxs. push ( Some ( idx) ) ;
1156
1198
values. push ( tx. output [ transaction_output_index as usize ] . value ) ;
1157
1199
total_value += htlc. amount_msat / 1000 ;
1200
+ input_descriptors. push ( if htlc. offered { InputDescriptors :: RevokedOfferedHTLC } else { InputDescriptors :: RevokedReceivedHTLC } ) ;
1158
1201
} else {
1159
1202
let mut single_htlc_tx = Transaction {
1160
1203
version : 2 ,
@@ -1165,6 +1208,7 @@ impl ChannelMonitor {
1165
1208
value: htlc. amount_msat / 1000 , //TODO: - fee
1166
1209
} ) ,
1167
1210
} ;
1211
+ single_htlc_tx. output [ 0 ] . value -= fee_estimator. get_est_sat_per_1000_weight ( ConfirmationTarget :: HighPriority ) * ( single_htlc_tx. get_weight ( ) + Self :: get_witnesses_weight ( & vec ! [ if htlc. offered { InputDescriptors :: RevokedOfferedHTLC } else { InputDescriptors :: RevokedReceivedHTLC } ] ) ) / 1000 ;
1168
1212
let sighash_parts = bip143:: SighashComponents :: new ( & single_htlc_tx) ;
1169
1213
sign_input ! ( sighash_parts, single_htlc_tx. input[ 0 ] , Some ( idx) , htlc. amount_msat / 1000 ) ;
1170
1214
txn_to_broadcast. push ( single_htlc_tx) ;
@@ -1208,14 +1252,15 @@ impl ChannelMonitor {
1208
1252
1209
1253
let outputs = vec ! ( TxOut {
1210
1254
script_pubkey: self . destination_script. clone( ) ,
1211
- value: total_value, //TODO: - fee
1255
+ value: total_value,
1212
1256
} ) ;
1213
1257
let mut spend_tx = Transaction {
1214
1258
version : 2 ,
1215
1259
lock_time : 0 ,
1216
1260
input : inputs,
1217
1261
output : outputs,
1218
1262
} ;
1263
+ spend_tx. output [ 0 ] . value -= fee_estimator. get_est_sat_per_1000_weight ( ConfirmationTarget :: HighPriority ) * ( spend_tx. get_weight ( ) + Self :: get_witnesses_weight ( & input_descriptors) ) / 1000 ;
1219
1264
1220
1265
let mut values_drain = values. drain ( ..) ;
1221
1266
let sighash_parts = bip143:: SighashComponents :: new ( & spend_tx) ;
@@ -1324,6 +1369,7 @@ impl ChannelMonitor {
1324
1369
let mut total_value = 0 ;
1325
1370
let mut values = Vec :: new ( ) ;
1326
1371
let mut inputs = Vec :: new ( ) ;
1372
+ let mut input_descriptors = Vec :: new ( ) ;
1327
1373
1328
1374
macro_rules! sign_input {
1329
1375
( $sighash_parts: expr, $input: expr, $amount: expr, $preimage: expr) => {
@@ -1370,16 +1416,18 @@ impl ChannelMonitor {
1370
1416
inputs. push ( input) ;
1371
1417
values. push ( ( tx. output [ transaction_output_index as usize ] . value , payment_preimage) ) ;
1372
1418
total_value += htlc. amount_msat / 1000 ;
1419
+ input_descriptors. push ( if htlc. offered { InputDescriptors :: OfferedHTLC } else { InputDescriptors :: ReceivedHTLC } ) ;
1373
1420
} else {
1374
1421
let mut single_htlc_tx = Transaction {
1375
1422
version : 2 ,
1376
1423
lock_time : 0 ,
1377
1424
input : vec ! [ input] ,
1378
1425
output : vec ! ( TxOut {
1379
1426
script_pubkey: self . destination_script. clone( ) ,
1380
- value: htlc. amount_msat / 1000 , //TODO: - fee
1427
+ value: htlc. amount_msat / 1000 ,
1381
1428
} ) ,
1382
1429
} ;
1430
+ single_htlc_tx. output [ 0 ] . value -= fee_estimator. get_est_sat_per_1000_weight ( ConfirmationTarget :: HighPriority ) * ( single_htlc_tx. get_weight ( ) + Self :: get_witnesses_weight ( & vec ! [ if htlc. offered { InputDescriptors :: OfferedHTLC } else { InputDescriptors :: ReceivedHTLC } ] ) ) / 1000 ;
1383
1431
let sighash_parts = bip143:: SighashComponents :: new ( & single_htlc_tx) ;
1384
1432
sign_input ! ( sighash_parts, single_htlc_tx. input[ 0 ] , htlc. amount_msat / 1000 , payment_preimage. 0 . to_vec( ) ) ;
1385
1433
spendable_outputs. push ( SpendableOutputDescriptor :: StaticOutput {
@@ -1421,14 +1469,15 @@ impl ChannelMonitor {
1421
1469
1422
1470
let outputs = vec ! ( TxOut {
1423
1471
script_pubkey: self . destination_script. clone( ) ,
1424
- value: total_value, //TODO: - fee
1472
+ value: total_value
1425
1473
} ) ;
1426
1474
let mut spend_tx = Transaction {
1427
1475
version : 2 ,
1428
1476
lock_time : 0 ,
1429
1477
input : inputs,
1430
1478
output : outputs,
1431
1479
} ;
1480
+ spend_tx. output [ 0 ] . value -= fee_estimator. get_est_sat_per_1000_weight ( ConfirmationTarget :: HighPriority ) * ( spend_tx. get_weight ( ) + Self :: get_witnesses_weight ( & input_descriptors) ) / 1000 ;
1432
1481
1433
1482
let mut values_drain = values. drain ( ..) ;
1434
1483
let sighash_parts = bip143:: SighashComponents :: new ( & spend_tx) ;
@@ -1698,7 +1747,7 @@ impl ChannelMonitor {
1698
1747
}
1699
1748
}
1700
1749
1701
- fn block_connected ( & mut self , txn_matched : & [ & Transaction ] , height : u32 , block_hash : & Sha256dHash , broadcaster : & BroadcasterInterface ) -> ( Vec < ( Sha256dHash , Vec < TxOut > ) > , Vec < SpendableOutputDescriptor > , Vec < ( HTLCSource , Option < PaymentPreimage > , PaymentHash ) > ) {
1750
+ fn block_connected ( & mut self , txn_matched : & [ & Transaction ] , height : u32 , block_hash : & Sha256dHash , broadcaster : & BroadcasterInterface , fee_estimator : & FeeEstimator ) -> ( Vec < ( Sha256dHash , Vec < TxOut > ) > , Vec < SpendableOutputDescriptor > , Vec < ( HTLCSource , Option < PaymentPreimage > , PaymentHash ) > ) {
1702
1751
let mut watch_outputs = Vec :: new ( ) ;
1703
1752
let mut spendable_outputs = Vec :: new ( ) ;
1704
1753
let mut htlc_updated = Vec :: new ( ) ;
@@ -1719,7 +1768,7 @@ impl ChannelMonitor {
1719
1768
}
1720
1769
} ;
1721
1770
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 ) {
1722
- let ( remote_txn, new_outputs, mut spendable_output, mut updated) = self . check_spend_remote_transaction ( tx, height) ;
1771
+ let ( remote_txn, new_outputs, mut spendable_output, mut updated) = self . check_spend_remote_transaction ( tx, height, fee_estimator ) ;
1723
1772
txn = remote_txn;
1724
1773
spendable_outputs. append ( & mut spendable_output) ;
1725
1774
if !new_outputs. 1 . is_empty ( ) {
0 commit comments