diff --git a/pkg/core/interop/callback/callback.go b/pkg/core/interop/callback/callback.go new file mode 100644 index 000000000..6c3a11d5a --- /dev/null +++ b/pkg/core/interop/callback/callback.go @@ -0,0 +1,28 @@ +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" +) + +// 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(_ *interop.Context, v *vm.VM) error { + cb := v.Estack().Pop().Interop().Value().(Callback) + args := v.Estack().Pop().Array() + if cb.ArgCount() != len(args) { + return errors.New("invalid argument count") + } + cb.LoadContext(v, args) + return nil +} diff --git a/pkg/core/interop/callback/pointer.go b/pkg/core/interop/callback/pointer.go new file mode 100644 index 000000000..87963ae6a --- /dev/null +++ b/pkg/core/interop/callback/pointer.go @@ -0,0 +1,42 @@ +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(_ *interop.Context, v *vm.VM) error { + ctx := v.Estack().Pop().Item().(*vm.Context) + offset := v.Estack().Pop().Item().(*stackitem.Pointer).Position() + count := v.Estack().Pop().BigInt().Int64() + v.Estack().PushVal(stackitem.NewInterop(&PointerCallback{ + paramCount: int(count), + offset: offset, + context: ctx, + })) + return nil +} diff --git a/pkg/core/interop_system_test.go b/pkg/core/interop_system_test.go index 33f9414b1..677b81104 100644 --- a/pkg/core/interop_system_test.go +++ b/pkg/core/interop_system_test.go @@ -6,6 +6,7 @@ import ( "github.com/nspcc-dev/dbft/crypto" "github.com/nspcc-dev/neo-go/pkg/config/netmode" + "github.com/nspcc-dev/neo-go/pkg/core/interop/callback" "github.com/nspcc-dev/neo-go/pkg/core/interop/runtime" "github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/core/transaction" @@ -733,3 +734,38 @@ func TestContractGetCallFlags(t *testing.T) { require.NoError(t, contractGetCallFlags(ic, v)) require.Equal(t, int64(smartcontract.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) { + v := loadScript(script, 2, stackitem.NewPointer(3, script)) + v.Estack().PushVal(v.Context()) + require.NoError(t, callback.Create(ic, v)) + + args := stackitem.NewArray([]stackitem.Item{stackitem.Make(3), stackitem.Make(12)}) + v.Estack().InsertAt(vm.NewElement(args), 1) + require.NoError(t, callback.Invoke(ic, v)) + + require.NoError(t, v.Run()) + require.Equal(t, 1, v.Estack().Len()) + require.Equal(t, big.NewInt(5), v.Estack().Pop().Item().Value()) + }) + t.Run("Invalid", func(t *testing.T) { + t.Run("NotEnoughParameters", func(t *testing.T) { + v := loadScript(script, 2, stackitem.NewPointer(3, script)) + v.Estack().PushVal(v.Context()) + require.NoError(t, callback.Create(ic, v)) + + args := stackitem.NewArray([]stackitem.Item{stackitem.Make(3)}) + v.Estack().InsertAt(vm.NewElement(args), 1) + require.Error(t, callback.Invoke(ic, v)) + }) + }) + +} diff --git a/pkg/core/interops.go b/pkg/core/interops.go index 778f4974f..7e305eb72 100644 --- a/pkg/core/interops.go +++ b/pkg/core/interops.go @@ -9,6 +9,7 @@ package core import ( "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/crypto" "github.com/nspcc-dev/neo-go/pkg/core/interop/enumerator" "github.com/nspcc-dev/neo-go/pkg/core/interop/iterator" @@ -40,6 +41,8 @@ var systemInterops = []interop.Function{ {Name: "System.Blockchain.GetTransaction", Func: bcGetTransaction, Price: 1000000, RequiredFlags: smartcontract.AllowStates}, {Name: "System.Blockchain.GetTransactionFromBlock", Func: bcGetTransactionFromBlock, Price: 1000000, RequiredFlags: smartcontract.AllowStates}, {Name: "System.Blockchain.GetTransactionHeight", Func: bcGetTransactionHeight, Price: 1000000, RequiredFlags: smartcontract.AllowStates}, + {Name: "System.Callback.Create", Func: callback.Create, Price: 400}, + {Name: "System.Callback.Invoke", Func: callback.Invoke, Price: 1000000}, {Name: "System.Contract.Call", Func: contractCall, Price: 1000000, RequiredFlags: smartcontract.AllowCall}, {Name: "System.Contract.CallEx", Func: contractCallEx, Price: 1000000, RequiredFlags: smartcontract.AllowCall}, {Name: "System.Contract.Create", Func: contractCreate, Price: 0, RequiredFlags: smartcontract.AllowModifyStates},