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"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
|
|
|
"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
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
if ic.Trigger != trigger.Application {
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil, errors.New("can't create contract when not triggered by an application")
|
|
|
|
}
|
|
|
|
script := v.Estack().Pop().Bytes()
|
|
|
|
if len(script) > MaxContractScriptSize {
|
|
|
|
return nil, errors.New("the script is too big")
|
|
|
|
}
|
|
|
|
paramBytes := v.Estack().Pop().Bytes()
|
|
|
|
if len(paramBytes) > MaxContractParametersNum {
|
|
|
|
return nil, errors.New("too many parameters for a script")
|
|
|
|
}
|
|
|
|
paramList := make([]smartcontract.ParamType, len(paramBytes))
|
|
|
|
for k, v := range paramBytes {
|
|
|
|
paramList[k] = smartcontract.ParamType(v)
|
|
|
|
}
|
|
|
|
retType := smartcontract.ParamType(v.Estack().Pop().BigInt().Int64())
|
|
|
|
properties := smartcontract.PropertyState(v.Estack().Pop().BigInt().Int64())
|
|
|
|
name := v.Estack().Pop().Bytes()
|
|
|
|
if len(name) > MaxContractStringLen {
|
|
|
|
return nil, errors.New("too big name")
|
|
|
|
}
|
|
|
|
version := v.Estack().Pop().Bytes()
|
|
|
|
if len(version) > MaxContractStringLen {
|
|
|
|
return nil, errors.New("too big version")
|
|
|
|
}
|
|
|
|
author := v.Estack().Pop().Bytes()
|
|
|
|
if len(author) > MaxContractStringLen {
|
|
|
|
return nil, errors.New("too big author")
|
|
|
|
}
|
|
|
|
email := v.Estack().Pop().Bytes()
|
|
|
|
if len(email) > MaxContractStringLen {
|
|
|
|
return nil, errors.New("too big email")
|
|
|
|
}
|
|
|
|
desc := v.Estack().Pop().Bytes()
|
2020-03-09 11:18:51 +00:00
|
|
|
if len(desc) > MaxContractDescriptionLen {
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil, errors.New("too big description")
|
|
|
|
}
|
2019-11-28 16:06:09 +00:00
|
|
|
contract := &state.Contract{
|
2019-10-11 14:00:11 +00:00
|
|
|
Script: script,
|
|
|
|
ParamList: paramList,
|
|
|
|
ReturnType: retType,
|
|
|
|
Properties: properties,
|
|
|
|
Name: string(name),
|
|
|
|
CodeVersion: string(version),
|
|
|
|
Author: string(author),
|
|
|
|
Email: string(email),
|
|
|
|
Description: string(desc),
|
|
|
|
}
|
|
|
|
return contract, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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())
|
2019-12-13 14:10:51 +00:00
|
|
|
if err != nil {
|
2019-10-11 14:00:11 +00:00
|
|
|
contract = newcontract
|
2020-04-08 10:35:39 +00:00
|
|
|
err := ic.DAO.PutContractState(contract)
|
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))
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// contractGetScript returns a script associated with a contract.
|
2020-04-08 10:35:39 +00:00
|
|
|
func contractGetScript(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)
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(cs.Script)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// contractIsPayable returns whether contract is payable.
|
2020-04-08 10:35:39 +00:00
|
|
|
func contractIsPayable(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)
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(cs.IsPayable())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// contractMigrate migrates a contract.
|
2020-04-08 10:35:39 +00:00
|
|
|
func contractMigrate(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())
|
2019-12-13 14:10:51 +00:00
|
|
|
if err != nil {
|
2019-10-11 14:00:11 +00:00
|
|
|
contract = newcontract
|
2020-04-08 10:35:39 +00:00
|
|
|
err := ic.DAO.PutContractState(contract)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if contract.HasStorage() {
|
2020-05-04 08:41:41 +00:00
|
|
|
hash := v.GetCurrentScriptHash()
|
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, v := range siMap {
|
|
|
|
v.IsConst = false
|
2020-04-08 10:35:39 +00:00
|
|
|
err = ic.DAO.PutStorageItem(contract.ScriptHash(), []byte(k), v)
|
2020-03-16 08:52:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
}
|