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

Write manifest file with storage and payable according the contract needs #129

Merged
merged 6 commits into from
Nov 9, 2019
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
2 changes: 1 addition & 1 deletion src/Neo.Compiler.MSIL/MSIL/Conv_Multi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ private bool TryInsertMethod(NeoModule outModule, Mono.Cecil.MethodDefinition me
//continue;
}

NeoMethod nm = new NeoMethod(method);
NeoMethod nm = new NeoMethod(_method);
this.methodLink[_method] = nm;
outModule.mapMethods[nm.name] = nm;
ConvertMethod(_method, nm);
Expand Down
11 changes: 7 additions & 4 deletions src/Neo.Compiler.MSIL/MSIL/Converter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;

Expand Down Expand Up @@ -91,7 +92,7 @@ public NeoModule Convert(ILModule _in, ConvOption option = null)
continue;
}
if (m.Value.method.Is_ctor()) continue;
NeoMethod nm = new NeoMethod(m.Value.method);
NeoMethod nm = new NeoMethod(m.Value);
this.methodLink[m.Value] = nm;
outModule.mapMethods[nm.name] = nm;
}
Expand Down Expand Up @@ -185,7 +186,6 @@ public NeoModule Convert(ILModule _in, ConvOption option = null)

foreach (var key in outModule.mapMethods.Keys)
{

if (key.Contains("::Main("))
{
NeoMethod m = outModule.mapMethods[key];
Expand All @@ -201,7 +201,6 @@ public NeoModule Convert(ILModule _in, ConvOption option = null)
}
}
}

}
}
if (mainmethod == "")
Expand All @@ -214,7 +213,11 @@ public NeoModule Convert(ILModule _in, ConvOption option = null)
//单一默认入口
logger.Log("Find entrypoint:" + mainmethod);
}

