Skip to content

Commit

Permalink
Script hash mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
meevee98 committed Nov 7, 2022
1 parent 92b9e1d commit 2e330a1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Neo.TestEngine/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/Neo.TestEngine/TestUtils/BuildScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Neo.IO;
using Neo.Json;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using System.Collections.Generic;
using System.IO;

Expand All @@ -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;
}
Expand Down
18 changes: 18 additions & 0 deletions src/Neo.TestEngine/TestUtils/Helper.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
8 changes: 6 additions & 2 deletions src/Neo.TestEngine/TestUtils/TestEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -188,7 +188,11 @@ public void Reset()
this.LoadScript(Nef.Script);
// Mock contract
var contextState = CurrentContext.GetState<ExecutionContextState>();
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;
}
}

Expand Down

0 comments on commit 2e330a1

Please sign in to comment.