Skip to content

Commit

Permalink
fix(gno test): pass ExecContext when initializing imported packages (#…
Browse files Browse the repository at this point in the history
…1965)

Cherry-picked from
8fe571f
(#880).

<details><summary>Contributors' checklist...</summary>

- [ ] 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).
</details>

---------

Signed-off-by: moul <94029+moul@users.noreply.github.com>
Co-authored-by: Morgan Bazalgette <morgan@morganbaz.com>
  • Loading branch information
moul and thehowl committed May 9, 2024
1 parent 91ac58b commit abaf103
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 18 deletions.
6 changes: 6 additions & 0 deletions examples/gno.land/r/demo/tests/tests.gno
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ func CurrentRealmPath() string {
return std.CurrentRealmPath()
}

var initOrigCaller = std.GetOrigCaller()

func InitOrigCaller() std.Address {
return initOrigCaller
}

func AssertOriginCall() {
std.AssertOriginCall()
}
Expand Down
27 changes: 27 additions & 0 deletions gno.land/cmd/gnoland/testdata/initctx.txtar
Original file line number Diff line number Diff line change
@@ -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()
}
21 changes: 13 additions & 8 deletions gnovm/tests/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 {
Expand Down
27 changes: 27 additions & 0 deletions gnovm/tests/files/zrealm_initctx.gno
Original file line number Diff line number Diff line change
@@ -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"]
86 changes: 76 additions & 10 deletions gnovm/tests/files/zrealm_tests0.gno
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
// "Line": "42",
// "Line": "48",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
Expand Down Expand Up @@ -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": {
Expand All @@ -623,7 +679,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
// "Line": "23",
// "Line": "29",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
Expand Down Expand Up @@ -669,7 +725,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
// "Line": "27",
// "Line": "33",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
Expand Down Expand Up @@ -728,7 +784,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
// "Line": "38",
// "Line": "44",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
Expand Down Expand Up @@ -777,7 +833,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
// "Line": "60",
// "Line": "66",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
Expand Down Expand Up @@ -813,7 +869,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
// "Line": "65",
// "Line": "71",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
Expand Down Expand Up @@ -849,7 +905,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
// "Line": "73",
// "Line": "79",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
Expand Down Expand Up @@ -895,7 +951,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
// "Line": "77",
// "Line": "83",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
Expand Down Expand Up @@ -951,7 +1007,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
// "Line": "81",
// "Line": "87",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
Expand Down Expand Up @@ -1008,7 +1064,7 @@ func main() {
// "BlockNode": null,
// "Location": {
// "File": "tests.gno",
// "Line": "85",
// "Line": "91",
// "Nonce": "0",
// "PkgPath": "gno.land/r/demo/tests"
// }
Expand Down Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions gnovm/tests/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit abaf103

Please sign in to comment.