vm, cli: allow to specify flags while loading VM

This commit is contained in:
Anna Shaleva 2021-05-28 12:07:03 +03:00
parent 93a331818e
commit e3611bfa4c
3 changed files with 33 additions and 9 deletions

View file

@ -18,6 +18,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/compiler" "github.com/nspcc-dev/neo-go/pkg/compiler"
"github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint" "github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm" "github.com/nspcc-dev/neo-go/pkg/vm"
@ -294,7 +295,7 @@ func handleLoadNEF(c *ishell.Context) {
c.Err(fmt.Errorf("%w: <file> <manifest>", ErrMissingParameter)) c.Err(fmt.Errorf("%w: <file> <manifest>", ErrMissingParameter))
return return
} }
if err := v.LoadFile(c.Args[0]); err != nil { if err := v.LoadFileWithFlags(c.Args[0], callflag.All); err != nil {
c.Err(err) c.Err(err)
return return
} }
@ -319,7 +320,7 @@ func handleLoadBase64(c *ishell.Context) {
c.Err(fmt.Errorf("%w: %v", ErrInvalidParameter, err)) c.Err(fmt.Errorf("%w: %v", ErrInvalidParameter, err))
return return
} }
v.Load(b) v.LoadWithFlags(b, callflag.All)
c.Printf("READY: loaded %d instructions\n", v.Context().LenInstr()) c.Printf("READY: loaded %d instructions\n", v.Context().LenInstr())
changePrompt(c, v) changePrompt(c, v)
} }
@ -335,7 +336,7 @@ func handleLoadHex(c *ishell.Context) {
c.Err(fmt.Errorf("%w: %v", ErrInvalidParameter, err)) c.Err(fmt.Errorf("%w: %v", ErrInvalidParameter, err))
return return
} }
v.Load(b) v.LoadWithFlags(b, callflag.All)
c.Printf("READY: loaded %d instructions\n", v.Context().LenInstr()) c.Printf("READY: loaded %d instructions\n", v.Context().LenInstr())
changePrompt(c, v) changePrompt(c, v)
} }
@ -360,7 +361,7 @@ func handleLoadGo(c *ishell.Context) {
} }
setManifestInContext(c, m) setManifestInContext(c, m)
v.Load(b) v.LoadWithFlags(b, callflag.All)
c.Printf("READY: loaded %d instructions\n", v.Context().LenInstr()) c.Printf("READY: loaded %d instructions\n", v.Context().LenInstr())
changePrompt(c, v) changePrompt(c, v)
} }

View file

@ -186,6 +186,24 @@ func TestLoad(t *testing.T) {
e.checkNextLine(t, "READY: loaded \\d* instructions") e.checkNextLine(t, "READY: loaded \\d* instructions")
e.checkStack(t, 8) e.checkStack(t, 8)
}) })
t.Run("loadgo, check calling flags", func(t *testing.T) {
srcAllowNotify := `package kek
import "github.com/nspcc-dev/neo-go/pkg/interop/runtime"
func Main() int {
runtime.Log("Hello, world!")
return 1
}
`
filename := path.Join(tmpDir, "vmtestcontract.go")
require.NoError(t, ioutil.WriteFile(filename, []byte(srcAllowNotify), os.ModePerm))
e := newTestVMCLI(t)
e.runProg(t,
"loadgo "+filename,
"run main")
e.checkNextLine(t, "READY: loaded \\d* instructions")
e.checkStack(t, 1)
})
t.Run("loadnef", func(t *testing.T) { t.Run("loadnef", func(t *testing.T) {
config.Version = "0.92.0-test" config.Version = "0.92.0-test"

View file

@ -247,28 +247,33 @@ func (v *VM) AddBreakPointRel(n int) {
v.AddBreakPoint(ctx.nextip + n) v.AddBreakPoint(ctx.nextip + n)
} }
// LoadFile loads a program in NEF format from the given path, ready to execute it. // LoadFileWithFlags loads a program in NEF format from the given path, ready to execute it.
func (v *VM) LoadFile(path string) error { func (v *VM) LoadFileWithFlags(path string, f callflag.CallFlag) error {
b, err := ioutil.ReadFile(path) b, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
return err return err
} }
f, err := nef.FileFromBytes(b) nef, err := nef.FileFromBytes(b)
if err != nil { if err != nil {
return err return err
} }
v.Load(f.Script) v.Load(nef.Script)
return nil return nil
} }
// Load initializes the VM with the program given. // Load initializes the VM with the program given.
func (v *VM) Load(prog []byte) { func (v *VM) Load(prog []byte) {
v.LoadWithFlags(prog, callflag.NoneFlag)
}
// LoadWithFlags initializes the VM with the program and flags given.
func (v *VM) LoadWithFlags(prog []byte, f callflag.CallFlag) {
// Clear all stacks and state, it could be a reload. // Clear all stacks and state, it could be a reload.
v.istack.Clear() v.istack.Clear()
v.estack.Clear() v.estack.Clear()
v.state = NoneState v.state = NoneState
v.gasConsumed = 0 v.gasConsumed = 0
v.LoadScript(prog) v.LoadScriptWithFlags(prog, f)
} }
// LoadScript loads a script from the internal script table. It // LoadScript loads a script from the internal script table. It