Skip to content

Commit

Permalink
[Gateway] Scaffold the unstake gateway message and nothing else (#67)
Browse files Browse the repository at this point in the history
Ran
```
ignite scaffold message unstake-gateway --module gateway --signer address --yes
```
  • Loading branch information
h5law authored Oct 16, 2023
1 parent e403777 commit b0678e0
Show file tree
Hide file tree
Showing 11 changed files with 215 additions and 9 deletions.
2 changes: 2 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75945,6 +75945,8 @@ definitions:
type: string
pocket.gateway.MsgStakeGatewayResponse:
type: object
pocket.gateway.MsgUnstakeGatewayResponse:
type: object
pocket.gateway.Params:
type: object
description: Params defines the parameters for the module.
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ require (
cosmossdk.io/api v0.3.1
github.com/cometbft/cometbft v0.37.2
github.com/cometbft/cometbft-db v0.8.0
github.com/cosmos/cosmos-proto v1.0.0-beta.2
github.com/cosmos/cosmos-sdk v0.47.3
github.com/cosmos/gogoproto v1.4.10
github.com/cosmos/ibc-go/v7 v7.1.0
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.3
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
Expand Down Expand Up @@ -62,7 +64,6 @@ require (
github.com/containerd/cgroups v1.1.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.2 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gogogateway v1.2.0 // indirect
github.com/cosmos/iavl v0.20.0 // indirect
Expand Down Expand Up @@ -101,7 +102,6 @@ require (
github.com/gogo/protobuf v1.3.3 // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/flatbuffers v2.0.0+incompatible // indirect
Expand Down
8 changes: 7 additions & 1 deletion proto/pocket/gateway/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ option go_package = "pocket/x/gateway/types";

// Msg defines the Msg service.
service Msg {
rpc StakeGateway (MsgStakeGateway) returns (MsgStakeGatewayResponse);
rpc StakeGateway (MsgStakeGateway ) returns (MsgStakeGatewayResponse );
rpc UnstakeGateway (MsgUnstakeGateway) returns (MsgUnstakeGatewayResponse);
}
message MsgStakeGateway {
string address = 1;
}

message MsgStakeGatewayResponse {}

message MsgUnstakeGateway {
string address = 1;
}

message MsgUnstakeGatewayResponse {}
5 changes: 2 additions & 3 deletions x/gateway/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import (
"pocket/x/gateway/types"
)

var (
DefaultRelativePacketTimeoutTimestamp = uint64((time.Duration(10) * time.Minute).Nanoseconds())
)
var DefaultRelativePacketTimeoutTimestamp = uint64((time.Duration(10) * time.Minute).Nanoseconds())

const (
flagPacketTimeoutTimestamp = "packet-timeout-timestamp"
Expand All @@ -31,6 +29,7 @@ func GetTxCmd() *cobra.Command {
}

cmd.AddCommand(CmdStakeGateway())
cmd.AddCommand(CmdUnstakeGateway())
// this line is used by starport scaffolding # 1

return cmd
Expand Down
40 changes: 40 additions & 0 deletions x/gateway/client/cli/tx_unstake_gateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cli

import (
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/spf13/cobra"
"pocket/x/gateway/types"
)

var _ = strconv.Itoa(0)

func CmdUnstakeGateway() *cobra.Command {
cmd := &cobra.Command{
Use: "unstake-gateway",
Short: "Broadcast message unstake-gateway",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) (err error) {

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := types.NewMsgUnstakeGateway(
clientCtx.GetFromAddress().String(),
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
17 changes: 17 additions & 0 deletions x/gateway/keeper/msg_server_unstake_gateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"pocket/x/gateway/types"
)

func (k msgServer) UnstakeGateway(goCtx context.Context, msg *types.MsgUnstakeGateway) (*types.MsgUnstakeGatewayResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: Handling the message
_ = ctx

return &types.MsgUnstakeGatewayResponse{}, nil
}
30 changes: 27 additions & 3 deletions x/gateway/module_simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package gateway
import (
"math/rand"

"pocket/testutil/sample"
gatewaysimulation "pocket/x/gateway/simulation"
"pocket/x/gateway/types"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
"pocket/testutil/sample"
gatewaysimulation "pocket/x/gateway/simulation"
"pocket/x/gateway/types"
)

// avoid unused import issue
Expand All @@ -27,6 +28,10 @@ const (
// TODO: Determine the simulation weight value
defaultWeightMsgStakeGateway int = 100

opWeightMsgUnstakeGateway = "op_weight_msg_unstake_gateway"
// TODO: Determine the simulation weight value
defaultWeightMsgUnstakeGateway int = 100

// this line is used by starport scaffolding # simapp/module/const
)

Expand Down Expand Up @@ -66,6 +71,17 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp
gatewaysimulation.SimulateMsgStakeGateway(am.accountKeeper, am.bankKeeper, am.keeper),
))

var weightMsgUnstakeGateway int
simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgUnstakeGateway, &weightMsgUnstakeGateway, nil,
func(_ *rand.Rand) {
weightMsgUnstakeGateway = defaultWeightMsgUnstakeGateway
},
)
operations = append(operations, simulation.NewWeightedOperation(
weightMsgUnstakeGateway,
gatewaysimulation.SimulateMsgUnstakeGateway(am.accountKeeper, am.bankKeeper, am.keeper),
))

// this line is used by starport scaffolding # simapp/module/operation

return operations
Expand All @@ -82,6 +98,14 @@ func (am AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.Wei
return nil
},
),
simulation.NewWeightedProposalMsg(
opWeightMsgUnstakeGateway,
defaultWeightMsgUnstakeGateway,
func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) sdk.Msg {
gatewaysimulation.SimulateMsgUnstakeGateway(am.accountKeeper, am.bankKeeper, am.keeper)
return nil
},
),
// this line is used by starport scaffolding # simapp/module/OpMsg
}
}
29 changes: 29 additions & 0 deletions x/gateway/simulation/unstake_gateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package simulation

