core: move interopContext to a separate package

This commit is contained in:
Evgenii Stratonikov 2020-04-08 13:35:39 +03:00
parent 90a08986d6
commit 7ffc6c0936
7 changed files with 225 additions and 204 deletions

View file

@ -12,6 +12,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/dao"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/mempool"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/core/storage"
@ -667,11 +668,11 @@ func (bc *Blockchain) storeBlock(block *block.Block) error {
err := v.Run()
if !v.HasFailed() {
_, err := systemInterop.dao.Persist()
_, err := systemInterop.DAO.Persist()
if err != nil {
return errors.Wrap(err, "failed to persist invocation results")
}
for _, note := range systemInterop.notifications {
for _, note := range systemInterop.Notifications {
arr, ok := note.Item.Value().([]vm.StackItem)
if !ok || len(arr) != 4 {
continue
@ -710,7 +711,7 @@ func (bc *Blockchain) storeBlock(block *block.Block) error {
VMState: v.State(),
GasConsumed: v.GasConsumed(),
Stack: v.Estack().ToContractParameters(),
Events: systemInterop.notifications,
Events: systemInterop.Notifications,
}
err = cache.PutAppExecResult(aer)
if err != nil {
@ -2024,7 +2025,7 @@ func ScriptFromWitness(hash util.Uint160, witness *transaction.Witness) ([]byte,
}
// verifyHashAgainstScript verifies given hash against the given witness.
func (bc *Blockchain) verifyHashAgainstScript(hash util.Uint160, witness *transaction.Witness, checkedHash util.Uint256, interopCtx *interopContext, useKeys bool) error {
func (bc *Blockchain) verifyHashAgainstScript(hash util.Uint160, witness *transaction.Witness, checkedHash util.Uint256, interopCtx *interop.Context, useKeys bool) error {
verification, err := ScriptFromWitness(hash, witness)
if err != nil {
return err
@ -2124,6 +2125,6 @@ func (bc *Blockchain) secondsPerBlock() int {
return bc.config.SecondsPerBlock
}
func (bc *Blockchain) newInteropContext(trigger trigger.Type, d dao.DAO, block *block.Block, tx *transaction.Transaction) *interopContext {
return newInteropContext(trigger, bc, d, block, tx, bc.log)
func (bc *Blockchain) newInteropContext(trigger trigger.Type, d dao.DAO, block *block.Block, tx *transaction.Transaction) *interop.Context {
return interop.NewContext(trigger, bc, d, block, tx, bc.log)
}

View file

@ -0,0 +1,48 @@
package interop
import (
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/blockchainer"
"github.com/nspcc-dev/neo-go/pkg/core/dao"
"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/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/vm"
"go.uber.org/zap"
)
// Context represents context in which interops are executed.
type Context struct {
Chain blockchainer.Blockchainer
Trigger trigger.Type
Block *block.Block
Tx *transaction.Transaction
DAO *dao.Cached
Notifications []state.NotificationEvent
Log *zap.Logger
}
// NewContext returns new interop context.
func NewContext(trigger trigger.Type, bc blockchainer.Blockchainer, d dao.DAO, block *block.Block, tx *transaction.Transaction, log *zap.Logger) *Context {
dao := dao.NewCached(d)
nes := make([]state.NotificationEvent, 0)
return &Context{
Chain: bc,
Trigger: trigger,
Block: block,
Tx: tx,
DAO: dao,
Notifications: nes,
Log: log,
}
}
// Function binds function name, id with the function itself and price,
// it's supposed to be inited once for all interopContexts, so it doesn't use
// vm.InteropFuncPrice directly.
type Function struct {
ID uint32
Name string
Func func(*Context, *vm.VM) error
Price int
}

View file

@ -6,6 +6,7 @@ import (
"math"
"strings"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"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"
@ -37,7 +38,7 @@ const (
)
// headerGetVersion returns version from the header.
func headerGetVersion(ic *interopContext, v *vm.VM) error {
func headerGetVersion(ic *interop.Context, v *vm.VM) error {
header, err := popHeaderFromVM(v)
if err != nil {
return err
@ -47,7 +48,7 @@ func headerGetVersion(ic *interopContext, v *vm.VM) error {
}
// headerGetConsensusData returns consensus data from the header.
func headerGetConsensusData(ic *interopContext, v *vm.VM) error {
func headerGetConsensusData(ic *interop.Context, v *vm.VM) error {
header, err := popHeaderFromVM(v)
if err != nil {
return err
@ -57,7 +58,7 @@ func headerGetConsensusData(ic *interopContext, v *vm.VM) error {
}
// headerGetMerkleRoot returns version from the header.
func headerGetMerkleRoot(ic *interopContext, v *vm.VM) error {
func headerGetMerkleRoot(ic *interop.Context, v *vm.VM) error {
header, err := popHeaderFromVM(v)
if err != nil {
return err
@ -67,7 +68,7 @@ func headerGetMerkleRoot(ic *interopContext, v *vm.VM) error {
}
// headerGetNextConsensus returns version from the header.
func headerGetNextConsensus(ic *interopContext, v *vm.VM) error {
func headerGetNextConsensus(ic *interop.Context, v *vm.VM) error {
header, err := popHeaderFromVM(v)
if err != nil {
return err
@ -77,7 +78,7 @@ func headerGetNextConsensus(ic *interopContext, v *vm.VM) error {
}
// txGetAttributes returns current transaction attributes.
func txGetAttributes(ic *interopContext, v *vm.VM) error {
func txGetAttributes(ic *interop.Context, v *vm.VM) error {
txInterface := v.Estack().Pop().Value()
tx, ok := txInterface.(*transaction.Transaction)
if !ok {
@ -95,7 +96,7 @@ func txGetAttributes(ic *interopContext, v *vm.VM) error {
}
// txGetInputs returns current transaction inputs.
func txGetInputs(ic *interopContext, v *vm.VM) error {
func txGetInputs(ic *interop.Context, v *vm.VM) error {
txInterface := v.Estack().Pop().Value()
tx, ok := txInterface.(*transaction.Transaction)
if !ok {
@ -113,7 +114,7 @@ func txGetInputs(ic *interopContext, v *vm.VM) error {
}
// txGetOutputs returns current transaction outputs.
func txGetOutputs(ic *interopContext, v *vm.VM) error {
func txGetOutputs(ic *interop.Context, v *vm.VM) error {
txInterface := v.Estack().Pop().Value()
tx, ok := txInterface.(*transaction.Transaction)
if !ok {
@ -131,13 +132,13 @@ func txGetOutputs(ic *interopContext, v *vm.VM) error {
}
// txGetReferences returns current transaction references.
func txGetReferences(ic *interopContext, v *vm.VM) error {
func txGetReferences(ic *interop.Context, v *vm.VM) error {
txInterface := v.Estack().Pop().Value()
tx, ok := txInterface.(*transaction.Transaction)
if !ok {
return fmt.Errorf("type mismatch: %T is not a Transaction", txInterface)
}
refs, err := ic.bc.References(tx)
refs, err := ic.Chain.References(tx)
if err != nil {
return err
}
@ -159,7 +160,7 @@ func txGetReferences(ic *interopContext, v *vm.VM) error {
}
// txGetType returns current transaction type.
func txGetType(ic *interopContext, v *vm.VM) error {
func txGetType(ic *interop.Context, v *vm.VM) error {
txInterface := v.Estack().Pop().Value()
tx, ok := txInterface.(*transaction.Transaction)
if !ok {
@ -170,13 +171,13 @@ func txGetType(ic *interopContext, v *vm.VM) error {
}
// txGetUnspentCoins returns current transaction unspent coins.
func txGetUnspentCoins(ic *interopContext, v *vm.VM) error {
func txGetUnspentCoins(ic *interop.Context, v *vm.VM) error {
txInterface := v.Estack().Pop().Value()
tx, ok := txInterface.(*transaction.Transaction)
if !ok {
return errors.New("value is not a transaction")
}
ucs, err := ic.dao.GetUnspentCoinState(tx.Hash())
ucs, err := ic.DAO.GetUnspentCoinState(tx.Hash())
if err != nil {
return errors.New("no unspent coin state found")
}
@ -185,7 +186,7 @@ func txGetUnspentCoins(ic *interopContext, v *vm.VM) error {
}
// txGetWitnesses returns current transaction witnesses.
func txGetWitnesses(ic *interopContext, v *vm.VM) error {
func txGetWitnesses(ic *interop.Context, v *vm.VM) error {
txInterface := v.Estack().Pop().Value()
tx, ok := txInterface.(*transaction.Transaction)
if !ok {
@ -203,7 +204,7 @@ func txGetWitnesses(ic *interopContext, v *vm.VM) error {
}
// invocationTx_GetScript returns invocation script from the current transaction.
func invocationTxGetScript(ic *interopContext, v *vm.VM) error {
func invocationTxGetScript(ic *interop.Context, v *vm.VM) error {
txInterface := v.Estack().Pop().Value()
tx, ok := txInterface.(*transaction.Transaction)
if !ok {
@ -221,7 +222,7 @@ func invocationTxGetScript(ic *interopContext, v *vm.VM) error {
}
// witnessGetVerificationScript returns current witness' script.
func witnessGetVerificationScript(ic *interopContext, v *vm.VM) error {
func witnessGetVerificationScript(ic *interop.Context, v *vm.VM) error {
witInterface := v.Estack().Pop().Value()
wit, ok := witInterface.(*transaction.Witness)
if !ok {
@ -235,8 +236,8 @@ func witnessGetVerificationScript(ic *interopContext, v *vm.VM) error {
}
// bcGetValidators returns validators.
func bcGetValidators(ic *interopContext, v *vm.VM) error {
validators := ic.dao.GetValidators()
func bcGetValidators(ic *interop.Context, v *vm.VM) error {
validators := ic.DAO.GetValidators()
v.Estack().PushVal(validators)
return nil
}
@ -256,7 +257,7 @@ func popInputFromVM(v *vm.VM) (*transaction.Input, error) {
}
// inputGetHash returns hash from the given input.
func inputGetHash(ic *interopContext, v *vm.VM) error {
func inputGetHash(ic *interop.Context, v *vm.VM) error {
input, err := popInputFromVM(v)
if err != nil {
return err
@ -266,7 +267,7 @@ func inputGetHash(ic *interopContext, v *vm.VM) error {
}
// inputGetIndex returns index from the given input.
func inputGetIndex(ic *interopContext, v *vm.VM) error {
func inputGetIndex(ic *interop.Context, v *vm.VM) error {
input, err := popInputFromVM(v)
if err != nil {
return err
@ -290,7 +291,7 @@ func popOutputFromVM(v *vm.VM) (*transaction.Output, error) {
}
// outputGetAssetId returns asset ID from the given output.
func outputGetAssetID(ic *interopContext, v *vm.VM) error {
func outputGetAssetID(ic *interop.Context, v *vm.VM) error {
output, err := popOutputFromVM(v)
if err != nil {
return err
@ -300,7 +301,7 @@ func outputGetAssetID(ic *interopContext, v *vm.VM) error {
}
// outputGetScriptHash returns scripthash from the given output.
func outputGetScriptHash(ic *interopContext, v *vm.VM) error {
func outputGetScriptHash(ic *interop.Context, v *vm.VM) error {
output, err := popOutputFromVM(v)
if err != nil {
return err
@ -310,7 +311,7 @@ func outputGetScriptHash(ic *interopContext, v *vm.VM) error {
}
// outputGetValue returns value (amount) from the given output.
func outputGetValue(ic *interopContext, v *vm.VM) error {
func outputGetValue(ic *interop.Context, v *vm.VM) error {
output, err := popOutputFromVM(v)
if err != nil {
return err
@ -320,7 +321,7 @@ func outputGetValue(ic *interopContext, v *vm.VM) error {
}
// attrGetData returns tx attribute data.
func attrGetData(ic *interopContext, v *vm.VM) error {
func attrGetData(ic *interop.Context, v *vm.VM) error {
attrInterface := v.Estack().Pop().Value()
attr, ok := attrInterface.(*transaction.Attribute)
if !ok {
@ -331,7 +332,7 @@ func attrGetData(ic *interopContext, v *vm.VM) error {
}
// attrGetData returns tx attribute usage field.
func attrGetUsage(ic *interopContext, v *vm.VM) error {
func attrGetUsage(ic *interop.Context, v *vm.VM) error {
attrInterface := v.Estack().Pop().Value()
attr, ok := attrInterface.(*transaction.Attribute)
if !ok {
@ -342,13 +343,13 @@ func attrGetUsage(ic *interopContext, v *vm.VM) error {
}
// bcGetAccount returns or creates an account.
func bcGetAccount(ic *interopContext, v *vm.VM) error {
func bcGetAccount(ic *interop.Context, v *vm.VM) error {
accbytes := v.Estack().Pop().Bytes()
acchash, err := util.Uint160DecodeBytesBE(accbytes)
if err != nil {
return err
}
acc, err := ic.dao.GetAccountStateOrNew(acchash)
acc, err := ic.DAO.GetAccountStateOrNew(acchash)
if err != nil {
return err
}
@ -357,13 +358,13 @@ func bcGetAccount(ic *interopContext, v *vm.VM) error {
}
// bcGetAsset returns an asset.
func bcGetAsset(ic *interopContext, v *vm.VM) error {
func bcGetAsset(ic *interop.Context, v *vm.VM) error {
asbytes := v.Estack().Pop().Bytes()
ashash, err := util.Uint256DecodeBytesBE(asbytes)
if err != nil {
return err
}
as, err := ic.dao.GetAssetState(ashash)
as, err := ic.DAO.GetAssetState(ashash)
if err != nil {
return errors.New("asset not found")
}
@ -372,7 +373,7 @@ func bcGetAsset(ic *interopContext, v *vm.VM) error {
}
// accountGetBalance returns balance for a given account.
func accountGetBalance(ic *interopContext, v *vm.VM) error {
func accountGetBalance(ic *interop.Context, v *vm.VM) error {
accInterface := v.Estack().Pop().Value()
acc, ok := accInterface.(*state.Account)
if !ok {
@ -392,7 +393,7 @@ func accountGetBalance(ic *interopContext, v *vm.VM) error {
}
// accountGetScriptHash returns script hash of a given account.
func accountGetScriptHash(ic *interopContext, v *vm.VM) error {
func accountGetScriptHash(ic *interop.Context, v *vm.VM) error {
accInterface := v.Estack().Pop().Value()
acc, ok := accInterface.(*state.Account)
if !ok {
@ -403,7 +404,7 @@ func accountGetScriptHash(ic *interopContext, v *vm.VM) error {
}
// accountGetVotes returns votes of a given account.
func accountGetVotes(ic *interopContext, v *vm.VM) error {
func accountGetVotes(ic *interop.Context, v *vm.VM) error {
accInterface := v.Estack().Pop().Value()
acc, ok := accInterface.(*state.Account)
if !ok {
@ -421,20 +422,20 @@ func accountGetVotes(ic *interopContext, v *vm.VM) error {
}
// accountIsStandard checks whether given account is standard.
func accountIsStandard(ic *interopContext, v *vm.VM) error {
func accountIsStandard(ic *interop.Context, v *vm.VM) error {
accbytes := v.Estack().Pop().Bytes()
acchash, err := util.Uint160DecodeBytesBE(accbytes)
if err != nil {
return err
}
contract, err := ic.dao.GetContractState(acchash)
contract, err := ic.DAO.GetContractState(acchash)
res := err != nil || vm.IsStandardContract(contract.Script)
v.Estack().PushVal(res)
return nil
}
// storageFind finds stored key-value pair.
func storageFind(ic *interopContext, v *vm.VM) error {
func storageFind(ic *interop.Context, v *vm.VM) error {
stcInterface := v.Estack().Pop().Value()
stc, ok := stcInterface.(*StorageContext)
if !ok {
@ -445,7 +446,7 @@ func storageFind(ic *interopContext, v *vm.VM) error {
return err
}
prefix := string(v.Estack().Pop().Bytes())
siMap, err := ic.dao.GetStorageItems(stc.ScriptHash)
siMap, err := ic.DAO.GetStorageItems(stc.ScriptHash)
if err != nil {
return err
}
@ -467,8 +468,8 @@ func storageFind(ic *interopContext, v *vm.VM) error {
// createContractStateFromVM pops all contract state elements from the VM
// evaluation stack, does a lot of checks and returns Contract if it
// succeeds.
func createContractStateFromVM(ic *interopContext, v *vm.VM) (*state.Contract, error) {
if ic.trigger != trigger.Application {
func createContractStateFromVM(ic *interop.Context, v *vm.VM) (*state.Contract, error) {
if ic.Trigger != trigger.Application {
return nil, errors.New("can't create contract when not triggered by an application")
}
script := v.Estack().Pop().Bytes()
@ -520,15 +521,15 @@ func createContractStateFromVM(ic *interopContext, v *vm.VM) (*state.Contract, e
}
// contractCreate creates a contract.
func contractCreate(ic *interopContext, v *vm.VM) error {
func contractCreate(ic *interop.Context, v *vm.VM) error {
newcontract, err := createContractStateFromVM(ic, v)
if err != nil {
return err
}
contract, err := ic.dao.GetContractState(newcontract.ScriptHash())
contract, err := ic.DAO.GetContractState(newcontract.ScriptHash())
if err != nil {
contract = newcontract
err := ic.dao.PutContractState(contract)
err := ic.DAO.PutContractState(contract)
if err != nil {
return err
}
@ -538,7 +539,7 @@ func contractCreate(ic *interopContext, v *vm.VM) error {
}
// contractGetScript returns a script associated with a contract.
func contractGetScript(ic *interopContext, v *vm.VM) error {
func contractGetScript(ic *interop.Context, v *vm.VM) error {
csInterface := v.Estack().Pop().Value()
cs, ok := csInterface.(*state.Contract)
if !ok {
@ -549,7 +550,7 @@ func contractGetScript(ic *interopContext, v *vm.VM) error {
}
// contractIsPayable returns whether contract is payable.
func contractIsPayable(ic *interopContext, v *vm.VM) error {
func contractIsPayable(ic *interop.Context, v *vm.VM) error {
csInterface := v.Estack().Pop().Value()
cs, ok := csInterface.(*state.Contract)
if !ok {
@ -560,27 +561,27 @@ func contractIsPayable(ic *interopContext, v *vm.VM) error {
}
// contractMigrate migrates a contract.
func contractMigrate(ic *interopContext, v *vm.VM) error {
func contractMigrate(ic *interop.Context, v *vm.VM) error {
newcontract, err := createContractStateFromVM(ic, v)
if err != nil {
return err
}
contract, err := ic.dao.GetContractState(newcontract.ScriptHash())
contract, err := ic.DAO.GetContractState(newcontract.ScriptHash())
if err != nil {
contract = newcontract
err := ic.dao.PutContractState(contract)
err := ic.DAO.PutContractState(contract)
if err != nil {
return err
}
if contract.HasStorage() {
hash := getContextScriptHash(v, 0)
siMap, err := ic.dao.GetStorageItems(hash)
siMap, err := ic.DAO.GetStorageItems(hash)
if err != nil {
return err
}
for k, v := range siMap {
v.IsConst = false
err = ic.dao.PutStorageItem(contract.ScriptHash(), []byte(k), v)
err = ic.DAO.PutStorageItem(contract.ScriptHash(), []byte(k), v)
if err != nil {
return err
}
@ -592,8 +593,8 @@ func contractMigrate(ic *interopContext, v *vm.VM) error {
}
// assetCreate creates an asset.
func assetCreate(ic *interopContext, v *vm.VM) error {
if ic.trigger != trigger.Application {
func assetCreate(ic *interop.Context, v *vm.VM) error {
if ic.Trigger != trigger.Application {
return errors.New("can't create asset when not triggered by an application")
}
atype := transaction.AssetType(v.Estack().Pop().BigInt().Int64())
@ -651,7 +652,7 @@ func assetCreate(ic *interopContext, v *vm.VM) error {
return gherr.Wrap(err, "failed to get issuer")
}
asset := &state.Asset{
ID: ic.tx.Hash(),
ID: ic.Tx.Hash(),
AssetType: atype,
Name: name,
Amount: amount,
@ -659,9 +660,9 @@ func assetCreate(ic *interopContext, v *vm.VM) error {
Owner: *owner,
Admin: admin,
Issuer: issuer,
Expiration: ic.bc.BlockHeight() + DefaultAssetLifetime,
Expiration: ic.Chain.BlockHeight() + DefaultAssetLifetime,
}
err = ic.dao.PutAssetState(asset)
err = ic.DAO.PutAssetState(asset)
if err != nil {
return gherr.Wrap(err, "failed to Store asset")
}
@ -670,7 +671,7 @@ func assetCreate(ic *interopContext, v *vm.VM) error {
}
// assetGetAdmin returns asset admin.
func assetGetAdmin(ic *interopContext, v *vm.VM) error {
func assetGetAdmin(ic *interop.Context, v *vm.VM) error {
asInterface := v.Estack().Pop().Value()
as, ok := asInterface.(*state.Asset)
if !ok {
@ -681,7 +682,7 @@ func assetGetAdmin(ic *interopContext, v *vm.VM) error {
}
// assetGetAmount returns the overall amount of asset available.
func assetGetAmount(ic *interopContext, v *vm.VM) error {
func assetGetAmount(ic *interop.Context, v *vm.VM) error {
asInterface := v.Estack().Pop().Value()
as, ok := asInterface.(*state.Asset)
if !ok {
@ -692,7 +693,7 @@ func assetGetAmount(ic *interopContext, v *vm.VM) error {
}
// assetGetAssetId returns the id of an asset.
func assetGetAssetID(ic *interopContext, v *vm.VM) error {
func assetGetAssetID(ic *interop.Context, v *vm.VM) error {
asInterface := v.Estack().Pop().Value()
as, ok := asInterface.(*state.Asset)
if !ok {
@ -703,7 +704,7 @@ func assetGetAssetID(ic *interopContext, v *vm.VM) error {
}
// assetGetAssetType returns type of an asset.
func assetGetAssetType(ic *interopContext, v *vm.VM) error {
func assetGetAssetType(ic *interop.Context, v *vm.VM) error {
asInterface := v.Estack().Pop().Value()
as, ok := asInterface.(*state.Asset)
if !ok {
@ -714,7 +715,7 @@ func assetGetAssetType(ic *interopContext, v *vm.VM) error {
}
// assetGetAvailable returns available (not yet issued) amount of asset.
func assetGetAvailable(ic *interopContext, v *vm.VM) error {
func assetGetAvailable(ic *interop.Context, v *vm.VM) error {
asInterface := v.Estack().Pop().Value()
as, ok := asInterface.(*state.Asset)
if !ok {
@ -725,7 +726,7 @@ func assetGetAvailable(ic *interopContext, v *vm.VM) error {
}
// assetGetIssuer returns issuer of an asset.
func assetGetIssuer(ic *interopContext, v *vm.VM) error {
func assetGetIssuer(ic *interop.Context, v *vm.VM) error {
asInterface := v.Estack().Pop().Value()
as, ok := asInterface.(*state.Asset)
if !ok {
@ -736,7 +737,7 @@ func assetGetIssuer(ic *interopContext, v *vm.VM) error {
}
// assetGetOwner returns owner of an asset.
func assetGetOwner(ic *interopContext, v *vm.VM) error {
func assetGetOwner(ic *interop.Context, v *vm.VM) error {
asInterface := v.Estack().Pop().Value()
as, ok := asInterface.(*state.Asset)
if !ok {
@ -747,7 +748,7 @@ func assetGetOwner(ic *interopContext, v *vm.VM) error {
}
// assetGetPrecision returns precision used to measure this asset.
func assetGetPrecision(ic *interopContext, v *vm.VM) error {
func assetGetPrecision(ic *interop.Context, v *vm.VM) error {
asInterface := v.Estack().Pop().Value()
as, ok := asInterface.(*state.Asset)
if !ok {
@ -758,8 +759,8 @@ func assetGetPrecision(ic *interopContext, v *vm.VM) error {
}
// assetRenew updates asset expiration date.
func assetRenew(ic *interopContext, v *vm.VM) error {
if ic.trigger != trigger.Application {
func assetRenew(ic *interop.Context, v *vm.VM) error {
if ic.Trigger != trigger.Application {
return errors.New("can't create asset when not triggered by an application")
}
asInterface := v.Estack().Pop().Value()
@ -769,19 +770,19 @@ func assetRenew(ic *interopContext, v *vm.VM) error {
}
years := byte(v.Estack().Pop().BigInt().Int64())
// Not sure why C# code regets an asset from the Store, but we also do it.
asset, err := ic.dao.GetAssetState(as.ID)
asset, err := ic.DAO.GetAssetState(as.ID)
if err != nil {
return errors.New("can't renew non-existent asset")
}
if asset.Expiration < ic.bc.BlockHeight()+1 {
asset.Expiration = ic.bc.BlockHeight() + 1
if asset.Expiration < ic.Chain.BlockHeight()+1 {
asset.Expiration = ic.Chain.BlockHeight() + 1
}
expiration := uint64(asset.Expiration) + uint64(years)*BlocksPerYear
if expiration > math.MaxUint32 {
expiration = math.MaxUint32
}
asset.Expiration = uint32(expiration)
err = ic.dao.PutAssetState(asset)
err = ic.DAO.PutAssetState(asset)
if err != nil {
return gherr.Wrap(err, "failed to Store asset")
}
@ -790,57 +791,57 @@ func assetRenew(ic *interopContext, v *vm.VM) error {
}
// runtimeSerialize serializes top stack item into a ByteArray.
func runtimeSerialize(_ *interopContext, v *vm.VM) error {
func runtimeSerialize(_ *interop.Context, v *vm.VM) error {
return vm.RuntimeSerialize(v)
}
// runtimeDeserialize deserializes ByteArray from a stack into an item.
func runtimeDeserialize(_ *interopContext, v *vm.VM) error {
func runtimeDeserialize(_ *interop.Context, v *vm.VM) error {
return vm.RuntimeDeserialize(v)
}
// enumeratorConcat concatenates 2 enumerators into a single one.
func enumeratorConcat(_ *interopContext, v *vm.VM) error {
func enumeratorConcat(_ *interop.Context, v *vm.VM) error {
return vm.EnumeratorConcat(v)
}
// enumeratorCreate creates an enumerator from an array-like stack item.
func enumeratorCreate(_ *interopContext, v *vm.VM) error {
func enumeratorCreate(_ *interop.Context, v *vm.VM) error {
return vm.EnumeratorCreate(v)
}
// enumeratorNext advances the enumerator, pushes true if is it was successful
// and false otherwise.
func enumeratorNext(_ *interopContext, v *vm.VM) error {
func enumeratorNext(_ *interop.Context, v *vm.VM) error {
return vm.EnumeratorNext(v)
}
// enumeratorValue returns the current value of the enumerator.
func enumeratorValue(_ *interopContext, v *vm.VM) error {
func enumeratorValue(_ *interop.Context, v *vm.VM) error {
return vm.EnumeratorValue(v)
}
// iteratorConcat concatenates 2 iterators into a single one.
func iteratorConcat(_ *interopContext, v *vm.VM) error {
func iteratorConcat(_ *interop.Context, v *vm.VM) error {
return vm.IteratorConcat(v)
}
// iteratorCreate creates an iterator from array-like or map stack item.
func iteratorCreate(_ *interopContext, v *vm.VM) error {
func iteratorCreate(_ *interop.Context, v *vm.VM) error {
return vm.IteratorCreate(v)
}
// iteratorKey returns current iterator key.
func iteratorKey(_ *interopContext, v *vm.VM) error {
func iteratorKey(_ *interop.Context, v *vm.VM) error {
return vm.IteratorKey(v)
}
// iteratorKeys returns keys of the iterator.
func iteratorKeys(_ *interopContext, v *vm.VM) error {
func iteratorKeys(_ *interop.Context, v *vm.VM) error {
return vm.IteratorKeys(v)
}
// iteratorValues returns values of the iterator.
func iteratorValues(_ *interopContext, v *vm.VM) error {
func iteratorValues(_ *interop.Context, v *vm.VM) error {
return vm.IteratorValues(v)
}

View file

@ -6,6 +6,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/dao"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/core/storage"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
@ -56,12 +57,12 @@ func TestStorageFind(t *testing.T) {
},
}
require.NoError(t, context.dao.PutContractState(contractState))
require.NoError(t, context.DAO.PutContractState(contractState))
scriptHash := contractState.ScriptHash()
for i := range skeys {
err := context.dao.PutStorageItem(scriptHash, skeys[i], items[i])
err := context.DAO.PutStorageItem(scriptHash, skeys[i], items[i])
require.NoError(t, err)
}
@ -459,7 +460,7 @@ func TestAssetGetPrecision(t *testing.T) {
// Helper functions to create VM, InteropContext, TX, Account, Contract, Asset.
func createVMAndPushBlock(t *testing.T) (*vm.VM, *block.Block, *interopContext, *Blockchain) {
func createVMAndPushBlock(t *testing.T) (*vm.VM, *block.Block, *interop.Context, *Blockchain) {
v := vm.New()
block := newDumbBlock()
chain := newTestChain(t)
@ -468,13 +469,13 @@ func createVMAndPushBlock(t *testing.T) (*vm.VM, *block.Block, *interopContext,
return v, block, context, chain
}
func createVMAndPushTX(t *testing.T) (*vm.VM, *transaction.Transaction, *interopContext, *Blockchain) {
func createVMAndPushTX(t *testing.T) (*vm.VM, *transaction.Transaction, *interop.Context, *Blockchain) {
v, tx, context, chain := createVMAndTX(t)
v.Estack().PushVal(vm.NewInteropItem(tx))
return v, tx, context, chain
}
func createVMAndAssetState(t *testing.T) (*vm.VM, *state.Asset, *interopContext, *Blockchain) {
func createVMAndAssetState(t *testing.T) (*vm.VM, *state.Asset, *interop.Context, *Blockchain) {
v := vm.New()
assetState := &state.Asset{
ID: util.Uint256{},
@ -497,7 +498,7 @@ func createVMAndAssetState(t *testing.T) (*vm.VM, *state.Asset, *interopContext,
return v, assetState, context, chain
}
func createVMAndContractState(t *testing.T) (*vm.VM, *state.Contract, *interopContext, *Blockchain) {
func createVMAndContractState(t *testing.T) (*vm.VM, *state.Contract, *interop.Context, *Blockchain) {
v := vm.New()
contractState := &state.Contract{
Script: []byte("testscript"),
@ -516,7 +517,7 @@ func createVMAndContractState(t *testing.T) (*vm.VM, *state.Contract, *interopCo
return v, contractState, context, chain
}
func createVMAndAccState(t *testing.T) (*vm.VM, *state.Account, *interopContext, *Blockchain) {
func createVMAndAccState(t *testing.T) (*vm.VM, *state.Account, *interop.Context, *Blockchain) {
v := vm.New()
rawHash := "4d3b96ae1bcc5a585e075e3b81920210dec16302"
hash, err := util.Uint160DecodeStringBE(rawHash)
@ -531,7 +532,7 @@ func createVMAndAccState(t *testing.T) (*vm.VM, *state.Account, *interopContext,
return v, accountState, context, chain
}
func createVMAndTX(t *testing.T) (*vm.VM, *transaction.Transaction, *interopContext, *Blockchain) {
func createVMAndTX(t *testing.T) (*vm.VM, *transaction.Transaction, *interop.Context, *Blockchain) {
v := vm.New()
script := []byte{byte(opcode.PUSH1), byte(opcode.RET)}
tx := transaction.NewInvocationTX(script, 0)

View file

@ -8,6 +8,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/blockchainer"
"github.com/nspcc-dev/neo-go/pkg/core/dao"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"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"
@ -49,12 +50,12 @@ func getBlockHashFromElement(bc blockchainer.Blockchainer, element *vm.Element)
}
// bcGetBlock returns current block.
func bcGetBlock(ic *interopContext, v *vm.VM) error {
hash, err := getBlockHashFromElement(ic.bc, v.Estack().Pop())
func bcGetBlock(ic *interop.Context, v *vm.VM) error {
hash, err := getBlockHashFromElement(ic.Chain, v.Estack().Pop())
if err != nil {
return err
}
block, err := ic.bc.GetBlock(hash)
block, err := ic.Chain.GetBlock(hash)
if err != nil {
v.Estack().PushVal([]byte{})
} else {
@ -64,13 +65,13 @@ func bcGetBlock(ic *interopContext, v *vm.VM) error {
}
// bcGetContract returns contract.
func bcGetContract(ic *interopContext, v *vm.VM) error {
func bcGetContract(ic *interop.Context, v *vm.VM) error {
hashbytes := v.Estack().Pop().Bytes()
hash, err := util.Uint160DecodeBytesBE(hashbytes)
if err != nil {
return err
}
cs, err := ic.dao.GetContractState(hash)
cs, err := ic.DAO.GetContractState(hash)
if err != nil {
v.Estack().PushVal([]byte{})
} else {
@ -80,12 +81,12 @@ func bcGetContract(ic *interopContext, v *vm.VM) error {
}
// bcGetHeader returns block header.
func bcGetHeader(ic *interopContext, v *vm.VM) error {
hash, err := getBlockHashFromElement(ic.bc, v.Estack().Pop())
func bcGetHeader(ic *interop.Context, v *vm.VM) error {
hash, err := getBlockHashFromElement(ic.Chain, v.Estack().Pop())
if err != nil {
return err
}
header, err := ic.bc.GetHeader(hash)
header, err := ic.Chain.GetHeader(hash)
if err != nil {
v.Estack().PushVal([]byte{})
} else {
@ -95,8 +96,8 @@ func bcGetHeader(ic *interopContext, v *vm.VM) error {
}
// bcGetHeight returns blockchain height.
func bcGetHeight(ic *interopContext, v *vm.VM) error {
v.Estack().PushVal(ic.bc.BlockHeight())
func bcGetHeight(ic *interop.Context, v *vm.VM) error {
v.Estack().PushVal(ic.Chain.BlockHeight())
return nil
}
@ -112,8 +113,8 @@ func getTransactionAndHeight(cd *dao.Cached, v *vm.VM) (*transaction.Transaction
}
// bcGetTransaction returns transaction.
func bcGetTransaction(ic *interopContext, v *vm.VM) error {
tx, _, err := getTransactionAndHeight(ic.dao, v)
func bcGetTransaction(ic *interop.Context, v *vm.VM) error {
tx, _, err := getTransactionAndHeight(ic.DAO, v)
if err != nil {
return err
}
@ -122,8 +123,8 @@ func bcGetTransaction(ic *interopContext, v *vm.VM) error {
}
// bcGetTransactionHeight returns transaction height.
func bcGetTransactionHeight(ic *interopContext, v *vm.VM) error {
_, h, err := getTransactionAndHeight(ic.dao, v)
func bcGetTransactionHeight(ic *interop.Context, v *vm.VM) error {
_, h, err := getTransactionAndHeight(ic.DAO, v)
if err != nil {
return err
}
@ -148,7 +149,7 @@ func popHeaderFromVM(v *vm.VM) (*block.Header, error) {
}
// headerGetIndex returns block index from the header.
func headerGetIndex(ic *interopContext, v *vm.VM) error {
func headerGetIndex(ic *interop.Context, v *vm.VM) error {
header, err := popHeaderFromVM(v)
if err != nil {
return err
@ -158,7 +159,7 @@ func headerGetIndex(ic *interopContext, v *vm.VM) error {
}
// headerGetHash returns header hash of the passed header.
func headerGetHash(ic *interopContext, v *vm.VM) error {
func headerGetHash(ic *interop.Context, v *vm.VM) error {
header, err := popHeaderFromVM(v)
if err != nil {
return err
@ -168,7 +169,7 @@ func headerGetHash(ic *interopContext, v *vm.VM) error {
}
// headerGetPrevHash returns previous header hash of the passed header.
func headerGetPrevHash(ic *interopContext, v *vm.VM) error {
func headerGetPrevHash(ic *interop.Context, v *vm.VM) error {
header, err := popHeaderFromVM(v)
if err != nil {
return err
@ -178,7 +179,7 @@ func headerGetPrevHash(ic *interopContext, v *vm.VM) error {
}
// headerGetTimestamp returns timestamp of the passed header.
func headerGetTimestamp(ic *interopContext, v *vm.VM) error {
func headerGetTimestamp(ic *interop.Context, v *vm.VM) error {
header, err := popHeaderFromVM(v)
if err != nil {
return err
@ -188,7 +189,7 @@ func headerGetTimestamp(ic *interopContext, v *vm.VM) error {
}
// blockGetTransactionCount returns transactions count in the given block.
func blockGetTransactionCount(ic *interopContext, v *vm.VM) error {
func blockGetTransactionCount(ic *interop.Context, v *vm.VM) error {
blockInterface := v.Estack().Pop().Value()
block, ok := blockInterface.(*block.Block)
if !ok {
@ -199,7 +200,7 @@ func blockGetTransactionCount(ic *interopContext, v *vm.VM) error {
}
// blockGetTransactions returns transactions from the given block.
func blockGetTransactions(ic *interopContext, v *vm.VM) error {
func blockGetTransactions(ic *interop.Context, v *vm.VM) error {
blockInterface := v.Estack().Pop().Value()
block, ok := blockInterface.(*block.Block)
if !ok {
@ -218,7 +219,7 @@ func blockGetTransactions(ic *interopContext, v *vm.VM) error {
// blockGetTransaction returns transaction with the given number from the given
// block.
func blockGetTransaction(ic *interopContext, v *vm.VM) error {
func blockGetTransaction(ic *interop.Context, v *vm.VM) error {
blockInterface := v.Estack().Pop().Value()
block, ok := blockInterface.(*block.Block)
if !ok {
@ -234,7 +235,7 @@ func blockGetTransaction(ic *interopContext, v *vm.VM) error {
}
// txGetHash returns transaction's hash.
func txGetHash(ic *interopContext, v *vm.VM) error {
func txGetHash(ic *interop.Context, v *vm.VM) error {
txInterface := v.Estack().Pop().Value()
tx, ok := txInterface.(*transaction.Transaction)
if !ok {
@ -246,8 +247,8 @@ func txGetHash(ic *interopContext, v *vm.VM) error {
// engineGetScriptContainer returns transaction that contains the script being
// run.
func engineGetScriptContainer(ic *interopContext, v *vm.VM) error {
v.Estack().PushVal(vm.NewInteropItem(ic.tx))
func engineGetScriptContainer(ic *interop.Context, v *vm.VM) error {
v.Estack().PushVal(vm.NewInteropItem(ic.Tx))
return nil
}
@ -268,36 +269,36 @@ func pushContextScriptHash(v *vm.VM, n int) error {
}
// engineGetExecutingScriptHash returns executing script hash.
func engineGetExecutingScriptHash(ic *interopContext, v *vm.VM) error {
func engineGetExecutingScriptHash(ic *interop.Context, v *vm.VM) error {
return pushContextScriptHash(v, 0)
}
// engineGetCallingScriptHash returns calling script hash.
func engineGetCallingScriptHash(ic *interopContext, v *vm.VM) error {
func engineGetCallingScriptHash(ic *interop.Context, v *vm.VM) error {
return pushContextScriptHash(v, 1)
}
// engineGetEntryScriptHash returns entry script hash.
func engineGetEntryScriptHash(ic *interopContext, v *vm.VM) error {
func engineGetEntryScriptHash(ic *interop.Context, v *vm.VM) error {
return pushContextScriptHash(v, v.Istack().Len()-1)
}
// runtimePlatform returns the name of the platform.
func runtimePlatform(ic *interopContext, v *vm.VM) error {
func runtimePlatform(ic *interop.Context, v *vm.VM) error {
v.Estack().PushVal([]byte("NEO"))
return nil
}
// runtimeGetTrigger returns the script trigger.
func runtimeGetTrigger(ic *interopContext, v *vm.VM) error {
v.Estack().PushVal(byte(ic.trigger))
func runtimeGetTrigger(ic *interop.Context, v *vm.VM) error {
v.Estack().PushVal(byte(ic.Trigger))
return nil
}
// checkHashedWitness checks given hash against current list of script hashes
// for verifying in the interop context.
func checkHashedWitness(ic *interopContext, hash util.Uint160) (bool, error) {
hashes, err := ic.bc.GetScriptHashesForVerifying(ic.tx)
func checkHashedWitness(ic *interop.Context, hash util.Uint160) (bool, error) {
hashes, err := ic.Chain.GetScriptHashesForVerifying(ic.Tx)
if err != nil {
return false, gherr.Wrap(err, "failed to get script hashes")
}
@ -311,12 +312,12 @@ func checkHashedWitness(ic *interopContext, hash util.Uint160) (bool, error) {
// checkKeyedWitness checks hash of signature check contract with a given public
// key against current list of script hashes for verifying in the interop context.
func checkKeyedWitness(ic *interopContext, key *keys.PublicKey) (bool, error) {
func checkKeyedWitness(ic *interop.Context, key *keys.PublicKey) (bool, error) {
return checkHashedWitness(ic, key.GetScriptHash())
}
// runtimeCheckWitness checks witnesses.
func runtimeCheckWitness(ic *interopContext, v *vm.VM) error {
func runtimeCheckWitness(ic *interop.Context, v *vm.VM) error {
var res bool
var err error
@ -341,7 +342,7 @@ func runtimeCheckWitness(ic *interopContext, v *vm.VM) error {
// runtimeNotify should pass stack item to the notify plugin to handle it, but
// in neo-go the only meaningful thing to do here is to log.
func runtimeNotify(ic *interopContext, v *vm.VM) error {
func runtimeNotify(ic *interop.Context, v *vm.VM) error {
// It can be just about anything.
e := v.Estack().Pop()
item := e.Item()
@ -355,38 +356,38 @@ func runtimeNotify(ic *interopContext, v *vm.VM) error {
item = vm.NewByteArrayItem([]byte(fmt.Sprintf("bad notification: %v", err)))
}
ne := state.NotificationEvent{ScriptHash: getContextScriptHash(v, 0), Item: item}
ic.notifications = append(ic.notifications, ne)
ic.Notifications = append(ic.Notifications, ne)
return nil
}
// runtimeLog logs the message passed.
func runtimeLog(ic *interopContext, v *vm.VM) error {
func runtimeLog(ic *interop.Context, v *vm.VM) error {
msg := fmt.Sprintf("%q", v.Estack().Pop().Bytes())
ic.log.Info("runtime log",
ic.Log.Info("runtime log",
zap.Stringer("script", getContextScriptHash(v, 0)),
zap.String("logs", msg))
return nil
}
// runtimeGetTime returns timestamp of the block being verified, or the latest
// one in the blockchain if no block is given to interopContext.
func runtimeGetTime(ic *interopContext, v *vm.VM) error {
// one in the blockchain if no block is given to Context.
func runtimeGetTime(ic *interop.Context, v *vm.VM) error {
var header *block.Header
if ic.block == nil {
if ic.Block == nil {
var err error
header, err = ic.bc.GetHeader(ic.bc.CurrentBlockHash())
header, err = ic.Chain.GetHeader(ic.Chain.CurrentBlockHash())
if err != nil {
return err
}
} else {
header = ic.block.Header()
header = ic.Block.Header()
}
v.Estack().PushVal(header.Timestamp)
return nil
}
func checkStorageContext(ic *interopContext, stc *StorageContext) error {
contract, err := ic.dao.GetContractState(stc.ScriptHash)
func checkStorageContext(ic *interop.Context, stc *StorageContext) error {
contract, err := ic.DAO.GetContractState(stc.ScriptHash)
if err != nil {
return errors.New("no contract found")
}
@ -397,8 +398,8 @@ func checkStorageContext(ic *interopContext, stc *StorageContext) error {
}
// storageDelete deletes stored key-value pair.
func storageDelete(ic *interopContext, v *vm.VM) error {
if ic.trigger != trigger.Application && ic.trigger != trigger.ApplicationR {
func storageDelete(ic *interop.Context, v *vm.VM) error {
if ic.Trigger != trigger.Application && ic.Trigger != trigger.ApplicationR {
return errors.New("can't delete when the trigger is not application")
}
stcInterface := v.Estack().Pop().Value()
@ -414,15 +415,15 @@ func storageDelete(ic *interopContext, v *vm.VM) error {
return err
}
key := v.Estack().Pop().Bytes()
si := ic.dao.GetStorageItem(stc.ScriptHash, key)
si := ic.DAO.GetStorageItem(stc.ScriptHash, key)
if si != nil && si.IsConst {
return errors.New("storage item is constant")
}
return ic.dao.DeleteStorageItem(stc.ScriptHash, key)
return ic.DAO.DeleteStorageItem(stc.ScriptHash, key)
}
// storageGet returns stored key-value pair.
func storageGet(ic *interopContext, v *vm.VM) error {
func storageGet(ic *interop.Context, v *vm.VM) error {
stcInterface := v.Estack().Pop().Value()
stc, ok := stcInterface.(*StorageContext)
if !ok {
@ -433,7 +434,7 @@ func storageGet(ic *interopContext, v *vm.VM) error {
return err
}
key := v.Estack().Pop().Bytes()
si := ic.dao.GetStorageItem(stc.ScriptHash, key)
si := ic.DAO.GetStorageItem(stc.ScriptHash, key)
if si != nil && si.Value != nil {
v.Estack().PushVal(si.Value)
} else {
@ -443,7 +444,7 @@ func storageGet(ic *interopContext, v *vm.VM) error {
}
// storageGetContext returns storage context (scripthash).
func storageGetContext(ic *interopContext, v *vm.VM) error {
func storageGetContext(ic *interop.Context, v *vm.VM) error {
sc := &StorageContext{
ScriptHash: getContextScriptHash(v, 0),
ReadOnly: false,
@ -453,7 +454,7 @@ func storageGetContext(ic *interopContext, v *vm.VM) error {
}
// storageGetReadOnlyContext returns read-only context (scripthash).
func storageGetReadOnlyContext(ic *interopContext, v *vm.VM) error {
func storageGetReadOnlyContext(ic *interop.Context, v *vm.VM) error {
sc := &StorageContext{
ScriptHash: getContextScriptHash(v, 0),
ReadOnly: true,
@ -462,8 +463,8 @@ func storageGetReadOnlyContext(ic *interopContext, v *vm.VM) error {
return nil
}
func putWithContextAndFlags(ic *interopContext, stc *StorageContext, key []byte, value []byte, isConst bool) error {
if ic.trigger != trigger.Application && ic.trigger != trigger.ApplicationR {
func putWithContextAndFlags(ic *interop.Context, stc *StorageContext, key []byte, value []byte, isConst bool) error {
if ic.Trigger != trigger.Application && ic.Trigger != trigger.ApplicationR {
return errors.New("can't delete when the trigger is not application")
}
if len(key) > MaxStorageKeyLen {
@ -476,7 +477,7 @@ func putWithContextAndFlags(ic *interopContext, stc *StorageContext, key []byte,
if err != nil {
return err
}
si := ic.dao.GetStorageItem(stc.ScriptHash, key)
si := ic.DAO.GetStorageItem(stc.ScriptHash, key)
if si == nil {
si = &state.StorageItem{}
}
@ -485,11 +486,11 @@ func putWithContextAndFlags(ic *interopContext, stc *StorageContext, key []byte,
}
si.Value = value
si.IsConst = isConst
return ic.dao.PutStorageItem(stc.ScriptHash, key, si)
return ic.DAO.PutStorageItem(stc.ScriptHash, key, si)
}
// storagePutInternal is a unified implementation of storagePut and storagePutEx.
func storagePutInternal(ic *interopContext, v *vm.VM, getFlag bool) error {
func storagePutInternal(ic *interop.Context, v *vm.VM, getFlag bool) error {
stcInterface := v.Estack().Pop().Value()
stc, ok := stcInterface.(*StorageContext)
if !ok {
@ -505,17 +506,17 @@ func storagePutInternal(ic *interopContext, v *vm.VM, getFlag bool) error {
}
// storagePut puts key-value pair into the storage.
func storagePut(ic *interopContext, v *vm.VM) error {
func storagePut(ic *interop.Context, v *vm.VM) error {
return storagePutInternal(ic, v, false)
}
// storagePutEx puts key-value pair with given flags into the storage.
func storagePutEx(ic *interopContext, v *vm.VM) error {
func storagePutEx(ic *interop.Context, v *vm.VM) error {
return storagePutInternal(ic, v, true)
}
// storageContextAsReadOnly sets given context to read-only mode.
func storageContextAsReadOnly(ic *interopContext, v *vm.VM) error {
func storageContextAsReadOnly(ic *interop.Context, v *vm.VM) error {
stcInterface := v.Estack().Pop().Value()
stc, ok := stcInterface.(*StorageContext)
if !ok {
@ -533,39 +534,39 @@ func storageContextAsReadOnly(ic *interopContext, v *vm.VM) error {
}
// contractDestroy destroys a contract.
func contractDestroy(ic *interopContext, v *vm.VM) error {
if ic.trigger != trigger.Application {
func contractDestroy(ic *interop.Context, v *vm.VM) error {
if ic.Trigger != trigger.Application {
return errors.New("can't destroy contract when not triggered by application")
}
hash := getContextScriptHash(v, 0)
cs, err := ic.dao.GetContractState(hash)
cs, err := ic.DAO.GetContractState(hash)
if err != nil {
return nil
}
err = ic.dao.DeleteContractState(hash)
err = ic.DAO.DeleteContractState(hash)
if err != nil {
return err
}
if cs.HasStorage() {
siMap, err := ic.dao.GetStorageItems(hash)
siMap, err := ic.DAO.GetStorageItems(hash)
if err != nil {
return err
}
for k := range siMap {
_ = ic.dao.DeleteStorageItem(hash, []byte(k))
_ = ic.DAO.DeleteStorageItem(hash, []byte(k))
}
}
return nil
}
// contractGetStorageContext retrieves StorageContext of a contract.
func contractGetStorageContext(ic *interopContext, v *vm.VM) error {
func contractGetStorageContext(ic *interop.Context, v *vm.VM) error {
csInterface := v.Estack().Pop().Value()
cs, ok := csInterface.(*state.Contract)
if !ok {
return fmt.Errorf("%T is not a contract state", cs)
}
contractState, err := ic.dao.GetContractState(cs.ScriptHash())
contractState, err := ic.DAO.GetContractState(cs.ScriptHash())
if contractState == nil || err != nil {
return fmt.Errorf("contract was not created in this transaction")
}

View file

@ -10,40 +10,18 @@ package core
import (
"sort"
"github.com/nspcc-dev/neo-go/pkg/core/block"
"github.com/nspcc-dev/neo-go/pkg/core/blockchainer"
"github.com/nspcc-dev/neo-go/pkg/core/dao"
"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/core/interop"
"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"
"go.uber.org/zap"
)
type interopContext struct {
bc blockchainer.Blockchainer
trigger trigger.Type
block *block.Block
tx *transaction.Transaction
dao *dao.Cached
notifications []state.NotificationEvent
log *zap.Logger
}
func newInteropContext(trigger trigger.Type, bc blockchainer.Blockchainer, d dao.DAO, block *block.Block, tx *transaction.Transaction, log *zap.Logger) *interopContext {
dao := dao.NewCached(d)
nes := make([]state.NotificationEvent, 0)
return &interopContext{bc, trigger, block, tx, dao, nes, log}
}
// SpawnVM returns a VM with script getter and interop functions set
// up for current blockchain.
func SpawnVM(ic *interopContext) *vm.VM {
func SpawnVM(ic *interop.Context) *vm.VM {
vm := vm.New()
vm.SetScriptGetter(func(hash util.Uint160) ([]byte, bool) {
cs, err := ic.dao.GetContractState(hash)
cs, err := ic.DAO.GetContractState(hash)
if err != nil {
return nil, false
}
@ -55,31 +33,21 @@ func SpawnVM(ic *interopContext) *vm.VM {
return vm
}
// interopedFunction binds function name, id with the function itself and price,
// it's supposed to be inited once for all interopContexts, so it doesn't use
// vm.InteropFuncPrice directly.
type interopedFunction struct {
ID uint32
Name string
Func func(*interopContext, *vm.VM) error
Price int
}
// getSystemInterop returns matching interop function from the System namespace
// for a given id in the current context.
func getSystemInterop(ic *interopContext) vm.InteropGetterFunc {
func getSystemInterop(ic *interop.Context) vm.InteropGetterFunc {
return getInteropFromSlice(ic, systemInterops)
}
// getNeoInterop returns matching interop function from the Neo and AntShares
// namespaces for a given id in the current context.
func getNeoInterop(ic *interopContext) vm.InteropGetterFunc {
func getNeoInterop(ic *interop.Context) vm.InteropGetterFunc {
return getInteropFromSlice(ic, neoInterops)
}
// getInteropFromSlice returns matching interop function from the given slice of
// interop functions in the current context.
func getInteropFromSlice(ic *interopContext, slice []interopedFunction) func(uint32) *vm.InteropFuncPrice {
func getInteropFromSlice(ic *interop.Context, slice []interop.Function) func(uint32) *vm.InteropFuncPrice {
return func(id uint32) *vm.InteropFuncPrice {
n := sort.Search(len(slice), func(i int) bool {
return slice[i].ID >= id
@ -94,7 +62,7 @@ func getInteropFromSlice(ic *interopContext, slice []interopedFunction) func(uin
}
// All lists are sorted, keep 'em this way, please.
var systemInterops = []interopedFunction{
var systemInterops = []interop.Function{
{Name: "System.Block.GetTransaction", Func: blockGetTransaction, Price: 1},
{Name: "System.Block.GetTransactionCount", Func: blockGetTransactionCount, Price: 1},
{Name: "System.Block.GetTransactions", Func: blockGetTransactions, Price: 1},
@ -132,7 +100,7 @@ var systemInterops = []interopedFunction{
{Name: "System.Transaction.GetHash", Func: txGetHash, Price: 1},
}
var neoInterops = []interopedFunction{
var neoInterops = []interop.Function{
{Name: "Neo.Account.GetBalance", Func: accountGetBalance, Price: 1},
{Name: "Neo.Account.GetScriptHash", Func: accountGetScriptHash, Price: 1},
{Name: "Neo.Account.GetVotes", Func: accountGetVotes, Price: 1},
@ -278,8 +246,8 @@ var neoInterops = []interopedFunction{
}
// initIDinInteropsSlice initializes IDs from names in one given
// interopedFunction slice and then sorts it.
func initIDinInteropsSlice(iops []interopedFunction) {
// Function slice and then sorts it.
func initIDinInteropsSlice(iops []interop.Function) {
for i := range iops {
iops[i].ID = vm.InteropNameToID([]byte(iops[i].Name))
}

View file

@ -6,13 +6,14 @@ import (
"testing"
"github.com/nspcc-dev/neo-go/pkg/core/dao"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/storage"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/stretchr/testify/require"
)
func testNonInterop(t *testing.T, value interface{}, f func(*interopContext, *vm.VM) error) {
func testNonInterop(t *testing.T, value interface{}, f func(*interop.Context, *vm.VM) error) {
v := vm.New()
v.Estack().PushVal(value)
chain := newTestChain(t)
@ -30,7 +31,7 @@ func TestUnexpectedNonInterops(t *testing.T) {
}
// All of these functions expect an interop item on the stack.
funcs := []func(*interopContext, *vm.VM) error{
funcs := []func(*interop.Context, *vm.VM) error{
accountGetBalance,
accountGetScriptHash,
accountGetVotes,