core: remove callback interops

Follow neo-project/neo#2168 .
This commit is contained in:
Evgenii Stratonikov 2020-12-30 12:26:50 +03:00 committed by Evgeniy Stratonikov
parent dbe81f9b80
commit 86b0e76bf0
7 changed files with 26 additions and 361 deletions

View file

@ -1,37 +0,0 @@
package callback
import (
"errors"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
// Callback is an interface for arbitrary callbacks.
type Callback interface {
// ArgCount returns expected number of arguments.
ArgCount() int
// LoadContext loads context and arguments on stack.
LoadContext(*vm.VM, []stackitem.Item)
}
// Invoke invokes provided callback.
func Invoke(ic *interop.Context) error {
cb := ic.VM.Estack().Pop().Interop().Value().(Callback)
args := ic.VM.Estack().Pop().Array()
if cb.ArgCount() != len(args) {
return errors.New("invalid argument count")
}
cb.LoadContext(ic.VM, args)
switch t := cb.(type) {
case *MethodCallback:
id := interopnames.ToID([]byte(interopnames.SystemContractCall))
return ic.SyscallHandler(ic.VM, id)
case *SyscallCallback:
return ic.SyscallHandler(ic.VM, t.desc.ID)
default:
return nil
}
}

View file

@ -1,61 +0,0 @@
package callback
import (
"errors"
"fmt"
"strings"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"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"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
// MethodCallback represents callback for contract method.
type MethodCallback struct {
contract *state.Contract
method *manifest.Method
}
var _ Callback = (*MethodCallback)(nil)
// ArgCount implements Callback interface.
func (s *MethodCallback) ArgCount() int {
return len(s.method.Parameters)
}
// LoadContext implements Callback interface.
func (s *MethodCallback) LoadContext(v *vm.VM, args []stackitem.Item) {
v.Estack().PushVal(args)
v.Estack().PushVal(s.method.Name)
v.Estack().PushVal(s.contract.Hash.BytesBE())
}
// CreateFromMethod creates callback for a contract method.
func CreateFromMethod(ic *interop.Context) error {
rawHash := ic.VM.Estack().Pop().Bytes()
h, err := util.Uint160DecodeBytesBE(rawHash)
if err != nil {
return err
}
cs, err := ic.GetContract(h)
if err != nil {
return fmt.Errorf("contract not found: %w", err)
}
method := string(ic.VM.Estack().Pop().Bytes())
if strings.HasPrefix(method, "_") {
return errors.New("invalid method name")
}
currCs, err := ic.GetContract(ic.VM.GetCurrentScriptHash())
if err == nil && !currCs.Manifest.CanCall(h, &cs.Manifest, method) {
return errors.New("method call is not allowed")
}
md := cs.Manifest.ABI.GetMethod(method)
ic.VM.Estack().PushVal(stackitem.NewInterop(&MethodCallback{
contract: cs,
method: md,
}))
return nil
}

View file

@ -1,42 +0,0 @@
package callback
import (
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
// PointerCallback represents callback for a pointer.
type PointerCallback struct {
paramCount int
offset int
context *vm.Context
}
var _ Callback = (*PointerCallback)(nil)
// ArgCount implements Callback interface.
func (p *PointerCallback) ArgCount() int {
return p.paramCount
}
// LoadContext implements Callback interface.
func (p *PointerCallback) LoadContext(v *vm.VM, args []stackitem.Item) {
v.Call(p.context, p.offset)
for i := len(args) - 1; i >= 0; i-- {
v.Estack().PushVal(args[i])
}
}
// Create creates callback using pointer and parameters count.
func Create(ic *interop.Context) error {
ctx := ic.VM.Estack().Pop().Item().(*vm.Context)
offset := ic.VM.Estack().Pop().Item().(*stackitem.Pointer).Position()
count := ic.VM.Estack().Pop().BigInt().Int64()
ic.VM.Estack().PushVal(stackitem.NewInterop(&PointerCallback{
paramCount: int(count),
offset: offset,
context: ctx,
}))
return nil
}

View file

@ -1,42 +0,0 @@
package callback
import (
"errors"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
// SyscallCallback represents callback for a syscall.
type SyscallCallback struct {
desc *interop.Function
}
var _ Callback = (*SyscallCallback)(nil)
// ArgCount implements Callback interface.
func (p *SyscallCallback) ArgCount() int {
return p.desc.ParamCount
}
// LoadContext implements Callback interface.
func (p *SyscallCallback) LoadContext(v *vm.VM, args []stackitem.Item) {
for i := len(args) - 1; i >= 0; i-- {
v.Estack().PushVal(args[i])
}
}
// CreateFromSyscall creates callback from syscall.
func CreateFromSyscall(ic *interop.Context) error {
id := uint32(ic.VM.Estack().Pop().BigInt().Int64())
f := ic.GetFunction(id)
if f == nil {
return errors.New("syscall not found")
}
if f.DisallowCallback {
return errors.New("syscall is not allowed to be used in a callback")
}
ic.VM.Estack().PushVal(stackitem.NewInterop(&SyscallCallback{f}))
return nil
}

View file

@ -70,8 +70,6 @@ type Function struct {
ID uint32
Name string
Func func(*Context) error
// DisallowCallback is true iff syscall can't be used in a callback.
DisallowCallback bool
// ParamCount is a number of function parameters.
ParamCount int
Price int64

View file

@ -9,7 +9,6 @@ import (
"github.com/nspcc-dev/neo-go/internal/random"
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/interop/callback"
"github.com/nspcc-dev/neo-go/pkg/core/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
"github.com/nspcc-dev/neo-go/pkg/core/interop/runtime"
@ -24,7 +23,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
@ -788,150 +786,6 @@ func TestContractGetCallFlags(t *testing.T) {
require.Equal(t, int64(callflag.All), v.Estack().Pop().Value().(*big.Int).Int64())
}
func TestPointerCallback(t *testing.T) {
_, ic, bc := createVM(t)
defer bc.Close()
script := []byte{
byte(opcode.NOP), byte(opcode.INC), byte(opcode.RET),
byte(opcode.DIV), byte(opcode.RET),
}
t.Run("Good", func(t *testing.T) {
loadScript(ic, script, 2, stackitem.NewPointer(3, script))
ic.VM.Estack().PushVal(ic.VM.Context())
require.NoError(t, callback.Create(ic))
args := stackitem.NewArray([]stackitem.Item{stackitem.Make(3), stackitem.Make(12)})
ic.VM.Estack().InsertAt(vm.NewElement(args), 1)
require.NoError(t, callback.Invoke(ic))
require.NoError(t, ic.VM.Run())
require.Equal(t, 1, ic.VM.Estack().Len())
require.Equal(t, big.NewInt(5), ic.VM.Estack().Pop().Item().Value())
})
t.Run("Invalid", func(t *testing.T) {
t.Run("NotEnoughParameters", func(t *testing.T) {
loadScript(ic, script, 2, stackitem.NewPointer(3, script))
ic.VM.Estack().PushVal(ic.VM.Context())
require.NoError(t, callback.Create(ic))
args := stackitem.NewArray([]stackitem.Item{stackitem.Make(3)})
ic.VM.Estack().InsertAt(vm.NewElement(args), 1)
require.Error(t, callback.Invoke(ic))
})
})
}
func TestMethodCallback(t *testing.T) {
_, ic, bc := createVM(t)
defer bc.Close()
cs, currCs := getTestContractState(bc)
require.NoError(t, bc.contracts.Management.PutContractState(ic.DAO, cs))
require.NoError(t, bc.contracts.Management.PutContractState(ic.DAO, currCs))
ic.Functions = append(ic.Functions, systemInterops)
rawHash := cs.Hash.BytesBE()
t.Run("Invalid", func(t *testing.T) {
runInvalid := func(args ...interface{}) func(t *testing.T) {
return func(t *testing.T) {
loadScript(ic, currCs.NEF.Script, 42)
for i := range args {
ic.VM.Estack().PushVal(args[i])
}
require.Error(t, callback.CreateFromMethod(ic))
}
}
t.Run("Hash", runInvalid("add", rawHash[1:]))
t.Run("MissingHash", runInvalid("add", util.Uint160{}.BytesBE()))
t.Run("MissingMethod", runInvalid("sub", rawHash))
t.Run("DisallowedMethod", runInvalid("ret7", rawHash))
t.Run("Initialize", runInvalid("_initialize", rawHash))
t.Run("NotEnoughArguments", func(t *testing.T) {
loadScript(ic, currCs.NEF.Script, 42, "add", rawHash)
require.NoError(t, callback.CreateFromMethod(ic))
ic.VM.Estack().InsertAt(vm.NewElement(stackitem.NewArray([]stackitem.Item{stackitem.Make(1)})), 1)
require.Error(t, callback.Invoke(ic))
})
t.Run("CallIsNotAllowed", func(t *testing.T) {
ic.SpawnVM()
ic.VM.Load(currCs.NEF.Script)
ic.VM.Estack().PushVal("add")
ic.VM.Estack().PushVal(rawHash)
require.NoError(t, callback.CreateFromMethod(ic))
args := stackitem.NewArray([]stackitem.Item{stackitem.Make(1), stackitem.Make(5)})
ic.VM.Estack().InsertAt(vm.NewElement(args), 1)
require.Error(t, callback.Invoke(ic))
})
})
t.Run("Good", func(t *testing.T) {
loadScript(ic, currCs.NEF.Script, 42, "add", rawHash)
require.NoError(t, callback.CreateFromMethod(ic))
args := stackitem.NewArray([]stackitem.Item{stackitem.Make(1), stackitem.Make(5)})
ic.VM.Estack().InsertAt(vm.NewElement(args), 1)
require.NoError(t, callback.Invoke(ic))
require.NoError(t, ic.VM.Run())
require.Equal(t, 2, ic.VM.Estack().Len())
require.Equal(t, big.NewInt(6), ic.VM.Estack().Pop().Item().Value())
require.Equal(t, big.NewInt(42), ic.VM.Estack().Pop().Item().Value())
})
}
func TestSyscallCallback(t *testing.T) {
_, ic, bc := createVM(t)
defer bc.Close()
ic.Functions = append(ic.Functions, []interop.Function{
{
ID: 0x42,
Func: func(ic *interop.Context) error {
a := ic.VM.Estack().Pop().BigInt()
b := ic.VM.Estack().Pop().BigInt()
ic.VM.Estack().PushVal(new(big.Int).Add(a, b))
return nil
},
ParamCount: 2,
},
{
ID: 0x53,
Func: func(_ *interop.Context) error { return nil },
DisallowCallback: true,
},
})
t.Run("Good", func(t *testing.T) {
args := stackitem.NewArray([]stackitem.Item{stackitem.Make(12), stackitem.Make(30)})
loadScript(ic, []byte{byte(opcode.RET)}, args, 0x42)
require.NoError(t, callback.CreateFromSyscall(ic))
require.NoError(t, callback.Invoke(ic))
require.Equal(t, 1, ic.VM.Estack().Len())
require.Equal(t, big.NewInt(42), ic.VM.Estack().Pop().Item().Value())
})
t.Run("Invalid", func(t *testing.T) {
t.Run("InvalidParameterCount", func(t *testing.T) {
args := stackitem.NewArray([]stackitem.Item{stackitem.Make(12)})
loadScript(ic, []byte{byte(opcode.RET)}, args, 0x42)
require.NoError(t, callback.CreateFromSyscall(ic))
require.Error(t, callback.Invoke(ic))
})
t.Run("MissingSyscall", func(t *testing.T) {
loadScript(ic, []byte{byte(opcode.RET)}, stackitem.NewArray(nil), 0x43)
require.Error(t, callback.CreateFromSyscall(ic))
})
t.Run("Disallowed", func(t *testing.T) {
loadScript(ic, []byte{byte(opcode.RET)}, stackitem.NewArray(nil), 0x53)
require.Error(t, callback.CreateFromSyscall(ic))
})
})
}
func TestRuntimeCheckWitness(t *testing.T) {
_, ic, bc := createVM(t)
defer bc.Close()

View file

@ -10,7 +10,6 @@ package core
import (
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/interop/binary"
"github.com/nspcc-dev/neo-go/pkg/core/interop/callback"
"github.com/nspcc-dev/neo-go/pkg/core/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/core/interop/crypto"
"github.com/nspcc-dev/neo-go/pkg/core/interop/enumerator"
@ -51,29 +50,25 @@ var systemInterops = []interop.Function{
RequiredFlags: callflag.ReadStates, ParamCount: 2},
{Name: interopnames.SystemBlockchainGetTransactionHeight, Func: bcGetTransactionHeight, Price: 1 << 15,
RequiredFlags: callflag.ReadStates, ParamCount: 1},
{Name: interopnames.SystemCallbackCreate, Func: callback.Create, Price: 1 << 4, ParamCount: 3, DisallowCallback: true},
{Name: interopnames.SystemCallbackCreateFromMethod, Func: callback.CreateFromMethod, Price: 1 << 15, ParamCount: 2, DisallowCallback: true},
{Name: interopnames.SystemCallbackCreateFromSyscall, Func: callback.CreateFromSyscall, Price: 1 << 4, ParamCount: 1, DisallowCallback: true},
{Name: interopnames.SystemCallbackInvoke, Func: callback.Invoke, Price: 1 << 15, ParamCount: 2, DisallowCallback: true},
{Name: interopnames.SystemContractCall, Func: contract.Call, Price: 1 << 15,
RequiredFlags: callflag.AllowCall, ParamCount: 3, DisallowCallback: true},
RequiredFlags: callflag.AllowCall, ParamCount: 3},
{Name: interopnames.SystemContractCallEx, Func: contract.CallEx, Price: 1 << 15,
RequiredFlags: callflag.AllowCall, ParamCount: 4, DisallowCallback: true},
{Name: interopnames.SystemContractCallNative, Func: native.Call, Price: 0, ParamCount: 1, DisallowCallback: true},
{Name: interopnames.SystemContractCreateStandardAccount, Func: contractCreateStandardAccount, Price: 1 << 8, ParamCount: 1, DisallowCallback: true},
RequiredFlags: callflag.AllowCall, ParamCount: 4},
{Name: interopnames.SystemContractCallNative, Func: native.Call, Price: 0, ParamCount: 1},
{Name: interopnames.SystemContractCreateStandardAccount, Func: contractCreateStandardAccount, Price: 1 << 8, ParamCount: 1},
{Name: interopnames.SystemContractIsStandard, Func: contractIsStandard, Price: 1 << 10, RequiredFlags: callflag.ReadStates, ParamCount: 1},
{Name: interopnames.SystemContractGetCallFlags, Func: contractGetCallFlags, Price: 1 << 10, DisallowCallback: true},
{Name: interopnames.SystemContractNativeOnPersist, Func: native.OnPersist, Price: 0, RequiredFlags: callflag.WriteStates, DisallowCallback: true},
{Name: interopnames.SystemContractNativePostPersist, Func: native.PostPersist, Price: 0, RequiredFlags: callflag.WriteStates, DisallowCallback: true},
{Name: interopnames.SystemEnumeratorConcat, Func: enumerator.Concat, Price: 1 << 4, ParamCount: 2, DisallowCallback: true},
{Name: interopnames.SystemEnumeratorCreate, Func: enumerator.Create, Price: 1 << 4, ParamCount: 1, DisallowCallback: true},
{Name: interopnames.SystemEnumeratorNext, Func: enumerator.Next, Price: 1 << 15, ParamCount: 1, DisallowCallback: true},
{Name: interopnames.SystemEnumeratorValue, Func: enumerator.Value, Price: 1 << 4, ParamCount: 1, DisallowCallback: true},
{Name: interopnames.SystemIteratorConcat, Func: iterator.Concat, Price: 1 << 4, ParamCount: 2, DisallowCallback: true},
{Name: interopnames.SystemIteratorCreate, Func: iterator.Create, Price: 1 << 4, ParamCount: 1, DisallowCallback: true},
{Name: interopnames.SystemIteratorKey, Func: iterator.Key, Price: 1 << 4, ParamCount: 1, DisallowCallback: true},
{Name: interopnames.SystemIteratorKeys, Func: iterator.Keys, Price: 1 << 4, ParamCount: 1, DisallowCallback: true},
{Name: interopnames.SystemIteratorValues, Func: iterator.Values, Price: 1 << 4, ParamCount: 1, DisallowCallback: true},
{Name: interopnames.SystemContractGetCallFlags, Func: contractGetCallFlags, Price: 1 << 10},
{Name: interopnames.SystemContractNativeOnPersist, Func: native.OnPersist, Price: 0, RequiredFlags: callflag.WriteStates},
{Name: interopnames.SystemContractNativePostPersist, Func: native.PostPersist, Price: 0, RequiredFlags: callflag.WriteStates},
{Name: interopnames.SystemEnumeratorConcat, Func: enumerator.Concat, Price: 1 << 4, ParamCount: 2},
{Name: interopnames.SystemEnumeratorCreate, Func: enumerator.Create, Price: 1 << 4, ParamCount: 1},
{Name: interopnames.SystemEnumeratorNext, Func: enumerator.Next, Price: 1 << 15, ParamCount: 1},
{Name: interopnames.SystemEnumeratorValue, Func: enumerator.Value, Price: 1 << 4, ParamCount: 1},
{Name: interopnames.SystemIteratorConcat, Func: iterator.Concat, Price: 1 << 4, ParamCount: 2},
{Name: interopnames.SystemIteratorCreate, Func: iterator.Create, Price: 1 << 4, ParamCount: 1},
{Name: interopnames.SystemIteratorKey, Func: iterator.Key, Price: 1 << 4, ParamCount: 1},
{Name: interopnames.SystemIteratorKeys, Func: iterator.Keys, Price: 1 << 4, ParamCount: 1},
{Name: interopnames.SystemIteratorValues, Func: iterator.Values, Price: 1 << 4, ParamCount: 1},
{Name: interopnames.SystemJSONDeserialize, Func: json.Deserialize, Price: 1 << 14, ParamCount: 1},
{Name: interopnames.SystemJSONSerialize, Func: json.Serialize, Price: 1 << 12, ParamCount: 1},
{Name: interopnames.SystemRuntimeCheckWitness, Func: runtime.CheckWitness, Price: 1 << 10,
@ -88,26 +83,26 @@ var systemInterops = []interop.Function{
{Name: interopnames.SystemRuntimeGetTime, Func: runtime.GetTime, Price: 1 << 3, RequiredFlags: callflag.ReadStates},
{Name: interopnames.SystemRuntimeGetTrigger, Func: runtime.GetTrigger, Price: 1 << 3},
{Name: interopnames.SystemRuntimeLog, Func: runtime.Log, Price: 1 << 15, RequiredFlags: callflag.AllowNotify,
ParamCount: 1, DisallowCallback: true},
ParamCount: 1},
{Name: interopnames.SystemRuntimeNotify, Func: runtime.Notify, Price: 1 << 15, RequiredFlags: callflag.AllowNotify,
ParamCount: 2, DisallowCallback: true},
ParamCount: 2},
{Name: interopnames.SystemRuntimePlatform, Func: runtime.Platform, Price: 1 << 3},
{Name: interopnames.SystemStorageDelete, Func: storageDelete, Price: 0,
RequiredFlags: callflag.WriteStates, ParamCount: 2, DisallowCallback: true},
RequiredFlags: callflag.WriteStates, ParamCount: 2},
{Name: interopnames.SystemStorageFind, Func: storageFind, Price: 1 << 15, RequiredFlags: callflag.ReadStates,
ParamCount: 2, DisallowCallback: true},
ParamCount: 2},
{Name: interopnames.SystemStorageGet, Func: storageGet, Price: 1 << 15, RequiredFlags: callflag.ReadStates,
ParamCount: 2, DisallowCallback: true},
ParamCount: 2},
{Name: interopnames.SystemStorageGetContext, Func: storageGetContext, Price: 1 << 4,
RequiredFlags: callflag.ReadStates, DisallowCallback: true},
RequiredFlags: callflag.ReadStates},
{Name: interopnames.SystemStorageGetReadOnlyContext, Func: storageGetReadOnlyContext, Price: 1 << 4,
RequiredFlags: callflag.ReadStates, DisallowCallback: true},
RequiredFlags: callflag.ReadStates},
{Name: interopnames.SystemStoragePut, Func: storagePut, Price: 0, RequiredFlags: callflag.WriteStates,
ParamCount: 3, DisallowCallback: true}, // These don't have static price in C# code.
ParamCount: 3}, // These don't have static price in C# code.
{Name: interopnames.SystemStoragePutEx, Func: storagePutEx, Price: 0, RequiredFlags: callflag.WriteStates,
ParamCount: 4, DisallowCallback: true},
ParamCount: 4},
{Name: interopnames.SystemStorageAsReadOnly, Func: storageContextAsReadOnly, Price: 1 << 4,
RequiredFlags: callflag.ReadStates, ParamCount: 1, DisallowCallback: true},
RequiredFlags: callflag.ReadStates, ParamCount: 1},
}
var neoInterops = []interop.Function{