Skip to content

Commit 48ccae1

Browse files
f Matt always upcast to u64, don't downcast to u32
1 parent ca96c87 commit 48ccae1

File tree

4 files changed

+31
-31
lines changed

4 files changed

+31
-31
lines changed

lightning/src/chain/chaininterface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub trait FeeEstimator: Sync + Send {
123123
}
124124

125125
/// Minimum relay fee as required by bitcoin network mempool policy.
126-
pub const MIN_RELAY_FEE_SAT_PER_1000_WEIGHT: u32 = 4000;
126+
pub const MIN_RELAY_FEE_SAT_PER_1000_WEIGHT: u64 = 4000;
127127

128128
/// Utility for tracking registered txn/outpoints and checking for matches
129129
#[cfg_attr(test, derive(PartialEq))]

lightning/src/ln/chan_utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,15 +475,15 @@ pub fn build_htlc_transaction(prev_hash: &Txid, feerate_per_kw: u32, to_self_del
475475
});
476476

477477
let total_fee = if htlc.offered {
478-
feerate_per_kw * HTLC_TIMEOUT_TX_WEIGHT as u32 / 1000
478+
feerate_per_kw as u64 * HTLC_TIMEOUT_TX_WEIGHT / 1000
479479
} else {
480-
feerate_per_kw * HTLC_SUCCESS_TX_WEIGHT as u32 / 1000
480+
feerate_per_kw as u64 * HTLC_SUCCESS_TX_WEIGHT / 1000
481481
};
482482

483483
let mut txouts: Vec<TxOut> = Vec::new();
484484
txouts.push(TxOut {
485485
script_pubkey: get_revokeable_redeemscript(revocation_key, to_self_delay, a_delayed_payment_key).to_v0_p2wsh(),
486-
value: htlc.amount_msat / 1000 - total_fee as u64 //TODO: BOLT 3 does not specify if we should add amount_msat before dividing or if we should divide by 1000 before subtracting (as we do here)
486+
value: htlc.amount_msat / 1000 - total_fee //TODO: BOLT 3 does not specify if we should add amount_msat before dividing or if we should divide by 1000 before subtracting (as we do here)
487487
});
488488

489489
Transaction {

lightning/src/ln/channel.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
561561
if feerate_per_kw < fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background) {
562562
return Err(ChannelError::Close("Peer's feerate much too low"));
563563
}
564-
if feerate_per_kw > fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority) * 2 {
564+
if feerate_per_kw as u64 > fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority) as u64 * 2 {
565565
return Err(ChannelError::Close("Peer's feerate much too high"));
566566
}
567567
Ok(())
@@ -1986,10 +1986,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
19861986
//If channel fee was updated by funder confirm funder can afford the new fee rate when applied to the current local commitment transaction
19871987
if update_fee {
19881988
let num_htlcs = local_commitment_tx.1;
1989-
let total_fee = feerate_per_kw * (COMMITMENT_TX_BASE_WEIGHT + (num_htlcs as u64) * COMMITMENT_TX_WEIGHT_PER_HTLC) as u32 / 1000;
1989+
let total_fee = feerate_per_kw as u64 * (COMMITMENT_TX_BASE_WEIGHT + (num_htlcs as u64) * COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000;
19901990

19911991
let remote_reserve_we_require = Channel::<ChanSigner>::get_remote_channel_reserve_satoshis(self.channel_value_satoshis);
1992-
if self.channel_value_satoshis - self.value_to_self_msat / 1000 < total_fee as u64 + remote_reserve_we_require {
1992+
if self.channel_value_satoshis - self.value_to_self_msat / 1000 < total_fee + remote_reserve_we_require {
19931993
return Err((None, ChannelError::Close("Funding remote cannot afford proposed new fee")));
19941994
}
19951995
}
@@ -3045,10 +3045,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
30453045
}
30463046
}
30473047

