forked from TrueCloudLab/neoneo-go
core: implement System.Callback.CreateFromSyscall
interop
Allow to create callbacks from syscalls. Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
c54b45e76d
commit
99b0397a61
5 changed files with 153 additions and 54 deletions
|
@ -25,10 +25,12 @@ func Invoke(ic *interop.Context, v *vm.VM) error {
|
|||
return errors.New("invalid argument count")
|
||||
}
|
||||
cb.LoadContext(v, args)
|
||||
switch cb.(type) {
|
||||
switch t := cb.(type) {
|
||||
case *MethodCallback:
|
||||
id := emit.InteropNameToID([]byte("System.Contract.Call"))
|
||||
return ic.SyscallHandler(v, id)
|
||||
case *SyscallCallback:
|
||||
return ic.SyscallHandler(v, t.desc.ID)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
|
39
pkg/core/interop/callback/syscall.go
Normal file
39
pkg/core/interop/callback/syscall.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
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, v *vm.VM) error {
|
||||
id := uint32(v.Estack().Pop().BigInt().Int64())
|
||||
f := ic.GetFunction(id)
|
||||
if f == nil {
|
||||
return errors.New("syscall not found")
|
||||
}
|
||||
v.Estack().PushVal(stackitem.NewInterop(&SyscallCallback{f}))
|
||||
return nil
|
||||
}
|
|
@ -62,10 +62,12 @@ func NewContext(trigger trigger.Type, bc blockchainer.Blockchainer, d dao.DAO, n
|
|||
// it's supposed to be inited once for all interopContexts, so it doesn't use
|
||||
// vm.InteropFuncPrice directly.
|
||||
type Function struct {
|
||||
ID uint32
|
||||
Name string
|
||||
Func func(*Context, *vm.VM) error
|
||||
Price int64
|
||||
ID uint32
|
||||
Name string
|
||||
Func func(*Context, *vm.VM) error
|
||||
// ParamCount is a number of function parameters.
|
||||
ParamCount int
|
||||
Price int64
|
||||
// RequiredFlags is a set of flags which must be set during script invocations.
|
||||
// Default value is NoneFlag i.e. no flags are required.
|
||||
RequiredFlags smartcontract.CallFlag
|
||||
|
|
|
@ -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"
|
||||
"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"
|
||||
|
@ -837,3 +838,42 @@ func TestMethodCallback(t *testing.T) {
|
|||
require.Equal(t, big.NewInt(42), v.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(_ *interop.Context, v *vm.VM) error {
|
||||
a := v.Estack().Pop().BigInt()
|
||||
b := v.Estack().Pop().BigInt()
|
||||
v.Estack().PushVal(new(big.Int).Add(a, b))
|
||||
return nil
|
||||
},
|
||||
ParamCount: 2,
|
||||
},
|
||||
})
|
||||
|
||||
t.Run("Good", func(t *testing.T) {
|
||||
args := stackitem.NewArray([]stackitem.Item{stackitem.Make(12), stackitem.Make(30)})
|
||||
v := loadScript([]byte{byte(opcode.RET)}, args, 0x42)
|
||||
require.NoError(t, callback.CreateFromSyscall(ic, v))
|
||||
require.NoError(t, callback.Invoke(ic, v))
|
||||
require.Equal(t, 1, v.Estack().Len())
|
||||
require.Equal(t, big.NewInt(42), v.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)})
|
||||
v := loadScript([]byte{byte(opcode.RET)}, args, 0x42)
|
||||
require.NoError(t, callback.CreateFromSyscall(ic, v))
|
||||
require.Error(t, callback.Invoke(ic, v))
|
||||
})
|
||||
t.Run("MissingSyscall", func(t *testing.T) {
|
||||
v := loadScript([]byte{byte(opcode.RET)}, stackitem.NewArray(nil), 0x43)
|
||||
require.Error(t, callback.CreateFromSyscall(ic, v))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
@ -31,70 +31,86 @@ func SpawnVM(ic *interop.Context) *vm.VM {
|
|||
|
||||
// All lists are sorted, keep 'em this way, please.
|
||||
var systemInterops = []interop.Function{
|
||||
{Name: "System.Binary.Base64Decode", Func: runtimeDecode, Price: 100000},
|
||||
{Name: "System.Binary.Base64Encode", Func: runtimeEncode, Price: 100000},
|
||||
{Name: "System.Binary.Deserialize", Func: runtimeDeserialize, Price: 500000},
|
||||
{Name: "System.Binary.Serialize", Func: runtimeSerialize, Price: 100000},
|
||||
{Name: "System.Blockchain.GetBlock", Func: bcGetBlock, Price: 2500000, RequiredFlags: smartcontract.AllowStates},
|
||||
{Name: "System.Blockchain.GetContract", Func: bcGetContract, Price: 1000000, RequiredFlags: smartcontract.AllowStates},
|
||||
{Name: "System.Blockchain.GetHeight", Func: bcGetHeight, Price: 400, 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.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},
|
||||
{Name: "System.Contract.Create", Func: contractCreate, Price: 0, RequiredFlags: smartcontract.AllowModifyStates},
|
||||
{Name: "System.Contract.CreateStandardAccount", Func: contractCreateStandardAccount, Price: 10000},
|
||||
{Name: "System.Contract.Destroy", Func: contractDestroy, Price: 1000000, RequiredFlags: smartcontract.AllowModifyStates},
|
||||
{Name: "System.Contract.IsStandard", Func: contractIsStandard, Price: 30000},
|
||||
{Name: "System.Contract.GetCallFlags", Func: contractGetCallFlags, Price: 30000},
|
||||
{Name: "System.Contract.Update", Func: contractUpdate, Price: 0, RequiredFlags: smartcontract.AllowModifyStates},
|
||||
{Name: "System.Enumerator.Concat", Func: enumerator.Concat, Price: 400},
|
||||
{Name: "System.Enumerator.Create", Func: enumerator.Create, Price: 400},
|
||||
{Name: "System.Enumerator.Next", Func: enumerator.Next, Price: 1000000},
|
||||
{Name: "System.Enumerator.Value", Func: enumerator.Value, Price: 400},
|
||||
{Name: "System.Iterator.Concat", Func: iterator.Concat, Price: 400},
|
||||
{Name: "System.Iterator.Create", Func: iterator.Create, Price: 400},
|
||||
{Name: "System.Iterator.Key", Func: iterator.Key, Price: 400},
|
||||
{Name: "System.Iterator.Keys", Func: iterator.Keys, Price: 400},
|
||||
{Name: "System.Iterator.Values", Func: iterator.Values, Price: 400},
|
||||
{Name: "System.Json.Deserialize", Func: json.Deserialize, Price: 500000},
|
||||
{Name: "System.Json.Serialize", Func: json.Serialize, Price: 100000},
|
||||
{Name: "System.Runtime.CheckWitness", Func: runtime.CheckWitness, Price: 30000,
|
||||
{Name: "System.Binary.Base64Decode", Func: runtimeDecode, Price: 100000, ParamCount: 1},
|
||||
{Name: "System.Binary.Base64Encode", Func: runtimeEncode, Price: 100000, ParamCount: 1},
|
||||
{Name: "System.Binary.Deserialize", Func: runtimeDeserialize, Price: 500000, ParamCount: 1},
|
||||
{Name: "System.Binary.Serialize", Func: runtimeSerialize, Price: 100000, ParamCount: 1},
|
||||
{Name: "System.Blockchain.GetBlock", Func: bcGetBlock, Price: 2500000,
|
||||
RequiredFlags: smartcontract.AllowStates, ParamCount: 1},
|
||||
{Name: "System.Blockchain.GetContract", Func: bcGetContract, Price: 1000000,
|
||||
RequiredFlags: smartcontract.AllowStates, ParamCount: 1},
|
||||
{Name: "System.Blockchain.GetHeight", Func: bcGetHeight, Price: 400,
|
||||
RequiredFlags: smartcontract.AllowStates},
|
||||
{Name: "System.Blockchain.GetTransaction", Func: bcGetTransaction, Price: 1000000,
|
||||
RequiredFlags: smartcontract.AllowStates, ParamCount: 1},
|
||||
{Name: "System.Blockchain.GetTransactionFromBlock", Func: bcGetTransactionFromBlock, Price: 1000000,
|
||||
RequiredFlags: smartcontract.AllowStates, ParamCount: 2},
|
||||
{Name: "System.Blockchain.GetTransactionHeight", Func: bcGetTransactionHeight, Price: 1000000,
|
||||
RequiredFlags: smartcontract.AllowStates, ParamCount: 1},
|
||||
{Name: "System.Callback.Create", Func: callback.Create, Price: 400, ParamCount: 3},
|
||||
{Name: "System.Callback.CreateFromMethod", Func: callback.CreateFromMethod, Price: 1000000, ParamCount: 2},
|
||||
{Name: "System.Callback.CreateFromSyscall", Func: callback.CreateFromSyscall, Price: 400, ParamCount: 1},
|
||||
{Name: "System.Callback.Invoke", Func: callback.Invoke, Price: 1000000, ParamCount: 2},
|
||||
{Name: "System.Contract.Call", Func: contractCall, Price: 1000000,
|
||||
RequiredFlags: smartcontract.AllowCall, ParamCount: 3},
|
||||
{Name: "System.Contract.CallEx", Func: contractCallEx, Price: 1000000,
|
||||
RequiredFlags: smartcontract.AllowCall, ParamCount: 4},
|
||||
{Name: "System.Contract.Create", Func: contractCreate, Price: 0,
|
||||
RequiredFlags: smartcontract.AllowModifyStates, ParamCount: 2},
|
||||
{Name: "System.Contract.CreateStandardAccount", Func: contractCreateStandardAccount, Price: 10000, ParamCount: 1},
|
||||
{Name: "System.Contract.Destroy", Func: contractDestroy, Price: 1000000, RequiredFlags: smartcontract.AllowModifyStates},
|
||||
{Name: "System.Contract.IsStandard", Func: contractIsStandard, Price: 30000, ParamCount: 1},
|
||||
{Name: "System.Contract.GetCallFlags", Func: contractGetCallFlags, Price: 30000},
|
||||
{Name: "System.Contract.Update", Func: contractUpdate, Price: 0,
|
||||
RequiredFlags: smartcontract.AllowModifyStates, ParamCount: 2},
|
||||
{Name: "System.Enumerator.Concat", Func: enumerator.Concat, Price: 400, ParamCount: 2},
|
||||
{Name: "System.Enumerator.Create", Func: enumerator.Create, Price: 400, ParamCount: 1},
|
||||
{Name: "System.Enumerator.Next", Func: enumerator.Next, Price: 1000000, ParamCount: 1},
|
||||
{Name: "System.Enumerator.Value", Func: enumerator.Value, Price: 400, ParamCount: 1},
|
||||
{Name: "System.Iterator.Concat", Func: iterator.Concat, Price: 400, ParamCount: 2},
|
||||
{Name: "System.Iterator.Create", Func: iterator.Create, Price: 400, ParamCount: 1},
|
||||
{Name: "System.Iterator.Key", Func: iterator.Key, Price: 400, ParamCount: 1},
|
||||
{Name: "System.Iterator.Keys", Func: iterator.Keys, Price: 400, ParamCount: 1},
|
||||
{Name: "System.Iterator.Values", Func: iterator.Values, Price: 400, ParamCount: 1},
|
||||
{Name: "System.Json.Deserialize", Func: json.Deserialize, Price: 500000, ParamCount: 1},
|
||||
{Name: "System.Json.Serialize", Func: json.Serialize, Price: 100000, ParamCount: 1},
|
||||
{Name: "System.Runtime.CheckWitness", Func: runtime.CheckWitness, Price: 30000,
|
||||
RequiredFlags: smartcontract.AllowStates, ParamCount: 1},
|
||||
{Name: "System.Runtime.GasLeft", Func: runtime.GasLeft, Price: 400},
|
||||
{Name: "System.Runtime.GetCallingScriptHash", Func: engineGetCallingScriptHash, Price: 400},
|
||||
{Name: "System.Runtime.GetEntryScriptHash", Func: engineGetEntryScriptHash, Price: 400},
|
||||
{Name: "System.Runtime.GetExecutingScriptHash", Func: engineGetExecutingScriptHash, Price: 400},
|
||||
{Name: "System.Runtime.GetInvocationCounter", Func: runtime.GetInvocationCounter, Price: 400},
|
||||
{Name: "System.Runtime.GetNotifications", Func: runtime.GetNotifications, Price: 10000},
|
||||
{Name: "System.Runtime.GetNotifications", Func: runtime.GetNotifications, Price: 10000, ParamCount: 1},
|
||||
{Name: "System.Runtime.GetScriptContainer", Func: engineGetScriptContainer, Price: 250},
|
||||
{Name: "System.Runtime.GetTime", Func: runtimeGetTime, Price: 250, RequiredFlags: smartcontract.AllowStates},
|
||||
{Name: "System.Runtime.GetTrigger", Func: runtimeGetTrigger, Price: 250},
|
||||
{Name: "System.Runtime.Log", Func: runtimeLog, Price: 1000000, RequiredFlags: smartcontract.AllowNotify},
|
||||
{Name: "System.Runtime.Notify", Func: runtimeNotify, Price: 1000000, RequiredFlags: smartcontract.AllowNotify},
|
||||
{Name: "System.Runtime.Log", Func: runtimeLog, Price: 1000000, RequiredFlags: smartcontract.AllowNotify, ParamCount: 1},
|
||||
{Name: "System.Runtime.Notify", Func: runtimeNotify, Price: 1000000, RequiredFlags: smartcontract.AllowNotify, ParamCount: 2},
|
||||
{Name: "System.Runtime.Platform", Func: runtimePlatform, Price: 250},
|
||||
{Name: "System.Storage.Delete", Func: storageDelete, Price: StoragePrice, RequiredFlags: smartcontract.AllowModifyStates},
|
||||
{Name: "System.Storage.Find", Func: storageFind, Price: 1000000, RequiredFlags: smartcontract.AllowStates},
|
||||
{Name: "System.Storage.Get", Func: storageGet, Price: 1000000, RequiredFlags: smartcontract.AllowStates},
|
||||
{Name: "System.Storage.Delete", Func: storageDelete, Price: StoragePrice,
|
||||
RequiredFlags: smartcontract.AllowModifyStates, ParamCount: 2},
|
||||
{Name: "System.Storage.Find", Func: storageFind, Price: 1000000, RequiredFlags: smartcontract.AllowStates, ParamCount: 2},
|
||||
{Name: "System.Storage.Get", Func: storageGet, Price: 1000000, RequiredFlags: smartcontract.AllowStates, ParamCount: 2},
|
||||
{Name: "System.Storage.GetContext", Func: storageGetContext, Price: 400, RequiredFlags: smartcontract.AllowStates},
|
||||
{Name: "System.Storage.GetReadOnlyContext", Func: storageGetReadOnlyContext, Price: 400, RequiredFlags: smartcontract.AllowStates},
|
||||
{Name: "System.Storage.Put", Func: storagePut, Price: 0, RequiredFlags: smartcontract.AllowModifyStates}, // These don't have static price in C# code.
|
||||
{Name: "System.Storage.PutEx", Func: storagePutEx, Price: 0, RequiredFlags: smartcontract.AllowModifyStates},
|
||||
{Name: "System.Storage.AsReadOnly", Func: storageContextAsReadOnly, Price: 400, RequiredFlags: smartcontract.AllowStates},
|
||||
{Name: "System.Storage.Put", Func: storagePut, Price: 0, RequiredFlags: smartcontract.AllowModifyStates,
|
||||
ParamCount: 3}, // These don't have static price in C# code.
|
||||
{Name: "System.Storage.PutEx", Func: storagePutEx, Price: 0, RequiredFlags: smartcontract.AllowModifyStates, ParamCount: 4},
|
||||
{Name: "System.Storage.AsReadOnly", Func: storageContextAsReadOnly, Price: 400,
|
||||
RequiredFlags: smartcontract.AllowStates, ParamCount: 1},
|
||||
}
|
||||
|
||||
var neoInterops = []interop.Function{
|
||||
{Name: "Neo.Crypto.VerifyWithECDsaSecp256r1", Func: crypto.ECDSASecp256r1Verify, Price: crypto.ECDSAVerifyPrice},
|
||||
{Name: "Neo.Crypto.VerifyWithECDsaSecp256k1", Func: crypto.ECDSASecp256k1Verify, Price: crypto.ECDSAVerifyPrice},
|
||||
{Name: "Neo.Crypto.CheckMultisigWithECDsaSecp256r1", Func: crypto.ECDSASecp256r1CheckMultisig, Price: 0},
|
||||
{Name: "Neo.Crypto.CheckMultisigWithECDsaSecp256k1", Func: crypto.ECDSASecp256k1CheckMultisig, Price: 0},
|
||||
{Name: "Neo.Crypto.SHA256", Func: crypto.Sha256, Price: 1000000},
|
||||
{Name: "Neo.Crypto.RIPEMD160", Func: crypto.RipeMD160, Price: 1000000},
|
||||
{Name: "Neo.Native.Call", Func: native.Call, Price: 0},
|
||||
{Name: "Neo.Crypto.VerifyWithECDsaSecp256r1", Func: crypto.ECDSASecp256r1Verify,
|
||||
Price: crypto.ECDSAVerifyPrice, ParamCount: 3},
|
||||
{Name: "Neo.Crypto.VerifyWithECDsaSecp256k1", Func: crypto.ECDSASecp256k1Verify,
|
||||
Price: crypto.ECDSAVerifyPrice, ParamCount: 3},
|
||||
{Name: "Neo.Crypto.CheckMultisigWithECDsaSecp256r1", Func: crypto.ECDSASecp256r1CheckMultisig, Price: 0, ParamCount: 3},
|
||||
{Name: "Neo.Crypto.CheckMultisigWithECDsaSecp256k1", Func: crypto.ECDSASecp256k1CheckMultisig, Price: 0, ParamCount: 3},
|
||||
{Name: "Neo.Crypto.SHA256", Func: crypto.Sha256, Price: 1000000, ParamCount: 1},
|
||||
{Name: "Neo.Crypto.RIPEMD160", Func: crypto.RipeMD160, Price: 1000000, ParamCount: 1},
|
||||
{Name: "Neo.Native.Call", Func: native.Call, Price: 0, ParamCount: 1},
|
||||
{Name: "Neo.Native.Deploy", Func: native.Deploy, Price: 0, RequiredFlags: smartcontract.AllowModifyStates},
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue