From 83d0b2f465116bdb1a7bc0898b58ca9fa825bcd5 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 19 Nov 2021 20:25:58 +0300 Subject: [PATCH] interop: move invocation counter from VM to Context It's created for interop and used by interop, VM doesn't care. --- pkg/core/interop/context.go | 2 ++ pkg/core/interop/contract/call.go | 2 +- pkg/core/interop/runtime/util.go | 4 ++-- pkg/core/interop/runtime/util_test.go | 4 ++-- pkg/core/interop_system_test.go | 2 +- pkg/vm/vm.go | 4 ---- 6 files changed, 8 insertions(+), 10 deletions(-) diff --git a/pkg/core/interop/context.go b/pkg/core/interop/context.go index ce6ba1550..daa202b94 100644 --- a/pkg/core/interop/context.go +++ b/pkg/core/interop/context.go @@ -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, } diff --git a/pkg/core/interop/contract/call.go b/pkg/core/interop/contract/call.go index 484a857cb..7daf13073 100644 --- a/pkg/core/interop/contract/call.go +++ b/pkg/core/interop/contract/call.go @@ -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) diff --git a/pkg/core/interop/runtime/util.go b/pkg/core/interop/runtime/util.go index d14684e66..efc844b35 100644 --- a/pkg/core/interop/runtime/util.go +++ b/pkg/core/interop/runtime/util.go @@ -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 diff --git a/pkg/core/interop/runtime/util_test.go b/pkg/core/interop/runtime/util_test.go index 4827498c4..88b1fbdd2 100644 --- a/pkg/core/interop/runtime/util_test.go +++ b/pkg/core/interop/runtime/util_test.go @@ -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 diff --git a/pkg/core/interop_system_test.go b/pkg/core/interop_system_test.go index 185955440..06e75656d 100644 --- a/pkg/core/interop_system_test.go +++ b/pkg/core/interop_system_test.go @@ -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}) diff --git a/pkg/vm/vm.go b/pkg/vm/vm.go index 42209fb5f..303c8445a 100644 --- a/pkg/vm/vm.go +++ b/pkg/vm/vm.go @@ -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)