core: move interopContext to a separate package
This commit is contained in:
parent
90a08986d6
commit
7ffc6c0936
7 changed files with 225 additions and 204 deletions
|
@ -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))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue