Skip to content

Commit deadccf

Browse files
committed
Use more idiomatic sum method when possible
1 parent eac01d0 commit deadccf

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2863,8 +2863,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitor<Signer> {
28632863
// In addition to `commit_tx_fee_sat`, this can also include dust HTLCs, and the total msat amount rounded down from non-dust HTLCs
28642864
transaction_fee_satoshis: if us.holder_pays_commitment_tx_fee.unwrap_or(true) {
28652865
let transaction = &us.funding.current_holder_commitment_tx.trust().built_transaction().transaction;
2866-
// Unwrap here; commitment transactions always have at least one output
2867-
let output_value_sat = transaction.output.iter().map(|txout| txout.value).reduce(|sum, value| sum + value).unwrap().to_sat();
2866+
let output_value_sat: u64 = transaction.output.iter().map(|txout| txout.value.to_sat()).sum();
28682867
us.funding.channel_parameters.channel_value_satoshis - output_value_sat
28692868
} else { 0 },
28702869
outbound_payment_htlc_rounded_msat,

lightning/src/ln/channel.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4140,12 +4140,17 @@ where
41404140
// Channel state once they will not be present in the next received commitment
41414141
// transaction).
41424142
let (local_balance_before_fee_msat, remote_balance_before_fee_msat) = {
4143-
let mut removed_outbound_total_msat = 0;
4144-
for htlc in self.pending_outbound_htlcs.iter() {
4145-
if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _)) | OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _)) = htlc.state {
4146-
removed_outbound_total_msat += htlc.amount_msat;
4147-
}
4148-
}
4143+
let removed_outbound_total_msat: u64 = self.pending_outbound_htlcs
4144+
.iter()
4145+
.filter_map(|htlc| {
4146+
matches!(
4147+
htlc.state,
4148+
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _))
4149+
| OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _))
4150+
)
4151+
.then_some(htlc.amount_msat)
4152+
})
4153+
.sum();
41494154
let pending_value_to_self_msat =
41504155
funding.value_to_self_msat + htlc_stats.pending_inbound_htlcs_value_msat - removed_outbound_total_msat;
41514156
let pending_remote_value_msat =
@@ -4372,13 +4377,17 @@ where
43724377
}
43734378

43744379
if !funding.is_outbound() {
4375-
let mut removed_outbound_total_msat = 0;
4376-
for htlc in self.pending_outbound_htlcs.iter() {
4377-
if let OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _)) | OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _)) = htlc.state {
4378-
removed_outbound_total_msat += htlc.amount_msat;
4379-
}
4380-
}
4381-
4380+
let removed_outbound_total_msat: u64 = self.pending_outbound_htlcs
4381+
.iter()
4382+
.filter_map(|htlc| {
4383+
matches!(
4384+
htlc.state,
4385+
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_, _))
4386+
| OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_, _))
4387+
)
4388+
.then_some(htlc.amount_msat)
4389+
})
4390+
.sum();
43824391
let pending_value_to_self_msat =
43834392
funding.value_to_self_msat + htlc_stats.pending_inbound_htlcs_value_msat - removed_outbound_total_msat;
43844393
let pending_remote_value_msat =

0 commit comments

Comments
 (0)