forked from cerc-io/laconicd
Add module invariants and input validations (#14)
Reviewed-on: deep-stack/laconic2d#14 Co-authored-by: Prathamesh Musale <prathamesh.musale0@gmail.com> Co-committed-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package keeper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
|
||||
types "git.vdb.to/cerc-io/laconic2d/x/bond"
|
||||
)
|
||||
|
||||
// RegisterInvariants registers all bond invariants
|
||||
func RegisterInvariants(ir sdk.InvariantRegistry, k *Keeper) {
|
||||
ir.RegisterRoute(types.ModuleName, "module-account", ModuleAccountInvariant(k))
|
||||
}
|
||||
|
||||
// AllInvariants runs all invariants of the bond module.
|
||||
func AllInvariants(k *Keeper) sdk.Invariant {
|
||||
return func(ctx sdk.Context) (string, bool) {
|
||||
return ModuleAccountInvariant(k)(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// ModuleAccountInvariant checks that the 'bond' module account balance is non-negative.
|
||||
func ModuleAccountInvariant(k *Keeper) sdk.Invariant {
|
||||
return func(ctx sdk.Context) (string, bool) {
|
||||
moduleAddress := k.accountKeeper.GetModuleAddress(types.ModuleName)
|
||||
if k.bankKeeper.GetAllBalances(ctx, moduleAddress).IsAnyNegative() {
|
||||
return sdk.FormatInvariant(
|
||||
types.ModuleName,
|
||||
"module-account",
|
||||
fmt.Sprintf("Module account '%s' has negative balance.", types.ModuleName),
|
||||
), true
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,10 @@ func NewMsgServerImpl(keeper *Keeper) bond.MsgServer {
|
||||
}
|
||||
|
||||
func (ms msgServer) CreateBond(c context.Context, msg *bond.MsgCreateBond) (*bond.MsgCreateBondResponse, error) {
|
||||
if err := msg.ValidateBasic(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ctx := sdk.UnwrapSDKContext(c)
|
||||
|
||||
signerAddress, err := sdk.AccAddressFromBech32(msg.Signer)
|
||||
@@ -50,6 +54,10 @@ func (ms msgServer) CreateBond(c context.Context, msg *bond.MsgCreateBond) (*bon
|
||||
|
||||
// RefillBond implements bond.MsgServer.
|
||||
func (ms msgServer) RefillBond(c context.Context, msg *bond.MsgRefillBond) (*bond.MsgRefillBondResponse, error) {
|
||||
if err := msg.ValidateBasic(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ctx := sdk.UnwrapSDKContext(c)
|
||||
|
||||
signerAddress, err := sdk.AccAddressFromBech32(msg.Signer)
|
||||
@@ -81,6 +89,10 @@ func (ms msgServer) RefillBond(c context.Context, msg *bond.MsgRefillBond) (*bon
|
||||
|
||||
// WithdrawBond implements bond.MsgServer.
|
||||
func (ms msgServer) WithdrawBond(c context.Context, msg *bond.MsgWithdrawBond) (*bond.MsgWithdrawBondResponse, error) {
|
||||
if err := msg.ValidateBasic(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ctx := sdk.UnwrapSDKContext(c)
|
||||
|
||||
signerAddress, err := sdk.AccAddressFromBech32(msg.Signer)
|
||||
@@ -112,11 +124,17 @@ func (ms msgServer) WithdrawBond(c context.Context, msg *bond.MsgWithdrawBond) (
|
||||
|
||||
// CancelBond implements bond.MsgServer.
|
||||
func (ms msgServer) CancelBond(c context.Context, msg *bond.MsgCancelBond) (*bond.MsgCancelBondResponse, error) {
|
||||
if err := msg.ValidateBasic(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ctx := sdk.UnwrapSDKContext(c)
|
||||
|
||||
signerAddress, err := sdk.AccAddressFromBech32(msg.Signer)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = ms.k.CancelBond(ctx, msg.Id, signerAddress)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -24,6 +24,7 @@ var (
|
||||
_ module.HasGenesis = AppModule{}
|
||||
_ module.HasServices = AppModule{}
|
||||
_ module.HasConsensusVersion = AppModule{}
|
||||
_ module.HasInvariants = AppModule{}
|
||||
)
|
||||
|
||||
// ConsensusVersion defines the current module consensus version
|
||||
@@ -116,3 +117,9 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
|
||||
bond.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
|
||||
bond.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServerImpl(am.keeper))
|
||||
}
|
||||
|
||||
// module.HasInvariants
|
||||
|
||||
func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
|
||||
keeper.RegisterInvariants(ir, am.keeper)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package bond
|
||||
|
||||
import (
|
||||
errorsmod "cosmossdk.io/errors"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -15,3 +17,49 @@ func NewMsgCreateBond(coins sdk.Coins, signer sdk.AccAddress) MsgCreateBond {
|
||||
Signer: signer.String(),
|
||||
}
|
||||
}
|
||||
|
||||
func (msg MsgCreateBond) ValidateBasic() error {
|
||||
if len(msg.Signer) == 0 {
|
||||
return errorsmod.Wrap(sdkerrors.ErrInvalidAddress, msg.Signer)
|
||||
}
|
||||
if len(msg.Coins) == 0 || !msg.Coins.IsValid() {
|
||||
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, "Invalid amount.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (msg MsgRefillBond) ValidateBasic() error {
|
||||
if len(msg.Id) == 0 {
|
||||
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, msg.Id)
|
||||
}
|
||||
if len(msg.Signer) == 0 {
|
||||
return errorsmod.Wrap(sdkerrors.ErrInvalidAddress, msg.Signer)
|
||||
}
|
||||
if len(msg.Coins) == 0 || !msg.Coins.IsValid() {
|
||||
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, "Invalid amount.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (msg MsgWithdrawBond) ValidateBasic() error {
|
||||
if len(msg.Id) == 0 {
|
||||
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, msg.Id)
|
||||
}
|
||||
if len(msg.Signer) == 0 {
|
||||
return errorsmod.Wrap(sdkerrors.ErrInvalidAddress, msg.Signer)
|
||||
}
|
||||
if len(msg.Coins) == 0 || !msg.Coins.IsValid() {
|
||||
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, "Invalid amount.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (msg MsgCancelBond) ValidateBasic() error {
|
||||
if len(msg.Id) == 0 {
|
||||
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, msg.Id)
|
||||
}
|
||||
if len(msg.Signer) == 0 {
|
||||
return errorsmod.Wrap(sdkerrors.ErrInvalidAddress, msg.Signer)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user