From abaf10369fcfb73316322cc988f7756baa9c0e78 Mon Sep 17 00:00:00 2001
From: Manfred Touron <94029+moul@users.noreply.github.com>
Date: Thu, 9 May 2024 18:11:05 +0200
Subject: [PATCH] fix(gno test): pass ExecContext when initializing imported
packages (#1965)
Cherry-picked from
https://github.com/gnolang/gno/pull/880/commits/8fe571fb7a01bc855b78484a028c41c2ed925915
(#880).
Contributors' checklist...
- [ ] Added new tests, or not needed, or not feasible
- [ ] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [ ] Updated the official documentation or not needed
- [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [ ] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
---------
Signed-off-by: moul <94029+moul@users.noreply.github.com>
Co-authored-by: Morgan Bazalgette
---
examples/gno.land/r/demo/tests/tests.gno | 6 ++
gno.land/cmd/gnoland/testdata/initctx.txtar | 27 +++++++
gnovm/tests/file.go | 21 +++--
gnovm/tests/files/zrealm_initctx.gno | 27 +++++++
gnovm/tests/files/zrealm_tests0.gno | 86 ++++++++++++++++++---
gnovm/tests/imports.go | 7 ++
6 files changed, 156 insertions(+), 18 deletions(-)
create mode 100644 gno.land/cmd/gnoland/testdata/initctx.txtar
create mode 100644 gnovm/tests/files/zrealm_initctx.gno
diff --git a/examples/gno.land/r/demo/tests/tests.gno b/examples/gno.land/r/demo/tests/tests.gno
index 0094ad2ae35..2062df6903d 100644
--- a/examples/gno.land/r/demo/tests/tests.gno
+++ b/examples/gno.land/r/demo/tests/tests.gno
@@ -20,6 +20,12 @@ func CurrentRealmPath() string {
return std.CurrentRealmPath()
}
+var initOrigCaller = std.GetOrigCaller()
+
+func InitOrigCaller() std.Address {
+ return initOrigCaller
+}
+
func AssertOriginCall() {
std.AssertOriginCall()
}
diff --git a/gno.land/cmd/gnoland/testdata/initctx.txtar b/gno.land/cmd/gnoland/testdata/initctx.txtar
new file mode 100644
index 00000000000..9210268e66f
--- /dev/null
+++ b/gno.land/cmd/gnoland/testdata/initctx.txtar
@@ -0,0 +1,27 @@
+loadpkg gno.land/r/foobar/bar $WORK/bar
+
+## start a new node
+gnoland start
+
+# execute Render
+gnokey maketx call -pkgpath gno.land/r/foobar/bar -func Render -args X -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1
+stdout OK!
+stdout '(" orig=g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 prev=g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" string)'
+stdout 'OK!'
+
+-- bar/bar.gno --
+package bar
+
+import "std"
+
+var orig = std.Address("orig")
+var prev = std.Address("prev")
+
+func init() {
+ orig = std.GetOrigCaller()
+ prev = std.PrevRealm().Addr()
+}
+
+func Render(addr string) string {
+ return " orig="+orig.String()+" prev="+prev.String()
+}
diff --git a/gnovm/tests/file.go b/gnovm/tests/file.go
index 9e4bdaf9e9a..8db662e53fb 100644
--- a/gnovm/tests/file.go
+++ b/gnovm/tests/file.go
@@ -35,6 +35,18 @@ func TestMachine(store gno.Store, stdout io.Writer, pkgPath string) *gno.Machine
}
func testMachineCustom(store gno.Store, pkgPath string, stdout io.Writer, maxAlloc int64, send std.Coins) *gno.Machine {
+ ctx := testContext(pkgPath, send)
+ m := gno.NewMachineWithOptions(gno.MachineOptions{
+ PkgPath: "", // set later.
+ Output: stdout,
+ Store: store,
+ Context: ctx,
+ MaxAllocBytes: maxAlloc,
+ })
+ return m
+}
+
+func testContext(pkgPath string, send std.Coins) stdlibs.ExecContext {
// FIXME: create a better package to manage this, with custom constructors
pkgAddr := gno.DerivePkgAddr(pkgPath) // the addr of the pkgPath called.
caller := gno.DerivePkgAddr("user1.gno")
@@ -53,14 +65,7 @@ func testMachineCustom(store gno.Store, pkgPath string, stdout io.Writer, maxAll
Banker: banker,
EventLogger: sdk.NewEventLogger(),
}
- m := gno.NewMachineWithOptions(gno.MachineOptions{
- PkgPath: "", // set later.
- Output: stdout,
- Store: store,
- Context: ctx,
- MaxAllocBytes: maxAlloc,
- })
- return m
+ return ctx
}
type runFileTestOptions struct {
diff --git a/gnovm/tests/files/zrealm_initctx.gno b/gnovm/tests/files/zrealm_initctx.gno
new file mode 100644
index 00000000000..2fda65e7681
--- /dev/null
+++ b/gnovm/tests/files/zrealm_initctx.gno
@@ -0,0 +1,27 @@
+// PKGPATH: gno.land/r/demo/tests_test
+package tests_test
+
+import (
+ "gno.land/r/demo/tests"
+ "std"
+)
+
+var addr = std.Address("test")
+var addrInit = std.Address("addrInit")
+
+func init() {
+ addr = std.GetOrigCaller()
+ addrInit = tests.InitOrigCaller()
+}
+
+func main() {
+ println(addr)
+ println(addrInit)
+}
+
+// Output:
+// g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm
+// g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm
+
+// Realm:
+// switchrealm["gno.land/r/demo/tests_test"]
diff --git a/gnovm/tests/files/zrealm_tests0.gno b/gnovm/tests/files/zrealm_tests0.gno
index 73d07f726eb..79a930b5ecc 100644
--- a/gnovm/tests/files/zrealm_tests0.gno
+++ b/gnovm/tests/files/zrealm_tests0.gno
@@ -247,7 +247,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
-// "Line": "42",
+// "Line": "48",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
@@ -603,6 +603,62 @@ func main() {
// "T": {
// "@type": "/gno.FuncType",
// "Params": [],
+// "Results": [
+// {
+// "Embedded": false,
+// "Name": "",
+// "Tag": "",
+// "Type": {
+// "@type": "/gno.RefType",
+// "ID": "std.Address"
+// }
+// }
+// ]
+// },
+// "V": {
+// "@type": "/gno.FuncValue",
+// "Closure": {
+// "@type": "/gno.RefValue",
+// "Escaped": true,
+// "ObjectID": "0ffe7732b4d549b4cf9ec18bd68641cd2c75ad0a:4"
+// },
+// "FileName": "tests.gno",
+// "IsMethod": false,
+// "Name": "InitOrigCaller",
+// "NativeName": "",
+// "NativePkg": "",
+// "PkgPath": "gno.land/r/demo/tests",
+// "Source": {
+// "@type": "/gno.RefNode",
+// "BlockNode": null,
+// "Location": {
+// "File": "tests.gno",
+// "Line": "25",
+// "Nonce": "0",
+// "PkgPath": "gno.land/r/demo/tests"
+// }
+// },
+// "Type": {
+// "@type": "/gno.FuncType",
+// "Params": [],
+// "Results": [
+// {
+// "Embedded": false,
+// "Name": "",
+// "Tag": "",
+// "Type": {
+// "@type": "/gno.RefType",
+// "ID": "std.Address"
+// }
+// }
+// ]
+// }
+// }
+// },
+// {
+// "T": {
+// "@type": "/gno.FuncType",
+// "Params": [],
// "Results": []
// },
// "V": {
@@ -623,7 +679,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
-// "Line": "23",
+// "Line": "29",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
@@ -669,7 +725,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
-// "Line": "27",
+// "Line": "33",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
@@ -728,7 +784,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
-// "Line": "38",
+// "Line": "44",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
@@ -777,7 +833,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
-// "Line": "60",
+// "Line": "66",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
@@ -813,7 +869,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
-// "Line": "65",
+// "Line": "71",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
@@ -849,7 +905,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
-// "Line": "73",
+// "Line": "79",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
@@ -895,7 +951,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
-// "Line": "77",
+// "Line": "83",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
@@ -951,7 +1007,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
-// "Line": "81",
+// "Line": "87",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
@@ -1008,7 +1064,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
-// "Line": "85",
+// "Line": "91",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
@@ -1060,6 +1116,16 @@ func main() {
// },
// {
// "T": {
+// "@type": "/gno.RefType",
+// "ID": "std.Address"
+// },
+// "V": {
+// "@type": "/gno.StringValue",
+// "value": "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"
+// }
+// },
+// {
+// "T": {
// "@type": "/gno.PointerType",
// "Elt": {
// "@type": "/gno.RefType",
diff --git a/gnovm/tests/imports.go b/gnovm/tests/imports.go
index 389c6717780..43d0969c007 100644
--- a/gnovm/tests/imports.go
+++ b/gnovm/tests/imports.go
@@ -43,6 +43,7 @@ import (
teststdlibs "github.com/gnolang/gno/gnovm/tests/stdlibs"
"github.com/gnolang/gno/tm2/pkg/db/memdb"
osm "github.com/gnolang/gno/tm2/pkg/os"
+ "github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/gno/tm2/pkg/store/dbadapter"
"github.com/gnolang/gno/tm2/pkg/store/iavl"
stypes "github.com/gnolang/gno/tm2/pkg/store/types"
@@ -78,10 +79,13 @@ func TestStore(rootDir, filesPath string, stdin io.Reader, stdout, stderr io.Wri
if strings.HasPrefix(pkgPath, testPath) {
baseDir := filepath.Join(filesPath, "extern", pkgPath[len(testPath):])
memPkg := gno.ReadMemPackage(baseDir, pkgPath)
+ send := std.Coins{}
+ ctx := testContext(pkgPath, send)
m2 := gno.NewMachineWithOptions(gno.MachineOptions{
PkgPath: "test",
Output: stdout,
Store: store,
+ Context: ctx,
})
// pkg := gno.NewPackageNode(gno.Name(memPkg.Name), memPkg.Path, nil)
// pv := pkg.NewPackage()
@@ -391,10 +395,13 @@ func TestStore(rootDir, filesPath string, stdin io.Reader, stdout, stderr io.Wri
panic(fmt.Sprintf("found an empty package %q", pkgPath))
}
+ send := std.Coins{}
+ ctx := testContext(pkgPath, send)
m2 := gno.NewMachineWithOptions(gno.MachineOptions{
PkgPath: "test",
Output: stdout,
Store: store,
+ Context: ctx,
})
pn, pv = m2.RunMemPackage(memPkg, true)
return