Skip to content

Commit

Permalink
[action] remove redundant errors.New() (#3199)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Mar 16, 2022
1 parent ad44742 commit 8733fd8
Show file tree
Hide file tree
Showing 20 changed files with 40 additions and 70 deletions.
2 changes: 1 addition & 1 deletion action/candidate_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (cr *CandidateRegister) Proto() *iotextypes.CandidateRegister {
// LoadProto converts a protobuf's Action to CandidateRegister
func (cr *CandidateRegister) LoadProto(pbAct *iotextypes.CandidateRegister) error {
if pbAct == nil {
return errors.New("empty action proto to load")
return ErrNilProto
}

cInfo := pbAct.GetCandidate()
Expand Down
2 changes: 1 addition & 1 deletion action/candidate_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (cu *CandidateUpdate) Proto() *iotextypes.CandidateBasicInfo {
// LoadProto converts a protobuf's Action to CandidateUpdate
func (cu *CandidateUpdate) LoadProto(pbAct *iotextypes.CandidateBasicInfo) error {
if pbAct == nil {
return errors.New("empty action proto to load")
return ErrNilProto
}

cu.name = pbAct.GetName()
Expand Down
64 changes: 18 additions & 46 deletions action/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,26 @@ package action

import "github.com/pkg/errors"

// vars
var (
// ErrAddress indicates error of address
ErrAddress = errors.New("invalid address")
// ErrVotee indicates the error of votee
ErrVotee = errors.New("votee is not a candidate")
// ErrNotFound indicates the nonexistence of action
ErrNotFound = errors.New("action not found")
// ErrChainID indicates the error of chainID
ErrChainID = errors.New("invalid chainID")
// ErrExistedInPool indicates the action already exists in the actpool
ErrExistedInPool = errors.New("known transaction")
// ErrReplaceUnderpriced is returned if a transaction is attempted to be replaced
// with a different one without the required price bump.
ErrAddress = errors.New("invalid address")
ErrVotee = errors.New("votee is not a candidate")
ErrNotFound = errors.New("action not found")
ErrChainID = errors.New("invalid chainID")
ErrExistedInPool = errors.New("known transaction")
ErrReplaceUnderpriced = errors.New("replacement transaction underpriced")
// ErrNonceTooLow indicates if the nonce of a transaction is lower than the
// one present in the local chain.
ErrNonceTooLow = errors.New("nonce too low")
// ErrUnderpriced is returned if a transaction's gas price is below the minimum
// configured for the transaction pool.
ErrUnderpriced = errors.New("transaction underpriced")
// ErrNegativeValue is a sanity error to ensure no one is able to specify a
// transaction with a negative value.
ErrNegativeValue = errors.New("negative value")
// ErrIntrinsicGas is returned if the transaction is specified to use less gas
// than required to start the invocation.
ErrIntrinsicGas = errors.New("intrinsic gas too low")
// ErrInsufficientFunds is returned if the total cost of executing a transaction
// is higher than the balance of the user's account.
ErrInsufficientFunds = errors.New("insufficient funds for gas * price + value")
// ErrNonceTooHigh is returned if the nonce of a transaction is higher than the
// next one expected based on the local chain.
ErrNonceTooHigh = errors.New("nonce too high")
// ErrInvalidSender is returned if the transaction contains an invalid signature.
ErrInvalidSender = errors.New("invalid sender")
// ErrTxPoolOverflow is returned if the transaction pool is full and can't accpet
// another remote transaction.
ErrTxPoolOverflow = errors.New("txpool is full")
// ErrGasLimit is returned if a transaction's requested gas limit exceeds the
// maximum allowance of the current block.
ErrGasLimit = errors.New("exceeds block gas limit")
// ErrOversizedData is returned if the input data of a transaction is greater
// than some meaningful limit a user might use. This is not a consensus error
// making the transaction invalid, rather a DOS protection.
ErrOversizedData = errors.New("oversized data")
// ErrEmptyActionPool indicates the error of empty action proto
ErrEmptyActionPool = errors.New("empty action proto to load")
// ErrNilAction indicates the error of nil action
ErrNilAction = errors.New("nil action to load proto")
ErrNonceTooLow = errors.New("nonce too low")
ErrUnderpriced = errors.New("transaction underpriced")
ErrNegativeValue = errors.New("negative value")
ErrIntrinsicGas = errors.New("intrinsic gas too low")
ErrInsufficientFunds = errors.New("insufficient funds for gas * price + value")
ErrNonceTooHigh = errors.New("nonce too high")
ErrInvalidSender = errors.New("invalid sender")
ErrTxPoolOverflow = errors.New("txpool is full")
ErrGasLimit = errors.New("exceeds block gas limit")
ErrOversizedData = errors.New("oversized data")
ErrNilProto = errors.New("empty action proto to load")
ErrNilAction = errors.New("nil action to load proto")
)

// LoadErrorDescription loads corresponding description related to the error
Expand Down
2 changes: 1 addition & 1 deletion action/const_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestLoadErrorDescription(t *testing.T) {
{ErrAddress, ErrAddress.Error()},
{ErrNegativeValue, ErrNegativeValue.Error()},
{ErrNilAction, "Unknown"},
{ErrEmptyActionPool, "Unknown"},
{ErrNilProto, "Unknown"},
}
for _, e := range tests {
request.Equal(e.ret, LoadErrorDescription(e.err))
Expand Down
4 changes: 2 additions & 2 deletions action/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ func (elp *envelope) Proto() *iotextypes.ActionCore {
// LoadProto loads fields from protobuf format.
func (elp *envelope) LoadProto(pbAct *iotextypes.ActionCore) error {
if pbAct == nil {
return errors.New("empty action proto to load")
return ErrNilProto
}
if elp == nil {
return errors.New("nil action to load proto")
return ErrNilAction
}
*elp = envelope{}
elp.version = pbAct.GetVersion()
Expand Down
4 changes: 2 additions & 2 deletions action/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ func (ex *Execution) Proto() *iotextypes.Execution {
// LoadProto converts a protobuf's Execution to Execution
func (ex *Execution) LoadProto(pbAct *iotextypes.Execution) error {
if pbAct == nil {
return errors.New("empty action proto to load")
return ErrNilProto
}
if ex == nil {
return errors.New("nil action to load proto")
return ErrNilAction
}
*ex = Execution{}

Expand Down
2 changes: 1 addition & 1 deletion action/protocol/staking/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func (p *Protocol) handle(ctx context.Context, act action.Action, csm CandidateS
// Validate validates a staking message
func (p *Protocol) Validate(ctx context.Context, act action.Action, sr protocol.StateReader) error {
if act == nil {
return ErrNilAction
return action.ErrNilAction
}
switch act := act.(type) {
case *action.CreateStake:
Expand Down
1 change: 0 additions & 1 deletion action/protocol/staking/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

// Errors
var (
ErrNilAction = errors.New("action is nil")
ErrInvalidAmount = errors.New("invalid staking amount")
ErrInvalidCanName = errors.New("invalid candidate name")
ErrInvalidOwner = errors.New("invalid owner address")
Expand Down
5 changes: 2 additions & 3 deletions action/putpollresult.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"math/big"

"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"

"github.com/iotexproject/iotex-core/pkg/util/byteutil"
Expand Down Expand Up @@ -47,10 +46,10 @@ func NewPutPollResult(
// LoadProto converts a proto message into put block action.
func (r *PutPollResult) LoadProto(putPollResultPb *iotextypes.PutPollResult) error {
if putPollResultPb == nil {
return errors.New("empty action proto to load")
return ErrNilProto
}
if r == nil {
return errors.New("nil action to load proto")
return ErrNilAction
}
*r = PutPollResult{}

Expand Down
2 changes: 1 addition & 1 deletion action/rlp_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func rlpSignedHash(tx rlpTransaction, chainID uint32, sig []byte) (hash.Hash256,

func generateRlpTx(act rlpTransaction) (*types.Transaction, error) {
if act == nil {
return nil, errors.New("nil action to generate RLP tx")
return nil, ErrNilAction
}

// generate raw tx
Expand Down
2 changes: 1 addition & 1 deletion action/rlp_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestGenerateRlp(t *testing.T) {
err string
hash hash.Hash256
}{
{nil, validSig, "nil action to generate RLP tx", hash.ZeroHash256},
{nil, validSig, ErrNilAction.Error(), hash.ZeroHash256},
{rlpTsf, validSig, "invalid recipient address", hash.ZeroHash256},
{rlpTsf1, signByte, "invalid signature length =", hash.ZeroHash256},
{rlpTsf1, validSig, "", hash.BytesToHash256(hT1)},
Expand Down
2 changes: 1 addition & 1 deletion action/sealedenvelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (sealed *SealedEnvelope) Proto() *iotextypes.Action {
// LoadProto loads from proto scheme.
func (sealed *SealedEnvelope) LoadProto(pbAct *iotextypes.Action) error {
if pbAct == nil {
return ErrEmptyActionPool
return ErrNilProto
}
if sealed == nil {
return ErrNilAction
Expand Down
2 changes: 1 addition & 1 deletion action/stake_adddeposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (ds *DepositToStake) Proto() *iotextypes.StakeAddDeposit {
// LoadProto converts a protobuf's Action to DepositToStake
func (ds *DepositToStake) LoadProto(pbAct *iotextypes.StakeAddDeposit) error {
if pbAct == nil {
return errors.New("empty action proto to load")
return ErrNilProto
}

ds.bucketIndex = pbAct.GetBucketIndex()
Expand Down
2 changes: 1 addition & 1 deletion action/stake_changecandidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (cc *ChangeCandidate) Proto() *iotextypes.StakeChangeCandidate {
// LoadProto loads change candidate from protobuf
func (cc *ChangeCandidate) LoadProto(pbAct *iotextypes.StakeChangeCandidate) error {
if pbAct == nil {
return errors.New("empty action proto to load")
return ErrNilProto
}

cc.candidateName = pbAct.GetCandidateName()
Expand Down
2 changes: 1 addition & 1 deletion action/stake_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (cs *CreateStake) Proto() *iotextypes.StakeCreate {
// LoadProto converts a protobuf's Action to CreateStake
func (cs *CreateStake) LoadProto(pbAct *iotextypes.StakeCreate) error {
if pbAct == nil {
return errors.New("empty action proto to load")
return ErrNilProto
}

cs.candName = pbAct.GetCandidateName()
Expand Down
2 changes: 1 addition & 1 deletion action/stake_reclaim.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (sr *reclaimStake) Proto() *iotextypes.StakeReclaim {
// LoadProto converts a protobuf's Action to reclaimStake
func (sr *reclaimStake) LoadProto(pbAct *iotextypes.StakeReclaim) error {
if pbAct == nil {
return errors.New("empty action proto to load")
return ErrNilProto
}

sr.bucketIndex = pbAct.GetBucketIndex()
Expand Down
2 changes: 1 addition & 1 deletion action/stake_restake.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (rs *Restake) Proto() *iotextypes.StakeRestake {
// LoadProto converts a protobuf's Action to Restake
func (rs *Restake) LoadProto(pbAct *iotextypes.StakeRestake) error {
if pbAct == nil {
return errors.New("empty action proto to load")
return ErrNilProto
}

rs.bucketIndex = pbAct.GetBucketIndex()
Expand Down
2 changes: 1 addition & 1 deletion action/stake_transferownership.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (ts *TransferStake) Proto() *iotextypes.StakeTransferOwnership {
// LoadProto loads transfer stake protobuf
func (ts *TransferStake) LoadProto(pbAct *iotextypes.StakeTransferOwnership) error {
if pbAct == nil {
return errors.New("empty action proto to load")
return ErrNilProto
}
voterAddress, err := address.FromString(pbAct.GetVoterAddress())
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions action/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ func (tsf *Transfer) Proto() *iotextypes.Transfer {
// LoadProto converts a protobuf's Action to Transfer
func (tsf *Transfer) LoadProto(pbAct *iotextypes.Transfer) error {
if pbAct == nil {
return errors.New("empty action proto to load")
return ErrNilProto
}
if tsf == nil {
return errors.New("nil action to load proto")
return ErrNilAction
}
*tsf = Transfer{}

Expand Down
2 changes: 1 addition & 1 deletion gasstation/gasstattion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func TestEstimateGasForAction(t *testing.T) {
require.Equal(uint64(10000)+10*action.ExecutionDataGas, ret)

ret, err = gs.EstimateGasForAction(nil)
require.ErrorIs(err, action.ErrEmptyActionPool)
require.ErrorIs(err, action.ErrNilProto)
require.Equal(ret, uint64(0))
}

Expand Down

0 comments on commit 8733fd8

Please sign in to comment.