3048-
let proposed_sat_per_kw = msg.fee_satoshis as u32 * 1000 / closing_tx.get_weight() as u32;
3048+
let proposed_sat_per_kw = msg.fee_satoshis * 1000 / closing_tx.get_weight() as u64;
30493049
if self.channel_outbound {
30503050
let our_max_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
3051-
if proposed_sat_per_kw > our_max_feerate {
3051+
if (proposed_sat_per_kw as u32) > our_max_feerate {
30523052
if let Some((last_feerate, _, _)) = self.last_sent_closing_fee {
30533053
if our_max_feerate <= last_feerate {
30543054
return Err(ChannelError::Close("Unable to come to consensus about closing feerate, remote wanted something higher than our Normal feerate"));
@@ -3058,7 +3058,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
30583058
}
30593059
} else {
30603060
let our_min_feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
3061-
if proposed_sat_per_kw < our_min_feerate {
3061+
if (proposed_sat_per_kw as u32) < our_min_feerate {
30623062
if let Some((last_feerate, _, _)) = self.last_sent_closing_fee {
30633063
if our_min_feerate >= last_feerate {
30643064
return Err(ChannelError::Close("Unable to come to consensus about closing feerate, remote wanted something lower than our Background feerate"));

lightning/src/ln/onchaintx.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -151,32 +151,32 @@ impl Readable for InputDescriptors {
151151
macro_rules! subtract_high_prio_fee {
152152
($logger: ident, $fee_estimator: expr, $value: expr, $predicted_weight: expr, $used_feerate: expr) => {
153153
{
154-
$used_feerate = $fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority);
155-
let mut fee = $used_feerate * ($predicted_weight as u32) / 1000;
156-
if $value <= fee.into() {
157-
$used_feerate = $fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
158-
fee = $used_feerate * ($predicted_weight as u32) / 1000;
154+
$used_feerate = $fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority).into();
155+
let mut fee = $used_feerate as u64 * $predicted_weight / 1000;
156+
if $value <= fee {
157+
$used_feerate = $fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal).into();
158+
fee = $used_feerate as u64 * $predicted_weight / 1000;
159159
if $value <= fee.into() {
160-
$used_feerate = $fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background);
161-
fee = $used_feerate * ($predicted_weight as u32) / 1000;
162-
if $value <= fee.into() {
160+
$used_feerate = $fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background).into();
161+
fee = $used_feerate as u64 * $predicted_weight / 1000;
162+
if $value <= fee {
163163
log_error!($logger, "Failed to generate an on-chain punishment tx as even low priority fee ({} sat) was more than the entire claim balance ({} sat)",
164164
fee, $value);
165165
false
166166
} else {
167167
log_warn!($logger, "Used low priority fee for on-chain punishment tx as high priority fee was more than the entire claim balance ({} sat)",
168168
$value);
169-
$value -= fee as u64;
169+
$value -= fee;
170170
true
171171
}
172172
} else {
173173
log_warn!($logger, "Used medium priority fee for on-chain punishment tx as high priority fee was more than the entire claim balance ({} sat)",
174174
$value);
175-
$value -= fee as u64;
175+
$value -= fee;
176176
true
177177
}
178178
} else {
179-
$value -= fee as u64;
179+
$value -= fee;
180180
true
181181
}
182182
}
@@ -490,29 +490,29 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
490490
macro_rules! RBF_bump {
491491
($amount: expr, $old_feerate: expr, $fee_estimator: expr, $predicted_weight: expr) => {
492492
{
493-
let mut used_feerate;
493+
let mut used_feerate: u32;
494494
// If old feerate inferior to actual one given back by Fee Estimator, use it to compute new fee...
495495
let new_fee = if $old_feerate < $fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::HighPriority) {
496496
let mut value = $amount;
497497
if subtract_high_prio_fee!(logger, $fee_estimator, value, $predicted_weight, used_feerate) {
498498
// Overflow check is done in subtract_high_prio_fee
499-
($amount - value) as u32
499+
($amount - value)
500500
} else {
501501
log_trace!(logger, "Can't new-estimation bump new claiming tx, amount {} is too small", $amount);
502502
return None;
503503
}
504504
// ...else just increase the previous feerate by 25% (because that's a nice number)
505505
} else {
506-
let fee = $old_feerate * ($predicted_weight as u32) / 750;
507-
if $amount <= fee as u64 {
506+
let fee = $old_feerate as u64 * ($predicted_weight as u64) / 750;
507+
if $amount <= fee {
508508
log_trace!(logger, "Can't 25% bump new claiming tx, amount {} is too small", $amount);
509509
return None;
510510
}
511511
fee
512512
};
513513

514-
let previous_fee = $old_feerate * ($predicted_weight as u32) / 1000;
515-
let min_relay_fee = MIN_RELAY_FEE_SAT_PER_1000_WEIGHT * ($predicted_weight as u32) / 1000;
514+
let previous_fee = $old_feerate as u64 * ($predicted_weight as u64) / 1000;
515+
let min_relay_fee = MIN_RELAY_FEE_SAT_PER_1000_WEIGHT * ($predicted_weight as u64) / 1000;
516516
// BIP 125 Opt-in Full Replace-by-Fee Signaling
517517
// * 3. The replacement transaction pays an absolute fee of at least the sum paid by the original transactions.
518518
// * 4. The replacement transaction must also pay for its own bandwidth at or above the rate set by the node's minimum relay fee setting.
@@ -521,7 +521,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
521521
} else {
522522
new_fee
523523
};
524-
Some((new_fee, new_fee * 1000 / ($predicted_weight as u32)))
524+
Some((new_fee, new_fee * 1000 / ($predicted_weight as u64)))
525525
}
526526
}
527527
}
@@ -551,7 +551,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
551551
}
552552
}
553553
if dynamic_fee {
554-
let predicted_weight = bumped_tx.get_weight() + inputs_witnesses_weight;
554+
let predicted_weight = (bumped_tx.get_weight() + inputs_witnesses_weight) as u64;
555555
let mut new_feerate;
556556
// If old feerate is 0, first iteration of this claim, use normal fee calculation
557557
if cached_claim_datas.feerate_previous != 0 {
@@ -620,8 +620,8 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
620620
}
621621
}
622622
log_trace!(logger, "...with timer {}", new_timer.unwrap());
623-
assert!(predicted_weight >= bumped_tx.get_weight());
624-
return Some((new_timer, new_feerate, bumped_tx))
623+
assert!(predicted_weight >= bumped_tx.get_weight() as u64);
624+
return Some((new_timer, new_feerate as u32, bumped_tx))
625625
} else {
626626
for (_, (outp, per_outp_material)) in cached_claim_datas.per_input_material.iter().enumerate() {
627627
match per_outp_material {

0 commit comments

Comments
 (0)