From 49f6b33eae6f2a35043f609218f1b85870900849 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 26 Nov 2020 22:45:51 +0300 Subject: [PATCH] core: fix contract-based verification script hash When using contract-based verification it's important to load contract's hash along with the script, otherwise it won't be valid. Simplify things along the way. --- pkg/core/blockchain.go | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index b07945fe7..aceabd0a7 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -1601,18 +1601,15 @@ var ( // initVerificationVM initializes VM for witness check. func (bc *Blockchain) initVerificationVM(ic *interop.Context, hash util.Uint160, witness *transaction.Witness) error { - var offset int - var isNative bool - var initMD *manifest.Method - verification := witness.VerificationScript - flags := smartcontract.NoneFlag - if len(verification) != 0 { + v := ic.VM + if len(witness.VerificationScript) != 0 { if witness.ScriptHash() != hash { return ErrWitnessHashMismatch } if bc.contracts.ByHash(hash) != nil { return ErrNativeContractWitness } + v.LoadScriptWithFlags(witness.VerificationScript, smartcontract.NoneFlag) } else { cs, err := ic.DAO.GetContractState(hash) if err != nil { @@ -1622,26 +1619,21 @@ func (bc *Blockchain) initVerificationVM(ic *interop.Context, hash util.Uint160, if md == nil { return ErrInvalidVerificationContract } - verification = cs.Script - offset = md.Offset - initMD = cs.Manifest.ABI.GetMethod(manifest.MethodInit) - isNative = cs.ID < 0 - flags = smartcontract.AllowStates - } + initMD := cs.Manifest.ABI.GetMethod(manifest.MethodInit) + v.LoadScriptWithHash(cs.Script, hash, smartcontract.AllowStates) + v.Jump(v.Context(), md.Offset) - v := ic.VM - v.LoadScriptWithFlags(verification, flags) - v.Jump(v.Context(), offset) - if isNative { - w := io.NewBufBinWriter() - emit.Opcodes(w.BinWriter, opcode.DEPTH, opcode.PACK) - emit.String(w.BinWriter, manifest.MethodVerify) - if w.Err != nil { - return w.Err + if cs.ID < 0 { + w := io.NewBufBinWriter() + emit.Opcodes(w.BinWriter, opcode.DEPTH, opcode.PACK) + emit.String(w.BinWriter, manifest.MethodVerify) + if w.Err != nil { + return w.Err + } + v.LoadScript(w.Bytes()) + } else if initMD != nil { + v.Call(v.Context(), initMD.Offset) } - v.LoadScript(w.Bytes()) - } else if initMD != nil { - v.Call(v.Context(), initMD.Offset) } v.LoadScript(witness.InvocationScript) return nil