interop: move invocation counter from VM to Context
It's created for interop and used by interop, VM doesn't care.
This commit is contained in:
parent
29cda5112a
commit
83d0b2f465
6 changed files with 8 additions and 10 deletions
|
@ -48,6 +48,7 @@ type Context struct {
|
||||||
Log *zap.Logger
|
Log *zap.Logger
|
||||||
VM *vm.VM
|
VM *vm.VM
|
||||||
Functions []Function
|
Functions []Function
|
||||||
|
Invocations map[util.Uint160]int
|
||||||
cancelFuncs []context.CancelFunc
|
cancelFuncs []context.CancelFunc
|
||||||
getContract func(dao.DAO, util.Uint160) (*state.Contract, error)
|
getContract func(dao.DAO, util.Uint160) (*state.Contract, error)
|
||||||
baseExecFee int64
|
baseExecFee int64
|
||||||
|
@ -73,6 +74,7 @@ func NewContext(trigger trigger.Type, bc blockchainer.Blockchainer, d dao.DAO,
|
||||||
Tx: tx,
|
Tx: tx,
|
||||||
DAO: dao,
|
DAO: dao,
|
||||||
Log: log,
|
Log: log,
|
||||||
|
Invocations: make(map[util.Uint160]int),
|
||||||
getContract: getContract,
|
getContract: getContract,
|
||||||
baseExecFee: baseExecFee,
|
baseExecFee: baseExecFee,
|
||||||
}
|
}
|
||||||
|
|
|
@ -118,7 +118,7 @@ func callExFromNative(ic *interop.Context, caller util.Uint160, cs *state.Contra
|
||||||
if md != nil {
|
if md != nil {
|
||||||
initOff = md.Offset
|
initOff = md.Offset
|
||||||
}
|
}
|
||||||
ic.VM.Invocations[cs.Hash]++
|
ic.Invocations[cs.Hash]++
|
||||||
ic.VM.LoadNEFMethod(&cs.NEF, caller, cs.Hash, ic.VM.Context().GetCallFlags()&f,
|
ic.VM.LoadNEFMethod(&cs.NEF, caller, cs.Hash, ic.VM.Context().GetCallFlags()&f,
|
||||||
hasReturn, methodOff, initOff)
|
hasReturn, methodOff, initOff)
|
||||||
|
|
||||||
|
|
|
@ -63,10 +63,10 @@ func GetNotifications(ic *interop.Context) error {
|
||||||
// GetInvocationCounter returns how many times current contract was invoked during current tx execution.
|
// GetInvocationCounter returns how many times current contract was invoked during current tx execution.
|
||||||
func GetInvocationCounter(ic *interop.Context) error {
|
func GetInvocationCounter(ic *interop.Context) error {
|
||||||
currentScriptHash := ic.VM.GetCurrentScriptHash()
|
currentScriptHash := ic.VM.GetCurrentScriptHash()
|
||||||
count, ok := ic.VM.Invocations[currentScriptHash]
|
count, ok := ic.Invocations[currentScriptHash]
|
||||||
if !ok {
|
if !ok {
|
||||||
count = 1
|
count = 1
|
||||||
ic.VM.Invocations[currentScriptHash] = count
|
ic.Invocations[currentScriptHash] = count
|
||||||
}
|
}
|
||||||
ic.VM.Estack().PushItem(stackitem.NewBigInteger(big.NewInt(int64(count))))
|
ic.VM.Estack().PushItem(stackitem.NewBigInteger(big.NewInt(int64(count))))
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -97,9 +97,9 @@ func TestRuntimeGetNotifications(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRuntimeGetInvocationCounter(t *testing.T) {
|
func TestRuntimeGetInvocationCounter(t *testing.T) {
|
||||||
ic := &interop.Context{VM: vm.New()}
|
ic := &interop.Context{VM: vm.New(), Invocations: make(map[util.Uint160]int)}
|
||||||
h := random.Uint160()
|
h := random.Uint160()
|
||||||
ic.VM.Invocations[h] = 42
|
ic.Invocations[h] = 42
|
||||||
|
|
||||||
t.Run("No invocations", func(t *testing.T) {
|
t.Run("No invocations", func(t *testing.T) {
|
||||||
h1 := h
|
h1 := h
|
||||||
|
|
|
@ -234,7 +234,7 @@ func TestRuntimeGetInvocationCounter(t *testing.T) {
|
||||||
cs, _ := getTestContractState(bc)
|
cs, _ := getTestContractState(bc)
|
||||||
require.NoError(t, bc.contracts.Management.PutContractState(ic.DAO, cs))
|
require.NoError(t, bc.contracts.Management.PutContractState(ic.DAO, cs))
|
||||||
|
|
||||||
ic.VM.Invocations[hash.Hash160([]byte{2})] = 42
|
ic.Invocations[hash.Hash160([]byte{2})] = 42
|
||||||
|
|
||||||
t.Run("No invocations", func(t *testing.T) {
|
t.Run("No invocations", func(t *testing.T) {
|
||||||
v.Load([]byte{1})
|
v.Load([]byte{1})
|
||||||
|
|
|
@ -85,9 +85,6 @@ type VM struct {
|
||||||
LoadToken func(id int32) error
|
LoadToken func(id int32) error
|
||||||
|
|
||||||
trigger trigger.Type
|
trigger trigger.Type
|
||||||
|
|
||||||
// Invocations is a script invocation counter.
|
|
||||||
Invocations map[util.Uint160]int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a new VM object ready to load AVM bytecode scripts.
|
// New returns a new VM object ready to load AVM bytecode scripts.
|
||||||
|
@ -102,7 +99,6 @@ func NewWithTrigger(t trigger.Type) *VM {
|
||||||
trigger: t,
|
trigger: t,
|
||||||
|
|
||||||
SyscallHandler: defaultSyscallHandler,
|
SyscallHandler: defaultSyscallHandler,
|
||||||
Invocations: make(map[util.Uint160]int),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
initStack(&vm.istack, "invocation", nil)
|
initStack(&vm.istack, "invocation", nil)
|
||||||
|
|
Loading…
Reference in a new issue