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

Update Neo nuget #325

Merged
merged 27 commits into from
Jul 14, 2020
Merged
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
69 changes: 69 additions & 0 deletions src/Neo.Compiler.MSIL/FuncExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Neo.SmartContract.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Neo.Compiler
Expand Down Expand Up @@ -155,5 +156,73 @@ public static MyJson.JsonNode_Object Export(NeoModule module, byte[] script, Dic

return outjson;
}

private static object BuildSupportedStandards(Mono.Collections.Generic.Collection<CustomAttributeArgument> supportedStandardsAttribute)
{
if (supportedStandardsAttribute == null || supportedStandardsAttribute.Count == 0)
{
return "[]";
}

var entry = supportedStandardsAttribute.First();
string extra = "[";
foreach (var item in entry.Value as CustomAttributeArgument[])
{
extra += ($"\"{ScapeJson(item.Value.ToString())}\",");
}
extra = extra[0..^1];
extra += "]";

return extra;
}

private static string BuildExtraAttributes(List<Mono.Collections.Generic.Collection<CustomAttributeArgument>> extraAttributes)
{
if (extraAttributes == null || extraAttributes.Count == 0)
{
return "null";
}

string extra = "{";
foreach (var extraAttribute in extraAttributes)
{
var key = ScapeJson(extraAttribute[0].Value.ToString());
var value = ScapeJson(extraAttribute[1].Value.ToString());
extra += ($"\"{key}\":\"{value}\",");
}
extra = extra[0..^1];
extra += "}";

return extra;
}

private static string ScapeJson(string value)
{
return value.Replace("\"", "");
}

public static string GenerateManifest(MyJson.JsonNode_Object abi, NeoModule module)
{
StringBuilder sbABI = new StringBuilder();
abi.ConvertToStringWithFormat(sbABI, 0);

var features = module == null ? ContractFeatures.NoProperty : module.attributes
.Where(u => u.AttributeType.Name == "FeaturesAttribute")
.Select(u => (ContractFeatures)u.ConstructorArguments.FirstOrDefault().Value)
.FirstOrDefault();

var extraAttributes = module == null ? new List<Mono.Collections.Generic.Collection<CustomAttributeArgument>>() : module.attributes.Where(u => u.AttributeType.Name == "ManifestExtraAttribute").Select(attribute => attribute.ConstructorArguments).ToList();
var supportedStandardsAttribute = module?.attributes.Where(u => u.AttributeType.Name == "SupportedStandardsAttribute").Select(attribute => attribute.ConstructorArguments).FirstOrDefault();

var extra = BuildExtraAttributes(extraAttributes);
var supportedStandards = BuildSupportedStandards(supportedStandardsAttribute);
var storage = features.HasFlag(ContractFeatures.HasStorage).ToString().ToLowerInvariant();
var payable = features.HasFlag(ContractFeatures.Payable).ToString().ToLowerInvariant();

return
@"{""groups"":[],""features"":{""storage"":" + storage + @",""payable"":" + payable + @"},""abi"":" +
sbABI.ToString() +
@",""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[],""supportedstandards"":" + supportedStandards + @",""extra"":" + extra + "}";
}
}
}
2 changes: 1 addition & 1 deletion src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic" Version="3.4.0" />
<PackageReference Include="Mono.Cecil" Version="0.11.2" />
<PackageReference Include="Neo" Version="3.0.0-CI00962" />
<PackageReference Include="Neo" Version="3.0.0-CI00971" />
</ItemGroup>

<ItemGroup>
Expand Down
74 changes: 6 additions & 68 deletions src/Neo.Compiler.MSIL/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static int Compile(Options options)
}
byte[] bytes;
int bSucc = 0;
string jsonstr = null;
MyJson.JsonNode_Object abi = null;
string debugstr = null;
NeoModule module = null;

Expand Down Expand Up @@ -183,10 +183,7 @@ public static int Compile(Options options)

try
{
var outjson = FuncExport.Export(module, bytes, addrConvTable);
StringBuilder sb = new StringBuilder();
outjson.ConvertToStringWithFormat(sb, 0);
jsonstr = sb.ToString();
abi = FuncExport.Export(module, bytes, addrConvTable);
log.Log("gen abi succ");
}
catch (Exception err)
Expand Down Expand Up @@ -246,10 +243,12 @@ public static int Compile(Options options)

try
{
StringBuilder sbABI = new StringBuilder();
abi.ConvertToStringWithFormat(sbABI, 0);
string abiname = onlyname + ".abi.json";

File.Delete(abiname);
File.WriteAllText(abiname, jsonstr);
File.WriteAllText(abiname, sbABI.ToString());
log.Log("write:" + abiname);
bSucc++;
}
Expand All @@ -267,7 +266,6 @@ public static int Compile(Options options)
var tempName = Path.GetTempFileName();
File.Delete(tempName);
File.WriteAllText(tempName, debugstr);

File.Delete(debugzip);
using (var archive = ZipFile.Open(debugzip, ZipArchiveMode.Create))
{
Expand All @@ -285,24 +283,8 @@ public static int Compile(Options options)

try
{
var features = module == null ? ContractFeatures.NoProperty : module.attributes
.Where(u => u.AttributeType.Name == "FeaturesAttribute")
.Select(u => (ContractFeatures)u.ConstructorArguments.FirstOrDefault().Value)
.FirstOrDefault();

var extraAttributes = module == null ? new List<Mono.Collections.Generic.Collection<CustomAttributeArgument>>() : module.attributes.Where(u => u.AttributeType.Name == "ManifestExtraAttribute").Select(attribute => attribute.ConstructorArguments).ToList();
var supportedStandardsAttribute = module?.attributes.Where(u => u.AttributeType.Name == "SupportedStandardsAttribute").Select(attribute => attribute.ConstructorArguments).FirstOrDefault();

var extra = BuildExtraAttributes(extraAttributes);
var supportedStandards = BuildSupportedStandards(supportedStandardsAttribute);
var storage = features.HasFlag(ContractFeatures.HasStorage).ToString().ToLowerInvariant();
var payable = features.HasFlag(ContractFeatures.Payable).ToString().ToLowerInvariant();

string manifest = onlyname + ".manifest.json";
string defManifest =
@"{""groups"":[],""features"":{""storage"":" + storage + @",""payable"":" + payable + @"},""abi"":" +
jsonstr +
@",""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[],""supportedstandards"":" + supportedStandards + @",""extra"":" + extra + "}";
var defManifest = FuncExport.GenerateManifest(abi, module);

File.Delete(manifest);
File.WriteAllText(manifest, defManifest);
Expand Down Expand Up @@ -333,49 +315,5 @@ public static int Compile(Options options)

return -1;
}

private static object BuildSupportedStandards(Mono.Collections.Generic.Collection<CustomAttributeArgument> supportedStandardsAttribute)
{
if (supportedStandardsAttribute == null || supportedStandardsAttribute.Count == 0)
{
return "[]";
}

var entry = supportedStandardsAttribute.First();
string extra = "[";
foreach (var item in entry.Value as CustomAttributeArgument[])
{
extra += ($"\"{ScapeJson(item.Value.ToString())}\",");
}
extra = extra[0..^1];
extra += "]";

return extra;
}

private static string BuildExtraAttributes(List<Mono.Collections.Generic.Collection<CustomAttributeArgument>> extraAttributes)
{
if (extraAttributes == null || extraAttributes.Count == 0)
{
return "null";
}

string extra = "{";
foreach (var extraAttribute in extraAttributes)
{
var key = ScapeJson(extraAttribute[0].Value.ToString());
var value = ScapeJson(extraAttribute[1].Value.ToString());
extra += ($"\"{key}\":\"{value}\",");
}
extra = extra[0..^1];
extra += "}";

return extra;
}

private static string ScapeJson(string value)
{
return value.Replace("\"", "");
}
}
}
5 changes: 5 additions & 0 deletions src/Neo.SmartContract.Framework/Services/Neo/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ public class Contract
/// </summary>
public readonly byte[] Script;

/// <summary>
/// Manifest
/// </summary>
public readonly string Manifest;

