core: use ic.SpawnVM() in tests, not vm.New()

vm.New() sets some arbitrary trigger while interop context always sets
something appropriate according to its state.
This commit is contained in:
Roman Khimov 2020-10-29 19:10:28 +03:00
parent d4da811d12
commit 2924ecd453
4 changed files with 20 additions and 25 deletions

View file

@ -147,10 +147,9 @@ func TestECDSAVerify(t *testing.T) {
ic := chain.newInteropContext(trigger.Application, dao.NewSimple(storage.NewMemoryStore(), netmode.UnitTestNet), nil, nil) ic := chain.newInteropContext(trigger.Application, dao.NewSimple(storage.NewMemoryStore(), netmode.UnitTestNet), nil, nil)
runCase := func(t *testing.T, isErr bool, result interface{}, args ...interface{}) { runCase := func(t *testing.T, isErr bool, result interface{}, args ...interface{}) {
v := vm.New() ic.SpawnVM()
ic.VM = v
for i := range args { for i := range args {
v.Estack().PushVal(args[i]) ic.VM.Estack().PushVal(args[i])
} }
var err error var err error
@ -168,8 +167,8 @@ func TestECDSAVerify(t *testing.T) {
return return
} }
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, v.Estack().Len()) require.Equal(t, 1, ic.VM.Estack().Len())
require.Equal(t, result, v.Estack().Pop().Value().(bool)) require.Equal(t, result, ic.VM.Estack().Pop().Value().(bool))
} }
msg := []byte("test message") msg := []byte("test message")

View file

@ -506,23 +506,21 @@ func getTestContractState() (*state.Contract, *state.Contract) {
} }
func loadScript(ic *interop.Context, script []byte, args ...interface{}) { func loadScript(ic *interop.Context, script []byte, args ...interface{}) {
v := vm.New() ic.SpawnVM()
v.LoadScriptWithFlags(script, smartcontract.AllowCall) ic.VM.LoadScriptWithFlags(script, smartcontract.AllowCall)
for i := range args { for i := range args {
v.Estack().PushVal(args[i]) ic.VM.Estack().PushVal(args[i])
} }
v.GasLimit = -1 ic.VM.GasLimit = -1
ic.VM = v
} }
func loadScriptWithHashAndFlags(ic *interop.Context, script []byte, hash util.Uint160, f smartcontract.CallFlag, args ...interface{}) { func loadScriptWithHashAndFlags(ic *interop.Context, script []byte, hash util.Uint160, f smartcontract.CallFlag, args ...interface{}) {
v := vm.New() ic.SpawnVM()
v.LoadScriptWithHash(script, hash, f) ic.VM.LoadScriptWithHash(script, hash, f)
for i := range args { for i := range args {
v.Estack().PushVal(args[i]) ic.VM.Estack().PushVal(args[i])
} }
v.GasLimit = -1 ic.VM.GasLimit = -1
ic.VM = v
} }
func TestContractCall(t *testing.T) { func TestContractCall(t *testing.T) {
@ -1029,15 +1027,14 @@ func TestMethodCallback(t *testing.T) {
require.Error(t, callback.Invoke(ic)) require.Error(t, callback.Invoke(ic))
}) })
t.Run("CallIsNotAllowed", func(t *testing.T) { t.Run("CallIsNotAllowed", func(t *testing.T) {
v := vm.New() ic.SpawnVM()
ic.VM = v ic.VM.Load(currCs.Script)
v.Load(currCs.Script) ic.VM.Estack().PushVal("add")
v.Estack().PushVal("add") ic.VM.Estack().PushVal(rawHash)
v.Estack().PushVal(rawHash)
require.NoError(t, callback.CreateFromMethod(ic)) require.NoError(t, callback.CreateFromMethod(ic))
args := stackitem.NewArray([]stackitem.Item{stackitem.Make(1), stackitem.Make(5)}) args := stackitem.NewArray([]stackitem.Item{stackitem.Make(1), stackitem.Make(5)})
v.Estack().InsertAt(vm.NewElement(args), 1) ic.VM.Estack().InsertAt(vm.NewElement(args), 1)
require.Error(t, callback.Invoke(ic)) require.Error(t, callback.Invoke(ic))
}) })
}) })

View file

@ -79,7 +79,7 @@ func TestDesignate_DesignateAsRole(t *testing.T) {
des := bc.contracts.Designate des := bc.contracts.Designate
tx := transaction.New(netmode.UnitTestNet, []byte{}, 0) tx := transaction.New(netmode.UnitTestNet, []byte{}, 0)
ic := bc.newInteropContext(trigger.System, bc.dao, nil, tx) ic := bc.newInteropContext(trigger.System, bc.dao, nil, tx)
ic.VM = vm.New() ic.SpawnVM()
ic.VM.LoadScript([]byte{byte(opcode.RET)}) ic.VM.LoadScript([]byte{byte(opcode.RET)})
pubs, err := des.GetDesignatedByRole(bc.dao, 0xFF) pubs, err := des.GetDesignatedByRole(bc.dao, 0xFF)

View file

@ -13,7 +13,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger" "github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
"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/opcode" "github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -32,7 +31,7 @@ func TestNEO_Vote(t *testing.T) {
neo := bc.contracts.NEO neo := bc.contracts.NEO
tx := transaction.New(netmode.UnitTestNet, []byte{byte(opcode.PUSH1)}, 0) tx := transaction.New(netmode.UnitTestNet, []byte{byte(opcode.PUSH1)}, 0)
ic := bc.newInteropContext(trigger.System, bc.dao, nil, tx) ic := bc.newInteropContext(trigger.System, bc.dao, nil, tx)
ic.VM = vm.New() ic.SpawnVM()
ic.Block = bc.newBlock(tx) ic.Block = bc.newBlock(tx)
freq := testchain.ValidatorsCount + testchain.CommitteeSize() freq := testchain.ValidatorsCount + testchain.CommitteeSize()
@ -126,7 +125,7 @@ func TestNEO_SetGasPerBlock(t *testing.T) {
neo := bc.contracts.NEO neo := bc.contracts.NEO
tx := transaction.New(netmode.UnitTestNet, []byte{}, 0) tx := transaction.New(netmode.UnitTestNet, []byte{}, 0)
ic := bc.newInteropContext(trigger.System, bc.dao, nil, tx) ic := bc.newInteropContext(trigger.System, bc.dao, nil, tx)
ic.VM = vm.New() ic.SpawnVM()
ic.VM.LoadScript([]byte{byte(opcode.RET)}) ic.VM.LoadScript([]byte{byte(opcode.RET)})
h := neo.GetCommitteeAddress() h := neo.GetCommitteeAddress()