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/encoding/address"
"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/util"
"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))
return
}
if err := v.LoadFile(c.Args[0]); err != nil {
if err := v.LoadFileWithFlags(c.Args[0], callflag.All); err != nil {
c.Err(err)
return
}
@ -319,7 +320,7 @@ func handleLoadBase64(c *ishell.Context) {
c.Err(fmt.Errorf("%w: %v", ErrInvalidParameter, err))
return
}
v.Load(b)
v.LoadWithFlags(b, callflag.All)
c.Printf("READY: loaded %d instructions\n", v.Context().LenInstr())
changePrompt(c, v)
}
@ -335,7 +336,7 @@ func handleLoadHex(c *ishell.Context) {
c.Err(fmt.Errorf("%w: %v", ErrInvalidParameter, err))
return
}
v.Load(b)
v.LoadWithFlags(b, callflag.All)
c.Printf("READY: loaded %d instructions\n", v.Context().LenInstr())
changePrompt(c, v)
}
@ -360,7 +361,7 @@ func handleLoadGo(c *ishell.Context) {
}
setManifestInContext(c, m)
v.Load(b)
v.LoadWithFlags(b, callflag.All)
c.Printf("READY: loaded %d instructions\n", v.Context().LenInstr())
changePrompt(c, v)
}

View file

@ -186,6 +186,24 @@ func TestLoad(t *testing.T) {
e.checkNextLine(t, "READY: loaded \\d* instructions")
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) {
config.Version = "0.92.0-test"

View file

@ -247,28 +247,33 @@ func (v *VM) AddBreakPointRel(n int) {
v.AddBreakPoint(ctx.nextip + n)
}
// LoadFile loads a program in NEF format from the given path, ready to execute it.
func (v *VM) LoadFile(path string) error {
// LoadFileWithFlags loads a program in NEF format from the given path, ready to execute it.
func (v *VM) LoadFileWithFlags(path string, f callflag.CallFlag) error {
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
f, err := nef.FileFromBytes(b)
nef, err := nef.FileFromBytes(b)
if err != nil {
return err
}
v.Load(f.Script)
v.Load(nef.Script)
return nil
}
// Load initializes the VM with the program given.
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.
v.istack.Clear()
v.estack.Clear()
v.state = NoneState
v.gasConsumed = 0
v.LoadScript(prog)
v.LoadScriptWithFlags(prog, f)
}
// LoadScript loads a script from the internal script table. It