Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sdk/vm): improve MsgCall panic error message for wrong number of args #1610

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions gno.land/pkg/sdk/vm/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ func (vm *VMKeeper) Call(ctx sdk.Context, msg MsgCall) (res string, err error) {
if cx.Varg {
panic("variadic calls not yet supported")
}
if len(msg.Args) < len(ft.Params) {
panic("not enough arguments in call to " + fnc)
} else if len(msg.Args) > len(ft.Params) {
panic("too many arguments in call to " + fnc)
}
jefft0 marked this conversation as resolved.
Show resolved Hide resolved
for i, arg := range msg.Args {
argType := ft.Params[i].Type
atv := convertArgToGno(arg, argType)
Expand Down
51 changes: 51 additions & 0 deletions gno.land/pkg/sdk/vm/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,54 @@ func main() {
expectedString := fmt.Sprintf("hello world! %s\n", addr.String())
assert.Equal(t, res, expectedString)
}

func TestNumberOfArgsError(t *testing.T) {
env := setupTestEnv()
ctx := env.ctx

// Give "addr1" some gnots.
addr := crypto.AddressFromPreimage([]byte("addr1"))
acc := env.acck.NewAccountWithAddress(ctx, addr)
env.acck.SetAccount(ctx, acc)
env.bank.SetCoins(ctx, addr, std.MustParseCoins("10000000ugnot"))
assert.True(t, env.bank.GetCoins(ctx, addr).IsEqual(std.MustParseCoins("10000000ugnot")))

// Create test package.
files := []*std.MemFile{
{
Name: "test.gno",
Body: `package test

import "std"

func Echo(msg string) string {
return "echo:"+msg
}`,
},
}
pkgPath := "gno.land/r/test"
msg1 := NewMsgAddPackage(addr, pkgPath, files)
err := env.vmk.AddPackage(ctx, msg1)
assert.NoError(t, err)

// Call Echo function with not enough arguments.
coins := std.MustParseCoins("1ugnot")
msg2 := NewMsgCall(addr, coins, pkgPath, "Echo", []string{})
assert.PanicsWithValue(
t,
func() {
env.vmk.Call(ctx, msg2)
},
"not enough arguments in call to Echo",
)

// Call Echo function with too many arguments.
msg3 := NewMsgCall(addr, coins, pkgPath, "Echo", []string{"hello world", "extra arg"})
assert.PanicsWithValue(
t,
func() {
env.vmk.Call(ctx, msg3)
},
"too many arguments in call to Echo",
)
}
Loading