From c54b45e76d60167cbc4ea7f5d30716536e609625 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 27 Jul 2020 12:23:48 +0300 Subject: [PATCH] core: implement `System.Callback.CreateFromMethod` interop Support creating callbacks from contract methods. Signed-off-by: Evgenii Stratonikov --- pkg/core/interop/callback/callback.go | 11 ++++- pkg/core/interop/callback/method.go | 60 +++++++++++++++++++++++++ pkg/core/interop_system_test.go | 63 ++++++++++++++++++++++++++- pkg/core/interops.go | 1 + 4 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 pkg/core/interop/callback/method.go diff --git a/pkg/core/interop/callback/callback.go b/pkg/core/interop/callback/callback.go index 6c3a11d5a..68d14cd77 100644 --- a/pkg/core/interop/callback/callback.go +++ b/pkg/core/interop/callback/callback.go @@ -5,6 +5,7 @@ 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/emit" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" ) @@ -17,12 +18,18 @@ type Callback interface { } // Invoke invokes provided callback. -func Invoke(_ *interop.Context, v *vm.VM) error { +func Invoke(ic *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 + switch cb.(type) { + case *MethodCallback: + id := emit.InteropNameToID([]byte("System.Contract.Call")) + return ic.SyscallHandler(v, id) + default: + return nil + } } diff --git a/pkg/core/interop/callback/method.go b/pkg/core/interop/callback/method.go new file mode 100644 index 000000000..4eade208d --- /dev/null +++ b/pkg/core/interop/callback/method.go @@ -0,0 +1,60 @@ +package callback + +import ( + "errors" + "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.ScriptHash().BytesBE()) +} + +// CreateFromMethod creates callback for a contract method. +func CreateFromMethod(ic *interop.Context, v *vm.VM) error { + rawHash := v.Estack().Pop().Bytes() + h, err := util.Uint160DecodeBytesBE(rawHash) + if err != nil { + return err + } + cs, err := ic.DAO.GetContractState(h) + if err != nil { + return errors.New("contract not found") + } + method := string(v.Estack().Pop().Bytes()) + if strings.HasPrefix(method, "_") { + return errors.New("invalid method name") + } + currCs, err := ic.DAO.GetContractState(v.GetCurrentScriptHash()) + if err == nil && !currCs.Manifest.CanCall(&cs.Manifest, method) { + return errors.New("method call is not allowed") + } + md := cs.Manifest.ABI.GetMethod(method) + v.Estack().PushVal(stackitem.NewInterop(&MethodCallback{ + contract: cs, + method: md, + })) + return nil +} diff --git a/pkg/core/interop_system_test.go b/pkg/core/interop_system_test.go index b96b0d463..b5606065c 100644 --- a/pkg/core/interop_system_test.go +++ b/pkg/core/interop_system_test.go @@ -378,7 +378,7 @@ func getTestContractState() (*state.Contract, *state.Contract) { ID: 42, } - currScript := []byte{byte(opcode.NOP)} + currScript := []byte{byte(opcode.RET)} m = manifest.NewManifest(hash.Hash160(currScript)) perm := manifest.NewPermission(manifest.PermissionHash, h) perm.Methods.Add("add") @@ -776,3 +776,64 @@ func TestPointerCallback(t *testing.T) { }) } + +func TestMethodCallback(t *testing.T) { + _, ic, bc := createVM(t) + defer bc.Close() + + cs, currCs := getTestContractState() + require.NoError(t, ic.DAO.PutContractState(cs)) + require.NoError(t, ic.DAO.PutContractState(currCs)) + + ic.Functions = append(ic.Functions, systemInterops) + rawHash := cs.Manifest.ABI.Hash.BytesBE() + + t.Run("Invalid", func(t *testing.T) { + runInvalid := func(args ...interface{}) func(t *testing.T) { + return func(t *testing.T) { + v := loadScript(currCs.Script, 42) + for i := range args { + v.Estack().PushVal(args[i]) + } + require.Error(t, callback.CreateFromMethod(ic, v)) + } + } + 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) { + v := loadScript(currCs.Script, 42, "add", rawHash) + require.NoError(t, callback.CreateFromMethod(ic, v)) + + v.Estack().InsertAt(vm.NewElement(stackitem.NewArray([]stackitem.Item{stackitem.Make(1)})), 1) + require.Error(t, callback.Invoke(ic, v)) + }) + t.Run("CallIsNotAllowed", func(t *testing.T) { + v := vm.New() + v.Load(currCs.Script) + v.Estack().PushVal("add") + v.Estack().PushVal(rawHash) + require.NoError(t, callback.CreateFromMethod(ic, v)) + + args := stackitem.NewArray([]stackitem.Item{stackitem.Make(1), stackitem.Make(5)}) + v.Estack().InsertAt(vm.NewElement(args), 1) + require.Error(t, callback.Invoke(ic, v)) + }) + }) + + t.Run("Good", func(t *testing.T) { + v := loadScript(currCs.Script, 42, "add", rawHash) + require.NoError(t, callback.CreateFromMethod(ic, v)) + + args := stackitem.NewArray([]stackitem.Item{stackitem.Make(1), stackitem.Make(5)}) + v.Estack().InsertAt(vm.NewElement(args), 1) + + require.NoError(t, callback.Invoke(ic, v)) + require.NoError(t, v.Run()) + require.Equal(t, 2, v.Estack().Len()) + require.Equal(t, big.NewInt(6), v.Estack().Pop().Item().Value()) + require.Equal(t, big.NewInt(42), v.Estack().Pop().Item().Value()) + }) +} diff --git a/pkg/core/interops.go b/pkg/core/interops.go index 7e305eb72..b46624239 100644 --- a/pkg/core/interops.go +++ b/pkg/core/interops.go @@ -42,6 +42,7 @@ var systemInterops = []interop.Function{ {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.CreateFromMethod", Func: callback.CreateFromMethod, Price: 1000000}, {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},