var attr = outModule.mapMethods.Values.Where(u => u.inSmartContract).Select(u => u.type.attributes.ToArray()).FirstOrDefault();
if (attr?.Length > 0)
{
outModule.attributes.AddRange(attr);
}
outModule.mainMethod = mainmethod;
this.LinkCode(mainmethod);
//this.findFirstFunc();//得找到第一个函数
Expand Down
11 changes: 10 additions & 1 deletion src/Neo.Compiler.MSIL/MSIL/ILModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,15 @@ public class ILType
{
public Dictionary<string, ILField> fields = new Dictionary<string, ILField>();
public Dictionary<string, ILMethod> methods = new Dictionary<string, ILMethod>();
public List<Mono.Cecil.CustomAttribute> attributes = new List<Mono.Cecil.CustomAttribute>();

public ILType(ILModule module, Mono.Cecil.TypeDefinition type, ILogger logger)
{
if (type.HasCustomAttributes && type.IsClass)
{
attributes.AddRange(type.CustomAttributes);
}

foreach (Mono.Cecil.FieldDefinition f in type.Fields)
{
this.fields.Add(f.Name, new ILField(this, f));
Expand Down Expand Up @@ -196,17 +202,20 @@ public override string ToString()

public class ILMethod
{
public ILType type = null;
public Mono.Cecil.MethodDefinition method;
public string returntype;
public List<NeoParam> paramtypes = new List<NeoParam>();
public bool hasParam = false;
public Mono.Cecil.MethodDefinition method;
public List<NeoParam> body_Variables = new List<NeoParam>();
public SortedDictionary<int, OpCode> body_Codes = new SortedDictionary<int, OpCode>();
public string fail = null;

public ILMethod(ILType type, Mono.Cecil.MethodDefinition method, ILogger logger = null)
{
this.type = type;
this.method = method;

if (method != null)
{
returntype = method.ReturnType.FullName;
Expand Down
20 changes: 13 additions & 7 deletions src/Neo.Compiler.MSIL/NeoModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public NeoModule(ILogger logger) { }

public string mainMethod;
public ConvOption option;
public List<CustomAttribute> attributes = new List<CustomAttribute>();
public Dictionary<string, NeoMethod> mapMethods = new Dictionary<string, NeoMethod>();
public Dictionary<string, NeoEvent> mapEvents = new Dictionary<string, NeoEvent>();
public Dictionary<string, NeoField> mapFields = new Dictionary<string, NeoField>();
Expand Down Expand Up @@ -95,6 +96,9 @@ public class NeoMethod
public string returntype;
public bool isPublic = true;
public bool inSmartContract;
public ILMethod method;
public ILType type;

//临时变量
public List<NeoParam> body_Variables = new List<NeoParam>();

Expand Down Expand Up @@ -145,18 +149,20 @@ public NeoMethod() { }
/// Constructor
/// </summary>
/// <param name="method">Method</param>
public NeoMethod(MethodDefinition method)
public NeoMethod(ILMethod method)
{
_namespace = method.DeclaringType.FullName;
name = method.FullName;
displayName = method.Name;
inSmartContract = method.DeclaringType.BaseType.Name == "SmartContract";
isPublic = method.IsPublic;
this.method = method;
this.type = method.type;

foreach (var attr in method.CustomAttributes)
foreach (var attr in method.method.CustomAttributes)
{
ProcessAttribute(attr);
}
_namespace = method.method.DeclaringType.FullName;
name = method.method.FullName;
displayName = method.method.Name;
inSmartContract = method.method.DeclaringType.BaseType.Name == "SmartContract";
isPublic = method.method.IsPublic;
}

private void ProcessAttribute(CustomAttribute attr)
Expand Down
19 changes: 15 additions & 4 deletions src/Neo.Compiler.MSIL/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Neo.Compiler.MSIL;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

Expand Down Expand Up @@ -81,18 +83,19 @@ public static void Main(string[] args)
byte[] bytes;
int bSucc = 0;
string jsonstr = null;
NeoModule module = null;
//convert and build
try
{
var conv = new ModuleConverter(log);
ConvOption option = new ConvOption();
NeoModule am = conv.Convert(mod, option);
bytes = am.Build();
module = conv.Convert(mod, option);
bytes = module.Build();
log.Log("convert succ");

try
{
var outjson = vmtool.FuncExport.Export(am, bytes);
var outjson = vmtool.FuncExport.Export(module, bytes);
StringBuilder sb = new StringBuilder();
outjson.ConvertToStringWithFormat(sb, 0);
jsonstr = sb.ToString();
Expand Down Expand Up @@ -153,9 +156,17 @@ public static void Main(string[] args)
}
try
{
var features = module == null ? ContractFeatures.NoProperty : module.attributes
.Where(u => u.AttributeType.Name == "FeaturesAttribute")
.Select(u => (ContractFeatures)u.ConstructorArguments.FirstOrDefault().Value)
.FirstOrDefault();

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"":false,""payable"":false},""abi"":" +
@"{""groups"":[],""features"":{""storage"":" + storage + @",""payable"":" + payable + @"},""abi"":" +
jsonstr +
@",""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safeMethods"":[]}";

Expand Down
13 changes: 13 additions & 0 deletions src/Neo.SmartContract.Framework/ContractFeatures.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace Neo.SmartContract.Framework
{
[Flags]
public enum ContractFeatures : byte
{
NoProperty = 0,

HasStorage = 1 << 0,
Payable = 1 << 2,
}
}
22 changes: 22 additions & 0 deletions src/Neo.SmartContract.Framework/FeaturesAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace Neo.SmartContract.Framework
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class FeaturesAttribute : Attribute
{
/// <summary>
/// Smart contract features
/// </summary>
public ContractFeatures Features { get; }

/// <summary>
/// Constructor
/// </summary>
/// <param name="features">Specify the smart contract features</param>
public FeaturesAttribute(ContractFeatures features)
{
Features = features;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public void Init()
{
testengine = new TestEngine();
testengine.AddEntryScript("./TestClasses/Contract_Storage.cs");
Assert.AreEqual(ContractFeatures.HasStorage, testengine.ScriptEntry.converterIL.outModule.attributes
.Where(u => u.AttributeType.Name == "FeaturesAttribute")
.Select(u => (ContractFeatures)u.ConstructorArguments.FirstOrDefault().Value)
.FirstOrDefault());

testengine.Snapshot.Contracts.Add(testengine.EntryScriptHash, new Ledger.ContractState()
{
Script = testengine.EntryContext.Script,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo;

namespace Neo.Compiler.MSIL.TestClasses
{
[Features(SmartContract.Framework.ContractFeatures.HasStorage)]
class Contract_Storage : SmartContract.Framework.SmartContract
{
// There is no main here, it can be auto generation.
Expand Down