From bec8f2a216fc8314a060d1d2adc5032a133650cf Mon Sep 17 00:00:00 2001 From: Shargon Date: Sat, 7 Nov 2020 10:41:43 +0100 Subject: [PATCH] Use nef as hash --- src/Neo.Compiler.MSIL/DebugExport.cs | 7 +-- src/Neo.Compiler.MSIL/FuncExport.cs | 21 +------- src/Neo.Compiler.MSIL/Program.cs | 49 ++++++++++--------- .../UnitTest_ContractCall.cs | 2 +- .../Utils/BuildNEF.cs | 2 +- .../Utils/BuildScript.cs | 44 ++++++++++++++--- .../Utils/TestEngine.cs | 2 +- .../Services/Neo/ContractTest.cs | 10 ++-- .../Services/System/BinaryTest.cs | 4 +- .../Services/System/CallbackTest.cs | 4 +- .../Services/System/ExecutionEngineTest.cs | 2 +- 11 files changed, 80 insertions(+), 67 deletions(-) diff --git a/src/Neo.Compiler.MSIL/DebugExport.cs b/src/Neo.Compiler.MSIL/DebugExport.cs index 14f34e2a2..ed544ceff 100644 --- a/src/Neo.Compiler.MSIL/DebugExport.cs +++ b/src/Neo.Compiler.MSIL/DebugExport.cs @@ -1,11 +1,8 @@ -using Mono.Cecil.Cil; using Neo.IO.Json; -using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics; using System.Linq; -using System.Text; namespace Neo.Compiler { @@ -117,13 +114,13 @@ private static JArray GetDocuments(IReadOnlyDictionary docMap) return outjson; } - public static JObject Export(NeoModule module, byte[] script, IReadOnlyDictionary addrConvTable) + public static JObject Export(NeoModule module, UInt160 hash, IReadOnlyDictionary addrConvTable) { var docMap = GetDocumentMap(module); addrConvTable ??= ImmutableDictionary.Empty; var outjson = new JObject(); - outjson["hash"] = FuncExport.ComputeHash(script); + outjson["hash"] = hash.ToString(); // outjson["entrypoint"]= module.mainMethod; outjson["documents"] = GetDocuments(docMap); outjson["methods"] = GetMethods(module, docMap, addrConvTable); diff --git a/src/Neo.Compiler.MSIL/FuncExport.cs b/src/Neo.Compiler.MSIL/FuncExport.cs index d9bf4033e..cfa1ccbf2 100644 --- a/src/Neo.Compiler.MSIL/FuncExport.cs +++ b/src/Neo.Compiler.MSIL/FuncExport.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; namespace Neo.Compiler { @@ -59,28 +58,12 @@ internal static string ConvType(TypeReference t) return "Unknown:" + type; } - public static string ComputeHash(byte[] script) - { - var sha256 = System.Security.Cryptography.SHA256.Create(); - byte[] hash256 = sha256.ComputeHash(script); - var ripemd160 = new Neo.Cryptography.RIPEMD160Managed(); - var hash = ripemd160.ComputeHash(hash256); - - StringBuilder sb = new StringBuilder(); - sb.Append("0x"); - for (int i = hash.Length - 1; i >= 0; i--) - { - sb.Append(hash[i].ToString("x02")); - } - return sb.ToString(); - } - - public static JObject Export(NeoModule module, byte[] script, Dictionary addrConvTable) + public static JObject Export(NeoModule module, UInt160 hash, Dictionary addrConvTable) { var outjson = new JObject(); //hash - outjson["hash"] = ComputeHash(script); + outjson["hash"] = hash.ToString(); //functions var methods = new JArray(); diff --git a/src/Neo.Compiler.MSIL/Program.cs b/src/Neo.Compiler.MSIL/Program.cs index 31cd8f2c6..4d9c287fd 100644 --- a/src/Neo.Compiler.MSIL/Program.cs +++ b/src/Neo.Compiler.MSIL/Program.cs @@ -2,6 +2,7 @@ using Mono.Cecil; using Neo.Compiler.MSIL; using Neo.Compiler.Optimizer; +using Neo.IO; using Neo.IO.Json; using Neo.SmartContract; using Neo.SmartContract.Manifest; @@ -156,6 +157,8 @@ public static int Compile(Options options, ILogger log = null) int bSucc = 0; string debugstr = null; NeoModule module; + UInt160 hash; + Dictionary addrConvTable = null; // Convert and build try @@ -165,7 +168,6 @@ public static int Compile(Options options, ILogger log = null) module = conv.Convert(mod, option); bytes = module.Build(); log.Log("convert succ"); - Dictionary addrConvTable = null; if (options.Optimize) { HashSet entryPoints = new HashSet(); @@ -177,28 +179,6 @@ public static int Compile(Options options, ILogger log = null) log.Log("optimization succ " + (((bytes.Length / (optimize.Length + 0.0)) * 100.0) - 100).ToString("0.00 '%'")); bytes = optimize; } - - try - { - abi = FuncExport.Export(module, bytes, addrConvTable); - log.Log("gen abi succ"); - } - catch (Exception err) - { - log.Log("gen abi Error:" + err.ToString()); - return -1; - } - - try - { - var outjson = DebugExport.Export(module, bytes, addrConvTable); - debugstr = outjson.ToString(false); - log.Log("gen debug succ"); - } - catch (Exception err) - { - log.Log("gen debug Error:" + err.ToString()); - } } catch (Exception err) { @@ -220,6 +200,7 @@ public static int Compile(Options options, ILogger log = null) ScriptHash = bytes.ToScriptHash() }; nef.CheckSum = NefFile.ComputeChecksum(nef); + hash = nef.ToArray().ToScriptHash(); File.Delete(bytesname); using (var stream = File.OpenWrite(bytesname)) @@ -236,6 +217,28 @@ public static int Compile(Options options, ILogger log = null) return -1; } + try + { + abi = FuncExport.Export(module, hash, addrConvTable); + log.Log("gen abi succ"); + } + catch (Exception err) + { + log.Log("gen abi Error:" + err.ToString()); + return -1; + } + + try + { + var outjson = DebugExport.Export(module, hash, addrConvTable); + debugstr = outjson.ToString(false); + log.Log("gen debug succ"); + } + catch (Exception err) + { + log.Log("gen debug Error:" + err.ToString()); + } + try { var sbABI = abi.ToString(false); diff --git a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ContractCall.cs b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ContractCall.cs index 5c10863f9..f557f7eff 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ContractCall.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ContractCall.cs @@ -19,7 +19,7 @@ public void Init() _engine = new TestEngine(); _engine.Snapshot.Contracts.Add(hash, new Ledger.ContractState() { - Script = _engine.Build("./TestClasses/Contract1.cs").finalNEF, + Script = _engine.Build("./TestClasses/Contract1.cs").finalNEFScript, Manifest = ContractManifest.FromJson(JObject.Parse(_engine.Build("./TestClasses/Contract1.cs").finalManifest)), }); diff --git a/tests/Neo.Compiler.MSIL.UnitTests/Utils/BuildNEF.cs b/tests/Neo.Compiler.MSIL.UnitTests/Utils/BuildNEF.cs index 8ce791f5f..909bee8ee 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/Utils/BuildNEF.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/Utils/BuildNEF.cs @@ -11,7 +11,7 @@ public BuildNEF(NefFile nefFile, string manifestFile) : base() IsBuild = true; UseOptimizer = false; Error = null; - finalNEF = nefFile.Script; + finalNEFScript = nefFile.Script; JObject manifestAbi = JObject.Parse(manifestFile); var abi = manifestAbi["abi"] as JObject; finalABI = abi; diff --git a/tests/Neo.Compiler.MSIL.UnitTests/Utils/BuildScript.cs b/tests/Neo.Compiler.MSIL.UnitTests/Utils/BuildScript.cs index d91317a69..fe4939549 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/Utils/BuildScript.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/Utils/BuildScript.cs @@ -1,8 +1,11 @@ using Neo.Compiler.Optimizer; +using Neo.IO; using Neo.IO.Json; +using Neo.SmartContract; using System; using System.Collections.Generic; using System.IO; +using System.Reflection; namespace Neo.Compiler.MSIL.UnitTests.Utils { @@ -13,7 +16,7 @@ public class BuildScript public Exception Error { get; protected set; } public ILModule modIL { get; private set; } public ModuleConverter converterIL { get; private set; } - public byte[] finalNEF { get; protected set; } + public byte[] finalNEFScript { get; protected set; } public JObject finalABI { get; protected set; } public string finalManifest { get; protected set; } public JObject debugInfo { get; private set; } @@ -22,6 +25,20 @@ public BuildScript() { } + public NefFile GetNef() + { + var nef = new NefFile + { + Compiler = "neon", + Version = Version.Parse(((AssemblyFileVersionAttribute)Assembly.GetAssembly(typeof(Program)) + .GetCustomAttribute(typeof(AssemblyFileVersionAttribute))).Version), + Script = finalNEFScript, + ScriptHash = finalNEFScript.ToScriptHash() + }; + nef.CheckSum = NefFile.ComputeChecksum(nef); + return nef; + } + public void Build(Stream fs, Stream fspdb, bool optimizer) { this.IsBuild = false; @@ -50,7 +67,7 @@ public void Build(Stream fs, Stream fspdb, bool optimizer) #endif { converterIL.Convert(modIL, option); - finalNEF = converterIL.outModule.Build(); + finalNEFScript = converterIL.outModule.Build(); if (optimizer) { List entryPoints = new List(); @@ -59,10 +76,10 @@ public void Build(Stream fs, Stream fspdb, bool optimizer) if (!entryPoints.Contains(f.funcaddr)) entryPoints.Add(f.funcaddr); } - var opbytes = NefOptimizeTool.Optimize(finalNEF, entryPoints.ToArray(), out addrConvTable); - float ratio = (opbytes.Length * 100.0f) / (float)finalNEF.Length; + var opbytes = NefOptimizeTool.Optimize(finalNEFScript, entryPoints.ToArray(), out addrConvTable); + float ratio = (opbytes.Length * 100.0f) / (float)finalNEFScript.Length; log.Log("optimization ratio = " + ratio + "%"); - finalNEF = opbytes; + finalNEFScript = opbytes; } IsBuild = true; } @@ -74,9 +91,22 @@ public void Build(Stream fs, Stream fspdb, bool optimizer) return; } #endif + UInt160 hash; + + try + { + hash = GetNef().ToArray().ToScriptHash(); + } + catch (Exception err) + { + log.Log("Gen Abi Error:" + err.ToString()); + this.Error = err; + return; + } + try { - finalABI = FuncExport.Export(converterIL.outModule, finalNEF, addrConvTable); + finalABI = FuncExport.Export(converterIL.outModule, hash, addrConvTable); } catch (Exception err) { @@ -87,7 +117,7 @@ public void Build(Stream fs, Stream fspdb, bool optimizer) try { - debugInfo = DebugExport.Export(converterIL.outModule, finalNEF, addrConvTable); + debugInfo = DebugExport.Export(converterIL.outModule, hash, addrConvTable); } catch (Exception err) { diff --git a/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestEngine.cs b/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestEngine.cs index ebc138812..e3a9e843e 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestEngine.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestEngine.cs @@ -96,7 +96,7 @@ public void Reset() { this.ResultStack.Pop(); } - if (ScriptEntry != null) this.LoadScript(ScriptEntry.finalNEF); + if (ScriptEntry != null) this.LoadScript(ScriptEntry.finalNEFScript); } public class ContractMethod diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/ContractTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/ContractTest.cs index 398b15eb9..e786a6915 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/ContractTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/ContractTest.cs @@ -39,14 +39,14 @@ public void Test_CreateCallDestroy() // Create _engine.Reset(); - result = _engine.ExecuteTestCaseStandard("create", script.finalNEF, manifest.ToJson().ToString()); + result = _engine.ExecuteTestCaseStandard("create", script.finalNEFScript, manifest.ToJson().ToString()); Assert.AreEqual(VMState.HALT, _engine.State); Assert.AreEqual(1, result.Count); var item = result.Pop(); Assert.IsInstanceOfType(item, typeof(Array)); var itemArray = item as Array; - Assert.AreEqual(script.finalNEF, itemArray[0]); // Script + Assert.AreEqual(script.finalNEFScript, itemArray[0]); // Script Assert.AreEqual(manifest.ToString(), itemArray[1].GetString()); // Manifest Assert.AreEqual(false, itemArray[2]); // HasStorage Assert.AreEqual(false, itemArray[3]); // Payable @@ -102,14 +102,14 @@ public void Test_Update() // Create _engine.Reset(); - result = _engine.ExecuteTestCaseStandard("create", script.finalNEF, manifest.ToJson().ToString()); + result = _engine.ExecuteTestCaseStandard("create", script.finalNEFScript, manifest.ToJson().ToString()); Assert.AreEqual(VMState.HALT, _engine.State); Assert.AreEqual(1, result.Count); var item = result.Pop(); Assert.IsInstanceOfType(item, typeof(Array)); var itemArray = item as Array; - Assert.AreEqual(script.finalNEF, itemArray[0]); // Script + Assert.AreEqual(script.finalNEFScript, itemArray[0]); // Script Assert.AreEqual(manifest.ToString(), itemArray[1].GetString()); // Manifest Assert.AreEqual(false, itemArray[2]); // HasStorage Assert.AreEqual(false, itemArray[3]); // Payable @@ -119,7 +119,7 @@ public void Test_Update() _engine.Reset(); var args = new Array { - scriptUpdate.finalNEF, + scriptUpdate.finalNEFScript, manifestUpdate.ToJson().ToString() }; result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray(), "oldContract", args); diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/System/BinaryTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/System/BinaryTest.cs index 857d4fc45..83d0051cb 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/System/BinaryTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/System/BinaryTest.cs @@ -24,11 +24,11 @@ public void Init() _engine = new TestEngine(TriggerType.Application, snapshot: snapshot); _engine.AddEntryScript("./TestClasses/Contract_Binary.cs"); - scriptHash = _engine.ScriptEntry.finalNEF.ToScriptHash(); + scriptHash = _engine.ScriptEntry.finalNEFScript.ToScriptHash(); snapshot.Contracts.Add(scriptHash, new ContractState() { - Script = _engine.ScriptEntry.finalNEF, + Script = _engine.ScriptEntry.finalNEFScript, Manifest = ContractManifest.Parse(_engine.ScriptEntry.finalManifest) }); } diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/System/CallbackTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/System/CallbackTest.cs index 5f46a605a..a74afe5e7 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/System/CallbackTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/System/CallbackTest.cs @@ -27,11 +27,11 @@ public void Init() _engine = new TestEngine(TriggerType.Application, snapshot: snapshot); _engine.AddEntryScript("./TestClasses/Contract_Callback.cs"); - scriptHash = _engine.ScriptEntry.finalNEF.ToScriptHash(); + scriptHash = _engine.ScriptEntry.finalNEFScript.ToScriptHash(); snapshot.Contracts.Add(scriptHash, new ContractState() { - Script = _engine.ScriptEntry.finalNEF, + Script = _engine.ScriptEntry.finalNEFScript, Manifest = ContractManifest.Parse(_engine.ScriptEntry.finalManifest) }); } diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/System/ExecutionEngineTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/System/ExecutionEngineTest.cs index 3c4e3317a..135dbcfdc 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/System/ExecutionEngineTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/System/ExecutionEngineTest.cs @@ -49,7 +49,7 @@ public void Init() { _engine = new TestEngine(TriggerType.Application, new DummyVerificable()); _engine.AddEntryScript("./TestClasses/Contract_ExecutionEngine.cs"); - scriptHash = _engine.ScriptEntry.finalNEF.ToScriptHash().ToArray().ToHexString(); + scriptHash = _engine.ScriptEntry.finalNEFScript.ToScriptHash().ToArray().ToHexString(); } [TestMethod]