vm/core: add ID support for SYSCALL, redo interop registration
This solves two problems: * adds support for shortened SYSCALL form that uses IDs (similar to #434, but for NEO 2.0, supporting both forms), which is important for compatibility with C# node and mainnet chain that uses it from some height * reworks interop plugging to use callbacks rather than appending to the map, these map mangling functions are clearly visible in the VM profiling statistics and we want spawning a VM to be fast, so it makes sense optimizing it. This change moves most of the work to the init() phase making VM setup cheaper. Caveats: * InteropNameToID accepts `[]byte` because that's the thing we have in SYSCALL processing and that's the most often usecase for it, it leads to some conversions in other places but that's acceptable because those are either tests or init() * three getInterop functions are: `getDefaultVMInterop`, `getSystemInterop` and `getNeoInterop` Our 100K (1.4M->1.5M) block import time improves by ~4% with this change.
This commit is contained in:
parent
1a26548be8
commit
a7457d08a1
7 changed files with 384 additions and 247 deletions
|
@ -16,12 +16,19 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func fooInteropGetter(id uint32) *InteropFuncPrice {
|
||||
if id == InteropNameToID([]byte("foo")) {
|
||||
return &InteropFuncPrice{func(evm *VM) error {
|
||||
evm.Estack().PushVal(1)
|
||||
return nil
|
||||
}, 1}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestInteropHook(t *testing.T) {
|
||||
v := New()
|
||||
v.RegisterInteropFunc("foo", func(evm *VM) error {
|
||||
evm.Estack().PushVal(1)
|
||||
return nil
|
||||
}, 1)
|
||||
v.RegisterInteropGetter(fooInteropGetter)
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
EmitSyscall(buf, "foo")
|
||||
|
@ -32,13 +39,27 @@ func TestInteropHook(t *testing.T) {
|
|||
assert.Equal(t, big.NewInt(1), v.estack.Pop().value.Value())
|
||||
}
|
||||
|
||||
func TestRegisterInterop(t *testing.T) {
|
||||
func TestInteropHookViaID(t *testing.T) {
|
||||
v := New()
|
||||
currRegistered := len(v.interop)
|
||||
v.RegisterInteropFunc("foo", func(evm *VM) error { return nil }, 1)
|
||||
assert.Equal(t, currRegistered+1, len(v.interop))
|
||||
_, ok := v.interop["foo"]
|
||||
assert.Equal(t, true, ok)
|
||||
v.RegisterInteropGetter(fooInteropGetter)
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
fooid := InteropNameToID([]byte("foo"))
|
||||
var id = make([]byte, 4)
|
||||
binary.LittleEndian.PutUint32(id, fooid)
|
||||
_ = EmitSyscall(buf, string(id))
|
||||
_ = EmitOpcode(buf, opcode.RET)
|
||||
v.Load(buf.Bytes())
|
||||
runVM(t, v)
|
||||
assert.Equal(t, 1, v.estack.Len())
|
||||
assert.Equal(t, big.NewInt(1), v.estack.Pop().value.Value())
|
||||
}
|
||||
|
||||
func TestRegisterInteropGetter(t *testing.T) {
|
||||
v := New()
|
||||
currRegistered := len(v.getInterop)
|
||||
v.RegisterInteropGetter(fooInteropGetter)
|
||||
assert.Equal(t, currRegistered+1, len(v.getInterop))
|
||||
}
|
||||
|
||||
func TestBytesToPublicKey(t *testing.T) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue