2019-10-11 14:00:11 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2020-04-08 10:56:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/blockchainer"
|
2020-04-07 09:41:12 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/dao"
|
2020-04-08 10:35:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2020-05-07 11:11:59 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2019-12-30 07:43:05 +00:00
|
|
|
"go.uber.org/zap"
|
2019-10-11 14:00:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// MaxStorageKeyLen is the maximum length of a key for storage items.
|
|
|
|
MaxStorageKeyLen = 1024
|
|
|
|
)
|
|
|
|
|
|
|
|
// StorageContext contains storing script hash and read/write flag, it's used as
|
|
|
|
// a context for storage manipulation functions.
|
|
|
|
type StorageContext struct {
|
|
|
|
ScriptHash util.Uint160
|
|
|
|
ReadOnly bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// getBlockHashFromElement converts given vm.Element to block hash using given
|
|
|
|
// Blockchainer if needed. Interop functions accept both block numbers and
|
|
|
|
// block hashes as parameters, thus this function is needed.
|
2020-04-08 10:56:04 +00:00
|
|
|
func getBlockHashFromElement(bc blockchainer.Blockchainer, element *vm.Element) (util.Uint256, error) {
|
2019-10-11 14:00:11 +00:00
|
|
|
var hash util.Uint256
|
|
|
|
hashbytes := element.Bytes()
|
|
|
|
if len(hashbytes) <= 5 {
|
|
|
|
hashint := element.BigInt().Int64()
|
|
|
|
if hashint < 0 || hashint > math.MaxUint32 {
|
|
|
|
return hash, errors.New("bad block index")
|
|
|
|
}
|
|
|
|
hash = bc.GetHeaderHash(int(hashint))
|
|
|
|
} else {
|
2020-03-05 16:44:09 +00:00
|
|
|
return util.Uint256DecodeBytesBE(hashbytes)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
return hash, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// bcGetBlock returns current block.
|
2020-04-08 10:35:39 +00:00
|
|
|
func bcGetBlock(ic *interop.Context, v *vm.VM) error {
|
|
|
|
hash, err := getBlockHashFromElement(ic.Chain, v.Estack().Pop())
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-08 10:35:39 +00:00
|
|
|
block, err := ic.Chain.GetBlock(hash)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
v.Estack().PushVal([]byte{})
|
|
|
|
} else {
|
|
|
|
v.Estack().PushVal(vm.NewInteropItem(block))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// bcGetContract returns contract.
|
2020-04-08 10:35:39 +00:00
|
|
|
func bcGetContract(ic *interop.Context, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
hashbytes := v.Estack().Pop().Bytes()
|
2019-11-27 09:20:31 +00:00
|
|
|
hash, err := util.Uint160DecodeBytesBE(hashbytes)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-08 10:35:39 +00:00
|
|
|
cs, err := ic.DAO.GetContractState(hash)
|
2019-12-13 14:10:51 +00:00
|
|
|
if err != nil {
|
2019-10-11 14:00:11 +00:00
|
|
|
v.Estack().PushVal([]byte{})
|
|
|
|
} else {
|
|
|
|
v.Estack().PushVal(vm.NewInteropItem(cs))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// bcGetHeader returns block header.
|
2020-04-08 10:35:39 +00:00
|
|
|
func bcGetHeader(ic *interop.Context, v *vm.VM) error {
|
|
|
|
hash, err := getBlockHashFromElement(ic.Chain, v.Estack().Pop())
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-08 10:35:39 +00:00
|
|
|
header, err := ic.Chain.GetHeader(hash)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
v.Estack().PushVal([]byte{})
|
|
|
|
} else {
|
|
|
|
v.Estack().PushVal(vm.NewInteropItem(header))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// bcGetHeight returns blockchain height.
|
2020-04-08 10:35:39 +00:00
|
|
|
func bcGetHeight(ic *interop.Context, v *vm.VM) error {
|
|
|
|
v.Estack().PushVal(ic.Chain.BlockHeight())
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getTransactionAndHeight gets parameter from the vm evaluation stack and
|
|
|
|
// returns transaction and its height if it's present in the blockchain.
|
2020-04-07 09:41:12 +00:00
|
|
|
func getTransactionAndHeight(cd *dao.Cached, v *vm.VM) (*transaction.Transaction, uint32, error) {
|
2019-10-11 14:00:11 +00:00
|
|
|
hashbytes := v.Estack().Pop().Bytes()
|
2020-03-05 16:44:09 +00:00
|
|
|
hash, err := util.Uint256DecodeBytesBE(hashbytes)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
2019-12-13 14:10:51 +00:00
|
|
|
return cd.GetTransaction(hash)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// bcGetTransaction returns transaction.
|
2020-04-08 10:35:39 +00:00
|
|
|
func bcGetTransaction(ic *interop.Context, v *vm.VM) error {
|
|
|
|
tx, _, err := getTransactionAndHeight(ic.DAO, v)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(vm.NewInteropItem(tx))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// bcGetTransactionHeight returns transaction height.
|
2020-04-08 10:35:39 +00:00
|
|
|
func bcGetTransactionHeight(ic *interop.Context, v *vm.VM) error {
|
|
|
|
_, h, err := getTransactionAndHeight(ic.DAO, v)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(h)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// popHeaderFromVM returns pointer to Header or error. It's main feature is
|
|
|
|
// proper treatment of Block structure, because C# code implicitly assumes
|
|
|
|
// that header APIs can also operate on blocks.
|
2020-01-14 12:32:07 +00:00
|
|
|
func popHeaderFromVM(v *vm.VM) (*block.Header, error) {
|
2019-10-11 14:00:11 +00:00
|
|
|
iface := v.Estack().Pop().Value()
|
2020-01-14 12:32:07 +00:00
|
|
|
header, ok := iface.(*block.Header)
|
2019-10-11 14:00:11 +00:00
|
|
|
if !ok {
|
2020-01-14 12:32:07 +00:00
|
|
|
block, ok := iface.(*block.Block)
|
2019-10-11 14:00:11 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("value is not a header or block")
|
|
|
|
}
|
|
|
|
return block.Header(), nil
|
|
|
|
}
|
|
|
|
return header, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// headerGetIndex returns block index from the header.
|
2020-04-08 10:35:39 +00:00
|
|
|
func headerGetIndex(ic *interop.Context, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
header, err := popHeaderFromVM(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(header.Index)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// headerGetHash returns header hash of the passed header.
|
2020-04-08 10:35:39 +00:00
|
|
|
func headerGetHash(ic *interop.Context, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
header, err := popHeaderFromVM(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-05 16:44:09 +00:00
|
|
|
v.Estack().PushVal(header.Hash().BytesBE())
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// headerGetPrevHash returns previous header hash of the passed header.
|
2020-04-08 10:35:39 +00:00
|
|
|
func headerGetPrevHash(ic *interop.Context, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
header, err := popHeaderFromVM(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-05 16:44:09 +00:00
|
|
|
v.Estack().PushVal(header.PrevHash.BytesBE())
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// headerGetTimestamp returns timestamp of the passed header.
|
2020-04-08 10:35:39 +00:00
|
|
|
func headerGetTimestamp(ic *interop.Context, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
header, err := popHeaderFromVM(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(header.Timestamp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// blockGetTransactionCount returns transactions count in the given block.
|
2020-04-08 10:35:39 +00:00
|
|
|
func blockGetTransactionCount(ic *interop.Context, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
blockInterface := v.Estack().Pop().Value()
|
2020-01-14 12:32:07 +00:00
|
|
|
block, ok := blockInterface.(*block.Block)
|
2019-10-11 14:00:11 +00:00
|
|
|
if !ok {
|
|
|
|
return errors.New("value is not a block")
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(len(block.Transactions))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// blockGetTransactions returns transactions from the given block.
|
2020-04-08 10:35:39 +00:00
|
|
|
func blockGetTransactions(ic *interop.Context, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
blockInterface := v.Estack().Pop().Value()
|
2020-01-14 12:32:07 +00:00
|
|
|
block, ok := blockInterface.(*block.Block)
|
2019-10-11 14:00:11 +00:00
|
|
|
if !ok {
|
|
|
|
return errors.New("value is not a block")
|
|
|
|
}
|
|
|
|
if len(block.Transactions) > vm.MaxArraySize {
|
|
|
|
return errors.New("too many transactions")
|
|
|
|
}
|
|
|
|
txes := make([]vm.StackItem, 0, len(block.Transactions))
|
|
|
|
for _, tx := range block.Transactions {
|
|
|
|
txes = append(txes, vm.NewInteropItem(tx))
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(txes)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// blockGetTransaction returns transaction with the given number from the given
|
|
|
|
// block.
|
2020-04-08 10:35:39 +00:00
|
|
|
func blockGetTransaction(ic *interop.Context, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
blockInterface := v.Estack().Pop().Value()
|
2020-01-14 12:32:07 +00:00
|
|
|
block, ok := blockInterface.(*block.Block)
|
2019-10-11 14:00:11 +00:00
|
|
|
if !ok {
|
|
|
|
return errors.New("value is not a block")
|
|
|
|
}
|
|
|
|
index := v.Estack().Pop().BigInt().Int64()
|
|
|
|
if index < 0 || index >= int64(len(block.Transactions)) {
|
|
|
|
return errors.New("wrong transaction index")
|
|
|
|
}
|
|
|
|
tx := block.Transactions[index]
|
|
|
|
v.Estack().PushVal(vm.NewInteropItem(tx))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// txGetHash returns transaction's hash.
|
2020-04-08 10:35:39 +00:00
|
|
|
func txGetHash(ic *interop.Context, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
txInterface := v.Estack().Pop().Value()
|
|
|
|
tx, ok := txInterface.(*transaction.Transaction)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("value is not a transaction")
|
|
|
|
}
|
2020-03-05 16:44:09 +00:00
|
|
|
v.Estack().PushVal(tx.Hash().BytesBE())
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// engineGetScriptContainer returns transaction that contains the script being
|
|
|
|
// run.
|
2020-04-08 10:35:39 +00:00
|
|
|
func engineGetScriptContainer(ic *interop.Context, v *vm.VM) error {
|
2020-04-13 13:31:04 +00:00
|
|
|
v.Estack().PushVal(vm.NewInteropItem(ic.Container))
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// engineGetExecutingScriptHash returns executing script hash.
|
2020-04-08 10:35:39 +00:00
|
|
|
func engineGetExecutingScriptHash(ic *interop.Context, v *vm.VM) error {
|
2020-04-13 12:37:44 +00:00
|
|
|
return v.PushContextScriptHash(0)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// engineGetCallingScriptHash returns calling script hash.
|
2020-04-08 10:35:39 +00:00
|
|
|
func engineGetCallingScriptHash(ic *interop.Context, v *vm.VM) error {
|
2020-04-13 12:37:44 +00:00
|
|
|
return v.PushContextScriptHash(1)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// engineGetEntryScriptHash returns entry script hash.
|
2020-04-08 10:35:39 +00:00
|
|
|
func engineGetEntryScriptHash(ic *interop.Context, v *vm.VM) error {
|
2020-04-13 12:37:44 +00:00
|
|
|
return v.PushContextScriptHash(v.Istack().Len() - 1)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// runtimePlatform returns the name of the platform.
|
2020-04-08 10:35:39 +00:00
|
|
|
func runtimePlatform(ic *interop.Context, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
v.Estack().PushVal([]byte("NEO"))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// runtimeGetTrigger returns the script trigger.
|
2020-04-08 10:35:39 +00:00
|
|
|
func runtimeGetTrigger(ic *interop.Context, v *vm.VM) error {
|
|
|
|
v.Estack().PushVal(byte(ic.Trigger))
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// runtimeNotify should pass stack item to the notify plugin to handle it, but
|
|
|
|
// in neo-go the only meaningful thing to do here is to log.
|
2020-04-08 10:35:39 +00:00
|
|
|
func runtimeNotify(ic *interop.Context, v *vm.VM) error {
|
2019-11-13 13:55:20 +00:00
|
|
|
// It can be just about anything.
|
|
|
|
e := v.Estack().Pop()
|
2020-02-07 09:17:39 +00:00
|
|
|
item := e.Item()
|
|
|
|
// But it has to be serializable, otherwise we either have some broken
|
|
|
|
// (recursive) structure inside or an interop item that can't be used
|
|
|
|
// outside of the interop subsystem anyway. I'd probably fail transactions
|
|
|
|
// that emit such broken notifications, but that might break compatibility
|
|
|
|
// with testnet/mainnet, so we're replacing these with error messages.
|
|
|
|
_, err := vm.SerializeItem(item)
|
|
|
|
if err != nil {
|
|
|
|
item = vm.NewByteArrayItem([]byte(fmt.Sprintf("bad notification: %v", err)))
|
|
|
|
}
|
2020-05-04 08:41:41 +00:00
|
|
|
ne := state.NotificationEvent{ScriptHash: v.GetCurrentScriptHash(), Item: item}
|
2020-04-08 10:35:39 +00:00
|
|
|
ic.Notifications = append(ic.Notifications, ne)
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// runtimeLog logs the message passed.
|
2020-04-08 10:35:39 +00:00
|
|
|
func runtimeLog(ic *interop.Context, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
msg := fmt.Sprintf("%q", v.Estack().Pop().Bytes())
|
2020-04-08 10:35:39 +00:00
|
|
|
ic.Log.Info("runtime log",
|
2020-05-04 08:41:41 +00:00
|
|
|
zap.Stringer("script", v.GetCurrentScriptHash()),
|
2019-12-30 07:43:05 +00:00
|
|
|
zap.String("logs", msg))
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// runtimeGetTime returns timestamp of the block being verified, or the latest
|
2020-04-08 10:35:39 +00:00
|
|
|
// one in the blockchain if no block is given to Context.
|
|
|
|
func runtimeGetTime(ic *interop.Context, v *vm.VM) error {
|
2020-01-14 12:32:07 +00:00
|
|
|
var header *block.Header
|
2020-04-08 10:35:39 +00:00
|
|
|
if ic.Block == nil {
|
2019-10-11 14:00:11 +00:00
|
|
|
var err error
|
2020-04-08 10:35:39 +00:00
|
|
|
header, err = ic.Chain.GetHeader(ic.Chain.CurrentBlockHash())
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2020-04-08 10:35:39 +00:00
|
|
|
header = ic.Block.Header()
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
v.Estack().PushVal(header.Timestamp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-08 10:35:39 +00:00
|
|
|
func checkStorageContext(ic *interop.Context, stc *StorageContext) error {
|
|
|
|
contract, err := ic.DAO.GetContractState(stc.ScriptHash)
|
2019-12-13 14:10:51 +00:00
|
|
|
if err != nil {
|
2019-10-11 14:00:11 +00:00
|
|
|
return errors.New("no contract found")
|
|
|
|
}
|
|
|
|
if !contract.HasStorage() {
|
2019-11-14 13:09:24 +00:00
|
|
|
return fmt.Errorf("contract %s can't use storage", stc.ScriptHash)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// storageDelete deletes stored key-value pair.
|
2020-04-08 10:35:39 +00:00
|
|
|
func storageDelete(ic *interop.Context, v *vm.VM) error {
|
|
|
|
if ic.Trigger != trigger.Application && ic.Trigger != trigger.ApplicationR {
|
2019-10-11 14:00:11 +00:00
|
|
|
return errors.New("can't delete when the trigger is not application")
|
|
|
|
}
|
|
|
|
stcInterface := v.Estack().Pop().Value()
|
|
|
|
stc, ok := stcInterface.(*StorageContext)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not a StorageContext", stcInterface)
|
|
|
|
}
|
|
|
|
if stc.ReadOnly {
|
|
|
|
return errors.New("StorageContext is read only")
|
|
|
|
}
|
2020-04-08 10:29:15 +00:00
|
|
|
err := checkStorageContext(ic, stc)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key := v.Estack().Pop().Bytes()
|
2020-04-08 10:35:39 +00:00
|
|
|
si := ic.DAO.GetStorageItem(stc.ScriptHash, key)
|
2019-10-11 14:00:11 +00:00
|
|
|
if si != nil && si.IsConst {
|
|
|
|
return errors.New("storage item is constant")
|
|
|
|
}
|
2020-04-08 10:35:39 +00:00
|
|
|
return ic.DAO.DeleteStorageItem(stc.ScriptHash, key)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// storageGet returns stored key-value pair.
|
2020-04-08 10:35:39 +00:00
|
|
|
func storageGet(ic *interop.Context, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
stcInterface := v.Estack().Pop().Value()
|
|
|
|
stc, ok := stcInterface.(*StorageContext)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not a StorageContext", stcInterface)
|
|
|
|
}
|
2020-04-08 10:29:15 +00:00
|
|
|
err := checkStorageContext(ic, stc)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key := v.Estack().Pop().Bytes()
|
2020-04-08 10:35:39 +00:00
|
|
|
si := ic.DAO.GetStorageItem(stc.ScriptHash, key)
|
2019-10-11 14:00:11 +00:00
|
|
|
if si != nil && si.Value != nil {
|
|
|
|
v.Estack().PushVal(si.Value)
|
|
|
|
} else {
|
|
|
|
v.Estack().PushVal([]byte{})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// storageGetContext returns storage context (scripthash).
|
2020-04-08 10:35:39 +00:00
|
|
|
func storageGetContext(ic *interop.Context, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
sc := &StorageContext{
|
2020-05-04 08:41:41 +00:00
|
|
|
ScriptHash: v.GetCurrentScriptHash(),
|
2019-10-11 14:00:11 +00:00
|
|
|
ReadOnly: false,
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(vm.NewInteropItem(sc))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// storageGetReadOnlyContext returns read-only context (scripthash).
|
2020-04-08 10:35:39 +00:00
|
|
|
func storageGetReadOnlyContext(ic *interop.Context, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
sc := &StorageContext{
|
2020-05-04 08:41:41 +00:00
|
|
|
ScriptHash: v.GetCurrentScriptHash(),
|
2019-10-11 14:00:11 +00:00
|
|
|
ReadOnly: true,
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(vm.NewInteropItem(sc))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-08 10:35:39 +00:00
|
|
|
func putWithContextAndFlags(ic *interop.Context, stc *StorageContext, key []byte, value []byte, isConst bool) error {
|
|
|
|
if ic.Trigger != trigger.Application && ic.Trigger != trigger.ApplicationR {
|
2019-10-11 14:00:11 +00:00
|
|
|
return errors.New("can't delete when the trigger is not application")
|
|
|
|
}
|
|
|
|
if len(key) > MaxStorageKeyLen {
|
|
|
|
return errors.New("key is too big")
|
|
|
|
}
|
|
|
|
if stc.ReadOnly {
|
|
|
|
return errors.New("StorageContext is read only")
|
|
|
|
}
|
2020-04-08 10:29:15 +00:00
|
|
|
err := checkStorageContext(ic, stc)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-08 10:35:39 +00:00
|
|
|
si := ic.DAO.GetStorageItem(stc.ScriptHash, key)
|
2019-10-11 14:00:11 +00:00
|
|
|
if si == nil {
|
2019-11-28 16:06:09 +00:00
|
|
|
si = &state.StorageItem{}
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
if si.IsConst {
|
|
|
|
return errors.New("storage item exists and is read-only")
|
|
|
|
}
|
|
|
|
si.Value = value
|
|
|
|
si.IsConst = isConst
|
2020-04-08 10:35:39 +00:00
|
|
|
return ic.DAO.PutStorageItem(stc.ScriptHash, key, si)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// storagePutInternal is a unified implementation of storagePut and storagePutEx.
|
2020-04-08 10:35:39 +00:00
|
|
|
func storagePutInternal(ic *interop.Context, v *vm.VM, getFlag bool) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
stcInterface := v.Estack().Pop().Value()
|
|
|
|
stc, ok := stcInterface.(*StorageContext)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not a StorageContext", stcInterface)
|
|
|
|
}
|
|
|
|
key := v.Estack().Pop().Bytes()
|
|
|
|
value := v.Estack().Pop().Bytes()
|
|
|
|
var flag int
|
|
|
|
if getFlag {
|
|
|
|
flag = int(v.Estack().Pop().BigInt().Int64())
|
|
|
|
}
|
2020-04-08 10:29:15 +00:00
|
|
|
return putWithContextAndFlags(ic, stc, key, value, flag == 1)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// storagePut puts key-value pair into the storage.
|
2020-04-08 10:35:39 +00:00
|
|
|
func storagePut(ic *interop.Context, v *vm.VM) error {
|
2020-04-08 10:29:15 +00:00
|
|
|
return storagePutInternal(ic, v, false)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// storagePutEx puts key-value pair with given flags into the storage.
|
2020-04-08 10:35:39 +00:00
|
|
|
func storagePutEx(ic *interop.Context, v *vm.VM) error {
|
2020-04-08 10:29:15 +00:00
|
|
|
return storagePutInternal(ic, v, true)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// storageContextAsReadOnly sets given context to read-only mode.
|
2020-04-08 10:35:39 +00:00
|
|
|
func storageContextAsReadOnly(ic *interop.Context, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
stcInterface := v.Estack().Pop().Value()
|
|
|
|
stc, ok := stcInterface.(*StorageContext)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not a StorageContext", stcInterface)
|
|
|
|
}
|
|
|
|
if !stc.ReadOnly {
|
|
|
|
stx := &StorageContext{
|
|
|
|
ScriptHash: stc.ScriptHash,
|
|
|
|
ReadOnly: true,
|
|
|
|
}
|
|
|
|
stc = stx
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(vm.NewInteropItem(stc))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-07 11:11:59 +00:00
|
|
|
// contractCall calls a contract.
|
|
|
|
func contractCall(ic *interop.Context, v *vm.VM) error {
|
|
|
|
h := v.Estack().Pop().Bytes()
|
|
|
|
method := v.Estack().Pop().Item()
|
|
|
|
args := v.Estack().Pop().Item()
|
|
|
|
return contractCallExInternal(ic, v, h, method, args, smartcontract.All)
|
|
|
|
}
|
|
|
|
|
|
|
|
// contractCallEx calls a contract with flags.
|
|
|
|
func contractCallEx(ic *interop.Context, v *vm.VM) error {
|
|
|
|
h := v.Estack().Pop().Bytes()
|
|
|
|
method := v.Estack().Pop().Item()
|
|
|
|
args := v.Estack().Pop().Item()
|
|
|
|
flags := smartcontract.CallFlag(int32(v.Estack().Pop().BigInt().Int64()))
|
|
|
|
return contractCallExInternal(ic, v, h, method, args, flags)
|
|
|
|
}
|
|
|
|
|
|
|
|
func contractCallExInternal(ic *interop.Context, v *vm.VM, h []byte, method vm.StackItem, args vm.StackItem, _ smartcontract.CallFlag) error {
|
|
|
|
u, err := util.Uint160DecodeBytesBE(h)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("invalid contract hash")
|
|
|
|
}
|
|
|
|
script, _ := ic.GetContract(u)
|
|
|
|
if script == nil {
|
|
|
|
return errors.New("contract not found")
|
|
|
|
}
|
|
|
|
// TODO perform flags checking after #923
|
|
|
|
v.LoadScript(script)
|
|
|
|
v.Estack().PushVal(args)
|
|
|
|
v.Estack().PushVal(method)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:00:11 +00:00
|
|
|
// contractDestroy destroys a contract.
|
2020-04-08 10:35:39 +00:00
|
|
|
func contractDestroy(ic *interop.Context, v *vm.VM) error {
|
|
|
|
if ic.Trigger != trigger.Application {
|
2019-10-11 14:00:11 +00:00
|
|
|
return errors.New("can't destroy contract when not triggered by application")
|
|
|
|
}
|
2020-05-04 08:41:41 +00:00
|
|
|
hash := v.GetCurrentScriptHash()
|
2020-04-08 10:35:39 +00:00
|
|
|
cs, err := ic.DAO.GetContractState(hash)
|
2019-12-13 14:10:51 +00:00
|
|
|
if err != nil {
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-04-08 10:35:39 +00:00
|
|
|
err = ic.DAO.DeleteContractState(hash)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if cs.HasStorage() {
|
2020-04-08 10:35:39 +00:00
|
|
|
siMap, err := ic.DAO.GetStorageItems(hash)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for k := range siMap {
|
2020-04-08 10:35:39 +00:00
|
|
|
_ = ic.DAO.DeleteStorageItem(hash, []byte(k))
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// contractGetStorageContext retrieves StorageContext of a contract.
|
2020-04-08 10:35:39 +00:00
|
|
|
func contractGetStorageContext(ic *interop.Context, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
csInterface := v.Estack().Pop().Value()
|
2019-11-28 16:06:09 +00:00
|
|
|
cs, ok := csInterface.(*state.Contract)
|
2019-10-11 14:00:11 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not a contract state", cs)
|
|
|
|
}
|
2020-05-17 20:58:23 +00:00
|
|
|
_, err := ic.DAO.GetContractState(cs.ScriptHash())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("non-existent contract")
|
|
|
|
}
|
|
|
|
_, err = ic.LowerDAO.GetContractState(cs.ScriptHash())
|
|
|
|
if err == nil {
|
2019-10-11 14:00:11 +00:00
|
|
|
return fmt.Errorf("contract was not created in this transaction")
|
|
|
|
}
|
|
|
|
stc := &StorageContext{
|
|
|
|
ScriptHash: cs.ScriptHash(),
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(vm.NewInteropItem(stc))
|
|
|
|
return nil
|
|
|
|
}
|