Skip to content

Commit

Permalink
fix: msg eval format
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <[email protected]>
  • Loading branch information
gfanton committed Jan 9, 2025
1 parent d574bf7 commit 4db0338
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
2 changes: 1 addition & 1 deletion gno.land/pkg/gnoland/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func EndBlocker(

// Run the VM to get the updates from the chain
expr := fmt.Sprintf("%s(%d)", valChangesFn, app.LastBlockHeight())
msgEval := vm.NewMsgEval(vm.ResultFormatMachine, valRealm, expr)
msgEval := vm.NewMsgEval(vm.FormatDefault, valRealm, expr)
response, err := vmk.Eval(ctx, msgEval)
if err != nil {
app.Logger().Error("unable to call VM during EndBlocker", "err", err)
Expand Down
12 changes: 6 additions & 6 deletions gno.land/pkg/sdk/vm/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (vh vmHandler) queryRender(ctx sdk.Context, req abci.RequestQuery) (res abc

// Generate msg eval request
expr := fmt.Sprintf("Render(%q)", path)
msgEval := NewMsgEval(ResultFormatString, pkgPath, expr)
msgEval := NewMsgEval(FormatString, pkgPath, expr)

// Try evaluate `Render` function
result, err := vh.vm.Eval(ctx, msgEval)
Expand All @@ -169,20 +169,20 @@ func (vh vmHandler) queryFuncs(ctx sdk.Context, req abci.RequestQuery) (res abci

// queryEval evaluates any expression in readonly mode and returns the results based on the given format.
func (vh vmHandler) queryEval(ctx sdk.Context, req abci.RequestQuery) (res abci.ResponseQuery) {
var format ResultFormat
var format Format
switch ss := strings.Split(req.Path, "/"); len(ss) {
case 2:
format = ResultFormatDefault
format = FormatDefault
case 3:
format = ResultFormat(ss[2])
format = Format(ss[2])
default:
res = sdk.ABCIResponseQueryFromError(fmt.Errorf("invalid query"))
return

}

switch format {
case ResultFormatMachine, ResultFormatJSON, ResultFormatString:
case FormatMachine, FormatJSON, FormatString:
default:
err := fmt.Errorf("invalid query result format %q", format)
res = sdk.ABCIResponseQueryFromError(err)
Expand Down Expand Up @@ -210,7 +210,7 @@ func (vh vmHandler) queryEval(ctx sdk.Context, req abci.RequestQuery) (res abci.
// queryEvalJSON evaluates any expression in readonly mode and returns the results in JSON format.
func (vh vmHandler) queryEvalJSON(ctx sdk.Context, req abci.RequestQuery) (res abci.ResponseQuery) {
pkgath, expr := parseQueryEvalData(string(req.Data))
msgEval := NewMsgEval(ResultFormatJSON, pkgath, expr)
msgEval := NewMsgEval(FormatJSON, pkgath, expr)
result, err := vh.vm.Eval(ctx, msgEval)
if err != nil {
res = sdk.ABCIResponseQueryFromError(err)
Expand Down
10 changes: 5 additions & 5 deletions gno.land/pkg/sdk/vm/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ func (vm *VMKeeper) Eval(ctx sdk.Context, msg MsgEval) (res string, err error) {
defer doRecover(m, &err)

rtvs := m.Eval(xx)
res = stringifyResultValues(m, msg.ResultFormat, rtvs)
res = stringifyResultValues(m, msg.Format, rtvs)
return res, nil
}

Expand Down Expand Up @@ -794,9 +794,9 @@ func (vm *VMKeeper) QueryFile(ctx sdk.Context, filepath string) (res string, err

}

func stringifyResultValues(m *gno.Machine, format ResultFormat, values []gnolang.TypedValue) string {
func stringifyResultValues(m *gno.Machine, format Format, values []gnolang.TypedValue) string {
switch format {
case ResultFormatString:
case FormatString:
if len(values) != 1 {
panic(fmt.Errorf("expected 1 string result, got %d", len(values)))
}
Expand All @@ -816,9 +816,9 @@ func stringifyResultValues(m *gno.Machine, format ResultFormat, values []gnolang

panic(fmt.Errorf("expected 1 `string` or `Stringer` result, got %v", tv.T.Kind()))

case ResultFormatJSON:
case FormatJSON:
return JSONPrimitiveValues(m, values)
case ResultFormatDefault, "":
case FormatDefault, "":
var res strings.Builder

for i, v := range values {
Expand Down
33 changes: 16 additions & 17 deletions gno.land/pkg/sdk/vm/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import (
"github.com/gnolang/gno/tm2/pkg/std"
)

type ResultFormat string
type Format string

const (
ResultFormatMachine ResultFormat = "machine" // Default machine representation
ResultFormatString = "string" // Single string represnetation
ResultFormatJSON = "json" // XXX: EXPERIMENTAL, only supports primitive types for now
ResultFormatDefault = ResultFormatMachine
FormatMachine Format = "machine" // Default machine representation
FormatString = "string" // Single string represnetation
FormatJSON = "json" // XXX: EXPERIMENTAL, only supports primitive types for now

FormatDefault = FormatMachine
)

//----------------------------------------
Expand Down Expand Up @@ -93,24 +94,22 @@ func (msg MsgAddPackage) GetReceived() std.Coins {

// MsgEval - eval a Gno Expr.
type MsgEval struct {
PkgPath string `json:"pkg_path" yaml:"pkg_path"`
Expr string `json:"expr" yaml:"expr"`

// Caller will be use for signing only
Caller crypto.Address `json:"caller" yaml:"caller"`
PkgPath string `json:"pkg_path" yaml:"pkg_path"`
Expr string `json:"expr" yaml:"expr"`
Caller crypto.Address `json:"caller" yaml:"caller"`

// XXX: This field is experimental, use with care as output is likely to change
// Default format is machine
ResultFormat ResultFormat `json:"format" yaml:"format"`
Format Format `json:"format" yaml:"format"`
}

var _ std.Msg = MsgEval{}

func NewMsgEval(format ResultFormat, pkgPath, expr string) MsgEval {
func NewMsgEval(format Format, pkgPath, expr string) MsgEval {
return MsgEval{
PkgPath: pkgPath,
Expr: expr,
ResultFormat: format,
PkgPath: pkgPath,
Expr: expr,
Format: format,
}
}

Expand Down Expand Up @@ -155,14 +154,14 @@ type MsgCall struct {
Args []string `json:"args" yaml:"args"`

// XXX: This field is experimental, use with care as output is likely to change
Format ResultFormat `json:"format" yaml:"format"`
Format Format `json:"format" yaml:"format"`
}

var _ std.Msg = MsgCall{}

func NewMsgCall(caller crypto.Address, send sdk.Coins, pkgPath, fnc string, args []string) MsgCall {
return MsgCall{
Format: ResultFormatDefault,
Format: FormatDefault,
Caller: caller,
Send: send,
PkgPath: pkgPath,
Expand Down

0 comments on commit 4db0338

Please sign in to comment.