Skip to content

Commit 29906e7

Browse files
authored
Merge pull request #85 from TheBlueMatt/2018-07-fees
Convert fee API to per_kw instead of per_vb
2 parents f98d4b3 + b8b7cb2 commit 29906e7

File tree

6 files changed

+38
-33
lines changed

6 files changed

+38
-33
lines changed

fuzz/fuzz_targets/channel_target.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ struct FuzzEstimator<'a> {
8080
input: &'a InputData<'a>,
8181
}
8282
impl<'a> FeeEstimator for FuzzEstimator<'a> {
83-
fn get_est_sat_per_vbyte(&self, _: ConfirmationTarget) -> u64 {
83+
fn get_est_sat_per_1000_weight(&self, _: ConfirmationTarget) -> u64 {
8484
//TODO: We should actually be testing at least much more than 64k...
8585
match self.input.get_slice(2) {
86-
Some(slice) => slice_to_be16(slice) as u64,
86+
Some(slice) => slice_to_be16(slice) as u64 * 250,
8787
None => 0
8888
}
8989
}

fuzz/fuzz_targets/full_stack_target.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ struct FuzzEstimator {
8484
input: Arc<InputData>,
8585
}
8686
impl FeeEstimator for FuzzEstimator {
87-
fn get_est_sat_per_vbyte(&self, _: ConfirmationTarget) -> u64 {
87+
fn get_est_sat_per_1000_weight(&self, _: ConfirmationTarget) -> u64 {
8888
//TODO: We should actually be testing at least much more than 64k...
8989
match self.input.get_slice(2) {
90-
Some(slice) => slice_to_be16(slice) as u64,
90+
Some(slice) => slice_to_be16(slice) as u64 * 250,
9191
None => 0
9292
}
9393
}

src/chain/chaininterface.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ pub enum ConfirmationTarget {
5757
/// called from inside the library in response to ChainListener events, P2P events, or timer
5858
/// events).
5959
pub trait FeeEstimator: Sync + Send {
60-
fn get_est_sat_per_vbyte(&self, confirmation_target: ConfirmationTarget) -> u64;
60+
/// Gets estimated satoshis of fee required per 1000 Weight-Units. This translates to:
61+
/// * satoshis-per-byte * 250
62+
/// * ceil(satoshis-per-kbyte / 4)
63+
/// Must be no smaller than 253 (ie 1 satoshi-per-byte rounded up to ensure later round-downs
64+
/// don't put us below 1 satoshi-per-byte).
65+
fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u64;
6166
}
6267

6368
/// Utility to capture some common parts of ChainWatchInterface implementors.

src/ln/channel.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ impl Channel {
350350
}
351351

352352
fn derive_our_dust_limit_satoshis(at_open_background_feerate: u64) -> u64 {
353-
at_open_background_feerate * B_OUTPUT_PLUS_SPENDING_INPUT_WEIGHT //TODO
353+
at_open_background_feerate * B_OUTPUT_PLUS_SPENDING_INPUT_WEIGHT / 1000 //TODO
354354
}
355355

356356
fn derive_our_htlc_minimum_msat(_at_open_channel_feerate_per_kw: u64) -> u64 {
@@ -365,8 +365,8 @@ impl Channel {
365365
panic!("funding value > 2^24");
366366
}
367367

368-
let feerate = fee_estimator.get_est_sat_per_vbyte(ConfirmationTarget::Normal);
369-
let background_feerate = fee_estimator.get_est_sat_per_vbyte(ConfirmationTarget::Background);
368+
let feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
369+
let background_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
370370

371371
let secp_ctx = Secp256k1::new();
372372
let our_channel_monitor_claim_key_hash = Hash160::from_data(&PublicKey::from_secret_key(&secp_ctx, &chan_keys.channel_monitor_claim_key).unwrap().serialize());
@@ -405,13 +405,13 @@ impl Channel {
405405
last_block_connected: Default::default(),
406406
funding_tx_confirmations: 0,
407407

408-
feerate_per_kw: feerate * 250,
408+
feerate_per_kw: feerate,
409409
their_dust_limit_satoshis: 0,
410410
our_dust_limit_satoshis: Channel::derive_our_dust_limit_satoshis(background_feerate),
411411
their_max_htlc_value_in_flight_msat: 0,
412412
their_channel_reserve_satoshis: 0,
413413
their_htlc_minimum_msat: 0,
414-
our_htlc_minimum_msat: Channel::derive_our_htlc_minimum_msat(feerate * 250),
414+
our_htlc_minimum_msat: Channel::derive_our_htlc_minimum_msat(feerate),
415415
their_to_self_delay: 0,
416416
their_max_accepted_htlcs: 0,
417417

@@ -432,10 +432,10 @@ impl Channel {
432432
}
433433

434434
fn check_remote_fee(fee_estimator: &FeeEstimator, feerate_per_kw: u32) -> Result<(), HandleError> {
435-
if (feerate_per_kw as u64) < fee_estimator.get_est_sat_per_vbyte(ConfirmationTarget::Background) * 250 {
435+
if (feerate_per_kw as u64) < fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background) {
436436
return Err(HandleError{err: "Peer's feerate much too low", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })});
437437
}
438-
if (feerate_per_kw as u64) > fee_estimator.get_est_sat_per_vbyte(ConfirmationTarget::HighPriority) * 375 { // 375 = 250 * 1.5x
438+
if (feerate_per_kw as u64) > fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority) * 2 {
439439
return Err(HandleError{err: "Peer's feerate much too high", action: Some(msgs::ErrorAction::DisconnectPeer{ msg: None })});
440440
}
441441
Ok(())
@@ -477,7 +477,7 @@ impl Channel {
477477

478478
let their_announce = if (msg.channel_flags & 1) == 1 { true } else { false };
479479

480-
let background_feerate = fee_estimator.get_est_sat_per_vbyte(ConfirmationTarget::Background);
480+
let background_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
481481

482482
let secp_ctx = Secp256k1::new();
483483
let our_channel_monitor_claim_key_hash = Hash160::from_data(&PublicKey::from_secret_key(&secp_ctx, &chan_keys.channel_monitor_claim_key).unwrap().serialize());
@@ -1650,12 +1650,12 @@ impl Channel {
16501650
let our_closing_script = self.get_closing_scriptpubkey();
16511651

16521652
let (proposed_feerate, proposed_fee, our_sig) = if self.channel_outbound && self.pending_htlcs.is_empty() {
1653-
let mut proposed_feerate = fee_estimator.get_est_sat_per_vbyte(ConfirmationTarget::Background);
1654-
if self.feerate_per_kw > proposed_feerate * 250 {
1655-
proposed_feerate = self.feerate_per_kw / 250;
1653+
let mut proposed_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
1654+
if self.feerate_per_kw > proposed_feerate {
1655+
proposed_feerate = self.feerate_per_kw;
16561656
}
16571657
let tx_weight = Self::get_closing_transaction_weight(&our_closing_script, &msg.scriptpubkey);
1658-
let proposed_total_fee_satoshis = proposed_feerate * tx_weight / 4;
1658+
let proposed_total_fee_satoshis = proposed_feerate * tx_weight / 1000;
16591659

16601660
let (closing_tx, total_fee_satoshis) = self.build_closing_transaction(proposed_total_fee_satoshis, false);
16611661
let funding_redeemscript = self.get_funding_redeemscript();
@@ -1754,7 +1754,7 @@ impl Channel {
17541754
macro_rules! propose_new_feerate {
17551755
($new_feerate: expr) => {
17561756
let closing_tx_max_weight = Self::get_closing_transaction_weight(&self.get_closing_scriptpubkey(), self.their_shutdown_scriptpubkey.as_ref().unwrap());
1757-
let (closing_tx, used_total_fee) = self.build_closing_transaction($new_feerate * closing_tx_max_weight / 4, false);
1757+
let (closing_tx, used_total_fee) = self.build_closing_transaction($new_feerate * closing_tx_max_weight / 1000, false);
17581758
sighash = Message::from_slice(&bip143::SighashComponents::new(&closing_tx).sighash_all(&closing_tx.input[0], &funding_redeemscript, self.channel_value_satoshis)[..]).unwrap();
17591759
let our_sig = self.secp_ctx.sign(&sighash, &self.local_keys.funding_key).unwrap();
17601760
self.last_sent_closing_fee = Some(($new_feerate, used_total_fee));
@@ -1766,10 +1766,10 @@ impl Channel {
17661766
}
17671767
}
17681768

1769-
let proposed_sat_per_vbyte = msg.fee_satoshis * 4 / closing_tx.get_weight();
1769+
let proposed_sat_per_kw = msg.fee_satoshis * 1000 / closing_tx.get_weight();
17701770
if self.channel_outbound {
1771-
let our_max_feerate = fee_estimator.get_est_sat_per_vbyte(ConfirmationTarget::Normal);
1772-
if proposed_sat_per_vbyte > our_max_feerate {
1771+
let our_max_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
1772+
if proposed_sat_per_kw > our_max_feerate {
17731773
if let Some((last_feerate, _)) = self.last_sent_closing_fee {
17741774
if our_max_feerate <= last_feerate {
17751775
return Err(HandleError{err: "Unable to come to consensus about closing feerate, remote wanted something higher than our Normal feerate", action: None});
@@ -1778,8 +1778,8 @@ impl Channel {
17781778
propose_new_feerate!(our_max_feerate);
17791779
}
17801780
} else {
1781-
let our_min_feerate = fee_estimator.get_est_sat_per_vbyte(ConfirmationTarget::Background);
1782-
if proposed_sat_per_vbyte < our_min_feerate {
1781+
let our_min_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
1782+
if proposed_sat_per_kw < our_min_feerate {
17831783
if let Some((last_feerate, _)) = self.last_sent_closing_fee {
17841784
if our_min_feerate >= last_feerate {
17851785
return Err(HandleError{err: "Unable to come to consensus about closing feerate, remote wanted something lower than our Background feerate", action: None});
@@ -1857,15 +1857,15 @@ impl Channel {
18571857
// output value back into a transaction with the regular channel output:
18581858

18591859
// the fee cost of the HTLC-Success/HTLC-Timeout transaction:
1860-
let mut res = self.feerate_per_kw * cmp::max(HTLC_TIMEOUT_TX_WEIGHT, HTLC_SUCCESS_TX_WEIGHT);
1860+
let mut res = self.feerate_per_kw * cmp::max(HTLC_TIMEOUT_TX_WEIGHT, HTLC_SUCCESS_TX_WEIGHT) / 1000;
18611861

18621862
if self.channel_outbound {
18631863
// + the marginal fee increase cost to us in the commitment transaction:
1864-
res += self.feerate_per_kw * COMMITMENT_TX_WEIGHT_PER_HTLC;
1864+
res += self.feerate_per_kw * COMMITMENT_TX_WEIGHT_PER_HTLC / 1000;
18651865
}
18661866

18671867
// + the marginal cost of an input which spends the HTLC-Success/HTLC-Timeout output:
1868-
res += fee_estimator.get_est_sat_per_vbyte(ConfirmationTarget::Normal) * SPENDING_INPUT_FOR_A_OUTPUT_WEIGHT * 250;
1868+
res += fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal) * SPENDING_INPUT_FOR_A_OUTPUT_WEIGHT / 1000;
18691869

18701870
res as u32
18711871
}
@@ -1988,7 +1988,7 @@ impl Channel {
19881988
max_htlc_value_in_flight_msat: Channel::get_our_max_htlc_value_in_flight_msat(self.channel_value_satoshis),
19891989
channel_reserve_satoshis: Channel::get_our_channel_reserve_satoshis(self.channel_value_satoshis),
19901990
htlc_minimum_msat: self.our_htlc_minimum_msat,
1991-
feerate_per_kw: fee_estimator.get_est_sat_per_vbyte(ConfirmationTarget::Background) as u32 * 250,
1991+
feerate_per_kw: fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background) as u32,
19921992
to_self_delay: BREAKDOWN_TIMEOUT,
19931993
max_accepted_htlcs: OUR_MAX_HTLCS,
19941994
funding_pubkey: PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.funding_key).unwrap(),
@@ -2350,7 +2350,7 @@ mod tests {
23502350
fee_est: u64
23512351
}
23522352
impl FeeEstimator for TestFeeEstimator {
2353-
fn get_est_sat_per_vbyte(&self, _: ConfirmationTarget) -> u64 {
2353+
fn get_est_sat_per_1000_weight(&self, _: ConfirmationTarget) -> u64 {
23542354
self.fee_est
23552355
}
23562356
}
@@ -2364,7 +2364,7 @@ mod tests {
23642364
#[test]
23652365
fn outbound_commitment_test() {
23662366
// Test vectors from BOLT 3 Appendix C:
2367-
let feeest = TestFeeEstimator{fee_est: 15000/250};
2367+
let feeest = TestFeeEstimator{fee_est: 15000};
23682368
let secp_ctx = Secp256k1::new();
23692369

23702370
let chan_keys = ChannelKeys {

src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2595,7 +2595,7 @@ mod tests {
25952595
let secp_ctx = Secp256k1::new();
25962596

25972597
for _ in 0..node_count {
2598-
let feeest = Arc::new(test_utils::TestFeeEstimator { sat_per_vbyte: 1 });
2598+
let feeest = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 });
25992599
let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new());
26002600
let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new())});
26012601
let chan_monitor = Arc::new(test_utils::TestChannelMonitor::new(chain_monitor.clone(), tx_broadcaster.clone()));

src/util/test_utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ use std::sync::{Arc,Mutex};
1414
use std::{mem};
1515

1616
pub struct TestFeeEstimator {
17-
pub sat_per_vbyte: u64,
17+
pub sat_per_kw: u64,
1818
}
1919
impl chaininterface::FeeEstimator for TestFeeEstimator {
20-
fn get_est_sat_per_vbyte(&self, _confirmation_target: ConfirmationTarget) -> u64 {
21-
self.sat_per_vbyte
20+
fn get_est_sat_per_1000_weight(&self, _confirmation_target: ConfirmationTarget) -> u64 {
21+
self.sat_per_kw
2222
}
2323
}
2424

0 commit comments

Comments
 (0)