2019-10-11 14:00:11 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2020-07-13 09:59:41 +00:00
|
|
|
"crypto/elliptic"
|
2019-10-11 14:00:11 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
2020-06-08 15:36:19 +00:00
|
|
|
"math/big"
|
2019-10-11 14:00:11 +00:00
|
|
|
|
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-06-16 08:25:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
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/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
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
|
2020-06-08 15:36:19 +00:00
|
|
|
// MaxTraceableBlocks is the maximum number of blocks before current chain
|
|
|
|
// height we're able to give information about.
|
|
|
|
MaxTraceableBlocks = transaction.MaxValidUntilBlockIncrement
|
2019-10-11 14:00:11 +00:00
|
|
|
)
|
|
|
|
|
2020-06-18 10:50:30 +00:00
|
|
|
// StorageContext contains storing id and read/write flag, it's used as
|
2019-10-11 14:00:11 +00:00
|
|
|
// a context for storage manipulation functions.
|
|
|
|
type StorageContext struct {
|
2020-06-18 10:50:30 +00:00
|
|
|
ID int32
|
|
|
|
ReadOnly bool
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-06-09 10:18:32 +00:00
|
|
|
// blockToStackItem converts block.Block to stackitem.Item
|
|
|
|
func blockToStackItem(b *block.Block) stackitem.Item {
|
|
|
|
return stackitem.NewArray([]stackitem.Item{
|
|
|
|
stackitem.NewByteArray(b.Hash().BytesBE()),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(b.Version))),
|
|
|
|
stackitem.NewByteArray(b.PrevHash.BytesBE()),
|
|
|
|
stackitem.NewByteArray(b.MerkleRoot.BytesBE()),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(b.Timestamp))),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(b.Index))),
|
|
|
|
stackitem.NewByteArray(b.NextConsensus.BytesBE()),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(len(b.Transactions)))),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:00:11 +00:00
|
|
|
// 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)
|
2020-06-09 10:18:32 +00:00
|
|
|
if err != nil || !isTraceableBlock(ic, block.Index) {
|
|
|
|
v.Estack().PushVal(stackitem.Null{})
|
2019-10-11 14:00:11 +00:00
|
|
|
} else {
|
2020-06-09 10:18:32 +00:00
|
|
|
v.Estack().PushVal(blockToStackItem(block))
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-15 09:53:09 +00:00
|
|
|
// contractToStackItem converts state.Contract to stackitem.Item
|
|
|
|
func contractToStackItem(cs *state.Contract) (stackitem.Item, error) {
|
|
|
|
manifest, err := cs.Manifest.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return stackitem.NewArray([]stackitem.Item{
|
|
|
|
stackitem.NewByteArray(cs.Script),
|
|
|
|
stackitem.NewByteArray(manifest),
|
|
|
|
stackitem.NewBool(cs.HasStorage()),
|
|
|
|
stackitem.NewBool(cs.IsPayable()),
|
|
|
|
}), nil
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:00:11 +00:00
|
|
|
// 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 {
|
2020-07-15 09:53:09 +00:00
|
|
|
v.Estack().PushVal(stackitem.Null{})
|
2019-10-11 14:00:11 +00:00
|
|
|
} else {
|
2020-07-15 09:53:09 +00:00
|
|
|
item, err := contractToStackItem(cs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(item)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-06-08 15:36:19 +00:00
|
|
|
// isTraceableBlock defines whether we're able to give information about
|
|
|
|
// the block with index specified.
|
|
|
|
func isTraceableBlock(ic *interop.Context, index uint32) bool {
|
|
|
|
height := ic.Chain.BlockHeight()
|
|
|
|
return index <= height && index+MaxTraceableBlocks > height
|
|
|
|
}
|
|
|
|
|
|
|
|
// transactionToStackItem converts transaction.Transaction to stackitem.Item
|
|
|
|
func transactionToStackItem(t *transaction.Transaction) stackitem.Item {
|
|
|
|
return stackitem.NewArray([]stackitem.Item{
|
|
|
|
stackitem.NewByteArray(t.Hash().BytesBE()),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(t.Version))),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(t.Nonce))),
|
|
|
|
stackitem.NewByteArray(t.Sender.BytesBE()),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(t.SystemFee))),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(t.NetworkFee))),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(t.ValidUntilBlock))),
|
|
|
|
stackitem.NewByteArray(t.Script),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-06-08 15:36:19 +00:00
|
|
|
tx, h, err := getTransactionAndHeight(ic.DAO, v)
|
|
|
|
if err != nil || !isTraceableBlock(ic, h) {
|
|
|
|
v.Estack().PushVal(stackitem.Null{})
|
|
|
|
return nil
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
2020-06-08 15:36:19 +00:00
|
|
|
v.Estack().PushVal(transactionToStackItem(tx))
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-09 09:18:08 +00:00
|
|
|
// bcGetTransactionFromBlock returns transaction with the given index from the
|
|
|
|
// block with height or hash specified.
|
|
|
|
func bcGetTransactionFromBlock(ic *interop.Context, v *vm.VM) error {
|
|
|
|
hash, err := getBlockHashFromElement(ic.Chain, v.Estack().Pop())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-15 10:05:29 +00:00
|
|
|
index := v.Estack().Pop().BigInt().Int64()
|
2020-06-09 09:18:08 +00:00
|
|
|
block, err := ic.DAO.GetBlock(hash)
|
|
|
|
if err != nil || !isTraceableBlock(ic, block.Index) {
|
|
|
|
v.Estack().PushVal(stackitem.Null{})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if index < 0 || index >= int64(len(block.Transactions)) {
|
|
|
|
return errors.New("wrong transaction index")
|
|
|
|
}
|
|
|
|
tx := block.Transactions[index]
|
|
|
|
v.Estack().PushVal(tx.Hash().BytesBE())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:00:11 +00:00
|
|
|
// 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)
|
2020-06-08 15:40:01 +00:00
|
|
|
if err != nil || !isTraceableBlock(ic, h) {
|
|
|
|
v.Estack().PushVal(-1)
|
|
|
|
return nil
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
v.Estack().PushVal(h)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-13 10:05:31 +00:00
|
|
|
// engineGetScriptContainer returns transaction or block that contains the script
|
|
|
|
// being run.
|
2020-04-08 10:35:39 +00:00
|
|
|
func engineGetScriptContainer(ic *interop.Context, v *vm.VM) error {
|
2020-07-13 10:05:31 +00:00
|
|
|
var item stackitem.Item
|
|
|
|
switch t := ic.Container.(type) {
|
|
|
|
case *transaction.Transaction:
|
|
|
|
item = transactionToStackItem(t)
|
|
|
|
case *block.Block:
|
|
|
|
item = blockToStackItem(t)
|
|
|
|
default:
|
|
|
|
return errors.New("unknown script container")
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(item)
|
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.
|
2020-06-03 12:55:06 +00:00
|
|
|
_, err := stackitem.SerializeItem(item)
|
2020-02-07 09:17:39 +00:00
|
|
|
if err != nil {
|
2020-06-03 12:55:06 +00:00
|
|
|
item = stackitem.NewByteArray([]byte(fmt.Sprintf("bad notification: %v", err)))
|
2020-02-07 09:17:39 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// storageDelete deletes stored key-value pair.
|
2020-04-08 10:35:39 +00:00
|
|
|
func storageDelete(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 {
|
|
|
|
return errors.New("StorageContext is read only")
|
|
|
|
}
|
|
|
|
key := v.Estack().Pop().Bytes()
|
2020-06-18 10:50:30 +00:00
|
|
|
si := ic.DAO.GetStorageItem(stc.ID, key)
|
2019-10-11 14:00:11 +00:00
|
|
|
if si != nil && si.IsConst {
|
|
|
|
return errors.New("storage item is constant")
|
|
|
|
}
|
2020-06-18 10:50:30 +00:00
|
|
|
return ic.DAO.DeleteStorageItem(stc.ID, 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)
|
|
|
|
}
|
|
|
|
key := v.Estack().Pop().Bytes()
|
2020-06-18 10:50:30 +00:00
|
|
|
si := ic.DAO.GetStorageItem(stc.ID, key)
|
2019-10-11 14:00:11 +00:00
|
|
|
if si != nil && si.Value != nil {
|
|
|
|
v.Estack().PushVal(si.Value)
|
|
|
|
} else {
|
2020-06-23 21:06:42 +00:00
|
|
|
v.Estack().PushVal(stackitem.Null{})
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// storageGetContext returns storage context (scripthash).
|
2020-04-08 10:35:39 +00:00
|
|
|
func storageGetContext(ic *interop.Context, v *vm.VM) error {
|
2020-06-18 10:50:30 +00:00
|
|
|
contract, err := ic.DAO.GetContractState(v.GetCurrentScriptHash())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !contract.HasStorage() {
|
2020-06-19 21:04:28 +00:00
|
|
|
return errors.New("contract is not allowed to use storage")
|
2020-06-18 10:50:30 +00:00
|
|
|
}
|
2019-10-11 14:00:11 +00:00
|
|
|
sc := &StorageContext{
|
2020-06-18 10:50:30 +00:00
|
|
|
ID: contract.ID,
|
|
|
|
ReadOnly: false,
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
v.Estack().PushVal(stackitem.NewInterop(sc))
|
2019-10-11 14:00:11 +00:00
|
|
|
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 {
|
2020-06-18 10:50:30 +00:00
|
|
|
contract, err := ic.DAO.GetContractState(v.GetCurrentScriptHash())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !contract.HasStorage() {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-11 14:00:11 +00:00
|
|
|
sc := &StorageContext{
|
2020-06-18 10:50:30 +00:00
|
|
|
ID: contract.ID,
|
|
|
|
ReadOnly: true,
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
v.Estack().PushVal(stackitem.NewInterop(sc))
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-15 08:39:15 +00:00
|
|
|
func putWithContextAndFlags(ic *interop.Context, v *vm.VM, stc *StorageContext, key []byte, value []byte, isConst bool) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
if len(key) > MaxStorageKeyLen {
|
|
|
|
return errors.New("key is too big")
|
|
|
|
}
|
|
|
|
if stc.ReadOnly {
|
|
|
|
return errors.New("StorageContext is read only")
|
|
|
|
}
|
2020-06-18 10:50:30 +00:00
|
|
|
si := ic.DAO.GetStorageItem(stc.ID, 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")
|
|
|
|
}
|
2020-06-15 08:39:15 +00:00
|
|
|
sizeInc := 1
|
|
|
|
if len(value) > len(si.Value) {
|
|
|
|
sizeInc = len(value) - len(si.Value)
|
|
|
|
}
|
2020-06-23 14:15:35 +00:00
|
|
|
if !v.AddGas(int64(sizeInc) * StoragePrice) {
|
2020-06-15 08:39:15 +00:00
|
|
|
return errGasLimitExceeded
|
|
|
|
}
|
2019-10-11 14:00:11 +00:00
|
|
|
si.Value = value
|
|
|
|
si.IsConst = isConst
|
2020-06-18 10:50:30 +00:00
|
|
|
return ic.DAO.PutStorageItem(stc.ID, 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-06-15 08:39:15 +00:00
|
|
|
return putWithContextAndFlags(ic, v, 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{
|
2020-06-18 10:50:30 +00:00
|
|
|
ID: stc.ID,
|
|
|
|
ReadOnly: true,
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
stc = stx
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
v.Estack().PushVal(stackitem.NewInterop(stc))
|
2019-10-11 14:00:11 +00:00
|
|
|
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()))
|
2020-07-15 10:53:24 +00:00
|
|
|
if flags&^smartcontract.All != 0 {
|
|
|
|
return errors.New("call flags out of range")
|
|
|
|
}
|
2020-05-07 11:11:59 +00:00
|
|
|
return contractCallExInternal(ic, v, h, method, args, flags)
|
|
|
|
}
|
|
|
|
|
2020-06-10 12:51:28 +00:00
|
|
|
func contractCallExInternal(ic *interop.Context, v *vm.VM, h []byte, method stackitem.Item, args stackitem.Item, f smartcontract.CallFlag) error {
|
2020-05-07 11:11:59 +00:00
|
|
|
u, err := util.Uint160DecodeBytesBE(h)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("invalid contract hash")
|
|
|
|
}
|
2020-06-09 13:24:03 +00:00
|
|
|
cs, err := ic.DAO.GetContractState(u)
|
|
|
|
if err != nil {
|
2020-05-07 11:11:59 +00:00
|
|
|
return errors.New("contract not found")
|
|
|
|
}
|
2020-06-09 13:24:03 +00:00
|
|
|
bs, err := method.TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
curr, err := ic.DAO.GetContractState(v.GetCurrentScriptHash())
|
|
|
|
if err == nil {
|
|
|
|
if !curr.Manifest.CanCall(&cs.Manifest, string(bs)) {
|
|
|
|
return errors.New("disallowed method call")
|
|
|
|
}
|
|
|
|
}
|
2020-06-16 09:47:42 +00:00
|
|
|
ic.Invocations[u]++
|
2020-06-10 14:21:26 +00:00
|
|
|
v.LoadScriptWithHash(cs.Script, u, v.Context().GetCallFlags()&f)
|
2020-05-07 11:11:59 +00:00
|
|
|
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 {
|
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-06-18 10:50:30 +00:00
|
|
|
siMap, err := ic.DAO.GetStorageItems(cs.ID)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for k := range siMap {
|
2020-06-18 10:50:30 +00:00
|
|
|
_ = ic.DAO.DeleteStorageItem(cs.ID, []byte(k))
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-16 08:17:18 +00:00
|
|
|
|
|
|
|
// contractIsStandard checks if contract is standard (sig or multisig) contract.
|
|
|
|
func contractIsStandard(ic *interop.Context, v *vm.VM) error {
|
|
|
|
h := v.Estack().Pop().Bytes()
|
|
|
|
u, err := util.Uint160DecodeBytesBE(h)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var result bool
|
|
|
|
cs, _ := ic.DAO.GetContractState(u)
|
2020-07-16 06:56:57 +00:00
|
|
|
if cs != nil {
|
|
|
|
result = vm.IsStandardContract(cs.Script)
|
|
|
|
} else {
|
|
|
|
if tx, ok := ic.Container.(*transaction.Transaction); ok {
|
|
|
|
for _, witness := range tx.Scripts {
|
|
|
|
if witness.ScriptHash() == u {
|
|
|
|
result = vm.IsStandardContract(witness.VerificationScript)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-16 08:17:18 +00:00
|
|
|
}
|
|
|
|
v.Estack().PushVal(result)
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-16 08:25:30 +00:00
|
|
|
|
|
|
|
// contractCreateStandardAccount calculates contract scripthash for a given public key.
|
|
|
|
func contractCreateStandardAccount(ic *interop.Context, v *vm.VM) error {
|
|
|
|
h := v.Estack().Pop().Bytes()
|
2020-07-13 09:59:41 +00:00
|
|
|
p, err := keys.NewPublicKeyFromBytes(h, elliptic.P256())
|
2020-06-16 08:25:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(p.GetScriptHash().BytesBE())
|
|
|
|
return nil
|
|
|
|
}
|