Skip to content

Commit

Permalink
Merge branch 'main' into feat/wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kingster-will authored Oct 23, 2024
2 parents c6f8877 + 10d14ad commit 18ceefb
Show file tree
Hide file tree
Showing 18 changed files with 189 additions and 96 deletions.
16 changes: 10 additions & 6 deletions client/server/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package server
import (
"net/http"
"strconv"
"strings"

"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/staking/keeper"
Expand Down Expand Up @@ -306,14 +307,17 @@ func (s *Server) GetDelegatorByDelegatorAddress(r *http.Request) (resp any, err

delOperatorEvmAddr, err := s.store.GetEvmStakingKeeper().DelegatorOperatorAddress.Get(queryContext, delAddr)
if err != nil {
return nil, err
if !strings.Contains(err.Error(), "not found") {
return nil, err
}
delOperatorEvmAddr = ""
}

return map[string]string{
"delegator_addr": delAddr,
"withdraw_address": delWithdrawEvmAddr,
"reward_address": delRewardEvmAddr,
"operator_address": delOperatorEvmAddr,
return &DelegatorBaseInfo{
DelegatorAddr: delAddr,
WithdrawAddress: delWithdrawEvmAddr,
RewardAddress: delRewardEvmAddr,
OperatorAddress: delOperatorEvmAddr,
}, nil
}

Expand Down
8 changes: 8 additions & 0 deletions client/server/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package server

type DelegatorBaseInfo struct {
DelegatorAddr string `json:"delegator_addr"`
WithdrawAddress string `json:"withdraw_address"`
RewardAddress string `json:"reward_address"`
OperatorAddress string `json:"operator_address"`
}
25 changes: 23 additions & 2 deletions client/x/evmengine/keeper/ubi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package keeper

import (
"context"
"strconv"

"cosmossdk.io/math"

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

"github.com/piplabs/story/client/x/evmengine/types"
"github.com/piplabs/story/contracts/bindings"
"github.com/piplabs/story/lib/errors"
Expand Down Expand Up @@ -41,9 +45,26 @@ func (k *Keeper) ProcessUbiEvents(ctx context.Context, height uint64, logs []*ty
return nil
}

func (k *Keeper) ProcessUBIPercentageSet(ctx context.Context, ev *bindings.UBIPoolUBIPercentageSet) error {
func (k *Keeper) ProcessUBIPercentageSet(ctx context.Context, ev *bindings.UBIPoolUBIPercentageSet) (err error) {
defer func() {
sdkCtx := sdk.UnwrapSDKContext(ctx)
if err != nil {
sdkCtx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeUpdateUbiFailure,
sdk.NewAttribute(types.AttributeKeyBlockHeight, strconv.FormatInt(sdkCtx.BlockHeight(), 10)),
sdk.NewAttribute(types.AttributeKeyUbiPercentage, strconv.FormatUint(uint64(ev.Percentage), 10)),
sdk.NewAttribute(types.AttributeKeyStatusCode, errors.UnwrapErrCode(err).String()),
),
})
}
}()

newUBI := math.LegacyNewDecWithPrec(int64(ev.Percentage), 4)
if err := k.distrKeeper.SetUbi(ctx, newUBI); err != nil {

if err = k.distrKeeper.SetUbi(ctx, newUBI); errors.Is(err, sdkerrors.ErrInvalidRequest) {
return errors.WrapErrWithCode(errors.InvalidRequest, err)
} else if err != nil {
return errors.Wrap(err, "set new UBI percentage")
}

Expand Down
29 changes: 25 additions & 4 deletions client/x/evmengine/keeper/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package keeper

import (
"context"
"strconv"

upgradetypes "cosmossdk.io/x/upgrade/types"

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

"github.com/piplabs/story/client/x/evmengine/types"
"github.com/piplabs/story/contracts/bindings"
"github.com/piplabs/story/lib/errors"
Expand Down Expand Up @@ -41,13 +45,30 @@ func (k *Keeper) ProcessUpgradeEvents(ctx context.Context, height uint64, logs [
return nil
}

func (k *Keeper) ProcessSoftwareUpgrade(ctx context.Context, ev *bindings.UpgradeEntrypointSoftwareUpgrade) error {
err := k.upgradeKeeper.ScheduleUpgrade(ctx, upgradetypes.Plan{
func (k *Keeper) ProcessSoftwareUpgrade(ctx context.Context, ev *bindings.UpgradeEntrypointSoftwareUpgrade) (err error) {
defer func() {
sdkCtx := sdk.UnwrapSDKContext(ctx)
if err != nil {
sdkCtx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeUpgradeFailure,
sdk.NewAttribute(types.AttributeKeyBlockHeight, strconv.FormatInt(sdkCtx.BlockHeight(), 10)),
sdk.NewAttribute(types.AttributeKeyUpgradeName, ev.Name),
sdk.NewAttribute(types.AttributeKeyUpgradeHeight, strconv.FormatInt(ev.Height, 10)),
sdk.NewAttribute(types.AttributeKeyUpgradeInfo, ev.Info),
sdk.NewAttribute(types.AttributeKeyStatusCode, errors.UnwrapErrCode(err).String()),
),
})
}
}()

if err = k.upgradeKeeper.ScheduleUpgrade(ctx, upgradetypes.Plan{
Name: ev.Name,
Info: ev.Info,
Height: ev.Height,
})
if err != nil {
}); errors.Is(err, sdkerrors.ErrInvalidRequest) {
return errors.WrapErrWithCode(errors.InvalidRequest, err)
} else if err != nil {
return errors.Wrap(err, "process software upgrade: schedule upgrade")
}

Expand Down
14 changes: 14 additions & 0 deletions client/x/evmengine/types/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package types

// evmstaking module event types.
const (
EventTypeUpgradeFailure = "upgrade_failure"
EventTypeUpdateUbiFailure = "update_ubi_failure"

AttributeKeyStatusCode = "status_code"
AttributeKeyBlockHeight = "block_height"
AttributeKeyUpgradeName = "upgrade_name"
AttributeKeyUpgradeHeight = "upgrade_height"
AttributeKeyUpgradeInfo = "upgrade_info"
AttributeKeyUbiPercentage = "ubi_percentage"
)
16 changes: 8 additions & 8 deletions client/x/evmstaking/keeper/delegator_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +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()),
sdk.NewAttribute(types.AttributeKeyStatusCode, errors.UnwrapErrCode(err).String()),
),
})
}
}()

delCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.DelegatorUncmpPubkey)
if err != nil {
return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress delegator pubkey"))
return errors.WrapErrWithCode(errors.InvalidUncmpPubKey, errors.Wrap(err, "compress delegator pubkey"))
}
depositorPubkey, err := k1util.PubKeyBytesToCosmos(delCmpPubkey)
if err != nil {
Expand All @@ -60,15 +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()),
sdk.NewAttribute(types.AttributeKeyStatusCode, errors.UnwrapErrCode(err).String()),
),
})
}
}()

delCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.DelegatorUncmpPubkey)
if err != nil {
return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress delegator pubkey"))
return errors.WrapErrWithCode(errors.InvalidUncmpPubKey, errors.Wrap(err, "compress delegator pubkey"))
}
depositorPubkey, err := k1util.PubKeyBytesToCosmos(delCmpPubkey)
if err != nil {
Expand All @@ -95,15 +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()),
sdk.NewAttribute(types.AttributeKeyStatusCode, errors.UnwrapErrCode(err).String()),
),
})
}
}()

delCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.UncmpPubkey)
if err != nil {
return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress delegator pubkey"))
return errors.WrapErrWithCode(errors.InvalidUncmpPubKey, errors.Wrap(err, "compress delegator pubkey"))
}
depositorPubkey, err := k1util.PubKeyBytesToCosmos(delCmpPubkey)
if err != nil {
Expand All @@ -129,15 +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()),
sdk.NewAttribute(types.AttributeKeyStatusCode, errors.UnwrapErrCode(err).String()),
),
})
}
}()

delCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.UncmpPubkey)
if err != nil {
return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress delegator pubkey"))
return errors.WrapErrWithCode(errors.InvalidUncmpPubKey, errors.Wrap(err, "compress delegator pubkey"))
}
depositorPubkey, err := k1util.PubKeyBytesToCosmos(delCmpPubkey)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions client/x/evmstaking/keeper/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +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()),
sdk.NewAttribute(types.AttributeKeyStatusCode, errors.UnwrapErrCode(err).String()),
),
})
}
}()

delCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.DelegatorUncmpPubkey)
if err != nil {
return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress delegator pubkey"))
return errors.WrapErrWithCode(errors.InvalidUncmpPubKey, errors.Wrap(err, "compress delegator pubkey"))
}
depositorPubkey, err := k1util.PubKeyBytesToCosmos(delCmpPubkey)
if err != nil {
Expand All @@ -48,7 +48,7 @@ func (k Keeper) ProcessDeposit(ctx context.Context, ev *bindings.IPTokenStakingD

valCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.ValidatorUncmpPubkey)
if err != nil {
return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress validator pubkey"))
return errors.WrapErrWithCode(errors.InvalidUncmpPubKey, errors.Wrap(err, "compress validator pubkey"))
}
validatorPubkey, err := k1util.PubKeyBytesToCosmos(valCmpPubkey)
if err != nil {
Expand Down Expand Up @@ -110,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 types.WrapErrWithCode(types.ValidatorNotFound, errors.New("validator not exists"))
return errors.WrapErrWithCode(errors.ValidatorNotFound, errors.New("validator not exists"))
} else if err != nil {
return errors.Wrap(err, "get validator failed")
}
Expand Down Expand Up @@ -143,9 +143,9 @@ func (k Keeper) ProcessDeposit(ctx context.Context, ev *bindings.IPTokenStakingD
)
_, err = skeeperMsgServer.Delegate(ctx, msg)
if errors.Is(err, stypes.ErrDelegationBelowMinimum) {
return types.WrapErrWithCode(types.InvalidDelegationAmount, err)
return errors.WrapErrWithCode(errors.InvalidDelegationAmount, err)
} else if errors.Is(err, stypes.ErrNoPeriodTypeFound) {
return types.WrapErrWithCode(types.InvalidPeriodType, err)
return errors.WrapErrWithCode(errors.InvalidPeriodType, err)
} else if err != nil {
return errors.Wrap(err, "delegate")
}
Expand Down
16 changes: 8 additions & 8 deletions client/x/evmstaking/keeper/redelegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +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()),
sdk.NewAttribute(types.AttributeKeyStatusCode, errors.UnwrapErrCode(err).String()),
),
})
}
Expand All @@ -51,7 +51,7 @@ func (k Keeper) ProcessRedelegate(ctx context.Context, ev *bindings.IPTokenStaki

delCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.DelegatorUncmpPubkey)
if err != nil {
return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress delegator pubkey"))
return errors.WrapErrWithCode(errors.InvalidUncmpPubKey, errors.Wrap(err, "compress delegator pubkey"))
}
depositorPubkey, err := k1util.PubKeyBytesToCosmos(delCmpPubkey)
if err != nil {
Expand All @@ -60,7 +60,7 @@ func (k Keeper) ProcessRedelegate(ctx context.Context, ev *bindings.IPTokenStaki

valSrcCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.ValidatorUncmpSrcPubkey)
if err != nil {
return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress src validator pubkey"))
return errors.WrapErrWithCode(errors.InvalidUncmpPubKey, errors.Wrap(err, "compress src validator pubkey"))
}
validatorSrcPubkey, err := k1util.PubKeyBytesToCosmos(valSrcCmpPubkey)
if err != nil {
Expand All @@ -69,7 +69,7 @@ func (k Keeper) ProcessRedelegate(ctx context.Context, ev *bindings.IPTokenStaki

valDstCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.ValidatorUncmpDstPubkey)
if err != nil {
return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress dst validator pubkey"))
return errors.WrapErrWithCode(errors.InvalidUncmpPubKey, errors.Wrap(err, "compress dst validator pubkey"))
}
validatorDstPubkey, err := k1util.PubKeyBytesToCosmos(valDstCmpPubkey)
if err != nil {
Expand Down Expand Up @@ -97,16 +97,16 @@ 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 types.WrapErrWithCode(
types.InvalidOperator,
return errors.WrapErrWithCode(
errors.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 types.WrapErrWithCode(
types.InvalidOperator,
return errors.WrapErrWithCode(
errors.InvalidOperator,
errors.New("invalid redelegateOnBehalf txn, not from operator"),
)
}
Expand Down
12 changes: 6 additions & 6 deletions client/x/evmstaking/keeper/unjail.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +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()),
sdk.NewAttribute(types.AttributeKeyStatusCode, errors.UnwrapErrCode(err).String()),
),
})
}
}()

valCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.ValidatorUncmpPubkey)
if err != nil {
return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress validator pubkey"))
return errors.WrapErrWithCode(errors.InvalidUncmpPubKey, errors.Wrap(err, "compress validator pubkey"))
}
validatorPubkey, err := k1util.PubKeyBytesToCosmos(valCmpPubkey)
if err != nil {
Expand All @@ -53,16 +53,16 @@ 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 types.WrapErrWithCode(
types.InvalidOperator,
return errors.WrapErrWithCode(
errors.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 types.WrapErrWithCode(
types.InvalidOperator,
return errors.WrapErrWithCode(
errors.InvalidOperator,
errors.New("invalid unjailOnBehalf txn, not from operator"),
)
}
Expand Down
4 changes: 2 additions & 2 deletions client/x/evmstaking/keeper/update_commission.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ func (k Keeper) ProcessUpdateValidatorCommission(ctx context.Context, ev *bindin
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()),
sdk.NewAttribute(types.AttributeKeyStatusCode, errors.UnwrapErrCode(err).String()),
),
})
}
}()

valCmpPubkey, err := UncmpPubKeyToCmpPubKey(ev.ValidatorUncmpPubkey)
if err != nil {
return types.WrapErrWithCode(types.InvalidUncmpPubKey, errors.Wrap(err, "compress validator pubkey"))
return errors.WrapErrWithCode(errors.InvalidUncmpPubKey, errors.Wrap(err, "compress validator pubkey"))
}
validatorPubkey, err := k1util.PubKeyBytesToCosmos(valCmpPubkey)
if err != nil {
Expand Down
Loading

0 comments on commit 18ceefb

Please sign in to comment.