Skip to content

Commit

Permalink
chore: use cosmossdk.io/errors (#1448)
Browse files Browse the repository at this point in the history
Closes #1446

Before: `make lint` showed warnings
After: `make lint` shows no more warnings

```shell
$ make lint
--> Running golangci-lint
--> Running markdownlint
```
  • Loading branch information
rootulp committed Mar 7, 2023
1 parent 909a566 commit e5e37ec
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 75 deletions.
3 changes: 2 additions & 1 deletion app/verify_txs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"runtime/debug"

"cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -33,7 +34,7 @@ func incrementSequenceAnteHandler(accKeeper *authkeeper.AccountKeeper) sdk.AnteH
// recoverHandler will simply wrap the caught panic in an error containing the
// stack trace.
func recoverHandler(recoveryObj interface{}) error {
return sdkerrors.Wrap(
return errors.Wrap(
sdkerrors.ErrPanic, fmt.Sprintf(
"recovered: %v\nstack:\n%v", recoveryObj, string(debug.Stack()),
),
Expand Down
3 changes: 2 additions & 1 deletion x/blob/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package blob
import (
"fmt"

"cosmossdk.io/errors"
"github.com/celestiaorg/celestia-app/x/blob/keeper"
"github.com/celestiaorg/celestia-app/x/blob/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -21,7 +22,7 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
return sdk.WrapServiceResult(ctx, res, err)
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
return nil, errors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
}
}
}
54 changes: 27 additions & 27 deletions x/blob/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@ package types
// DONTCOVER

import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"cosmossdk.io/errors"
)

var (
ErrReservedNamespace = sdkerrors.Register(ModuleName, 11110, "cannot use reserved namespace IDs")
ErrInvalidNamespaceLen = sdkerrors.Register(ModuleName, 11111, "invalid namespace length")
ErrInvalidDataSize = sdkerrors.Register(ModuleName, 11112, "data must be multiple of shareSize")
ErrBlobSizeMismatch = sdkerrors.Register(ModuleName, 11113, "actual blob size differs from that specified in the MsgPayForBlob")
ErrCommittedSquareSizeNotPowOf2 = sdkerrors.Register(ModuleName, 11114, "committed to invalid square size: must be power of two")
ErrCalculateCommitment = sdkerrors.Register(ModuleName, 11115, "unexpected error calculating commitment for share")
ErrInvalidShareCommitment = sdkerrors.Register(ModuleName, 11116, "invalid commitment for share")
ErrParitySharesNamespace = sdkerrors.Register(ModuleName, 11117, "cannot use parity shares namespace ID")
ErrTailPaddingNamespace = sdkerrors.Register(ModuleName, 11118, "cannot use tail padding namespace ID")
ErrTxNamespace = sdkerrors.Register(ModuleName, 11119, "cannot use transaction namespace ID")
ErrEvidenceNamespace = sdkerrors.Register(ModuleName, 11120, "cannot use evidence namespace ID")
ErrEmptyShareCommitment = sdkerrors.Register(ModuleName, 11121, "empty share commitment")
ErrInvalidShareCommitments = sdkerrors.Register(ModuleName, 11122, "invalid share commitments: all relevant square sizes must be committed to")
ErrUnsupportedShareVersion = sdkerrors.Register(ModuleName, 11123, "unsupported share version")
ErrZeroBlobSize = sdkerrors.Register(ModuleName, 11124, "cannot use zero blob size")
ErrMismatchedNumberOfPFBorBlob = sdkerrors.Register(ModuleName, 11125, "mismatched number of blobs per MsgPayForBlob")
ErrNoPFB = sdkerrors.Register(ModuleName, 11126, "no MsgPayForBlobs found in blob transaction")
ErrNamespaceMismatch = sdkerrors.Register(ModuleName, 11127, "namespace of blob and its respective MsgPayForBlobs differ")
ErrProtoParsing = sdkerrors.Register(ModuleName, 11128, "failure to parse a transaction from its protobuf representation")
ErrMultipleMsgsInBlobTx = sdkerrors.Register(ModuleName, 11129, "not yet supported: multiple sdk.Msgs found in BlobTx")
ErrMismatchedNumberOfPFBComponent = sdkerrors.Register(ModuleName, 11130, "number of each component in a MsgPayForBlobs must be identical")
ErrNoBlobs = sdkerrors.Register(ModuleName, 11131, "no blobs provided")
ErrNoNamespaceIds = sdkerrors.Register(ModuleName, 11132, "no namespace IDs provided")
ErrNoShareVersions = sdkerrors.Register(ModuleName, 11133, "no share versions provided")
ErrNoBlobSizes = sdkerrors.Register(ModuleName, 11134, "no blob sizes provided")
ErrNoShareCommitments = sdkerrors.Register(ModuleName, 11135, "no share commitments provided")
ErrReservedNamespace = errors.Register(ModuleName, 11110, "cannot use reserved namespace IDs")
ErrInvalidNamespaceLen = errors.Register(ModuleName, 11111, "invalid namespace length")
ErrInvalidDataSize = errors.Register(ModuleName, 11112, "data must be multiple of shareSize")
ErrBlobSizeMismatch = errors.Register(ModuleName, 11113, "actual blob size differs from that specified in the MsgPayForBlob")
ErrCommittedSquareSizeNotPowOf2 = errors.Register(ModuleName, 11114, "committed to invalid square size: must be power of two")
ErrCalculateCommitment = errors.Register(ModuleName, 11115, "unexpected error calculating commitment for share")
ErrInvalidShareCommitment = errors.Register(ModuleName, 11116, "invalid commitment for share")
ErrParitySharesNamespace = errors.Register(ModuleName, 11117, "cannot use parity shares namespace ID")
ErrTailPaddingNamespace = errors.Register(ModuleName, 11118, "cannot use tail padding namespace ID")
ErrTxNamespace = errors.Register(ModuleName, 11119, "cannot use transaction namespace ID")
ErrEvidenceNamespace = errors.Register(ModuleName, 11120, "cannot use evidence namespace ID")
ErrEmptyShareCommitment = errors.Register(ModuleName, 11121, "empty share commitment")
ErrInvalidShareCommitments = errors.Register(ModuleName, 11122, "invalid share commitments: all relevant square sizes must be committed to")
ErrUnsupportedShareVersion = errors.Register(ModuleName, 11123, "unsupported share version")
ErrZeroBlobSize = errors.Register(ModuleName, 11124, "cannot use zero blob size")
ErrMismatchedNumberOfPFBorBlob = errors.Register(ModuleName, 11125, "mismatched number of blobs per MsgPayForBlob")
ErrNoPFB = errors.Register(ModuleName, 11126, "no MsgPayForBlobs found in blob transaction")
ErrNamespaceMismatch = errors.Register(ModuleName, 11127, "namespace of blob and its respective MsgPayForBlobs differ")
ErrProtoParsing = errors.Register(ModuleName, 11128, "failure to parse a transaction from its protobuf representation")
ErrMultipleMsgsInBlobTx = errors.Register(ModuleName, 11129, "not yet supported: multiple sdk.Msgs found in BlobTx")
ErrMismatchedNumberOfPFBComponent = errors.Register(ModuleName, 11130, "number of each component in a MsgPayForBlobs must be identical")
ErrNoBlobs = errors.Register(ModuleName, 11131, "no blobs provided")
ErrNoNamespaceIds = errors.Register(ModuleName, 11132, "no namespace IDs provided")
ErrNoShareVersions = errors.Register(ModuleName, 11133, "no share versions provided")
ErrNoBlobSizes = errors.Register(ModuleName, 11134, "no blob sizes provided")
ErrNoShareCommitments = errors.Register(ModuleName, 11135, "no share commitments provided")
)
2 changes: 1 addition & 1 deletion x/qgb/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package qgb
import (
"errors"

sdkerrors "cosmossdk.io/errors"
"github.com/celestiaorg/celestia-app/x/qgb/keeper"
"github.com/celestiaorg/celestia-app/x/qgb/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// SignificantPowerDifferenceThreshold the threshold of change in the validator set power
Expand Down
3 changes: 2 additions & 1 deletion x/qgb/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package qgb
import (
"fmt"

"cosmossdk.io/errors"
"github.com/celestiaorg/celestia-app/x/qgb/keeper"
"github.com/celestiaorg/celestia-app/x/qgb/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -17,7 +18,7 @@ func NewHandler(_ keeper.Keeper) sdk.Handler {
switch msg := msg.(type) {
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
return nil, errors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
}
}
}
10 changes: 5 additions & 5 deletions x/qgb/keeper/keeper_data_commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package keeper
import (
"fmt"

"cosmossdk.io/errors"
"github.com/celestiaorg/celestia-app/x/qgb/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// TODO add unit tests for all the keepers
Expand Down Expand Up @@ -38,7 +38,7 @@ func (k Keeper) GetDataCommitmentForHeight(ctx sdk.Context, height uint64) (type
return types.DataCommitment{}, err
}
if lastDC.EndBlock < height {
return types.DataCommitment{}, sdkerrors.Wrap(
return types.DataCommitment{}, errors.Wrap(
types.ErrDataCommitmentNotGenerated,
fmt.Sprintf(
"Last height %d < %d",
Expand All @@ -55,7 +55,7 @@ func (k Keeper) GetDataCommitmentForHeight(ctx sdk.Context, height uint64) (type
return types.DataCommitment{}, err
}
if !found {
return types.DataCommitment{}, sdkerrors.Wrap(types.ErrAttestationNotFound, fmt.Sprintf("nonce %d", i))
return types.DataCommitment{}, errors.Wrap(types.ErrAttestationNotFound, fmt.Sprintf("nonce %d", i))
}
dcc, ok := att.(*types.DataCommitment)
if !ok {
Expand All @@ -65,7 +65,7 @@ func (k Keeper) GetDataCommitmentForHeight(ctx sdk.Context, height uint64) (type
return *dcc, nil
}
}
return types.DataCommitment{}, sdkerrors.Wrap(types.ErrDataCommitmentNotFound, "data commitment for height not found")
return types.DataCommitment{}, errors.Wrap(types.ErrDataCommitmentNotFound, "data commitment for height not found")
}

// GetLastDataCommitment returns the last data commitment.
Expand All @@ -77,7 +77,7 @@ func (k Keeper) GetLastDataCommitment(ctx sdk.Context) (types.DataCommitment, er
return types.DataCommitment{}, err
}
if !found {
return types.DataCommitment{}, sdkerrors.Wrapf(types.ErrAttestationNotFound, fmt.Sprintf("nonce %d", latestNonce-i))
return types.DataCommitment{}, errors.Wrapf(types.ErrAttestationNotFound, fmt.Sprintf("nonce %d", latestNonce-i))
}
dcc, ok := att.(*types.DataCommitment)
if !ok {
Expand Down
24 changes: 13 additions & 11 deletions x/qgb/keeper/keeper_valset.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (

cosmosmath "cosmossdk.io/math"
"github.com/celestiaorg/celestia-app/x/qgb/types"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -25,21 +27,21 @@ func (k Keeper) GetLatestValset(ctx sdk.Context) (*types.Valset, error) {
return nil, err
}
if !found {
panic(sdkerrors.Wrap(
panic(errors.Wrap(
types.ErrNilAttestation,
fmt.Sprintf("stumbled upon nil attestation for nonce %d", i),
))
}
if at.Type() == types.ValsetRequestType {
valset, ok := at.(*types.Valset)
if !ok {
return nil, sdkerrors.Wrap(types.ErrAttestationNotValsetRequest, "couldn't cast attestation to valset")
return nil, errors.Wrap(types.ErrAttestationNotValsetRequest, "couldn't cast attestation to valset")
}
return valset, nil
}
}
// should never execute
panic(sdkerrors.Wrap(sdkerrors.ErrNotFound, "couldn't find latest valset"))
panic(errors.Wrap(sdkerrors.ErrNotFound, "couldn't find latest valset"))
}

// SetLastUnBondingBlockHeight sets the last unbonding block height. Note this
Expand Down Expand Up @@ -78,7 +80,7 @@ func (k Keeper) GetCurrentValset(ctx sdk.Context) (types.Valset, error) {
for _, validator := range validators {
val := validator.GetOperator()
if err := sdk.VerifyAddressFormat(val); err != nil {
return types.Valset{}, sdkerrors.Wrap(err, types.ErrInvalidValAddress.Error())
return types.Valset{}, errors.Wrap(err, types.ErrInvalidValAddress.Error())
}

p := sdk.NewInt(k.StakingKeeper.GetLastValidatorPower(ctx, val))
Expand All @@ -87,7 +89,7 @@ func (k Keeper) GetCurrentValset(ctx sdk.Context) (types.Valset, error) {
bv := types.BridgeValidator{Power: p.Uint64(), EvmAddress: validator.EvmAddress}
ibv, err := types.NewInternalBridgeValidator(bv)
if err != nil {
return types.Valset{}, sdkerrors.Wrapf(err, types.ErrInvalidEVMAddress.Error(), val)
return types.Valset{}, errors.Wrapf(err, types.ErrInvalidEVMAddress.Error(), val)
}
bridgeValidators = append(bridgeValidators, ibv)
totalPower = totalPower.Add(p)
Expand All @@ -102,7 +104,7 @@ func (k Keeper) GetCurrentValset(ctx sdk.Context) (types.Valset, error) {

valset, err := types.NewValset(valsetNonce, uint64(ctx.BlockHeight()), bridgeValidators)
if err != nil {
return types.Valset{}, (sdkerrors.Wrap(err, types.ErrInvalidValset.Error()))
return types.Valset{}, (errors.Wrap(err, types.ErrInvalidValset.Error()))
}
return *valset, nil
}
Expand Down Expand Up @@ -146,20 +148,20 @@ func (k Keeper) GetLastValsetBeforeNonce(ctx sdk.Context, nonce uint64) (*types.
return nil, err
}
if !found {
return nil, sdkerrors.Wrap(
return nil, errors.Wrap(
types.ErrNilAttestation,
fmt.Sprintf("nonce=%d", nonce-i),
)
}
if at.Type() == types.ValsetRequestType {
valset, ok := at.(*types.Valset)
if !ok {
return nil, sdkerrors.Wrap(types.ErrAttestationNotValsetRequest, "couldn't cast attestation to valset")
return nil, errors.Wrap(types.ErrAttestationNotValsetRequest, "couldn't cast attestation to valset")
}
return valset, nil
}
}
return nil, sdkerrors.Wrap(
return nil, errors.Wrap(
sdkerrors.ErrNotFound,
fmt.Sprintf("couldn't find valset before nonce %d", nonce),
)
Expand All @@ -177,12 +179,12 @@ func (k Keeper) GetValsetByNonce(ctx sdk.Context, nonce uint64) (*types.Valset,
return nil, false, nil
}
if at.Type() != types.ValsetRequestType {
return nil, false, sdkerrors.Wrap(types.ErrAttestationNotValsetRequest, "attestation is not a valset request")
return nil, false, errors.Wrap(types.ErrAttestationNotValsetRequest, "attestation is not a valset request")
}

valset, ok := at.(*types.Valset)
if !ok {
return nil, false, sdkerrors.Wrap(types.ErrAttestationNotValsetRequest, "couldn't cast attestation to valset")
return nil, false, errors.Wrap(types.ErrAttestationNotValsetRequest, "couldn't cast attestation to valset")
}
return valset, true, nil
}
32 changes: 17 additions & 15 deletions x/qgb/types/errors.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package types

import sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
import (
"cosmossdk.io/errors"
)

var (
ErrDuplicate = sdkerrors.Register(ModuleName, 2, "duplicate")
ErrEmpty = sdkerrors.Register(ModuleName, 6, "empty")
ErrNoValidators = sdkerrors.Register(ModuleName, 12, "no bonded validators in active set")
ErrInvalidValAddress = sdkerrors.Register(ModuleName, 13, "invalid validator address in current valset %v")
ErrInvalidEVMAddress = sdkerrors.Register(ModuleName, 14, "discovered invalid EVM address stored for validator %v")
ErrInvalidValset = sdkerrors.Register(ModuleName, 15, "generated invalid valset")
ErrAttestationNotValsetRequest = sdkerrors.Register(ModuleName, 16, "attestation is not a valset request")
ErrAttestationNotFound = sdkerrors.Register(ModuleName, 18, "attestation not found")
ErrNilAttestation = sdkerrors.Register(ModuleName, 22, "nil attestation")
ErrUnmarshalllAttestation = sdkerrors.Register(ModuleName, 26, "couldn't unmarshall attestation from store")
ErrNonceHigherThanLatestAttestationNonce = sdkerrors.Register(ModuleName, 27, "the provided nonce is higher than the latest attestation nonce")
ErrNoValsetBeforeNonceOne = sdkerrors.Register(ModuleName, 28, "there is no valset before attestation nonce 1")
ErrDataCommitmentNotGenerated = sdkerrors.Register(ModuleName, 29, "no data commitment has been generated for the provided height")
ErrDataCommitmentNotFound = sdkerrors.Register(ModuleName, 30, "data commitment not found")
ErrDuplicate = errors.Register(ModuleName, 2, "duplicate")
ErrEmpty = errors.Register(ModuleName, 6, "empty")
ErrNoValidators = errors.Register(ModuleName, 12, "no bonded validators in active set")
ErrInvalidValAddress = errors.Register(ModuleName, 13, "invalid validator address in current valset %v")
ErrInvalidEVMAddress = errors.Register(ModuleName, 14, "discovered invalid EVM address stored for validator %v")
ErrInvalidValset = errors.Register(ModuleName, 15, "generated invalid valset")
ErrAttestationNotValsetRequest = errors.Register(ModuleName, 16, "attestation is not a valset request")
ErrAttestationNotFound = errors.Register(ModuleName, 18, "attestation not found")
ErrNilAttestation = errors.Register(ModuleName, 22, "nil attestation")
ErrUnmarshalllAttestation = errors.Register(ModuleName, 26, "couldn't unmarshall attestation from store")
ErrNonceHigherThanLatestAttestationNonce = errors.Register(ModuleName, 27, "the provided nonce is higher than the latest attestation nonce")
ErrNoValsetBeforeNonceOne = errors.Register(ModuleName, 28, "there is no valset before attestation nonce 1")
ErrDataCommitmentNotGenerated = errors.Register(ModuleName, 29, "no data commitment has been generated for the provided height")
ErrDataCommitmentNotFound = errors.Register(ModuleName, 30, "data commitment not found")
)
7 changes: 3 additions & 4 deletions x/qgb/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package types
import (
"fmt"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"cosmossdk.io/errors"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)

Expand All @@ -30,7 +29,7 @@ func DefaultGenesis() *GenesisState {
func (gs GenesisState) Validate() error {
// this line is used by starport scaffolding # genesis/types/validate
if err := gs.Params.ValidateBasic(); err != nil {
return sdkerrors.Wrap(err, "params")
return errors.Wrap(err, "params")
}
return nil
}
Expand Down Expand Up @@ -61,7 +60,7 @@ func validateDataCommitmentWindow(i interface{}) error {
// ValidateBasic checks that the parameters have valid values.
func (p Params) ValidateBasic() error {
if err := validateDataCommitmentWindow(p.DataCommitmentWindow); err != nil {
return sdkerrors.Wrap(err, "data commitment window")
return errors.Wrap(err, "data commitment window")
}
return nil
}
12 changes: 6 additions & 6 deletions x/qgb/types/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"cosmossdk.io/errors"
)

// ToInternal transforms a BridgeValidator into its fully validated internal type.
Expand All @@ -25,7 +25,7 @@ func (b BridgeValidators) ToInternal() (*InternalBridgeValidators, error) {
for i := range b {
ibv, err := NewInternalBridgeValidator(b[i])
if err != nil {
return nil, sdkerrors.Wrapf(err, "member %d", i)
return nil, errors.Wrapf(err, "member %d", i)
}
ret[i] = ibv
}
Expand All @@ -48,14 +48,14 @@ func NewInternalBridgeValidator(bridgeValidator BridgeValidator) (*InternalBridg
EVMAddress: validatorEVMAddr,
}
if err := i.ValidateBasic(); err != nil {
return nil, sdkerrors.Wrap(err, "invalid bridge validator")
return nil, errors.Wrap(err, "invalid bridge validator")
}
return i, nil
}

func (i InternalBridgeValidator) ValidateBasic() error {
if i.Power == 0 {
return sdkerrors.Wrap(ErrEmpty, "power")
return errors.Wrap(ErrEmpty, "power")
}
return nil
}
Expand Down Expand Up @@ -170,11 +170,11 @@ func (ibv InternalBridgeValidators) ValidateBasic() error {
}
for i := range ibv {
if err := ibv[i].ValidateBasic(); err != nil {
return sdkerrors.Wrapf(err, "member %d", i)
return errors.Wrapf(err, "member %d", i)
}
}
if ibv.HasDuplicates() {
return sdkerrors.Wrap(ErrDuplicate, "addresses")
return errors.Wrap(ErrDuplicate, "addresses")
}
return nil
}
Loading

0 comments on commit e5e37ec

Please sign in to comment.