Skip to content

Commit

Permalink
Review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
luckychess committed Dec 11, 2024
1 parent 7a48127 commit c1bddc6
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 104 deletions.
10 changes: 2 additions & 8 deletions x/gmp/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ package cli

import (
"fmt"
// "strings"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
// "github.com/cosmos/cosmos-sdk/client/flags"
// sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/sagaxyz/ssc/x/gmp/types"
"github.com/spf13/cobra"
)

// GetQueryCmd returns the cli query commands for this module
Expand All @@ -27,6 +22,5 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
cmd.AddCommand(CmdQueryParams())
// this line is used by starport scaffolding # 1

return cmd
return cmd
}

5 changes: 2 additions & 3 deletions x/gmp/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
// "github.com/cosmos/cosmos-sdk/client/flags"
"github.com/sagaxyz/ssc/x/gmp/types"
)

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

const (
Expand All @@ -32,5 +31,5 @@ func GetTxCmd() *cobra.Command {

// this line is used by starport scaffolding # 1

return cmd
return cmd
}
29 changes: 14 additions & 15 deletions x/gmp/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,27 @@ import (

// InitGenesis initializes the module's state from a provided genesis state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
// this line is used by starport scaffolding # genesis/module/init
k.SetPort(ctx, genState.PortId)
// Only try to bind to port if it is not already bound, since we may already own
// port capability from capability InitGenesis
if !k.IsBound(ctx, genState.PortId) {
// module binds to the port on InitChain
// and claims the returned capability
err := k.BindPort(ctx, genState.PortId)
if err != nil {
panic("could not claim port capability: " + err.Error())
// this line is used by starport scaffolding # genesis/module/init
k.SetPort(ctx, genState.PortId)
// Only try to bind to port if it is not already bound, since we may already own
// port capability from capability InitGenesis
if !k.IsBound(ctx, genState.PortId) {
// module binds to the port on InitChain
// and claims the returned capability
err := k.BindPort(ctx, genState.PortId)
if err != nil {
panic("could not claim port capability: " + err.Error())
}
}
}
k.SetParams(ctx, genState.Params)
}

// ExportGenesis returns the module's exported genesis
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
genesis := types.DefaultGenesis()
genesis.Params = k.GetParams(ctx)
genesis.PortId = k.GetPort(ctx)
// this line is used by starport scaffolding # genesis/module/export

genesis.PortId = k.GetPort(ctx)
// this line is used by starport scaffolding # genesis/module/export

return genesis
return genesis
}
2 changes: 1 addition & 1 deletion x/gmp/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func TestGenesis(t *testing.T) {
genesisState := types.GenesisState{
Params: types.DefaultParams(),
Params: types.DefaultParams(),
PortId: types.PortID,
// this line is used by starport scaffolding # genesis/test/state
}
Expand Down
6 changes: 3 additions & 3 deletions x/gmp/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
keepertest "github.com/sagaxyz/ssc/testutil/keeper"
"github.com/sagaxyz/ssc/x/gmp/keeper"
"github.com/sagaxyz/ssc/x/gmp/types"
"github.com/stretchr/testify/require"
"github.com/sagaxyz/ssc/x/gmp/types"
"github.com/sagaxyz/ssc/x/gmp/keeper"
keepertest "github.com/sagaxyz/ssc/testutil/keeper"
)

func setupMsgServer(t testing.TB) (types.MsgServer, context.Context) {
Expand Down
6 changes: 2 additions & 4 deletions x/gmp/keeper/params.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package keeper

import (
"github.com/sagaxyz/ssc/x/gmp/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sagaxyz/ssc/x/gmp/types"
)

// GetParams get all parameters as types.Params
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
return types.NewParams(
)
return types.NewParams()
}

// SetParams set the params
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramstore.SetParamSet(ctx, &params)
}

2 changes: 1 addition & 1 deletion x/gmp/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package keeper_test
import (
"testing"

"github.com/stretchr/testify/require"
testkeeper "github.com/sagaxyz/ssc/testutil/keeper"
"github.com/sagaxyz/ssc/x/gmp/types"
"github.com/stretchr/testify/require"
)