import (
"math/rand"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"pocket/x/gateway/keeper"
"pocket/x/gateway/types"
)

func SimulateMsgUnstakeGateway(
ak types.AccountKeeper,
bk types.BankKeeper,
k keeper.Keeper,
) simtypes.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
simAccount, _ := simtypes.RandomAcc(r, accs)
msg := &types.MsgUnstakeGateway{
Address: simAccount.Address.String(),
}

// TODO: Handling the UnstakeGateway simulation

return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "UnstakeGateway simulation not implemented"), nil, nil
}
}
4 changes: 4 additions & 0 deletions x/gateway/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import (

func RegisterCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgStakeGateway{}, "gateway/StakeGateway", nil)
cdc.RegisterConcrete(&MsgUnstakeGateway{}, "gateway/UnstakeGateway", nil)
// this line is used by starport scaffolding # 2
}

func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgStakeGateway{},
)
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgUnstakeGateway{},
)
// this line is used by starport scaffolding # 3

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
Expand Down
45 changes: 45 additions & 0 deletions x/gateway/types/message_unstake_gateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

const TypeMsgUnstakeGateway = "unstake_gateway"

var _ sdk.Msg = &MsgUnstakeGateway{}

func NewMsgUnstakeGateway(address string) *MsgUnstakeGateway {
return &MsgUnstakeGateway{
Address: address,
}
}

func (msg *MsgUnstakeGateway) Route() string {
return RouterKey
}

func (msg *MsgUnstakeGateway) Type() string {
return TypeMsgUnstakeGateway
}

func (msg *MsgUnstakeGateway) GetSigners() []sdk.AccAddress {
address, err := sdk.AccAddressFromBech32(msg.Address)
if err != nil {
panic(err)
}
return []sdk.AccAddress{address}
}

func (msg *MsgUnstakeGateway) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}

func (msg *MsgUnstakeGateway) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Address)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid address address (%s)", err)
}
return nil
}
40 changes: 40 additions & 0 deletions x/gateway/types/message_unstake_gateway_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package types

import (
"testing"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/stretchr/testify/require"
"pocket/testutil/sample"
)

func TestMsgUnstakeGateway_ValidateBasic(t *testing.T) {
tests := []struct {
name string
msg MsgUnstakeGateway
err error
}{
{
name: "invalid address",
msg: MsgUnstakeGateway{
Address: "invalid_address",
},
err: sdkerrors.ErrInvalidAddress,
}, {
name: "valid address",
msg: MsgUnstakeGateway{
Address: sample.AccAddress(),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.msg.ValidateBasic()
if tt.err != nil {
require.ErrorIs(t, err, tt.err)
return
}
require.NoError(t, err)
})
}
}

0 comments on commit b0678e0

Please sign in to comment.