core: make SpawnVM a method of context

spawnVMWithInterops is rather long too type and
it doesn't use Blockchain in any way.
This commit is contained in:
Evgenii Stratonikov 2020-04-07 12:54:43 +03:00
parent a71cd0961e
commit ee0ba9b1b4
3 changed files with 23 additions and 21 deletions

View file

@ -14,7 +14,9 @@ import (
"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"
"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"
)
@ -35,6 +37,23 @@ func newInteropContext(trigger trigger.Type, bc Blockchainer, d dao.DAO, block *
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 (ic *interopContext) SpawnVM() *vm.VM {
vm := vm.New()
vm.SetScriptGetter(func(hash util.Uint160) ([]byte, bool) {
cs, err := ic.dao.GetContractState(hash)
if err != nil {
return nil, false
}
hasDynamicInvoke := (cs.Properties & smartcontract.HasDynamicInvoke) != 0
return cs.Script, hasDynamicInvoke
})
vm.RegisterInteropGetter(ic.getSystemInterop)
vm.RegisterInteropGetter(ic.getNeoInterop)
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.