2019-10-11 14:00:11 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2020-05-29 08:02:26 +00:00
|
|
|
"bytes"
|
2020-07-15 08:49:24 +00:00
|
|
|
"encoding/base64"
|
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-08-23 14:19:56 +00:00
|
|
|
"github.com/mr-tron/base58"
|
2020-04-08 10:35:39 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2020-10-02 09:56:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/contract"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2020-07-16 08:32:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
|
2020-10-02 09:56:51 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-06-09 09:12:56 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
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-08-07 11:37:49 +00:00
|
|
|
func storageFind(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
|
|
|
prefix := ic.VM.Estack().Pop().Bytes()
|
2020-06-18 10:50:30 +00:00
|
|
|
siMap, err := ic.DAO.GetStorageItemsWithPrefix(stc.ID, 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)
|
2020-08-07 11:37:49 +00:00
|
|
|
ic.VM.Estack().PushVal(item)
|
2019-12-26 11:34:38 +00:00
|
|
|
|
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-08-07 11:37:49 +00:00
|
|
|
func createContractStateFromVM(ic *interop.Context) (*state.Contract, error) {
|
|
|
|
script := ic.VM.Estack().Pop().Bytes()
|
2019-10-11 14:00:11 +00:00
|
|
|
if len(script) > MaxContractScriptSize {
|
|
|
|
return nil, errors.New("the script is too big")
|
|
|
|
}
|
2020-08-07 11:37:49 +00:00
|
|
|
manifestBytes := ic.VM.Estack().Pop().Bytes()
|
2020-06-09 09:12:56 +00:00
|
|
|
if len(manifestBytes) > manifest.MaxManifestSize {
|
|
|
|
return nil, errors.New("manifest is too big")
|
|
|
|
}
|
2020-08-07 11:37:49 +00:00
|
|
|
if !ic.VM.AddGas(int64(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 {
|
2020-08-06 16:09:57 +00:00
|
|
|
return nil, fmt.Errorf("unable to retrieve manifest from stack: %w", 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-08-07 11:37:49 +00:00
|
|
|
func contractCreate(ic *interop.Context) error {
|
|
|
|
newcontract, err := createContractStateFromVM(ic)
|
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-08-14 09:08:57 +00:00
|
|
|
if contract != nil && err == nil {
|
2020-06-09 09:12:56 +00:00
|
|
|
return errors.New("contract already exists")
|
2020-06-09 12:12:23 +00:00
|
|
|
}
|
2020-06-23 18:09:37 +00:00
|
|
|
id, err := ic.DAO.GetAndUpdateNextContractID()
|
2020-06-09 12:12:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newcontract.ID = id
|
2020-07-15 11:39:20 +00:00
|
|
|
if !newcontract.Manifest.IsValid(newcontract.ScriptHash()) {
|
|
|
|
return errors.New("failed to check contract script hash against manifest")
|
|
|
|
}
|
2020-06-09 12:12:23 +00:00
|
|
|
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-07-15 11:39:20 +00:00
|
|
|
cs, err := contractToStackItem(newcontract)
|
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return fmt.Errorf("cannot convert contract to stack item: %w", err)
|
2020-07-15 11:39:20 +00:00
|
|
|
}
|
2020-08-07 11:37:49 +00:00
|
|
|
ic.VM.Estack().PushVal(cs)
|
2020-10-02 09:56:51 +00:00
|
|
|
return callDeploy(ic, newcontract, false)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 13:12:08 +00:00
|
|
|
func checkNonEmpty(b []byte, max int) error {
|
|
|
|
if b != nil {
|
|
|
|
if l := len(b); l == 0 {
|
|
|
|
return errors.New("empty")
|
|
|
|
} else if l > max {
|
|
|
|
return fmt.Errorf("len is %d (max %d)", l, max)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 08:32:32 +00:00
|
|
|
// contractUpdate migrates a contract. This method assumes that Manifest and Script
|
|
|
|
// of the contract can be updated independently.
|
2020-08-07 11:37:49 +00:00
|
|
|
func contractUpdate(ic *interop.Context) error {
|
|
|
|
contract, _ := ic.DAO.GetContractState(ic.VM.GetCurrentScriptHash())
|
2020-06-09 09:12:56 +00:00
|
|
|
if contract == nil {
|
|
|
|
return errors.New("contract doesn't exist")
|
|
|
|
}
|
2020-10-05 13:12:08 +00:00
|
|
|
script := ic.VM.Estack().Pop().BytesOrNil()
|
|
|
|
manifestBytes := ic.VM.Estack().Pop().BytesOrNil()
|
|
|
|
if script == nil && manifestBytes == nil {
|
|
|
|
return errors.New("both script and manifest are nil")
|
2020-07-16 08:32:32 +00:00
|
|
|
}
|
2020-10-05 13:12:08 +00:00
|
|
|
if err := checkNonEmpty(script, MaxContractScriptSize); err != nil {
|
|
|
|
return fmt.Errorf("invalid script size: %w", err)
|
|
|
|
}
|
|
|
|
if err := checkNonEmpty(manifestBytes, manifest.MaxManifestSize); err != nil {
|
|
|
|
return fmt.Errorf("invalid manifest size: %w", err)
|
2020-07-16 08:32:32 +00:00
|
|
|
}
|
2020-08-07 11:37:49 +00:00
|
|
|
if !ic.VM.AddGas(int64(StoragePrice * (len(script) + len(manifestBytes)))) {
|
2020-07-16 08:32:32 +00:00
|
|
|
return errGasLimitExceeded
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
2020-07-16 08:32:32 +00:00
|
|
|
// if script was provided, update the old contract script and Manifest.ABI hash
|
2020-10-05 13:12:08 +00:00
|
|
|
if script != nil {
|
2020-07-16 08:32:32 +00:00
|
|
|
newHash := hash.Hash160(script)
|
|
|
|
if newHash.Equals(contract.ScriptHash()) {
|
2020-06-09 09:12:56 +00:00
|
|
|
return errors.New("the script is the same")
|
2020-07-16 08:32:32 +00:00
|
|
|
} else if _, err := ic.DAO.GetContractState(newHash); err == nil {
|
2020-06-09 09:12:56 +00:00
|
|
|
return errors.New("contract already exists")
|
|
|
|
}
|
2020-07-16 08:32:32 +00:00
|
|
|
oldHash := contract.ScriptHash()
|
|
|
|
// re-write existing contract variable, as we need it to be up-to-date during manifest update
|
|
|
|
contract = &state.Contract{
|
|
|
|
ID: contract.ID,
|
|
|
|
Script: script,
|
|
|
|
Manifest: contract.Manifest,
|
|
|
|
}
|
|
|
|
contract.Manifest.ABI.Hash = newHash
|
|
|
|
if err := ic.DAO.PutContractState(contract); err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return fmt.Errorf("failed to update script: %w", err)
|
2020-06-09 09:12:56 +00:00
|
|
|
}
|
2020-07-16 08:32:32 +00:00
|
|
|
if err := ic.DAO.DeleteContractState(oldHash); err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return fmt.Errorf("failed to update script: %w", err)
|
2020-06-09 09:12:56 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-16 08:32:32 +00:00
|
|
|
// if manifest was provided, update the old contract manifest and check associated
|
|
|
|
// storage items if needed
|
2020-10-05 13:12:08 +00:00
|
|
|
if manifestBytes != nil {
|
2020-07-16 08:32:32 +00:00
|
|
|
var newManifest manifest.Manifest
|
|
|
|
err := newManifest.UnmarshalJSON(manifestBytes)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return fmt.Errorf("unable to retrieve manifest from stack: %w", err)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
2020-07-16 08:32:32 +00:00
|
|
|
// we don't have to perform `GetContractState` one more time as it's already up-to-date
|
|
|
|
contract.Manifest = newManifest
|
|
|
|
if !contract.Manifest.IsValid(contract.ScriptHash()) {
|
|
|
|
return errors.New("failed to check contract script hash against new manifest")
|
|
|
|
}
|
|
|
|
if err := ic.DAO.PutContractState(contract); err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return fmt.Errorf("failed to update manifest: %w", err)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-06 14:17:49 +00:00
|
|
|
|
2020-10-02 09:56:51 +00:00
|
|
|
return callDeploy(ic, contract, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func callDeploy(ic *interop.Context, cs *state.Contract, isUpdate bool) error {
|
|
|
|
md := cs.Manifest.ABI.GetMethod(manifest.MethodDeploy)
|
|
|
|
if md != nil {
|
|
|
|
return contract.CallExInternal(ic, cs, manifest.MethodDeploy,
|
2020-10-12 11:32:27 +00:00
|
|
|
[]stackitem.Item{stackitem.NewBool(isUpdate)}, smartcontract.All, vm.EnsureIsEmpty)
|
2020-10-02 09:56:51 +00:00
|
|
|
}
|
2020-07-16 08:32:32 +00:00
|
|
|
return nil
|
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-08-07 11:37:49 +00:00
|
|
|
func runtimeSerialize(ic *interop.Context) error {
|
|
|
|
return vm.RuntimeSerialize(ic.VM)
|
2019-11-05 14:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// runtimeDeserialize deserializes ByteArray from a stack into an item.
|
2020-08-07 11:37:49 +00:00
|
|
|
func runtimeDeserialize(ic *interop.Context) error {
|
|
|
|
return vm.RuntimeDeserialize(ic.VM)
|
2019-11-05 14:10:52 +00:00
|
|
|
}
|
2020-07-15 08:49:24 +00:00
|
|
|
|
2020-08-23 14:19:56 +00:00
|
|
|
// runtimeEncodeBase64 encodes top stack item into a base64 string.
|
|
|
|
func runtimeEncodeBase64(ic *interop.Context) error {
|
2020-08-07 11:37:49 +00:00
|
|
|
src := ic.VM.Estack().Pop().Bytes()
|
2020-07-15 08:49:24 +00:00
|
|
|
result := base64.StdEncoding.EncodeToString(src)
|
2020-08-07 11:37:49 +00:00
|
|
|
ic.VM.Estack().PushVal([]byte(result))
|
2020-07-15 08:49:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-23 14:19:56 +00:00
|
|
|
// runtimeDecodeBase64 decodes top stack item from base64 string to byte array.
|
|
|
|
func runtimeDecodeBase64(ic *interop.Context) error {
|
2020-08-07 11:37:49 +00:00
|
|
|
src := ic.VM.Estack().Pop().String()
|
2020-07-15 08:49:24 +00:00
|
|
|
result, err := base64.StdEncoding.DecodeString(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-07 11:37:49 +00:00
|
|
|
ic.VM.Estack().PushVal(result)
|
2020-07-15 08:49:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-08-23 14:19:56 +00:00
|
|
|
|
|
|
|
// runtimeEncodeBase58 encodes top stack item into a base58 string.
|
|
|
|
func runtimeEncodeBase58(ic *interop.Context) error {
|
|
|
|
src := ic.VM.Estack().Pop().Bytes()
|
|
|
|
result := base58.Encode(src)
|
|
|
|
ic.VM.Estack().PushVal([]byte(result))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// runtimeDecodeBase58 decodes top stack item from base58 string to byte array.
|
|
|
|
func runtimeDecodeBase58(ic *interop.Context) error {
|
|
|
|
src := ic.VM.Estack().Pop().String()
|
|
|
|
result, err := base58.Decode(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ic.VM.Estack().PushVal(result)
|
|
|
|
return nil
|
|
|
|
}
|