From 2e330a1e3590dd97ed04c4912f0c1c2e1182c566 Mon Sep 17 00:00:00 2001 From: Mirella de Medeiros Date: Mon, 7 Nov 2022 19:07:31 -0300 Subject: [PATCH] Script hash mocking --- src/Neo.TestEngine/Engine.cs | 2 +- src/Neo.TestEngine/TestUtils/BuildScript.cs | 4 +++- src/Neo.TestEngine/TestUtils/Helper.cs | 18 ++++++++++++++++++ src/Neo.TestEngine/TestUtils/TestEngine.cs | 8 ++++++-- 4 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 src/Neo.TestEngine/TestUtils/Helper.cs diff --git a/src/Neo.TestEngine/Engine.cs b/src/Neo.TestEngine/Engine.cs index fae97e0e3..8b0d7abb7 100644 --- a/src/Neo.TestEngine/Engine.cs +++ b/src/Neo.TestEngine/Engine.cs @@ -91,7 +91,7 @@ public Engine AddSmartContract(TestContract contract) if (engine.ScriptContext?.Success == true) { - var hash = engine.Nef.Script.Span.ToScriptHash(); + var hash = TestHelper.GetContractHash(engine.Nef.CheckSum, ContractManifest.FromJson(engine.Manifest).Name); var snapshot = engine.Snapshot; ContractState state; diff --git a/src/Neo.TestEngine/TestUtils/BuildScript.cs b/src/Neo.TestEngine/TestUtils/BuildScript.cs index 00b4e49d9..45c234c6e 100644 --- a/src/Neo.TestEngine/TestUtils/BuildScript.cs +++ b/src/Neo.TestEngine/TestUtils/BuildScript.cs @@ -13,6 +13,7 @@ using Neo.IO; using Neo.Json; using Neo.SmartContract; +using Neo.SmartContract.Manifest; using System.Collections.Generic; using System.IO; @@ -37,7 +38,8 @@ public BuildScript(NefFile nefFile, JObject manifestJson, UInt160? originHash = if (originHash is null && nefFile != null) { - originHash = Nef.Script.Span.ToScriptHash(); + ContractManifest parsedManifest = ContractManifest.FromJson(manifestJson); + originHash = TestHelper.GetContractHash(nefFile.CheckSum, parsedManifest.Name); } ScriptHash = originHash; } diff --git a/src/Neo.TestEngine/TestUtils/Helper.cs b/src/Neo.TestEngine/TestUtils/Helper.cs new file mode 100644 index 000000000..8e0892d2f --- /dev/null +++ b/src/Neo.TestEngine/TestUtils/Helper.cs @@ -0,0 +1,18 @@ +using Neo.VM; +using Neo.SmartContract; + +namespace Neo.TestingEngine +{ + internal class TestHelper + { + public static UInt160 GetContractHash(uint nefCheckSum, string name) + { + using var sb = new ScriptBuilder(); + sb.Emit(OpCode.ABORT); + sb.EmitPush(nefCheckSum); + sb.EmitPush(name); + + return sb.ToArray().ToScriptHash(); + } + } +} diff --git a/src/Neo.TestEngine/TestUtils/TestEngine.cs b/src/Neo.TestEngine/TestUtils/TestEngine.cs index 472358e15..69544be2a 100644 --- a/src/Neo.TestEngine/TestUtils/TestEngine.cs +++ b/src/Neo.TestEngine/TestUtils/TestEngine.cs @@ -137,7 +137,7 @@ public bool AddEntryScript(UInt160 contractHash) public bool AddEntryScript(BuildScript script) { - var contractHash = script.Nef.Script.Span.ToScriptHash(); + var contractHash = TestHelper.GetContractHash(script.Nef.CheckSum, ContractManifest.FromJson(script.Manifest).Name); var contract = NativeContract.ContractManagement.GetContract(Snapshot, contractHash); if (contract != null) @@ -188,7 +188,11 @@ public void Reset() this.LoadScript(Nef.Script); // Mock contract var contextState = CurrentContext.GetState(); - contextState.Contract ??= new ContractState { Nef = Nef }; + var contractManifest = ContractManifest.FromJson(Manifest); + var contractHash = TestHelper.GetContractHash(Nef.CheckSum, contractManifest.Name); + + contextState.Contract ??= new ContractState { Nef = Nef, Manifest = contractManifest, Hash = contractHash }; + contextState.ScriptHash = contractHash; } }