github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/core/interops.go (about)

     1  package core
     2  
     3  /*
     4    Interops are designed to run under VM's execute() panic protection, so it's OK
     5    for them to do things like
     6            smth := v.Estack().Pop().Bytes()
     7    even though technically Pop() can return a nil pointer.
     8  */
     9  
    10  import (
    11  	"github.com/nspcc-dev/neo-go/pkg/core/fee"
    12  	"github.com/nspcc-dev/neo-go/pkg/core/interop"
    13  	"github.com/nspcc-dev/neo-go/pkg/core/interop/contract"
    14  	"github.com/nspcc-dev/neo-go/pkg/core/interop/crypto"
    15  	"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
    16  	"github.com/nspcc-dev/neo-go/pkg/core/interop/iterator"
    17  	"github.com/nspcc-dev/neo-go/pkg/core/interop/runtime"
    18  	"github.com/nspcc-dev/neo-go/pkg/core/interop/storage"
    19  	"github.com/nspcc-dev/neo-go/pkg/core/native"
    20  	"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
    21  	"github.com/nspcc-dev/neo-go/pkg/vm"
    22  )
    23  
    24  // SpawnVM returns a VM with script getter and interop functions set
    25  // up for current blockchain.
    26  func SpawnVM(ic *interop.Context) *vm.VM {
    27  	vm := ic.SpawnVM()
    28  	ic.Functions = systemInterops
    29  	return vm
    30  }
    31  
    32  // All lists are sorted, keep 'em this way, please.
    33  var systemInterops = []interop.Function{
    34  	{Name: interopnames.SystemContractCall, Func: contract.Call, Price: 1 << 15,
    35  		RequiredFlags: callflag.ReadStates | callflag.AllowCall, ParamCount: 4},
    36  	{Name: interopnames.SystemContractCallNative, Func: native.Call, Price: 0, ParamCount: 1},
    37  	{Name: interopnames.SystemContractCreateMultisigAccount, Func: contract.CreateMultisigAccount, Price: 0, ParamCount: 2},
    38  	{Name: interopnames.SystemContractCreateStandardAccount, Func: contract.CreateStandardAccount, Price: 0, ParamCount: 1},
    39  	{Name: interopnames.SystemContractGetCallFlags, Func: contract.GetCallFlags, Price: 1 << 10},
    40  	{Name: interopnames.SystemContractNativeOnPersist, Func: native.OnPersist, Price: 0, RequiredFlags: callflag.States},
    41  	{Name: interopnames.SystemContractNativePostPersist, Func: native.PostPersist, Price: 0, RequiredFlags: callflag.States},
    42  	{Name: interopnames.SystemCryptoCheckMultisig, Func: crypto.ECDSASecp256r1CheckMultisig, Price: 0, ParamCount: 2},
    43  	{Name: interopnames.SystemCryptoCheckSig, Func: crypto.ECDSASecp256r1CheckSig, Price: fee.ECDSAVerifyPrice, ParamCount: 2},
    44  	{Name: interopnames.SystemIteratorNext, Func: iterator.Next, Price: 1 << 15, ParamCount: 1},
    45  	{Name: interopnames.SystemIteratorValue, Func: iterator.Value, Price: 1 << 4, ParamCount: 1},
    46  	{Name: interopnames.SystemRuntimeBurnGas, Func: runtime.BurnGas, Price: 1 << 4, ParamCount: 1},
    47  	{Name: interopnames.SystemRuntimeCheckWitness, Func: runtime.CheckWitness, Price: 1 << 10,
    48  		RequiredFlags: callflag.NoneFlag, ParamCount: 1},
    49  	{Name: interopnames.SystemRuntimeCurrentSigners, Func: runtime.CurrentSigners, Price: 1 << 4,
    50  		RequiredFlags: callflag.NoneFlag},
    51  	{Name: interopnames.SystemRuntimeGasLeft, Func: runtime.GasLeft, Price: 1 << 4},
    52  	{Name: interopnames.SystemRuntimeGetAddressVersion, Func: runtime.GetAddressVersion, Price: 1 << 3},
    53  	{Name: interopnames.SystemRuntimeGetCallingScriptHash, Func: runtime.GetCallingScriptHash, Price: 1 << 4},
    54  	{Name: interopnames.SystemRuntimeGetEntryScriptHash, Func: runtime.GetEntryScriptHash, Price: 1 << 4},
    55  	{Name: interopnames.SystemRuntimeGetExecutingScriptHash, Func: runtime.GetExecutingScriptHash, Price: 1 << 4},
    56  	{Name: interopnames.SystemRuntimeGetInvocationCounter, Func: runtime.GetInvocationCounter, Price: 1 << 4},
    57  	{Name: interopnames.SystemRuntimeGetNetwork, Func: runtime.GetNetwork, Price: 1 << 3},
    58  	{Name: interopnames.SystemRuntimeGetNotifications, Func: runtime.GetNotifications, Price: 1 << 12, ParamCount: 1},
    59  	{Name: interopnames.SystemRuntimeGetRandom, Func: runtime.GetRandom, Price: 0},
    60  	{Name: interopnames.SystemRuntimeGetScriptContainer, Func: runtime.GetScriptContainer, Price: 1 << 3},
    61  	{Name: interopnames.SystemRuntimeGetTime, Func: runtime.GetTime, Price: 1 << 3, RequiredFlags: callflag.ReadStates},
    62  	{Name: interopnames.SystemRuntimeGetTrigger, Func: runtime.GetTrigger, Price: 1 << 3},
    63  	{Name: interopnames.SystemRuntimeLoadScript, Func: runtime.LoadScript, Price: 1 << 15, RequiredFlags: callflag.AllowCall,
    64  		ParamCount: 3},
    65  	{Name: interopnames.SystemRuntimeLog, Func: runtime.Log, Price: 1 << 15, RequiredFlags: callflag.AllowNotify,
    66  		ParamCount: 1},
    67  	{Name: interopnames.SystemRuntimeNotify, Func: runtime.Notify, Price: 1 << 15, RequiredFlags: callflag.AllowNotify,
    68  		ParamCount: 2},
    69  	{Name: interopnames.SystemRuntimePlatform, Func: runtime.Platform, Price: 1 << 3},
    70  	{Name: interopnames.SystemStorageDelete, Func: storage.Delete, Price: 1 << 15,
    71  		RequiredFlags: callflag.WriteStates, ParamCount: 2},
    72  	{Name: interopnames.SystemStorageFind, Func: storage.Find, Price: 1 << 15, RequiredFlags: callflag.ReadStates,
    73  		ParamCount: 3},
    74  	{Name: interopnames.SystemStorageGet, Func: storage.Get, Price: 1 << 15, RequiredFlags: callflag.ReadStates,
    75  		ParamCount: 2},
    76  	{Name: interopnames.SystemStorageGetContext, Func: storage.GetContext, Price: 1 << 4,
    77  		RequiredFlags: callflag.ReadStates},
    78  	{Name: interopnames.SystemStorageGetReadOnlyContext, Func: storage.GetReadOnlyContext, Price: 1 << 4,
    79  		RequiredFlags: callflag.ReadStates},
    80  	{Name: interopnames.SystemStoragePut, Func: storage.Put, Price: 1 << 15, RequiredFlags: callflag.WriteStates,
    81  		ParamCount: 3},
    82  	{Name: interopnames.SystemStorageAsReadOnly, Func: storage.ContextAsReadOnly, Price: 1 << 4,
    83  		RequiredFlags: callflag.ReadStates, ParamCount: 1},
    84  }
    85  
    86  // init initializes IDs in the global interop slices.
    87  func init() {
    88  	for i := range systemInterops {
    89  		systemInterops[i].ID = interopnames.ToID([]byte(systemInterops[i].Name))
    90  	}
    91  	interop.Sort(systemInterops)
    92  }