2019-10-11 14:00:11 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
/*
|
|
|
|
Interops are designed to run under VM's execute() panic protection, so it's OK
|
|
|
|
for them to do things like
|
|
|
|
smth := v.Estack().Pop().Bytes()
|
|
|
|
even though technically Pop() can return a nil pointer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import (
|
2019-12-18 16:49:56 +00:00
|
|
|
"sort"
|
|
|
|
|
2020-04-08 10:35:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2020-03-18 09:41:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/crypto"
|
2020-04-08 11:08:43 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/enumerator"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/iterator"
|
2020-06-11 14:17:15 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/json"
|
2020-04-13 12:56:41 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/runtime"
|
2020-03-19 15:52:37 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native"
|
2020-06-10 14:21:26 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-06-10 15:07:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-04-15 14:13:50 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
2019-10-11 14:00:11 +00:00
|
|
|
)
|
|
|
|
|
2020-04-07 09:54:43 +00:00
|
|
|
// SpawnVM returns a VM with script getter and interop functions set
|
|
|
|
// up for current blockchain.
|
2020-04-08 10:35:39 +00:00
|
|
|
func SpawnVM(ic *interop.Context) *vm.VM {
|
2020-06-10 15:07:21 +00:00
|
|
|
vm := vm.NewWithTrigger(ic.Trigger)
|
2020-04-08 10:29:15 +00:00
|
|
|
vm.RegisterInteropGetter(getSystemInterop(ic))
|
|
|
|
vm.RegisterInteropGetter(getNeoInterop(ic))
|
2020-05-07 11:38:19 +00:00
|
|
|
if ic.Chain != nil {
|
|
|
|
vm.RegisterInteropGetter(ic.Chain.(*Blockchain).contracts.GetNativeInterop(ic))
|
|
|
|
}
|
2020-07-15 19:43:30 +00:00
|
|
|
ic.ScriptGetter = vm
|
2020-04-07 09:54:43 +00:00
|
|
|
return vm
|
|
|
|
}
|
|
|
|
|
2019-12-18 16:49:56 +00:00
|
|
|
// getSystemInterop returns matching interop function from the System namespace
|
|
|
|
// for a given id in the current context.
|
2020-04-08 10:35:39 +00:00
|
|
|
func getSystemInterop(ic *interop.Context) vm.InteropGetterFunc {
|
2020-04-08 10:29:15 +00:00
|
|
|
return getInteropFromSlice(ic, systemInterops)
|
2019-12-18 16:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getNeoInterop returns matching interop function from the Neo and AntShares
|
|
|
|
// namespaces for a given id in the current context.
|
2020-04-08 10:35:39 +00:00
|
|
|
func getNeoInterop(ic *interop.Context) vm.InteropGetterFunc {
|
2020-04-08 10:29:15 +00:00
|
|
|
return getInteropFromSlice(ic, neoInterops)
|
2019-12-18 16:49:56 +00:00
|
|
|
}
|
2019-10-11 14:00:11 +00:00
|
|
|
|
2019-12-18 16:49:56 +00:00
|
|
|
// getInteropFromSlice returns matching interop function from the given slice of
|
|
|
|
// interop functions in the current context.
|
2020-04-08 10:35:39 +00:00
|
|
|
func getInteropFromSlice(ic *interop.Context, slice []interop.Function) func(uint32) *vm.InteropFuncPrice {
|
2020-04-08 10:29:15 +00:00
|
|
|
return func(id uint32) *vm.InteropFuncPrice {
|
|
|
|
n := sort.Search(len(slice), func(i int) bool {
|
|
|
|
return slice[i].ID >= id
|
|
|
|
})
|
|
|
|
if n < len(slice) && slice[n].ID == id {
|
2020-06-10 14:21:26 +00:00
|
|
|
return &vm.InteropFuncPrice{
|
|
|
|
Func: func(v *vm.VM) error {
|
|
|
|
return slice[n].Func(ic, v)
|
|
|
|
},
|
|
|
|
Price: slice[n].Price,
|
|
|
|
RequiredFlags: slice[n].RequiredFlags,
|
|
|
|
}
|
2020-04-08 10:29:15 +00:00
|
|
|
}
|
|
|
|
return nil
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-18 16:49:56 +00:00
|
|
|
// All lists are sorted, keep 'em this way, please.
|
2020-04-08 10:35:39 +00:00
|
|
|
var systemInterops = []interop.Function{
|
2020-07-15 08:49:24 +00:00
|
|
|
{Name: "System.Binary.Base64Decode", Func: runtimeDecode, Price: 100000},
|
|
|
|
{Name: "System.Binary.Base64Encode", Func: runtimeEncode, Price: 100000},
|
2020-06-16 07:56:06 +00:00
|
|
|
{Name: "System.Binary.Deserialize", Func: runtimeDeserialize, Price: 500000},
|
|
|
|
{Name: "System.Binary.Serialize", Func: runtimeSerialize, Price: 100000},
|
2020-06-16 08:05:03 +00:00
|
|
|
{Name: "System.Blockchain.GetBlock", Func: bcGetBlock, Price: 2500000,
|
2020-06-10 15:07:21 +00:00
|
|
|
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowStates},
|
2020-06-16 08:05:03 +00:00
|
|
|
{Name: "System.Blockchain.GetContract", Func: bcGetContract, Price: 1000000,
|
2020-06-10 15:07:21 +00:00
|
|
|
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowStates},
|
2020-06-16 08:05:03 +00:00
|
|
|
{Name: "System.Blockchain.GetHeight", Func: bcGetHeight, Price: 400,
|
2020-06-10 15:07:21 +00:00
|
|
|
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowStates},
|
2020-06-16 08:05:03 +00:00
|
|
|
{Name: "System.Blockchain.GetTransaction", Func: bcGetTransaction, Price: 1000000,
|
2020-06-10 15:07:21 +00:00
|
|
|
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowStates},
|
2020-06-16 08:05:03 +00:00
|
|
|
{Name: "System.Blockchain.GetTransactionFromBlock", Func: bcGetTransactionFromBlock, Price: 1000000,
|
2020-06-10 15:07:21 +00:00
|
|
|
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowStates},
|
2020-06-16 08:05:03 +00:00
|
|
|
{Name: "System.Blockchain.GetTransactionHeight", Func: bcGetTransactionHeight, Price: 1000000,
|
2020-06-10 15:07:21 +00:00
|
|
|
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowStates},
|
2020-06-16 08:05:03 +00:00
|
|
|
{Name: "System.Contract.Call", Func: contractCall, Price: 1000000,
|
2020-06-10 15:07:21 +00:00
|
|
|
AllowedTriggers: trigger.System | trigger.Application, RequiredFlags: smartcontract.AllowCall},
|
2020-06-16 08:05:03 +00:00
|
|
|
{Name: "System.Contract.CallEx", Func: contractCallEx, Price: 1000000,
|
2020-06-10 15:07:21 +00:00
|
|
|
AllowedTriggers: trigger.System | trigger.Application, RequiredFlags: smartcontract.AllowCall},
|
|
|
|
{Name: "System.Contract.Create", Func: contractCreate, Price: 0,
|
|
|
|
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowModifyStates},
|
2020-06-16 08:25:30 +00:00
|
|
|
{Name: "System.Contract.CreateStandardAccount", Func: contractCreateStandardAccount, Price: 10000},
|
2020-06-16 08:05:03 +00:00
|
|
|
{Name: "System.Contract.Destroy", Func: contractDestroy, Price: 1000000,
|
2020-06-10 15:07:21 +00:00
|
|
|
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowModifyStates},
|
2020-06-16 08:17:18 +00:00
|
|
|
{Name: "System.Contract.IsStandard", Func: contractIsStandard, Price: 30000},
|
2020-07-16 09:13:55 +00:00
|
|
|
{Name: "System.Contract.GetCallFlags", Func: contractGetCallFlags, Price: 30000},
|
2020-06-10 15:07:21 +00:00
|
|
|
{Name: "System.Contract.Update", Func: contractUpdate, Price: 0,
|
|
|
|
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowModifyStates},
|
2020-06-16 08:00:19 +00:00
|
|
|
{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},
|
2020-06-11 14:17:15 +00:00
|
|
|
{Name: "System.Json.Deserialize", Func: json.Deserialize, Price: 500000},
|
|
|
|
{Name: "System.Json.Serialize", Func: json.Serialize, Price: 100000},
|
2020-06-16 08:56:51 +00:00
|
|
|
{Name: "System.Runtime.CheckWitness", Func: runtime.CheckWitness, Price: 30000},
|
2020-06-16 09:04:08 +00:00
|
|
|
{Name: "System.Runtime.GasLeft", Func: runtime.GasLeft, Price: 400},
|
2020-06-16 08:54:48 +00:00
|
|
|
{Name: "System.Runtime.GetCallingScriptHash", Func: engineGetCallingScriptHash, Price: 400},
|
|
|
|
{Name: "System.Runtime.GetEntryScriptHash", Func: engineGetEntryScriptHash, Price: 400},
|
|
|
|
{Name: "System.Runtime.GetExecutingScriptHash", Func: engineGetExecutingScriptHash, Price: 400},
|
2020-06-16 09:47:42 +00:00
|
|
|
{Name: "System.Runtime.GetInvocationCounter", Func: runtime.GetInvocationCounter, Price: 400},
|
2020-06-16 08:54:48 +00:00
|
|
|
{Name: "System.Runtime.GetScriptContainer", Func: engineGetScriptContainer, Price: 250},
|
2020-06-16 08:56:51 +00:00
|
|
|
{Name: "System.Runtime.GetTime", Func: runtimeGetTime, Price: 250,
|
|
|
|
AllowedTriggers: trigger.Application},
|
|
|
|
{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.Platform", Func: runtimePlatform, Price: 250},
|
2020-06-15 09:01:04 +00:00
|
|
|
{Name: "System.Storage.Delete", Func: storageDelete, Price: StoragePrice,
|
2020-06-10 15:07:21 +00:00
|
|
|
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowModifyStates},
|
2020-06-15 09:01:04 +00:00
|
|
|
{Name: "System.Storage.Find", Func: storageFind, Price: 1000000,
|
2020-06-10 15:07:21 +00:00
|
|
|
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowStates},
|
2020-06-15 09:01:04 +00:00
|
|
|
{Name: "System.Storage.Get", Func: storageGet, Price: 1000000,
|
2020-06-10 15:07:21 +00:00
|
|
|
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowStates},
|
2020-06-15 09:01:04 +00:00
|
|
|
{Name: "System.Storage.GetContext", Func: storageGetContext, Price: 400,
|
2020-06-10 15:07:21 +00:00
|
|
|
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowStates},
|
2020-06-15 09:01:04 +00:00
|
|
|
{Name: "System.Storage.GetReadOnlyContext", Func: storageGetReadOnlyContext, Price: 400,
|
2020-06-10 15:07:21 +00:00
|
|
|
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowStates},
|
|
|
|
{Name: "System.Storage.Put", Func: storagePut, Price: 0,
|
|
|
|
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowModifyStates}, // These don't have static price in C# code.
|
|
|
|
{Name: "System.Storage.PutEx", Func: storagePutEx, Price: 0,
|
|
|
|
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowModifyStates},
|
2020-06-15 09:01:04 +00:00
|
|
|
{Name: "System.Storage.AsReadOnly", Func: storageContextAsReadOnly, Price: 400,
|
2020-06-10 15:07:21 +00:00
|
|
|
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowStates},
|
2019-12-18 16:49:56 +00:00
|
|
|
}
|
2019-10-11 14:00:11 +00:00
|
|
|
|
2020-04-08 10:35:39 +00:00
|
|
|
var neoInterops = []interop.Function{
|
2020-07-10 06:56:15 +00:00
|
|
|
{Name: "Neo.Crypto.VerifyWithECDsaSecp256r1", Func: crypto.ECDSASecp256r1Verify, Price: crypto.ECDSAVerifyPrice},
|
2020-07-13 09:59:41 +00:00
|
|
|
{Name: "Neo.Crypto.VerifyWithECDsaSecp256k1", Func: crypto.ECDSASecp256k1Verify, Price: crypto.ECDSAVerifyPrice},
|
2020-07-13 10:39:36 +00:00
|
|
|
{Name: "Neo.Crypto.CheckMultisigWithECDsaSecp256r1", Func: crypto.ECDSASecp256r1CheckMultisig, Price: 0},
|
2020-07-13 09:59:41 +00:00
|
|
|
{Name: "Neo.Crypto.CheckMultisigWithECDsaSecp256k1", Func: crypto.ECDSASecp256k1CheckMultisig, Price: 0},
|
2020-06-19 09:21:37 +00:00
|
|
|
{Name: "Neo.Crypto.SHA256", Func: crypto.Sha256, Price: 1000000},
|
2020-07-22 09:22:20 +00:00
|
|
|
{Name: "Neo.Crypto.RIPEMD160", Func: crypto.RipeMD160, Price: 1000000},
|
2020-06-19 09:22:18 +00:00
|
|
|
{Name: "Neo.Native.Deploy", Func: native.Deploy, Price: 0,
|
2020-06-10 15:07:21 +00:00
|
|
|
AllowedTriggers: trigger.Application, RequiredFlags: smartcontract.AllowModifyStates},
|
2019-12-18 16:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// initIDinInteropsSlice initializes IDs from names in one given
|
2020-04-08 10:35:39 +00:00
|
|
|
// Function slice and then sorts it.
|
|
|
|
func initIDinInteropsSlice(iops []interop.Function) {
|
2019-12-18 16:49:56 +00:00
|
|
|
for i := range iops {
|
2020-04-15 14:13:50 +00:00
|
|
|
iops[i].ID = emit.InteropNameToID([]byte(iops[i].Name))
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
2019-12-18 16:49:56 +00:00
|
|
|
sort.Slice(iops, func(i, j int) bool {
|
|
|
|
return iops[i].ID < iops[j].ID
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// init initializes IDs in the global interop slices.
|
|
|
|
func init() {
|
|
|
|
initIDinInteropsSlice(systemInterops)
|
|
|
|
initIDinInteropsSlice(neoInterops)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|