2019-10-11 14:00:11 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2021-10-06 12:54:44 +00:00
|
|
|
"context"
|
2020-07-13 09:59:41 +00:00
|
|
|
"crypto/elliptic"
|
2019-10-11 14:00:11 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-02-18 17:29:14 +00:00
|
|
|
"math"
|
2021-08-30 20:43:17 +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:35:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2021-05-11 14:51:45 +00:00
|
|
|
istorage "github.com/nspcc-dev/neo-go/pkg/core/interop/storage"
|
2021-02-03 19:01:20 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native"
|
2021-03-30 13:47:15 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2021-02-18 17:29:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2020-06-16 08:25:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-02-18 17:29:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2019-10-11 14:00:11 +00:00
|
|
|
)
|
|
|
|
|
2021-05-11 14:51:45 +00:00
|
|
|
var (
|
|
|
|
errGasLimitExceeded = errors.New("gas limit exceeded")
|
|
|
|
errFindInvalidOptions = errors.New("invalid Find options")
|
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-13 10:05:31 +00:00
|
|
|
// engineGetScriptContainer returns transaction or block that contains the script
|
|
|
|
// being run.
|
2020-08-07 11:37:49 +00:00
|
|
|
func engineGetScriptContainer(ic *interop.Context) error {
|
2020-07-13 10:05:31 +00:00
|
|
|
var item stackitem.Item
|
|
|
|
switch t := ic.Container.(type) {
|
|
|
|
case *transaction.Transaction:
|
2021-02-03 19:01:20 +00:00
|
|
|
item = native.TransactionToStackItem(t)
|
2020-07-13 10:05:31 +00:00
|
|
|
case *block.Block:
|
2021-02-03 19:01:20 +00:00
|
|
|
item = native.BlockToStackItem(t)
|
2020-07-13 10:05:31 +00:00
|
|
|
default:
|
|
|
|
return errors.New("unknown script container")
|
|
|
|
}
|
2021-08-30 20:43:17 +00:00
|
|
|
ic.VM.Estack().PushItem(item)
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// storageDelete deletes stored key-value pair.
|
2020-08-07 11:37:49 +00:00
|
|
|
func storageDelete(ic *interop.Context) error {
|
|
|
|
stcInterface := ic.VM.Estack().Pop().Value()
|
2019-10-11 14:00:11 +00:00
|
|
|
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-08-07 11:37:49 +00:00
|
|
|
key := ic.VM.Estack().Pop().Bytes()
|
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-08-07 11:37:49 +00:00
|
|
|
func storageGet(ic *interop.Context) error {
|
|
|
|
stcInterface := ic.VM.Estack().Pop().Value()
|
2019-10-11 14:00:11 +00:00
|
|
|
stc, ok := stcInterface.(*StorageContext)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not a StorageContext", stcInterface)
|
|
|
|
}
|
2020-08-07 11:37:49 +00:00
|
|
|
key := ic.VM.Estack().Pop().Bytes()
|
2020-06-18 10:50:30 +00:00
|
|
|
si := ic.DAO.GetStorageItem(stc.ID, key)
|
2021-03-05 14:06:54 +00:00
|
|
|
if si != nil {
|
2021-08-30 20:43:17 +00:00
|
|
|
ic.VM.Estack().PushItem(stackitem.NewByteArray([]byte(si)))
|
2019-10-11 14:00:11 +00:00
|
|
|
} else {
|
2021-08-30 20:43:17 +00:00
|
|
|
ic.VM.Estack().PushItem(stackitem.Null{})
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// storageGetContext returns storage context (scripthash).
|
2020-08-07 11:37:49 +00:00
|
|
|
func storageGetContext(ic *interop.Context) error {
|
|
|
|
return storageGetContextInternal(ic, false)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// storageGetReadOnlyContext returns read-only context (scripthash).
|
2020-08-07 11:37:49 +00:00
|
|
|
func storageGetReadOnlyContext(ic *interop.Context) error {
|
|
|
|
return storageGetContextInternal(ic, true)
|
2020-07-22 07:46:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// storageGetContextInternal is internal version of storageGetContext and
|
|
|
|
// storageGetReadOnlyContext which allows to specify ReadOnly context flag.
|
2020-08-07 11:37:49 +00:00
|
|
|
func storageGetContextInternal(ic *interop.Context, isReadOnly bool) error {
|
2020-12-13 15:26:35 +00:00
|
|
|
contract, err := ic.GetContract(ic.VM.GetCurrentScriptHash())
|
2020-06-18 10:50:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-11 14:00:11 +00:00
|
|
|
sc := &StorageContext{
|
2020-06-18 10:50:30 +00:00
|
|
|
ID: contract.ID,
|
2020-07-22 07:46:28 +00:00
|
|
|
ReadOnly: isReadOnly,
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
2021-08-30 20:43:17 +00:00
|
|
|
ic.VM.Estack().PushItem(stackitem.NewInterop(sc))
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-05 12:36:32 +00:00
|
|
|
func putWithContext(ic *interop.Context, stc *StorageContext, key []byte, value []byte) error {
|
2021-03-30 13:47:15 +00:00
|
|
|
if len(key) > storage.MaxStorageKeyLen {
|
2019-10-11 14:00:11 +00:00
|
|
|
return errors.New("key is too big")
|
|
|
|
}
|
2021-03-30 13:47:15 +00:00
|
|
|
if len(value) > storage.MaxStorageValueLen {
|
2020-07-22 08:05:10 +00:00
|
|
|
return errors.New("value is too big")
|
|
|
|
}
|
2019-10-11 14:00:11 +00:00
|
|
|
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)
|
2021-03-05 12:36:32 +00:00
|
|
|
sizeInc := len(value)
|
2020-10-05 09:32:04 +00:00
|
|
|
if si == nil {
|
|
|
|
sizeInc = len(key) + len(value)
|
2020-11-03 19:50:40 +00:00
|
|
|
} else if len(value) != 0 {
|
2021-03-05 14:06:54 +00:00
|
|
|
if len(value) <= len(si) {
|
2020-11-03 19:50:40 +00:00
|
|
|
sizeInc = (len(value)-1)/4 + 1
|
2021-03-05 14:06:54 +00:00
|
|
|
} else if len(si) != 0 {
|
|
|
|
sizeInc = (len(si)-1)/4 + 1 + len(value) - len(si)
|
2020-11-03 19:50:40 +00:00
|
|
|
}
|
2020-06-15 08:39:15 +00:00
|
|
|
}
|
2022-01-11 21:58:03 +00:00
|
|
|
if !ic.VM.AddGas(int64(sizeInc) * ic.Chain.GetStoragePrice()) {
|
2020-06-15 08:39:15 +00:00
|
|
|
return errGasLimitExceeded
|
|
|
|
}
|
2021-03-05 14:06:54 +00:00
|
|
|
return ic.DAO.PutStorageItem(stc.ID, key, value)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
|
2021-03-05 12:36:32 +00:00
|
|
|
// storagePut puts key-value pair into the storage.
|
|
|
|
func storagePut(ic *interop.Context) error {
|
2020-08-07 11:37:49 +00:00
|
|
|
stcInterface := ic.VM.Estack().Pop().Value()
|
2019-10-11 14:00:11 +00:00
|
|
|
stc, ok := stcInterface.(*StorageContext)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not a StorageContext", stcInterface)
|
|
|
|
}
|
2020-08-07 11:37:49 +00:00
|
|
|
key := ic.VM.Estack().Pop().Bytes()
|
|
|
|
value := ic.VM.Estack().Pop().Bytes()
|
2021-03-05 12:36:32 +00:00
|
|
|
return putWithContext(ic, stc, key, value)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// storageContextAsReadOnly sets given context to read-only mode.
|
2020-08-07 11:37:49 +00:00
|
|
|
func storageContextAsReadOnly(ic *interop.Context) error {
|
|
|
|
stcInterface := ic.VM.Estack().Pop().Value()
|
2019-10-11 14:00:11 +00:00
|
|
|
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
|
|
|
|
}
|
2021-08-30 20:43:17 +00:00
|
|
|
ic.VM.Estack().PushItem(stackitem.NewInterop(stc))
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-11 14:51:45 +00:00
|
|
|
// storageFind finds stored key-value pair.
|
|
|
|
func storageFind(ic *interop.Context) error {
|
|
|
|
stcInterface := ic.VM.Estack().Pop().Value()
|
|
|
|
stc, ok := stcInterface.(*StorageContext)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not a StorageContext", stcInterface)
|
|
|
|
}
|
|
|
|
prefix := ic.VM.Estack().Pop().Bytes()
|
|
|
|
opts := ic.VM.Estack().Pop().BigInt().Int64()
|
|
|
|
if opts&^istorage.FindAll != 0 {
|
|
|
|
return fmt.Errorf("%w: unknown flag", errFindInvalidOptions)
|
|
|
|
}
|
|
|
|
if opts&istorage.FindKeysOnly != 0 &&
|
|
|
|
opts&(istorage.FindDeserialize|istorage.FindPick0|istorage.FindPick1) != 0 {
|
|
|
|
return fmt.Errorf("%w KeysOnly conflicts with other options", errFindInvalidOptions)
|
|
|
|
}
|
|
|
|
if opts&istorage.FindValuesOnly != 0 &&
|
|
|
|
opts&(istorage.FindKeysOnly|istorage.FindRemovePrefix) != 0 {
|
|
|
|
return fmt.Errorf("%w: KeysOnly conflicts with ValuesOnly", errFindInvalidOptions)
|
|
|
|
}
|
|
|
|
if opts&istorage.FindPick0 != 0 && opts&istorage.FindPick1 != 0 {
|
|
|
|
return fmt.Errorf("%w: Pick0 conflicts with Pick1", errFindInvalidOptions)
|
|
|
|
}
|
|
|
|
if opts&istorage.FindDeserialize == 0 && (opts&istorage.FindPick0 != 0 || opts&istorage.FindPick1 != 0) {
|
|
|
|
return fmt.Errorf("%w: PickN is specified without Deserialize", errFindInvalidOptions)
|
|
|
|
}
|
2021-10-04 14:01:42 +00:00
|
|
|
// Items in seekres should be sorted by key, but GetStorageItemsWithPrefix returns
|
2021-09-24 14:22:45 +00:00
|
|
|
// sorted items, so no need to sort them one more time.
|
2021-10-06 12:54:44 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
seekres := ic.DAO.SeekAsync(ctx, stc.ID, prefix)
|
2021-10-07 11:27:55 +00:00
|
|
|
item := istorage.NewIterator(seekres, prefix, opts)
|
2021-08-30 20:43:17 +00:00
|
|
|
ic.VM.Estack().PushItem(stackitem.NewInterop(item))
|
2021-10-07 11:27:55 +00:00
|
|
|
ic.RegisterCancelFunc(cancel)
|
2021-05-11 14:51:45 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 17:29:14 +00:00
|
|
|
// contractCreateMultisigAccount calculates multisig contract scripthash for a
|
|
|
|
// given m and a set of public keys.
|
|
|
|
func contractCreateMultisigAccount(ic *interop.Context) error {
|
|
|
|
m := ic.VM.Estack().Pop().BigInt()
|
2021-07-19 12:15:14 +00:00
|
|
|
mu64 := m.Uint64()
|
|
|
|
if !m.IsUint64() || mu64 > math.MaxInt32 {
|
|
|
|
return errors.New("m must be positive and fit int32")
|
2021-02-18 17:29:14 +00:00
|
|
|
}
|
|
|
|
arr := ic.VM.Estack().Pop().Array()
|
|
|
|
pubs := make(keys.PublicKeys, len(arr))
|
|
|
|
for i, pk := range arr {
|
|
|
|
p, err := keys.NewPublicKeyFromBytes(pk.Value().([]byte), elliptic.P256())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pubs[i] = p
|
|
|
|
}
|
2021-07-19 12:15:14 +00:00
|
|
|
script, err := smartcontract.CreateMultiSigRedeemScript(int(mu64), pubs)
|
2021-02-18 17:29:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-30 20:43:17 +00:00
|
|
|
ic.VM.Estack().PushItem(stackitem.NewByteArray(hash.Hash160(script).BytesBE()))
|
2021-02-18 17:29:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-16 08:25:30 +00:00
|
|
|
// contractCreateStandardAccount calculates contract scripthash for a given public key.
|
2020-08-07 11:37:49 +00:00
|
|
|
func contractCreateStandardAccount(ic *interop.Context) error {
|
|
|
|
h := ic.VM.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
|
|
|
|
}
|
2021-08-30 20:43:17 +00:00
|
|
|
ic.VM.Estack().PushItem(stackitem.NewByteArray(p.GetScriptHash().BytesBE()))
|
2020-06-16 08:25:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-07-16 09:13:55 +00:00
|
|
|
|
|
|
|
// contractGetCallFlags returns current context calling flags.
|
2020-08-07 11:37:49 +00:00
|
|
|
func contractGetCallFlags(ic *interop.Context) error {
|
2021-08-30 20:43:17 +00:00
|
|
|
ic.VM.Estack().PushItem(stackitem.NewBigInteger(big.NewInt(int64(ic.VM.Context().GetCallFlags()))))
|
2020-07-16 09:13:55 +00:00
|
|
|
return nil
|
|
|
|
}
|