mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-23 23:25:22 +00:00
core: implement System.Callback.CreateFromMethod
interop
Support creating callbacks from contract methods. Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
f96c217aba
commit
c54b45e76d
4 changed files with 132 additions and 3 deletions
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
60
pkg/core/interop/callback/method.go
Normal file
60
pkg/core/interop/callback/method.go
Normal file
|
@ -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
|
||||
}
|
|
@ -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())
|
||||
})
|
||||
}
|
||||
|
|
|
@ -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},
|
||||
|
|
Loading…
Reference in a new issue