From a2414acea326ef096dad81aa8a8089f4c2e9818b Mon Sep 17 00:00:00 2001 From: GnaD13 Date: Mon, 29 Jan 2024 15:16:29 +0700 Subject: [PATCH 1/7] add validate for genesis --- x/multi-staking/module.go | 8 ++++++- x/multi-staking/types/errors.go | 7 ++++-- x/multi-staking/types/genesis.go | 35 +++++++++++++++++++++++++++++ x/multi-staking/types/lock.go | 4 ++++ x/multi-staking/types/unlock.go | 38 ++++++++++++++++++++++---------- 5 files changed, 77 insertions(+), 15 deletions(-) diff --git a/x/multi-staking/module.go b/x/multi-staking/module.go index 3ddd8f15..06f2dfe8 100644 --- a/x/multi-staking/module.go +++ b/x/multi-staking/module.go @@ -54,11 +54,17 @@ func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { } // ValidateGenesis validate genesis state for feeabs module -func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error { +func (am AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error { var genState multistakingtypes.GenesisState if err := cdc.UnmarshalJSON(bz, &genState); err != nil { return fmt.Errorf("failed to unmarshal %s genesis state: %w", multistakingtypes.ModuleName, err) } + + // validate staking genesis + if err := staking.ValidateGenesis(&genState.StakingGenesisState); err != nil { + return err + } + return genState.Validate() } diff --git a/x/multi-staking/types/errors.go b/x/multi-staking/types/errors.go index 48df793c..b92613f7 100644 --- a/x/multi-staking/types/errors.go +++ b/x/multi-staking/types/errors.go @@ -6,6 +6,9 @@ import ( // x/multistaking module sentinel errors var ( - ErrInvalidAddMultiStakingCoinProposal = sdkerrors.Register(ModuleName, 2, "invalid add multi staking coin proposal") - ErrInvalidUpdateBondWeightProposal = sdkerrors.Register(ModuleName, 3, "invalid update bond weight proposal") + ErrInvalidAddMultiStakingCoinProposal = sdkerrors.Register(ModuleName, 2, "invalid add multi staking coin proposal") + ErrInvalidUpdateBondWeightProposal = sdkerrors.Register(ModuleName, 3, "invalid update bond weight proposal") + ErrInvalidTotalMultiStakingLocks = sdkerrors.Register(ModuleName, 4, "invalid total multi-staking lock") + ErrInvalidTotalMultiStakingUnlocks = sdkerrors.Register(ModuleName, 5, "invalid total multi-staking unlock") + ErrInvalidMultiStakingUnlocksCreationHeight = sdkerrors.Register(ModuleName, 6, "invalid unlock creation height") ) diff --git a/x/multi-staking/types/genesis.go b/x/multi-staking/types/genesis.go index 7919a8de..1968af04 100644 --- a/x/multi-staking/types/genesis.go +++ b/x/multi-staking/types/genesis.go @@ -1,6 +1,7 @@ package types import ( + "cosmossdk.io/errors" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -15,5 +16,39 @@ func DefaultGenesis() *GenesisState { // Validate performs basic genesis state validation, returning an error upon any failure. // TODO: Add validate genesis func (gs GenesisState) Validate() error { + // validate locks + if len(gs.MultiStakingLocks) != len(gs.StakingGenesisState.Delegations) { + return errors.Wrapf( + ErrInvalidTotalMultiStakingLocks, + "locks and delegations not equal: locks: %v delegations: %v", + len(gs.MultiStakingLocks), + len(gs.StakingGenesisState.Delegations), + ) + } + + for _, lock := range gs.MultiStakingLocks { + err := lock.Validate() + if err != nil { + return err + } + } + + // validate unlocks + if len(gs.MultiStakingUnlocks) != len(gs.StakingGenesisState.UnbondingDelegations) { + return errors.Wrapf( + ErrInvalidTotalMultiStakingUnlocks, + "unlocks and unbondingdelegations not equal: unlocks: %v unbondingdelegations: %v", + len(gs.MultiStakingUnlocks), + len(gs.StakingGenesisState.UnbondingDelegations), + ) + } + + for _, unlock := range gs.MultiStakingUnlocks { + err := unlock.Validate() + if err != nil { + return err + } + } + return nil } diff --git a/x/multi-staking/types/lock.go b/x/multi-staking/types/lock.go index 0549f867..2ced1cf3 100644 --- a/x/multi-staking/types/lock.go +++ b/x/multi-staking/types/lock.go @@ -13,6 +13,10 @@ func NewMultiStakingLock(lockID LockID, lockedCoin MultiStakingCoin) MultiStakin } } +func (lock MultiStakingLock) Validate() error { + return lock.LockedCoin.Validate() +} + func (lock MultiStakingLock) MultiStakingCoin(withAmount math.Int) MultiStakingCoin { return lock.LockedCoin.WithAmount(withAmount) } diff --git a/x/multi-staking/types/unlock.go b/x/multi-staking/types/unlock.go index 1010c25b..9e477184 100644 --- a/x/multi-staking/types/unlock.go +++ b/x/multi-staking/types/unlock.go @@ -23,6 +23,18 @@ func (e UnlockEntry) String() string { return string(out) } +func (u UnlockEntry) GetBondWeight() sdk.Dec { + return u.UnlockingCoin.BondWeight +} + +func (unlockEntry UnlockEntry) UnbondAmountToUnlockAmount(unbondAmount math.Int) math.Int { + return sdk.NewDecFromInt(unbondAmount).Quo(unlockEntry.GetBondWeight()).TruncateInt() +} + +func (unlockEntry UnlockEntry) UnlockAmountToUnbondAmount(unlockAmount math.Int) math.Int { + return unlockEntry.GetBondWeight().MulInt(unlockAmount).TruncateInt() +} + // NewMultiStakingUnlock - create a new MultiStaking unlock object // //nolint:interfacer @@ -37,6 +49,20 @@ func NewMultiStakingUnlock( } } +func (unlock MultiStakingUnlock) Validate() error { + for _, entry := range unlock.Entries { + if entry.CreationHeight <= 0 { + return ErrInvalidMultiStakingUnlocksCreationHeight + } + + if err := entry.UnlockingCoin.Validate(); err != nil { + return err + } + } + + return nil +} + func (unlock *MultiStakingUnlock) FindEntryIndexByHeight(creationHeight int64) (int, bool) { for index, unlockEntry := range unlock.Entries { if unlockEntry.CreationHeight == creationHeight { @@ -101,18 +127,6 @@ func (unlock *MultiStakingUnlock) RemoveEntryAtCreationHeight(creationHeight int } } -func (u UnlockEntry) GetBondWeight() sdk.Dec { - return u.UnlockingCoin.BondWeight -} - -func (unlockEntry UnlockEntry) UnbondAmountToUnlockAmount(unbondAmount math.Int) math.Int { - return sdk.NewDecFromInt(unbondAmount).Quo(unlockEntry.GetBondWeight()).TruncateInt() -} - -func (unlockEntry UnlockEntry) UnlockAmountToUnbondAmount(unlockAmount math.Int) math.Int { - return unlockEntry.GetBondWeight().MulInt(unlockAmount).TruncateInt() -} - // String returns a human readable string representation of an MultiStakingUnlock. func (unlock MultiStakingUnlock) String() string { out := fmt.Sprintf(`Unlock ID: %s From 27f1baf869949f89d5e57fbeaa4dc8dcfd3fce7a Mon Sep 17 00:00:00 2001 From: GnaD13 Date: Mon, 29 Jan 2024 15:30:24 +0700 Subject: [PATCH 2/7] nit --- x/multi-staking/module.go | 17 ++++++----------- x/multi-staking/types/genesis.go | 6 ++++++ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/x/multi-staking/module.go b/x/multi-staking/module.go index 06f2dfe8..802ce510 100644 --- a/x/multi-staking/module.go +++ b/x/multi-staking/module.go @@ -48,23 +48,18 @@ func (am AppModuleBasic) RegisterInterfaces(reg cdctypes.InterfaceRegistry) { am.stakingAppModBasic.RegisterInterfaces(reg) } -// DefaultGenesis returns feeabs module default genesis state. +// DefaultGenesis returns multi-staking module default genesis state. func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { return cdc.MustMarshalJSON(multistakingtypes.DefaultGenesis()) } -// ValidateGenesis validate genesis state for feeabs module +// ValidateGenesis validate genesis state for multi-staking module func (am AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error { var genState multistakingtypes.GenesisState if err := cdc.UnmarshalJSON(bz, &genState); err != nil { return fmt.Errorf("failed to unmarshal %s genesis state: %w", multistakingtypes.ModuleName, err) } - // validate staking genesis - if err := staking.ValidateGenesis(&genState.StakingGenesisState); err != nil { - return err - } - return genState.Validate() } @@ -148,7 +143,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { stakingtypes.RegisterQueryServer(cfg.QueryServer(), querier) } -// InitGenesis initial genesis state for feeabs module +// InitGenesis initial genesis state for multi-staking module func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate { var genesisState multistakingtypes.GenesisState cdc.MustUnmarshalJSON(data, &genesisState) @@ -156,18 +151,18 @@ func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json. return am.keeper.InitGenesis(ctx, genesisState) } -// ExportGenesis export feeabs state as raw message for feeabs module +// ExportGenesis export multi-staking state as raw message for multi-staking module func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { gs := am.keeper.ExportGenesis(ctx) return cdc.MustMarshalJSON(gs) } -// BeginBlock returns the begin blocker for the feeabs module. +// BeginBlock returns the begin blocker for the multi-staking module. func (am AppModule) BeginBlock(ctx sdk.Context, requestBeginBlock abci.RequestBeginBlock) { am.skAppModule.BeginBlock(ctx, requestBeginBlock) } -// EndBlock returns the end blocker for the feeabs module. It returns no validator +// EndBlock returns the end blocker for the multi-staking module. It returns no validator // updates. func (am AppModule) EndBlock(ctx sdk.Context, requestEndBlock abci.RequestEndBlock) []abci.ValidatorUpdate { // calculate the amount of coin diff --git a/x/multi-staking/types/genesis.go b/x/multi-staking/types/genesis.go index 1968af04..b5dceec6 100644 --- a/x/multi-staking/types/genesis.go +++ b/x/multi-staking/types/genesis.go @@ -2,6 +2,7 @@ package types import ( "cosmossdk.io/errors" + "github.com/cosmos/cosmos-sdk/x/staking" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) @@ -16,6 +17,11 @@ func DefaultGenesis() *GenesisState { // Validate performs basic genesis state validation, returning an error upon any failure. // TODO: Add validate genesis func (gs GenesisState) Validate() error { + // validate staking genesis + if err := staking.ValidateGenesis(&gs.StakingGenesisState); err != nil { + return err + } + // validate locks if len(gs.MultiStakingLocks) != len(gs.StakingGenesisState.Delegations) { return errors.Wrapf( From 5a75e659a5b707f85f195b16c2b1c1650d46c395 Mon Sep 17 00:00:00 2001 From: GnaD13 Date: Mon, 29 Jan 2024 15:33:17 +0700 Subject: [PATCH 3/7] add validate genesis --- x/multi-staking/keeper/genesis.go | 5 +++++ x/multi-staking/types/genesis.go | 1 - 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/x/multi-staking/keeper/genesis.go b/x/multi-staking/keeper/genesis.go index 6230ff10..cf9f2eba 100644 --- a/x/multi-staking/keeper/genesis.go +++ b/x/multi-staking/keeper/genesis.go @@ -8,6 +8,11 @@ import ( ) func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) (res []abci.ValidatorUpdate) { + // validate genesis + if err := data.Validate(); err != nil { + panic(err) + } + // multi-staking state for _, multiStakingLock := range data.MultiStakingLocks { k.SetMultiStakingLock(ctx, multiStakingLock) diff --git a/x/multi-staking/types/genesis.go b/x/multi-staking/types/genesis.go index b5dceec6..4a0b5495 100644 --- a/x/multi-staking/types/genesis.go +++ b/x/multi-staking/types/genesis.go @@ -15,7 +15,6 @@ func DefaultGenesis() *GenesisState { } // Validate performs basic genesis state validation, returning an error upon any failure. -// TODO: Add validate genesis func (gs GenesisState) Validate() error { // validate staking genesis if err := staking.ValidateGenesis(&gs.StakingGenesisState); err != nil { From 05dc5e0c57d14749e23d595d6aae8413f17a931d Mon Sep 17 00:00:00 2001 From: GnaD13 Date: Mon, 29 Jan 2024 15:34:45 +0700 Subject: [PATCH 4/7] nit --- x/multi-staking/types/genesis.go | 1 + 1 file changed, 1 insertion(+) diff --git a/x/multi-staking/types/genesis.go b/x/multi-staking/types/genesis.go index 4a0b5495..d8e8dedd 100644 --- a/x/multi-staking/types/genesis.go +++ b/x/multi-staking/types/genesis.go @@ -2,6 +2,7 @@ package types import ( "cosmossdk.io/errors" + "github.com/cosmos/cosmos-sdk/x/staking" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) From 1793691c69a2a6f25ab997b01f39cb72bfa3643f Mon Sep 17 00:00:00 2001 From: GnaD13 Date: Mon, 29 Jan 2024 18:41:10 +0700 Subject: [PATCH 5/7] nit --- x/multi-staking/keeper/genesis.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/x/multi-staking/keeper/genesis.go b/x/multi-staking/keeper/genesis.go index cf9f2eba..6230ff10 100644 --- a/x/multi-staking/keeper/genesis.go +++ b/x/multi-staking/keeper/genesis.go @@ -8,11 +8,6 @@ import ( ) func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) (res []abci.ValidatorUpdate) { - // validate genesis - if err := data.Validate(); err != nil { - panic(err) - } - // multi-staking state for _, multiStakingLock := range data.MultiStakingLocks { k.SetMultiStakingLock(ctx, multiStakingLock) From d5906fb1edc45ab7c8a4af842dddc6825a21670a Mon Sep 17 00:00:00 2001 From: Hieu Vu Date: Mon, 29 Jan 2024 18:47:51 +0700 Subject: [PATCH 6/7] addtional lock id validate --- x/multi-staking/types/lock.go | 6 ++++++ x/multi-staking/types/unlock.go | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/x/multi-staking/types/lock.go b/x/multi-staking/types/lock.go index 2ced1cf3..78eadf5b 100644 --- a/x/multi-staking/types/lock.go +++ b/x/multi-staking/types/lock.go @@ -14,6 +14,12 @@ func NewMultiStakingLock(lockID LockID, lockedCoin MultiStakingCoin) MultiStakin } func (lock MultiStakingLock) Validate() error { + if _, err := sdk.AccAddressFromBech32(lock.LockID.MultiStakerAddr); err != nil { + return err + } + if _, err := sdk.ValAddressFromBech32(lock.LockID.ValAddr); err != nil { + return err + } return lock.LockedCoin.Validate() } diff --git a/x/multi-staking/types/unlock.go b/x/multi-staking/types/unlock.go index 9e477184..d10aa7f7 100644 --- a/x/multi-staking/types/unlock.go +++ b/x/multi-staking/types/unlock.go @@ -50,6 +50,12 @@ func NewMultiStakingUnlock( } func (unlock MultiStakingUnlock) Validate() error { + if _, err := sdk.AccAddressFromBech32(unlock.UnlockID.MultiStakerAddr); err != nil { + return err + } + if _, err := sdk.ValAddressFromBech32(unlock.UnlockID.ValAddr); err != nil { + return err + } for _, entry := range unlock.Entries { if entry.CreationHeight <= 0 { return ErrInvalidMultiStakingUnlocksCreationHeight From caa5c2af3fe442cafe3129612ef43a11b9dba21b Mon Sep 17 00:00:00 2001 From: Hieu Vu Date: Mon, 29 Jan 2024 18:49:17 +0700 Subject: [PATCH 7/7] linting :((( --- x/multi-staking/types/genesis.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/x/multi-staking/types/genesis.go b/x/multi-staking/types/genesis.go index f3c1cb8e..b484b65a 100644 --- a/x/multi-staking/types/genesis.go +++ b/x/multi-staking/types/genesis.go @@ -3,10 +3,8 @@ package types import ( "cosmossdk.io/errors" - "github.com/cosmos/cosmos-sdk/x/staking" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" - + "github.com/cosmos/cosmos-sdk/x/staking" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" )