Skip to content

Commit a5aae94

Browse files
author
Antoine Riard
committed
Add FeeEstimator in ChannelMonitor
1 parent f5a5729 commit a5aae94

File tree

6 files changed

+68
-19
lines changed

6 files changed

+68
-19
lines changed

fuzz/fuzz_targets/chanmon_fail_consistency.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ pub struct TestChannelMonitor {
6969
pub update_ret: Mutex<Result<(), channelmonitor::ChannelMonitorUpdateErr>>,
7070
}
7171
impl TestChannelMonitor {
72-
pub fn new(chain_monitor: Arc<chaininterface::ChainWatchInterface>, broadcaster: Arc<chaininterface::BroadcasterInterface>, logger: Arc<Logger>) -> Self {
72+
pub fn new(chain_monitor: Arc<chaininterface::ChainWatchInterface>, broadcaster: Arc<chaininterface::BroadcasterInterface>, logger: Arc<Logger>, feeest: Arc<chaininterface::FeeEstimator>) -> Self {
7373
Self {
74-
simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster, logger),
74+
simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster, logger, feeest),
7575
update_ret: Mutex::new(Ok(())),
7676
}
7777
}
@@ -142,7 +142,7 @@ pub fn do_test(data: &[u8]) {
142142
($node_id: expr) => { {
143143
let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new($node_id.to_string()));
144144
let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin, Arc::clone(&logger)));
145-
let monitor = Arc::new(TestChannelMonitor::new(watch.clone(), broadcast.clone(), logger.clone()));
145+
let monitor = Arc::new(TestChannelMonitor::new(watch.clone(), broadcast.clone(), logger.clone(), fee_est.clone()));
146146

147147
let keys_manager = Arc::new(KeyProvider { node_id: $node_id });
148148
let mut config = UserConfig::new();

fuzz/fuzz_targets/full_stack_target.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ pub fn do_test(data: &[u8], logger: &Arc<Logger>) {
344344

345345
let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin, Arc::clone(&logger)));
346346
let broadcast = Arc::new(TestBroadcaster{});
347-
let monitor = channelmonitor::SimpleManyChannelMonitor::new(watch.clone(), broadcast.clone(), Arc::clone(&logger));
347+
let monitor = channelmonitor::SimpleManyChannelMonitor::new(watch.clone(), broadcast.clone(), Arc::clone(&logger), fee_est.clone());
348348

349349
let keys_manager = Arc::new(KeyProvider { node_secret: our_network_key.clone() });
350350
let mut config = UserConfig::new();

src/ln/channelmonitor.rs

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use ln::chan_utils;
3434
use ln::chan_utils::HTLCOutputInCommitment;
3535
use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
3636
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};
3838
use chain::transaction::OutPoint;
3939
use chain::keysinterface::SpendableOutputDescriptor;
4040
use util::logger::Logger;
@@ -143,6 +143,7 @@ pub struct SimpleManyChannelMonitor<Key> {
143143
pending_events: Mutex<Vec<events::Event>>,
144144
pending_htlc_updated: Mutex<HashMap<PaymentHash, Vec<(HTLCSource, Option<PaymentPreimage>)>>>,
145145
logger: Arc<Logger>,
146+
fee_estimator: Arc<FeeEstimator>
146147
}
147148

