vm: add cached ScriptHash() to Context

Avoid recalculating it over and over again in interop.
This commit is contained in:
Roman Khimov 2019-12-13 17:05:03 +03:00
parent 0afaff2f79
commit 0abd55c2c2
2 changed files with 14 additions and 1 deletions

View file

@ -254,7 +254,7 @@ func (ic *interopContext) engineGetScriptContainer(v *vm.VM) error {
func getContextScriptHash(v *vm.VM, n int) util.Uint160 { func getContextScriptHash(v *vm.VM, n int) util.Uint160 {
ctxIface := v.Istack().Peek(n).Value() ctxIface := v.Istack().Peek(n).Value()
ctx := ctxIface.(*vm.Context) ctx := ctxIface.(*vm.Context)
return hash.Hash160(ctx.Program()) return ctx.ScriptHash()
} }
// pushContextScriptHash pushes to evaluation stack the script hash of the // pushContextScriptHash pushes to evaluation stack the script hash of the

View file

@ -3,7 +3,9 @@ package vm
import ( import (
"errors" "errors"
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/util"
"github.com/CityOfZion/neo-go/pkg/vm/opcode" "github.com/CityOfZion/neo-go/pkg/vm/opcode"
) )
@ -29,6 +31,9 @@ type Context struct {
// Alt stack pointer. // Alt stack pointer.
astack *Stack astack *Stack
// Script hash of the prog.
scriptHash util.Uint160
} }
// NewContext returns a new Context object. // NewContext returns a new Context object.
@ -125,6 +130,14 @@ func (c *Context) Program() []byte {
return c.prog return c.prog
} }
// ScriptHash returns a hash of the script in the current context.
func (c *Context) ScriptHash() util.Uint160 {
if c.scriptHash.Equals(util.Uint160{}) {
c.scriptHash = hash.Hash160(c.prog)
}
return c.scriptHash
}
// Value implements StackItem interface. // Value implements StackItem interface.
func (c *Context) Value() interface{} { func (c *Context) Value() interface{} {
return c return c