2019-10-11 14:00:11 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2020-05-29 08:02:26 +00:00
|
|
|
"bytes"
|
2019-10-11 14:00:11 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-05-29 08:02:26 +00:00
|
|
|
"sort"
|
2019-10-11 14:00:11 +00:00
|
|
|
|
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"
|
2020-06-09 09:12:56 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2020-06-09 09:23:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-03-03 14:21:42 +00:00
|
|
|
"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-10-11 14:00:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-03-09 11:18:51 +00:00
|
|
|
// MaxContractDescriptionLen is the maximum length for contract description.
|
|
|
|
MaxContractDescriptionLen = 65536
|
2019-10-11 14:00:11 +00:00
|
|
|
// MaxContractScriptSize is the maximum script size for a contract.
|
|
|
|
MaxContractScriptSize = 1024 * 1024
|
|
|
|
// MaxContractParametersNum is the maximum number of parameters for a contract.
|
|
|
|
MaxContractParametersNum = 252
|
|
|
|
// MaxContractStringLen is the maximum length for contract metadata strings.
|
|
|
|
MaxContractStringLen = 252
|
|
|
|
)
|
|
|
|
|
2020-06-15 08:39:15 +00:00
|
|
|
var errGasLimitExceeded = errors.New("gas limit exceeded")
|
|
|
|
|
2019-10-11 14:00:11 +00:00
|
|
|
// storageFind finds stored key-value pair.
|
2020-04-08 10:35:39 +00:00
|
|
|
func storageFind(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
|
|
|
|
}
|
2020-05-29 08:02:26 +00:00
|
|
|
prefix := v.Estack().Pop().Bytes()
|
|
|
|
siMap, err := ic.DAO.GetStorageItemsWithPrefix(stc.ScriptHash, prefix)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-26 11:34:38 +00:00
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
filteredMap := stackitem.NewMap()
|
2019-10-11 14:00:11 +00:00
|
|
|
for k, v := range siMap {
|
2020-06-03 12:55:06 +00:00
|
|
|
filteredMap.Add(stackitem.NewByteArray(append(prefix, []byte(k)...)), stackitem.NewByteArray(v.Value))
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
sort.Slice(filteredMap.Value().([]stackitem.MapElement), func(i, j int) bool {
|
|
|
|
return bytes.Compare(filteredMap.Value().([]stackitem.MapElement)[i].Key.Value().([]byte),
|
|
|
|
filteredMap.Value().([]stackitem.MapElement)[j].Key.Value().([]byte)) == -1
|
2020-05-29 08:02:26 +00:00
|
|
|
})
|
2019-10-11 14:00:11 +00:00
|
|
|
|
2019-12-26 11:34:38 +00:00
|
|
|
item := vm.NewMapIterator(filteredMap)
|
|
|
|
v.Estack().PushVal(item)
|
|
|
|
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-12-26 11:34:38 +00:00
|
|
|
|
2019-10-11 14:00:11 +00:00
|
|
|
// createContractStateFromVM pops all contract state elements from the VM
|
2019-11-28 16:06:09 +00:00
|
|
|
// evaluation stack, does a lot of checks and returns Contract if it
|
2019-10-17 09:30:24 +00:00
|
|
|
// succeeds.
|
2020-04-08 10:35:39 +00:00
|
|
|
func createContractStateFromVM(ic *interop.Context, v *vm.VM) (*state.Contract, error) {
|
2019-10-11 14:00:11 +00:00
|
|
|
script := v.Estack().Pop().Bytes()
|
|
|
|
if len(script) > MaxContractScriptSize {
|
|
|
|
return nil, errors.New("the script is too big")
|
|
|
|
}
|
2020-06-09 09:12:56 +00:00
|
|
|
manifestBytes := v.Estack().Pop().Bytes()
|
|
|
|
if len(manifestBytes) > manifest.MaxManifestSize {
|
|
|
|
return nil, errors.New("manifest is too big")
|
|
|
|
}
|
2020-06-09 09:23:14 +00:00
|
|
|
if !v.AddGas(util.Fixed8(StoragePrice * (len(script) + len(manifestBytes)))) {
|
2020-06-15 08:39:15 +00:00
|
|
|
return nil, errGasLimitExceeded
|
2020-06-09 09:23:14 +00:00
|
|
|
}
|
2020-06-09 09:12:56 +00:00
|
|
|
var m manifest.Manifest
|
2020-06-18 19:36:28 +00:00
|
|
|
err := m.UnmarshalJSON(manifestBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-06-09 09:12:56 +00:00
|
|
|
}
|
|
|
|
return &state.Contract{
|
|
|
|
Script: script,
|
|
|
|
Manifest: m,
|
|
|
|
}, nil
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// contractCreate creates a contract.
|
2020-04-08 10:35:39 +00:00
|
|
|
func contractCreate(ic *interop.Context, v *vm.VM) error {
|
2020-04-08 10:29:15 +00:00
|
|
|
newcontract, err := createContractStateFromVM(ic, v)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
2020-03-09 11:18:00 +00:00
|
|
|
return err
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
2020-04-08 10:35:39 +00:00
|
|
|
contract, err := ic.DAO.GetContractState(newcontract.ScriptHash())
|
2020-06-09 09:12:56 +00:00
|
|
|
if contract != nil {
|
|
|
|
return errors.New("contract already exists")
|
2020-06-09 12:12:23 +00:00
|
|
|
}
|
|
|
|
id, err := ic.DAO.GetNextContractID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newcontract.ID = id
|
|
|
|
if err := ic.DAO.PutNextContractID(id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := ic.DAO.PutContractState(newcontract); err != nil {
|
2020-06-09 09:12:56 +00:00
|
|
|
return err
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
2020-06-09 09:12:56 +00:00
|
|
|
v.Estack().PushVal(stackitem.NewInterop(newcontract))
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-10 11:07:26 +00:00
|
|
|
// contractUpdate migrates a contract.
|
|
|
|
func contractUpdate(ic *interop.Context, v *vm.VM) error {
|
2020-06-09 09:12:56 +00:00
|
|
|
contract, err := ic.DAO.GetContractState(v.GetCurrentScriptHash())
|
|
|
|
if contract == nil {
|
|
|
|
return errors.New("contract doesn't exist")
|
|
|
|
}
|
2020-04-08 10:29:15 +00:00
|
|
|
newcontract, err := createContractStateFromVM(ic, v)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
2020-03-09 11:18:00 +00:00
|
|
|
return err
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
2020-06-09 09:12:56 +00:00
|
|
|
if newcontract.Script != nil {
|
|
|
|
if l := len(newcontract.Script); l == 0 || l > MaxContractScriptSize {
|
|
|
|
return errors.New("invalid script len")
|
|
|
|
}
|
|
|
|
h := newcontract.ScriptHash()
|
|
|
|
if h.Equals(contract.ScriptHash()) {
|
|
|
|
return errors.New("the script is the same")
|
|
|
|
} else if _, err := ic.DAO.GetContractState(h); err == nil {
|
|
|
|
return errors.New("contract already exists")
|
|
|
|
}
|
|
|
|
newcontract.ID = contract.ID
|
|
|
|
if err := ic.DAO.PutContractState(newcontract); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := ic.DAO.DeleteContractState(contract.ScriptHash()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if contract.HasStorage() {
|
|
|
|
// TODO store items by ID #1037
|
|
|
|
hash := v.GetCurrentScriptHash()
|
|
|
|
siMap, err := ic.DAO.GetStorageItems(hash)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-09 09:12:56 +00:00
|
|
|
for k, v := range siMap {
|
|
|
|
v.IsConst = false
|
|
|
|
err = ic.DAO.PutStorageItem(contract.ScriptHash(), []byte(k), v)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
v.Estack().PushVal(stackitem.NewInterop(contract))
|
2020-04-08 10:29:15 +00:00
|
|
|
return contractDestroy(ic, v)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 14:10:52 +00:00
|
|
|
// runtimeSerialize serializes top stack item into a ByteArray.
|
2020-04-08 10:35:39 +00:00
|
|
|
func runtimeSerialize(_ *interop.Context, v *vm.VM) error {
|
2019-11-05 14:10:52 +00:00
|
|
|
return vm.RuntimeSerialize(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// runtimeDeserialize deserializes ByteArray from a stack into an item.
|
2020-04-08 10:35:39 +00:00
|
|
|
func runtimeDeserialize(_ *interop.Context, v *vm.VM) error {
|
2019-11-05 14:10:52 +00:00
|
|
|
return vm.RuntimeDeserialize(v)
|
|
|
|
}
|