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(gnovm): show "out of gas" errors (#2365) #2368

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
203 changes: 203 additions & 0 deletions gno.land/cmd/gnoland/testdata/issue_2365.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
# issue 2365
#
# addpkg with insufficient gas giving "VM call panic {WritePerByte}"
# instead of some "out of gas" message.

adduserfrom bob 'source bonus chronic canvas draft south burst lottery vacant surface solve popular case indicate oppose farm nothing bullet exhibit title speed wink action roast'
stdout 'g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5'

## start a new node
gnoland start

## check users initial balance
gnokey query bank/balances/${USER_ADDR_bob}
stdout '10000000ugnot'

# addpkg w/ 0.0000000001gnot should fail
! gnokey maketx addpkg -deposit 1ugnot -gas-fee 1ugnot -pkgpath gno.land/p/demo/mux -pkgdir $WORK -gas-wanted 9000000 -broadcast -chainid=tendermint_test ${USER_ADDR_bob}

# before fix
# stderr '(.+\n)+VM call panic {WritePerByte}'

# after #2368
stderr '(.+\n)+Data: out of gas error'

-- gno.mod --
module gno.land/p/demo/ufmt

-- ufmt.gno --
// Package ufmt provides utility functions for formatting strings, similarly
// to the Go package "fmt", of which only a subset is currently supported
// (hence the name µfmt - micro fmt).
package ufmt

import (
"strconv"
"strings"
)

// Println formats using the default formats for its operands and writes to standard output.
// Println writes the given arguments to standard output with spaces between arguments
// and a newline at the end.
func Println(args ...interface{}) {
var strs []string
for _, arg := range args {
switch v := arg.(type) {
case string:
strs = append(strs, v)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
strs = append(strs, Sprintf("%d", v))
case bool:
if v {
strs = append(strs, "true")

continue
}

strs = append(strs, "false")
default:
strs = append(strs, "(unhandled)")
}
}

// TODO: remove println after gno supports os.Stdout
println(strings.Join(strs, " "))
}

// Sprintf offers similar functionality to Go's fmt.Sprintf, or the sprintf
// equivalent available in many languages, including C/C++.
// The number of args passed must exactly match the arguments consumed by the format.
// A limited number of formatting verbs and features are currently supported,
// hence the name ufmt (µfmt, micro-fmt).
//
// The currently formatted verbs are the following:
//
// %s: places a string value directly.
// If the value implements the interface interface{ String() string },
// the String() method is called to retrieve the value.
// %d: formats an integer value using package "strconv".
// Currently supports only uint, uint64, int, int64.
// %t: formats a boolean value to "true" or "false".
// %%: outputs a literal %. Does not consume an argument.
func Sprintf(format string, args ...interface{}) string {
// we use runes to handle multi-byte characters
sTor := []rune(format)
end := len(sTor)
argNum := 0
argLen := len(args)
buf := ""

for i := 0; i < end; {
isLast := i == end-1
c := string(sTor[i])

if isLast || c != "%" {
// we don't check for invalid format like a one ending with "%"
buf += string(c)
i++
continue
}

verb := string(sTor[i+1])
if verb == "%" {
buf += "%"
i += 2
continue
}

if argNum > argLen {
panic("invalid number of arguments to ufmt.Sprintf")
}
arg := args[argNum]
argNum++

switch verb {
case "s":
switch v := arg.(type) {
case interface{ String() string }:
buf += v.String()
case string:
buf += v
default:
buf += "(unhandled)"
}
case "d":
switch v := arg.(type) {
case int:
buf += strconv.Itoa(v)
case int8:
buf += strconv.Itoa(int(v))
case int16:
buf += strconv.Itoa(int(v))
case int32:
buf += strconv.Itoa(int(v))
case int64:
buf += strconv.Itoa(int(v))
case uint:
buf += strconv.FormatUint(uint64(v), 10)
case uint8:
buf += strconv.FormatUint(uint64(v), 10)
case uint16:
buf += strconv.FormatUint(uint64(v), 10)
case uint32:
buf += strconv.FormatUint(uint64(v), 10)
case uint64:
buf += strconv.FormatUint(v, 10)
default:
buf += "(unhandled)"
}
case "t":
switch v := arg.(type) {
case bool:
if v {
buf += "true"
} else {
buf += "false"
}
default:
buf += "(unhandled)"
}
// % handled before, as it does not consume an argument
default:
buf += "(unhandled)"
}

i += 2
}
if argNum < argLen {
panic("too many arguments to ufmt.Sprintf")
}
return buf
}

// errMsg implements the error interface.
type errMsg struct {
msg string
}

// Error defines the requirements of the error interface.
// It functions similarly to Go's errors.New()
func (e *errMsg) Error() string {
return e.msg
}

// Errorf is a function that mirrors the functionality of fmt.Errorf.
//
// It takes a format string and arguments to create a formatted string,
// then sets this string as the 'msg' field of an errMsg struct and returns a pointer to this struct.
//
// This function operates in a similar manner to Go's fmt.Errorf,
// providing a way to create formatted error messages.
//
// The currently formatted verbs are the following:
//
// %s: places a string value directly.
// If the value implements the interface interface{ String() string },
// the String() method is called to retrieve the value.
// %d: formats an integer value using package "strconv".
// Currently supports only uint, uint64, int, int64.
// %t: formats a boolean value to "true" or "false".
// %%: outputs a literal %. Does not consume an argument.
func Errorf(format string, args ...interface{}) error {
return &errMsg{Sprintf(format, args...)}
}
5 changes: 5 additions & 0 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"sync/atomic"

"github.com/gnolang/gno/tm2/pkg/errors"
"github.com/gnolang/gno/tm2/pkg/store/types"
)

const (
Expand Down Expand Up @@ -2855,6 +2856,10 @@
if rerr, ok := r.(error); ok {
// NOTE: gotuna/gorilla expects error exceptions.
panic(errors.Wrap(rerr, loc.String()))
} else if ex, ok := r.(types.OutOfGasException); ok {
// NOTE: baseapp.runTx() handles OutOfGasException
// which it then converts into ABCIError(std.ErrOutOfGas)
panic(ex)

Check warning on line 2862 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L2862

Added line #L2862 was not covered by tests
} else {
// NOTE: gotuna/gorilla expects error exceptions.
panic(fmt.Errorf("%s: %v", loc.String(), r))
Expand Down
Loading