2019-10-11 14:00:11 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
2019-12-26 11:34:38 +00:00
|
|
|
"strings"
|
2019-10-11 14:00:11 +00:00
|
|
|
|
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"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2019-10-11 14:00:11 +00:00
|
|
|
gherr "github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
// MaxAssetNameLen is the maximum length of asset name.
|
|
|
|
MaxAssetNameLen = 1024
|
|
|
|
// MaxAssetPrecision is the maximum precision of asset.
|
|
|
|
MaxAssetPrecision = 8
|
|
|
|
// BlocksPerYear is a multiplier for asset renewal.
|
|
|
|
BlocksPerYear = 2000000
|
|
|
|
// DefaultAssetLifetime is the default lifetime of an asset (which differs
|
|
|
|
// from assets created by register tx).
|
|
|
|
DefaultAssetLifetime = 1 + BlocksPerYear
|
|
|
|
)
|
|
|
|
|
|
|
|
// headerGetVersion returns version from the header.
|
2020-04-08 10:29:15 +00:00
|
|
|
func headerGetVersion(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
header, err := popHeaderFromVM(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(header.Version)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// headerGetConsensusData returns consensus data from the header.
|
2020-04-08 10:29:15 +00:00
|
|
|
func headerGetConsensusData(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
header, err := popHeaderFromVM(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(header.ConsensusData)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// headerGetMerkleRoot returns version from the header.
|
2020-04-08 10:29:15 +00:00
|
|
|
func headerGetMerkleRoot(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
header, err := popHeaderFromVM(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-05 16:44:09 +00:00
|
|
|
v.Estack().PushVal(header.MerkleRoot.BytesBE())
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// headerGetNextConsensus returns version from the header.
|
2020-04-08 10:29:15 +00:00
|
|
|
func headerGetNextConsensus(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
header, err := popHeaderFromVM(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-05 16:44:09 +00:00
|
|
|
v.Estack().PushVal(header.NextConsensus.BytesBE())
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// txGetAttributes returns current transaction attributes.
|
2020-04-08 10:29:15 +00:00
|
|
|
func txGetAttributes(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
txInterface := v.Estack().Pop().Value()
|
|
|
|
tx, ok := txInterface.(*transaction.Transaction)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("value is not a transaction")
|
|
|
|
}
|
|
|
|
if len(tx.Attributes) > vm.MaxArraySize {
|
|
|
|
return errors.New("too many attributes")
|
|
|
|
}
|
|
|
|
attrs := make([]vm.StackItem, 0, len(tx.Attributes))
|
2019-12-09 14:14:10 +00:00
|
|
|
for i := range tx.Attributes {
|
|
|
|
attrs = append(attrs, vm.NewInteropItem(&tx.Attributes[i]))
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
v.Estack().PushVal(attrs)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// txGetInputs returns current transaction inputs.
|
2020-04-08 10:29:15 +00:00
|
|
|
func txGetInputs(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
txInterface := v.Estack().Pop().Value()
|
|
|
|
tx, ok := txInterface.(*transaction.Transaction)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("value is not a transaction")
|
|
|
|
}
|
|
|
|
if len(tx.Inputs) > vm.MaxArraySize {
|
|
|
|
return errors.New("too many inputs")
|
|
|
|
}
|
|
|
|
inputs := make([]vm.StackItem, 0, len(tx.Inputs))
|
2019-12-09 14:14:10 +00:00
|
|
|
for i := range tx.Inputs {
|
|
|
|
inputs = append(inputs, vm.NewInteropItem(&tx.Inputs[i]))
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
v.Estack().PushVal(inputs)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// txGetOutputs returns current transaction outputs.
|
2020-04-08 10:29:15 +00:00
|
|
|
func txGetOutputs(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
txInterface := v.Estack().Pop().Value()
|
|
|
|
tx, ok := txInterface.(*transaction.Transaction)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("value is not a transaction")
|
|
|
|
}
|
|
|
|
if len(tx.Outputs) > vm.MaxArraySize {
|
|
|
|
return errors.New("too many outputs")
|
|
|
|
}
|
|
|
|
outputs := make([]vm.StackItem, 0, len(tx.Outputs))
|
2019-12-09 14:14:10 +00:00
|
|
|
for i := range tx.Outputs {
|
|
|
|
outputs = append(outputs, vm.NewInteropItem(&tx.Outputs[i]))
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
v.Estack().PushVal(outputs)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// txGetReferences returns current transaction references.
|
2020-04-08 10:29:15 +00:00
|
|
|
func txGetReferences(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
txInterface := v.Estack().Pop().Value()
|
|
|
|
tx, ok := txInterface.(*transaction.Transaction)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("type mismatch: %T is not a Transaction", txInterface)
|
|
|
|
}
|
2020-02-26 07:58:20 +00:00
|
|
|
refs, err := ic.bc.References(tx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-11 14:00:11 +00:00
|
|
|
if len(refs) > vm.MaxArraySize {
|
|
|
|
return errors.New("too many references")
|
|
|
|
}
|
|
|
|
|
|
|
|
stackrefs := make([]vm.StackItem, 0, len(refs))
|
2020-03-06 16:18:54 +00:00
|
|
|
for i := range tx.Inputs {
|
|
|
|
for j := range refs {
|
|
|
|
if refs[j].In == tx.Inputs[i] {
|
|
|
|
stackrefs = append(stackrefs, vm.NewInteropItem(refs[j]))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
v.Estack().PushVal(stackrefs)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// txGetType returns current transaction type.
|
2020-04-08 10:29:15 +00:00
|
|
|
func txGetType(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
txInterface := v.Estack().Pop().Value()
|
|
|
|
tx, ok := txInterface.(*transaction.Transaction)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("value is not a transaction")
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(int(tx.Type))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// txGetUnspentCoins returns current transaction unspent coins.
|
2020-04-08 10:29:15 +00:00
|
|
|
func txGetUnspentCoins(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
txInterface := v.Estack().Pop().Value()
|
|
|
|
tx, ok := txInterface.(*transaction.Transaction)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("value is not a transaction")
|
|
|
|
}
|
2019-12-13 14:10:51 +00:00
|
|
|
ucs, err := ic.dao.GetUnspentCoinState(tx.Hash())
|
|
|
|
if err != nil {
|
2019-10-11 14:00:11 +00:00
|
|
|
return errors.New("no unspent coin state found")
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(vm.NewInteropItem(ucs))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// txGetWitnesses returns current transaction witnesses.
|
2020-04-08 10:29:15 +00:00
|
|
|
func txGetWitnesses(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
txInterface := v.Estack().Pop().Value()
|
|
|
|
tx, ok := txInterface.(*transaction.Transaction)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("value is not a transaction")
|
|
|
|
}
|
|
|
|
if len(tx.Scripts) > vm.MaxArraySize {
|
|
|
|
return errors.New("too many outputs")
|
|
|
|
}
|
|
|
|
scripts := make([]vm.StackItem, 0, len(tx.Scripts))
|
2019-12-09 14:14:10 +00:00
|
|
|
for i := range tx.Scripts {
|
|
|
|
scripts = append(scripts, vm.NewInteropItem(&tx.Scripts[i]))
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
v.Estack().PushVal(scripts)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-23 12:04:23 +00:00
|
|
|
// invocationTx_GetScript returns invocation script from the current transaction.
|
2020-04-08 10:29:15 +00:00
|
|
|
func invocationTxGetScript(ic *interopContext, v *vm.VM) error {
|
2019-12-23 12:04:23 +00:00
|
|
|
txInterface := v.Estack().Pop().Value()
|
|
|
|
tx, ok := txInterface.(*transaction.Transaction)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("value is not a transaction")
|
|
|
|
}
|
|
|
|
inv, ok := tx.Data.(*transaction.InvocationTX)
|
|
|
|
if tx.Type != transaction.InvocationType || !ok {
|
|
|
|
return errors.New("value is not an invocation transaction")
|
|
|
|
}
|
|
|
|
// It's important not to share inv.Script slice with the code running in VM.
|
|
|
|
script := make([]byte, len(inv.Script))
|
|
|
|
copy(script, inv.Script)
|
|
|
|
v.Estack().PushVal(script)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-23 12:19:16 +00:00
|
|
|
// witnessGetVerificationScript returns current witness' script.
|
2020-04-08 10:29:15 +00:00
|
|
|
func witnessGetVerificationScript(ic *interopContext, v *vm.VM) error {
|
2019-12-23 12:19:16 +00:00
|
|
|
witInterface := v.Estack().Pop().Value()
|
|
|
|
wit, ok := witInterface.(*transaction.Witness)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("value is not a witness")
|
|
|
|
}
|
|
|
|
// It's important not to share wit.VerificationScript slice with the code running in VM.
|
|
|
|
script := make([]byte, len(wit.VerificationScript))
|
|
|
|
copy(script, wit.VerificationScript)
|
|
|
|
v.Estack().PushVal(script)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-11 15:25:28 +00:00
|
|
|
// bcGetValidators returns validators.
|
2020-04-08 10:29:15 +00:00
|
|
|
func bcGetValidators(ic *interopContext, v *vm.VM) error {
|
2019-12-13 14:10:51 +00:00
|
|
|
validators := ic.dao.GetValidators()
|
2019-11-11 15:25:28 +00:00
|
|
|
v.Estack().PushVal(validators)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:00:11 +00:00
|
|
|
// popInputFromVM returns transaction.Input from the first estack element.
|
|
|
|
func popInputFromVM(v *vm.VM) (*transaction.Input, error) {
|
|
|
|
inInterface := v.Estack().Pop().Value()
|
|
|
|
input, ok := inInterface.(*transaction.Input)
|
|
|
|
if !ok {
|
2020-02-26 07:58:20 +00:00
|
|
|
txio, ok := inInterface.(transaction.InOut)
|
2019-10-11 14:00:11 +00:00
|
|
|
if !ok {
|
2020-02-26 07:58:20 +00:00
|
|
|
return nil, fmt.Errorf("type mismatch: %T is not an Input or InOut", inInterface)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
2020-02-26 07:58:20 +00:00
|
|
|
input = &txio.In
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
return input, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// inputGetHash returns hash from the given input.
|
2020-04-08 10:29:15 +00:00
|
|
|
func inputGetHash(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
input, err := popInputFromVM(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-27 09:23:18 +00:00
|
|
|
v.Estack().PushVal(input.PrevHash.BytesBE())
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// inputGetIndex returns index from the given input.
|
2020-04-08 10:29:15 +00:00
|
|
|
func inputGetIndex(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
input, err := popInputFromVM(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(input.PrevIndex)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// popOutputFromVM returns transaction.Input from the first estack element.
|
|
|
|
func popOutputFromVM(v *vm.VM) (*transaction.Output, error) {
|
|
|
|
outInterface := v.Estack().Pop().Value()
|
|
|
|
output, ok := outInterface.(*transaction.Output)
|
|
|
|
if !ok {
|
2020-02-26 07:58:20 +00:00
|
|
|
txio, ok := outInterface.(transaction.InOut)
|
2019-10-11 14:00:11 +00:00
|
|
|
if !ok {
|
2020-02-26 07:58:20 +00:00
|
|
|
return nil, fmt.Errorf("type mismatch: %T is not an Output or InOut", outInterface)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
2020-02-26 07:58:20 +00:00
|
|
|
output = &txio.Out
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
return output, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// outputGetAssetId returns asset ID from the given output.
|
2020-04-08 10:29:15 +00:00
|
|
|
func outputGetAssetID(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
output, err := popOutputFromVM(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-27 09:23:18 +00:00
|
|
|
v.Estack().PushVal(output.AssetID.BytesBE())
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// outputGetScriptHash returns scripthash from the given output.
|
2020-04-08 10:29:15 +00:00
|
|
|
func outputGetScriptHash(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
output, err := popOutputFromVM(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-27 09:20:31 +00:00
|
|
|
v.Estack().PushVal(output.ScriptHash.BytesBE())
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// outputGetValue returns value (amount) from the given output.
|
2020-04-08 10:29:15 +00:00
|
|
|
func outputGetValue(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
output, err := popOutputFromVM(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(int64(output.Amount))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// attrGetData returns tx attribute data.
|
2020-04-08 10:29:15 +00:00
|
|
|
func attrGetData(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
attrInterface := v.Estack().Pop().Value()
|
|
|
|
attr, ok := attrInterface.(*transaction.Attribute)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not an attribute", attr)
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(attr.Data)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// attrGetData returns tx attribute usage field.
|
2020-04-08 10:29:15 +00:00
|
|
|
func attrGetUsage(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
attrInterface := v.Estack().Pop().Value()
|
|
|
|
attr, ok := attrInterface.(*transaction.Attribute)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not an attribute", attr)
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(int(attr.Usage))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// bcGetAccount returns or creates an account.
|
2020-04-08 10:29:15 +00:00
|
|
|
func bcGetAccount(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
accbytes := v.Estack().Pop().Bytes()
|
2019-11-27 09:20:31 +00:00
|
|
|
acchash, err := util.Uint160DecodeBytesBE(accbytes)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-13 14:10:51 +00:00
|
|
|
acc, err := ic.dao.GetAccountStateOrNew(acchash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
v.Estack().PushVal(vm.NewInteropItem(acc))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// bcGetAsset returns an asset.
|
2020-04-08 10:29:15 +00:00
|
|
|
func bcGetAsset(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
asbytes := v.Estack().Pop().Bytes()
|
2019-11-27 09:23:18 +00:00
|
|
|
ashash, err := util.Uint256DecodeBytesBE(asbytes)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-13 14:10:51 +00:00
|
|
|
as, err := ic.dao.GetAssetState(ashash)
|
|
|
|
if err != nil {
|
2019-10-11 14:00:11 +00:00
|
|
|
return errors.New("asset not found")
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(vm.NewInteropItem(as))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// accountGetBalance returns balance for a given account.
|
2020-04-08 10:29:15 +00:00
|
|
|
func accountGetBalance(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
accInterface := v.Estack().Pop().Value()
|
2019-11-28 16:06:09 +00:00
|
|
|
acc, ok := accInterface.(*state.Account)
|
2019-10-11 14:00:11 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not an account state", acc)
|
|
|
|
}
|
|
|
|
asbytes := v.Estack().Pop().Bytes()
|
2019-11-27 09:23:18 +00:00
|
|
|
ashash, err := util.Uint256DecodeBytesBE(asbytes)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-15 17:35:09 +00:00
|
|
|
balance, ok := acc.GetBalanceValues()[ashash]
|
2019-10-11 14:00:11 +00:00
|
|
|
if !ok {
|
|
|
|
balance = util.Fixed8(0)
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(int64(balance))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// accountGetScriptHash returns script hash of a given account.
|
2020-04-08 10:29:15 +00:00
|
|
|
func accountGetScriptHash(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
accInterface := v.Estack().Pop().Value()
|
2019-11-28 16:06:09 +00:00
|
|
|
acc, ok := accInterface.(*state.Account)
|
2019-10-11 14:00:11 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not an account state", acc)
|
|
|
|
}
|
2019-11-27 09:20:31 +00:00
|
|
|
v.Estack().PushVal(acc.ScriptHash.BytesBE())
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// accountGetVotes returns votes of a given account.
|
2020-04-08 10:29:15 +00:00
|
|
|
func accountGetVotes(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
accInterface := v.Estack().Pop().Value()
|
2019-11-28 16:06:09 +00:00
|
|
|
acc, ok := accInterface.(*state.Account)
|
2019-10-11 14:00:11 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not an account state", acc)
|
|
|
|
}
|
|
|
|
if len(acc.Votes) > vm.MaxArraySize {
|
|
|
|
return errors.New("too many votes")
|
|
|
|
}
|
|
|
|
votes := make([]vm.StackItem, 0, len(acc.Votes))
|
|
|
|
for _, key := range acc.Votes {
|
|
|
|
votes = append(votes, vm.NewByteArrayItem(key.Bytes()))
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(votes)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// accountIsStandard checks whether given account is standard.
|
2020-04-08 10:29:15 +00:00
|
|
|
func accountIsStandard(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
accbytes := v.Estack().Pop().Bytes()
|
2019-11-27 09:20:31 +00:00
|
|
|
acchash, err := util.Uint160DecodeBytesBE(accbytes)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-13 14:10:51 +00:00
|
|
|
contract, err := ic.dao.GetContractState(acchash)
|
|
|
|
res := err != nil || vm.IsStandardContract(contract.Script)
|
2019-10-11 14:00:11 +00:00
|
|
|
v.Estack().PushVal(res)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// storageFind finds stored key-value pair.
|
2020-04-08 10:29:15 +00:00
|
|
|
func storageFind(ic *interopContext, 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
|
|
|
|
}
|
|
|
|
prefix := string(v.Estack().Pop().Bytes())
|
2019-12-13 14:10:51 +00:00
|
|
|
siMap, err := ic.dao.GetStorageItems(stc.ScriptHash)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-26 11:34:38 +00:00
|
|
|
|
2020-03-31 10:30:38 +00:00
|
|
|
filteredMap := vm.NewMapItem()
|
2019-10-11 14:00:11 +00:00
|
|
|
for k, v := range siMap {
|
|
|
|
if strings.HasPrefix(k, prefix) {
|
2020-03-31 10:30:38 +00:00
|
|
|
filteredMap.Add(vm.NewByteArrayItem([]byte(k)),
|
|
|
|
vm.NewByteArrayItem(v.Value))
|
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:29:15 +00:00
|
|
|
func createContractStateFromVM(ic *interopContext, v *vm.VM) (*state.Contract, error) {
|
2019-12-04 09:27:04 +00:00
|
|
|
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:29:15 +00:00
|
|
|
func contractCreate(ic *interopContext, v *vm.VM) error {
|
|
|
|
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
|
|
|
}
|
2019-12-13 14:10:51 +00:00
|
|
|
contract, err := ic.dao.GetContractState(newcontract.ScriptHash())
|
|
|
|
if err != nil {
|
2019-10-11 14:00:11 +00:00
|
|
|
contract = newcontract
|
2019-11-25 17:39:11 +00:00
|
|
|
err := ic.dao.PutContractState(contract)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(vm.NewInteropItem(contract))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// contractGetScript returns a script associated with a contract.
|
2020-04-08 10:29:15 +00:00
|
|
|
func contractGetScript(ic *interopContext, 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:29:15 +00:00
|
|
|
func contractIsPayable(ic *interopContext, 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:29:15 +00:00
|
|
|
func contractMigrate(ic *interopContext, v *vm.VM) error {
|
|
|
|
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
|
|
|
}
|
2019-12-13 14:10:51 +00:00
|
|
|
contract, err := ic.dao.GetContractState(newcontract.ScriptHash())
|
|
|
|
if err != nil {
|
2019-10-11 14:00:11 +00:00
|
|
|
contract = newcontract
|
2019-11-25 17:39:11 +00:00
|
|
|
err := ic.dao.PutContractState(contract)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if contract.HasStorage() {
|
|
|
|
hash := getContextScriptHash(v, 0)
|
2019-12-13 14:10:51 +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-03-16 08:52:09 +00:00
|
|
|
err = ic.dao.PutStorageItem(contract.ScriptHash(), []byte(k), v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(vm.NewInteropItem(contract))
|
2020-04-08 10:29:15 +00:00
|
|
|
return contractDestroy(ic, v)
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// assetCreate creates an asset.
|
2020-04-08 10:29:15 +00:00
|
|
|
func assetCreate(ic *interopContext, v *vm.VM) error {
|
2019-12-04 09:27:04 +00:00
|
|
|
if ic.trigger != trigger.Application {
|
2019-10-11 14:00:11 +00:00
|
|
|
return errors.New("can't create asset when not triggered by an application")
|
|
|
|
}
|
|
|
|
atype := transaction.AssetType(v.Estack().Pop().BigInt().Int64())
|
|
|
|
switch atype {
|
|
|
|
case transaction.Currency, transaction.Share, transaction.Invoice, transaction.Token:
|
|
|
|
// ok
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("wrong asset type: %x", atype)
|
|
|
|
}
|
|
|
|
name := string(v.Estack().Pop().Bytes())
|
|
|
|
if len(name) > MaxAssetNameLen {
|
|
|
|
return errors.New("too big name")
|
|
|
|
}
|
|
|
|
amount := util.Fixed8(v.Estack().Pop().BigInt().Int64())
|
|
|
|
if amount == util.Fixed8(0) {
|
|
|
|
return errors.New("asset amount can't be zero")
|
|
|
|
}
|
|
|
|
if amount < -util.Satoshi() {
|
|
|
|
return errors.New("asset amount can't be negative (except special -Satoshi value")
|
|
|
|
}
|
|
|
|
if atype == transaction.Invoice && amount != -util.Satoshi() {
|
|
|
|
return errors.New("invoice assets can only have -Satoshi amount")
|
|
|
|
}
|
|
|
|
precision := byte(v.Estack().Pop().BigInt().Int64())
|
|
|
|
if precision > MaxAssetPrecision {
|
|
|
|
return fmt.Errorf("can't have asset precision of more than %d", MaxAssetPrecision)
|
|
|
|
}
|
|
|
|
if atype == transaction.Share && precision != 0 {
|
|
|
|
return errors.New("share assets can only have zero precision")
|
|
|
|
}
|
|
|
|
if amount != -util.Satoshi() && (int64(amount)%int64(math.Pow10(int(MaxAssetPrecision-precision))) != 0) {
|
|
|
|
return errors.New("given asset amount has fractional component")
|
|
|
|
}
|
|
|
|
owner := &keys.PublicKey{}
|
|
|
|
err := owner.DecodeBytes(v.Estack().Pop().Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return gherr.Wrap(err, "failed to get owner key")
|
|
|
|
}
|
|
|
|
if owner.IsInfinity() {
|
|
|
|
return errors.New("can't have infinity as an owner key")
|
|
|
|
}
|
2020-04-08 10:29:15 +00:00
|
|
|
witnessOk, err := checkKeyedWitness(ic, owner)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !witnessOk {
|
|
|
|
return errors.New("witness check didn't succeed")
|
|
|
|
}
|
2019-11-27 09:20:31 +00:00
|
|
|
admin, err := util.Uint160DecodeBytesBE(v.Estack().Pop().Bytes())
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return gherr.Wrap(err, "failed to get admin")
|
|
|
|
}
|
2019-11-27 09:20:31 +00:00
|
|
|
issuer, err := util.Uint160DecodeBytesBE(v.Estack().Pop().Bytes())
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return gherr.Wrap(err, "failed to get issuer")
|
|
|
|
}
|
2019-11-28 16:06:09 +00:00
|
|
|
asset := &state.Asset{
|
2019-10-11 14:00:11 +00:00
|
|
|
ID: ic.tx.Hash(),
|
|
|
|
AssetType: atype,
|
|
|
|
Name: name,
|
|
|
|
Amount: amount,
|
|
|
|
Precision: precision,
|
2019-12-09 15:33:04 +00:00
|
|
|
Owner: *owner,
|
2019-10-11 14:00:11 +00:00
|
|
|
Admin: admin,
|
|
|
|
Issuer: issuer,
|
|
|
|
Expiration: ic.bc.BlockHeight() + DefaultAssetLifetime,
|
|
|
|
}
|
2019-11-25 17:39:11 +00:00
|
|
|
err = ic.dao.PutAssetState(asset)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
2020-04-07 09:41:12 +00:00
|
|
|
return gherr.Wrap(err, "failed to Store asset")
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
v.Estack().PushVal(vm.NewInteropItem(asset))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// assetGetAdmin returns asset admin.
|
2020-04-08 10:29:15 +00:00
|
|
|
func assetGetAdmin(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
asInterface := v.Estack().Pop().Value()
|
2019-11-28 16:06:09 +00:00
|
|
|
as, ok := asInterface.(*state.Asset)
|
2019-10-11 14:00:11 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not an asset state", as)
|
|
|
|
}
|
2019-11-27 09:20:31 +00:00
|
|
|
v.Estack().PushVal(as.Admin.BytesBE())
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// assetGetAmount returns the overall amount of asset available.
|
2020-04-08 10:29:15 +00:00
|
|
|
func assetGetAmount(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
asInterface := v.Estack().Pop().Value()
|
2019-11-28 16:06:09 +00:00
|
|
|
as, ok := asInterface.(*state.Asset)
|
2019-10-11 14:00:11 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not an asset state", as)
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(int64(as.Amount))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// assetGetAssetId returns the id of an asset.
|
2020-04-08 10:29:15 +00:00
|
|
|
func assetGetAssetID(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
asInterface := v.Estack().Pop().Value()
|
2019-11-28 16:06:09 +00:00
|
|
|
as, ok := asInterface.(*state.Asset)
|
2019-10-11 14:00:11 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not an asset state", as)
|
|
|
|
}
|
2019-11-27 09:23:18 +00:00
|
|
|
v.Estack().PushVal(as.ID.BytesBE())
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// assetGetAssetType returns type of an asset.
|
2020-04-08 10:29:15 +00:00
|
|
|
func assetGetAssetType(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
asInterface := v.Estack().Pop().Value()
|
2019-11-28 16:06:09 +00:00
|
|
|
as, ok := asInterface.(*state.Asset)
|
2019-10-11 14:00:11 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not an asset state", as)
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(int(as.AssetType))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// assetGetAvailable returns available (not yet issued) amount of asset.
|
2020-04-08 10:29:15 +00:00
|
|
|
func assetGetAvailable(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
asInterface := v.Estack().Pop().Value()
|
2019-11-28 16:06:09 +00:00
|
|
|
as, ok := asInterface.(*state.Asset)
|
2019-10-11 14:00:11 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not an asset state", as)
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(int(as.Available))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// assetGetIssuer returns issuer of an asset.
|
2020-04-08 10:29:15 +00:00
|
|
|
func assetGetIssuer(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
asInterface := v.Estack().Pop().Value()
|
2019-11-28 16:06:09 +00:00
|
|
|
as, ok := asInterface.(*state.Asset)
|
2019-10-11 14:00:11 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not an asset state", as)
|
|
|
|
}
|
2019-11-27 09:20:31 +00:00
|
|
|
v.Estack().PushVal(as.Issuer.BytesBE())
|
2019-10-11 14:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// assetGetOwner returns owner of an asset.
|
2020-04-08 10:29:15 +00:00
|
|
|
func assetGetOwner(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
asInterface := v.Estack().Pop().Value()
|
2019-11-28 16:06:09 +00:00
|
|
|
as, ok := asInterface.(*state.Asset)
|
2019-10-11 14:00:11 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not an asset state", as)
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(as.Owner.Bytes())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// assetGetPrecision returns precision used to measure this asset.
|
2020-04-08 10:29:15 +00:00
|
|
|
func assetGetPrecision(ic *interopContext, v *vm.VM) error {
|
2019-10-11 14:00:11 +00:00
|
|
|
asInterface := v.Estack().Pop().Value()
|
2019-11-28 16:06:09 +00:00
|
|
|
as, ok := asInterface.(*state.Asset)
|
2019-10-11 14:00:11 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not an asset state", as)
|
|
|
|
}
|
|
|
|
v.Estack().PushVal(int(as.Precision))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// assetRenew updates asset expiration date.
|
2020-04-08 10:29:15 +00:00
|
|
|
func assetRenew(ic *interopContext, v *vm.VM) error {
|
2019-12-04 09:27:04 +00:00
|
|
|
if ic.trigger != trigger.Application {
|
2019-10-11 14:00:11 +00:00
|
|
|
return errors.New("can't create asset when not triggered by an application")
|
|
|
|
}
|
|
|
|
asInterface := v.Estack().Pop().Value()
|
2019-11-28 16:06:09 +00:00
|
|
|
as, ok := asInterface.(*state.Asset)
|
2019-10-11 14:00:11 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%T is not an asset state", as)
|
|
|
|
}
|
|
|
|
years := byte(v.Estack().Pop().BigInt().Int64())
|
|
|
|
// Not sure why C# code regets an asset from the Store, but we also do it.
|
2019-12-13 14:10:51 +00:00
|
|
|
asset, err := ic.dao.GetAssetState(as.ID)
|
|
|
|
if err != nil {
|
2019-10-11 14:00:11 +00:00
|
|
|
return errors.New("can't renew non-existent asset")
|
|
|
|
}
|
|
|
|
if asset.Expiration < ic.bc.BlockHeight()+1 {
|
|
|
|
asset.Expiration = ic.bc.BlockHeight() + 1
|
|
|
|
}
|
|
|
|
expiration := uint64(asset.Expiration) + uint64(years)*BlocksPerYear
|
|
|
|
if expiration > math.MaxUint32 {
|
|
|
|
expiration = math.MaxUint32
|
|
|
|
}
|
|
|
|
asset.Expiration = uint32(expiration)
|
2019-12-13 14:10:51 +00:00
|
|
|
err = ic.dao.PutAssetState(asset)
|
2019-10-11 14:00:11 +00:00
|
|
|
if err != nil {
|
2020-04-07 09:41:12 +00:00
|
|
|
return gherr.Wrap(err, "failed to Store asset")
|
2019-10-11 14:00:11 +00:00
|
|
|
}
|
|
|
|
v.Estack().PushVal(expiration)
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-05 14:10:52 +00:00
|
|
|
|
|
|
|
// runtimeSerialize serializes top stack item into a ByteArray.
|
2020-04-08 10:29:15 +00:00
|
|
|
func runtimeSerialize(_ *interopContext, 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:29:15 +00:00
|
|
|
func runtimeDeserialize(_ *interopContext, v *vm.VM) error {
|
2019-11-05 14:10:52 +00:00
|
|
|
return vm.RuntimeDeserialize(v)
|
|
|
|
}
|
2019-11-13 11:34:03 +00:00
|
|
|
|
|
|
|
// enumeratorConcat concatenates 2 enumerators into a single one.
|
2020-04-08 10:29:15 +00:00
|
|
|
func enumeratorConcat(_ *interopContext, v *vm.VM) error {
|
2019-11-13 11:34:03 +00:00
|
|
|
return vm.EnumeratorConcat(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// enumeratorCreate creates an enumerator from an array-like stack item.
|
2020-04-08 10:29:15 +00:00
|
|
|
func enumeratorCreate(_ *interopContext, v *vm.VM) error {
|
2019-11-13 11:34:03 +00:00
|
|
|
return vm.EnumeratorCreate(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// enumeratorNext advances the enumerator, pushes true if is it was successful
|
|
|
|
// and false otherwise.
|
2020-04-08 10:29:15 +00:00
|
|
|
func enumeratorNext(_ *interopContext, v *vm.VM) error {
|
2019-11-13 11:34:03 +00:00
|
|
|
return vm.EnumeratorNext(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// enumeratorValue returns the current value of the enumerator.
|
2020-04-08 10:29:15 +00:00
|
|
|
func enumeratorValue(_ *interopContext, v *vm.VM) error {
|
2019-11-13 11:34:03 +00:00
|
|
|
return vm.EnumeratorValue(v)
|
|
|
|
}
|
2019-11-13 12:29:27 +00:00
|
|
|
|
|
|
|
// iteratorConcat concatenates 2 iterators into a single one.
|
2020-04-08 10:29:15 +00:00
|
|
|
func iteratorConcat(_ *interopContext, v *vm.VM) error {
|
2019-11-13 12:29:27 +00:00
|
|
|
return vm.IteratorConcat(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// iteratorCreate creates an iterator from array-like or map stack item.
|
2020-04-08 10:29:15 +00:00
|
|
|
func iteratorCreate(_ *interopContext, v *vm.VM) error {
|
2019-11-13 12:29:27 +00:00
|
|
|
return vm.IteratorCreate(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// iteratorKey returns current iterator key.
|
2020-04-08 10:29:15 +00:00
|
|
|
func iteratorKey(_ *interopContext, v *vm.VM) error {
|
2019-11-13 12:29:27 +00:00
|
|
|
return vm.IteratorKey(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// iteratorKeys returns keys of the iterator.
|
2020-04-08 10:29:15 +00:00
|
|
|
func iteratorKeys(_ *interopContext, v *vm.VM) error {
|
2019-11-13 12:29:27 +00:00
|
|
|
return vm.IteratorKeys(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// iteratorValues returns values of the iterator.
|
2020-04-08 10:29:15 +00:00
|
|
|
func iteratorValues(_ *interopContext, v *vm.VM) error {
|
2019-11-13 12:29:27 +00:00
|
|
|
return vm.IteratorValues(v)
|
|
|
|
}
|