Skip to content

Commit

Permalink
fix(gnovm): don't print to stdout by default (#3076)
Browse files Browse the repository at this point in the history
Attempt to fix #3075 

cc/ @zivkovicmilos @sw360cab
  • Loading branch information
thehowl authored Nov 7, 2024
1 parent 81a88a2 commit 9129e4e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
2 changes: 2 additions & 0 deletions contribs/gnodev/pkg/dev/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"sync"
Expand Down Expand Up @@ -576,5 +577,6 @@ func newNodeConfig(tmc *tmcfg.Config, chainid string, appstate gnoland.GnoGenesi
PrivValidator: pv,
TMConfig: tmc,
Genesis: genesis,
VMOutput: os.Stdout,
}
}
5 changes: 4 additions & 1 deletion gno.land/pkg/gnoland/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gnoland

import (
"fmt"
"io"
"log/slog"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -36,10 +37,11 @@ type AppOptions struct {
DB dbm.DB // required
Logger *slog.Logger // required
EventSwitch events.EventSwitch // required
VMOutput io.Writer // optional
InitChainerConfig // options related to InitChainer
}

// DefaultAppOptions provides a "ready" default [AppOptions] for use with
// TestAppOptions provides a "ready" default [AppOptions] for use with
// [NewAppWithOptions], using the provided db.
func TestAppOptions(db dbm.DB) *AppOptions {
return &AppOptions{
Expand Down Expand Up @@ -91,6 +93,7 @@ func NewAppWithOptions(cfg *AppOptions) (abci.Application, error) {
bankKpr := bank.NewBankKeeper(acctKpr)
paramsKpr := params.NewParamsKeeper(mainKey, "vm")
vmk := vm.NewVMKeeper(baseKey, mainKey, acctKpr, bankKpr, paramsKpr)
vmk.Output = cfg.VMOutput

// Set InitChainer
icc := cfg.InitChainerConfig
Expand Down
3 changes: 3 additions & 0 deletions gno.land/pkg/gnoland/node_inmemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gnoland

import (
"fmt"
"io"
"log/slog"
"path/filepath"
"time"
Expand All @@ -23,6 +24,7 @@ type InMemoryNodeConfig struct {
Genesis *bft.GenesisDoc
TMConfig *tmcfg.Config
DB *memdb.MemDB // will be initialized if nil
VMOutput io.Writer // optional

// If StdlibDir not set, then it's filepath.Join(TMConfig.RootDir, "gnovm", "stdlibs")
InitChainerConfig
Expand Down Expand Up @@ -107,6 +109,7 @@ func NewInMemoryNode(logger *slog.Logger, cfg *InMemoryNodeConfig) (*node.Node,
DB: cfg.DB,
EventSwitch: evsw,
InitChainerConfig: cfg.InitChainerConfig,
VMOutput: cfg.VMOutput,
})
if err != nil {
return nil, fmt.Errorf("error initializing new app: %w", err)
Expand Down
28 changes: 17 additions & 11 deletions gno.land/pkg/sdk/vm/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"bytes"
"context"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -57,6 +57,9 @@ var _ VMKeeperI = &VMKeeper{}

// VMKeeper holds all package code and store state.
type VMKeeper struct {
// Needs to be explicitly set, like in the case of gnodev.
Output io.Writer

baseKey store.StoreKey
iavlKey store.StoreKey
acck auth.AccountKeeper
Expand Down Expand Up @@ -108,7 +111,7 @@ func (vm *VMKeeper) Initialize(
m2 := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: "",
Output: os.Stdout, // XXX
Output: vm.Output,
Store: vm.gnoStore,
})
defer m2.Release()
Expand Down Expand Up @@ -191,8 +194,7 @@ func loadStdlibPackage(pkgPath, stdlibDir string, store gno.Store) {
m := gno.NewMachineWithOptions(gno.MachineOptions{
PkgPath: "gno.land/r/stdlibs/" + pkgPath,
// PkgPath: pkgPath, XXX why?
Output: os.Stdout,
Store: store,
Store: store,
})
defer m.Release()
m.RunMemPackage(memPkg, true)
Expand Down Expand Up @@ -275,7 +277,7 @@ func (vm *VMKeeper) checkNamespacePermission(ctx sdk.Context, creator crypto.Add
m := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: "",
Output: os.Stdout, // XXX
Output: vm.Output,
Store: store,
Context: msgCtx,
Alloc: store.GetAllocator(),
Expand Down Expand Up @@ -376,7 +378,7 @@ func (vm *VMKeeper) AddPackage(ctx sdk.Context, msg MsgAddPackage) (err error) {
m2 := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: "",
Output: os.Stdout, // XXX
Output: vm.Output,
Store: gnostore,
Alloc: gnostore.GetAllocator(),
Context: msgCtx,
Expand Down Expand Up @@ -477,7 +479,7 @@ func (vm *VMKeeper) Call(ctx sdk.Context, msg MsgCall) (res string, err error) {
m := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: "",
Output: os.Stdout, // XXX
Output: vm.Output,
Store: gnostore,
Context: msgCtx,
Alloc: gnostore.GetAllocator(),
Expand Down Expand Up @@ -574,10 +576,14 @@ func (vm *VMKeeper) Run(ctx sdk.Context, msg MsgRun) (res string, err error) {
}
// Parse and run the files, construct *PV.
buf := new(bytes.Buffer)
output := io.Writer(buf)
if vm.Output != nil {
output = io.MultiWriter(buf, vm.Output)
}
m := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: "",
Output: buf,
Output: output,
Store: gnostore,
Alloc: gnostore.GetAllocator(),
Context: msgCtx,
Expand All @@ -603,7 +609,7 @@ func (vm *VMKeeper) Run(ctx sdk.Context, msg MsgRun) (res string, err error) {
m2 := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: "",
Output: buf,
Output: output,
Store: gnostore,
Alloc: gnostore.GetAllocator(),
Context: msgCtx,
Expand Down Expand Up @@ -735,7 +741,7 @@ func (vm *VMKeeper) QueryEval(ctx sdk.Context, pkgPath string, expr string) (res
m := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: pkgPath,
Output: os.Stdout, // XXX
Output: vm.Output,
Store: gnostore,
Context: msgCtx,
Alloc: alloc,
Expand Down Expand Up @@ -802,7 +808,7 @@ func (vm *VMKeeper) QueryEvalString(ctx sdk.Context, pkgPath string, expr string
m := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: pkgPath,
Output: os.Stdout, // XXX
Output: vm.Output,
Store: gnostore,
Context: msgCtx,
Alloc: alloc,
Expand Down
3 changes: 1 addition & 2 deletions gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"os"
"reflect"
"slices"
"strconv"
Expand Down Expand Up @@ -144,7 +143,7 @@ func NewMachineWithOptions(opts MachineOptions) *Machine {

output := opts.Output
if output == nil {
output = os.Stdout
output = io.Discard
}
alloc := opts.Alloc
if alloc == nil {
Expand Down

0 comments on commit 9129e4e

Please sign in to comment.