From 25d92d05f8734d32ad32569820eb2f112ddf0239 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Tue, 25 Feb 2020 10:04:24 +0100 Subject: [PATCH] types: VerifyCommitX return when +2/3 sigs are verified (#4445) Closes #4417 --- CHANGELOG_PENDING.md | 2 ++ types/validator_set.go | 24 +++++++++++++----------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index a2fc85ee0..ebc087136 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -19,6 +19,8 @@ program](https://hackerone.com/tendermint). ### IMPROVEMENTS: +- [types] [\#4417](https://github.com/tendermint/tendermint/issues/4417) VerifyCommitX() functions should return as soon as +2/3 threashold is reached. + ### BUG FIXES: - [rpc] [\#4437](https://github.com/tendermint/tendermint/pull/4437) Fix tx_search pagination with ordered results (@erikgrinaker) diff --git a/types/validator_set.go b/types/validator_set.go index 390df09cf..04de50646 100644 --- a/types/validator_set.go +++ b/types/validator_set.go @@ -636,6 +636,7 @@ func (vals *ValidatorSet) VerifyCommit(chainID string, blockID BlockID, } talliedVotingPower := int64(0) + votingPowerNeeded := vals.TotalVotingPower() * 2 / 3 for idx, commitSig := range commit.Signatures { if commitSig.Absent() { continue // OK, some signatures can be absent. @@ -658,13 +659,15 @@ func (vals *ValidatorSet) VerifyCommit(chainID string, blockID BlockID, // It's OK that the BlockID doesn't match. We include stray // signatures (~votes for nil) to measure validator availability. // } - } - if got, needed := talliedVotingPower, vals.TotalVotingPower()*2/3; got <= needed { - return ErrNotEnoughVotingPowerSigned{Got: got, Needed: needed} + // return as soon as +2/3 of the signatures are verified + if talliedVotingPower > votingPowerNeeded { + return nil + } } - return nil + // talliedVotingPower <= needed, thus return error + return ErrNotEnoughVotingPowerSigned{Got: talliedVotingPower, Needed: votingPowerNeeded} } // VerifyFutureCommit will check to see if the set would be valid with a different @@ -762,6 +765,7 @@ func (vals *ValidatorSet) VerifyCommitTrusting(chainID string, blockID BlockID, var ( talliedVotingPower int64 seenVals = make(map[int]int, len(commit.Signatures)) // validator index -> commit index + votingPowerNeeded = (vals.TotalVotingPower() * trustLevel.Numerator) / trustLevel.Denominator ) for idx, commitSig := range commit.Signatures { @@ -795,16 +799,14 @@ func (vals *ValidatorSet) VerifyCommitTrusting(chainID string, blockID BlockID, // It's OK that the BlockID doesn't match. We include stray // signatures (~votes for nil) to measure validator availability. // } - } - } - got := talliedVotingPower - needed := (vals.TotalVotingPower() * trustLevel.Numerator) / trustLevel.Denominator - if got <= needed { - return ErrNotEnoughVotingPowerSigned{Got: got, Needed: needed} + if talliedVotingPower > votingPowerNeeded { + return nil + } + } } - return nil + return ErrNotEnoughVotingPowerSigned{Got: talliedVotingPower, Needed: votingPowerNeeded} } func verifyCommitBasic(commit *Commit, height int64, blockID BlockID) error {