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:
Roman Khimov 2021-11-19 20:25:58 +03:00
parent 29cda5112a
commit 83d0b2f465
6 changed files with 8 additions and 10 deletions

View file

@ -48,6 +48,7 @@ type Context struct {
Log *zap.Logger
VM *vm.VM
Functions []Function
Invocations map[util.Uint160]int
cancelFuncs []context.CancelFunc
getContract func(dao.DAO, util.Uint160) (*state.Contract, error)
baseExecFee int64
@ -73,6 +74,7 @@ func NewContext(trigger trigger.Type, bc blockchainer.Blockchainer, d dao.DAO,
Tx: tx,
DAO: dao,
Log: log,
Invocations: make(map[util.Uint160]int),
getContract: getContract,
baseExecFee: baseExecFee,
}

View file

@ -118,7 +118,7 @@ func callExFromNative(ic *interop.Context, caller util.Uint160, cs *state.Contra
if md != nil {
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,
hasReturn, methodOff, initOff)

View file

@ -63,10 +63,10 @@ func GetNotifications(ic *interop.Context) error {
// GetInvocationCounter returns how many times current contract was invoked during current tx execution.
func GetInvocationCounter(ic *interop.Context) error {
currentScriptHash := ic.VM.GetCurrentScriptHash()
count, ok := ic.VM.Invocations[currentScriptHash]
count, ok := ic.Invocations[currentScriptHash]
if !ok {
count = 1
ic.VM.Invocations[currentScriptHash] = count
ic.Invocations[currentScriptHash] = count
}
ic.VM.Estack().PushItem(stackitem.NewBigInteger(big.NewInt(int64(count))))
return nil

View file

@ -97,9 +97,9 @@ func TestRuntimeGetNotifications(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()
ic.VM.Invocations[h] = 42
ic.Invocations[h] = 42
t.Run("No invocations", func(t *testing.T) {
h1 := h

View file

@ -234,7 +234,7 @@ func TestRuntimeGetInvocationCounter(t *testing.T) {
cs, _ := getTestContractState(bc)
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) {
v.Load([]byte{1})

View file

@ -85,9 +85,6 @@ type VM struct {
LoadToken func(id int32) error
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.
@ -102,7 +99,6 @@ func NewWithTrigger(t trigger.Type) *VM {
trigger: t,
SyscallHandler: defaultSyscallHandler,
Invocations: make(map[util.Uint160]int),
}
initStack(&vm.istack, "invocation", nil)