148149
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
153154
{
154155
let mut monitors = self.monitors.lock().unwrap();
155156
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);
157158
if spendable_outputs.len() > 0 {
158159
new_events.push(events::Event::SpendableOutputs {
159160
outputs: spendable_outputs,
@@ -210,14 +211,15 @@ impl<Key : Send + cmp::Eq + hash::Hash> ChainListener for SimpleManyChannelMonit
210211
impl<Key : Send + cmp::Eq + hash::Hash + 'static> SimpleManyChannelMonitor<Key> {
211212
/// Creates a new object which can be used to monitor several channels given the chain
212213
/// 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>> {
214215
let res = Arc::new(SimpleManyChannelMonitor {
215216
monitors: Mutex::new(HashMap::new()),
216217
chain_monitor,
217218
broadcaster,
218219
pending_events: Mutex::new(Vec::new()),
219220
pending_htlc_updated: Mutex::new(HashMap::new()),
220221
logger,
222+
fee_estimator: feeest,
221223
});
222224
let weak_res = Arc::downgrade(&res);
223225
res.chain_monitor.register_listener(weak_res);
@@ -340,6 +342,14 @@ struct LocalSignedTx {
340342
htlc_outputs: Vec<(HTLCOutputInCommitment, Option<(Signature, Signature)>, Option<HTLCSource>)>,
341343
}
342344

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+
343353
const SERIALIZATION_VERSION: u8 = 1;
344354
const MIN_SERIALIZATION_VERSION: u8 = 1;
345355

@@ -475,6 +485,36 @@ impl ChannelMonitor {
475485
}
476486
}
477487

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+
478518
#[inline]
479519
fn place_secret(idx: u64) -> u8 {
480520
for i in 0..48 {
@@ -1019,7 +1059,7 @@ impl ChannelMonitor {
10191059
/// HTLC-Success/HTLC-Timeout transactions.
10201060
/// Return updates for HTLC pending in the channel and failed automatically by the broadcast of
10211061
/// 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)>) {
10231063
// Most secp and related errors trying to create keys means we have no hope of constructing
10241064
// a spend transaction...so we return no transactions to broadcast
10251065
let mut txn_to_broadcast = Vec::new();
@@ -1077,6 +1117,7 @@ impl ChannelMonitor {
10771117
let mut values = Vec::new();
10781118
let mut inputs = Vec::new();
10791119
let mut htlc_idxs = Vec::new();
1120+
let mut input_descriptors = Vec::new();
10801121

10811122
for (idx, outp) in tx.output.iter().enumerate() {
10821123
if outp.script_pubkey == revokeable_p2wsh {
@@ -1092,6 +1133,7 @@ impl ChannelMonitor {
10921133
htlc_idxs.push(None);
10931134
values.push(outp.value);
10941135
total_value += outp.value;
1136+
input_descriptors.push(InputDescriptors::RevokedOutput);
10951137
} else if Some(&outp.script_pubkey) == local_payment_p2wpkh.as_ref() {
10961138
spendable_outputs.push(SpendableOutputDescriptor::DynamicOutputP2WPKH {
10971139
outpoint: BitcoinOutPoint { txid: commitment_txid, vout: idx as u32 },
@@ -1155,6 +1197,7 @@ impl ChannelMonitor {
11551197
htlc_idxs.push(Some(idx));
11561198
values.push(tx.output[transaction_output_index as usize].value);
11571199
total_value += htlc.amount_msat / 1000;
1200+
input_descriptors.push(if htlc.offered { InputDescriptors::RevokedOfferedHTLC } else { InputDescriptors::RevokedReceivedHTLC });
11581201
} else {
11591202
let mut single_htlc_tx = Transaction {
11601203
version: 2,
@@ -1165,6 +1208,7 @@ impl ChannelMonitor {
11651208
value: htlc.amount_msat / 1000, //TODO: - fee
11661209
}),
11671210
};
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;
11681212
let sighash_parts = bip143::SighashComponents::new(&single_htlc_tx);
11691213
sign_input!(sighash_parts, single_htlc_tx.input[0], Some(idx), htlc.amount_msat / 1000);
11701214
txn_to_broadcast.push(single_htlc_tx);
@@ -1208,14 +1252,15 @@ impl ChannelMonitor {
12081252

12091253
let outputs = vec!(TxOut {
12101254
script_pubkey: self.destination_script.clone(),
1211-
value: total_value, //TODO: - fee
1255+
value: total_value,
12121256
});
12131257
let mut spend_tx = Transaction {
12141258
version: 2,
12151259
lock_time: 0,
12161260
input: inputs,
12171261
output: outputs,
12181262
};
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;
12191264

12201265
let mut values_drain = values.drain(..);
12211266
let sighash_parts = bip143::SighashComponents::new(&spend_tx);
@@ -1324,6 +1369,7 @@ impl ChannelMonitor {
13241369
let mut total_value = 0;
13251370
let mut values = Vec::new();
13261371
let mut inputs = Vec::new();
1372+
let mut input_descriptors = Vec::new();
13271373

13281374
macro_rules! sign_input {
13291375
($sighash_parts: expr, $input: expr, $amount: expr, $preimage: expr) => {
@@ -1370,16 +1416,18 @@ impl ChannelMonitor {
13701416
inputs.push(input);
13711417
values.push((tx.output[transaction_output_index as usize].value, payment_preimage));
13721418
total_value += htlc.amount_msat / 1000;
1419+
input_descriptors.push(if htlc.offered { InputDescriptors::OfferedHTLC } else { InputDescriptors::ReceivedHTLC });
13731420
} else {
13741421
let mut single_htlc_tx = Transaction {
13751422
version: 2,
13761423
lock_time: 0,
13771424
input: vec![input],
13781425
output: vec!(TxOut {
13791426
script_pubkey: self.destination_script.clone(),
1380-
value: htlc.amount_msat / 1000, //TODO: - fee
1427+
value: htlc.amount_msat / 1000,
13811428
}),
13821429
};
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;
13831431
let sighash_parts = bip143::SighashComponents::new(&single_htlc_tx);
13841432
sign_input!(sighash_parts, single_htlc_tx.input[0], htlc.amount_msat / 1000, payment_preimage.0.to_vec());
13851433
spendable_outputs.push(SpendableOutputDescriptor::StaticOutput {
@@ -1421,14 +1469,15 @@ impl ChannelMonitor {
14211469

14221470
let outputs = vec!(TxOut {
14231471
script_pubkey: self.destination_script.clone(),
1424-
value: total_value, //TODO: - fee
1472+
value: total_value
14251473
});
14261474
let mut spend_tx = Transaction {
14271475
version: 2,
14281476
lock_time: 0,
14291477
input: inputs,
14301478
output: outputs,
14311479
};
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;
14321481

14331482
let mut values_drain = values.drain(..);
14341483
let sighash_parts = bip143::SighashComponents::new(&spend_tx);
@@ -1698,7 +1747,7 @@ impl ChannelMonitor {
16981747
}
16991748
}
17001749

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)>) {
17021751
let mut watch_outputs = Vec::new();
17031752
let mut spendable_outputs = Vec::new();
17041753
let mut htlc_updated = Vec::new();
@@ -1719,7 +1768,7 @@ impl ChannelMonitor {
17191768
}
17201769
};
17211770
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);
17231772
txn = remote_txn;
17241773
spendable_outputs.append(&mut spendable_output);
17251774
if !new_outputs.1.is_empty() {

src/ln/functional_test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ pub fn create_network(node_count: usize) -> Vec<Node> {
821821
let mut seed = [0; 32];
822822
rng.fill_bytes(&mut seed);
823823
let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet, Arc::clone(&logger)));
824-
let chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone()));
824+
let chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone(), logger.clone(), feeest.clone()));
825825
let mut config = UserConfig::new();
826826
config.channel_options.announced_channel = true;
827827
config.channel_limits.force_announced_channel_preference = false;

src/ln/functional_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3210,7 +3210,7 @@ fn test_no_txn_manager_serialize_deserialize() {
32103210
let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
32113211
nodes[0].chan_monitor.simple_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write_for_disk(&mut chan_0_monitor_serialized).unwrap();
32123212

3213-
nodes[0].chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone(), Arc::new(test_utils::TestLogger::new())));
3213+
nodes[0].chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone(), Arc::new(test_utils::TestLogger::new()), Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 })));
32143214
let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..];
32153215
let (_, chan_0_monitor) = <(Sha256dHash, ChannelMonitor)>::read(&mut chan_0_monitor_read, Arc::new(test_utils::TestLogger::new())).unwrap();
32163216
assert!(chan_0_monitor_read.is_empty());
@@ -3276,7 +3276,7 @@ fn test_simple_manager_serialize_deserialize() {
32763276
let mut chan_0_monitor_serialized = test_utils::TestVecWriter(Vec::new());
32773277
nodes[0].chan_monitor.simple_monitor.monitors.lock().unwrap().iter().next().unwrap().1.write_for_disk(&mut chan_0_monitor_serialized).unwrap();
32783278

3279-
nodes[0].chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone(), Arc::new(test_utils::TestLogger::new())));
3279+
nodes[0].chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone(), Arc::new(test_utils::TestLogger::new()), Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 })));
32803280
let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..];
32813281
let (_, chan_0_monitor) = <(Sha256dHash, ChannelMonitor)>::read(&mut chan_0_monitor_read, Arc::new(test_utils::TestLogger::new())).unwrap();
32823282
assert!(chan_0_monitor_read.is_empty());
@@ -3336,7 +3336,7 @@ fn test_manager_serialize_deserialize_inconsistent_monitor() {
33363336
node_0_monitors_serialized.push(writer.0);
33373337
}
33383338

