core: implement System.Callback.* interops

Support creating callbacks from pointers.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2020-07-27 11:06:06 +03:00
parent c09ea04df3
commit 382a7f5b3e
4 changed files with 109 additions and 0 deletions

View file

@ -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
}

View file

@ -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
}

View file

@ -6,6 +6,7 @@ import (
"github.com/nspcc-dev/dbft/crypto" "github.com/nspcc-dev/dbft/crypto"
"github.com/nspcc-dev/neo-go/pkg/config/netmode" "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/interop/runtime"
"github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/core/transaction" "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.NoError(t, contractGetCallFlags(ic, v))
require.Equal(t, int64(smartcontract.All), v.Estack().Pop().Value().(*big.Int).Int64()) 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))
})
})
}

View file

@ -9,6 +9,7 @@ package core
import ( import (
"github.com/nspcc-dev/neo-go/pkg/core/interop" "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/crypto"
"github.com/nspcc-dev/neo-go/pkg/core/interop/enumerator" "github.com/nspcc-dev/neo-go/pkg/core/interop/enumerator"
"github.com/nspcc-dev/neo-go/pkg/core/interop/iterator" "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.GetTransaction", Func: bcGetTransaction, Price: 1000000, RequiredFlags: smartcontract.AllowStates},
{Name: "System.Blockchain.GetTransactionFromBlock", Func: bcGetTransactionFromBlock, 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.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.Call", Func: contractCall, Price: 1000000, RequiredFlags: smartcontract.AllowCall},
{Name: "System.Contract.CallEx", Func: contractCallEx, 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}, {Name: "System.Contract.Create", Func: contractCreate, Price: 0, RequiredFlags: smartcontract.AllowModifyStates},