func TestGetParams(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions x/gmp/keeper/query_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
testkeeper "github.com/sagaxyz/ssc/testutil/keeper"
testkeeper "github.com/sagaxyz/ssc/testutil/keeper"
"github.com/sagaxyz/ssc/x/gmp/types"
"github.com/stretchr/testify/require"
)

func TestParamsQuery(t *testing.T) {
Expand Down
20 changes: 6 additions & 14 deletions x/gmp/module_ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ type Message struct {
Type int64 `json:"type"`
}

type MessageType int

const (
// TypeUnrecognized means coin type is unrecognized
TypeUnrecognized = iota
Expand Down Expand Up @@ -130,14 +128,13 @@ func (im IBCModule) OnRecvPacket(
// this line is used by starport scaffolding # oracle/packet/module/recv

var data transfertypes.FungibleTokenPacketData
var err error
if err := types.ModuleCdc.UnmarshalJSON(modulePacket.GetData(), &data); err != nil {
ctx.Logger().Debug(fmt.Sprintf("cannot unmarshal ICS-20 transfer packet data: %s", err.Error()))
return im.app.OnRecvPacket(ctx, modulePacket, relayer)
}

var msg Message
if err = json.Unmarshal([]byte(data.GetMemo()), &msg); err != nil {
if err := json.Unmarshal([]byte(data.GetMemo()), &msg); err != nil {
ctx.Logger().Debug(fmt.Sprintf("cannot unmarshal memo: %s", err.Error()))
return im.app.OnRecvPacket(ctx, modulePacket, relayer)
}
Expand All @@ -148,31 +145,26 @@ func (im IBCModule) OnRecvPacket(

switch msg.Type {
case TypeGeneralMessage:
ctx.Logger().Info(fmt.Sprintf("Got pure general message: %v", msg))
ctx.Logger().Debug(fmt.Sprintf("Got pure general message: %v", msg))
return im.app.OnRecvPacket(ctx, modulePacket, relayer)
case TypeGeneralMessageWithToken:
ctx.Logger().Info(fmt.Sprintf("Got general message with token: %v", msg))
ctx.Logger().Debug(fmt.Sprintf("Got general message with token: %v", msg))
payloadType, err := abi.NewType("string", "", nil)
if err != nil {
ctx.Logger().Info(fmt.Sprintf("failed to create reflection: %s", err.Error()))
return channeltypes.NewErrorAcknowledgement(cosmossdkerrors.Wrapf(transfertypes.ErrInvalidMemo, "unable to define new abi type (%s)", err.Error()))
}

// Add 4 bytes to the payload to match the length of the payload otherwise the unpack will fail
payloadData := msg.Payload
ctx.Logger().Info(fmt.Sprintf("ABI-encoded data: %v", hex.EncodeToString(payloadData)))
ctx.Logger().Debug(fmt.Sprintf("ABI-encoded data: %v", hex.EncodeToString(payloadData)))

// Add 4 bytes to the payload to match the length of the payload otherwise the unpack will fail
payloadData = append(make([]byte, 4), payloadData...)
ctx.Logger().Info(fmt.Sprintf("Padded ABI-encoded data: %v", hex.EncodeToString(payloadData)))

args, err := abi.Arguments{{Type: payloadType}}.Unpack(payloadData)
if err != nil {
ctx.Logger().Info(fmt.Sprintf("failed to unpack: %s", err.Error()))
ctx.Logger().Debug(fmt.Sprintf("failed to unpack: %s", err.Error()))
return channeltypes.NewErrorAcknowledgement(cosmossdkerrors.Wrapf(transfertypes.ErrInvalidMemo, "unable to unpack payload (%s)", err.Error()))
}
ctx.Logger().Info(fmt.Sprintf("Unpacked data: %+v", args))
pfmPayload := args[0].(string)
ctx.Logger().Info(fmt.Sprintf("new memo: %s", pfmPayload))
data.Memo = string(pfmPayload)
modulePacket.Data, err = types.ModuleCdc.MarshalJSON(&data)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions x/gmp/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package types

import (
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"

// this line is used by starport scaffolding # 1
"github.com/cosmos/cosmos-sdk/types/msgservice"
)

func RegisterCodec(cdc *codec.LegacyAmino) {
// this line is used by starport scaffolding # 2
}
}

func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
// this line is used by starport scaffolding # 3
Expand All @@ -18,6 +19,6 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
}

var (
Amino = codec.NewLegacyAmino()
Amino = codec.NewLegacyAmino()
ModuleCdc = codec.NewProtoCodec(cdctypes.NewInterfaceRegistry())
)
4 changes: 1 addition & 3 deletions x/gmp/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ import (

// x/gmp module sentinel errors
var (
ErrSample = cosmossdkerrors.Register(ModuleName, 1100, "sample error")
ErrInvalidPacketTimeout = cosmossdkerrors.Register(ModuleName, 1500, "invalid packet timeout")
ErrInvalidVersion = cosmossdkerrors.Register(ModuleName, 1501, "invalid version")
ErrSample = cosmossdkerrors.Register(ModuleName, 1100, "sample error")
)
8 changes: 4 additions & 4 deletions x/gmp/types/events_ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package types

// IBC events
const (
EventTypeTimeout = "timeout"
EventTypeTimeout = "timeout"
// this line is used by starport scaffolding # ibc/packet/event

AttributeKeyAckSuccess = "success"
AttributeKeyAck = "acknowledgement"
AttributeKeyAckError = "error"
AttributeKeyAckSuccess = "success"
AttributeKeyAck = "acknowledgement"
AttributeKeyAckError = "error"
)
58 changes: 29 additions & 29 deletions x/gmp/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,39 @@ package types_test
import (
"testing"

"github.com/stretchr/testify/require"
"github.com/sagaxyz/ssc/x/gmp/types"
"github.com/stretchr/testify/require"
)

func TestGenesisState_Validate(t *testing.T) {
tests := []struct {
desc string
genState *types.GenesisState
valid bool
} {
{
desc: "default is valid",
genState: types.DefaultGenesis(),
valid: true,
},
{
desc: "valid genesis state",
genState: &types.GenesisState{
PortId: types.PortID,
// this line is used by starport scaffolding # types/genesis/validField
},
valid: true,
},
// this line is used by starport scaffolding # types/genesis/testcase
tests := []struct {
desc string
genState *types.GenesisState
valid bool
}{
{
desc: "default is valid",
genState: types.DefaultGenesis(),
valid: true,
},
{
desc: "valid genesis state",
genState: &types.GenesisState{
PortId: types.PortID,
// this line is used by starport scaffolding # types/genesis/validField
},
valid: true,
},
// this line is used by starport scaffolding # types/genesis/testcase
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
err := tc.genState.Validate()
if tc.valid {
require.NoError(t, err)
} else {
require.Error(t, err)
}
})
}
}
err := tc.genState.Validate()
if tc.valid {
require.NoError(t, err)
} else {
require.Error(t, err)
}
})
}
}
16 changes: 4 additions & 12 deletions x/gmp/types/params.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
package types

import (


paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"gopkg.in/yaml.v2"
)

var _ paramtypes.ParamSet = (*Params)(nil)



// ParamKeyTable the param key table for launch module
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}

// NewParams creates a new Params instance
func NewParams(
) Params {
return Params{
}
func NewParams() Params {
return Params{}
}

// DefaultParams returns a default set of parameters
func DefaultParams() Params {
return NewParams(
)
return NewParams()
}

// ParamSetPairs get the params.ParamSet
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
}
return paramtypes.ParamSetPairs{}
}

// Validate validates the set of params
Expand Down
3 changes: 1 addition & 2 deletions x/ssc/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
// "github.com/cosmos/cosmos-sdk/client/flags"
"github.com/sagaxyz/ssc/x/ssc/types"
)

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

const (
Expand Down

0 comments on commit c1bddc6

Please sign in to comment.