2020-12-08 15:28:00 +00:00
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
2020-12-13 15:26:35 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2020-12-08 15:28:00 +00:00
|
|
|
"fmt"
|
2020-12-13 15:26:35 +00:00
|
|
|
"math"
|
|
|
|
"math/big"
|
2020-12-15 10:53:35 +00:00
|
|
|
"sync"
|
2020-12-08 15:28:00 +00:00
|
|
|
|
2020-12-13 15:26:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/dao"
|
2020-12-08 15:28:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2020-12-13 15:26:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/contract"
|
2020-12-13 18:25:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
2020-12-08 15:28:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2020-12-15 10:53:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
2020-12-13 15:26:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
2020-12-15 10:53:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-12-13 15:26:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
2020-12-29 10:45:49 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2020-12-13 15:26:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-12-08 15:28:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Management is contract-managing native contract.
|
|
|
|
type Management struct {
|
|
|
|
interop.ContractMD
|
2021-01-21 12:05:15 +00:00
|
|
|
NEO *NEO
|
2020-12-15 10:53:35 +00:00
|
|
|
|
|
|
|
mtx sync.RWMutex
|
|
|
|
contracts map[util.Uint160]*state.Contract
|
2020-12-08 15:28:00 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 15:26:35 +00:00
|
|
|
// StoragePrice is the price to pay for 1 byte of storage.
|
|
|
|
const StoragePrice = 100000
|
|
|
|
|
2020-12-08 15:28:00 +00:00
|
|
|
const (
|
2021-01-15 21:17:31 +00:00
|
|
|
managementContractID = -1
|
|
|
|
|
2020-12-13 15:26:35 +00:00
|
|
|
prefixContract = 8
|
2020-12-16 13:11:12 +00:00
|
|
|
|
2020-12-16 13:41:55 +00:00
|
|
|
defaultMinimumDeploymentFee = 10_00000000
|
|
|
|
contractDeployNotificationName = "Deploy"
|
|
|
|
contractUpdateNotificationName = "Update"
|
|
|
|
contractDestroyNotificationName = "Destroy"
|
2020-12-08 15:28:00 +00:00
|
|
|
)
|
|
|
|
|
2020-12-16 13:11:12 +00:00
|
|
|
var (
|
|
|
|
errGasLimitExceeded = errors.New("gas limit exceeded")
|
|
|
|
|
|
|
|
keyNextAvailableID = []byte{15}
|
|
|
|
keyMinimumDeploymentFee = []byte{20}
|
|
|
|
)
|
2020-12-13 15:26:35 +00:00
|
|
|
|
|
|
|
// makeContractKey creates a key from account script hash.
|
|
|
|
func makeContractKey(h util.Uint160) []byte {
|
|
|
|
return makeUint160Key(prefixContract, h)
|
|
|
|
}
|
|
|
|
|
2020-12-08 15:28:00 +00:00
|
|
|
// newManagement creates new Management native contract.
|
|
|
|
func newManagement() *Management {
|
2020-12-15 10:53:35 +00:00
|
|
|
var m = &Management{
|
2021-01-15 21:17:31 +00:00
|
|
|
ContractMD: *interop.NewContractMD(nativenames.Management, managementContractID),
|
2020-12-15 10:53:35 +00:00
|
|
|
contracts: make(map[util.Uint160]*state.Contract),
|
|
|
|
}
|
2020-12-08 15:28:00 +00:00
|
|
|
|
2020-12-13 15:26:35 +00:00
|
|
|
desc := newDescriptor("getContract", smartcontract.ArrayType,
|
2021-01-12 15:06:27 +00:00
|
|
|
manifest.NewParameter("hash", smartcontract.Hash160Type))
|
2020-12-29 10:45:49 +00:00
|
|
|
md := newMethodAndPrice(m.getContract, 1000000, callflag.ReadStates)
|
2020-12-13 15:26:35 +00:00
|
|
|
m.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("deploy", smartcontract.ArrayType,
|
|
|
|
manifest.NewParameter("script", smartcontract.ByteArrayType),
|
|
|
|
manifest.NewParameter("manifest", smartcontract.ByteArrayType))
|
2020-12-29 10:45:49 +00:00
|
|
|
md = newMethodAndPrice(m.deploy, 0, callflag.WriteStates|callflag.AllowNotify)
|
2020-12-13 15:26:35 +00:00
|
|
|
m.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("update", smartcontract.VoidType,
|
|
|
|
manifest.NewParameter("script", smartcontract.ByteArrayType),
|
|
|
|
manifest.NewParameter("manifest", smartcontract.ByteArrayType))
|
2020-12-29 10:45:49 +00:00
|
|
|
md = newMethodAndPrice(m.update, 0, callflag.WriteStates|callflag.AllowNotify)
|
2020-12-13 15:26:35 +00:00
|
|
|
m.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("destroy", smartcontract.VoidType)
|
2020-12-29 10:45:49 +00:00
|
|
|
md = newMethodAndPrice(m.destroy, 1000000, callflag.WriteStates|callflag.AllowNotify)
|
2020-12-13 15:26:35 +00:00
|
|
|
m.AddMethod(md, desc)
|
|
|
|
|
2020-12-16 13:11:12 +00:00
|
|
|
desc = newDescriptor("getMinimumDeploymentFee", smartcontract.IntegerType)
|
2020-12-29 10:45:49 +00:00
|
|
|
md = newMethodAndPrice(m.getMinimumDeploymentFee, 100_0000, callflag.ReadStates)
|
2020-12-16 13:11:12 +00:00
|
|
|
m.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("setMinimumDeploymentFee", smartcontract.BoolType,
|
|
|
|
manifest.NewParameter("value", smartcontract.IntegerType))
|
2020-12-29 10:45:49 +00:00
|
|
|
md = newMethodAndPrice(m.setMinimumDeploymentFee, 300_0000, callflag.WriteStates)
|
2020-12-16 13:11:12 +00:00
|
|
|
m.AddMethod(md, desc)
|
2020-12-16 13:41:55 +00:00
|
|
|
|
|
|
|
hashParam := manifest.NewParameter("Hash", smartcontract.Hash160Type)
|
|
|
|
m.AddEvent(contractDeployNotificationName, hashParam)
|
|
|
|
m.AddEvent(contractUpdateNotificationName, hashParam)
|
|
|
|
m.AddEvent(contractDestroyNotificationName, hashParam)
|
2020-12-08 15:28:00 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2020-12-13 15:26:35 +00:00
|
|
|
// getContract is an implementation of public getContract method, it's run under
|
|
|
|
// VM protections, so it's OK for it to panic instead of returning errors.
|
|
|
|
func (m *Management) getContract(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
hashBytes, err := args[0].TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
hash, err := util.Uint160DecodeBytesBE(hashBytes)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
ctr, err := m.GetContract(ic.DAO, hash)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return contractToStack(ctr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetContract returns contract with given hash from given DAO.
|
|
|
|
func (m *Management) GetContract(d dao.DAO, hash util.Uint160) (*state.Contract, error) {
|
2020-12-15 10:53:35 +00:00
|
|
|
m.mtx.RLock()
|
|
|
|
cs, ok := m.contracts[hash]
|
|
|
|
m.mtx.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
return nil, storage.ErrKeyNotFound
|
|
|
|
} else if cs != nil {
|
|
|
|
return cs, nil
|
|
|
|
}
|
|
|
|
return m.getContractFromDAO(d, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Management) getContractFromDAO(d dao.DAO, hash util.Uint160) (*state.Contract, error) {
|
2020-12-13 15:26:35 +00:00
|
|
|
contract := new(state.Contract)
|
|
|
|
key := makeContractKey(hash)
|
|
|
|
err := getSerializableFromDAO(m.ContractID, d, key, contract)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return contract, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLimitedSlice(arg stackitem.Item, max int) ([]byte, error) {
|
|
|
|
_, isNull := arg.(stackitem.Null)
|
|
|
|
if isNull {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
b, err := arg.TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
l := len(b)
|
|
|
|
if l == 0 {
|
|
|
|
return nil, errors.New("empty")
|
|
|
|
} else if l > max {
|
|
|
|
return nil, fmt.Errorf("len is %d (max %d)", l, max)
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getNefAndManifestFromItems converts input arguments into NEF and manifest
|
|
|
|
// adding appropriate deployment GAS price and sanitizing inputs.
|
2020-12-16 13:11:12 +00:00
|
|
|
func (m *Management) getNefAndManifestFromItems(ic *interop.Context, args []stackitem.Item, isDeploy bool) (*nef.File, *manifest.Manifest, error) {
|
2020-12-13 15:26:35 +00:00
|
|
|
nefBytes, err := getLimitedSlice(args[0], math.MaxInt32) // Upper limits are checked during NEF deserialization.
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("invalid NEF file: %w", err)
|
|
|
|
}
|
|
|
|
manifestBytes, err := getLimitedSlice(args[1], manifest.MaxManifestSize)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("invalid manifest: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-12-16 13:11:12 +00:00
|
|
|
gas := ic.Chain.GetPolicer().GetStoragePrice() * int64(len(nefBytes)+len(manifestBytes))
|
|
|
|
if isDeploy {
|
|
|
|
fee := m.GetMinimumDeploymentFee(ic.DAO)
|
|
|
|
if fee > gas {
|
|
|
|
gas = fee
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ic.VM.AddGas(gas) {
|
2020-12-13 15:26:35 +00:00
|
|
|
return nil, nil, errGasLimitExceeded
|
|
|
|
}
|
|
|
|
var resManifest *manifest.Manifest
|
|
|
|
var resNef *nef.File
|
|
|
|
if nefBytes != nil {
|
|
|
|
nf, err := nef.FileFromBytes(nefBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("invalid NEF file: %w", err)
|
|
|
|
}
|
|
|
|
resNef = &nf
|
|
|
|
}
|
|
|
|
if manifestBytes != nil {
|
|
|
|
resManifest = new(manifest.Manifest)
|
|
|
|
err := json.Unmarshal(manifestBytes, resManifest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("invalid manifest: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resNef, resManifest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// deploy is an implementation of public deploy method, it's run under
|
|
|
|
// VM protections, so it's OK for it to panic instead of returning errors.
|
|
|
|
func (m *Management) deploy(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
2020-12-16 13:11:12 +00:00
|
|
|
neff, manif, err := m.getNefAndManifestFromItems(ic, args, true)
|
2020-12-13 15:26:35 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if neff == nil {
|
|
|
|
panic(errors.New("no valid NEF provided"))
|
|
|
|
}
|
|
|
|
if manif == nil {
|
|
|
|
panic(errors.New("no valid manifest provided"))
|
|
|
|
}
|
|
|
|
if ic.Tx == nil {
|
|
|
|
panic(errors.New("no transaction provided"))
|
|
|
|
}
|
|
|
|
newcontract, err := m.Deploy(ic.DAO, ic.Tx.Sender(), neff, manif)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-12-29 10:44:07 +00:00
|
|
|
m.callDeploy(ic, newcontract, false)
|
2020-12-16 13:41:55 +00:00
|
|
|
m.emitNotification(ic, contractDeployNotificationName, newcontract.Hash)
|
2020-12-13 15:26:35 +00:00
|
|
|
return contractToStack(newcontract)
|
2020-12-15 10:53:35 +00:00
|
|
|
}
|
2020-12-13 15:26:35 +00:00
|
|
|
|
2020-12-15 10:53:35 +00:00
|
|
|
func (m *Management) markUpdated(h util.Uint160) {
|
|
|
|
m.mtx.Lock()
|
|
|
|
// Just set it to nil, to refresh cache in `PostPersist`.
|
|
|
|
m.contracts[h] = nil
|
|
|
|
m.mtx.Unlock()
|
2020-12-13 15:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deploy creates contract's hash/ID and saves new contract into the given DAO.
|
2020-12-16 13:41:55 +00:00
|
|
|
// It doesn't run _deploy method and doesn't emit notification.
|
2020-12-13 15:26:35 +00:00
|
|
|
func (m *Management) Deploy(d dao.DAO, sender util.Uint160, neff *nef.File, manif *manifest.Manifest) (*state.Contract, error) {
|
2021-01-22 09:22:48 +00:00
|
|
|
h := state.CreateContractHash(sender, neff.Checksum, manif.Name)
|
2020-12-13 15:26:35 +00:00
|
|
|
key := makeContractKey(h)
|
|
|
|
si := d.GetStorageItem(m.ContractID, key)
|
|
|
|
if si != nil {
|
|
|
|
return nil, errors.New("contract already exists")
|
|
|
|
}
|
|
|
|
id, err := m.getNextContractID(d)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !manif.IsValid(h) {
|
|
|
|
return nil, errors.New("invalid manifest for this contract")
|
|
|
|
}
|
|
|
|
newcontract := &state.Contract{
|
|
|
|
ID: id,
|
|
|
|
Hash: h,
|
2021-01-13 12:34:10 +00:00
|
|
|
NEF: *neff,
|
2020-12-13 15:26:35 +00:00
|
|
|
Manifest: *manif,
|
|
|
|
}
|
|
|
|
err = m.PutContractState(d, newcontract)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-15 10:53:35 +00:00
|
|
|
m.markUpdated(newcontract.Hash)
|
2020-12-13 15:26:35 +00:00
|
|
|
return newcontract, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// update is an implementation of public update method, it's run under
|
|
|
|
// VM protections, so it's OK for it to panic instead of returning errors.
|
|
|
|
func (m *Management) update(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
2020-12-16 13:11:12 +00:00
|
|
|
neff, manif, err := m.getNefAndManifestFromItems(ic, args, false)
|
2020-12-13 15:26:35 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if neff == nil && manif == nil {
|
|
|
|
panic(errors.New("both NEF and manifest are nil"))
|
|
|
|
}
|
|
|
|
contract, err := m.Update(ic.DAO, ic.VM.GetCallingScriptHash(), neff, manif)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-12-29 10:44:07 +00:00
|
|
|
m.callDeploy(ic, contract, true)
|
2020-12-16 13:41:55 +00:00
|
|
|
m.emitNotification(ic, contractUpdateNotificationName, contract.Hash)
|
2020-12-13 15:26:35 +00:00
|
|
|
return stackitem.Null{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates contract's script and/or manifest in the given DAO.
|
2020-12-16 13:41:55 +00:00
|
|
|
// It doesn't run _deploy method and doesn't emit notification.
|
2020-12-13 15:26:35 +00:00
|
|
|
func (m *Management) Update(d dao.DAO, hash util.Uint160, neff *nef.File, manif *manifest.Manifest) (*state.Contract, error) {
|
|
|
|
contract, err := m.GetContract(d, hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("contract doesn't exist")
|
|
|
|
}
|
|
|
|
// if NEF was provided, update the contract script
|
|
|
|
if neff != nil {
|
2020-12-15 10:53:35 +00:00
|
|
|
m.markUpdated(hash)
|
2021-01-13 12:34:10 +00:00
|
|
|
contract.NEF = *neff
|
2020-12-13 15:26:35 +00:00
|
|
|
}
|
|
|
|
// if manifest was provided, update the contract manifest
|
|
|
|
if manif != nil {
|
2020-12-15 10:53:35 +00:00
|
|
|
if !manif.IsValid(contract.Hash) {
|
2020-12-13 15:26:35 +00:00
|
|
|
return nil, errors.New("invalid manifest for this contract")
|
|
|
|
}
|
2020-12-15 10:53:35 +00:00
|
|
|
m.markUpdated(hash)
|
|
|
|
contract.Manifest = *manif
|
2020-12-13 15:26:35 +00:00
|
|
|
}
|
|
|
|
contract.UpdateCounter++
|
|
|
|
err = m.PutContractState(d, contract)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return contract, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// destroy is an implementation of destroy update method, it's run under
|
|
|
|
// VM protections, so it's OK for it to panic instead of returning errors.
|
|
|
|
func (m *Management) destroy(ic *interop.Context, sis []stackitem.Item) stackitem.Item {
|
|
|
|
hash := ic.VM.GetCallingScriptHash()
|
|
|
|
err := m.Destroy(ic.DAO, hash)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-12-16 13:41:55 +00:00
|
|
|
m.emitNotification(ic, contractDestroyNotificationName, hash)
|
2020-12-13 15:26:35 +00:00
|
|
|
return stackitem.Null{}
|
|
|
|
}
|
|
|
|
|
2020-12-16 13:41:55 +00:00
|
|
|
// Destroy drops given contract from DAO along with its storage. It doesn't emit notification.
|
2020-12-13 15:26:35 +00:00
|
|
|
func (m *Management) Destroy(d dao.DAO, hash util.Uint160) error {
|
|
|
|
contract, err := m.GetContract(d, hash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
key := makeContractKey(hash)
|
|
|
|
err = d.DeleteStorageItem(m.ContractID, key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = d.DeleteContractID(contract.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
siMap, err := d.GetStorageItems(contract.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for k := range siMap {
|
|
|
|
err := d.DeleteStorageItem(contract.ID, []byte(k))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-12-15 10:53:35 +00:00
|
|
|
m.markUpdated(hash)
|
2020-12-13 15:26:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-16 13:11:12 +00:00
|
|
|
func (m *Management) getMinimumDeploymentFee(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
|
|
|
return stackitem.NewBigInteger(big.NewInt(m.GetMinimumDeploymentFee(ic.DAO)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMinimumDeploymentFee returns the minimum required fee for contract deploy.
|
|
|
|
func (m *Management) GetMinimumDeploymentFee(dao dao.DAO) int64 {
|
2020-12-22 13:09:16 +00:00
|
|
|
return int64(getUint32WithKey(m.ContractID, dao, keyMinimumDeploymentFee, defaultMinimumDeploymentFee))
|
2020-12-16 13:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Management) setMinimumDeploymentFee(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
2020-12-22 13:09:16 +00:00
|
|
|
value := toUint32(args[0])
|
2020-12-16 13:11:12 +00:00
|
|
|
if value < 0 {
|
|
|
|
panic(fmt.Errorf("MinimumDeploymentFee cannot be negative"))
|
|
|
|
}
|
2021-01-21 12:05:15 +00:00
|
|
|
if !m.NEO.checkCommittee(ic) {
|
2020-12-16 13:11:12 +00:00
|
|
|
return stackitem.NewBool(false)
|
|
|
|
}
|
2021-01-21 12:05:15 +00:00
|
|
|
err := setUint32WithKey(m.ContractID, ic.DAO, keyMinimumDeploymentFee, value)
|
2020-12-16 13:11:12 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return stackitem.NewBool(true)
|
|
|
|
}
|
|
|
|
|
2020-12-29 10:44:07 +00:00
|
|
|
func (m *Management) callDeploy(ic *interop.Context, cs *state.Contract, isUpdate bool) {
|
2021-01-26 14:37:34 +00:00
|
|
|
md := cs.Manifest.ABI.GetMethod(manifest.MethodDeploy, 1)
|
2020-12-13 15:26:35 +00:00
|
|
|
if md != nil {
|
2020-12-29 10:44:07 +00:00
|
|
|
err := contract.CallFromNative(ic, m.Hash, cs, manifest.MethodDeploy,
|
|
|
|
[]stackitem.Item{stackitem.NewBool(isUpdate)}, false)
|
2020-12-13 15:26:35 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func contractToStack(cs *state.Contract) stackitem.Item {
|
|
|
|
si, err := cs.ToStackItem()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("contract to stack item: %w", err))
|
|
|
|
}
|
|
|
|
return si
|
|
|
|
}
|
|
|
|
|
2020-12-08 15:28:00 +00:00
|
|
|
// Metadata implements Contract interface.
|
|
|
|
func (m *Management) Metadata() *interop.ContractMD {
|
|
|
|
return &m.ContractMD
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnPersist implements Contract interface.
|
|
|
|
func (m *Management) OnPersist(ic *interop.Context) error {
|
|
|
|
if ic.Block.Index != 0 { // We're only deploying at 0 at the moment.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, native := range ic.Natives {
|
|
|
|
md := native.Metadata()
|
|
|
|
|
|
|
|
cs := &state.Contract{
|
|
|
|
ID: md.ContractID,
|
|
|
|
Hash: md.Hash,
|
2021-01-13 12:34:10 +00:00
|
|
|
NEF: md.NEF,
|
2020-12-08 15:28:00 +00:00
|
|
|
Manifest: md.Manifest,
|
|
|
|
}
|
2020-12-13 15:26:35 +00:00
|
|
|
err := m.PutContractState(ic.DAO, cs)
|
|
|
|
if err != nil {
|
2020-12-08 15:28:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := native.Initialize(ic); err != nil {
|
|
|
|
return fmt.Errorf("initializing %s native contract: %w", md.Name, err)
|
|
|
|
}
|
2020-12-15 10:53:35 +00:00
|
|
|
m.mtx.Lock()
|
|
|
|
m.contracts[md.Hash] = cs
|
|
|
|
m.mtx.Unlock()
|
2020-12-08 15:28:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-15 10:53:35 +00:00
|
|
|
// InitializeCache initializes contract cache with the proper values from storage.
|
|
|
|
// Cache initialisation should be done apart from Initialize because Initialize is
|
|
|
|
// called only when deploying native contracts.
|
|
|
|
func (m *Management) InitializeCache(d dao.DAO) error {
|
|
|
|
m.mtx.Lock()
|
|
|
|
defer m.mtx.Unlock()
|
|
|
|
|
|
|
|
var initErr error
|
|
|
|
d.Seek(m.ContractID, []byte{prefixContract}, func(_, v []byte) {
|
|
|
|
var r = io.NewBinReaderFromBuf(v)
|
|
|
|
var si state.StorageItem
|
|
|
|
si.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
initErr = r.Err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var cs state.Contract
|
|
|
|
r = io.NewBinReaderFromBuf(si.Value)
|
|
|
|
cs.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
initErr = r.Err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m.contracts[cs.Hash] = &cs
|
|
|
|
})
|
|
|
|
return initErr
|
|
|
|
}
|
|
|
|
|
2020-12-08 15:28:00 +00:00
|
|
|
// PostPersist implements Contract interface.
|
2020-12-15 10:53:35 +00:00
|
|
|
func (m *Management) PostPersist(ic *interop.Context) error {
|
|
|
|
m.mtx.Lock()
|
|
|
|
for h, cs := range m.contracts {
|
|
|
|
if cs != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
newCs, err := m.getContractFromDAO(ic.DAO, h)
|
|
|
|
if err != nil {
|
|
|
|
// Contract was destroyed.
|
|
|
|
delete(m.contracts, h)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m.contracts[h] = newCs
|
|
|
|
}
|
|
|
|
m.mtx.Unlock()
|
2020-12-08 15:28:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize implements Contract interface.
|
2020-12-16 13:11:12 +00:00
|
|
|
func (m *Management) Initialize(ic *interop.Context) error {
|
2020-12-22 13:09:16 +00:00
|
|
|
return setUint32WithKey(m.ContractID, ic.DAO, keyMinimumDeploymentFee, defaultMinimumDeploymentFee)
|
2020-12-08 15:28:00 +00:00
|
|
|
}
|
2020-12-13 15:26:35 +00:00
|
|
|
|
|
|
|
// PutContractState saves given contract state into given DAO.
|
|
|
|
func (m *Management) PutContractState(d dao.DAO, cs *state.Contract) error {
|
|
|
|
key := makeContractKey(cs.Hash)
|
|
|
|
if err := putSerializableToDAO(m.ContractID, d, key, cs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-15 10:53:35 +00:00
|
|
|
m.markUpdated(cs.Hash)
|
2020-12-13 15:26:35 +00:00
|
|
|
if cs.UpdateCounter != 0 { // Update.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return d.PutContractID(cs.ID, cs.Hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Management) getNextContractID(d dao.DAO) (int32, error) {
|
|
|
|
var id = big.NewInt(1)
|
|
|
|
si := d.GetStorageItem(m.ContractID, keyNextAvailableID)
|
|
|
|
if si != nil {
|
|
|
|
id = bigint.FromBytes(si.Value)
|
|
|
|
} else {
|
|
|
|
si = new(state.StorageItem)
|
|
|
|
si.Value = make([]byte, 0, 2)
|
|
|
|
}
|
|
|
|
ret := int32(id.Int64())
|
|
|
|
id.Add(id, big.NewInt(1))
|
|
|
|
si.Value = bigint.ToPreallocatedBytes(id, si.Value)
|
|
|
|
return ret, d.PutStorageItem(m.ContractID, keyNextAvailableID, si)
|
|
|
|
}
|
2020-12-16 13:41:55 +00:00
|
|
|
|
|
|
|
func (m *Management) emitNotification(ic *interop.Context, name string, hash util.Uint160) {
|
|
|
|
ne := state.NotificationEvent{
|
|
|
|
ScriptHash: m.Hash,
|
|
|
|
Name: name,
|
|
|
|
Item: stackitem.NewArray([]stackitem.Item{addrToStackItem(&hash)}),
|
|
|
|
}
|
|
|
|
ic.Notifications = append(ic.Notifications, ne)
|
|
|
|
}
|