/// <summary>
/// Has storage
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ABI_Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void Test_Good()
{
var testengine = new TestEngine();
testengine.AddEntryScript("./TestClasses/Contract_Event.cs");
var abi = testengine.ScriptEntry.finialABI;
var abi = testengine.ScriptEntry.finalABI;
Console.WriteLine("abi=" + abi.ToString());
var events = abi["events"].AsList()[0].ToString();
Console.WriteLine("event abi info =" + events);
Expand Down
4 changes: 2 additions & 2 deletions tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ABI_Offset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class UnitTest_ABI_Offset
public void UnitTest_TestABIOffsetWithoutOptimizer()
{
var buildScript = NeonTestTool.BuildScript("./TestClasses/Contract_ABIOffset.cs", true, false);
var abi = buildScript.finialABI;
var abi = buildScript.finalABI;

var methodsABI = abi["methods"].AsList();
Assert.AreEqual("0", methodsABI[0].GetDictItem("offset").ToString());
Expand All @@ -26,7 +26,7 @@ public void UnitTest_TestABIOffsetWithoutOptimizer()
public void UnitTest_TestABIOffsetWithOptimizer()
{
var buildScript = NeonTestTool.BuildScript("./TestClasses/Contract_ABIOffset.cs", true, true);
var abi = buildScript.finialABI;
var abi = buildScript.finalABI;

var methodsABI = abi["methods"].AsList();
Assert.AreEqual("0", methodsABI[0].GetDictItem("offset").ToString());
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 @@ -16,7 +16,7 @@ public BuildNEF(NefFile nefFile, string manifestFile) : base()
finalNEF = nefFile.Script;
MyJson.JsonNode_Object manifestAbi = MyJson.Parse(manifestFile) as MyJson.JsonNode_Object;
var abi = manifestAbi.GetDictItem("abi") as MyJson.JsonNode_Object;
finialABI = abi;
finalABI = abi;
finalManifest = manifestFile;
}
}
Expand Down
27 changes: 7 additions & 20 deletions tests/Neo.Compiler.MSIL.UnitTests/Utils/BuildScript.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using Mono.Cecil;
using Neo.Compiler.Optimizer;
using Neo.SmartContract.Manifest;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Neo.Compiler.MSIL.UnitTests.Utils
{
Expand All @@ -16,7 +13,7 @@ public class BuildScript
public ILModule modIL { get; private set; }
public ModuleConverter converterIL { get; private set; }
public byte[] finalNEF { get; protected set; }
public MyJson.JsonNode_Object finialABI { get; protected set; }
public MyJson.JsonNode_Object finalABI { get; protected set; }
public string finalManifest { get; protected set; }
public MyJson.JsonNode_Object debugInfo { get; private set; }

Expand Down Expand Up @@ -78,7 +75,7 @@ public void Build(Stream fs, Stream fspdb, bool optimizer)
#endif
try
{
finialABI = FuncExport.Export(converterIL.outModule, finalNEF, addrConvTable);
finalABI = FuncExport.Export(converterIL.outModule, finalNEF, addrConvTable);
}
catch (Exception err)
{
Expand All @@ -100,23 +97,13 @@ public void Build(Stream fs, Stream fspdb, bool optimizer)

try
{
var features = converterIL.outModule == null ? ContractFeatures.NoProperty : converterIL.outModule.attributes
.Where(u => u.AttributeType.Name == "FeaturesAttribute")
.Select(u => (ContractFeatures)u.ConstructorArguments.FirstOrDefault().Value)
.FirstOrDefault();

var extraAttributes = converterIL.outModule == null ? new List<Mono.Collections.Generic.Collection<CustomAttributeArgument>>()
: converterIL.outModule.attributes.Where(u => u.AttributeType.Name == "ManifestExtraAttribute").Select(attribute => attribute.ConstructorArguments).ToList();
var storage = features.HasFlag(ContractFeatures.HasStorage).ToString().ToLowerInvariant();
var payable = features.HasFlag(ContractFeatures.Payable).ToString().ToLowerInvariant();

finalManifest =
@"{""groups"":[],""features"":{""storage"":" + storage + @",""payable"":" + payable + @"},""abi"":" +
finialABI +
@",""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[],""extra"":[]" + "}";
finalManifest = FuncExport.GenerateManifest(finalABI, converterIL.outModule);
}
catch
catch (Exception err)
{
log.Log("Gen Manifest Error:" + err.ToString());
this.Error = err;
return;
}
}

Expand Down
14 changes: 12 additions & 2 deletions tests/Neo.Compiler.MSIL.UnitTests/Utils/TestEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Neo.Compiler.MSIL.UnitTests.Utils
{
Expand Down Expand Up @@ -128,7 +127,7 @@ public ContractMethod GetMethod(string methodname)
public int GetMethodEntryOffset(string methodname)
{
if (this.ScriptEntry is null) return -1;
var methods = this.ScriptEntry.finialABI.GetDictItem("methods") as MyJson.JsonNode_Array;
var methods = this.ScriptEntry.finalABI.GetDictItem("methods") as MyJson.JsonNode_Array;
foreach (var item in methods)
{
var method = item as MyJson.JsonNode_Object;
Expand Down Expand Up @@ -200,8 +199,19 @@ public EvaluationStack ExecuteTestCase(params StackItem[] args)
return this.ResultStack;
}

public void SendNotification(UInt160 hash, string eventName, VM.Types.Array state)
{
typeof(ApplicationEngine).GetMethod("SendNotification", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
.Invoke(this, new object[] { hash, eventName, state });
}

static Dictionary<uint, InteropDescriptor> callmethod;

public void ClearNotifications()
{
typeof(ApplicationEngine).GetField("notifications", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(this, null);
}

protected override void OnSysCall(uint method)
{
if (callmethod == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void Test_AccountIsStandard()
Assert.AreEqual(VM.VMState.FAULT, _engine.State);
Assert.AreEqual(0, result.Count);

// Standard
// No standard

_engine.Reset();
result = _engine.ExecuteTestCaseStandard("accountIsStandard", new ByteString(new byte[20]));
Expand All @@ -46,9 +46,7 @@ public void Test_AccountIsStandard()

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(Boolean));
Assert.AreEqual(true, item.GetBoolean());

// No standard
Assert.AreEqual(false, item.GetBoolean());

_engine.Reset();
result = _engine.ExecuteTestCaseStandard("accountIsStandard", new ByteString(noStandard));
Expand Down
Loading