From 1391f2345e976a73f287536322974cee5ed3d4cc Mon Sep 17 00:00:00 2001 From: moul <94029+moul@users.noreply.github.com> Date: Fri, 28 Jun 2024 19:11:24 +0200 Subject: [PATCH 01/10] feat: add unit tests for vmHandler Signed-off-by: moul <94029+moul@users.noreply.github.com> --- gno.land/pkg/sdk/vm/common_test.go | 4 ++- gno.land/pkg/sdk/vm/gas_test.go | 3 +-- gno.land/pkg/sdk/vm/handler_test.go | 39 +++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/gno.land/pkg/sdk/vm/common_test.go b/gno.land/pkg/sdk/vm/common_test.go index 216de980aff..623f39252ee 100644 --- a/gno.land/pkg/sdk/vm/common_test.go +++ b/gno.land/pkg/sdk/vm/common_test.go @@ -22,6 +22,7 @@ type testEnv struct { vmk *VMKeeper bank bankm.BankKeeper acck authm.AccountKeeper + vmh vmHandler } func setupTestEnv() testEnv { @@ -42,6 +43,7 @@ func setupTestEnv() testEnv { vmk := NewVMKeeper(baseCapKey, iavlCapKey, acck, bank, stdlibsDir, 100_000_000) vmk.Initialize(ms.MultiCacheWrap()) + vmh := NewHandler(vmk) - return testEnv{ctx: ctx, vmk: vmk, bank: bank, acck: acck} + return testEnv{ctx: ctx, vmk: vmk, bank: bank, acck: acck, vmh: vmh} } diff --git a/gno.land/pkg/sdk/vm/gas_test.go b/gno.land/pkg/sdk/vm/gas_test.go index 9f4ae1a6678..6de06ce7f52 100644 --- a/gno.land/pkg/sdk/vm/gas_test.go +++ b/gno.land/pkg/sdk/vm/gas_test.go @@ -133,7 +133,6 @@ func setupAddPkg(success bool) (sdk.Context, sdk.Tx, vmHandler) { ctx := env.ctx // conduct base gas meter tests from a non-genesis block since genesis block use infinite gas meter instead. ctx = ctx.WithBlockHeader(&bft.Header{Height: int64(1)}) - vmHandler := NewHandler(env.vmk) // Create an account with 10M ugnot (10gnot) addr := crypto.AddressFromPreimage([]byte("test1")) acc := env.acck.NewAccountWithAddress(ctx, addr) @@ -173,5 +172,5 @@ func Echo() UnknowType { fee := std.NewFee(500000, std.MustParseCoin("1ugnot")) tx := std.NewTx(msgs, fee, []std.Signature{}, "") - return ctx, tx, vmHandler + return ctx, tx, env.vmh } diff --git a/gno.land/pkg/sdk/vm/handler_test.go b/gno.land/pkg/sdk/vm/handler_test.go index 38ac8fa61b9..3e5431be78f 100644 --- a/gno.land/pkg/sdk/vm/handler_test.go +++ b/gno.land/pkg/sdk/vm/handler_test.go @@ -3,6 +3,9 @@ package vm import ( "testing" + abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types" + "github.com/gnolang/gno/tm2/pkg/crypto" + "github.com/gnolang/gno/tm2/pkg/std" "github.com/stretchr/testify/assert" ) @@ -48,3 +51,39 @@ func Test_parseQueryEval_panic(t *testing.T) { parseQueryEvalData("gno.land/r/demo/users") }) } + +// Call Run with stdlibs. +func TestQuery_Eval(t *testing.T) { + env := setupTestEnv() + ctx := env.ctx + vmHandler := env.vmh + + // 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{ + {"echo.gno", ` +package echo + +func Echo(msg string) string { + return "echo:"+msg +}`}, + } + pkgPath := "gno.land/r/echo" + msg1 := NewMsgAddPackage(addr, pkgPath, files) + err := env.vmk.AddPackage(ctx, msg1) + assert.NoError(t, err) + + req := abci.RequestQuery{ + Path: "vm/qeval", + Data: []byte(`gno.land/r/echo.Echo("hello")`), + } + res := vmHandler.Query(env.ctx, req) + assert.True(t, res.IsOK()) + assert.Equal(t, string(res.Data), `("echo:hello" string)`) +} From 22ebd637b261c5aa76163809741bfc435f29cd6c Mon Sep 17 00:00:00 2001 From: moul <94029+moul@users.noreply.github.com> Date: Fri, 28 Jun 2024 19:59:18 +0200 Subject: [PATCH 02/10] chore: fixup Signed-off-by: moul <94029+moul@users.noreply.github.com> --- gno.land/pkg/sdk/vm/handler_test.go | 131 ++++++++++++++++++++++------ 1 file changed, 103 insertions(+), 28 deletions(-) diff --git a/gno.land/pkg/sdk/vm/handler_test.go b/gno.land/pkg/sdk/vm/handler_test.go index 3e5431be78f..e130022a45c 100644 --- a/gno.land/pkg/sdk/vm/handler_test.go +++ b/gno.land/pkg/sdk/vm/handler_test.go @@ -1,6 +1,7 @@ package vm import ( + "fmt" "testing" abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types" @@ -53,37 +54,111 @@ func Test_parseQueryEval_panic(t *testing.T) { } // Call Run with stdlibs. -func TestQuery_Eval(t *testing.T) { - env := setupTestEnv() - ctx := env.ctx - vmHandler := env.vmh - - // 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"))) +func TestVmHandlerQuery_Eval(t *testing.T) { + tt := []struct { + input []byte + expectedResult string + expectedResultMatch string + expectedErrorMatch string + expectedPanicMatch string + // XXX: expectedEvents + }{ + // valid queries + {input: []byte(`gno.land/r/hello.Echo("hello")`), expectedResult: `("echo:hello" string)`}, + {input: []byte(`gno.land/r/hello.PubString`), expectedResult: `("public string" string)`}, + {input: []byte(`gno.land/r/hello.ConstString`), expectedResult: `("const string" string)`}, + {input: []byte(`gno.land/r/hello.pvString`), expectedResult: `("private string" string)`}, + {input: []byte(`gno.land/r/hello.counter`), expectedResult: `(42 int)`}, + {input: []byte(`gno.land/r/hello.GetCounter()`), expectedResult: `(42 int)`}, + {input: []byte(`gno.land/r/hello.Inc()`), expectedResult: `(43 int)`}, + {input: []byte(`gno.land/r/hello.pvEcho("hello")`), expectedResult: `("pvecho:hello" string)`}, + {input: []byte(`gno.land/r/hello.1337`), expectedResult: `(1337 int)`}, + {input: []byte(`gno.land/r/hello.myStructInst`), expectedResult: `(struct{(1000 int)} gno.land/r/hello.myStruct)`}, + {input: []byte(`gno.land/r/hello.myStructInst.Foo()`), expectedResult: `("myStruct.Foo" string)`}, + {input: []byte(`gno.land/r/hello.myStruct`), expectedResultMatch: `\(typeval{gno.land/r/hello.myStruct \(0x.*\)} type{}\)`}, + {input: []byte(`gno.land/r/hello.Inc`), expectedResult: `(Inc func()( int))`}, + {input: []byte(`gno.land/r/hello.fn()("hi")`), expectedResult: `("echo:hi" string)`}, + {input: []byte(`gno.land/r/hello.sl`), expectedResultMatch: `(slice[ref(.*)] []int)`}, // XXX: should return the actual value + {input: []byte(`gno.land/r/hello.sl[1]`), expectedResultMatch: `(slice[ref(.*)] []int)`}, // XXX: should return the actual value + {input: []byte(`gno.land/r/hello.println(1234)`), expectedResultMatch: `^$`}, // XXX: compare stdout? - // Create test package. - files := []*std.MemFile{ - {"echo.gno", ` -package echo + // panics + {input: []byte(`gno.land/r/hello`), expectedPanicMatch: `expected . syntax in query input data`}, -func Echo(msg string) string { - return "echo:"+msg -}`}, + // errors + {input: []byte(`gno.land/r/hello.doesnotexist`), expectedErrorMatch: `^/:0: name doesnotexist not declared:`}, // multiline error + {input: []byte(`gno.land/r/doesnotexist.Foo`), expectedErrorMatch: `^invalid package path$`}, + {input: []byte(`gno.land/r/hello.Panic()`), expectedErrorMatch: `^foo$`}, + {input: []byte(`gno.land/r/hello.sl[6]`), expectedErrorMatch: `^slice index out of bounds: 6 \(len=5\)$`}, } - pkgPath := "gno.land/r/echo" - msg1 := NewMsgAddPackage(addr, pkgPath, files) - err := env.vmk.AddPackage(ctx, msg1) - assert.NoError(t, err) - req := abci.RequestQuery{ - Path: "vm/qeval", - Data: []byte(`gno.land/r/echo.Echo("hello")`), + for _, tc := range tt { + name := string(tc.input) + t.Run(name, func(t *testing.T) { + env := setupTestEnv() + ctx := env.ctx + vmHandler := env.vmh + + // 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{ + {"hello.gno", ` +package hello + +var sl = []int{1,2,3,4,5} +func fn() func(string) string { return Echo } +type myStruct struct{a int} +var myStructInst = myStruct{a: 1000} +func (ms myStruct) Foo() string { return "myStruct.Foo" } +func Panic() { panic("foo") } +var counter int = 42 +var pvString = "private string" +var PubString = "public string" +const ConstString = "const string" +func Echo(msg string) string { return "echo:"+msg } +func GetCounter() int { return counter } +func Inc() int { counter += 1; return counter } +func pvEcho(msg string) string { return "pvecho:"+msg } +`}, + } + pkgPath := "gno.land/r/hello" + msg1 := NewMsgAddPackage(addr, pkgPath, files) + err := env.vmk.AddPackage(ctx, msg1) + assert.NoError(t, err) + + req := abci.RequestQuery{ + Path: "vm/qeval", + Data: tc.input, + } + + defer func() { + if r := recover(); r != nil { + output := fmt.Sprintf("%v", r) + assert.Regexp(t, tc.expectedPanicMatch, output) + } else { + assert.Equal(t, "", tc.expectedPanicMatch, "should not panic") + } + }() + res := vmHandler.Query(env.ctx, req) + if tc.expectedErrorMatch == "" { + assert.True(t, res.IsOK(), "should not have error") + if tc.expectedResult != "" { + assert.Equal(t, string(res.Data), tc.expectedResult) + } + if tc.expectedResultMatch != "" { + assert.Regexp(t, tc.expectedResultMatch, string(res.Data)) + } + } else { + assert.False(t, res.IsOK(), "should have an error") + errmsg := res.Error.Error() + assert.Regexp(t, tc.expectedErrorMatch, errmsg) + } + }) } - res := vmHandler.Query(env.ctx, req) - assert.True(t, res.IsOK()) - assert.Equal(t, string(res.Data), `("echo:hello" string)`) } From a969dd82f5290f5d454adfbb8a5900de1f97c1a7 Mon Sep 17 00:00:00 2001 From: moul <94029+moul@users.noreply.github.com> Date: Fri, 28 Jun 2024 20:02:43 +0200 Subject: [PATCH 03/10] chore: fixup Signed-off-by: moul <94029+moul@users.noreply.github.com> --- gno.land/pkg/sdk/vm/handler_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gno.land/pkg/sdk/vm/handler_test.go b/gno.land/pkg/sdk/vm/handler_test.go index e130022a45c..e03d56e3243 100644 --- a/gno.land/pkg/sdk/vm/handler_test.go +++ b/gno.land/pkg/sdk/vm/handler_test.go @@ -73,6 +73,8 @@ func TestVmHandlerQuery_Eval(t *testing.T) { {input: []byte(`gno.land/r/hello.Inc()`), expectedResult: `(43 int)`}, {input: []byte(`gno.land/r/hello.pvEcho("hello")`), expectedResult: `("pvecho:hello" string)`}, {input: []byte(`gno.land/r/hello.1337`), expectedResult: `(1337 int)`}, + {input: []byte(`gno.land/r/hello.13.37`), expectedResult: `(13.37 float64)`}, + {input: []byte(`gno.land/r/hello.float64(1337)`), expectedResult: `(1337 float64)`}, {input: []byte(`gno.land/r/hello.myStructInst`), expectedResult: `(struct{(1000 int)} gno.land/r/hello.myStruct)`}, {input: []byte(`gno.land/r/hello.myStructInst.Foo()`), expectedResult: `("myStruct.Foo" string)`}, {input: []byte(`gno.land/r/hello.myStruct`), expectedResultMatch: `\(typeval{gno.land/r/hello.myStruct \(0x.*\)} type{}\)`}, @@ -89,6 +91,7 @@ func TestVmHandlerQuery_Eval(t *testing.T) { {input: []byte(`gno.land/r/hello.doesnotexist`), expectedErrorMatch: `^/:0: name doesnotexist not declared:`}, // multiline error {input: []byte(`gno.land/r/doesnotexist.Foo`), expectedErrorMatch: `^invalid package path$`}, {input: []byte(`gno.land/r/hello.Panic()`), expectedErrorMatch: `^foo$`}, + {input: []byte(`gno.land/r/hello.panic("bar")`), expectedErrorMatch: `^bar$`}, {input: []byte(`gno.land/r/hello.sl[6]`), expectedErrorMatch: `^slice index out of bounds: 6 \(len=5\)$`}, } From f40bcf301fe15694294402c9ea11f0beb064a189 Mon Sep 17 00:00:00 2001 From: moul <94029+moul@users.noreply.github.com> Date: Fri, 28 Jun 2024 20:46:57 +0200 Subject: [PATCH 04/10] chore: fixup Signed-off-by: moul <94029+moul@users.noreply.github.com> --- gno.land/pkg/sdk/vm/handler_test.go | 88 ++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/gno.land/pkg/sdk/vm/handler_test.go b/gno.land/pkg/sdk/vm/handler_test.go index e03d56e3243..2db2797286e 100644 --- a/gno.land/pkg/sdk/vm/handler_test.go +++ b/gno.land/pkg/sdk/vm/handler_test.go @@ -53,7 +53,6 @@ func Test_parseQueryEval_panic(t *testing.T) { }) } -// Call Run with stdlibs. func TestVmHandlerQuery_Eval(t *testing.T) { tt := []struct { input []byte @@ -165,3 +164,90 @@ func pvEcho(msg string) string { return "pvecho:"+msg } }) } } + +func TestVmHandlerQuery_Funcs(t *testing.T) { + tt := []struct { + input []byte + expectedResult string + expectedResultMatch string + expectedErrorMatch string + expectedPanicMatch string + // XXX: expectedEvents + }{ + // valid queries + {input: []byte(`gno.land/r/hello`), expectedResult: `[{"FuncName":"Panic","Params":null,"Results":null},{"FuncName":"Echo","Params":[{"Name":"msg","Type":"string","Value":""}],"Results":[{"Name":"_","Type":"string","Value":""}]},{"FuncName":"GetCounter","Params":null,"Results":[{"Name":"_","Type":"int","Value":""}]},{"FuncName":"Inc","Params":null,"Results":[{"Name":"_","Type":"int","Value":""}]}]`}, + {input: []byte(`gno.land/r/doesnotexist`), expectedErrorMatch: `invalid package path`}, + {input: []byte(`std`), expectedErrorMatch: `invalid package path`}, + {input: []byte(`strings`), expectedErrorMatch: `invalid package path`}, + } + + for _, tc := range tt { + name := string(tc.input) + t.Run(name, func(t *testing.T) { + env := setupTestEnv() + ctx := env.ctx + vmHandler := env.vmh + + // 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{ + {"hello.gno", ` +package hello + +var sl = []int{1,2,3,4,5} +func fn() func(string) string { return Echo } +type myStruct struct{a int} +var myStructInst = myStruct{a: 1000} +func (ms myStruct) Foo() string { return "myStruct.Foo" } +func Panic() { panic("foo") } +var counter int = 42 +var pvString = "private string" +var PubString = "public string" +const ConstString = "const string" +func Echo(msg string) string { return "echo:"+msg } +func GetCounter() int { return counter } +func Inc() int { counter += 1; return counter } +func pvEcho(msg string) string { return "pvecho:"+msg } +`}, + } + pkgPath := "gno.land/r/hello" + msg1 := NewMsgAddPackage(addr, pkgPath, files) + err := env.vmk.AddPackage(ctx, msg1) + assert.NoError(t, err) + + req := abci.RequestQuery{ + Path: "vm/qfuncs", + Data: tc.input, + } + + defer func() { + if r := recover(); r != nil { + output := fmt.Sprintf("%v", r) + assert.Regexp(t, tc.expectedPanicMatch, output) + } else { + assert.Equal(t, "", tc.expectedPanicMatch, "should not panic") + } + }() + res := vmHandler.Query(env.ctx, req) + if tc.expectedErrorMatch == "" { + assert.True(t, res.IsOK(), "should not have error") + if tc.expectedResult != "" { + assert.Equal(t, string(res.Data), tc.expectedResult) + } + if tc.expectedResultMatch != "" { + assert.Regexp(t, tc.expectedResultMatch, string(res.Data)) + } + } else { + assert.False(t, res.IsOK(), "should have an error") + errmsg := res.Error.Error() + assert.Regexp(t, tc.expectedErrorMatch, errmsg) + } + }) + } +} From b9062d237e316b569d822521d4139bc1c59da0b8 Mon Sep 17 00:00:00 2001 From: moul <94029+moul@users.noreply.github.com> Date: Fri, 28 Jun 2024 20:58:04 +0200 Subject: [PATCH 05/10] chore: fixup Signed-off-by: moul <94029+moul@users.noreply.github.com> --- gno.land/pkg/sdk/vm/handler_test.go | 71 ++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 6 deletions(-) diff --git a/gno.land/pkg/sdk/vm/handler_test.go b/gno.land/pkg/sdk/vm/handler_test.go index 2db2797286e..09e4d194570 100644 --- a/gno.land/pkg/sdk/vm/handler_test.go +++ b/gno.land/pkg/sdk/vm/handler_test.go @@ -167,12 +167,9 @@ func pvEcho(msg string) string { return "pvecho:"+msg } func TestVmHandlerQuery_Funcs(t *testing.T) { tt := []struct { - input []byte - expectedResult string - expectedResultMatch string - expectedErrorMatch string - expectedPanicMatch string - // XXX: expectedEvents + input []byte + expectedResult string + expectedErrorMatch string }{ // valid queries {input: []byte(`gno.land/r/hello`), expectedResult: `[{"FuncName":"Panic","Params":null,"Results":null},{"FuncName":"Echo","Params":[{"Name":"msg","Type":"string","Value":""}],"Results":[{"Name":"_","Type":"string","Value":""}]},{"FuncName":"GetCounter","Params":null,"Results":[{"Name":"_","Type":"int","Value":""}]},{"FuncName":"Inc","Params":null,"Results":[{"Name":"_","Type":"int","Value":""}]}]`}, @@ -226,6 +223,68 @@ func pvEcho(msg string) string { return "pvecho:"+msg } Data: tc.input, } + res := vmHandler.Query(env.ctx, req) + if tc.expectedErrorMatch == "" { + assert.True(t, res.IsOK(), "should not have error") + if tc.expectedResult != "" { + assert.Equal(t, string(res.Data), tc.expectedResult) + } + } else { + assert.False(t, res.IsOK(), "should have an error") + errmsg := res.Error.Error() + assert.Regexp(t, tc.expectedErrorMatch, errmsg) + } + }) + } +} + +func TestVmHandlerQuery_File(t *testing.T) { + tt := []struct { + input []byte + expectedResult string + expectedResultMatch string + expectedErrorMatch string + expectedPanicMatch string + // XXX: expectedEvents + }{ + // valid queries + {input: []byte(`gno.land/r/hello/hello.gno`), expectedResult: "package hello\nfunc Hello() string { return \"hello\" }"}, + {input: []byte(`gno.land/r/hello/README.md`), expectedResult: "# Hello"}, + {input: []byte(`gno.land/r/hello/doesnotexist.gno`), expectedErrorMatch: `file "gno.land/r/hello/doesnotexist.gno" is not available`}, + {input: []byte(`gno.land/r/hello`), expectedResult: "README.md\nhello.gno"}, + {input: []byte(`gno.land/r/doesnotexist`), expectedErrorMatch: `package "gno.land/r/doesnotexist" is not available`}, + {input: []byte(`gno.land/r/doesnotexist/hello.gno`), expectedErrorMatch: `file "gno.land/r/doesnotexist/hello.gno" is not available`}, + } + + for _, tc := range tt { + name := string(tc.input) + t.Run(name, func(t *testing.T) { + env := setupTestEnv() + ctx := env.ctx + vmHandler := env.vmh + + // 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{ + {"README.md", "# Hello"}, + {"hello.gno", "package hello\nfunc Hello() string { return \"hello\" }"}, + } + pkgPath := "gno.land/r/hello" + msg1 := NewMsgAddPackage(addr, pkgPath, files) + err := env.vmk.AddPackage(ctx, msg1) + assert.NoError(t, err) + + req := abci.RequestQuery{ + Path: "vm/qfile", + Data: tc.input, + } + defer func() { if r := recover(); r != nil { output := fmt.Sprintf("%v", r) From e9e18a0edb9a1b2b43d6e4e23003f9d67cc76132 Mon Sep 17 00:00:00 2001 From: moul <94029+moul@users.noreply.github.com> Date: Wed, 3 Jul 2024 07:28:55 -0700 Subject: [PATCH 06/10] chore: fixup Signed-off-by: moul <94029+moul@users.noreply.github.com> --- gno.land/pkg/sdk/vm/handler_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gno.land/pkg/sdk/vm/handler_test.go b/gno.land/pkg/sdk/vm/handler_test.go index 09e4d194570..b918749e870 100644 --- a/gno.land/pkg/sdk/vm/handler_test.go +++ b/gno.land/pkg/sdk/vm/handler_test.go @@ -87,7 +87,7 @@ func TestVmHandlerQuery_Eval(t *testing.T) { {input: []byte(`gno.land/r/hello`), expectedPanicMatch: `expected . syntax in query input data`}, // errors - {input: []byte(`gno.land/r/hello.doesnotexist`), expectedErrorMatch: `^/:0: name doesnotexist not declared:`}, // multiline error + {input: []byte(`gno.land/r/hello.doesnotexikst`), expectedErrorMatch: `^/0:0: name doesnotexist not declared:`}, // multiline error {input: []byte(`gno.land/r/doesnotexist.Foo`), expectedErrorMatch: `^invalid package path$`}, {input: []byte(`gno.land/r/hello.Panic()`), expectedErrorMatch: `^foo$`}, {input: []byte(`gno.land/r/hello.panic("bar")`), expectedErrorMatch: `^bar$`}, From 363a930b97cff2a40fd8476524180f658c315a70 Mon Sep 17 00:00:00 2001 From: moul <94029+moul@users.noreply.github.com> Date: Wed, 3 Jul 2024 14:39:16 -0700 Subject: [PATCH 07/10] chore: fixup Signed-off-by: moul <94029+moul@users.noreply.github.com> --- gno.land/pkg/sdk/vm/handler_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gno.land/pkg/sdk/vm/handler_test.go b/gno.land/pkg/sdk/vm/handler_test.go index b918749e870..d03e3e4677c 100644 --- a/gno.land/pkg/sdk/vm/handler_test.go +++ b/gno.land/pkg/sdk/vm/handler_test.go @@ -87,7 +87,7 @@ func TestVmHandlerQuery_Eval(t *testing.T) { {input: []byte(`gno.land/r/hello`), expectedPanicMatch: `expected . syntax in query input data`}, // errors - {input: []byte(`gno.land/r/hello.doesnotexikst`), expectedErrorMatch: `^/0:0: name doesnotexist not declared:`}, // multiline error + {input: []byte(`gno.land/r/hello.doesnotexist`), expectedErrorMatch: `^/:0:0: name doesnotexist not declared:`}, // multiline error {input: []byte(`gno.land/r/doesnotexist.Foo`), expectedErrorMatch: `^invalid package path$`}, {input: []byte(`gno.land/r/hello.Panic()`), expectedErrorMatch: `^foo$`}, {input: []byte(`gno.land/r/hello.panic("bar")`), expectedErrorMatch: `^bar$`}, From 056ab0a3f5edb3e54e0735a3b9ed7846fe6f32b6 Mon Sep 17 00:00:00 2001 From: moul <94029+moul@users.noreply.github.com> Date: Sat, 21 Sep 2024 21:23:40 +0200 Subject: [PATCH 08/10] chore: fixup Signed-off-by: moul <94029+moul@users.noreply.github.com> --- gno.land/pkg/sdk/vm/common_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/gno.land/pkg/sdk/vm/common_test.go b/gno.land/pkg/sdk/vm/common_test.go index 1a2b3f47bc8..649d1f2031d 100644 --- a/gno.land/pkg/sdk/vm/common_test.go +++ b/gno.land/pkg/sdk/vm/common_test.go @@ -61,6 +61,7 @@ func _setupTestEnv(cacheStdlibs bool) testEnv { } vmk.CommitGnoTransactionStore(stdlibCtx) mcw.MultiWrite() + vmh := NewHandler(vmk) return testEnv{ctx: ctx, vmk: vmk, bank: bank, acck: acck, vmh: vmh} } From bb9366db99ac88798ad1207ece541450f975c466 Mon Sep 17 00:00:00 2001 From: moul <94029+moul@users.noreply.github.com> Date: Sun, 22 Sep 2024 09:28:05 +0200 Subject: [PATCH 09/10] chore: fixup Signed-off-by: moul <94029+moul@users.noreply.github.com> --- gno.land/pkg/sdk/vm/handler_test.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/gno.land/pkg/sdk/vm/handler_test.go b/gno.land/pkg/sdk/vm/handler_test.go index d03e3e4677c..dafd641a15e 100644 --- a/gno.land/pkg/sdk/vm/handler_test.go +++ b/gno.land/pkg/sdk/vm/handler_test.go @@ -64,6 +64,9 @@ func TestVmHandlerQuery_Eval(t *testing.T) { }{ // valid queries {input: []byte(`gno.land/r/hello.Echo("hello")`), expectedResult: `("echo:hello" string)`}, + {input: []byte(`gno.land/r/hello.caller()`), expectedResult: `("" std.Address)`}, // FIXME? + {input: []byte(`gno.land/r/hello.GetHeight()`), expectedResult: `(0 int64)`}, + // {input: []byte(`gno.land/r/hello.time.RFC3339`), expectedResult: `test`}, // not working, but should we care? {input: []byte(`gno.land/r/hello.PubString`), expectedResult: `("public string" string)`}, {input: []byte(`gno.land/r/hello.ConstString`), expectedResult: `("const string" string)`}, {input: []byte(`gno.land/r/hello.pvString`), expectedResult: `("private string" string)`}, @@ -90,7 +93,6 @@ func TestVmHandlerQuery_Eval(t *testing.T) { {input: []byte(`gno.land/r/hello.doesnotexist`), expectedErrorMatch: `^/:0:0: name doesnotexist not declared:`}, // multiline error {input: []byte(`gno.land/r/doesnotexist.Foo`), expectedErrorMatch: `^invalid package path$`}, {input: []byte(`gno.land/r/hello.Panic()`), expectedErrorMatch: `^foo$`}, - {input: []byte(`gno.land/r/hello.panic("bar")`), expectedErrorMatch: `^bar$`}, {input: []byte(`gno.land/r/hello.sl[6]`), expectedErrorMatch: `^slice index out of bounds: 6 \(len=5\)$`}, } @@ -98,7 +100,7 @@ func TestVmHandlerQuery_Eval(t *testing.T) { name := string(tc.input) t.Run(name, func(t *testing.T) { env := setupTestEnv() - ctx := env.ctx + ctx := env.vmk.MakeGnoTransactionStore(env.ctx) vmHandler := env.vmh // Give "addr1" some gnots. @@ -113,6 +115,12 @@ func TestVmHandlerQuery_Eval(t *testing.T) { {"hello.gno", ` package hello +import "std" +import "time" + +var _ = time.RFC3339 +func caller() std.Address { return std.GetOrigCaller() } +var GetHeight = std.GetHeight var sl = []int{1,2,3,4,5} func fn() func(string) string { return Echo } type myStruct struct{a int} @@ -133,6 +141,7 @@ func pvEcho(msg string) string { return "pvecho:"+msg } msg1 := NewMsgAddPackage(addr, pkgPath, files) err := env.vmk.AddPackage(ctx, msg1) assert.NoError(t, err) + env.vmk.CommitGnoTransactionStore(ctx) req := abci.RequestQuery{ Path: "vm/qeval", From 6c935a8ff9ef5e06a3a6f3e62cf97eb868a6ecc0 Mon Sep 17 00:00:00 2001 From: moul <94029+moul@users.noreply.github.com> Date: Thu, 3 Oct 2024 18:16:39 +0200 Subject: [PATCH 10/10] chore: fixup Signed-off-by: moul <94029+moul@users.noreply.github.com> --- gno.land/pkg/sdk/vm/handler_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gno.land/pkg/sdk/vm/handler_test.go b/gno.land/pkg/sdk/vm/handler_test.go index dafd641a15e..ab4d3a2d159 100644 --- a/gno.land/pkg/sdk/vm/handler_test.go +++ b/gno.land/pkg/sdk/vm/handler_test.go @@ -191,7 +191,7 @@ func TestVmHandlerQuery_Funcs(t *testing.T) { name := string(tc.input) t.Run(name, func(t *testing.T) { env := setupTestEnv() - ctx := env.ctx + ctx := env.vmk.MakeGnoTransactionStore(env.ctx) vmHandler := env.vmh // Give "addr1" some gnots. @@ -257,7 +257,7 @@ func TestVmHandlerQuery_File(t *testing.T) { // XXX: expectedEvents }{ // valid queries - {input: []byte(`gno.land/r/hello/hello.gno`), expectedResult: "package hello\nfunc Hello() string { return \"hello\" }"}, + {input: []byte(`gno.land/r/hello/hello.gno`), expectedResult: "package hello\n\nfunc Hello() string { return \"hello\" }\n"}, {input: []byte(`gno.land/r/hello/README.md`), expectedResult: "# Hello"}, {input: []byte(`gno.land/r/hello/doesnotexist.gno`), expectedErrorMatch: `file "gno.land/r/hello/doesnotexist.gno" is not available`}, {input: []byte(`gno.land/r/hello`), expectedResult: "README.md\nhello.gno"}, @@ -269,7 +269,7 @@ func TestVmHandlerQuery_File(t *testing.T) { name := string(tc.input) t.Run(name, func(t *testing.T) { env := setupTestEnv() - ctx := env.ctx + ctx := env.vmk.MakeGnoTransactionStore(env.ctx) vmHandler := env.vmh // Give "addr1" some gnots. @@ -282,7 +282,7 @@ func TestVmHandlerQuery_File(t *testing.T) { // Create test package. files := []*std.MemFile{ {"README.md", "# Hello"}, - {"hello.gno", "package hello\nfunc Hello() string { return \"hello\" }"}, + {"hello.gno", "package hello\n\nfunc Hello() string { return \"hello\" }\n"}, } pkgPath := "gno.land/r/hello" msg1 := NewMsgAddPackage(addr, pkgPath, files)