From 6ee80c571aee98582ae4c5beda4f5fdbbd2fb0cb Mon Sep 17 00:00:00 2001 From: Zerui Ge Date: Sat, 19 Oct 2024 21:29:27 -0700 Subject: [PATCH] feat(evmstaking): enhance failure log of processing event logs (#252) enhance failure log of processing event logs issue: none --- client/server/staking.go | 2 +- .../x/evmstaking/keeper/delegator_address.go | 12 ++- client/x/evmstaking/keeper/deposit.go | 13 ++- client/x/evmstaking/keeper/redelegation.go | 17 +++- client/x/evmstaking/keeper/unjail.go | 13 ++- .../x/evmstaking/keeper/update_commission.go | 22 ++++- client/x/evmstaking/keeper/validator.go | 20 ++-- client/x/evmstaking/keeper/withdraw.go | 19 +++- client/x/evmstaking/types/errors.go | 95 +++++++++++++++++++ client/x/evmstaking/types/events.go | 20 ++-- go.mod | 2 +- go.sum | 4 +- 12 files changed, 194 insertions(+), 45 deletions(-) create mode 100644 client/x/evmstaking/types/errors.go diff --git a/client/server/staking.go b/client/server/staking.go index 44906e9c..1cc8f690 100644 --- a/client/server/staking.go +++ b/client/server/staking.go @@ -381,7 +381,7 @@ func (s *Server) GetPeriodDelegationsByDelegatorAddress(r *http.Request) (resp a return nil, err } - return s.store.GetStakingKeeper().GetAllPeriodDelegation(queryContext, delAddr, valAddr) + return s.store.GetStakingKeeper().GetAllPeriodDelegationsByDelAndValAddr(queryContext, delAddr, valAddr) } // GetPeriodDelegationByDelegatorAddressAndID queries period delegation info for given validator delegator pair and period delegation id. diff --git a/client/x/evmstaking/keeper/delegator_address.go b/client/x/evmstaking/keeper/delegator_address.go index a61d0ee9..043562bc 100644 --- a/client/x/evmstaking/keeper/delegator_address.go +++ b/client/x/evmstaking/keeper/delegator_address.go @@ -25,6 +25,7 @@ func (k Keeper) ProcessSetWithdrawalAddress(ctx context.Context, ev *bindings.IP sdk.NewAttribute(types.AttributeKeyBlockHeight, strconv.FormatInt(sdkCtx.BlockHeight(), 10)), sdk.NewAttribute(types.AttributeKeyDelegatorUncmpPubKey, hex.EncodeToString(ev.DelegatorUncmpPubkey)), sdk.NewAttribute(types.AttributeKeyRewardAddress, hex.EncodeToString(ev.ExecutionAddress[:])), + sdk.NewAttribute(types.AttributeKeyStatusCode, types.UnwrapErrCode(err).String()), ), }) } @@ -32,7 +33,7 @@ func (k Keeper) ProcessSetWithdrawalAddress(ctx context.Context, ev *bindings.IP delCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.DelegatorUncmpPubkey) if err != nil { - return errors.Wrap(err, "compress depositor pubkey") + return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress delegator pubkey")) } depositorPubkey, err := k1util.PubKeyBytesToCosmos(delCmpPubkey) if err != nil { @@ -59,6 +60,7 @@ func (k Keeper) ProcessSetRewardAddress(ctx context.Context, ev *bindings.IPToke sdk.NewAttribute(types.AttributeKeyBlockHeight, strconv.FormatInt(sdkCtx.BlockHeight(), 10)), sdk.NewAttribute(types.AttributeKeyDelegatorUncmpPubKey, hex.EncodeToString(ev.DelegatorUncmpPubkey)), sdk.NewAttribute(types.AttributeKeyWithdrawalAddress, hex.EncodeToString(ev.ExecutionAddress[:])), + sdk.NewAttribute(types.AttributeKeyStatusCode, types.UnwrapErrCode(err).String()), ), }) } @@ -66,7 +68,7 @@ func (k Keeper) ProcessSetRewardAddress(ctx context.Context, ev *bindings.IPToke delCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.DelegatorUncmpPubkey) if err != nil { - return errors.Wrap(err, "compress depositor pubkey") + return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress delegator pubkey")) } depositorPubkey, err := k1util.PubKeyBytesToCosmos(delCmpPubkey) if err != nil { @@ -93,6 +95,7 @@ func (k Keeper) ProcessAddOperator(ctx context.Context, ev *bindings.IPTokenStak sdk.NewAttribute(types.AttributeKeyBlockHeight, strconv.FormatInt(sdkCtx.BlockHeight(), 10)), sdk.NewAttribute(types.AttributeKeyDelegatorUncmpPubKey, hex.EncodeToString(ev.UncmpPubkey)), sdk.NewAttribute(types.AttributeKeyOperatorAddress, ev.Operator.Hex()), + sdk.NewAttribute(types.AttributeKeyStatusCode, types.UnwrapErrCode(err).String()), ), }) } @@ -100,7 +103,7 @@ func (k Keeper) ProcessAddOperator(ctx context.Context, ev *bindings.IPTokenStak delCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.UncmpPubkey) if err != nil { - return errors.Wrap(err, "compress depositor pubkey") + return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress delegator pubkey")) } depositorPubkey, err := k1util.PubKeyBytesToCosmos(delCmpPubkey) if err != nil { @@ -126,6 +129,7 @@ func (k Keeper) ProcessRemoveOperator(ctx context.Context, ev *bindings.IPTokenS sdk.NewAttribute(types.AttributeKeyBlockHeight, strconv.FormatInt(sdkCtx.BlockHeight(), 10)), sdk.NewAttribute(types.AttributeKeyDelegatorUncmpPubKey, hex.EncodeToString(ev.UncmpPubkey)), sdk.NewAttribute(types.AttributeKeyOperatorAddress, ev.Operator.Hex()), + sdk.NewAttribute(types.AttributeKeyStatusCode, types.UnwrapErrCode(err).String()), ), }) } @@ -133,7 +137,7 @@ func (k Keeper) ProcessRemoveOperator(ctx context.Context, ev *bindings.IPTokenS delCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.UncmpPubkey) if err != nil { - return errors.Wrap(err, "compress depositor pubkey") + return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress delegator pubkey")) } depositorPubkey, err := k1util.PubKeyBytesToCosmos(delCmpPubkey) if err != nil { diff --git a/client/x/evmstaking/keeper/deposit.go b/client/x/evmstaking/keeper/deposit.go index b3927f1b..55523544 100644 --- a/client/x/evmstaking/keeper/deposit.go +++ b/client/x/evmstaking/keeper/deposit.go @@ -31,6 +31,7 @@ func (k Keeper) ProcessDeposit(ctx context.Context, ev *bindings.IPTokenStakingD sdk.NewAttribute(types.AttributeKeyPeriodType, strconv.FormatInt(ev.StakingPeriod.Int64(), 10)), sdk.NewAttribute(types.AttributeKeyAmount, ev.StakeAmount.String()), sdk.NewAttribute(types.AttributeKeySenderAddress, ev.OperatorAddress.Hex()), + sdk.NewAttribute(types.AttributeKeyStatusCode, types.UnwrapErrCode(err).String()), ), }) } @@ -38,7 +39,7 @@ func (k Keeper) ProcessDeposit(ctx context.Context, ev *bindings.IPTokenStakingD delCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.DelegatorUncmpPubkey) if err != nil { - return errors.Wrap(err, "compress delegator pubkey") + return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress delegator pubkey")) } depositorPubkey, err := k1util.PubKeyBytesToCosmos(delCmpPubkey) if err != nil { @@ -47,7 +48,7 @@ func (k Keeper) ProcessDeposit(ctx context.Context, ev *bindings.IPTokenStakingD valCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.ValidatorUnCmpPubkey) if err != nil { - return errors.Wrap(err, "compress validator pubkey") + return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress validator pubkey")) } validatorPubkey, err := k1util.PubKeyBytesToCosmos(valCmpPubkey) if err != nil { @@ -109,7 +110,7 @@ func (k Keeper) ProcessDeposit(ctx context.Context, ev *bindings.IPTokenStakingD val, err := k.stakingKeeper.GetValidator(ctx, validatorAddr) if errors.Is(err, stypes.ErrNoValidatorFound) { - return errors.New("validator not exists") + return types.WrapErrWithCode(types.ValidatorNotFound, errors.New("validator not exists")) } else if err != nil { return errors.Wrap(err, "get validator failed") } @@ -141,7 +142,11 @@ func (k Keeper) ProcessDeposit(ctx context.Context, ev *bindings.IPTokenStakingD delID, periodType, ) _, err = skeeperMsgServer.Delegate(ctx, msg) - if err != nil { + if errors.Is(err, stypes.ErrDelegationBelowMinimum) { + return types.WrapErrWithCode(types.InvalidDelegationAmount, err) + } else if errors.Is(err, stypes.ErrNoPeriodTypeFound) { + return types.WrapErrWithCode(types.InvalidPeriodType, err) + } else if err != nil { return errors.Wrap(err, "delegate") } diff --git a/client/x/evmstaking/keeper/redelegation.go b/client/x/evmstaking/keeper/redelegation.go index 20e4d037..62c11888 100644 --- a/client/x/evmstaking/keeper/redelegation.go +++ b/client/x/evmstaking/keeper/redelegation.go @@ -33,6 +33,7 @@ func (k Keeper) ProcessRedelegate(ctx context.Context, ev *bindings.IPTokenStaki sdk.NewAttribute(types.AttributeKeyDelegateID, ev.DelegationId.String()), sdk.NewAttribute(types.AttributeKeyAmount, ev.Amount.String()), sdk.NewAttribute(types.AttributeKeySenderAddress, ev.OperatorAddress.Hex()), + sdk.NewAttribute(types.AttributeKeyStatusCode, types.UnwrapErrCode(err).String()), ), }) } @@ -50,7 +51,7 @@ func (k Keeper) ProcessRedelegate(ctx context.Context, ev *bindings.IPTokenStaki delCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.DelegatorUncmpPubkey) if err != nil { - return errors.Wrap(err, "compress depositor pubkey") + return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress delegator pubkey")) } depositorPubkey, err := k1util.PubKeyBytesToCosmos(delCmpPubkey) if err != nil { @@ -59,7 +60,7 @@ func (k Keeper) ProcessRedelegate(ctx context.Context, ev *bindings.IPTokenStaki valSrcCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.ValidatorUncmpSrcPubkey) if err != nil { - return errors.Wrap(err, "compress src validator pubkey") + return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress src validator pubkey")) } validatorSrcPubkey, err := k1util.PubKeyBytesToCosmos(valSrcCmpPubkey) if err != nil { @@ -68,7 +69,7 @@ func (k Keeper) ProcessRedelegate(ctx context.Context, ev *bindings.IPTokenStaki valDstCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.ValidatorUncmpDstPubkey) if err != nil { - return errors.Wrap(err, "compress dst validator pubkey") + return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress dst validator pubkey")) } validatorDstPubkey, err := k1util.PubKeyBytesToCosmos(valDstCmpPubkey) if err != nil { @@ -96,12 +97,18 @@ func (k Keeper) ProcessRedelegate(ctx context.Context, ev *bindings.IPTokenStaki if delEvmAddr.String() != ev.OperatorAddress.String() { operatorAddr, err := k.DelegatorOperatorAddress.Get(ctx, depositorAddr.String()) if errors.Is(err, collections.ErrNotFound) { - return errors.New("invalid redelegateOnBehalf txn, no operator for delegator") + return types.WrapErrWithCode( + types.InvalidOperator, + errors.New("invalid redelegateOnBehalf txn, no operator"), + ) } else if err != nil { return errors.Wrap(err, "get delegator's operator address failed") } if operatorAddr != ev.OperatorAddress.String() { - return errors.New("invalid redelegateOnBehalf txn, not from operator") + return types.WrapErrWithCode( + types.InvalidOperator, + errors.New("invalid redelegateOnBehalf txn, not from operator"), + ) } } diff --git a/client/x/evmstaking/keeper/unjail.go b/client/x/evmstaking/keeper/unjail.go index 6217eb33..2278c143 100644 --- a/client/x/evmstaking/keeper/unjail.go +++ b/client/x/evmstaking/keeper/unjail.go @@ -26,6 +26,7 @@ func (k Keeper) ProcessUnjail(ctx context.Context, ev *bindings.IPTokenStakingUn sdk.NewAttribute(types.AttributeKeyBlockHeight, strconv.FormatInt(sdkCtx.BlockHeight(), 10)), sdk.NewAttribute(types.AttributeKeyValidatorUncmpPubKey, hex.EncodeToString(ev.ValidatorUncmpPubkey)), sdk.NewAttribute(types.AttributeKeySenderAddress, ev.Unjailer.Hex()), + sdk.NewAttribute(types.AttributeKeyStatusCode, types.UnwrapErrCode(err).String()), ), }) } @@ -33,7 +34,7 @@ func (k Keeper) ProcessUnjail(ctx context.Context, ev *bindings.IPTokenStakingUn valCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.ValidatorUncmpPubkey) if err != nil { - return errors.Wrap(err, "compress validator pubkey") + return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress validator pubkey")) } validatorPubkey, err := k1util.PubKeyBytesToCosmos(valCmpPubkey) if err != nil { @@ -52,12 +53,18 @@ func (k Keeper) ProcessUnjail(ctx context.Context, ev *bindings.IPTokenStakingUn if valEvmAddr.String() != ev.Unjailer.String() { operatorAddr, err := k.DelegatorOperatorAddress.Get(ctx, valDelAddr.String()) if errors.Is(err, collections.ErrNotFound) { - return errors.New("invalid unjailOnBehalf txn, no operator for delegator") + return types.WrapErrWithCode( + types.InvalidOperator, + errors.New("invalid unjailOnBehalf txn, no operator for delegator"), + ) } else if err != nil { return errors.Wrap(err, "get validator's operator address failed") } if operatorAddr != ev.Unjailer.String() { - return errors.New("invalid unjailOnBehalf txn, not from operator") + return types.WrapErrWithCode( + types.InvalidOperator, + errors.New("invalid unjailOnBehalf txn, not from operator"), + ) } } diff --git a/client/x/evmstaking/keeper/update_commission.go b/client/x/evmstaking/keeper/update_commission.go index 1d8e8837..c7577c42 100644 --- a/client/x/evmstaking/keeper/update_commission.go +++ b/client/x/evmstaking/keeper/update_commission.go @@ -2,6 +2,8 @@ package keeper import ( "context" + "encoding/hex" + "strconv" "cosmossdk.io/math" @@ -9,15 +11,31 @@ import ( skeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" stypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/piplabs/story/client/x/evmstaking/types" "github.com/piplabs/story/contracts/bindings" "github.com/piplabs/story/lib/errors" "github.com/piplabs/story/lib/k1util" ) -func (k Keeper) ProcessUpdateValidatorCommission(ctx context.Context, ev *bindings.IPTokenStakingUpdateValidatorCommssion) error { +func (k Keeper) ProcessUpdateValidatorCommission(ctx context.Context, ev *bindings.IPTokenStakingUpdateValidatorCommssion) (err error) { + defer func() { + sdkCtx := sdk.UnwrapSDKContext(ctx) + if err != nil { + sdkCtx.EventManager().EmitEvents(sdk.Events{ + sdk.NewEvent( + types.EventTypeUpdateValidatorCommissionFailure, + sdk.NewAttribute(types.AttributeKeyBlockHeight, strconv.FormatInt(sdkCtx.BlockHeight(), 10)), + sdk.NewAttribute(types.AttributeKeyValidatorUncmpPubKey, hex.EncodeToString(ev.ValidatorUncmpPubkey)), + sdk.NewAttribute(types.AttributeKeyCommissionRate, strconv.FormatUint(uint64(ev.CommissionRate), 10)), + sdk.NewAttribute(types.AttributeKeyStatusCode, types.UnwrapErrCode(err).String()), + ), + }) + } + }() + valCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.ValidatorUncmpPubkey) if err != nil { - return errors.Wrap(err, "compress validator pubkey") + return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress validator pubkey")) } validatorPubkey, err := k1util.PubKeyBytesToCosmos(valCmpPubkey) if err != nil { diff --git a/client/x/evmstaking/keeper/validator.go b/client/x/evmstaking/keeper/validator.go index 6524b758..1ce8449b 100644 --- a/client/x/evmstaking/keeper/validator.go +++ b/client/x/evmstaking/keeper/validator.go @@ -35,6 +35,7 @@ func (k Keeper) ProcessCreateValidator(ctx context.Context, ev *bindings.IPToken sdk.NewAttribute(types.AttributeKeyMaxCommissionChangeRate, strconv.FormatUint(uint64(ev.MaxCommissionChangeRate), 10)), sdk.NewAttribute(types.AttributeKeyTokenType, strconv.FormatUint(uint64(ev.SupportsUnlocked), 10)), sdk.NewAttribute(types.AttributeKeySenderAddress, ev.OperatorAddress.Hex()), + sdk.NewAttribute(types.AttributeKeyStatusCode, types.UnwrapErrCode(err).String()), ), }) } @@ -43,7 +44,7 @@ func (k Keeper) ProcessCreateValidator(ctx context.Context, ev *bindings.IPToken // When creating a validator, it's self-delegation. Thus, validator pubkey is also delegation pubkey. valCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.ValidatorUncmpPubkey) if err != nil { - return errors.Wrap(err, "compress validator pubkey") + return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress validator pubkey")) } validatorPubkey, err := k1util.PubKeyBytesToCosmos(valCmpPubkey) if err != nil { @@ -104,7 +105,7 @@ func (k Keeper) ProcessCreateValidator(ctx context.Context, ev *bindings.IPToken skeeperMsgServer := skeeper.NewMsgServerImpl(evmstakingSKeeper) if _, err = k.stakingKeeper.GetValidator(ctx, validatorAddr); err == nil { - return errors.New("validator already exists") + return types.WrapErrWithCode(types.ValidatorAlreadyExists, errors.New("validator already exists")) } else if !errors.Is(err, stypes.ErrNoValidatorFound) { // Either the validator does not exist, or unknown error. return errors.Wrap(err, "get validator") @@ -120,11 +121,6 @@ func (k Keeper) ProcessCreateValidator(ctx context.Context, ev *bindings.IPToken return errors.Wrap(err, "get min self delegation") } - tokenType := int32(ev.SupportsUnlocked) - if _, err := k.stakingKeeper.GetTokenTypeInfo(ctx, tokenType); err != nil { - return errors.Wrap(err, "invalid token type") - } - // Validator does not exist, create validator with self-delegation. msg, err := stypes.NewMsgCreateValidator( validatorAddr.String(), @@ -138,14 +134,20 @@ func (k Keeper) ProcessCreateValidator(ctx context.Context, ev *bindings.IPToken math.LegacyNewDec(int64(ev.MaxCommissionChangeRate)).Quo(math.LegacyNewDec(10000)), ), minSelfDelegation, // make minimum self delegation align with minimum delegation amount - tokenType, + int32(ev.SupportsUnlocked), ) if err != nil { return errors.Wrap(err, "create validator message") } _, err = skeeperMsgServer.CreateValidator(ctx, msg) - if err != nil { + if errors.Is(err, stypes.ErrCommissionLTMinRate) { + return types.WrapErrWithCode(types.InvalidCommissionRate, err) + } else if errors.Is(err, stypes.ErrMinSelfDelegationBelowMinDelegation) { + return types.WrapErrWithCode(types.InvalidMinSelfDelegation, err) + } else if errors.Is(err, stypes.ErrNoTokenTypeFound) { + return types.WrapErrWithCode(types.InvalidTokenType, err) + } else if err != nil { return errors.Wrap(err, "create validator") } diff --git a/client/x/evmstaking/keeper/withdraw.go b/client/x/evmstaking/keeper/withdraw.go index e29b8b43..2f195ee5 100644 --- a/client/x/evmstaking/keeper/withdraw.go +++ b/client/x/evmstaking/keeper/withdraw.go @@ -327,6 +327,7 @@ func (k Keeper) ProcessWithdraw(ctx context.Context, ev *bindings.IPTokenStaking sdk.NewAttribute(types.AttributeKeyDelegateID, ev.DelegationId.String()), sdk.NewAttribute(types.AttributeKeyAmount, ev.StakeAmount.String()), sdk.NewAttribute(types.AttributeKeySenderAddress, ev.OperatorAddress.Hex()), + sdk.NewAttribute(types.AttributeKeyStatusCode, types.UnwrapErrCode(err).String()), ), }) } @@ -344,7 +345,7 @@ func (k Keeper) ProcessWithdraw(ctx context.Context, ev *bindings.IPTokenStaking delCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.DelegatorUncmpPubkey) if err != nil { - return errors.Wrap(err, "compress depositor pubkey") + return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress delegator pubkey")) } depositorPubkey, err := k1util.PubKeyBytesToCosmos(delCmpPubkey) if err != nil { @@ -353,7 +354,7 @@ func (k Keeper) ProcessWithdraw(ctx context.Context, ev *bindings.IPTokenStaking valCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.ValidatorUnCmpPubkey) if err != nil { - return errors.Wrap(err, "compress validator pubkey") + return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress validator pubkey")) } validatorPubkey, err := k1util.PubKeyBytesToCosmos(valCmpPubkey) if err != nil { @@ -376,12 +377,18 @@ func (k Keeper) ProcessWithdraw(ctx context.Context, ev *bindings.IPTokenStaking if delEvmAddr.String() != ev.OperatorAddress.String() { operatorAddr, err := k.DelegatorOperatorAddress.Get(ctx, depositorAddr.String()) if errors.Is(err, collections.ErrNotFound) { - return errors.New("invalid unstakeOnBehalf txn, no operator for delegator") + return types.WrapErrWithCode( + types.InvalidOperator, + errors.New("invalid unstakeOnBehalf txn, no operator"), + ) } else if err != nil { return errors.Wrap(err, "get delegator's operator address failed") } if operatorAddr != ev.OperatorAddress.String() { - return errors.New("invalid unstakeOnBehalf txn, not from operator") + return types.WrapErrWithCode( + types.InvalidOperator, + errors.New("invalid unstakeOnBehalf txn, not from operator"), + ) } } @@ -405,7 +412,9 @@ func (k Keeper) ProcessWithdraw(ctx context.Context, ev *bindings.IPTokenStaking // Undelegate from the validator (validator existence is checked in ValidateUnbondAmount) resp, err := skeeper.NewMsgServerImpl(k.stakingKeeper.(*skeeper.Keeper)).Undelegate(ctx, msg) - if err != nil { + if errors.Is(err, stypes.ErrNoPeriodDelegation) { + return types.WrapErrWithCode(types.PeriodDelegationNotFound, err) + } else if err != nil { return errors.Wrap(err, "undelegate") } diff --git a/client/x/evmstaking/types/errors.go b/client/x/evmstaking/types/errors.go new file mode 100644 index 00000000..bdc90694 --- /dev/null +++ b/client/x/evmstaking/types/errors.go @@ -0,0 +1,95 @@ +package types + +import ( + "errors" +) + +type ErrCode uint32 + +const ( + Unspecified ErrCode = 0 + InvalidUncmpPubKey ErrCode = 1 + ValidatorNotFound ErrCode = 2 + ValidatorAlreadyExists ErrCode = 3 + InvalidTokenType ErrCode = 4 + InvalidPeriodType ErrCode = 5 + InvalidOperator ErrCode = 6 + InvalidCommissionRate ErrCode = 7 + InvalidMinSelfDelegation ErrCode = 8 + InvalidDelegationAmount ErrCode = 9 + PeriodDelegationNotFound ErrCode = 10 +) + +var ( + ErrUnspecified = errors.New("unspecified") + ErrInvalidUncmpPubKey = errors.New("invalid_uncompressed_pubkey") + ErrValidatorNotFound = errors.New("validator_not_found") + ErrValidatorAlreadyExists = errors.New("validator_already_exists") + ErrInvalidTokenType = errors.New("invalid_token_type") + ErrInvalidPeriodType = errors.New("invalid_period_type") + ErrInvalidOperator = errors.New("invalid_operator") + ErrInvalidCommissionRate = errors.New("invalid_commission_rate") + ErrInvalidMinSelfDelegation = errors.New("invalid_min_self_delegation") + ErrInvalidDelegationAmount = errors.New("invalid_delegation_amount") + ErrPeriodDelegationNotFound = errors.New("period_delegation_not_found") +) + +var codeToErr = map[ErrCode]error{ + Unspecified: ErrUnspecified, + InvalidUncmpPubKey: ErrInvalidUncmpPubKey, + ValidatorNotFound: ErrValidatorNotFound, + ValidatorAlreadyExists: ErrValidatorAlreadyExists, + InvalidTokenType: ErrInvalidTokenType, + InvalidPeriodType: ErrInvalidPeriodType, + InvalidOperator: ErrInvalidOperator, + InvalidCommissionRate: ErrInvalidCommissionRate, + InvalidMinSelfDelegation: ErrInvalidMinSelfDelegation, + InvalidDelegationAmount: ErrInvalidDelegationAmount, + PeriodDelegationNotFound: ErrPeriodDelegationNotFound, +} + +func (c ErrCode) String() string { + if _, ok := codeToErr[c]; !ok { + return ErrUnspecified.Error() + } + + return codeToErr[c].Error() +} + +//nolint:wrapcheck // we are wrapping the error with the code +func WrapErrWithCode(code ErrCode, err error) error { + if _, ok := codeToErr[code]; !ok { + code = Unspecified + } + + return errors.Join(codeToErr[code], err) +} + +func UnwrapErrCode(err error) ErrCode { + switch { + case errors.Is(err, ErrUnspecified): + return Unspecified + case errors.Is(err, ErrInvalidUncmpPubKey): + return InvalidUncmpPubKey + case errors.Is(err, ErrValidatorNotFound): + return ValidatorNotFound + case errors.Is(err, ErrValidatorAlreadyExists): + return ValidatorAlreadyExists + case errors.Is(err, ErrInvalidTokenType): + return InvalidTokenType + case errors.Is(err, ErrInvalidPeriodType): + return InvalidPeriodType + case errors.Is(err, ErrInvalidOperator): + return InvalidOperator + case errors.Is(err, ErrInvalidCommissionRate): + return InvalidCommissionRate + case errors.Is(err, ErrInvalidMinSelfDelegation): + return InvalidMinSelfDelegation + case errors.Is(err, ErrInvalidDelegationAmount): + return InvalidDelegationAmount + case errors.Is(err, ErrPeriodDelegationNotFound): + return PeriodDelegationNotFound + default: + return Unspecified + } +} diff --git a/client/x/evmstaking/types/events.go b/client/x/evmstaking/types/events.go index 72fbf04b..6e922fa7 100644 --- a/client/x/evmstaking/types/events.go +++ b/client/x/evmstaking/types/events.go @@ -2,16 +2,18 @@ package types // evmstaking module event types. const ( - EventTypeSetWithdrawalAddressFailure = "set_withdrawal_address_failure" - EventTypeSetRewardAddressFailure = "set_reward_address_failure" - EventTypeAddOperatorFailure = "add_operator_failure" - EventTypeRemoveOperatorFailure = "remove_operator_failure" - EventTypeCreateValidatorFailure = "create_validator_failure" - EventTypeDelegateFailure = "delegate_failure" - EventTypeRedelegateFailure = "redelegate_failure" - EventTypeUndelegateFailure = "undelegate_failure" - EventTypeUnjailFailure = "unjail_failure" + EventTypeUpdateValidatorCommissionFailure = "update_validator_commission_failure" + EventTypeSetWithdrawalAddressFailure = "set_withdrawal_address_failure" + EventTypeSetRewardAddressFailure = "set_reward_address_failure" + EventTypeAddOperatorFailure = "add_operator_failure" + EventTypeRemoveOperatorFailure = "remove_operator_failure" + EventTypeCreateValidatorFailure = "create_validator_failure" + EventTypeDelegateFailure = "delegate_failure" + EventTypeRedelegateFailure = "redelegate_failure" + EventTypeUndelegateFailure = "undelegate_failure" + EventTypeUnjailFailure = "unjail_failure" + AttributeKeyStatusCode = "status_code" AttributeKeyBlockHeight = "block_height" AttributeKeyDelegatorUncmpPubKey = "delegator_uncmp_pubkey" AttributeKeyValidatorUncmpPubKey = "validator_uncmp_pubkey" diff --git a/go.mod b/go.mod index 150acb01..b0ee2455 100644 --- a/go.mod +++ b/go.mod @@ -295,7 +295,7 @@ replace ( cosmossdk.io/core v0.12.0 => cosmossdk.io/core v0.11.0 // Direct cosmos-sdk branch link: https://github.com/piplabs/cosmos-sdk/tree/piplabs/v0.50.7, current branch: piplabs/v0.50.7 - github.com/cosmos/cosmos-sdk => github.com/piplabs/cosmos-sdk v0.50.7-piplabs-v0.12 + github.com/cosmos/cosmos-sdk => github.com/piplabs/cosmos-sdk v0.50.7-piplabs-v0.14 // See https://github.com/cosmos/cosmos-sdk/pull/14952 // Also https://github.com/cosmos/cosmos-db/blob/main/go.mod#L11-L12 diff --git a/go.sum b/go.sum index b6650606..0a84b719 100644 --- a/go.sum +++ b/go.sum @@ -1032,8 +1032,8 @@ github.com/pierrec/lz4 v1.0.2-0.20190131084431-473cd7ce01a1/go.mod h1:3/3N9NVKO0 github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= -github.com/piplabs/cosmos-sdk v0.50.7-piplabs-v0.12 h1:zIb4t8DjBnk7ZR9QcHMW7hpU2qaZp/pQ52CARjJ26Nc= -github.com/piplabs/cosmos-sdk v0.50.7-piplabs-v0.12/go.mod h1:84xDDJEHttRT7NDGwBaUOLVOMN0JNE9x7NbsYIxXs1s= +github.com/piplabs/cosmos-sdk v0.50.7-piplabs-v0.14 h1:Ks4P6BZOAHs4Nhr3hX5uMhcK8KitVUajAr1RVXvz448= +github.com/piplabs/cosmos-sdk v0.50.7-piplabs-v0.14/go.mod h1:84xDDJEHttRT7NDGwBaUOLVOMN0JNE9x7NbsYIxXs1s= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ= github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=