diff --git a/.changelog/unreleased/improvements/1395-optimize-voting-power-calculation.md b/.changelog/unreleased/improvements/1395-optimize-voting-power-calculation.md new file mode 100644 index 00000000..0616950b --- /dev/null +++ b/.changelog/unreleased/improvements/1395-optimize-voting-power-calculation.md @@ -0,0 +1 @@ +- `[cometbft-light-client-verifer]` Optimizing voting power calculation by breaking the loop when we have enough voting power ([#19](https://github.com/cometbft/cometbft-rs/issues/19)). diff --git a/light-client-verifier/src/operations/voting_power.rs b/light-client-verifier/src/operations/voting_power.rs index 65e3ddb8..af68551d 100644 --- a/light-client-verifier/src/operations/voting_power.rs +++ b/light-client-verifier/src/operations/voting_power.rs @@ -131,6 +131,7 @@ impl VotingPowerCalculator for ProvidedVotingPowerCalcul trust_threshold: TrustThreshold, ) -> Result { let signatures = &signed_header.commit.signatures; + let total_voting_power = self.total_power_of(validator_set); let mut tallied_voting_power = 0_u64; let mut seen_validators = HashSet::new(); @@ -185,12 +186,14 @@ impl VotingPowerCalculator for ProvidedVotingPowerCalcul // to measure validator availability. } - // TODO: Break out of the loop when we have enough voting power. - // See https://github.com/informalsystems/tendermint-rs/issues/235 + // Break out of the loop when we have enough voting power. + if trust_threshold.is_enough_power(tallied_voting_power, total_voting_power) { + break; + } } let voting_power = VotingPowerTally { - total: self.total_power_of(validator_set), + total: total_voting_power, tallied: tallied_voting_power, trust_threshold, };