From 54d976c02f8b913606a46a2e32ba871b227d8983 Mon Sep 17 00:00:00 2001 From: Kirk Haines Date: Fri, 27 Sep 2024 01:50:05 -0600 Subject: [PATCH] Removed the code duplication from test, and just directly use test.TestContext. --- gnovm/cmd/gno/run.go | 132 ++++--------------------------------------- 1 file changed, 11 insertions(+), 121 deletions(-) diff --git a/gnovm/cmd/gno/run.go b/gnovm/cmd/gno/run.go index df822b99d63..f174c2b4cc5 100644 --- a/gnovm/cmd/gno/run.go +++ b/gnovm/cmd/gno/run.go @@ -10,15 +10,10 @@ import ( "path/filepath" "strings" - "github.com/gnolang/gno/gno.land/pkg/gnoland/ugnot" "github.com/gnolang/gno/gnovm/pkg/gnoenv" gno "github.com/gnolang/gno/gnovm/pkg/gnolang" - "github.com/gnolang/gno/gnovm/stdlibs" "github.com/gnolang/gno/gnovm/tests" - teststd "github.com/gnolang/gno/gnovm/tests/stdlibs/std" "github.com/gnolang/gno/tm2/pkg/commands" - "github.com/gnolang/gno/tm2/pkg/crypto" - "github.com/gnolang/gno/tm2/pkg/sdk" "github.com/gnolang/gno/tm2/pkg/std" ) @@ -118,13 +113,17 @@ func execRun(cfg *runCfg, args []string, io commands.IO) error { return errors.New("no files to run") } - m := runMachine( - testStore, - stdin, - stdout, - string(files[0].PkgName), - cfg.debug || cfg.debugAddr != "", - ) + var send std.Coins + pkgPath := string(files[0].PkgName) + ctx := tests.TestContext(pkgPath, send) + m := gno.NewMachineWithOptions(gno.MachineOptions{ + PkgPath: pkgPath, + Output: stdout, + Input: stdin, + Store: testStore, + Context: ctx, + Debug: cfg.debug || cfg.debugAddr != "", + }) defer m.Release() @@ -142,115 +141,6 @@ func execRun(cfg *runCfg, args []string, io commands.IO) error { return nil } -func runMachine(store gno.Store, stdin io.Reader, stdout io.Writer, pkgPath string, debug bool) *gno.Machine { - var ( - send std.Coins - maxAlloc int64 - ) - - return runMachineCustom(store, pkgPath, stdin, stdout, maxAlloc, send, debug) -} - -func runMachineCustom(store gno.Store, pkgPath string, stdin io.Reader, stdout io.Writer, maxAlloc int64, send std.Coins, debug bool) *gno.Machine { - ctx := runContext(pkgPath, send) - m := gno.NewMachineWithOptions(gno.MachineOptions{ - PkgPath: pkgPath, - Output: stdout, - Input: stdin, - Store: store, - Context: ctx, - Debug: debug, - }) - return m -} - -// runContext() creates the context for gno run. It has been intentially setup to mirror the context that is -// created for gno test, so that behavior remains consistent between running code and testing code. -func runContext(pkgPath string, send std.Coins) *teststd.TestExecContext { - pkgAddr := gno.DerivePkgAddr(pkgPath) - caller := gno.DerivePkgAddr("user1.gno") - - pkgCoins := std.MustParseCoins(ugnot.ValueString(200000000)).Add(send) // >= send. - banker := newTestBanker(pkgAddr.Bech32(), pkgCoins) - ctx := stdlibs.ExecContext{ - ChainID: "run", - Height: 123, - Timestamp: 1234567890, - Msg: nil, - OrigCaller: caller.Bech32(), - OrigPkgAddr: pkgAddr.Bech32(), - OrigSend: send, - OrigSendSpent: new(std.Coins), - Banker: banker, - EventLogger: sdk.NewEventLogger(), - } - return &teststd.TestExecContext{ - ExecContext: ctx, - RealmFrames: make(map[*gno.Frame]teststd.RealmOverride), - } -} - -type testBanker struct { - coinTable map[crypto.Bech32Address]std.Coins -} - -func newTestBanker(args ...interface{}) *testBanker { - coinTable := make(map[crypto.Bech32Address]std.Coins) - if len(args)%2 != 0 { - panic("newTestBanker requires even number of arguments; addr followed by coins") - } - for i := 0; i < len(args); i += 2 { - addr := args[i].(crypto.Bech32Address) - amount := args[i+1].(std.Coins) - coinTable[addr] = amount - } - return &testBanker{ - coinTable: coinTable, - } -} - -func (tb *testBanker) GetCoins(addr crypto.Bech32Address) (dst std.Coins) { - return tb.coinTable[addr] -} - -func (tb *testBanker) SendCoins(from, to crypto.Bech32Address, amt std.Coins) { - fcoins, fexists := tb.coinTable[from] - if !fexists { - panic(fmt.Sprintf( - "source address %s does not exist", - from.String())) - } - if !fcoins.IsAllGTE(amt) { - panic(fmt.Sprintf( - "source address %s has %s; cannot send %s", - from.String(), fcoins, amt)) - } - // First, subtract from 'from'. - frest := fcoins.Sub(amt) - tb.coinTable[from] = frest - // Second, add to 'to'. - // NOTE: even works when from==to, due to 2-step isolation. - tcoins, _ := tb.coinTable[to] - tsum := tcoins.Add(amt) - tb.coinTable[to] = tsum -} - -func (tb *testBanker) TotalCoin(denom string) int64 { - panic("not yet implemented") -} - -func (tb *testBanker) IssueCoin(addr crypto.Bech32Address, denom string, amt int64) { - coins, _ := tb.coinTable[addr] - sum := coins.Add(std.Coins{{denom, amt}}) - tb.coinTable[addr] = sum -} - -func (tb *testBanker) RemoveCoin(addr crypto.Bech32Address, denom string, amt int64) { - coins, _ := tb.coinTable[addr] - rest := coins.Sub(std.Coins{{denom, amt}}) - tb.coinTable[addr] = rest -} - func parseFiles(fnames []string, stderr io.WriteCloser) ([]*gno.FileNode, error) { files := make([]*gno.FileNode, 0, len(fnames)) var hasError bool