Skip to content

Commit

Permalink
feat(evmstaking): enhance failure log of processing event logs (#252)
Browse files Browse the repository at this point in the history
enhance failure log of processing event logs

issue: none
  • Loading branch information
ezreal1997 authored Oct 20, 2024
1 parent e6a8885 commit 6ee80c5
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 45 deletions.
2 changes: 1 addition & 1 deletion client/server/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 8 additions & 4 deletions client/x/evmstaking/keeper/delegator_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ 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()),
),
})
}
}()

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 {
Expand All @@ -59,14 +60,15 @@ 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()),
),
})
}
}()

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 {
Expand All @@ -93,14 +95,15 @@ 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()),
),
})
}
}()

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 {
Expand All @@ -126,14 +129,15 @@ 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()),
),
})
}
}()

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 {
Expand Down
13 changes: 9 additions & 4 deletions client/x/evmstaking/keeper/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ 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()),
),
})
}
}()

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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}

Expand Down
17 changes: 12 additions & 5 deletions client/x/evmstaking/keeper/redelegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
),
})
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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"),
)
}
}

Expand Down
13 changes: 10 additions & 3 deletions client/x/evmstaking/keeper/unjail.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ 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()),
),
})
}
}()

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 {
Expand All @@ -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"),
)
}
}

Expand Down
22 changes: 20 additions & 2 deletions client/x/evmstaking/keeper/update_commission.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,40 @@ package keeper

import (
"context"
"encoding/hex"
"strconv"

"cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
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 {
Expand Down
20 changes: 11 additions & 9 deletions client/x/evmstaking/keeper/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
),
})
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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")
Expand All @@ -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(),
Expand All @@ -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")
}

Expand Down
19 changes: 14 additions & 5 deletions client/x/evmstaking/keeper/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
),
})
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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"),
)
}
}

Expand All @@ -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")
}

Expand Down
Loading

0 comments on commit 6ee80c5

Please sign in to comment.