change use of errors.Wrap to fmt.Errorf with %w verb

Closes #4603

Commands used (VIM):

```
:args `rg -l errors.Wrap`
:argdo normal @q | update
```

where q is a macros rewriting the `errors.Wrap` to `fmt.Errorf`.
This commit is contained in:
Anton Kaliaev
2020-05-12 07:35:47 +04:00
committed by GitHub
parent 8d63d7192f
commit b7b721c484
90 changed files with 321 additions and 391 deletions

View File

@@ -2,13 +2,12 @@ package types
import (
"bytes"
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/pkg/errors"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/merkle"
"github.com/tendermint/tendermint/crypto/tmhash"

View File

@@ -2,8 +2,7 @@ package types
import (
"bytes"
"github.com/pkg/errors"
"fmt"
)
// BlockMeta contains meta information.
@@ -58,7 +57,7 @@ func (bm *BlockMeta) ValidateBasic() error {
return err
}
if !bytes.Equal(bm.BlockID.Hash, bm.Header.Hash()) {
return errors.Errorf("expected BlockID#Hash and Header#Hash to be the same, got %X != %X",
return fmt.Errorf("expected BlockID#Hash and Header#Hash to be the same, got %X != %X",
bm.BlockID.Hash, bm.Header.Hash())
}
return nil

View File

@@ -2,12 +2,11 @@ package types
import (
"bytes"
"errors"
"fmt"
"strings"
"time"
"github.com/pkg/errors"
"github.com/tendermint/tendermint/crypto/tmhash"
tmmath "github.com/tendermint/tendermint/libs/math"
@@ -485,7 +484,7 @@ func (ev ConflictingHeadersEvidence) VerifyComposite(committedHeader *Header, va
// Max validator set size = 100 * 2 = 200 [fork?]
maxNumValidators := valSet.Size() * 2
if len(alternativeHeader.Commit.Signatures) > maxNumValidators {
return errors.Errorf("alt commit contains too many signatures: %d, expected no more than %d",
return fmt.Errorf("alt commit contains too many signatures: %d, expected no more than %d",
len(alternativeHeader.Commit.Signatures),
maxNumValidators)
}
@@ -496,7 +495,7 @@ func (ev ConflictingHeadersEvidence) VerifyComposite(committedHeader *Header, va
alternativeHeader.ChainID,
alternativeHeader.Commit,
tmmath.Fraction{Numerator: 1, Denominator: 3}); err != nil {
return errors.Wrap(err, "alt header does not have 1/3+ of voting power of our validator set")
return fmt.Errorf("alt header does not have 1/3+ of voting power of our validator set: %w", err)
}
return nil

View File

@@ -3,12 +3,11 @@ package types
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"time"
"github.com/pkg/errors"
"github.com/tendermint/tendermint/crypto"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
tmos "github.com/tendermint/tendermint/libs/os"
@@ -70,7 +69,7 @@ func (genDoc *GenesisDoc) ValidateAndComplete() error {
return errors.New("genesis doc must include non-empty chain_id")
}
if len(genDoc.ChainID) > MaxChainIDLen {
return errors.Errorf("chain_id in genesis doc is too long (max: %d)", MaxChainIDLen)
return fmt.Errorf("chain_id in genesis doc is too long (max: %d)", MaxChainIDLen)
}
if genDoc.ConsensusParams == nil {
@@ -81,10 +80,10 @@ func (genDoc *GenesisDoc) ValidateAndComplete() error {
for i, v := range genDoc.Validators {
if v.Power == 0 {
return errors.Errorf("the genesis file cannot contain validators with no voting power: %v", v)
return fmt.Errorf("the genesis file cannot contain validators with no voting power: %v", v)
}
if len(v.Address) > 0 && !bytes.Equal(v.PubKey.Address(), v.Address) {
return errors.Errorf("incorrect address for validator %v in the genesis file, should be %v", v, v.PubKey.Address())
return fmt.Errorf("incorrect address for validator %v in the genesis file, should be %v", v, v.PubKey.Address())
}
if len(v.Address) == 0 {
genDoc.Validators[i].Address = v.PubKey.Address()
@@ -120,11 +119,11 @@ func GenesisDocFromJSON(jsonBlob []byte) (*GenesisDoc, error) {
func GenesisDocFromFile(genDocFile string) (*GenesisDoc, error) {
jsonBlob, err := ioutil.ReadFile(genDocFile)
if err != nil {
return nil, errors.Wrap(err, "Couldn't read GenesisDoc file")
return nil, fmt.Errorf("couldn't read GenesisDoc file: %w", err)
}
genDoc, err := GenesisDocFromJSON(jsonBlob)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("Error reading GenesisDoc at %v", genDocFile))
return nil, fmt.Errorf("error reading GenesisDoc at %s: %w", genDocFile, err)
}
return genDoc, nil
}

View File

@@ -1,10 +1,10 @@
package types
import (
"errors"
"fmt"
"time"
"github.com/pkg/errors"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/tmhash"
tmstrings "github.com/tendermint/tendermint/libs/strings"
@@ -130,41 +130,41 @@ func (params *ValidatorParams) IsValidPubkeyType(pubkeyType string) bool {
// allowed limits, and returns an error if they are not.
func (params *ConsensusParams) Validate() error {
if params.Block.MaxBytes <= 0 {
return errors.Errorf("block.MaxBytes must be greater than 0. Got %d",
return fmt.Errorf("block.MaxBytes must be greater than 0. Got %d",
params.Block.MaxBytes)
}
if params.Block.MaxBytes > MaxBlockSizeBytes {
return errors.Errorf("block.MaxBytes is too big. %d > %d",
return fmt.Errorf("block.MaxBytes is too big. %d > %d",
params.Block.MaxBytes, MaxBlockSizeBytes)
}
if params.Block.MaxGas < -1 {
return errors.Errorf("block.MaxGas must be greater or equal to -1. Got %d",
return fmt.Errorf("block.MaxGas must be greater or equal to -1. Got %d",
params.Block.MaxGas)
}
if params.Block.TimeIotaMs <= 0 {
return errors.Errorf("block.TimeIotaMs must be greater than 0. Got %v",
return fmt.Errorf("block.TimeIotaMs must be greater than 0. Got %v",
params.Block.TimeIotaMs)
}
if params.Evidence.MaxAgeNumBlocks <= 0 {
return errors.Errorf("evidenceParams.MaxAgeNumBlocks must be greater than 0. Got %d",
return fmt.Errorf("evidenceParams.MaxAgeNumBlocks must be greater than 0. Got %d",
params.Evidence.MaxAgeNumBlocks)
}
if params.Evidence.MaxAgeDuration <= 0 {
return errors.Errorf("evidenceParams.MaxAgeDuration must be grater than 0 if provided, Got %v",
return fmt.Errorf("evidenceParams.MaxAgeDuration must be grater than 0 if provided, Got %v",
params.Evidence.MaxAgeDuration)
}
if params.Evidence.MaxNum > MaxEvidencePerBlock {
return errors.Errorf("evidenceParams.MaxNumEvidence is greater than upper bound, %d > %d",
return fmt.Errorf("evidenceParams.MaxNumEvidence is greater than upper bound, %d > %d",
params.Evidence.MaxNum, MaxEvidencePerBlock)
}
if int64(params.Evidence.MaxNum)*MaxEvidenceBytes > params.Block.MaxBytes {
return errors.Errorf("total possible evidence size is bigger than block.MaxBytes, %d > %d",
return fmt.Errorf("total possible evidence size is bigger than block.MaxBytes, %d > %d",
int64(params.Evidence.MaxNum)*MaxEvidenceBytes, params.Block.MaxBytes)
}
@@ -176,7 +176,7 @@ func (params *ConsensusParams) Validate() error {
for i := 0; i < len(params.Validator.PubKeyTypes); i++ {
keyType := params.Validator.PubKeyTypes[i]
if _, ok := ABCIPubKeyTypesToAminoNames[keyType]; !ok {
return errors.Errorf("params.Validator.PubKeyTypes[%d], %s, is an unknown pubkey type",
return fmt.Errorf("params.Validator.PubKeyTypes[%d], %s, is an unknown pubkey type",
i, keyType)
}
}

View File

@@ -2,12 +2,11 @@ package types
import (
"bytes"
"errors"
"fmt"
"io"
"sync"
"github.com/pkg/errors"
"github.com/tendermint/tendermint/crypto/merkle"
"github.com/tendermint/tendermint/libs/bits"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
@@ -31,10 +30,10 @@ func (part *Part) ValidateBasic() error {
return errors.New("negative Index")
}
if len(part.Bytes) > BlockPartSizeBytes {
return errors.Errorf("too big: %d bytes, max: %d", len(part.Bytes), BlockPartSizeBytes)
return fmt.Errorf("too big: %d bytes, max: %d", len(part.Bytes), BlockPartSizeBytes)
}
if err := part.Proof.ValidateBasic(); err != nil {
return errors.Wrap(err, "wrong Proof")
return fmt.Errorf("wrong Proof: %w", err)
}
return nil
}
@@ -80,7 +79,7 @@ func (psh PartSetHeader) ValidateBasic() error {
}
// Hash can be empty in case of POLBlockID.PartsHeader in Proposal.
if err := ValidateHash(psh.Hash); err != nil {
return errors.Wrap(err, "Wrong Hash")
return fmt.Errorf("wrong Hash: %w", err)
}
return nil
}

View File

@@ -1,9 +1,8 @@
package types
import (
"fmt"
"time"
"github.com/pkg/errors"
)
func MakeCommit(blockID BlockID, height int64, round int,
@@ -13,7 +12,7 @@ func MakeCommit(blockID BlockID, height int64, round int,
for i := 0; i < len(validators); i++ {
pubKey, err := validators[i].GetPubKey()
if err != nil {
return nil, errors.Wrap(err, "can't get pubkey")
return nil, fmt.Errorf("can't get pubkey: %w", err)
}
vote := &Vote{
ValidatorAddress: pubKey.Address(),
@@ -52,7 +51,7 @@ func MakeVote(
) (*Vote, error) {
pubKey, err := privVal.GetPubKey()
if err != nil {
return nil, errors.Wrap(err, "can't get pubkey")
return nil, fmt.Errorf("can't get pubkey: %w", err)
}
addr := pubKey.Address()
idx, _ := valSet.GetByAddress(addr)

View File

@@ -2,14 +2,13 @@ package types
import (
"bytes"
"errors"
"fmt"
"math"
"math/big"
"sort"
"strings"
"github.com/pkg/errors"
"github.com/tendermint/tendermint/crypto/merkle"
tmmath "github.com/tendermint/tendermint/libs/math"
)
@@ -751,7 +750,7 @@ func (vals *ValidatorSet) VerifyFutureCommit(newSet *ValidatorSet, chainID strin
// Validate signature.
voteSignBytes := commit.VoteSignBytes(chainID, idx)
if !val.PubKey.VerifyBytes(voteSignBytes, commitSig.Signature) {
return errors.Errorf("wrong signature (#%d): %X", idx, commitSig.Signature)
return fmt.Errorf("wrong signature (#%d): %X", idx, commitSig.Signature)
}
// Good!
if blockID.Equals(commitSig.BlockID(commit.BlockID)) {
@@ -805,14 +804,14 @@ func (vals *ValidatorSet) VerifyCommitTrusting(chainID string, commit *Commit, t
// check for double vote of validator on the same commit
if firstIndex, ok := seenVals[valIdx]; ok {
secondIndex := idx
return errors.Errorf("double vote from %v (%d and %d)", val, firstIndex, secondIndex)
return fmt.Errorf("double vote from %v (%d and %d)", val, firstIndex, secondIndex)
}
seenVals[valIdx] = idx
// Validate signature.
voteSignBytes := commit.VoteSignBytes(chainID, idx)
if !val.PubKey.VerifyBytes(voteSignBytes, commitSig.Signature) {
return errors.Errorf("wrong signature (#%d): %X", idx, commitSig.Signature)
return fmt.Errorf("wrong signature (#%d): %X", idx, commitSig.Signature)
}
// Good!
@@ -838,8 +837,7 @@ func (vals *ValidatorSet) VerifyCommitTrusting(chainID string, commit *Commit, t
// IsErrNotEnoughVotingPowerSigned returns true if err is
// ErrNotEnoughVotingPowerSigned.
func IsErrNotEnoughVotingPowerSigned(err error) bool {
_, ok := errors.Cause(err).(ErrNotEnoughVotingPowerSigned)
return ok
return errors.As(err, &ErrNotEnoughVotingPowerSigned{})
}
// ErrNotEnoughVotingPowerSigned is returned when not enough validators signed

View File

@@ -6,8 +6,6 @@ import (
"strings"
"sync"
"github.com/pkg/errors"
"github.com/tendermint/tendermint/libs/bits"
)
@@ -160,33 +158,34 @@ func (voteSet *VoteSet) addVote(vote *Vote) (added bool, err error) {
// Ensure that validator index was set
if valIndex < 0 {
return false, errors.Wrap(ErrVoteInvalidValidatorIndex, "Index < 0")
return false, fmt.Errorf("index < 0: %w", ErrVoteInvalidValidatorIndex)
} else if len(valAddr) == 0 {
return false, errors.Wrap(ErrVoteInvalidValidatorAddress, "Empty address")
return false, fmt.Errorf("empty address: %w", ErrVoteInvalidValidatorAddress)
}
// Make sure the step matches.
if (vote.Height != voteSet.height) ||
(vote.Round != voteSet.round) ||
(vote.Type != voteSet.signedMsgType) {
return false, errors.Wrapf(ErrVoteUnexpectedStep, "Expected %d/%d/%d, but got %d/%d/%d",
return false, fmt.Errorf("expected %d/%d/%d, but got %d/%d/%d: %w",
voteSet.height, voteSet.round, voteSet.signedMsgType,
vote.Height, vote.Round, vote.Type)
vote.Height, vote.Round, vote.Type, ErrVoteUnexpectedStep)
}
// Ensure that signer is a validator.
lookupAddr, val := voteSet.valSet.GetByIndex(valIndex)
if val == nil {
return false, errors.Wrapf(ErrVoteInvalidValidatorIndex,
"Cannot find validator %d in valSet of size %d", valIndex, voteSet.valSet.Size())
return false, fmt.Errorf(
"cannot find validator %d in valSet of size %d: %w",
valIndex, voteSet.valSet.Size(), ErrVoteInvalidValidatorIndex)
}
// Ensure that the signer has the right address.
if !bytes.Equal(valAddr, lookupAddr) {
return false, errors.Wrapf(ErrVoteInvalidValidatorAddress,
return false, fmt.Errorf(
"vote.ValidatorAddress (%X) does not match address (%X) for vote.ValidatorIndex (%d)\n"+
"Ensure the genesis file is correct across all validators.",
valAddr, lookupAddr, valIndex)
"Ensure the genesis file is correct across all validators: %w",
valAddr, lookupAddr, valIndex, ErrVoteInvalidValidatorAddress)
}
// If we already know of this vote, return false.
@@ -194,12 +193,12 @@ func (voteSet *VoteSet) addVote(vote *Vote) (added bool, err error) {
if bytes.Equal(existing.Signature, vote.Signature) {
return false, nil // duplicate
}
return false, errors.Wrapf(ErrVoteNonDeterministicSignature, "Existing vote: %v; New vote: %v", existing, vote)
return false, fmt.Errorf("existing vote: %v; new vote: %v: %w", existing, vote, ErrVoteNonDeterministicSignature)
}
// Check signature.
if err := vote.Verify(voteSet.chainID, val.PubKey); err != nil {
return false, errors.Wrapf(err, "Failed to verify vote with ChainID %s and PubKey %s", voteSet.chainID, val.PubKey)
return false, fmt.Errorf("failed to verify vote with ChainID %s and PubKey %s: %w", voteSet.chainID, val.PubKey, err)
}
// Add vote and get conflicting vote if any.