Skip to content

Commit 5ade2f3

Browse files
committed
f: remove i64 casts in channel build_commitment_transaction
1 parent d0a0121 commit 5ade2f3

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

lightning/src/ln/channel.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4567,7 +4567,8 @@ where
45674567

45684568
let num_htlcs = self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len();
45694569
let mut htlcs_included: Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)> = Vec::with_capacity(num_htlcs);
4570-
let mut value_to_self_msat_offset = 0;
4570+
let mut value_to_self_claimed_msat = 0;
4571+
let mut value_to_remote_claimed_msat = 0;
45714572

45724573
log_trace!(logger, "Building commitment transaction number {} (really {} xor {}) for channel {} for {}, generated by {} with fee {}...",
45734574
commitment_number, (INITIAL_COMMITMENT_NUMBER - commitment_number),
@@ -4605,7 +4606,7 @@ where
46054606
log_trace!(logger, " ...not including inbound HTLC {} (hash {}) with value {} due to state ({})", htlc.htlc_id, htlc.payment_hash, htlc.amount_msat, htlc.state);
46064607
if let Some(preimage) = htlc.state.preimage() {
46074608
inbound_htlc_preimages.push(preimage);
4608-
value_to_self_msat_offset += htlc.amount_msat as i64;
4609+
value_to_self_claimed_msat += htlc.amount_msat;
46094610
}
46104611
}
46114612
};
@@ -4620,26 +4621,24 @@ where
46204621
} else {
46214622
log_trace!(logger, " ...not including outbound HTLC {} (hash {}) with value {} due to state ({})", htlc.htlc_id, htlc.payment_hash, htlc.amount_msat, htlc.state);
46224623
if htlc.state.preimage().is_some() {
4623-
value_to_self_msat_offset -= htlc.amount_msat as i64;
4624+
value_to_remote_claimed_msat += htlc.amount_msat;
46244625
}
46254626
}
46264627
};
46274628

46284629
// # Panics
46294630
//
4630-
// While we expect `value_to_self_msat_offset` to be negative in some cases, the local
4631-
// balance MUST remain greater than or equal to 0.
4631+
// After all HTLC claims have been accounted for, the local balance MUST remain greater than or equal to 0.
46324632

4633-
// TODO: When MSRV >= 1.66.0, use u64::checked_add_signed
4634-
let value_to_self_with_offset_msat = (funding.value_to_self_msat as i64 + value_to_self_msat_offset).try_into().unwrap();
4633+
let value_to_self_msat = (funding.value_to_self_msat + value_to_self_claimed_msat).checked_sub(value_to_remote_claimed_msat).unwrap();
46354634

46364635
let (tx, stats) = SpecTxBuilder {}.build_commitment_transaction(
46374636
local,
46384637
commitment_number,
46394638
per_commitment_point,
46404639
&funding.channel_transaction_parameters,
46414640
&self.secp_ctx,
4642-
value_to_self_with_offset_msat,
4641+
value_to_self_msat,
46434642
htlcs_included.iter().map(|(htlc, _source)| htlc).cloned().collect(),
46444643
feerate_per_kw,
46454644
broadcaster_dust_limit_sat,

0 commit comments

Comments
 (0)