Skip to content

Commit

Permalink
chore: refine crosschain module (#146)
Browse files Browse the repository at this point in the history
* chore: refine crosschain module

* fix comments

* check init module balance

---------

Co-authored-by: yutianwu <[email protected]>
  • Loading branch information
owen-reorg and yutianwu authored Mar 23, 2023
1 parent a6cfc5d commit 461b280
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 57 deletions.
9 changes: 8 additions & 1 deletion proto/cosmos/crosschain/v1/crosschain.proto
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
syntax = "proto3";
package cosmos.crosschain.v1;

import "cosmos_proto/cosmos.proto";
import "gogoproto/gogo.proto";

option go_package = "github.com/cosmos/cosmos-sdk/x/crosschain/types";

// Params holds parameters for the cross chain module.
message Params {
// initial balance to mint for crosschain module when the chain starts
string init_module_balance = 1;
string init_module_balance = 1 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}
15 changes: 14 additions & 1 deletion x/crosschain/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/crosschain/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

var _ types.QueryServer = Keeper{}
Expand All @@ -19,8 +21,11 @@ func (k Keeper) Params(c context.Context, _ *types.QueryParamsRequest) (*types.Q

// CrossChainPackage returns the specified cross chain package
func (k Keeper) CrossChainPackage(c context.Context, req *types.QueryCrossChainPackageRequest) (*types.QueryCrossChainPackageResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(c)
pack, err := k.GetCrossChainPackage(ctx, sdk.ChannelID(req.ChannelId), req.Sequence)
if err != nil {
return nil, err
Expand All @@ -32,6 +37,10 @@ func (k Keeper) CrossChainPackage(c context.Context, req *types.QueryCrossChainP

// SendSequence returns the send sequence of the channel
func (k Keeper) SendSequence(c context.Context, req *types.QuerySendSequenceRequest) (*types.QuerySendSequenceResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(c)
sequence := k.GetSendSequence(ctx, sdk.ChannelID(req.ChannelId))

Expand All @@ -42,6 +51,10 @@ func (k Keeper) SendSequence(c context.Context, req *types.QuerySendSequenceRequ

// ReceiveSequence returns the receive sequence of the channel
func (k Keeper) ReceiveSequence(c context.Context, req *types.QueryReceiveSequenceRequest) (*types.QueryReceiveSequenceResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(c)
sequence := k.GetReceiveSequence(ctx, sdk.ChannelID(req.ChannelId))

Expand Down
15 changes: 6 additions & 9 deletions x/crosschain/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"math/big"

sdkmath "cosmossdk.io/math"
"github.com/tendermint/tendermint/libs/log"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -56,22 +57,18 @@ func (k Keeper) InitGenesis(ctx sdk.Context, state *types.GenesisState, bankKeep

err := bankKeeper.MintCoins(ctx, types.ModuleName, sdk.Coins{sdk.Coin{
Denom: bondDenom,
Amount: sdk.NewIntFromBigInt(initModuleBalance),
Amount: initModuleBalance,
}})
if err != nil {
panic(fmt.Sprintf("mint initial cross chain module balance error, err=%s", err.Error()))
}
}

// GetInitModuleBalance returns the initial balance of cross chain module
func (k Keeper) GetInitModuleBalance(ctx sdk.Context) *big.Int {
var initModuleBlaanceParam string
k.paramSpace.Get(ctx, types.KeyParamInitModuleBalance, &initModuleBlaanceParam)
moduleBalance, valid := big.NewInt(0).SetString(initModuleBlaanceParam, 10)
if !valid {
panic(fmt.Errorf("invalid init module balance: %s", initModuleBlaanceParam))
}
return moduleBalance
func (k Keeper) GetInitModuleBalance(ctx sdk.Context) sdkmath.Int {
var initModuleBalanceParam sdkmath.Int
k.paramSpace.Get(ctx, types.KeyParamInitModuleBalance, &initModuleBalanceParam)
return initModuleBalanceParam
}

// SetParams sets the params of cross chain module
Expand Down
59 changes: 31 additions & 28 deletions x/crosschain/types/crosschain.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 2 additions & 8 deletions x/crosschain/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

import (
"fmt"
"math/big"
)

// NewGenesisState creates a new GenesisState object
Expand All @@ -23,13 +22,8 @@ func DefaultGenesisState() *GenesisState {

// ValidateGenesis validates the cross chain genesis parameters
func ValidateGenesis(data GenesisState) error {
balance, valid := big.NewInt(0).SetString(data.Params.InitModuleBalance, 10)
if !valid {
return fmt.Errorf("invalid module balance, is %s", data.Params.InitModuleBalance)
}

if balance.Cmp(big.NewInt(0)) < 0 {
return fmt.Errorf("init module balance should be positive, is %s", balance.String())
if data.Params.InitModuleBalance.IsNil() || !data.Params.InitModuleBalance.IsPositive() {
return fmt.Errorf("init module balance should be positive, is %s", data.Params.InitModuleBalance.String())
}

return nil
Expand Down
23 changes: 15 additions & 8 deletions x/crosschain/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ package types

import (
"fmt"
"math/big"

sdkmath "cosmossdk.io/math"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)

const DefaultInitModuleBalance string = "2000000000000000000000000" // 2M
var DefaultInitModuleBalance sdkmath.Int

func init() {
initModuleBalance, ok := sdkmath.NewIntFromString("2000000000000000000000000") // 2M
if !ok {
panic("invalid init module balance")
}
DefaultInitModuleBalance = initModuleBalance
}

var KeyParamInitModuleBalance = []byte("InitModuleBalance")

Expand All @@ -29,18 +37,17 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
}

func validateInitModuleBalance(i interface{}) error {
v, ok := i.(string)
v, ok := i.(sdkmath.Int)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}

balance, valid := big.NewInt(0).SetString(v, 10)
if !valid {
return fmt.Errorf("invalid module balance, is %s", v)
if v.IsNil() {
return fmt.Errorf("init module balance should not be nil")
}

if balance.Cmp(big.NewInt(0)) < 0 {
return fmt.Errorf("init module balance should be positive, is %s", v)
if !v.IsPositive() {
return fmt.Errorf("init module balance should be positive, is %s", v.String())
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions x/oracle/types/oracle.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 461b280

Please sign in to comment.