Skip to content

Commit 022aed5

Browse files
shargonvncoelho
andauthored
new Testing environment (#890)
* Creating source from nef * change to LF * Fix _deploy in NEP17 template * change comment * Replace SetOwner * Remove extra file * Remove _deploy and _initialize * change to _ * Add args * Separate safe from unsafe * abstract Class * format * Only source * Generate events * Mock * starting native artifacts * CryptoLib * Rename project to avoid old one * LF * Escape name * Avoid generation native artifacts * TestNativeContracts * Redirect Logs and Notifications to the mocked smart contract * Mock notifications * Converts * format * LoadFromJson * Initialize native contracts * Partially mocked * Mocked with args * Fix Scope * Found bug in MemoryStore * TriggerType.Application * clean and fixes * format * Remove utartifact * Fix FromHash * Clean code * Store current script in transaction * Allow to set the Gas * Clean code * Reduce changes * Change Snapshot * Change to Artifact to ArtifactExtensions * Clean code * Move to Extensions folder * Fix Native initialization * Allow Custom mocks * Format * some vars * Add some summaries * Allow to mock undeployed contracts * Test undeployed mocked contracts * Change init native to true by default * Clean using * Allow null in deploy * Modify ContractManagement artifact * Readme * Fix menu * Resume example using native access * Clean storage example * fix readme * Fix readme * Fix readme and move CommitteeAddress * Add event testing to readme * Allow properties! * Allow properties if the method is without args and safe * User uppercase for first letter (conflicts with events and methods in native contracts) * All events start with "On" (avoid native conflicts) * Rename log event * Avoid re-query contract id if it was checked * format * Update Readme examples * Improve Mock.OnSysCall * Some small changes * Generate compiled artifacts (.dll) * LF * is 3 not 2 * Update src/Neo.SmartContract.Testing/README.md * Update src/Neo.SmartContract.Testing/NativeArtifacts.cs * Sign by default with Validators and committee * Rename to ValidatorsAddress * Allow to get address without initialize * Clean comment * Allow deploy with byte[] * allow null on deploy and update * Update readme * Fix static event * Allow native init notifications * Belane's feedback * Nullable when ends in object argument * Speedup InvokeOnNotify * Increase protected words * Allow Export and Import contract storage * format * Fix summary * Known limitations * Fix comments * Fix "Value" string * devcontainer ready for testing PR * Revert "devcontainer ready for testing PR" This reverts commit 04cc1ef. * Add more ways to set the signers easy * Avoid base64 strings * cschuchardt88's feedback * Change to IList when Array * Clean Check initialized --------- Co-authored-by: Vitor Nazario Coelho <vncoelho@gmail.com>
1 parent e8d08a5 commit 022aed5

32 files changed

+3139
-10
lines changed

neo-devpack-dotnet.sln

+24-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
2-
# Visual Studio Version 16
3-
VisualStudioVersion = 16.0.28803.452
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.7.34003.232
44
MinimumVisualStudioVersion = 10.0.40219.1
55
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.SmartContract.Framework", "src\Neo.SmartContract.Framework\Neo.SmartContract.Framework.csproj", "{C30B5859-D4B9-46E8-A797-6B0A1B49B590}"
66
EndProject
@@ -16,22 +16,26 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{79389FC0-C62
1616
EndProject
1717
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{D5266066-0AFD-44D5-A83E-2F73668A63C8}"
1818
EndProject
19-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.Compiler.CSharp.TestContracts", "tests\Neo.Compiler.CSharp.TestContracts\Neo.Compiler.CSharp.TestContracts.csproj", "{8D67DD5A-D683-481F-915E-98683EA38791}"
19+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.Compiler.CSharp.TestContracts", "tests\Neo.Compiler.CSharp.TestContracts\Neo.Compiler.CSharp.TestContracts.csproj", "{8D67DD5A-D683-481F-915E-98683EA38791}"
2020
EndProject
21-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.SmartContract.Framework.TestContracts", "tests\Neo.SmartContract.Framework.TestContracts\Neo.SmartContract.Framework.TestContracts.csproj", "{A372F1D6-51FF-472C-9508-FDAF7E6FEB13}"
21+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.SmartContract.Framework.TestContracts", "tests\Neo.SmartContract.Framework.TestContracts\Neo.SmartContract.Framework.TestContracts.csproj", "{A372F1D6-51FF-472C-9508-FDAF7E6FEB13}"
2222
EndProject
23-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.SmartContract.TestEngine", "tests\Neo.SmartContract.TestEngine\Neo.SmartContract.TestEngine.csproj", "{D0153204-6AEF-4D94-B0E1-8124C38C91D4}"
23+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.SmartContract.TestEngine", "tests\Neo.SmartContract.TestEngine\Neo.SmartContract.TestEngine.csproj", "{D0153204-6AEF-4D94-B0E1-8124C38C91D4}"
2424
EndProject
25-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo", "neo\src\Neo\Neo.csproj", "{73223FBD-C562-4FA0-9722-C7F1C382A9DE}"
25+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo", "neo\src\Neo\Neo.csproj", "{73223FBD-C562-4FA0-9722-C7F1C382A9DE}"
2626
EndProject
27-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.Cryptography.BLS12_381", "neo\src\Neo.Cryptography.BLS12_381\Neo.Cryptography.BLS12_381.csproj", "{D541BCE9-65BC-475B-94E5-19B6BFFF2B8E}"
27+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.Cryptography.BLS12_381", "neo\src\Neo.Cryptography.BLS12_381\Neo.Cryptography.BLS12_381.csproj", "{D541BCE9-65BC-475B-94E5-19B6BFFF2B8E}"
2828
EndProject
29-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.Json", "neo\src\Neo.Json\Neo.Json.csproj", "{35A34EBD-F2BF-4D83-A096-D5F007B12732}"
29+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.Json", "neo\src\Neo.Json\Neo.Json.csproj", "{35A34EBD-F2BF-4D83-A096-D5F007B12732}"
3030
EndProject
31-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.VM", "neo\src\Neo.VM\Neo.VM.csproj", "{D6D53889-5A10-46A4-BA66-E78B56EC1881}"
31+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.VM", "neo\src\Neo.VM\Neo.VM.csproj", "{D6D53889-5A10-46A4-BA66-E78B56EC1881}"
3232
EndProject
3333
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Dependency", "Dependency", "{49D5873D-7B38-48A5-B853-85146F032091}"
3434
EndProject
35+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.SmartContract.Testing", "src\Neo.SmartContract.Testing\Neo.SmartContract.Testing.csproj", "{648DCE6F-A0BA-4032-951B-20CF5BBFD998}"
36+
EndProject
37+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.SmartContract.Testing.UnitTests", "tests\Neo.SmartContract.Testing.UnitTests\Neo.SmartContract.Testing.UnitTests.csproj", "{B772B8A9-9362-4C6F-A6D3-2A4138439B2C}"
38+
EndProject
3539
Global
3640
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3741
Debug|Any CPU = Debug|Any CPU
@@ -86,6 +90,14 @@ Global
8690
{D6D53889-5A10-46A4-BA66-E78B56EC1881}.Debug|Any CPU.Build.0 = Debug|Any CPU
8791
{D6D53889-5A10-46A4-BA66-E78B56EC1881}.Release|Any CPU.ActiveCfg = Release|Any CPU
8892
{D6D53889-5A10-46A4-BA66-E78B56EC1881}.Release|Any CPU.Build.0 = Release|Any CPU
93+
{648DCE6F-A0BA-4032-951B-20CF5BBFD998}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
94+
{648DCE6F-A0BA-4032-951B-20CF5BBFD998}.Debug|Any CPU.Build.0 = Debug|Any CPU
95+
{648DCE6F-A0BA-4032-951B-20CF5BBFD998}.Release|Any CPU.ActiveCfg = Release|Any CPU
96+
{648DCE6F-A0BA-4032-951B-20CF5BBFD998}.Release|Any CPU.Build.0 = Release|Any CPU
97+
{B772B8A9-9362-4C6F-A6D3-2A4138439B2C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
98+
{B772B8A9-9362-4C6F-A6D3-2A4138439B2C}.Debug|Any CPU.Build.0 = Debug|Any CPU
99+
{B772B8A9-9362-4C6F-A6D3-2A4138439B2C}.Release|Any CPU.ActiveCfg = Release|Any CPU
100+
{B772B8A9-9362-4C6F-A6D3-2A4138439B2C}.Release|Any CPU.Build.0 = Release|Any CPU
89101
EndGlobalSection
90102
GlobalSection(SolutionProperties) = preSolution
91103
HideSolutionNode = FALSE
@@ -101,8 +113,10 @@ Global
101113
{D0153204-6AEF-4D94-B0E1-8124C38C91D4} = {D5266066-0AFD-44D5-A83E-2F73668A63C8}
102114
{73223FBD-C562-4FA0-9722-C7F1C382A9DE} = {49D5873D-7B38-48A5-B853-85146F032091}
103115
{D541BCE9-65BC-475B-94E5-19B6BFFF2B8E} = {49D5873D-7B38-48A5-B853-85146F032091}
104-
{D6D53889-5A10-46A4-BA66-E78B56EC1881} = {49D5873D-7B38-48A5-B853-85146F032091}
105116
{35A34EBD-F2BF-4D83-A096-D5F007B12732} = {49D5873D-7B38-48A5-B853-85146F032091}
117+
{D6D53889-5A10-46A4-BA66-E78B56EC1881} = {49D5873D-7B38-48A5-B853-85146F032091}
118+
{648DCE6F-A0BA-4032-951B-20CF5BBFD998} = {79389FC0-C621-4CEA-AD2B-6074C32E7BCA}
119+
{B772B8A9-9362-4C6F-A6D3-2A4138439B2C} = {D5266066-0AFD-44D5-A83E-2F73668A63C8}
106120
EndGlobalSection
107121
GlobalSection(ExtensibilityGlobals) = postSolution
108122
SolutionGuid = {6DA935E1-C674-4364-B087-F1B511B79215}

src/Neo.Compiler.CSharp/Neo.Compiler.CSharp.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<ProjectReference Include="..\Neo.SmartContract.Framework\Neo.SmartContract.Framework.csproj">
2929
<Aliases>scfx</Aliases>
3030
</ProjectReference>
31+
<ProjectReference Include="..\Neo.SmartContract.Testing\Neo.SmartContract.Testing.csproj" />
3132
</ItemGroup>
3233

3334
</Project>

src/Neo.Compiler.CSharp/Options.cs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class Options
2222
public bool Checked { get; set; }
2323
public bool Debug { get; set; }
2424
public bool Assembly { get; set; }
25+
public bool NoArtifacts { get; set; }
2526
public bool NoOptimize { get; set; }
2627
public bool NoInline { get; set; }
2728
public byte AddressVersion { get; set; }

src/Neo.Compiler.CSharp/Program.cs

+63
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@
99
// modifications are permitted.
1010

1111
using Microsoft.CodeAnalysis;
12+
using Microsoft.CodeAnalysis.CSharp;
13+
using Microsoft.CodeAnalysis.Emit;
1214
using Neo.IO;
1315
using Neo.Json;
1416
using Neo.Optimizer;
1517
using Neo.SmartContract;
1618
using Neo.SmartContract.Manifest;
19+
using Neo.SmartContract.Testing.Extensions;
1720
using System;
1821
using System.CommandLine;
1922
using System.CommandLine.Invocation;
2023
using System.CommandLine.NamingConventionBinder;
24+
using System.ComponentModel;
2125
using System.IO;
2226
using System.IO.Compression;
2327
using System.Linq;
@@ -38,6 +42,7 @@ static int Main(string[] args)
3842
new Option<bool>("--checked", "Indicates whether to check for overflow and underflow."),
3943
new Option<bool>(new[] { "-d", "--debug" }, "Indicates whether to generate debugging information."),
4044
new Option<bool>("--assembly", "Indicates whether to generate assembly."),
45+
new Option<bool>("--no-artifacts", "Instruct the compiler not to generate artifacts."),
4146
new Option<bool>("--no-optimize", "Instruct the compiler not to optimize the code."),
4247
new Option<bool>("--no-inline", "Instruct the compiler not to insert inline code."),
4348
new Option<byte>("--address-version", () => ProtocolSettings.Default.AddressVersion, "Indicates the address version used by the compiler.")
@@ -176,6 +181,64 @@ private static int ProcessOutputs(Options options, string folder, CompilationCon
176181
return 1;
177182
}
178183
Console.WriteLine($"Created {path}");
184+
if (!options.NoArtifacts)
185+
{
186+
var artifact = manifest.Abi.GetArtifactsSource(baseName);
187+
path = Path.Combine(outputFolder, $"{baseName}.artifacts.cs");
188+
File.WriteAllText(path, artifact);
189+
Console.WriteLine($"Created {path}");
190+
191+
try
192+
{
193+
// Try to compile the artifacts into a dll
194+
195+
string coreDir = Path.GetDirectoryName(typeof(object).Assembly.Location)!;
196+
197+
var syntaxTree = CSharpSyntaxTree.ParseText(artifact);
198+
var references = new MetadataReference[]
199+
{
200+
MetadataReference.CreateFromFile(Path.Combine(coreDir, "System.Runtime.dll")),
201+
MetadataReference.CreateFromFile(Path.Combine(coreDir, "System.Runtime.InteropServices.dll")),
202+
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
203+
MetadataReference.CreateFromFile(typeof(DisplayNameAttribute).Assembly.Location),
204+
MetadataReference.CreateFromFile(typeof(System.Numerics.BigInteger).Assembly.Location),
205+
MetadataReference.CreateFromFile(typeof(UInt160).Assembly.Location),
206+
MetadataReference.CreateFromFile(typeof(SmartContract.Testing.SmartContract).Assembly.Location)
207+
};
208+
209+
var compilation = CSharpCompilation.Create(baseName, new[] { syntaxTree }, references,
210+
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
211+
212+
using var ms = new MemoryStream();
213+
EmitResult result = compilation.Emit(ms);
214+
215+
if (!result.Success)
216+
{
217+
var failures = result.Diagnostics.Where(diagnostic =>
218+
diagnostic.IsWarningAsError ||
219+
diagnostic.Severity == DiagnosticSeverity.Error);
220+
221+
foreach (var diagnostic in failures)
222+
{
223+
Console.Error.WriteLine("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
224+
}
225+
}
226+
else
227+
{
228+
ms.Seek(0, SeekOrigin.Begin);
229+
230+
// Write dll
231+
232+
path = Path.Combine(outputFolder, $"{baseName}.artifacts.dll");
233+
File.WriteAllBytes(path, ms.ToArray());
234+
Console.WriteLine($"Created {path}");
235+
}
236+
}
237+
catch
238+
{
239+
Console.Error.WriteLine("Artifacts compilation error.");
240+
}
241+
}
179242
if (options.Debug)
180243
{
181244
path = Path.Combine(outputFolder, $"{baseName}.nefdbgnfo");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Reflection;
2+
3+
namespace Neo.SmartContract.Testing
4+
{
5+
internal class CustomMock
6+
{
7+
/// <summary>
8+
/// Mocked contract
9+
/// </summary>
10+
public required SmartContract Contract { get; init; }
11+
12+
/// <summary>
13+
/// Mocked method
14+
/// </summary>
15+
public required MethodInfo Method { get; init; }
16+
}
17+
}

0 commit comments

Comments
 (0)