From 8b50ff60d94e6572e768e60ce734f04831058091 Mon Sep 17 00:00:00 2001 From: Joe Bowman Date: Fri, 29 May 2020 07:15:46 +0100 Subject: [PATCH] only retrieve pubkey once for all validators (partially fixes #4865) (#4895) ## Description in consensus/state.go, when calulating metrics, retrieve address (ergo, pubkey) once prior to iterating over validatorset to ensure we do not make excessive calls to signer. Partially closes: #4865 --- CHANGELOG_PENDING.md | 1 + consensus/state.go | 37 ++++++++++++++++++++----------------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index 805b10ce2..ccd4394c3 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -87,3 +87,4 @@ Friendly reminder, we have a [bug bounty program](https://hackerone.com/tendermi - [types] [\#4764](https://github.com/tendermint/tendermint/pull/4764) Return an error if voting power overflows in `VerifyCommitTrusting` (@melekes) - [privval] [\#4812](https://github.com/tendermint/tendermint/pull/4812) Retry `GetPubKey/SignVote/SignProposal` a few times before returning an error (@melekes) - [p2p] [\#4847](https://github.com/tendermint/tendermint/pull/4847) Return masked IP (not the actual IP) in addrbook#groupKey (@melekes) +- [consensus] [\#4895](https://github.com/tendermint/tendermint/pull/4895) Cache the address of the validator to reduce querying a remote KMS (@joe-bowman) diff --git a/consensus/state.go b/consensus/state.go index 5524eb268..fea1eb54e 100644 --- a/consensus/state.go +++ b/consensus/state.go @@ -1552,12 +1552,23 @@ func (cs *State) recordMetrics(height int64, block *types.Block) { var ( commitSize = block.LastCommit.Size() valSetLen = len(cs.LastValidators.Validators) + address types.Address ) if commitSize != valSetLen { panic(fmt.Sprintf("commit size (%d) doesn't match valset length (%d) at height %d\n\n%v\n\n%v", commitSize, valSetLen, block.Height, block.LastCommit.Signatures, cs.LastValidators.Validators)) } + if cs.privValidator != nil { + pubkey, err := cs.privValidator.GetPubKey() + if err != nil { + // Metrics won't be updated, but it's not critical. + cs.Logger.Error("Error on retrieval of pubkey", "err", err) + } else { + address = pubkey.Address() + } + } + for i, val := range cs.LastValidators.Validators { commitSig := block.LastCommit.Signatures[i] if commitSig.Absent() { @@ -1565,26 +1576,18 @@ func (cs *State) recordMetrics(height int64, block *types.Block) { missingValidatorsPower += val.VotingPower } - if cs.privValidator != nil { - pubKey, err := cs.privValidator.GetPubKey() - if err != nil { - // Metrics won't be updated, but it's not critical. - cs.Logger.Error("Error on retrival of pubkey", "err", err) - continue + if bytes.Equal(val.Address, address) { + label := []string{ + "validator_address", val.Address.String(), } - - if bytes.Equal(val.Address, pubKey.Address()) { - label := []string{ - "validator_address", val.Address.String(), - } - cs.metrics.ValidatorPower.With(label...).Set(float64(val.VotingPower)) - if commitSig.ForBlock() { - cs.metrics.ValidatorLastSignedHeight.With(label...).Set(float64(height)) - } else { - cs.metrics.ValidatorMissedBlocks.With(label...).Add(float64(1)) - } + cs.metrics.ValidatorPower.With(label...).Set(float64(val.VotingPower)) + if commitSig.ForBlock() { + cs.metrics.ValidatorLastSignedHeight.With(label...).Set(float64(height)) + } else { + cs.metrics.ValidatorMissedBlocks.With(label...).Add(float64(1)) } } + } } cs.metrics.MissingValidators.Set(float64(missingValidators))