Skip to content

Commit dfb1d3d

Browse files
Jim8yshargon
andauthored
[Framework Add] predefine manifest (neo-project#903)
* predefine manifest * one attribute per file * remove empty line * Update tests/Neo.SmartContract.Framework.UnitTests/ManifestAttributeTest.cs * Update src/Neo.SmartContract.Framework/Attributes/ManifestExtraAttribute.cs * Update src/Neo.SmartContract.Framework/Attributes/ManifestExtraAttribute.cs * Update src/Neo.Compiler.CSharp/CompilationContext.cs --------- Co-authored-by: Shargon <shargon@gmail.com>
1 parent 502b294 commit dfb1d3d

File tree

8 files changed

+124
-11
lines changed

8 files changed

+124
-11
lines changed

src/Neo.Compiler.CSharp/CompilationContext.cs

+12-6
Original file line numberDiff line numberDiff line change
@@ -426,31 +426,37 @@ private void ProcessClass(SemanticModel model, INamedTypeSymbol symbol)
426426
bool isSmartContract = isPublic && !isAbstract && isContractType;
427427
if (isSmartContract)
428428
{
429-
if (scTypeFound) throw new CompilationException(DiagnosticId.MultiplyContracts, $"Only one smart contract is allowed.");
429+
if (scTypeFound) throw new CompilationException(DiagnosticId.MultiplyContracts, "Only one smart contract is allowed.");
430430
scTypeFound = true;
431431
foreach (var attribute in symbol.GetAttributesWithInherited())
432432
{
433+
if (attribute.AttributeClass!.IsSubclassOf(nameof(ManifestExtraAttribute)))
434+
{
435+
manifestExtra[ManifestExtraAttribute.AttributeType[attribute.AttributeClass!.Name]] = (string)attribute.ConstructorArguments[0].Value!;
436+
continue;
437+
}
438+
433439
switch (attribute.AttributeClass!.Name)
434440
{
435441
case nameof(DisplayNameAttribute):
436442
displayName = (string)attribute.ConstructorArguments[0].Value!;
437443
break;
438-
case nameof(scfx.Neo.SmartContract.Framework.Attributes.ContractSourceCodeAttribute):
444+
case nameof(ContractSourceCodeAttribute):
439445
Source = (string)attribute.ConstructorArguments[0].Value!;
440446
break;
441-
case nameof(scfx.Neo.SmartContract.Framework.Attributes.ManifestExtraAttribute):
447+
case nameof(ManifestExtraAttribute):
442448
manifestExtra[(string)attribute.ConstructorArguments[0].Value!] = (string)attribute.ConstructorArguments[1].Value!;
443449
break;
444-
case nameof(scfx.Neo.SmartContract.Framework.Attributes.ContractPermissionAttribute):
450+
case nameof(ContractPermissionAttribute):
445451
permissions.Add((string)attribute.ConstructorArguments[0].Value!, attribute.ConstructorArguments[1].Values.Select(p => (string)p.Value!).ToArray());
446452
break;
447-
case nameof(scfx.Neo.SmartContract.Framework.Attributes.ContractTrustAttribute):
453+
case nameof(ContractTrustAttribute):
448454
string trust = (string)attribute.ConstructorArguments[0].Value!;
449455
if (!ValidateContractTrust(trust))
450456
throw new ArgumentException($"The value {trust} is not a valid one for ContractTrust");
451457
trusts.Add(trust);
452458
break;
453-
case nameof(scfx.Neo.SmartContract.Framework.Attributes.SupportedStandardsAttribute):
459+
case nameof(SupportedStandardsAttribute):
454460
supportedStandards.UnionWith(
455461
attribute.ConstructorArguments[0].Values
456462
.Select(p => p.Value)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace Neo.SmartContract.Framework.Attributes
4+
{
5+
[AttributeUsage(AttributeTargets.Class)]
6+
public class AuthorAttribute : ManifestExtraAttribute
7+
{
8+
public AuthorAttribute(string value) : base(AttributeType[nameof(AuthorAttribute)], value)
9+
{
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace Neo.SmartContract.Framework.Attributes
4+
{
5+
[AttributeUsage(AttributeTargets.Class)]
6+
public class DescriptionAttribute : ManifestExtraAttribute
7+
{
8+
public DescriptionAttribute(string value) : base(AttributeType[nameof(DescriptionAttribute)], value)
9+
{
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace Neo.SmartContract.Framework.Attributes
4+
{
5+
[AttributeUsage(AttributeTargets.Class)]
6+
public class EmailAttribute : ManifestExtraAttribute
7+
{
8+
public EmailAttribute(string value) : base(AttributeType[nameof(EmailAttribute)], value)
9+
{
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
// Copyright (C) 2015-2023 The Neo Project.
2-
//
3-
// The Neo.SmartContract.Framework is free software distributed under the MIT
4-
// software license, see the accompanying file LICENSE in the main directory
5-
// of the project or http://www.opensource.org/licenses/mit-license.php
2+
//
3+
// The Neo.SmartContract.Framework is free software distributed under the MIT
4+
// software license, see the accompanying file LICENSE in the main directory
5+
// of the project or http://www.opensource.org/licenses/mit-license.php
66
// for more details.
7-
//
7+
//
88
// Redistribution and use in source and binary forms with or without
99
// modifications are permitted.
1010

1111
using System;
12+
using System.Collections.Generic;
1213

1314
namespace Neo.SmartContract.Framework.Attributes
1415
{
@@ -18,5 +19,13 @@ public class ManifestExtraAttribute : Attribute
1819
public ManifestExtraAttribute(string key, string value)
1920
{
2021
}
22+
23+
internal static readonly Dictionary<string, string> AttributeType = new Dictionary<string, string>
24+
{
25+
{ nameof(AuthorAttribute), "Author" },
26+
{ nameof(EmailAttribute), "E-mail" },
27+
{ nameof(DescriptionAttribute), "Description" },
28+
{ nameof(VersionAttribute), "Version" },
29+
};
2130
}
2231
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace Neo.SmartContract.Framework.Attributes
4+
{
5+
[AttributeUsage(AttributeTargets.Class)]
6+
public class VersionAttribute : ManifestExtraAttribute
7+
{
8+
public VersionAttribute(string value) : base(AttributeType[nameof(VersionAttribute)], value)
9+
{
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Neo.SmartContract.Framework.Attributes;
2+
3+
namespace Neo.SmartContract.Framework.UnitTests.TestClasses
4+
{
5+
[Author("core-dev")]
6+
[Email("core@neo.org")]
7+
[Version("v3.6.3")]
8+
[Description("This is a test contract.")]
9+
[ManifestExtra("ExtraKey", "ExtraValue")]
10+
public class Contract_ManifestAttribute : SmartContract
11+
{
12+
[NoReentrant]
13+
public void reentrantTest(int value)
14+
{
15+
if (value == 0) return;
16+
if (value == 123)
17+
{
18+
reentrantTest(0);
19+
}
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
3+
namespace Neo.SmartContract.Framework.UnitTests;
4+
5+
[TestClass]
6+
public class ManifestAttributeTest
7+
{
8+
[TestMethod]
9+
public void TestManifestAttribute()
10+
{
11+
var testEngine = new TestEngine.TestEngine();
12+
testEngine.AddEntryScript(Utils.Extensions.TestContractRoot + "Contract_ManifestAttribute.cs");
13+
14+
var extra = testEngine.Manifest!.Extra;
15+
16+
Assert.AreEqual(5, extra.Count);
17+
// [Author("core-dev")]
18+
// [Email("core@neo.org")]
19+
// [Version("v3.6.3")]
20+
// [Description("This is a test contract.")]
21+
// [ManifestExtra("ExtraKey", "ExtraValue")]
22+
Assert.AreEqual("core-dev", extra["Author"]!.GetString());
23+
Assert.AreEqual("core@neo.org", extra["E-mail"]!.GetString());
24+
Assert.AreEqual("v3.6.3", extra["Version"]!.GetString());
25+
Assert.AreEqual("This is a test contract.", extra["Description"]!.GetString());
26+
Assert.AreEqual("ExtraValue", extra["ExtraKey"]!.GetString());
27+
}
28+
}

0 commit comments

Comments
 (0)