Skip to content

Commit

Permalink
refactor(templates)!: remove ValidateBasic() and use address codec (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Apr 2, 2024
1 parent 6b6bd3b commit bc57707
Show file tree
Hide file tree
Showing 38 changed files with 354 additions and 737 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

### Changes

- [#4058](https://github.com/ignite/cli/pull/4058) Simplify scaffolded modules by including `ValidateBasic()` logic in message handler.
- [#4058](https://github.com/ignite/cli/pull/4058) Use `address.Codec` instead of `AccAddressFromBech32`.
- [#3993](https://github.com/ignite/cli/pull/3993) Oracle scaffolding was deprecated and has been removed
- [#3959](https://github.com/ignite/cli/pull/3959) Remove app name prefix from the `.gitignore` file
- [#3962](https://github.com/ignite/cli/pull/3962) Rename all RPC endpoints and autocli commands generated for `map`/`list`/`single` types
Expand Down
5 changes: 3 additions & 2 deletions ignite/pkg/cosmostestutil/sample/sample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package cosmostestutilsample
import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
"github.com/stretchr/testify/require"
)

func TestAccAddress(t *testing.T) {
got := AccAddress()
require.NotEmpty(t, got)
_, err := sdk.AccAddressFromBech32(got)
exampleAccountAddress := addresscodec.NewBech32Codec("cosmos")
_, err := exampleAccountAddress.StringToBytes(got)
require.NoError(t, err)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (k Keeper) Transmit<%= packetName.UpperCamel %>Packet(
timeoutHeight clienttypes.Height,
timeoutTimestamp uint64,
) (uint64, error) {
channelCap, ok := k.scopedKeeper.GetCapability(ctx, host.ChannelCapabilityPath(sourcePort, sourceChannel))
channelCap, ok := k.ScopedKeeper().GetCapability(ctx, host.ChannelCapabilityPath(sourcePort, sourceChannel))
if !ok {
return 0, errorsmod.Wrap(channeltypes.ErrChannelCapabilityNotFound, "module does not own channel capability")
}
Expand All @@ -37,9 +37,6 @@ func (k Keeper) Transmit<%= packetName.UpperCamel %>Packet(
// OnRecv<%= packetName.UpperCamel %>Packet processes packet reception
func (k Keeper) OnRecv<%= packetName.UpperCamel %>Packet(ctx sdk.Context, packet channeltypes.Packet, data types.<%= packetName.UpperCamel %>PacketData) (packetAck types.<%= packetName.UpperCamel %>PacketAck, err error) {
// validate packet data upon receiving
if err := data.ValidateBasic(); err != nil {
return packetAck, err
}

// TODO: packet reception logic

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package types

// ValidateBasic is used for validating the packet
func (p <%= packetName.UpperCamel %>PacketData) ValidateBasic() error {

// TODO: Validate the packet data

return nil
}

// GetBytes is a helper for serialising
func (p <%= packetName.UpperCamel %>PacketData) GetBytes() ([]byte, error) {
var modulePacket <%= title(moduleName) %>PacketData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ func CmdSend<%= packetName.UpperCamel %>() *cobra.Command {
}

msg := types.NewMsgSend<%= packetName.UpperCamel %>(<%= MsgSigner.LowerCamel %>, srcPort, srcChannel, timeoutTimestamp<%= for (i, field) in fields { %>, arg<%= field.Name.UpperCamel %><% } %>)
if err := msg.ValidateBasic(); err != nil {
return err
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
package keeper

import (
"fmt"
"context"

errorsmod "cosmossdk.io/errors"
"<%= ModulePath %>/x/<%= moduleName %>/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
)


func (k msgServer) Send<%= packetName.UpperCamel %>(goCtx context.Context, msg *types.MsgSend<%= packetName.UpperCamel %>) (*types.MsgSend<%= packetName.UpperCamel %>Response, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// validate incoming message
if _, err := k.addressCodec.StringToBytes(msg.<%= MsgSigner.UpperCamel %>); err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidAddress, fmt.Sprintf("invalid address: %s", err))
}

if msg.Port == "" {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid packet port")
}

if msg.ChannelID == "" {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid packet channel")
}

if msg.TimeoutTimestamp == 0 {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid packet timeout")
}

// TODO: logic before transmitting the packet

Expand All @@ -20,6 +38,7 @@ func (k msgServer) Send<%= packetName.UpperCamel %>(goCtx context.Context, msg
packet.<%= field.Name.UpperCamel %> = msg.<%= field.Name.UpperCamel %><% } %>

// Transmit the packet
ctx := sdk.UnwrapSDKContext(goCtx)
_, err := k.Transmit<%= packetName.UpperCamel %>Packet(
ctx,
packet,
Expand All @@ -33,4 +52,4 @@ func (k msgServer) Send<%= packetName.UpperCamel %>(goCtx context.Context, msg
}

return &types.MsgSend<%= packetName.UpperCamel %>Response{}, nil
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package keeper_test

import (
"testing"

"github.com/stretchr/testify/require"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"

keepertest "<%= ModulePath %>/testutil/keeper"
"<%= ModulePath %>/x/<%= moduleName %>/keeper"
"<%= ModulePath %>/x/<%= moduleName %>/types"
)

func TestMsgServerSend<%= packetName.UpperCamel %>(t *testing.T) {
k, ctx, addressCodec := keepertest.<%= title(moduleName) %>Keeper(t)
<%= MsgSigner.LowerCamel %>, err := addressCodec.BytesToString([]byte("signerAddr__________________"))
require.NoError(t, err)
srv := keeper.NewMsgServerImpl(k)

tests := []struct {
name string
msg types.MsgSend<%= packetName.UpperCamel %>
err error
}{
{
name: "invalid address",
msg: types.MsgSend<%= packetName.UpperCamel %>{
<%= MsgSigner.UpperCamel %>: "invalid address",
Port: "port",
ChannelID: "channel-0",
TimeoutTimestamp: 100,
},
err: sdkerrors.ErrInvalidAddress,
}, {
name: "invalid port",
msg: types.MsgSend<%= packetName.UpperCamel %>{
<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>,
Port: "",
ChannelID: "channel-0",
TimeoutTimestamp: 100,
},
err: sdkerrors.ErrInvalidRequest,
}, {
name: "invalid channel",
msg: types.MsgSend<%= packetName.UpperCamel %>{
<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>,
Port: "port",
ChannelID: "",
TimeoutTimestamp: 100,
},
err: sdkerrors.ErrInvalidRequest,
}, {
name: "invalid timeout",
msg: types.MsgSend<%= packetName.UpperCamel %>{
<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>,
Port: "port",
ChannelID: "channel-0",
TimeoutTimestamp: 0,
},
err: sdkerrors.ErrInvalidRequest,
}, {
name: "valid message",
msg: types.MsgSend<%= packetName.UpperCamel %>{
<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>,
Port: "port",
ChannelID: "channel-0",
TimeoutTimestamp: 100,
},
err: channeltypes.ErrChannelCapabilityNotFound,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err = srv.Send<%= packetName.UpperCamel %>(ctx, &tt.msg)
if tt.err != nil {
require.ErrorContains(t, err, tt.err.Error())
return
}
require.NoError(t, err)
})
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package types

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

var _ sdk.Msg = &MsgSend<%= packetName.UpperCamel %>{}

func NewMsgSend<%= packetName.UpperCamel %>(
<%= MsgSigner.LowerCamel %> string,
port string,
Expand All @@ -22,21 +14,4 @@ func NewMsgSend<%= packetName.UpperCamel %>(
TimeoutTimestamp: timeoutTimestamp,<%= for (field) in fields { %>
<%= field.Name.UpperCamel %>: <%= field.Name.LowerCamel %>,<% } %>
}
}

func (msg *MsgSend<%= packetName.UpperCamel %>) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.<%= MsgSigner.UpperCamel %>)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid <%= MsgSigner.LowerCamel %> address (%s)", err)
}
if msg.Port == "" {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid packet port")
}
if msg.ChannelID == "" {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid packet channel")
}
if msg.TimeoutTimestamp == 0 {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "invalid packet timeout")
}
return nil
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (


func (k msgServer) <%= MsgName.UpperCamel %>(ctx context.Context, msg *types.Msg<%= MsgName.UpperCamel %>) (*types.Msg<%= MsgName.UpperCamel %>Response, error) {
if _, err := k.addressCodec.StringToBytes(msg.<%= MsgSigner.UpperCamel %>); err != nil {
return nil, errorsmod.Wrap(err, "invalid authority address")
}

// TODO: Handle the message

return &types.Msg<%= MsgName.UpperCamel %>Response{}, nil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
package types

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

var _ sdk.Msg = &Msg<%= MsgName.UpperCamel %>{}

func NewMsg<%= MsgName.UpperCamel %>(<%= MsgSigner.LowerCamel %> string<%= for (field) in Fields { %>, <%= field.Name.LowerCamel %> <%= field.DataType() %><% } %>) *Msg<%= MsgName.UpperCamel %> {
return &Msg<%= MsgName.UpperCamel %>{
<%= MsgSigner.UpperCamel %>: <%= MsgSigner.LowerCamel %>,<%= for (field) in Fields { %>
<%= field.Name.UpperCamel %>: <%= field.Name.LowerCamel %>,<% } %>
}
}

func (msg *Msg<%= MsgName.UpperCamel %>) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.<%= MsgSigner.UpperCamel %>)
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid <%= MsgSigner.LowerCamel %> address (%s)", err)
}
return nil
}

}

This file was deleted.

Loading

0 comments on commit bc57707

Please sign in to comment.