diff --git a/pkg/core/interop_system.go b/pkg/core/interop_system.go index a399e67e7..319c8bb14 100644 --- a/pkg/core/interop_system.go +++ b/pkg/core/interop_system.go @@ -254,7 +254,7 @@ func (ic *interopContext) engineGetScriptContainer(v *vm.VM) error { func getContextScriptHash(v *vm.VM, n int) util.Uint160 { ctxIface := v.Istack().Peek(n).Value() ctx := ctxIface.(*vm.Context) - return hash.Hash160(ctx.Program()) + return ctx.ScriptHash() } // pushContextScriptHash pushes to evaluation stack the script hash of the diff --git a/pkg/vm/context.go b/pkg/vm/context.go index bf42b4440..b8a102410 100644 --- a/pkg/vm/context.go +++ b/pkg/vm/context.go @@ -3,7 +3,9 @@ package vm import ( "errors" + "github.com/CityOfZion/neo-go/pkg/crypto/hash" "github.com/CityOfZion/neo-go/pkg/io" + "github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/vm/opcode" ) @@ -29,6 +31,9 @@ type Context struct { // Alt stack pointer. astack *Stack + + // Script hash of the prog. + scriptHash util.Uint160 } // NewContext returns a new Context object. @@ -125,6 +130,14 @@ func (c *Context) Program() []byte { 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. func (c *Context) Value() interface{} { return c