Skip to content

Commit

Permalink
chore: more validation on host-chain (#733)
Browse files Browse the repository at this point in the history
* add more validation

* lint

* add CHANGELOG.md

* add CHANGELOG.md

* more tests
  • Loading branch information
puneet2019 authored Jan 22, 2024
1 parent 869a03e commit ca1b386
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 27 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Features

- [729](https://github.com/persistenceOne/pstake-native/pull/729) Add rewards account query (hence autocompound) OnChanOpenAck.
- [733](https://github.com/persistenceOne/pstake-native/pull/733) Add more validation for host-chain
- [729](https://github.com/persistenceOne/pstake-native/pull/729) Add rewards account query (hence autocompound)
OnChanOpenAck.
- [727](https://github.com/persistenceOne/pstake-native/pull/727) Send LSM redeem messages in chunks.
- [721](https://github.com/persistenceOne/pstake-native/pull/721) Add Query host chain user unbondings.

### Bug Fixes

- [731](https://github.com/persistenceOne/pstake-native/pull/731) Set limit to LSM deposit filtering.
- [730](https://github.com/persistenceOne/pstake-native/pull/730) Fix deposit validate.
- [730](https://github.com/persistenceOne/pstake-native/pull/730) Fix deposit validate.
- [728](https://github.com/persistenceOne/pstake-native/pull/728) Fix prevent users from liquid-staking funds by
removing the Deposit entry.
- [726](https://github.com/persistenceOne/pstake-native/pull/726) Fix minimal unbondings.
Expand Down
12 changes: 0 additions & 12 deletions x/liquidstakeibc/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,18 +285,6 @@ func ValidGenesis() *types.GenesisState {
HostDenom: "uatom",
ChannelId: "channel-1",
PortId: "transfer",
DelegationAccount: &types.ICAAccount{
Address: "",
Balance: sdk.Coin{},
Owner: "",
ChannelState: 0,
},
RewardsAccount: &types.ICAAccount{
Address: "",
Balance: sdk.Coin{},
Owner: "",
ChannelState: 0,
},
Validators: []*types.Validator{{
OperatorAddress: authtypes.NewModuleAddressOrBech32Address("testval").String(),
Status: stakingtypes.BondStatusBonded,
Expand Down
75 changes: 74 additions & 1 deletion x/liquidstakeibc/types/liquidstakeibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"fmt"
"strings"

"github.com/cometbft/cometbft/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types"
host "github.com/cosmos/ibc-go/v7/modules/core/24-host"
)

func IsLiquidStakingDenom(denom string) bool {
Expand Down Expand Up @@ -76,16 +79,86 @@ func (hc *HostChain) Validate() error {
if hc.CValue.LT(sdk.ZeroDec()) { // GT limits should be checked by module level params, invariants.
return fmt.Errorf("host chain %s has c value out of bounds: %d", hc.ChainId, hc.CValue)
}

if strings.TrimSpace(hc.ChainId) == "" {
return fmt.Errorf("chain_id must be non-empty")
}
if len(hc.ChainId) > types.MaxChainIDLen {
return fmt.Errorf("chain_id is too long (max: %d)", types.MaxChainIDLen)
}
err = host.ConnectionIdentifierValidator(hc.ConnectionId)
if err != nil {
return fmt.Errorf("hostchain connectionID invalid err: %v", err)
}
err = host.PortIdentifierValidator(hc.PortId)
if err != nil {
return err
}
err = host.ChannelIdentifierValidator(hc.ChannelId)
if err != nil {
return err
}
if hc.DelegationAccount != nil {
err = hc.DelegationAccount.Validate()
if err != nil {
return err
}
}
if hc.RewardsAccount != nil {
err = hc.RewardsAccount.Validate()
if err != nil {
return err
}
}
for _, validator := range hc.Validators {
err := validator.Validate()
if err != nil {
return fmt.Errorf("host chain %s validator is invalid, err: %s", hc.ChainId, err)
}
}
if hc.RewardParams != nil {
err = hc.RewardParams.Validate()
if err != nil {
return err
}
}
return nil
}

func (icaAccount *ICAAccount) Validate() error {
if icaAccount.ChannelState != ICAAccount_ICA_CHANNEL_CREATING &&
icaAccount.ChannelState != ICAAccount_ICA_CHANNEL_CREATED {
return fmt.Errorf("invalid channel state")
}
portID, err := icatypes.NewControllerPortID(icaAccount.Owner)
if err != nil {
return err
}
err = host.PortIdentifierValidator(portID)
if err != nil {
return err
}
err = icaAccount.Balance.Validate()
if err != nil {
return err
}
if icaAccount.Address != "" {
_, _, err = bech32.DecodeAndConvert(icaAccount.Address)
if err != nil {
return err
}
}

return nil
}

func (rewardParams *RewardParams) Validate() error {
_, _, err := bech32.DecodeAndConvert(rewardParams.Destination)
if err != nil {
return err
}
return sdk.ValidateDenom(rewardParams.Denom)
}

func (params *HostChainLSParams) Validate() error {
if params.DepositFee.LT(sdk.ZeroDec()) || params.DepositFee.GT(sdk.OneDec()) {
return fmt.Errorf("host chain lsparams has invalid deposit fee, should be 0<=fee<=1")
Expand Down
Loading

0 comments on commit ca1b386

Please sign in to comment.