From d41e0e1a8a21005057c55d1da0007520a01f8e5d Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Wed, 6 Nov 2024 16:24:11 +0100 Subject: [PATCH 1/2] fix(gnovm): don't print to stdout by default --- gno.land/pkg/sdk/vm/keeper.go | 10 +--------- gnovm/pkg/gnolang/machine.go | 3 +-- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/gno.land/pkg/sdk/vm/keeper.go b/gno.land/pkg/sdk/vm/keeper.go index 7f5216a4f03..c74003274ca 100644 --- a/gno.land/pkg/sdk/vm/keeper.go +++ b/gno.land/pkg/sdk/vm/keeper.go @@ -7,7 +7,6 @@ import ( "context" "fmt" "log/slog" - "os" "path/filepath" "regexp" "strings" @@ -108,7 +107,6 @@ func (vm *VMKeeper) Initialize( m2 := gno.NewMachineWithOptions( gno.MachineOptions{ PkgPath: "", - Output: os.Stdout, // XXX Store: vm.gnoStore, }) defer m2.Release() @@ -191,8 +189,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) @@ -275,7 +272,6 @@ func (vm *VMKeeper) checkNamespacePermission(ctx sdk.Context, creator crypto.Add m := gno.NewMachineWithOptions( gno.MachineOptions{ PkgPath: "", - Output: os.Stdout, // XXX Store: store, Context: msgCtx, Alloc: store.GetAllocator(), @@ -376,7 +372,6 @@ func (vm *VMKeeper) AddPackage(ctx sdk.Context, msg MsgAddPackage) (err error) { m2 := gno.NewMachineWithOptions( gno.MachineOptions{ PkgPath: "", - Output: os.Stdout, // XXX Store: gnostore, Alloc: gnostore.GetAllocator(), Context: msgCtx, @@ -477,7 +472,6 @@ func (vm *VMKeeper) Call(ctx sdk.Context, msg MsgCall) (res string, err error) { m := gno.NewMachineWithOptions( gno.MachineOptions{ PkgPath: "", - Output: os.Stdout, // XXX Store: gnostore, Context: msgCtx, Alloc: gnostore.GetAllocator(), @@ -735,7 +729,6 @@ func (vm *VMKeeper) QueryEval(ctx sdk.Context, pkgPath string, expr string) (res m := gno.NewMachineWithOptions( gno.MachineOptions{ PkgPath: pkgPath, - Output: os.Stdout, // XXX Store: gnostore, Context: msgCtx, Alloc: alloc, @@ -802,7 +795,6 @@ func (vm *VMKeeper) QueryEvalString(ctx sdk.Context, pkgPath string, expr string m := gno.NewMachineWithOptions( gno.MachineOptions{ PkgPath: pkgPath, - Output: os.Stdout, // XXX Store: gnostore, Context: msgCtx, Alloc: alloc, diff --git a/gnovm/pkg/gnolang/machine.go b/gnovm/pkg/gnolang/machine.go index 09be71682a5..33bf32730c5 100644 --- a/gnovm/pkg/gnolang/machine.go +++ b/gnovm/pkg/gnolang/machine.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "io" - "os" "reflect" "slices" "strconv" @@ -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 { From ab12b41fb591550ca42af1cce2c2fc9e6d5dceb0 Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Wed, 6 Nov 2024 20:38:56 +0100 Subject: [PATCH 2/2] show output in gnodev --- contribs/gnodev/pkg/dev/node.go | 2 ++ gno.land/pkg/gnoland/app.go | 5 ++++- gno.land/pkg/gnoland/node_inmemory.go | 3 +++ gno.land/pkg/sdk/vm/keeper.go | 18 ++++++++++++++++-- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/contribs/gnodev/pkg/dev/node.go b/contribs/gnodev/pkg/dev/node.go index 54baa2ea774..9b3f838b8a0 100644 --- a/contribs/gnodev/pkg/dev/node.go +++ b/contribs/gnodev/pkg/dev/node.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "log/slog" + "os" "path/filepath" "strings" "sync" @@ -576,5 +577,6 @@ func newNodeConfig(tmc *tmcfg.Config, chainid string, appstate gnoland.GnoGenesi PrivValidator: pv, TMConfig: tmc, Genesis: genesis, + VMOutput: os.Stdout, } } diff --git a/gno.land/pkg/gnoland/app.go b/gno.land/pkg/gnoland/app.go index ff1b5a88fea..d29ae3fd181 100644 --- a/gno.land/pkg/gnoland/app.go +++ b/gno.land/pkg/gnoland/app.go @@ -3,6 +3,7 @@ package gnoland import ( "fmt" + "io" "log/slog" "path/filepath" "strconv" @@ -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{ @@ -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 diff --git a/gno.land/pkg/gnoland/node_inmemory.go b/gno.land/pkg/gnoland/node_inmemory.go index 9dccbbfac8d..426a8c780c7 100644 --- a/gno.land/pkg/gnoland/node_inmemory.go +++ b/gno.land/pkg/gnoland/node_inmemory.go @@ -2,6 +2,7 @@ package gnoland import ( "fmt" + "io" "log/slog" "path/filepath" "time" @@ -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 @@ -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) diff --git a/gno.land/pkg/sdk/vm/keeper.go b/gno.land/pkg/sdk/vm/keeper.go index c74003274ca..5921e3eb3bb 100644 --- a/gno.land/pkg/sdk/vm/keeper.go +++ b/gno.land/pkg/sdk/vm/keeper.go @@ -6,6 +6,7 @@ import ( "bytes" "context" "fmt" + "io" "log/slog" "path/filepath" "regexp" @@ -56,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 @@ -107,6 +111,7 @@ func (vm *VMKeeper) Initialize( m2 := gno.NewMachineWithOptions( gno.MachineOptions{ PkgPath: "", + Output: vm.Output, Store: vm.gnoStore, }) defer m2.Release() @@ -272,6 +277,7 @@ func (vm *VMKeeper) checkNamespacePermission(ctx sdk.Context, creator crypto.Add m := gno.NewMachineWithOptions( gno.MachineOptions{ PkgPath: "", + Output: vm.Output, Store: store, Context: msgCtx, Alloc: store.GetAllocator(), @@ -372,6 +378,7 @@ func (vm *VMKeeper) AddPackage(ctx sdk.Context, msg MsgAddPackage) (err error) { m2 := gno.NewMachineWithOptions( gno.MachineOptions{ PkgPath: "", + Output: vm.Output, Store: gnostore, Alloc: gnostore.GetAllocator(), Context: msgCtx, @@ -472,6 +479,7 @@ func (vm *VMKeeper) Call(ctx sdk.Context, msg MsgCall) (res string, err error) { m := gno.NewMachineWithOptions( gno.MachineOptions{ PkgPath: "", + Output: vm.Output, Store: gnostore, Context: msgCtx, Alloc: gnostore.GetAllocator(), @@ -568,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, @@ -597,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, @@ -729,6 +741,7 @@ func (vm *VMKeeper) QueryEval(ctx sdk.Context, pkgPath string, expr string) (res m := gno.NewMachineWithOptions( gno.MachineOptions{ PkgPath: pkgPath, + Output: vm.Output, Store: gnostore, Context: msgCtx, Alloc: alloc, @@ -795,6 +808,7 @@ func (vm *VMKeeper) QueryEvalString(ctx sdk.Context, pkgPath string, expr string m := gno.NewMachineWithOptions( gno.MachineOptions{ PkgPath: pkgPath, + Output: vm.Output, Store: gnostore, Context: msgCtx, Alloc: alloc,