vm, core: move invocation counter from InteropContext to VM

This commit is contained in:
Anna Shaleva 2020-10-05 13:08:55 +03:00
parent 45bfce60a5
commit 6ce00fde82
5 changed files with 8 additions and 6 deletions

View file

@ -35,7 +35,6 @@ type Context struct {
DAO *dao.Cached DAO *dao.Cached
Notifications []state.NotificationEvent Notifications []state.NotificationEvent
Log *zap.Logger Log *zap.Logger
Invocations map[util.Uint160]int
VM *vm.VM VM *vm.VM
Functions [][]Function Functions [][]Function
} }
@ -53,7 +52,6 @@ func NewContext(trigger trigger.Type, bc blockchainer.Blockchainer, d dao.DAO, n
DAO: dao, DAO: dao,
Notifications: nes, Notifications: nes,
Log: log, Log: log,
Invocations: make(map[util.Uint160]int),
// Functions is a slice of slices of interops sorted by ID. // Functions is a slice of slices of interops sorted by ID.
Functions: [][]Function{}, Functions: [][]Function{},
} }

View file

@ -67,7 +67,7 @@ func CallExInternal(ic *interop.Context, cs *state.Contract,
} }
u := cs.ScriptHash() u := cs.ScriptHash()
ic.Invocations[u]++ ic.VM.Invocations[u]++
ic.VM.LoadScriptWithHash(cs.Script, u, ic.VM.Context().GetCallFlags()&f) ic.VM.LoadScriptWithHash(cs.Script, u, ic.VM.Context().GetCallFlags()&f)
var isNative bool var isNative bool
for i := range ic.Natives { for i := range ic.Natives {

View file

@ -59,10 +59,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.Invocations[currentScriptHash] count, ok := ic.VM.Invocations[currentScriptHash]
if !ok { if !ok {
count = 1 count = 1
ic.Invocations[currentScriptHash] = count ic.VM.Invocations[currentScriptHash] = count
} }
ic.VM.Estack().PushVal(count) ic.VM.Estack().PushVal(count)
return nil return nil

View file

@ -296,7 +296,7 @@ func TestRuntimeGetInvocationCounter(t *testing.T) {
v, ic, chain := createVM(t) v, ic, chain := createVM(t)
defer chain.Close() defer chain.Close()
ic.Invocations[hash.Hash160([]byte{2})] = 42 ic.VM.Invocations[hash.Hash160([]byte{2})] = 42
t.Run("No invocations", func(t *testing.T) { t.Run("No invocations", func(t *testing.T) {
v.LoadScript([]byte{1}) v.LoadScript([]byte{1})

View file

@ -80,6 +80,9 @@ type VM struct {
SyscallHandler func(v *VM, id uint32) error SyscallHandler func(v *VM, id uint32) 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.
@ -96,6 +99,7 @@ func NewWithTrigger(t trigger.Type) *VM {
trigger: t, trigger: t,
SyscallHandler: defaultSyscallHandler, SyscallHandler: defaultSyscallHandler,
Invocations: make(map[util.Uint160]int),
} }
vm.estack = vm.newItemStack("evaluation") vm.estack = vm.newItemStack("evaluation")