Skip to content

Commit

Permalink
new Testing environment (#890)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
shargon and vncoelho authored Feb 13, 2024
1 parent e8d08a5 commit 022aed5
Show file tree
Hide file tree
Showing 32 changed files with 3,139 additions and 10 deletions.
34 changes: 24 additions & 10 deletions neo-devpack-dotnet.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28803.452
# Visual Studio Version 17
VisualStudioVersion = 17.7.34003.232
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.SmartContract.Framework", "src\Neo.SmartContract.Framework\Neo.SmartContract.Framework.csproj", "{C30B5859-D4B9-46E8-A797-6B0A1B49B590}"
EndProject
Expand All @@ -16,22 +16,26 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{79389FC0-C62
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{D5266066-0AFD-44D5-A83E-2F73668A63C8}"
EndProject
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}"
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}"
EndProject
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}"
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}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.SmartContract.TestEngine", "tests\Neo.SmartContract.TestEngine\Neo.SmartContract.TestEngine.csproj", "{D0153204-6AEF-4D94-B0E1-8124C38C91D4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.SmartContract.TestEngine", "tests\Neo.SmartContract.TestEngine\Neo.SmartContract.TestEngine.csproj", "{D0153204-6AEF-4D94-B0E1-8124C38C91D4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo", "neo\src\Neo\Neo.csproj", "{73223FBD-C562-4FA0-9722-C7F1C382A9DE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo", "neo\src\Neo\Neo.csproj", "{73223FBD-C562-4FA0-9722-C7F1C382A9DE}"
EndProject
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}"
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}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.Json", "neo\src\Neo.Json\Neo.Json.csproj", "{35A34EBD-F2BF-4D83-A096-D5F007B12732}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.Json", "neo\src\Neo.Json\Neo.Json.csproj", "{35A34EBD-F2BF-4D83-A096-D5F007B12732}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.VM", "neo\src\Neo.VM\Neo.VM.csproj", "{D6D53889-5A10-46A4-BA66-E78B56EC1881}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.VM", "neo\src\Neo.VM\Neo.VM.csproj", "{D6D53889-5A10-46A4-BA66-E78B56EC1881}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Dependency", "Dependency", "{49D5873D-7B38-48A5-B853-85146F032091}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Neo.SmartContract.Testing", "src\Neo.SmartContract.Testing\Neo.SmartContract.Testing.csproj", "{648DCE6F-A0BA-4032-951B-20CF5BBFD998}"
EndProject
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -86,6 +90,14 @@ Global
{D6D53889-5A10-46A4-BA66-E78B56EC1881}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D6D53889-5A10-46A4-BA66-E78B56EC1881}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D6D53889-5A10-46A4-BA66-E78B56EC1881}.Release|Any CPU.Build.0 = Release|Any CPU
{648DCE6F-A0BA-4032-951B-20CF5BBFD998}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{648DCE6F-A0BA-4032-951B-20CF5BBFD998}.Debug|Any CPU.Build.0 = Debug|Any CPU
{648DCE6F-A0BA-4032-951B-20CF5BBFD998}.Release|Any CPU.ActiveCfg = Release|Any CPU
{648DCE6F-A0BA-4032-951B-20CF5BBFD998}.Release|Any CPU.Build.0 = Release|Any CPU
{B772B8A9-9362-4C6F-A6D3-2A4138439B2C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B772B8A9-9362-4C6F-A6D3-2A4138439B2C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B772B8A9-9362-4C6F-A6D3-2A4138439B2C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B772B8A9-9362-4C6F-A6D3-2A4138439B2C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -101,8 +113,10 @@ Global
{D0153204-6AEF-4D94-B0E1-8124C38C91D4} = {D5266066-0AFD-44D5-A83E-2F73668A63C8}
{73223FBD-C562-4FA0-9722-C7F1C382A9DE} = {49D5873D-7B38-48A5-B853-85146F032091}
{D541BCE9-65BC-475B-94E5-19B6BFFF2B8E} = {49D5873D-7B38-48A5-B853-85146F032091}
{D6D53889-5A10-46A4-BA66-E78B56EC1881} = {49D5873D-7B38-48A5-B853-85146F032091}
{35A34EBD-F2BF-4D83-A096-D5F007B12732} = {49D5873D-7B38-48A5-B853-85146F032091}
{D6D53889-5A10-46A4-BA66-E78B56EC1881} = {49D5873D-7B38-48A5-B853-85146F032091}
{648DCE6F-A0BA-4032-951B-20CF5BBFD998} = {79389FC0-C621-4CEA-AD2B-6074C32E7BCA}
{B772B8A9-9362-4C6F-A6D3-2A4138439B2C} = {D5266066-0AFD-44D5-A83E-2F73668A63C8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6DA935E1-C674-4364-B087-F1B511B79215}
Expand Down
1 change: 1 addition & 0 deletions src/Neo.Compiler.CSharp/Neo.Compiler.CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<ProjectReference Include="..\Neo.SmartContract.Framework\Neo.SmartContract.Framework.csproj">
<Aliases>scfx</Aliases>
</ProjectReference>
<ProjectReference Include="..\Neo.SmartContract.Testing\Neo.SmartContract.Testing.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions src/Neo.Compiler.CSharp/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Options
public bool Checked { get; set; }
public bool Debug { get; set; }
public bool Assembly { get; set; }
public bool NoArtifacts { get; set; }
public bool NoOptimize { get; set; }
public bool NoInline { get; set; }
public byte AddressVersion { get; set; }
Expand Down
63 changes: 63 additions & 0 deletions src/Neo.Compiler.CSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@
// modifications are permitted.

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using Neo.IO;
using Neo.Json;
using Neo.Optimizer;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using Neo.SmartContract.Testing.Extensions;
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.NamingConventionBinder;
using System.ComponentModel;
using System.IO;
using System.IO.Compression;
using System.Linq;
Expand All @@ -38,6 +42,7 @@ static int Main(string[] args)
new Option<bool>("--checked", "Indicates whether to check for overflow and underflow."),
new Option<bool>(new[] { "-d", "--debug" }, "Indicates whether to generate debugging information."),
new Option<bool>("--assembly", "Indicates whether to generate assembly."),
new Option<bool>("--no-artifacts", "Instruct the compiler not to generate artifacts."),
new Option<bool>("--no-optimize", "Instruct the compiler not to optimize the code."),
new Option<bool>("--no-inline", "Instruct the compiler not to insert inline code."),
new Option<byte>("--address-version", () => ProtocolSettings.Default.AddressVersion, "Indicates the address version used by the compiler.")
Expand Down Expand Up @@ -176,6 +181,64 @@ private static int ProcessOutputs(Options options, string folder, CompilationCon
return 1;
}
Console.WriteLine($"Created {path}");
if (!options.NoArtifacts)
{
var artifact = manifest.Abi.GetArtifactsSource(baseName);
path = Path.Combine(outputFolder, $"{baseName}.artifacts.cs");
File.WriteAllText(path, artifact);
Console.WriteLine($"Created {path}");

try
{
// Try to compile the artifacts into a dll

string coreDir = Path.GetDirectoryName(typeof(object).Assembly.Location)!;

var syntaxTree = CSharpSyntaxTree.ParseText(artifact);
var references = new MetadataReference[]
{
MetadataReference.CreateFromFile(Path.Combine(coreDir, "System.Runtime.dll")),
MetadataReference.CreateFromFile(Path.Combine(coreDir, "System.Runtime.InteropServices.dll")),
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
MetadataReference.CreateFromFile(typeof(DisplayNameAttribute).Assembly.Location),
MetadataReference.CreateFromFile(typeof(System.Numerics.BigInteger).Assembly.Location),
MetadataReference.CreateFromFile(typeof(UInt160).Assembly.Location),
MetadataReference.CreateFromFile(typeof(SmartContract.Testing.SmartContract).Assembly.Location)
};

var compilation = CSharpCompilation.Create(baseName, new[] { syntaxTree }, references,
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

using var ms = new MemoryStream();
EmitResult result = compilation.Emit(ms);

if (!result.Success)
{
var failures = result.Diagnostics.Where(diagnostic =>
diagnostic.IsWarningAsError ||
diagnostic.Severity == DiagnosticSeverity.Error);

foreach (var diagnostic in failures)
{
Console.Error.WriteLine("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
}
}
else
{
ms.Seek(0, SeekOrigin.Begin);

// Write dll

path = Path.Combine(outputFolder, $"{baseName}.artifacts.dll");
File.WriteAllBytes(path, ms.ToArray());
Console.WriteLine($"Created {path}");
}
}
catch
{
Console.Error.WriteLine("Artifacts compilation error.");
}
}
if (options.Debug)
{
path = Path.Combine(outputFolder, $"{baseName}.nefdbgnfo");
Expand Down
17 changes: 17 additions & 0 deletions src/Neo.SmartContract.Testing/CustomMock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Reflection;

namespace Neo.SmartContract.Testing
{
internal class CustomMock
{
/// <summary>
/// Mocked contract
/// </summary>
public required SmartContract Contract { get; init; }

/// <summary>
/// Mocked method
/// </summary>
public required MethodInfo Method { get; init; }
}
}
Loading

0 comments on commit 022aed5

Please sign in to comment.