Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use nef file as hash #388

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/Neo.Compiler.MSIL/DebugExport.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -117,13 +114,13 @@ private static JArray GetDocuments(IReadOnlyDictionary<string, int> docMap)
return outjson;
}

public static JObject Export(NeoModule module, byte[] script, IReadOnlyDictionary<int, int> addrConvTable)
public static JObject Export(NeoModule module, UInt160 hash, IReadOnlyDictionary<int, int> addrConvTable)
{
var docMap = GetDocumentMap(module);
addrConvTable ??= ImmutableDictionary<int, int>.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);
Expand Down
21 changes: 2 additions & 19 deletions src/Neo.Compiler.MSIL/FuncExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Neo.Compiler
{
Expand Down Expand Up @@ -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<int, int> addrConvTable)
public static JObject Export(NeoModule module, UInt160 hash, Dictionary<int, int> addrConvTable)
{
var outjson = new JObject();

//hash
outjson["hash"] = ComputeHash(script);
outjson["hash"] = hash.ToString();

//functions
var methods = new JArray();
Expand Down
49 changes: 26 additions & 23 deletions src/Neo.Compiler.MSIL/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -156,6 +157,8 @@ public static int Compile(Options options, ILogger log = null)
int bSucc = 0;
string debugstr = null;
NeoModule module;
UInt160 hash;
Dictionary<int, int> addrConvTable = null;

// Convert and build
try
Expand All @@ -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<int, int> addrConvTable = null;
if (options.Optimize)
{
HashSet<int> entryPoints = new HashSet<int>();
Expand All @@ -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)
{
Expand All @@ -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))
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ContractCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
});

Expand Down
2 changes: 1 addition & 1 deletion tests/Neo.Compiler.MSIL.UnitTests/Utils/BuildNEF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
44 changes: 37 additions & 7 deletions tests/Neo.Compiler.MSIL.UnitTests/Utils/BuildScript.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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; }
Expand All @@ -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;
Expand Down Expand Up @@ -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<int> entryPoints = new List<int>();
Expand All @@ -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;
}
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Neo.Compiler.MSIL.UnitTests/Utils/TestEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down