Skip to content

Commit

Permalink
fix(cmd/gno): pass an ExecContext to MachineOptions in gno run (#2856)
Browse files Browse the repository at this point in the history
This fixes: #2834

It tries to hew very closely to how the context is created under `gno
test` so that there is consistency of results between code that is
executed in the test and run contexts. Please let me know if there are
any conventions for the codebase that I should have followed that I did
not.

I hooked into the portion of the `ExecRun()` that was calling
`NewMachineWithOptions()`, and instead followed a similar pattern to
what is defined in `test.go` to configure the machine with a context
that has reasonable defaults. These, except for the Chain ID (which is
`dev` when running under `gno test`), are identical to the test
settings.

This allows packages like `gno.land/p/demo/entropy` to work when code is
executed with `gno run`, as well as any others which might try to access
information only available from the context.

A simple piece of code to demonstrate the issue is below. This will fail
without this change.

```
package foo

import (
  "fmt"
  "std"
)

func main() {
  fmt.Printf("GetHeight(): %d\n", std.GetHeight())
}
```

A test has been added to the tests for the run command, which tries to
run code similar to the code above within the test.

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

- [X] Added new tests, or not needed, or not feasible
- [X] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [X] Updated the official documentation or not needed
- [X] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [X] Added references to related issues and PRs
- [X] Provided any useful hints for running manual tests
</details>
  • Loading branch information
wyhaines committed Sep 27, 2024
1 parent 7a1a966 commit 14d4d21
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
9 changes: 7 additions & 2 deletions gnovm/cmd/gno/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/tests"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/std"
)

type runCfg struct {
Expand Down Expand Up @@ -112,11 +113,15 @@ func execRun(cfg *runCfg, args []string, io commands.IO) error {
return errors.New("no files to run")
}

var send std.Coins
pkgPath := string(files[0].PkgName)
ctx := tests.TestContext(pkgPath, send)
m := gno.NewMachineWithOptions(gno.MachineOptions{
PkgPath: string(files[0].PkgName),
Input: stdin,
PkgPath: pkgPath,
Output: stdout,
Input: stdin,
Store: testStore,
Context: ctx,
Debug: cfg.debug || cfg.debugAddr != "",
})

Expand Down
4 changes: 4 additions & 0 deletions gnovm/cmd/gno/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func TestRunApp(t *testing.T) {
args: []string{"run", "../../tests/integ/invalid_assign/main.gno"},
recoverShouldContain: "cannot use bool as main.C without explicit conversion",
},
{
args: []string{"run", "-expr", "Context()", "../../tests/integ/context/context.gno"},
stdoutShouldContain: "Context worked",
},
// TODO: a test file
// TODO: args
// TODO: nativeLibs VS stdlibs
Expand Down
11 changes: 11 additions & 0 deletions gnovm/tests/integ/context/context.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package runtests

import (
"fmt"
"std"
)

func Context() {
// This requires a Context to work; it will fail ugly if the Context isn't available.
fmt.Printf("Context worked: %d\n", std.GetHeight())
}

0 comments on commit 14d4d21

Please sign in to comment.