Skip to content

Commit

Permalink
add validation for delegate/undelegate msg
Browse files Browse the repository at this point in the history
  • Loading branch information
leonz789 committed Sep 11, 2024
1 parent 265ad84 commit f34bb2c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
4 changes: 4 additions & 0 deletions x/delegation/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,8 @@ var (
ModuleName, 17,
"the client chain has not been registered",
)
ErrInvalidAssetID = errorsmod.Register(
ModuleName, 18,
"assetID is invalid",
)
)
30 changes: 22 additions & 8 deletions x/delegation/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
errorsmod "cosmossdk.io/errors"
assetstype "github.com/ExocoreNetwork/exocore/x/assets/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -18,10 +19,7 @@ func (m *MsgDelegation) GetSigners() []sdk.AccAddress {

// ValidateBasic does a sanity check of the provided data
func (m *MsgDelegation) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.BaseInfo.FromAddress); err != nil {
return errorsmod.Wrap(err, "invalid from address")
}
return nil
return validateDelegationInfo(m.AssetID, m.BaseInfo)
}

// new message to delegate asset to operator
Expand Down Expand Up @@ -52,10 +50,7 @@ func (m *MsgUndelegation) GetSigners() []sdk.AccAddress {

// ValidateBasic does a sanity check of the provided data
func (m *MsgUndelegation) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.BaseInfo.FromAddress); err != nil {
return errorsmod.Wrap(err, "invalid from address")
}
return nil
return validateDelegationInfo(m.AssetID, m.BaseInfo)
}

// GetSignBytes implements the LegacyMsg interface.
Expand All @@ -77,3 +72,22 @@ func NewMsgUndelegation(assetID, fromAddress string, amountPerOperator []KeyValu
BaseInfo: baseInfo,
}
}

// TODO: delegation and undelegation have the same params, try to use one single message with different flag to indicate action:delegation/undelegation
func validateDelegationInfo(assetID string, baseInfo *DelegationIncOrDecInfo) error {
for _, kv := range baseInfo.PerOperatorAmounts {
if _, err := sdk.AccAddressFromBech32(kv.Key); err != nil {
return errorsmod.Wrap(err, "invalid operator address delegateTO")
}
if !kv.Value.Amount.IsPositive() {
return errorsmod.Wrapf(ErrAmountIsNotPositive, "amount should be positive, got%s", kv.Value.Amount.String())
}
}
if assetID != assetstype.NativeAssetID {
return errorsmod.Wrapf(ErrInvalidAssetID, "only nativeToken is support, expected:%s,got:%s", assetstype.NativeAssetID, assetID)
}
if _, err := sdk.AccAddressFromBech32(baseInfo.FromAddress); err != nil {
return errorsmod.Wrap(err, "invalid from address")
}
return nil
}

0 comments on commit f34bb2c

Please sign in to comment.