3339-
nodes[0].chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone(), Arc::new(test_utils::TestLogger::new())));
3339+
nodes[0].chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone(), Arc::new(test_utils::TestLogger::new()), Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 })));
33403340
let mut node_0_monitors = Vec::new();
33413341
for serialized in node_0_monitors_serialized.iter() {
33423342
let mut read = &serialized[..];

src/util/test_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ pub struct TestChannelMonitor {
4646
pub update_ret: Mutex<Result<(), channelmonitor::ChannelMonitorUpdateErr>>,
4747
}
4848
impl TestChannelMonitor {
49-
pub fn new(chain_monitor: Arc<chaininterface::ChainWatchInterface>, broadcaster: Arc<chaininterface::BroadcasterInterface>, logger: Arc<Logger>) -> Self {
49+
pub fn new(chain_monitor: Arc<chaininterface::ChainWatchInterface>, broadcaster: Arc<chaininterface::BroadcasterInterface>, logger: Arc<Logger>, fee_estimator: Arc<chaininterface::FeeEstimator>) -> Self {
5050
Self {
5151
added_monitors: Mutex::new(Vec::new()),
52-
simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster, logger),
52+
simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster, logger, fee_estimator),
5353
update_ret: Mutex::new(Ok(())),
5454
}
5555
}

0 commit comments

Comments
 (0)