From 6b0c49c799e25ac09395478ca0a82e9127f701b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BF=97=E5=90=8C?= Date: Mon, 23 Nov 2020 16:08:21 +0800 Subject: [PATCH 01/29] Upgrade target framework. --- .github/workflows/main.yml | 2 +- src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj | 4 ++-- .../Neo.SmartContract.Framework.csproj | 2 +- templates/Template.CSharp/ProjectTemplate.csproj | 2 +- templates/Template.NEP5.CSharp/Template.NEP5.CSharp.csproj | 2 +- templates/Template.VB/ProjectTemplate.vbproj | 2 +- .../Neo.Compiler.MSIL.UnitTests.csproj | 2 +- .../Neo.SmartContract.Framework.UnitTests.csproj | 2 +- tests/Template.NEP5.UnitTests/Template.NEP5.UnitTests.csproj | 2 +- 9 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 470faa827..4ff62ff98 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -6,7 +6,7 @@ on: pull_request: env: - DOTNET_VERSION: 3.1.100 + DOTNET_VERSION: 5.0.100 jobs: diff --git a/src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj b/src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj index 0d9091a75..c9f90b4fc 100644 --- a/src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj +++ b/src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj @@ -30,12 +30,12 @@ Exe Neo.Neon true - netcoreapp3.1 + net5.0 Neo.Compiler.MSIL - netstandard2.1 + net5.0 diff --git a/src/Neo.SmartContract.Framework/Neo.SmartContract.Framework.csproj b/src/Neo.SmartContract.Framework/Neo.SmartContract.Framework.csproj index 3e36e5f33..7fe05c822 100644 --- a/src/Neo.SmartContract.Framework/Neo.SmartContract.Framework.csproj +++ b/src/Neo.SmartContract.Framework/Neo.SmartContract.Framework.csproj @@ -6,7 +6,7 @@ 3.0.0 preview3 The Neo Project - netstandard2.1 + net5.0 Neo.SmartContract.Framework Neo.SmartContract.Framework NEO;AntShares;Blockchain;Smart Contract;VM diff --git a/templates/Template.CSharp/ProjectTemplate.csproj b/templates/Template.CSharp/ProjectTemplate.csproj index 74533c27c..b6641adb8 100644 --- a/templates/Template.CSharp/ProjectTemplate.csproj +++ b/templates/Template.CSharp/ProjectTemplate.csproj @@ -1,7 +1,7 @@ - netstandard2.1 + net5.0 diff --git a/templates/Template.NEP5.CSharp/Template.NEP5.CSharp.csproj b/templates/Template.NEP5.CSharp/Template.NEP5.CSharp.csproj index 57acd808c..2ab59b7a3 100644 --- a/templates/Template.NEP5.CSharp/Template.NEP5.CSharp.csproj +++ b/templates/Template.NEP5.CSharp/Template.NEP5.CSharp.csproj @@ -1,7 +1,7 @@ - netstandard2.1 + net5.0 diff --git a/templates/Template.VB/ProjectTemplate.vbproj b/templates/Template.VB/ProjectTemplate.vbproj index 68fc466a0..ba7a1e1b7 100644 --- a/templates/Template.VB/ProjectTemplate.vbproj +++ b/templates/Template.VB/ProjectTemplate.vbproj @@ -2,7 +2,7 @@ $safeprojectname$ - netstandard2.1 + net5.0 diff --git a/tests/Neo.Compiler.MSIL.UnitTests/Neo.Compiler.MSIL.UnitTests.csproj b/tests/Neo.Compiler.MSIL.UnitTests/Neo.Compiler.MSIL.UnitTests.csproj index 3ba2fe80a..3c084d021 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/Neo.Compiler.MSIL.UnitTests.csproj +++ b/tests/Neo.Compiler.MSIL.UnitTests/Neo.Compiler.MSIL.UnitTests.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1 + net5.0 false Neo.Compiler.MSIL false diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Neo.SmartContract.Framework.UnitTests.csproj b/tests/Neo.SmartContract.Framework.UnitTests/Neo.SmartContract.Framework.UnitTests.csproj index 179204a92..2a401f042 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Neo.SmartContract.Framework.UnitTests.csproj +++ b/tests/Neo.SmartContract.Framework.UnitTests/Neo.SmartContract.Framework.UnitTests.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1 + net5.0 false diff --git a/tests/Template.NEP5.UnitTests/Template.NEP5.UnitTests.csproj b/tests/Template.NEP5.UnitTests/Template.NEP5.UnitTests.csproj index d6f92fff9..a776c1411 100644 --- a/tests/Template.NEP5.UnitTests/Template.NEP5.UnitTests.csproj +++ b/tests/Template.NEP5.UnitTests/Template.NEP5.UnitTests.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1 + net5.0 false From c36f9b2d731af6e738a1e6107d034e1aef9ba157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BF=97=E5=90=8C?= Date: Wed, 25 Nov 2020 15:13:57 +0800 Subject: [PATCH 02/29] Revert "Upgrade target framework." This reverts commit 6b0c49c799e25ac09395478ca0a82e9127f701b5. --- .github/workflows/main.yml | 2 +- src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj | 4 ++-- .../Neo.SmartContract.Framework.csproj | 2 +- templates/Template.CSharp/ProjectTemplate.csproj | 2 +- templates/Template.NEP5.CSharp/Template.NEP5.CSharp.csproj | 2 +- templates/Template.VB/ProjectTemplate.vbproj | 2 +- .../Neo.Compiler.MSIL.UnitTests.csproj | 2 +- .../Neo.SmartContract.Framework.UnitTests.csproj | 2 +- tests/Template.NEP5.UnitTests/Template.NEP5.UnitTests.csproj | 2 +- 9 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4ff62ff98..470faa827 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -6,7 +6,7 @@ on: pull_request: env: - DOTNET_VERSION: 5.0.100 + DOTNET_VERSION: 3.1.100 jobs: diff --git a/src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj b/src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj index c9f90b4fc..0d9091a75 100644 --- a/src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj +++ b/src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj @@ -30,12 +30,12 @@ Exe Neo.Neon true - net5.0 + netcoreapp3.1 Neo.Compiler.MSIL - net5.0 + netstandard2.1 diff --git a/src/Neo.SmartContract.Framework/Neo.SmartContract.Framework.csproj b/src/Neo.SmartContract.Framework/Neo.SmartContract.Framework.csproj index 7fe05c822..3e36e5f33 100644 --- a/src/Neo.SmartContract.Framework/Neo.SmartContract.Framework.csproj +++ b/src/Neo.SmartContract.Framework/Neo.SmartContract.Framework.csproj @@ -6,7 +6,7 @@ 3.0.0 preview3 The Neo Project - net5.0 + netstandard2.1 Neo.SmartContract.Framework Neo.SmartContract.Framework NEO;AntShares;Blockchain;Smart Contract;VM diff --git a/templates/Template.CSharp/ProjectTemplate.csproj b/templates/Template.CSharp/ProjectTemplate.csproj index b6641adb8..74533c27c 100644 --- a/templates/Template.CSharp/ProjectTemplate.csproj +++ b/templates/Template.CSharp/ProjectTemplate.csproj @@ -1,7 +1,7 @@ - net5.0 + netstandard2.1 diff --git a/templates/Template.NEP5.CSharp/Template.NEP5.CSharp.csproj b/templates/Template.NEP5.CSharp/Template.NEP5.CSharp.csproj index 2ab59b7a3..57acd808c 100644 --- a/templates/Template.NEP5.CSharp/Template.NEP5.CSharp.csproj +++ b/templates/Template.NEP5.CSharp/Template.NEP5.CSharp.csproj @@ -1,7 +1,7 @@ - net5.0 + netstandard2.1 diff --git a/templates/Template.VB/ProjectTemplate.vbproj b/templates/Template.VB/ProjectTemplate.vbproj index ba7a1e1b7..68fc466a0 100644 --- a/templates/Template.VB/ProjectTemplate.vbproj +++ b/templates/Template.VB/ProjectTemplate.vbproj @@ -2,7 +2,7 @@ $safeprojectname$ - net5.0 + netstandard2.1 diff --git a/tests/Neo.Compiler.MSIL.UnitTests/Neo.Compiler.MSIL.UnitTests.csproj b/tests/Neo.Compiler.MSIL.UnitTests/Neo.Compiler.MSIL.UnitTests.csproj index 3c084d021..3ba2fe80a 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/Neo.Compiler.MSIL.UnitTests.csproj +++ b/tests/Neo.Compiler.MSIL.UnitTests/Neo.Compiler.MSIL.UnitTests.csproj @@ -1,7 +1,7 @@ - net5.0 + netcoreapp3.1 false Neo.Compiler.MSIL false diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Neo.SmartContract.Framework.UnitTests.csproj b/tests/Neo.SmartContract.Framework.UnitTests/Neo.SmartContract.Framework.UnitTests.csproj index 2a401f042..179204a92 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Neo.SmartContract.Framework.UnitTests.csproj +++ b/tests/Neo.SmartContract.Framework.UnitTests/Neo.SmartContract.Framework.UnitTests.csproj @@ -1,7 +1,7 @@ - net5.0 + netcoreapp3.1 false diff --git a/tests/Template.NEP5.UnitTests/Template.NEP5.UnitTests.csproj b/tests/Template.NEP5.UnitTests/Template.NEP5.UnitTests.csproj index a776c1411..d6f92fff9 100644 --- a/tests/Template.NEP5.UnitTests/Template.NEP5.UnitTests.csproj +++ b/tests/Template.NEP5.UnitTests/Template.NEP5.UnitTests.csproj @@ -1,7 +1,7 @@ - net5.0 + netcoreapp3.1 false From 0ce51ed512f0e383bfb534aebe9cc54fcad2cf12 Mon Sep 17 00:00:00 2001 From: Shargon Date: Sat, 5 Dec 2020 14:21:28 +0100 Subject: [PATCH 03/29] Safe Attribute --- src/Neo.Compiler.MSIL/FuncExport.cs | 3 ++- .../SafeAttribute.cs | 9 +++++++ .../TestClasses/Contract_ABIOSafe.cs | 25 +++++++++++++++++++ .../TestClasses/Contract_ABIOffset.cs | 1 + .../UnitTest_ABI_Safe.cs | 22 ++++++++++++++++ 5 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/Neo.SmartContract.Framework/SafeAttribute.cs create mode 100644 tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_ABIOSafe.cs create mode 100644 tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ABI_Safe.cs diff --git a/src/Neo.Compiler.MSIL/FuncExport.cs b/src/Neo.Compiler.MSIL/FuncExport.cs index 9a2ad7510..95e094c3d 100644 --- a/src/Neo.Compiler.MSIL/FuncExport.cs +++ b/src/Neo.Compiler.MSIL/FuncExport.cs @@ -109,6 +109,7 @@ public static JObject Export(NeoModule module, byte[] script, Dictionary u.AttributeType.FullName == "Neo.SmartContract.Framework.SafeAttribute") == true; JArray funcparams = new JArray(); funcsign["parameters"] = funcparams; if (mm.paramtypes != null) @@ -220,7 +221,7 @@ public static string GenerateManifest(JObject abi, NeoModule module) return @"{""groups"":[],""abi"":" + sbABI + - @",""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""safemethods"":[],""supportedstandards"":" + supportedStandards + @",""extra"":" + extra + "}"; + @",""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[],""supportedstandards"":" + supportedStandards + @",""extra"":" + extra + "}"; } } } diff --git a/src/Neo.SmartContract.Framework/SafeAttribute.cs b/src/Neo.SmartContract.Framework/SafeAttribute.cs new file mode 100644 index 000000000..4c014937c --- /dev/null +++ b/src/Neo.SmartContract.Framework/SafeAttribute.cs @@ -0,0 +1,9 @@ +using System; + +namespace Neo.SmartContract.Framework +{ + [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] + public class SafeAttribute : Attribute + { + } +} diff --git a/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_ABIOSafe.cs b/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_ABIOSafe.cs new file mode 100644 index 000000000..c1cec9144 --- /dev/null +++ b/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_ABIOSafe.cs @@ -0,0 +1,25 @@ +using Neo.SmartContract.Framework; + +namespace Neo.Compiler.MSIL.UnitTests.TestClasses +{ + public class Contract_ABISafe : SmartContract.Framework.SmartContract + { + static int s = 1; + + public static int UnitTest_001() + { + return 1; + } + + [Safe] + public static int UnitTest_002() + { + return 2; + } + + public static int UnitTest_003() + { + return 3; + } + } +} diff --git a/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_ABIOffset.cs b/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_ABIOffset.cs index 1e91d9bfc..aab19efa9 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_ABIOffset.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_ABIOffset.cs @@ -21,6 +21,7 @@ public static int UnitTest_002() } return a; } + public static int UnitTest_003() { int c = UnitTest_002() + s; diff --git a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ABI_Safe.cs b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ABI_Safe.cs new file mode 100644 index 000000000..0c58704d5 --- /dev/null +++ b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ABI_Safe.cs @@ -0,0 +1,22 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Compiler.MSIL.UnitTests.Utils; +using Neo.IO.Json; + +namespace Neo.Compiler.MSIL.UnitTests +{ + [TestClass] + public class UnitTest_ABI_Safe + { + [TestMethod] + public void UnitTest_TestSafe() + { + var buildScript = NeonTestTool.BuildScript("./TestClasses/Contract_ABIOSafe.cs", true, false); + var abi = buildScript.finalABI; + + var methodsABI = abi["methods"] as JArray; + Assert.IsFalse(methodsABI[0]["safe"].AsBoolean()); + Assert.IsTrue(methodsABI[1]["safe"].AsBoolean()); + Assert.IsFalse(methodsABI[2]["safe"].AsBoolean()); + } + } +} From 782fa68398b3fba03901fcd251d184d84035e1d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BF=97=E5=90=8C?= Date: Thu, 26 Nov 2020 02:52:46 -0600 Subject: [PATCH 04/29] Update Contract1.cs (#398) --- templates/Template.CSharp/Contract1.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/templates/Template.CSharp/Contract1.cs b/templates/Template.CSharp/Contract1.cs index e1da5e501..3dbe7f34f 100644 --- a/templates/Template.CSharp/Contract1.cs +++ b/templates/Template.CSharp/Contract1.cs @@ -1,3 +1,4 @@ +using Neo; using Neo.SmartContract.Framework; using Neo.SmartContract.Framework.Services.Neo; using System; @@ -11,7 +12,7 @@ namespace $safeprojectname$ public class Contract1 : SmartContract { //TODO: Replace it with your own address. - static readonly byte[] Owner = "NiNmXL8FjEUEs1nfX9uHFBNaenxDHJtmuB".ToScriptHash(); + static readonly UInt160 Owner = "NiNmXL8FjEUEs1nfX9uHFBNaenxDHJtmuB".ToScriptHash(); private static bool IsOwner() => Runtime.CheckWitness(Owner); From d13e363f631a46f7abd9fe5b8fd07fcacac4e99a Mon Sep 17 00:00:00 2001 From: Shargon Date: Sat, 28 Nov 2020 13:09:01 +0100 Subject: [PATCH 05/29] Revert "publish neon to myget/github packages" (#395) --- .github/workflows/main.yml | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 470faa827..46b8281ff 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -51,9 +51,7 @@ jobs: - name: Setup NuGet.exe for use with actions uses: NuGet/setup-nuget@v1 - name: Pack with dotnet - run: | - git rev-list --count HEAD | xargs printf "CI%05d" | xargs dotnet pack src/Neo.SmartContract.Framework -c Debug -o out --include-source --version-suffix - git rev-list --count HEAD | xargs printf "CI%05d" | xargs dotnet pack src/Neo.Compiler.MSIL -c Debug -o out --include-source --version-suffix + run: git rev-list --count HEAD | xargs printf "CI%05d" | xargs dotnet pack src/Neo.SmartContract.Framework -c Debug -o out --include-source --version-suffix - name: Publish to Github Packages run: | nuget source Add -Name "GitHub" -Source "https://nuget.pkg.github.com/neo-project/index.json" -UserName neo-project -Password ${GITHUB_TOKEN} @@ -75,9 +73,7 @@ jobs: with: dotnet-version: ${{ env.DOTNET_VERSION }} - name: Pack with dotnet - run: | - git rev-list --count HEAD |xargs printf "CI%05d" |xargs dotnet pack src/Neo.SmartContract.Framework -c Debug -o out --include-source --version-suffix - git rev-list --count HEAD |xargs printf "CI%05d" |xargs dotnet pack src/Neo.Compiler.MSIL -c Debug -o out --include-source --version-suffix + run: git rev-list --count HEAD |xargs printf "CI%05d" |xargs dotnet pack src/Neo.SmartContract.Framework -c Debug -o out --include-source --version-suffix - name: Publish to MyGet run: dotnet nuget push out/*.nupkg -s https://www.myget.org/F/neo/api/v2/package -k ${MYGET_TOKEN} -ss https://www.myget.org/F/neo/symbols/api/v2/package -sk ${MYGET_TOKEN} env: From 85ca99538cbe19df61feae0e25b6497c0e350db1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BF=97=E5=90=8C?= Date: Mon, 7 Dec 2020 14:43:07 +0800 Subject: [PATCH 06/29] =?UTF-8?q?Remove=20Contract.Create=E3=80=81Contract?= =?UTF-8?q?.Update=E3=80=81Contract.Destroy;=20Add=20Contract.CallNative.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/Neo/Contract.cs | 10 +- templates/Template.CSharp/Contract1.cs | 12 -- templates/Template.NEP5.CSharp/NEP5.Owner.cs | 12 -- .../Services/Neo/ContractTest.cs | 141 ------------------ .../TestClasses/Contract_Contract.cs | 15 -- .../TestClasses/Contract_Create.cs | 10 -- .../TestClasses/Contract_CreateAndUpdate.cs | 13 -- 7 files changed, 2 insertions(+), 211 deletions(-) delete mode 100644 tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_Create.cs delete mode 100644 tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs diff --git a/src/Neo.SmartContract.Framework/Services/Neo/Contract.cs b/src/Neo.SmartContract.Framework/Services/Neo/Contract.cs index 8b52e29a9..42829a49e 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/Contract.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/Contract.cs @@ -28,14 +28,8 @@ public class Contract [Syscall("System.Contract.CallEx")] public static extern object CallEx(UInt160 scriptHash, string method, object[] arguments, CallFlags flag); - [Syscall("System.Contract.Create")] - public static extern Contract Create(byte[] script, string manifest); - - [Syscall("System.Contract.Update")] - public static extern void Update(byte[] script, string manifest); - - [Syscall("System.Contract.Destroy")] - public static extern void Destroy(); + [Syscall("System.Contract.CallNative")] + public static extern object CallNative(string name); [Syscall("System.Contract.GetCallFlags")] public static extern byte GetCallFlags(); diff --git a/templates/Template.CSharp/Contract1.cs b/templates/Template.CSharp/Contract1.cs index 3dbe7f34f..33fcaa3ec 100644 --- a/templates/Template.CSharp/Contract1.cs +++ b/templates/Template.CSharp/Contract1.cs @@ -34,17 +34,5 @@ public static void _deploy(bool update) // It will be executed during deploy Storage.Put("Hello", "World"); } - - public static void Update(byte[] script, string manifest) - { - if (!IsOwner()) throw new Exception("No authorization."); - Contract.Update(script, manifest); - } - - public static void Destroy() - { - if (!IsOwner()) throw new Exception("No authorization."); - Contract.Destroy(); - } } } diff --git a/templates/Template.NEP5.CSharp/NEP5.Owner.cs b/templates/Template.NEP5.CSharp/NEP5.Owner.cs index 94bd8f545..3ae2b194d 100644 --- a/templates/Template.NEP5.CSharp/NEP5.Owner.cs +++ b/templates/Template.NEP5.CSharp/NEP5.Owner.cs @@ -19,18 +19,6 @@ public static bool Deploy() return true; } - public static void Update(byte[] script, string manifest) - { - if (!IsOwner()) throw new Exception("No authorization."); - Contract.Update(script, manifest); - } - - public static void Destroy() - { - if (!IsOwner()) throw new Exception("No authorization."); - Contract.Destroy(); - } - private static bool IsOwner() => Runtime.CheckWitness(Owner); } } diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/ContractTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/ContractTest.cs index 61d589592..98160453e 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/ContractTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/ContractTest.cs @@ -21,147 +21,6 @@ public void Init() _engine.AddEntryScript("./TestClasses/Contract_Contract.cs"); } - [TestMethod] - public void Test_CreateCallDestroy() - { - // Create - - var script = _engine.Build("./TestClasses/Contract_Create.cs"); - var manifest = ContractManifest.FromJson(JObject.Parse(script.finalManifest)); - - // Create - - _engine.Reset(); - var result = _engine.ExecuteTestCaseStandard("create", script.finalNEF, manifest.ToJson().ToString()); - Assert.AreEqual(VMState.HALT, _engine.State); - Assert.AreEqual(1, result.Count); - - var item = result.Pop(); - Assert.IsInstanceOfType(item, typeof(Array)); - var itemArray = item as Array; - Assert.AreEqual(script.finalNEF, itemArray[0]); // Script - Assert.AreEqual(manifest.ToString(), itemArray[1].GetString()); // Manifest - - // Call - - _engine.Reset(); - result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray(), "oldContract", new Array()); - Assert.AreEqual(VMState.HALT, _engine.State); - Assert.AreEqual(1, result.Count); - - item = result.Pop(); - Assert.IsInstanceOfType(item, typeof(Integer)); - Assert.AreEqual(123, item.GetInteger()); - - // Destroy - - _engine.Reset(); - result = _engine.ExecuteTestCaseStandard("destroy"); - Assert.AreEqual(VMState.HALT, _engine.State); - Assert.AreEqual(0, result.Count); - - // Check again for failures - - _engine.Reset(); - result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray()); - Assert.AreEqual(VMState.FAULT, _engine.State); - Assert.AreEqual(0, result.Count); - } - - [TestMethod] - public void Test_Update() - { - // Create - - var script = _engine.Build("./TestClasses/Contract_CreateAndUpdate.cs"); - var manifest = ContractManifest.FromJson(JObject.Parse(script.finalManifest)); - - var scriptUpdate = _engine.Build("./TestClasses/Contract_Update.cs"); - var manifestUpdate = ContractManifest.FromJson(JObject.Parse(scriptUpdate.finalManifest)); - - // Create - - _engine.Reset(); - var result = _engine.ExecuteTestCaseStandard("create", script.finalNEF, manifest.ToJson().ToString()); - Assert.AreEqual(VMState.HALT, _engine.State); - Assert.AreEqual(1, result.Count); - - var item = result.Pop(); - Assert.IsInstanceOfType(item, typeof(Array)); - var itemArray = item as Array; - Assert.AreEqual(script.finalNEF, itemArray[0]); // Script - Assert.AreEqual(manifest.ToString(), itemArray[1].GetString()); // Manifest - - // Call & Update - - _engine.Reset(); - var args = new Array - { - scriptUpdate.finalNEF, - manifestUpdate.ToJson().ToString() - }; - result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray(), "oldContract", args); - Assert.AreEqual(VMState.HALT, _engine.State); - Assert.AreEqual(1, result.Count); - - item = result.Pop(); - Assert.IsInstanceOfType(item, typeof(Integer)); - Assert.AreEqual(123, item.GetInteger()); - - // Call Again - - _engine.Reset(); - result = _engine.ExecuteTestCaseStandard("call", manifestUpdate.Hash.ToArray(), "newContract", new Array()); - Assert.AreEqual(VMState.HALT, _engine.State); - Assert.AreEqual(1, result.Count); - - item = result.Pop(); - Assert.IsInstanceOfType(item, typeof(Integer)); - Assert.AreEqual(124, item.GetInteger()); - - // Check again for failures - - _engine.Reset(); - result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray(), "oldContract", new Array()); - Assert.AreEqual(VMState.FAULT, _engine.State); - Assert.AreEqual(0, result.Count); - } - - [TestMethod] - public void Test_CreateStandardAccount() - { - // Wrong pubKey - - _engine.Reset(); - var result = _engine.ExecuteTestCaseStandard("createStandardAccount", new byte[] { 0x01, 0x02 }); - Assert.AreEqual(VMState.FAULT, _engine.State); - Assert.AreEqual(0, result.Count); - - _engine.Reset(); - - // Good pubKey (compressed) - - _engine.Reset(); - result = _engine.ExecuteTestCaseStandard("createStandardAccount", new byte[] { 0x02, 0x48, 0x6f, 0xd1, 0x57, 0x02, 0xc4, 0x49, 0x0a, 0x26, 0x70, 0x31, 0x12, 0xa5, 0xcc, 0x1d, 0x09, 0x23, 0xfd, 0x69, 0x7a, 0x33, 0x40, 0x6b, 0xd5, 0xa1, 0xc0, 0x0e, 0x00, 0x13, 0xb0, 0x9a, 0x70 }); - Assert.AreEqual(VMState.HALT, _engine.State); - Assert.AreEqual(1, result.Count); - - var item = result.Pop(); - Assert.IsTrue(item.Type == VM.Types.StackItemType.ByteString); - Assert.AreEqual("3ae15fc83b48d9bb5c327e578e2f1d2100ba1b89", item.GetSpan().ToHexString()); - - // Good pubKey (uncompressed) - - _engine.Reset(); - result = _engine.ExecuteTestCaseStandard("createStandardAccount", new byte[] { 0x04, 0x48, 0x6f, 0xd1, 0x57, 0x02, 0xc4, 0x49, 0x0a, 0x26, 0x70, 0x31, 0x12, 0xa5, 0xcc, 0x1d, 0x09, 0x23, 0xfd, 0x69, 0x7a, 0x33, 0x40, 0x6b, 0xd5, 0xa1, 0xc0, 0x0e, 0x00, 0x13, 0xb0, 0x9a, 0x70, 0x05, 0x43, 0x6c, 0x08, 0x2c, 0x2c, 0x88, 0x08, 0x5b, 0x4b, 0x53, 0xd5, 0x4c, 0x55, 0x66, 0xba, 0x44, 0x8d, 0x5c, 0x3e, 0x2a, 0x2a, 0x5c, 0x3a, 0x3e, 0xa5, 0x00, 0xe1, 0x40, 0x77, 0x55, 0x9c }); - Assert.AreEqual(VMState.HALT, _engine.State); - Assert.AreEqual(1, result.Count); - - item = result.Pop(); - Assert.IsTrue(item.Type == VM.Types.StackItemType.ByteString); - Assert.AreEqual("3ae15fc83b48d9bb5c327e578e2f1d2100ba1b89", item.GetSpan().ToHexString()); - } - [TestMethod] public void Test_GetCallFlags() { diff --git a/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_Contract.cs b/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_Contract.cs index 33c897562..45e56ed32 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_Contract.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_Contract.cs @@ -9,21 +9,6 @@ public static object Call(UInt160 scriptHash, string method, object[] arguments) return Contract.Call(scriptHash, method, arguments); } - public static object Create(byte[] script, string manifest) - { - return Contract.Create(script, manifest); - } - - public static void Update(byte[] script, string manifest) - { - Contract.Update(script, manifest); - } - - public static void Destroy() - { - Contract.Destroy(); - } - public static int GetCallFlags() { return Contract.GetCallFlags(); diff --git a/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_Create.cs b/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_Create.cs deleted file mode 100644 index 3cb016bf6..000000000 --- a/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_Create.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Neo.Compiler.MSIL.TestClasses -{ - public class Contract_Create : SmartContract.Framework.SmartContract - { - public static int OldContract() - { - return 123; - } - } -} diff --git a/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs b/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs deleted file mode 100644 index fb653e3b4..000000000 --- a/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Neo.SmartContract.Framework.Services.Neo; - -namespace Neo.Compiler.MSIL.TestClasses -{ - public class Contract_CreateAndUpdate : SmartContract.Framework.SmartContract - { - public static int OldContract(byte[] script, string manifest) - { - Contract.Update(script, manifest); - return 123; - } - } -} From b80c12f1b178ef62b5c4241f6b2a08c0269bf7a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BF=97=E5=90=8C?= Date: Mon, 7 Dec 2020 16:47:48 +0800 Subject: [PATCH 07/29] Update --- .../Services/Neo/GAS.cs | 2 + .../Services/Neo/ManagementContract.cs | 16 ++ .../Services/Neo/Oracle.cs | 2 + .../Services/Neo/Policy.cs | 2 + templates/Template.CSharp/Contract1.cs | 64 ++++---- templates/Template.NEP5.CSharp/NEP5.Owner.cs | 12 ++ .../Services/Neo/ContractTest.cs | 141 ++++++++++++++++++ .../TestClasses/Contract_Create.cs | 10 ++ .../TestClasses/Contract_CreateAndUpdate.cs | 14 ++ 9 files changed, 237 insertions(+), 26 deletions(-) create mode 100644 src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs create mode 100644 tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_Create.cs create mode 100644 tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs diff --git a/src/Neo.SmartContract.Framework/Services/Neo/GAS.cs b/src/Neo.SmartContract.Framework/Services/Neo/GAS.cs index 55840d805..cf3e8ecc6 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/GAS.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/GAS.cs @@ -9,6 +9,8 @@ public class GAS { public static extern UInt160 Hash { [ContractHash] get; } public static extern string Name { get; } + public static extern int Id { get; } + public static extern uint ActiveBlockIndex { get; } public static extern string Symbol { get; } public static extern byte Decimals { get; } public static extern BigInteger TotalSupply(); diff --git a/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs b/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs new file mode 100644 index 000000000..5faf25f0f --- /dev/null +++ b/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Neo.SmartContract.Framework.Services.Neo +{ + [Contract("0xdf18cb2476964c241558ed1e2e8881dcd2d50bde")] + class ManagementContract + { + public static extern UInt160 Hash { [ContractHash] get; } + public static extern string Name { get; } + public static extern int Id { get; } + public static extern uint ActiveBlockIndex { get; } + public static extern Contract GetContract(UInt160 hash); + } +} diff --git a/src/Neo.SmartContract.Framework/Services/Neo/Oracle.cs b/src/Neo.SmartContract.Framework/Services/Neo/Oracle.cs index f1e376bbc..8be32e99c 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/Oracle.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/Oracle.cs @@ -8,6 +8,8 @@ public class Oracle public static extern UInt160 Hash { [ContractHash] get; } public const uint MinimumResponseFee = 0_10000000; public static extern string Name { get; } + public static extern int Id { get; } + public static extern uint ActiveBlockIndex { get; } public static extern void Request(string url, string filter, string callback, object userData, long gasForResponse); } } diff --git a/src/Neo.SmartContract.Framework/Services/Neo/Policy.cs b/src/Neo.SmartContract.Framework/Services/Neo/Policy.cs index d68b43c3d..e09fe1043 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/Policy.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/Policy.cs @@ -10,6 +10,8 @@ public class Policy { public static extern UInt160 Hash { [ContractHash] get; } public static extern string Name(); + public static extern int Id { get; } + public static extern uint ActiveBlockIndex { get; } public static extern uint GetMaxTransactionsPerBlock(); public static extern uint GetMaxBlockSize(); public static extern long GetMaxBlockSystemFee(); diff --git a/templates/Template.CSharp/Contract1.cs b/templates/Template.CSharp/Contract1.cs index 33fcaa3ec..f5d3ba1e6 100644 --- a/templates/Template.CSharp/Contract1.cs +++ b/templates/Template.CSharp/Contract1.cs @@ -7,32 +7,44 @@ namespace $safeprojectname$ { [ManifestExtra("Author", "Neo")] - [ManifestExtra("Email", "dev@neo.org")] - [ManifestExtra("Description", "This is a contract example")] - public class Contract1 : SmartContract +[ManifestExtra("Email", "dev@neo.org")] +[ManifestExtra("Description", "This is a contract example")] +public class Contract1 : SmartContract +{ + //TODO: Replace it with your own address. + static readonly UInt160 Owner = "NiNmXL8FjEUEs1nfX9uHFBNaenxDHJtmuB".ToScriptHash(); + + private static bool IsOwner() => Runtime.CheckWitness(Owner); + + // When this contract address is included in the transaction signature, + // this method will be triggered as a VerificationTrigger to verify that the signature is correct. + // For example, this method needs to be called when withdrawing token from the contract. + public static bool Verify() => IsOwner(); + + // TODO: Replace it with your methods. + public static byte[] MyMethod() { - //TODO: Replace it with your own address. - static readonly UInt160 Owner = "NiNmXL8FjEUEs1nfX9uHFBNaenxDHJtmuB".ToScriptHash(); - - private static bool IsOwner() => Runtime.CheckWitness(Owner); - - // When this contract address is included in the transaction signature, - // this method will be triggered as a VerificationTrigger to verify that the signature is correct. - // For example, this method needs to be called when withdrawing token from the contract. - public static bool Verify() => IsOwner(); - - // TODO: Replace it with your methods. - public static byte[] MyMethod() - { - return Storage.Get("Hello"); - } - - public static void _deploy(bool update) - { - if (update) return; - - // It will be executed during deploy - Storage.Put("Hello", "World"); - } + return Storage.Get("Hello"); + } + + public static void _deploy(bool update) + { + if (update) return; + + // It will be executed during deploy + Storage.Put("Hello", "World"); } + + public static void Update(byte[] nefFile, string manifest) + { + if (!IsOwner()) throw new Exception("No authorization."); + Contract.Call(ManagementContract.Hash, "update", nefFile, manifest); + } + + public static void Destroy() + { + if (!IsOwner()) throw new Exception("No authorization."); + Contract.Call(ManagementContract.Hash, "destroy"); + } +} } diff --git a/templates/Template.NEP5.CSharp/NEP5.Owner.cs b/templates/Template.NEP5.CSharp/NEP5.Owner.cs index 3ae2b194d..8de191e87 100644 --- a/templates/Template.NEP5.CSharp/NEP5.Owner.cs +++ b/templates/Template.NEP5.CSharp/NEP5.Owner.cs @@ -19,6 +19,18 @@ public static bool Deploy() return true; } + public static void Update(byte[] nefFile, string manifest) + { + if (!IsOwner()) throw new Exception("No authorization."); + Contract.Call(ManagementContract.Hash, "update", nefFile, manifest); + } + + public static void Destroy() + { + if (!IsOwner()) throw new Exception("No authorization."); + Contract.Call(ManagementContract.Hash, "destroy"); + } + private static bool IsOwner() => Runtime.CheckWitness(Owner); } } diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/ContractTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/ContractTest.cs index 98160453e..61d589592 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/ContractTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/ContractTest.cs @@ -21,6 +21,147 @@ public void Init() _engine.AddEntryScript("./TestClasses/Contract_Contract.cs"); } + [TestMethod] + public void Test_CreateCallDestroy() + { + // Create + + var script = _engine.Build("./TestClasses/Contract_Create.cs"); + var manifest = ContractManifest.FromJson(JObject.Parse(script.finalManifest)); + + // Create + + _engine.Reset(); + var result = _engine.ExecuteTestCaseStandard("create", script.finalNEF, manifest.ToJson().ToString()); + Assert.AreEqual(VMState.HALT, _engine.State); + Assert.AreEqual(1, result.Count); + + var item = result.Pop(); + Assert.IsInstanceOfType(item, typeof(Array)); + var itemArray = item as Array; + Assert.AreEqual(script.finalNEF, itemArray[0]); // Script + Assert.AreEqual(manifest.ToString(), itemArray[1].GetString()); // Manifest + + // Call + + _engine.Reset(); + result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray(), "oldContract", new Array()); + Assert.AreEqual(VMState.HALT, _engine.State); + Assert.AreEqual(1, result.Count); + + item = result.Pop(); + Assert.IsInstanceOfType(item, typeof(Integer)); + Assert.AreEqual(123, item.GetInteger()); + + // Destroy + + _engine.Reset(); + result = _engine.ExecuteTestCaseStandard("destroy"); + Assert.AreEqual(VMState.HALT, _engine.State); + Assert.AreEqual(0, result.Count); + + // Check again for failures + + _engine.Reset(); + result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray()); + Assert.AreEqual(VMState.FAULT, _engine.State); + Assert.AreEqual(0, result.Count); + } + + [TestMethod] + public void Test_Update() + { + // Create + + var script = _engine.Build("./TestClasses/Contract_CreateAndUpdate.cs"); + var manifest = ContractManifest.FromJson(JObject.Parse(script.finalManifest)); + + var scriptUpdate = _engine.Build("./TestClasses/Contract_Update.cs"); + var manifestUpdate = ContractManifest.FromJson(JObject.Parse(scriptUpdate.finalManifest)); + + // Create + + _engine.Reset(); + var result = _engine.ExecuteTestCaseStandard("create", script.finalNEF, manifest.ToJson().ToString()); + Assert.AreEqual(VMState.HALT, _engine.State); + Assert.AreEqual(1, result.Count); + + var item = result.Pop(); + Assert.IsInstanceOfType(item, typeof(Array)); + var itemArray = item as Array; + Assert.AreEqual(script.finalNEF, itemArray[0]); // Script + Assert.AreEqual(manifest.ToString(), itemArray[1].GetString()); // Manifest + + // Call & Update + + _engine.Reset(); + var args = new Array + { + scriptUpdate.finalNEF, + manifestUpdate.ToJson().ToString() + }; + result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray(), "oldContract", args); + Assert.AreEqual(VMState.HALT, _engine.State); + Assert.AreEqual(1, result.Count); + + item = result.Pop(); + Assert.IsInstanceOfType(item, typeof(Integer)); + Assert.AreEqual(123, item.GetInteger()); + + // Call Again + + _engine.Reset(); + result = _engine.ExecuteTestCaseStandard("call", manifestUpdate.Hash.ToArray(), "newContract", new Array()); + Assert.AreEqual(VMState.HALT, _engine.State); + Assert.AreEqual(1, result.Count); + + item = result.Pop(); + Assert.IsInstanceOfType(item, typeof(Integer)); + Assert.AreEqual(124, item.GetInteger()); + + // Check again for failures + + _engine.Reset(); + result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray(), "oldContract", new Array()); + Assert.AreEqual(VMState.FAULT, _engine.State); + Assert.AreEqual(0, result.Count); + } + + [TestMethod] + public void Test_CreateStandardAccount() + { + // Wrong pubKey + + _engine.Reset(); + var result = _engine.ExecuteTestCaseStandard("createStandardAccount", new byte[] { 0x01, 0x02 }); + Assert.AreEqual(VMState.FAULT, _engine.State); + Assert.AreEqual(0, result.Count); + + _engine.Reset(); + + // Good pubKey (compressed) + + _engine.Reset(); + result = _engine.ExecuteTestCaseStandard("createStandardAccount", new byte[] { 0x02, 0x48, 0x6f, 0xd1, 0x57, 0x02, 0xc4, 0x49, 0x0a, 0x26, 0x70, 0x31, 0x12, 0xa5, 0xcc, 0x1d, 0x09, 0x23, 0xfd, 0x69, 0x7a, 0x33, 0x40, 0x6b, 0xd5, 0xa1, 0xc0, 0x0e, 0x00, 0x13, 0xb0, 0x9a, 0x70 }); + Assert.AreEqual(VMState.HALT, _engine.State); + Assert.AreEqual(1, result.Count); + + var item = result.Pop(); + Assert.IsTrue(item.Type == VM.Types.StackItemType.ByteString); + Assert.AreEqual("3ae15fc83b48d9bb5c327e578e2f1d2100ba1b89", item.GetSpan().ToHexString()); + + // Good pubKey (uncompressed) + + _engine.Reset(); + result = _engine.ExecuteTestCaseStandard("createStandardAccount", new byte[] { 0x04, 0x48, 0x6f, 0xd1, 0x57, 0x02, 0xc4, 0x49, 0x0a, 0x26, 0x70, 0x31, 0x12, 0xa5, 0xcc, 0x1d, 0x09, 0x23, 0xfd, 0x69, 0x7a, 0x33, 0x40, 0x6b, 0xd5, 0xa1, 0xc0, 0x0e, 0x00, 0x13, 0xb0, 0x9a, 0x70, 0x05, 0x43, 0x6c, 0x08, 0x2c, 0x2c, 0x88, 0x08, 0x5b, 0x4b, 0x53, 0xd5, 0x4c, 0x55, 0x66, 0xba, 0x44, 0x8d, 0x5c, 0x3e, 0x2a, 0x2a, 0x5c, 0x3a, 0x3e, 0xa5, 0x00, 0xe1, 0x40, 0x77, 0x55, 0x9c }); + Assert.AreEqual(VMState.HALT, _engine.State); + Assert.AreEqual(1, result.Count); + + item = result.Pop(); + Assert.IsTrue(item.Type == VM.Types.StackItemType.ByteString); + Assert.AreEqual("3ae15fc83b48d9bb5c327e578e2f1d2100ba1b89", item.GetSpan().ToHexString()); + } + [TestMethod] public void Test_GetCallFlags() { diff --git a/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_Create.cs b/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_Create.cs new file mode 100644 index 000000000..3cb016bf6 --- /dev/null +++ b/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_Create.cs @@ -0,0 +1,10 @@ +namespace Neo.Compiler.MSIL.TestClasses +{ + public class Contract_Create : SmartContract.Framework.SmartContract + { + public static int OldContract() + { + return 123; + } + } +} diff --git a/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs b/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs new file mode 100644 index 000000000..59a512de1 --- /dev/null +++ b/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs @@ -0,0 +1,14 @@ +using Neo.SmartContract.Framework.Services.Neo; + +namespace Neo.Compiler.MSIL.TestClasses +{ + public class Contract_CreateAndUpdate : SmartContract.Framework.SmartContract + { + public static int OldContract(byte[] nefFile, string manifest) + { + Contract.Update(script, manifest); + Contract.Call(ManagementContract.Hash, "update", nefFile, manifest); + return 123; + } + } +} From 25793fe623751c5906822786d38a61fc7e0c3299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BF=97=E5=90=8C?= Date: Mon, 7 Dec 2020 16:50:25 +0800 Subject: [PATCH 08/29] remove CallNative --- src/Neo.SmartContract.Framework/Services/Neo/Contract.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Neo.SmartContract.Framework/Services/Neo/Contract.cs b/src/Neo.SmartContract.Framework/Services/Neo/Contract.cs index 42829a49e..9bb82357b 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/Contract.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/Contract.cs @@ -28,9 +28,6 @@ public class Contract [Syscall("System.Contract.CallEx")] public static extern object CallEx(UInt160 scriptHash, string method, object[] arguments, CallFlags flag); - [Syscall("System.Contract.CallNative")] - public static extern object CallNative(string name); - [Syscall("System.Contract.GetCallFlags")] public static extern byte GetCallFlags(); From 6dc95ee7613e1c5e1b42d7a4f8d9071bb69e669e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BF=97=E5=90=8C?= Date: Tue, 8 Dec 2020 02:50:53 -0600 Subject: [PATCH 09/29] Update templates/Template.CSharp/Contract1.cs Co-authored-by: Owen Zhang <38493437+superboyiii@users.noreply.github.com> --- templates/Template.CSharp/Contract1.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/Template.CSharp/Contract1.cs b/templates/Template.CSharp/Contract1.cs index f5d3ba1e6..a6c91d9c2 100644 --- a/templates/Template.CSharp/Contract1.cs +++ b/templates/Template.CSharp/Contract1.cs @@ -38,7 +38,7 @@ public static void _deploy(bool update) public static void Update(byte[] nefFile, string manifest) { if (!IsOwner()) throw new Exception("No authorization."); - Contract.Call(ManagementContract.Hash, "update", nefFile, manifest); + Contract.Call(ManagementContract.Hash, "update", new object[] { nefFile, manifest }); } public static void Destroy() From ca0a550b4e9f9a5352d334aa6f061f7289915dc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BF=97=E5=90=8C?= Date: Tue, 8 Dec 2020 02:51:00 -0600 Subject: [PATCH 10/29] Update templates/Template.CSharp/Contract1.cs Co-authored-by: Owen Zhang <38493437+superboyiii@users.noreply.github.com> --- templates/Template.CSharp/Contract1.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/Template.CSharp/Contract1.cs b/templates/Template.CSharp/Contract1.cs index a6c91d9c2..eb38f0bfa 100644 --- a/templates/Template.CSharp/Contract1.cs +++ b/templates/Template.CSharp/Contract1.cs @@ -44,7 +44,7 @@ public static void Update(byte[] nefFile, string manifest) public static void Destroy() { if (!IsOwner()) throw new Exception("No authorization."); - Contract.Call(ManagementContract.Hash, "destroy"); + Contract.Call(ManagementContract.Hash, "destroy", new object[] {}); } } } From da4e1d8919de1b3cf530d66fbfba9c3765d693aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BF=97=E5=90=8C?= Date: Tue, 8 Dec 2020 02:51:08 -0600 Subject: [PATCH 11/29] Update tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs Co-authored-by: Owen Zhang <38493437+superboyiii@users.noreply.github.com> --- .../TestClasses/Contract_CreateAndUpdate.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs b/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs index 59a512de1..732226467 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs @@ -7,7 +7,7 @@ public class Contract_CreateAndUpdate : SmartContract.Framework.SmartContract public static int OldContract(byte[] nefFile, string manifest) { Contract.Update(script, manifest); - Contract.Call(ManagementContract.Hash, "update", nefFile, manifest); + Contract.Call(ManagementContract.Hash, "update", new object[] { nefFile, manifest }); return 123; } } From e5d168511328a055241bf9c1144e530dbd7569cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BF=97=E5=90=8C?= Date: Wed, 9 Dec 2020 16:47:21 +0800 Subject: [PATCH 12/29] public --- .../Services/Neo/ManagementContract.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs b/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs index 5faf25f0f..2a8921c86 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs @@ -5,7 +5,7 @@ namespace Neo.SmartContract.Framework.Services.Neo { [Contract("0xdf18cb2476964c241558ed1e2e8881dcd2d50bde")] - class ManagementContract + public class ManagementContract { public static extern UInt160 Hash { [ContractHash] get; } public static extern string Name { get; } From e2cf49b74a6d4885865942f7dd5a71204a21848c Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 10 Dec 2020 10:29:40 +0100 Subject: [PATCH 13/29] Update --- .../Services/Neo/ManagementContract.cs | 8 ++++---- .../TestClasses/Contract_CreateAndUpdate.cs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs b/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs index 2a8921c86..876a51ecb 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs @@ -1,7 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Text; - namespace Neo.SmartContract.Framework.Services.Neo { [Contract("0xdf18cb2476964c241558ed1e2e8881dcd2d50bde")] @@ -12,5 +8,9 @@ public class ManagementContract public static extern int Id { get; } public static extern uint ActiveBlockIndex { get; } public static extern Contract GetContract(UInt160 hash); + public static extern Contract Deploy(byte[] nefFile, string manifest); + public static extern void Update(byte[] nefFile, string manifest); + public static extern void Destroy(); + } } diff --git a/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs b/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs index 732226467..0d879ef7a 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs @@ -7,7 +7,7 @@ public class Contract_CreateAndUpdate : SmartContract.Framework.SmartContract public static int OldContract(byte[] nefFile, string manifest) { Contract.Update(script, manifest); - Contract.Call(ManagementContract.Hash, "update", new object[] { nefFile, manifest }); + ManagementContract.Update(nefFile, manifest); return 123; } } From 479d5dd34f7848be68f415bd657eee193810e650 Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 10 Dec 2020 10:31:04 +0100 Subject: [PATCH 14/29] Update call --- templates/Template.NEP17.CSharp/NEP17.Owner.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/Template.NEP17.CSharp/NEP17.Owner.cs b/templates/Template.NEP17.CSharp/NEP17.Owner.cs index df908dd84..5a27a5181 100644 --- a/templates/Template.NEP17.CSharp/NEP17.Owner.cs +++ b/templates/Template.NEP17.CSharp/NEP17.Owner.cs @@ -21,13 +21,13 @@ public static bool Deploy() public static void Update(byte[] nefFile, string manifest) { if (!IsOwner()) throw new Exception("No authorization."); - Contract.Update(nefFile, manifest); + ManagementContract.Update(nefFile, manifest); } public static void Destroy() { if (!IsOwner()) throw new Exception("No authorization."); - Contract.Destroy(); + ManagementContract.Destroy(); } public static void EnablePayment() From 0bd69154d0245fd4ff472adc94efe9f61cd81cad Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 10 Dec 2020 10:36:36 +0100 Subject: [PATCH 15/29] Fix more UT --- .../Neo.Compiler.MSIL.csproj | 2 +- .../Services/Neo/ManagementContract.cs | 1 - templates/Template.CSharp/Contract1.cs | 78 +++++++++---------- .../Utils/TestSnapshot.cs | 6 -- 4 files changed, 40 insertions(+), 47 deletions(-) diff --git a/src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj b/src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj index 061c50793..c15ea6b68 100644 --- a/src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj +++ b/src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj @@ -43,7 +43,7 @@ - + diff --git a/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs b/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs index 876a51ecb..3731ad01f 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs @@ -11,6 +11,5 @@ public class ManagementContract public static extern Contract Deploy(byte[] nefFile, string manifest); public static extern void Update(byte[] nefFile, string manifest); public static extern void Destroy(); - } } diff --git a/templates/Template.CSharp/Contract1.cs b/templates/Template.CSharp/Contract1.cs index eb38f0bfa..58290c406 100644 --- a/templates/Template.CSharp/Contract1.cs +++ b/templates/Template.CSharp/Contract1.cs @@ -6,45 +6,45 @@ namespace $safeprojectname$ { - [ManifestExtra("Author", "Neo")] -[ManifestExtra("Email", "dev@neo.org")] -[ManifestExtra("Description", "This is a contract example")] -public class Contract1 : SmartContract -{ - //TODO: Replace it with your own address. - static readonly UInt160 Owner = "NiNmXL8FjEUEs1nfX9uHFBNaenxDHJtmuB".ToScriptHash(); - - private static bool IsOwner() => Runtime.CheckWitness(Owner); - - // When this contract address is included in the transaction signature, - // this method will be triggered as a VerificationTrigger to verify that the signature is correct. - // For example, this method needs to be called when withdrawing token from the contract. - public static bool Verify() => IsOwner(); - - // TODO: Replace it with your methods. - public static byte[] MyMethod() + [ManifestExtra("Author", "Neo")] + [ManifestExtra("Email", "dev@neo.org")] + [ManifestExtra("Description", "This is a contract example")] + public class Contract1 : SmartContract { - return Storage.Get("Hello"); - } - - public static void _deploy(bool update) - { - if (update) return; - - // It will be executed during deploy - Storage.Put("Hello", "World"); + //TODO: Replace it with your own address. + static readonly UInt160 Owner = "NiNmXL8FjEUEs1nfX9uHFBNaenxDHJtmuB".ToScriptHash(); + + private static bool IsOwner() => Runtime.CheckWitness(Owner); + + // When this contract address is included in the transaction signature, + // this method will be triggered as a VerificationTrigger to verify that the signature is correct. + // For example, this method needs to be called when withdrawing token from the contract. + public static bool Verify() => IsOwner(); + + // TODO: Replace it with your methods. + public static byte[] MyMethod() + { + return Storage.Get("Hello"); + } + + public static void _deploy(bool update) + { + if (update) return; + + // It will be executed during deploy + Storage.Put("Hello", "World"); + } + + public static void Update(byte[] nefFile, string manifest) + { + if (!IsOwner()) throw new Exception("No authorization."); + ManagementContract.Update(nefFile, manifest); + } + + public static void Destroy() + { + if (!IsOwner()) throw new Exception("No authorization."); + ManagementContract.Destroy(); + } } - - public static void Update(byte[] nefFile, string manifest) - { - if (!IsOwner()) throw new Exception("No authorization."); - Contract.Call(ManagementContract.Hash, "update", new object[] { nefFile, manifest }); - } - - public static void Destroy() - { - if (!IsOwner()) throw new Exception("No authorization."); - Contract.Call(ManagementContract.Hash, "destroy", new object[] {}); - } -} } diff --git a/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestSnapshot.cs b/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestSnapshot.cs index 5953b9a8d..d60429bbf 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestSnapshot.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestSnapshot.cs @@ -10,23 +10,17 @@ public class TestSnapshot : StoreView { private DataCache _Blocks = new TestDataCache(); private DataCache _Transactions = new TestDataCache(); - private DataCache _Contracts = new TestDataCache(); private DataCache _Storages = new TestDataCache(); private DataCache, HeaderHashList> _HeaderHashList = new TestDataCache, HeaderHashList>(); private MetaDataCache _BlockHashIndex = new TestMetaDataCache(); private MetaDataCache _HeaderHashIndex = new TestMetaDataCache(); - private MetaDataCache _ContractId = new TestMetaDataCache(); public override DataCache Blocks => _Blocks; public override DataCache Transactions => _Transactions; - public override DataCache Contracts => _Contracts; - public override DataCache Storages => _Storages; - public override MetaDataCache ContractId => _ContractId; - public override DataCache, HeaderHashList> HeaderHashList => _HeaderHashList; public override MetaDataCache BlockHashIndex => _BlockHashIndex; From 545143a301086a64a017297fb66bae73541c559e Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 10 Dec 2020 10:39:08 +0100 Subject: [PATCH 16/29] Update hashes --- src/Neo.SmartContract.Framework/Services/Neo/Designation.cs | 2 +- src/Neo.SmartContract.Framework/Services/Neo/GAS.cs | 2 +- src/Neo.SmartContract.Framework/Services/Neo/NEO.cs | 2 +- src/Neo.SmartContract.Framework/Services/Neo/Policy.cs | 2 +- .../Services/Neo/BlockchainTest.cs | 1 - 5 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Neo.SmartContract.Framework/Services/Neo/Designation.cs b/src/Neo.SmartContract.Framework/Services/Neo/Designation.cs index 0c1312014..6559e7e3a 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/Designation.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/Designation.cs @@ -2,7 +2,7 @@ namespace Neo.SmartContract.Framework.Services.Neo { - [Contract("0x7062149f9377e3a110a343f811b9e406f8ef7824")] + [Contract("0x7ab39c37afd995f2f947a7ecbf40e91307058595")] public class Designation { public static extern UInt160 Hash { [ContractHash] get; } diff --git a/src/Neo.SmartContract.Framework/Services/Neo/GAS.cs b/src/Neo.SmartContract.Framework/Services/Neo/GAS.cs index ddc1bcaec..b0e3b226d 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/GAS.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/GAS.cs @@ -4,7 +4,7 @@ namespace Neo.SmartContract.Framework.Services.Neo { - [Contract("0x36a019d836d964c438c573f78badf79b9e7eebdd")] + [Contract("0xb399c051778cf37a1e4ef88509b2e054d0420a32")] public class GAS { public static extern UInt160 Hash { [ContractHash] get; } diff --git a/src/Neo.SmartContract.Framework/Services/Neo/NEO.cs b/src/Neo.SmartContract.Framework/Services/Neo/NEO.cs index 7ab283f1c..7d042c196 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/NEO.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/NEO.cs @@ -5,7 +5,7 @@ namespace Neo.SmartContract.Framework.Services.Neo { - [Contract("0xe22f9134cef8b03e53f71b3f960a20a65cddc972")] + [Contract("0x74c21a1ca66b7a190bf2a65db83ba6fe550cea64")] public class NEO { public static extern UInt160 Hash { [ContractHash] get; } diff --git a/src/Neo.SmartContract.Framework/Services/Neo/Policy.cs b/src/Neo.SmartContract.Framework/Services/Neo/Policy.cs index 3b6a6f076..9982223c1 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/Policy.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/Policy.cs @@ -4,7 +4,7 @@ namespace Neo.SmartContract.Framework.Services.Neo { - [Contract("0x1ca594b36b6b6b3f05efce8b106c824053d18713")] + [Contract("0xe8ff1989c19526f4d8102f226e2c6c993b63efe9")] public class Policy { public static extern UInt160 Hash { [ContractHash] get; } diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/BlockchainTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/BlockchainTest.cs index 2abd18b83..7d35af549 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/BlockchainTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/BlockchainTest.cs @@ -338,7 +338,6 @@ public void GetContract() Groups = new Manifest.ContractGroup[0], Trusts = Manifest.WildcardContainer.Create(), Permissions = new Manifest.ContractPermission[0], - SafeMethods = Manifest.WildcardContainer.Create(), Abi = new Manifest.ContractAbi() { Methods = new Manifest.ContractMethodDescriptor[0], From c2f974b500e83e612d562158fbc4b52577571330 Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 10 Dec 2020 10:58:17 +0100 Subject: [PATCH 17/29] Mock contract create --- .../UnitTest_ContractCall.cs | 5 +++-- .../UnitTest_NULL.cs | 7 +++++-- .../Utils/TestEngine.cs | 8 ------- .../Utils/TestSnapshot.cs | 7 +++++++ .../Services/Neo/AccountTest.cs | 4 ++-- .../Services/Neo/BlockchainTest.cs | 2 +- .../Services/Neo/RuntimeTest.cs | 4 ++-- .../Services/Neo/StaticStorageMapTest.cs | 8 +++---- .../Services/Neo/StorageTest.cs | 3 ++- .../Services/System/BinaryTest.cs | 6 +++--- .../Services/System/CallbackTest.cs | 4 ++-- .../UnitTest_NEP17.cs | 21 ++++++++++++------- 12 files changed, 44 insertions(+), 35 deletions(-) diff --git a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ContractCall.cs b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ContractCall.cs index 153486004..2026706a5 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ContractCall.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ContractCall.cs @@ -1,6 +1,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Neo.Compiler.MSIL.UnitTests.Utils; using Neo.IO.Json; +using Neo.SmartContract; using Neo.SmartContract.Manifest; using Neo.VM; using Neo.VM.Types; @@ -17,9 +18,9 @@ public void Init() { var hash = UInt160.Parse("0102030405060708090A0102030405060708090A"); _engine = new TestEngine(); - _engine.Snapshot.Contracts.Add(hash, new Ledger.ContractState() + ((TestSnapshot)_engine.Snapshot).ContractAdd(new ContractState() { - Hash = UInt160.Zero, + Hash = hash, Script = _engine.Build("./TestClasses/Contract1.cs").finalNEFScript, Manifest = ContractManifest.FromJson(JObject.Parse(_engine.Build("./TestClasses/Contract1.cs").finalManifest)), }); diff --git a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NULL.cs b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NULL.cs index 275088389..0923567d1 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NULL.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NULL.cs @@ -1,5 +1,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Neo.Compiler.MSIL.UnitTests.Utils; +using Neo.SmartContract; using Neo.SmartContract.Manifest; using Neo.VM.Types; @@ -108,8 +109,9 @@ public void NullCollationAndCollation() { var _testengine = new TestEngine(); _testengine.AddEntryScript("./TestClasses/Contract_NULL.cs"); - _testengine.Snapshot.Contracts.Add(testengine.EntryScriptHash, new Ledger.ContractState() + ((TestSnapshot)_testengine.Snapshot).ContractAdd(new ContractState() { + Hash = testengine.EntryScriptHash, Script = testengine.EntryContext.Script, Manifest = new ContractManifest() }); @@ -124,8 +126,9 @@ public void NullCollationAndCollation2() { var _testengine = new TestEngine(); _testengine.AddEntryScript("./TestClasses/Contract_NULL.cs"); - _testengine.Snapshot.Contracts.Add(testengine.EntryScriptHash, new Ledger.ContractState() + ((TestSnapshot)_testengine.Snapshot).ContractAdd(new ContractState() { + Hash = testengine.EntryScriptHash, Script = testengine.EntryContext.Script, Manifest = new ContractManifest() }); diff --git a/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestEngine.cs b/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestEngine.cs index e3a9e843e..001464b24 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestEngine.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestEngine.cs @@ -14,14 +14,6 @@ namespace Neo.Compiler.MSIL.UnitTests.Utils public class TestEngine : ApplicationEngine { public const long TestGas = 2000_000_000; - public static InteropDescriptor Native_Deploy; - - static TestEngine() - { - // Extract Native deploy syscall - - Native_Deploy = Neo_Native_Deploy; - } static readonly IDictionary scriptsAll = new Dictionary(); diff --git a/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestSnapshot.cs b/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestSnapshot.cs index d60429bbf..56395444b 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestSnapshot.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestSnapshot.cs @@ -3,6 +3,7 @@ using Neo.Ledger; using Neo.Network.P2P.Payloads; using Neo.Persistence; +using Neo.SmartContract; namespace Neo.Compiler.MSIL.UnitTests.Utils { @@ -35,5 +36,11 @@ public void SetPersistingBlock(Block block) { this.GetType().GetProperty("PersistingBlock").SetValue(this, block); } + + public void ContractAdd(ContractState contract) + { + var key = new KeyBuilder(0, 8).Add(contract.Hash); + Storages.Add(key, new StorageItem(contract)); + } } } diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/AccountTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/AccountTest.cs index 84175c104..c9a0b6541 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/AccountTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/AccountTest.cs @@ -1,6 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Neo.Compiler.MSIL.UnitTests.Utils; -using Neo.Ledger; using Neo.VM.Types; namespace Neo.SmartContract.Framework.UnitTests.Services.Neo @@ -26,8 +25,9 @@ public void Test_AccountIsStandard() 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF }; - _engine.Snapshot.Contracts.Add(new UInt160(noStandard), new ContractState() + ((TestSnapshot)_engine.Snapshot).ContractAdd(new ContractState() { + Hash = new UInt160(noStandard), Script = new byte[0] { } }); diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/BlockchainTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/BlockchainTest.cs index 7d35af549..3b67c7a26 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/BlockchainTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/BlockchainTest.cs @@ -345,7 +345,7 @@ public void GetContract() }, } }; - _engine.Snapshot.Contracts.GetOrAdd(contract.Hash, () => contract); + ((TestSnapshot)_engine.Snapshot).ContractAdd(contract); // Not found diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/RuntimeTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/RuntimeTest.cs index f9c7fd3b5..2f78a0651 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/RuntimeTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/RuntimeTest.cs @@ -54,11 +54,11 @@ public void Test_InvocationCounter() var contract = _engine.EntryScriptHash; var engine = new TestEngine(TriggerType.Application, new DummyVerificable()); - engine.Snapshot.Contracts.Add(contract, new Ledger.ContractState() + ((TestSnapshot)engine.Snapshot).ContractAdd(new ContractState() { + Hash = contract, Script = _engine.InvocationStack.Peek().Script, Manifest = ContractManifest.FromJson(JObject.Parse(_engine.Build("./TestClasses/Contract_Runtime.cs").finalManifest)), - Hash = UInt160.Zero }); using (ScriptBuilder sb = new ScriptBuilder()) diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/StaticStorageMapTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/StaticStorageMapTest.cs index 6f397f58f..61cce0c7c 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/StaticStorageMapTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/StaticStorageMapTest.cs @@ -4,7 +4,6 @@ using Neo.Compiler.MSIL.UnitTests.Utils; using Neo.Ledger; using Neo.VM; -using System.Linq; namespace Neo.SmartContract.Framework.UnitTests.Services.Neo { @@ -17,12 +16,13 @@ public class StaticStorageMapTest public void Init() { var _ = TestBlockchain.TheNeoSystem; - var snapshot = Blockchain.Singleton.GetSnapshot(); + var snapshot = (TestSnapshot)Blockchain.Singleton.GetSnapshot().Clone(); - _engine = new TestEngine(snapshot: snapshot.Clone()); + _engine = new TestEngine(snapshot: snapshot); _engine.AddEntryScript("./TestClasses/Contract_StaticStorageMap.cs"); - _engine.Snapshot.Contracts.Add(_engine.EntryScriptHash, new ContractState() + snapshot.ContractAdd(new ContractState() { + Hash = _engine.EntryScriptHash, Script = _engine.EntryContext.Script, Manifest = new Manifest.ContractManifest() }); diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/StorageTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/StorageTest.cs index b0bb73f28..df3509f52 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/StorageTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/StorageTest.cs @@ -65,8 +65,9 @@ public void Init() testengine = new TestEngine(snapshot: snapshot.Clone()); testengine.AddEntryScript("./TestClasses/Contract_Storage.cs"); - testengine.Snapshot.Contracts.Add(testengine.EntryScriptHash, new Ledger.ContractState() + ((TestSnapshot)testengine.Snapshot).ContractAdd(new ContractState() { + Hash = testengine.EntryScriptHash, Script = testengine.EntryContext.Script, Manifest = new Manifest.ContractManifest() }); diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/System/BinaryTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/System/BinaryTest.cs index 02a0857f2..3c9b4bc0e 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/System/BinaryTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/System/BinaryTest.cs @@ -5,7 +5,6 @@ using Neo.SmartContract.Framework.UnitTests; using Neo.SmartContract.Manifest; using Neo.VM; -using Neo.VM.Types; namespace Neo.Compiler.MSIL.SmartContractFramework.Services.System { @@ -19,15 +18,16 @@ public class BinaryTest public void Init() { var _ = TestBlockchain.TheNeoSystem; - var snapshot = Blockchain.Singleton.GetSnapshot().Clone(); + var snapshot = (TestSnapshot)Blockchain.Singleton.GetSnapshot().Clone(); snapshot.BlockHashIndex.Get().Index = 1234; _engine = new TestEngine(TriggerType.Application, snapshot: snapshot); _engine.AddEntryScript("./TestClasses/Contract_Binary.cs"); scriptHash = _engine.ScriptEntry.finalNEFScript.ToScriptHash(); - snapshot.Contracts.Add(scriptHash, new ContractState() + snapshot.ContractAdd(new ContractState() { + Hash = scriptHash, Script = _engine.ScriptEntry.finalNEFScript, Manifest = ContractManifest.Parse(_engine.ScriptEntry.finalManifest) }); diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/System/CallbackTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/System/CallbackTest.cs index cea38d16a..4ef4067e4 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/System/CallbackTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/System/CallbackTest.cs @@ -24,14 +24,14 @@ public class CallbackTest public void Init() { var _ = TestBlockchain.TheNeoSystem; - var snapshot = Blockchain.Singleton.GetSnapshot().Clone(); + var snapshot = (TestSnapshot)Blockchain.Singleton.GetSnapshot().Clone(); snapshot.BlockHashIndex.Get().Index = 1234; _engine = new TestEngine(TriggerType.Application, snapshot: snapshot); _engine.AddEntryScript("./TestClasses/Contract_Callback.cs"); scriptHash = _engine.ScriptEntry.finalNEFScript.ToScriptHash(); - snapshot.Contracts.Add(scriptHash, new ContractState() + snapshot.ContractAdd(new ContractState() { Hash = scriptHash, Script = _engine.ScriptEntry.finalNEFScript, diff --git a/tests/Template.NEP17.UnitTests/UnitTest_NEP17.cs b/tests/Template.NEP17.UnitTests/UnitTest_NEP17.cs index 42d312ffd..823ee04d5 100644 --- a/tests/Template.NEP17.UnitTests/UnitTest_NEP17.cs +++ b/tests/Template.NEP17.UnitTests/UnitTest_NEP17.cs @@ -1,6 +1,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Neo.Compiler.MSIL.UnitTests.Utils; using Neo.IO.Json; +using Neo.SmartContract; using Neo.VM.Types; using System.Linq; using System.Numerics; @@ -72,10 +73,11 @@ public void Test_totalSupply() { var engine = CreateEngine(); var hash = engine.CurrentScriptHash; - var snapshot = engine.Snapshot; + var snapshot = (TestSnapshot)engine.Snapshot; - snapshot.Contracts.Add(hash, new Neo.Ledger.ContractState() + snapshot.ContractAdd(new ContractState() { + Hash = hash, Manifest = new Neo.SmartContract.Manifest.ContractManifest() }); @@ -103,10 +105,11 @@ public void Test_totalSupply_empty() { var engine = CreateEngine(); var hash = engine.CurrentScriptHash; - var snapshot = engine.Snapshot; + var snapshot = (TestSnapshot)engine.Snapshot; - snapshot.Contracts.Add(hash, new Neo.Ledger.ContractState() + snapshot.ContractAdd(new ContractState() { + Hash = hash, Manifest = new Neo.SmartContract.Manifest.ContractManifest() }); @@ -123,11 +126,12 @@ public void Test_balanceOf() { var engine = CreateEngine(); var hash = engine.CurrentScriptHash; - var snapshot = engine.Snapshot; + var snapshot = (TestSnapshot)engine.Snapshot; var address = new byte[] { 0xf6, 0x64, 0x43, 0x49, 0x8d, 0x38, 0x78, 0xd3, 0x2b, 0x99, 0x4e, 0x4e, 0x12, 0x83, 0xc6, 0x93, 0x44, 0x21, 0xda, 0xfe }; - snapshot.Contracts.Add(hash, new Neo.Ledger.ContractState() + snapshot.ContractAdd(new ContractState() { + Hash = hash, Manifest = new Neo.SmartContract.Manifest.ContractManifest() }); @@ -155,11 +159,12 @@ public void Test_balanceOf_empty() { var engine = CreateEngine(); var hash = engine.CurrentScriptHash; - var snapshot = engine.Snapshot; + var snapshot = (TestSnapshot)engine.Snapshot; var address = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 }; - snapshot.Contracts.Add(hash, new Neo.Ledger.ContractState() + snapshot.ContractAdd(new ContractState() { + Hash = hash, Manifest = new Neo.SmartContract.Manifest.ContractManifest() }); From f287e4d0031e84e3872ff2c1d6bf1cc3702a7a5d Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 10 Dec 2020 11:25:04 +0100 Subject: [PATCH 18/29] Fix more UT --- .../Services/Neo/Blockchain.cs | 3 --- .../Template.NEP17.CSharp/NEP17.Helpers.cs | 2 +- .../Extensions/TestExtensions.cs | 23 +++++++++++++++++ .../UnitTest_ContractCall.cs | 3 ++- .../UnitTest_NULL.cs | 3 ++- .../UnitTest_NativeContracts.cs | 25 ++++++------------- .../Utils/TestEngine.cs | 7 ++---- .../Utils/TestSnapshot.cs | 6 ----- .../Services/Neo/AccountTest.cs | 3 ++- .../Services/Neo/BlockchainTest.cs | 3 ++- .../Services/Neo/NativeTest.cs | 9 ++----- .../Services/Neo/RuntimeTest.cs | 3 ++- .../Services/Neo/StaticStorageMapTest.cs | 1 + .../Services/Neo/StorageTest.cs | 3 ++- .../Services/System/BinaryTest.cs | 1 + .../Services/System/CallbackTest.cs | 3 ++- .../SyscallTest.cs | 1 - .../TestClasses/Contract_CreateAndUpdate.cs | 1 - .../UnitTest_NEP17.cs | 9 ++++--- 19 files changed, 57 insertions(+), 52 deletions(-) create mode 100644 tests/Neo.Compiler.MSIL.UnitTests/Extensions/TestExtensions.cs diff --git a/src/Neo.SmartContract.Framework/Services/Neo/Blockchain.cs b/src/Neo.SmartContract.Framework/Services/Neo/Blockchain.cs index d23580261..032a8a04c 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/Blockchain.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/Blockchain.cs @@ -24,8 +24,5 @@ public static class Blockchain [Syscall("System.Blockchain.GetTransactionHeight")] public static extern BigInteger GetTransactionHeight(UInt256 hash); - - [Syscall("System.Blockchain.GetContract")] - public static extern Contract GetContract(UInt160 script_hash); } } diff --git a/templates/Template.NEP17.CSharp/NEP17.Helpers.cs b/templates/Template.NEP17.CSharp/NEP17.Helpers.cs index 51c109665..88f131cb3 100644 --- a/templates/Template.NEP17.CSharp/NEP17.Helpers.cs +++ b/templates/Template.NEP17.CSharp/NEP17.Helpers.cs @@ -7,6 +7,6 @@ namespace Template.NEP17.CSharp public partial class NEP17 : SmartContract { private static bool ValidateAddress(UInt160 address) => address.IsValid && !address.IsZero; - private static bool IsDeployed(UInt160 address) => Blockchain.GetContract(address) != null; + private static bool IsDeployed(UInt160 address) => ManagementContract.GetContract(address) != null; } } diff --git a/tests/Neo.Compiler.MSIL.UnitTests/Extensions/TestExtensions.cs b/tests/Neo.Compiler.MSIL.UnitTests/Extensions/TestExtensions.cs new file mode 100644 index 000000000..46e81508d --- /dev/null +++ b/tests/Neo.Compiler.MSIL.UnitTests/Extensions/TestExtensions.cs @@ -0,0 +1,23 @@ +using Neo.Compiler.MSIL.UnitTests.Utils; +using Neo.Ledger; +using Neo.Persistence; +using Neo.SmartContract; + +namespace Neo.Compiler.MSIL.Extensions +{ + public static class TestExtensions + { + public static void ContractAdd(this StoreView snapshot, ContractState contract) + { + var key = new KeyBuilder(0, 8).Add(contract.Hash); + snapshot.Storages.Add(key, new StorageItem(contract)); + } + + public static void DeployNativeContracts(this StoreView snapshot) + { + var method = typeof(SmartContract.Native.ManagementContract).GetMethod("OnPersist", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); + var engine = new TestEngine(TriggerType.OnPersist, null, snapshot); + method.Invoke(SmartContract.Native.NativeContract.Management, new object[] { engine }); + } + } +} diff --git a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ContractCall.cs b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ContractCall.cs index 2026706a5..d74a92c70 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ContractCall.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ContractCall.cs @@ -1,4 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Compiler.MSIL.Extensions; using Neo.Compiler.MSIL.UnitTests.Utils; using Neo.IO.Json; using Neo.SmartContract; @@ -18,7 +19,7 @@ public void Init() { var hash = UInt160.Parse("0102030405060708090A0102030405060708090A"); _engine = new TestEngine(); - ((TestSnapshot)_engine.Snapshot).ContractAdd(new ContractState() + _engine.Snapshot.ContractAdd(new ContractState() { Hash = hash, Script = _engine.Build("./TestClasses/Contract1.cs").finalNEFScript, diff --git a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NULL.cs b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NULL.cs index 0923567d1..50b57a337 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NULL.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NULL.cs @@ -1,4 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Compiler.MSIL.Extensions; using Neo.Compiler.MSIL.UnitTests.Utils; using Neo.SmartContract; using Neo.SmartContract.Manifest; @@ -109,7 +110,7 @@ public void NullCollationAndCollation() { var _testengine = new TestEngine(); _testengine.AddEntryScript("./TestClasses/Contract_NULL.cs"); - ((TestSnapshot)_testengine.Snapshot).ContractAdd(new ContractState() + _testengine.Snapshot.ContractAdd(new ContractState() { Hash = testengine.EntryScriptHash, Script = testengine.EntryContext.Script, diff --git a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NativeContracts.cs b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NativeContracts.cs index f39ef46ec..518384e75 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NativeContracts.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NativeContracts.cs @@ -1,4 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Compiler.MSIL.Extensions; using Neo.Compiler.MSIL.UnitTests.Utils; using Neo.SmartContract; using Neo.SmartContract.Native; @@ -14,30 +15,20 @@ public class Contract_NativeContracts [TestInitialize] public void Test_Init() { - byte[] script; - using (ScriptBuilder sb = new ScriptBuilder()) - { - sb.EmitSysCall(ApplicationEngine.Neo_Native_Deploy); - script = sb.ToArray(); - } - // Fake native deploy - - typeof(TestSnapshot).GetProperty("PersistingBlock").SetValue(snapshot, new Network.P2P.Payloads.Block() { Index = 0 }); - var testengine = new TestEngine(TriggerType.System, null, snapshot); - testengine.LoadScript(script); - Assert.AreEqual(VMState.HALT, testengine.Execute()); + snapshot.SetPersistingBlock(new Network.P2P.Payloads.Block() { Index = 0 }); + snapshot.DeployNativeContracts(); } [TestMethod] public void TestHashes() { // var attr = typeof(Oracle).GetCustomAttribute(); - Assert.AreEqual(NativeContract.Designate.Hash.ToString(), "0x7062149f9377e3a110a343f811b9e406f8ef7824"); - Assert.AreEqual(NativeContract.Oracle.Hash.ToString(), "0x35e4fc2e69a4d04d1db4d755c4150c50aff2e9a9"); - Assert.AreEqual(NativeContract.NEO.Hash.ToString(), "0xe22f9134cef8b03e53f71b3f960a20a65cddc972"); - Assert.AreEqual(NativeContract.GAS.Hash.ToString(), "0x36a019d836d964c438c573f78badf79b9e7eebdd"); - Assert.AreEqual(NativeContract.Policy.Hash.ToString(), "0x1ca594b36b6b6b3f05efce8b106c824053d18713"); + Assert.AreEqual(NativeContract.Designate.Hash.ToString(), "0x7ab39c37afd995f2f947a7ecbf40e91307058595"); + Assert.AreEqual(NativeContract.Oracle.Hash.ToString(), "0x479f134275b36d64f31fdbdc557cd2aa2f32d8d1"); + Assert.AreEqual(NativeContract.NEO.Hash.ToString(), "0x74c21a1ca66b7a190bf2a65db83ba6fe550cea64"); + Assert.AreEqual(NativeContract.GAS.Hash.ToString(), "0xb399c051778cf37a1e4ef88509b2e054d0420a32"); + Assert.AreEqual(NativeContract.Policy.Hash.ToString(), "0xe8ff1989c19526f4d8102f226e2c6c993b63efe9"); } [TestMethod] diff --git a/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestEngine.cs b/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestEngine.cs index 001464b24..7ab009365 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestEngine.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestEngine.cs @@ -216,11 +216,8 @@ protected override void OnSysCall(uint method) { if (callmethod == null) { - callmethod = new Dictionary() - { - { Native_Deploy.Hash , Native_Deploy } - }; - foreach (var m in ApplicationEngine.Services) + callmethod = new Dictionary(); + foreach (var m in Services) { callmethod[m.Key] = m.Value; } diff --git a/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestSnapshot.cs b/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestSnapshot.cs index 56395444b..c9319508d 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestSnapshot.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestSnapshot.cs @@ -36,11 +36,5 @@ public void SetPersistingBlock(Block block) { this.GetType().GetProperty("PersistingBlock").SetValue(this, block); } - - public void ContractAdd(ContractState contract) - { - var key = new KeyBuilder(0, 8).Add(contract.Hash); - Storages.Add(key, new StorageItem(contract)); - } } } diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/AccountTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/AccountTest.cs index c9a0b6541..2cf5448f7 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/AccountTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/AccountTest.cs @@ -1,4 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Compiler.MSIL.Extensions; using Neo.Compiler.MSIL.UnitTests.Utils; using Neo.VM.Types; @@ -25,7 +26,7 @@ public void Test_AccountIsStandard() 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF }; - ((TestSnapshot)_engine.Snapshot).ContractAdd(new ContractState() + _engine.Snapshot.ContractAdd(new ContractState() { Hash = new UInt160(noStandard), Script = new byte[0] { } diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/BlockchainTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/BlockchainTest.cs index 3b67c7a26..e1768dade 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/BlockchainTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/BlockchainTest.cs @@ -1,4 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Compiler.MSIL.Extensions; using Neo.Compiler.MSIL.UnitTests.Utils; using Neo.IO; using Neo.Ledger; @@ -345,7 +346,7 @@ public void GetContract() }, } }; - ((TestSnapshot)_engine.Snapshot).ContractAdd(contract); + _engine.Snapshot.ContractAdd(contract); // Not found diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/NativeTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/NativeTest.cs index 640c36305..80bb9e34a 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/NativeTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/NativeTest.cs @@ -1,4 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Compiler.MSIL.Extensions; using Neo.Compiler.MSIL.UnitTests.Utils; using Neo.Cryptography.ECC; using Neo.VM; @@ -34,13 +35,7 @@ public void Init() _engine = new TestEngine(TriggerType.Application, block); ((TestSnapshot)_engine.Snapshot).SetPersistingBlock(block); - - using (var script = new ScriptBuilder()) - { - script.EmitSysCall(TestEngine.Native_Deploy); - _engine.LoadScript(script.ToArray()); - Assert.AreEqual(VMState.HALT, _engine.Execute()); - } + _engine.Snapshot.DeployNativeContracts(); _engine.Reset(); _engine.AddEntryScript("./TestClasses/Contract_Native.cs"); diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/RuntimeTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/RuntimeTest.cs index 2f78a0651..d325b90ea 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/RuntimeTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/RuntimeTest.cs @@ -1,4 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Compiler.MSIL.Extensions; using Neo.Compiler.MSIL.UnitTests.Utils; using Neo.IO; using Neo.IO.Json; @@ -54,7 +55,7 @@ public void Test_InvocationCounter() var contract = _engine.EntryScriptHash; var engine = new TestEngine(TriggerType.Application, new DummyVerificable()); - ((TestSnapshot)engine.Snapshot).ContractAdd(new ContractState() + engine.Snapshot.ContractAdd(new ContractState() { Hash = contract, Script = _engine.InvocationStack.Peek().Script, diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/StaticStorageMapTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/StaticStorageMapTest.cs index 61cce0c7c..64d039d64 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/StaticStorageMapTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/StaticStorageMapTest.cs @@ -1,6 +1,7 @@ extern alias scfx; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Compiler.MSIL.Extensions; using Neo.Compiler.MSIL.UnitTests.Utils; using Neo.Ledger; using Neo.VM; diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/StorageTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/StorageTest.cs index df3509f52..54f987a0b 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/StorageTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/StorageTest.cs @@ -1,6 +1,7 @@ extern alias scfx; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Compiler.MSIL.Extensions; using Neo.Compiler.MSIL.UnitTests.Utils; using Neo.Ledger; using Neo.VM.Types; @@ -65,7 +66,7 @@ public void Init() testengine = new TestEngine(snapshot: snapshot.Clone()); testengine.AddEntryScript("./TestClasses/Contract_Storage.cs"); - ((TestSnapshot)testengine.Snapshot).ContractAdd(new ContractState() + testengine.Snapshot.ContractAdd(new ContractState() { Hash = testengine.EntryScriptHash, Script = testengine.EntryContext.Script, diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/System/BinaryTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/System/BinaryTest.cs index 3c9b4bc0e..4cf36bcbd 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/System/BinaryTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/System/BinaryTest.cs @@ -1,4 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Compiler.MSIL.Extensions; using Neo.Compiler.MSIL.UnitTests.Utils; using Neo.Ledger; using Neo.SmartContract; diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/System/CallbackTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/System/CallbackTest.cs index 4ef4067e4..ae8b88612 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/System/CallbackTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/System/CallbackTest.cs @@ -1,6 +1,7 @@ extern alias scfx; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Compiler.MSIL.Extensions; using Neo.Compiler.MSIL.UnitTests.Utils; using Neo.IO; using Neo.Ledger; @@ -24,7 +25,7 @@ public class CallbackTest public void Init() { var _ = TestBlockchain.TheNeoSystem; - var snapshot = (TestSnapshot)Blockchain.Singleton.GetSnapshot().Clone(); + var snapshot = Blockchain.Singleton.GetSnapshot().Clone(); snapshot.BlockHashIndex.Get().Index = 1234; _engine = new TestEngine(TriggerType.Application, snapshot: snapshot); diff --git a/tests/Neo.SmartContract.Framework.UnitTests/SyscallTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/SyscallTest.cs index 3e7659ec9..2551fa457 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/SyscallTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/SyscallTest.cs @@ -36,7 +36,6 @@ public void TestAllSyscalls() foreach (var syscall in ApplicationEngine.Services) { - if (syscall.Value.Name == "Neo.Native.Deploy") continue; if (syscall.Value.Name == "Neo.Native.Tokens.NEO") continue; if (syscall.Value.Name == "Neo.Native.Tokens.GAS") continue; if (syscall.Value.Name == "Neo.Native.Policy") continue; diff --git a/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs b/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs index 0d879ef7a..d919ad0b0 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_CreateAndUpdate.cs @@ -6,7 +6,6 @@ public class Contract_CreateAndUpdate : SmartContract.Framework.SmartContract { public static int OldContract(byte[] nefFile, string manifest) { - Contract.Update(script, manifest); ManagementContract.Update(nefFile, manifest); return 123; } diff --git a/tests/Template.NEP17.UnitTests/UnitTest_NEP17.cs b/tests/Template.NEP17.UnitTests/UnitTest_NEP17.cs index 823ee04d5..b188187b2 100644 --- a/tests/Template.NEP17.UnitTests/UnitTest_NEP17.cs +++ b/tests/Template.NEP17.UnitTests/UnitTest_NEP17.cs @@ -1,4 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Compiler.MSIL.Extensions; using Neo.Compiler.MSIL.UnitTests.Utils; using Neo.IO.Json; using Neo.SmartContract; @@ -73,7 +74,7 @@ public void Test_totalSupply() { var engine = CreateEngine(); var hash = engine.CurrentScriptHash; - var snapshot = (TestSnapshot)engine.Snapshot; + var snapshot = engine.Snapshot; snapshot.ContractAdd(new ContractState() { @@ -105,7 +106,7 @@ public void Test_totalSupply_empty() { var engine = CreateEngine(); var hash = engine.CurrentScriptHash; - var snapshot = (TestSnapshot)engine.Snapshot; + var snapshot = engine.Snapshot; snapshot.ContractAdd(new ContractState() { @@ -126,7 +127,7 @@ public void Test_balanceOf() { var engine = CreateEngine(); var hash = engine.CurrentScriptHash; - var snapshot = (TestSnapshot)engine.Snapshot; + var snapshot = engine.Snapshot; var address = new byte[] { 0xf6, 0x64, 0x43, 0x49, 0x8d, 0x38, 0x78, 0xd3, 0x2b, 0x99, 0x4e, 0x4e, 0x12, 0x83, 0xc6, 0x93, 0x44, 0x21, 0xda, 0xfe }; snapshot.ContractAdd(new ContractState() @@ -159,7 +160,7 @@ public void Test_balanceOf_empty() { var engine = CreateEngine(); var hash = engine.CurrentScriptHash; - var snapshot = (TestSnapshot)engine.Snapshot; + var snapshot = engine.Snapshot; var address = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 }; snapshot.ContractAdd(new ContractState() From 1e02f34db7966f957e923aae78644682c4205598 Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 10 Dec 2020 11:26:06 +0100 Subject: [PATCH 19/29] Fix UT --- .../Services/Neo/StaticStorageMapTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/StaticStorageMapTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/StaticStorageMapTest.cs index 64d039d64..c609fb19f 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/StaticStorageMapTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/StaticStorageMapTest.cs @@ -17,7 +17,7 @@ public class StaticStorageMapTest public void Init() { var _ = TestBlockchain.TheNeoSystem; - var snapshot = (TestSnapshot)Blockchain.Singleton.GetSnapshot().Clone(); + var snapshot = Blockchain.Singleton.GetSnapshot().Clone(); _engine = new TestEngine(snapshot: snapshot); _engine.AddEntryScript("./TestClasses/Contract_StaticStorageMap.cs"); From 0298a2e4a6445b6cdf38a6a662abc0f0b625793e Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 10 Dec 2020 11:31:52 +0100 Subject: [PATCH 20/29] More fixes --- tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NULL.cs | 2 +- .../Services/System/BinaryTest.cs | 2 +- .../TestClasses/Contract_Blockchain.cs | 2 +- .../TestClasses/Contract_Contract.cs | 6 +++--- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NULL.cs b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NULL.cs index 50b57a337..35d8eca69 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NULL.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NULL.cs @@ -127,7 +127,7 @@ public void NullCollationAndCollation2() { var _testengine = new TestEngine(); _testengine.AddEntryScript("./TestClasses/Contract_NULL.cs"); - ((TestSnapshot)_testengine.Snapshot).ContractAdd(new ContractState() + _testengine.Snapshot.ContractAdd(new ContractState() { Hash = testengine.EntryScriptHash, Script = testengine.EntryContext.Script, diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/System/BinaryTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/System/BinaryTest.cs index 4cf36bcbd..7e1f084aa 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/System/BinaryTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/System/BinaryTest.cs @@ -19,7 +19,7 @@ public class BinaryTest public void Init() { var _ = TestBlockchain.TheNeoSystem; - var snapshot = (TestSnapshot)Blockchain.Singleton.GetSnapshot().Clone(); + var snapshot = Blockchain.Singleton.GetSnapshot().Clone(); snapshot.BlockHashIndex.Get().Index = 1234; _engine = new TestEngine(TriggerType.Application, snapshot: snapshot); diff --git a/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_Blockchain.cs b/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_Blockchain.cs index bafb8c867..c5f7946de 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_Blockchain.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_Blockchain.cs @@ -88,7 +88,7 @@ private static object GetTxInfo(Transaction tx, string whatReturn) public static object GetContract(UInt160 hash, string whatReturn) { - var contract = Blockchain.GetContract(hash); + var contract = ManagementContract.GetContract(hash); return GetContractInfo(contract, whatReturn); } diff --git a/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_Contract.cs b/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_Contract.cs index 7339b8549..9606c4328 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_Contract.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/TestClasses/Contract_Contract.cs @@ -11,17 +11,17 @@ public static object Call(UInt160 scriptHash, string method, object[] arguments) public static object Create(byte[] nef, string manifest) { - return Contract.Create(nef, manifest); + return ManagementContract.Deploy(nef, manifest); } public static void Update(byte[] nef, string manifest) { - Contract.Update(nef, manifest); + ManagementContract.Update(nef, manifest); } public static void Destroy() { - Contract.Destroy(); + ManagementContract.Destroy(); } public static int GetCallFlags() From 06c3b303bd93ac00a0bfcdd92815fafc65df89fe Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 10 Dec 2020 17:15:43 +0100 Subject: [PATCH 21/29] More fixes --- src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj | 2 +- .../Services/System/SyscallCallback.cs | 1 - tests/Neo.SmartContract.Framework.UnitTests/SyscallTest.cs | 7 +++---- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj b/src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj index c15ea6b68..405a0fa89 100644 --- a/src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj +++ b/src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj @@ -43,7 +43,7 @@ - + diff --git a/src/Neo.SmartContract.Framework/Services/System/SyscallCallback.cs b/src/Neo.SmartContract.Framework/Services/System/SyscallCallback.cs index c3d0508ab..de0c36d3f 100644 --- a/src/Neo.SmartContract.Framework/Services/System/SyscallCallback.cs +++ b/src/Neo.SmartContract.Framework/Services/System/SyscallCallback.cs @@ -13,7 +13,6 @@ public enum SyscallCallback : uint System_Blockchain_GetTransaction = 0x488d55e6, System_Blockchain_GetTransactionHeight = 0xb188324a, System_Blockchain_GetTransactionFromBlock = 0x69fd567e, - System_Blockchain_GetContract = 0x414bc5a9, System_Contract_IsStandard = 0x859d6bd7, System_Contract_CreateStandardAccount = 0x28799cf, Neo_Crypto_RIPEMD160 = 0xd2d6d126, diff --git a/tests/Neo.SmartContract.Framework.UnitTests/SyscallTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/SyscallTest.cs index 2551fa457..bf1a1e328 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/SyscallTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/SyscallTest.cs @@ -36,10 +36,9 @@ public void TestAllSyscalls() foreach (var syscall in ApplicationEngine.Services) { - if (syscall.Value.Name == "Neo.Native.Tokens.NEO") continue; - if (syscall.Value.Name == "Neo.Native.Tokens.GAS") continue; - if (syscall.Value.Name == "Neo.Native.Policy") continue; - if (syscall.Value.Name == "Neo.Native.Call") continue; + if (syscall.Value.Name == "System.Contract.NativeOnPersist") continue; + if (syscall.Value.Name == "System.Contract.NativePostPersist") continue; + if (syscall.Value.Name == "System.Contract.CallNative") continue; if (syscall.Value.Name == "System.Runtime.Notify") continue; if (list.Remove(syscall.Value.Name)) continue; From a7ce9fec376b926dd2b4158a481ab3bd032f8c2b Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 10 Dec 2020 17:39:53 +0100 Subject: [PATCH 22/29] More UT --- .../Extensions/TestExtensions.cs | 10 +++++ .../Utils/TestSnapshot.cs | 11 ------ .../Services/Neo/BlockchainTest.cs | 37 +++++++++++++++++-- .../Services/Neo/ContractTest.cs | 8 +++- 4 files changed, 50 insertions(+), 16 deletions(-) diff --git a/tests/Neo.Compiler.MSIL.UnitTests/Extensions/TestExtensions.cs b/tests/Neo.Compiler.MSIL.UnitTests/Extensions/TestExtensions.cs index 46e81508d..fc816ae6f 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/Extensions/TestExtensions.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/Extensions/TestExtensions.cs @@ -1,5 +1,6 @@ using Neo.Compiler.MSIL.UnitTests.Utils; using Neo.Ledger; +using Neo.Network.P2P.Payloads; using Neo.Persistence; using Neo.SmartContract; @@ -19,5 +20,14 @@ public static void DeployNativeContracts(this StoreView snapshot) var engine = new TestEngine(TriggerType.OnPersist, null, snapshot); method.Invoke(SmartContract.Native.NativeContract.Management, new object[] { engine }); } + + /// + /// Set Persisting block for unit test + /// + /// Block + public static void SetPersistingBlock(this StoreView snapshot, Block block) + { + snapshot.GetType().GetProperty("PersistingBlock").SetValue(snapshot, block); + } } } diff --git a/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestSnapshot.cs b/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestSnapshot.cs index c9319508d..350521a7b 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestSnapshot.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/Utils/TestSnapshot.cs @@ -1,9 +1,7 @@ using Neo.IO; using Neo.IO.Caching; using Neo.Ledger; -using Neo.Network.P2P.Payloads; using Neo.Persistence; -using Neo.SmartContract; namespace Neo.Compiler.MSIL.UnitTests.Utils { @@ -27,14 +25,5 @@ public class TestSnapshot : StoreView public override MetaDataCache BlockHashIndex => _BlockHashIndex; public override MetaDataCache HeaderHashIndex => _HeaderHashIndex; - - /// - /// Set Persisting block for unit test - /// - /// Block - public void SetPersistingBlock(Block block) - { - this.GetType().GetProperty("PersistingBlock").SetValue(this, block); - } } } diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/BlockchainTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/BlockchainTest.cs index e1768dade..dffc109a7 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/BlockchainTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/BlockchainTest.cs @@ -20,10 +20,41 @@ public class BlockchainTest public void Init() { var _ = TestBlockchain.TheNeoSystem; - var snapshot = Blockchain.Singleton.GetSnapshot(); + var snapshot = Blockchain.Singleton.GetSnapshot().Clone(); - _block = Blockchain.GenesisBlock; - _engine = new TestEngine(snapshot: snapshot.Clone()); + _block = new Network.P2P.Payloads.Block() + { + ConsensusData = new Network.P2P.Payloads.ConsensusData(), + Index = 1, + PrevHash = Blockchain.GenesisBlock.Hash, + Witness = new Network.P2P.Payloads.Witness() { InvocationScript = new byte[0], VerificationScript = new byte[0] }, + NextConsensus = UInt160.Zero, + MerkleRoot = UInt256.Zero, + Transactions = new Network.P2P.Payloads.Transaction[] + { + new Network.P2P.Payloads.Transaction() + { + Attributes = new Network.P2P.Payloads.TransactionAttribute[0], + Signers = new Network.P2P.Payloads.Signer[]{ new Network.P2P.Payloads.Signer() { Account = UInt160.Zero } }, + Witnesses = new Network.P2P.Payloads.Witness[]{ }, + Script = new byte[0] + } + } + }; + + snapshot.SetPersistingBlock(_block); + snapshot.BlockHashIndex.GetAndChange().Index = _block.Index; + snapshot.BlockHashIndex.GetAndChange().Hash = _block.Hash; + snapshot.Blocks.Add(_block.Hash, _block.Trim()); + snapshot.Transactions.Add(_block.Transactions[0].Hash, new TransactionState() + { + BlockIndex = _block.Index, + Transaction = _block.Transactions[0], + VMState = VMState.HALT + }); + + + _engine = new TestEngine(snapshot: snapshot); _engine.AddEntryScript("./TestClasses/Contract_Blockchain.cs"); } diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/ContractTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/ContractTest.cs index 46e54b0cc..797082c38 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/ContractTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/ContractTest.cs @@ -1,7 +1,9 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using Neo.Compiler.MSIL.Extensions; using Neo.Compiler.MSIL.UnitTests.Utils; using Neo.IO; using Neo.IO.Json; +using Neo.Ledger; using Neo.Network.P2P.Payloads; using Neo.SmartContract.Manifest; using Neo.VM; @@ -23,6 +25,8 @@ public void Init() Signers = new Signer[] { new Signer() { Account = UInt160.Parse("0xa400ff00ff00ff00ff00ff00ff00ff00ff00ff01") } } }); _engine.AddEntryScript("./TestClasses/Contract_Contract.cs"); + _engine.Snapshot.SetPersistingBlock(Blockchain.GenesisBlock); + _engine.Snapshot.DeployNativeContracts(); } [TestMethod] @@ -46,7 +50,7 @@ public void Test_CreateCallDestroy() var item = result.Pop(); Assert.IsInstanceOfType(item, typeof(Array)); var itemArray = item as Array; - Assert.AreEqual(0, itemArray[0].GetInteger()); // Id + Assert.AreEqual(1, itemArray[0].GetInteger()); // Id Assert.AreEqual(0, itemArray[1].GetInteger()); // UpdateCounter Assert.AreEqual(hash.ToArray(), itemArray[2]); // Hash Assert.AreEqual(script.finalNEFScript, itemArray[3]); // Script @@ -102,7 +106,7 @@ public void Test_Update() var item = result.Pop(); Assert.IsInstanceOfType(item, typeof(Array)); var itemArray = item as Array; - Assert.AreEqual(0, itemArray[0].GetInteger()); // Id + Assert.AreEqual(1, itemArray[0].GetInteger()); // Id Assert.AreEqual(0, itemArray[1].GetInteger()); // UpdateCounter Assert.AreEqual(hash.ToArray(), itemArray[2]); // Hash Assert.AreEqual(script.finalNEFScript, itemArray[3]); // Script From 3493312f84d8fa85193d8e71269d5dd270e278ef Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 10 Dec 2020 17:46:33 +0100 Subject: [PATCH 23/29] Fix UT's --- templates/Template.CSharp/Contract1.cs | 12 ++++++------ .../Services/Neo/BlockchainTest.cs | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/templates/Template.CSharp/Contract1.cs b/templates/Template.CSharp/Contract1.cs index 58290c406..b5996bcdd 100644 --- a/templates/Template.CSharp/Contract1.cs +++ b/templates/Template.CSharp/Contract1.cs @@ -6,10 +6,10 @@ namespace $safeprojectname$ { - [ManifestExtra("Author", "Neo")] - [ManifestExtra("Email", "dev@neo.org")] - [ManifestExtra("Description", "This is a contract example")] - public class Contract1 : SmartContract + [ManifestExtra("Author", "Neo")] + [ManifestExtra("Email", "dev@neo.org")] + [ManifestExtra("Description", "This is a contract example")] + public class Contract1 : SmartContract { //TODO: Replace it with your own address. static readonly UInt160 Owner = "NiNmXL8FjEUEs1nfX9uHFBNaenxDHJtmuB".ToScriptHash(); @@ -43,8 +43,8 @@ public static void Update(byte[] nefFile, string manifest) public static void Destroy() { - if (!IsOwner()) throw new Exception("No authorization."); + if (!IsOwner()) throw new Exception("No authorization."); ManagementContract.Destroy(); - } + } } } diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/BlockchainTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/BlockchainTest.cs index dffc109a7..71be296cc 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/BlockchainTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/BlockchainTest.cs @@ -5,8 +5,10 @@ using Neo.Ledger; using Neo.VM; using Neo.VM.Types; +using System.Collections.Generic; using System.Linq; using System.Numerics; +using System.Reflection; namespace Neo.SmartContract.Framework.UnitTests.Services.Neo { @@ -53,11 +55,27 @@ public void Init() VMState = VMState.HALT }); + // Fake header_index + + var header_index = (List)Blockchain.Singleton.GetType() + .GetField("header_index", BindingFlags.Instance | BindingFlags.NonPublic) + .GetValue(Blockchain.Singleton); + header_index.Add(_block.Hash); _engine = new TestEngine(snapshot: snapshot); _engine.AddEntryScript("./TestClasses/Contract_Blockchain.cs"); } + [TestCleanup] + public void Clean() + { + // Revert header_index + var header_index = (List)Blockchain.Singleton.GetType() + .GetField("header_index", BindingFlags.Instance | BindingFlags.NonPublic) + .GetValue(Blockchain.Singleton); + header_index.RemoveAt(1); + } + [TestMethod] public void Test_GetHeight() { From a754a237ef4b3e484ba965ccaa1d27d19033f72f Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 10 Dec 2020 17:55:02 +0100 Subject: [PATCH 24/29] dotnet-format --- .../Extensions/TestExtensions.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Neo.Compiler.MSIL.UnitTests/Extensions/TestExtensions.cs b/tests/Neo.Compiler.MSIL.UnitTests/Extensions/TestExtensions.cs index fc816ae6f..db13ce6a7 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/Extensions/TestExtensions.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/Extensions/TestExtensions.cs @@ -1,8 +1,8 @@ -using Neo.Compiler.MSIL.UnitTests.Utils; -using Neo.Ledger; +using Neo.Compiler.MSIL.UnitTests.Utils; +using Neo.Ledger; using Neo.Network.P2P.Payloads; using Neo.Persistence; -using Neo.SmartContract; +using Neo.SmartContract; namespace Neo.Compiler.MSIL.Extensions { From 3e09e8dcc70de459a00ae1dd662e6ad2198e0011 Mon Sep 17 00:00:00 2001 From: Shargon Date: Thu, 10 Dec 2020 17:57:40 +0100 Subject: [PATCH 25/29] Fix Last test --- .../Services/Neo/RuntimeTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/RuntimeTest.cs b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/RuntimeTest.cs index d325b90ea..7ac82008a 100644 --- a/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/RuntimeTest.cs +++ b/tests/Neo.SmartContract.Framework.UnitTests/Services/Neo/RuntimeTest.cs @@ -143,7 +143,7 @@ public void Test_GasLeft() var item = result.Pop(); Assert.IsInstanceOfType(item, typeof(Integer)); - Assert.AreEqual(TestEngine.TestGas - 2000, item.GetInteger()); + Assert.AreEqual(TestEngine.TestGas - 2400, item.GetInteger()); } [TestMethod] From 9439ccd7fadaecd91afb4cf0004d6d867345a3ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BF=97=E5=90=8C?= Date: Mon, 14 Dec 2020 16:23:10 +0800 Subject: [PATCH 26/29] Update --- src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj | 2 +- .../Services/Neo/Designation.cs | 2 +- .../Services/Neo/GAS.cs | 2 +- .../Services/Neo/ManagementContract.cs | 2 +- .../Services/Neo/NEO.cs | 2 +- .../Services/Neo/Oracle.cs | 2 +- .../Services/Neo/Policy.cs | 2 +- .../UnitTest_NativeContracts.cs | 14 +++++++------- 8 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj b/src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj index 405a0fa89..ca8eb9686 100644 --- a/src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj +++ b/src/Neo.Compiler.MSIL/Neo.Compiler.MSIL.csproj @@ -43,7 +43,7 @@ - + diff --git a/src/Neo.SmartContract.Framework/Services/Neo/Designation.cs b/src/Neo.SmartContract.Framework/Services/Neo/Designation.cs index 6559e7e3a..beeb95ceb 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/Designation.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/Designation.cs @@ -2,7 +2,7 @@ namespace Neo.SmartContract.Framework.Services.Neo { - [Contract("0x7ab39c37afd995f2f947a7ecbf40e91307058595")] + [Contract("0xc0073f4c7069bf38995780c9da065f9b3949ea7a")] public class Designation { public static extern UInt160 Hash { [ContractHash] get; } diff --git a/src/Neo.SmartContract.Framework/Services/Neo/GAS.cs b/src/Neo.SmartContract.Framework/Services/Neo/GAS.cs index b0e3b226d..44a32cfc0 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/GAS.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/GAS.cs @@ -4,7 +4,7 @@ namespace Neo.SmartContract.Framework.Services.Neo { - [Contract("0xb399c051778cf37a1e4ef88509b2e054d0420a32")] + [Contract("0xa6a6c15dcdc9b997dac448b6926522d22efeedfb")] public class GAS { public static extern UInt160 Hash { [ContractHash] get; } diff --git a/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs b/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs index 3731ad01f..27a627975 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs @@ -1,6 +1,6 @@ namespace Neo.SmartContract.Framework.Services.Neo { - [Contract("0xdf18cb2476964c241558ed1e2e8881dcd2d50bde")] + [Contract("0xcd97b70d82d69adfcd9165374109419fade8d6ab")] public class ManagementContract { public static extern UInt160 Hash { [ContractHash] get; } diff --git a/src/Neo.SmartContract.Framework/Services/Neo/NEO.cs b/src/Neo.SmartContract.Framework/Services/Neo/NEO.cs index 7d042c196..05b70d9af 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/NEO.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/NEO.cs @@ -5,7 +5,7 @@ namespace Neo.SmartContract.Framework.Services.Neo { - [Contract("0x74c21a1ca66b7a190bf2a65db83ba6fe550cea64")] + [Contract("0x0a46e2e37c9987f570b4af253fb77e7eef0f72b6")] public class NEO { public static extern UInt160 Hash { [ContractHash] get; } diff --git a/src/Neo.SmartContract.Framework/Services/Neo/Oracle.cs b/src/Neo.SmartContract.Framework/Services/Neo/Oracle.cs index de3bd6872..9dfeafb39 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/Oracle.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/Oracle.cs @@ -2,7 +2,7 @@ namespace Neo.SmartContract.Framework.Services.Neo { - [Contract("0x35e4fc2e69a4d04d1db4d755c4150c50aff2e9a9")] + [Contract("0xb1c37d5847c2ae36bdde31d0cc833a7ad9667f8f")] public class Oracle { public static extern UInt160 Hash { [ContractHash] get; } diff --git a/src/Neo.SmartContract.Framework/Services/Neo/Policy.cs b/src/Neo.SmartContract.Framework/Services/Neo/Policy.cs index 9982223c1..f60c3c0e9 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/Policy.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/Policy.cs @@ -4,7 +4,7 @@ namespace Neo.SmartContract.Framework.Services.Neo { - [Contract("0xe8ff1989c19526f4d8102f226e2c6c993b63efe9")] + [Contract("0xdde31084c0fdbebc7f5ed5f53a38905305ccee14")] public class Policy { public static extern UInt160 Hash { [ContractHash] get; } diff --git a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NativeContracts.cs b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NativeContracts.cs index 518384e75..137ca30a0 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NativeContracts.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_NativeContracts.cs @@ -24,11 +24,11 @@ public void Test_Init() public void TestHashes() { // var attr = typeof(Oracle).GetCustomAttribute(); - Assert.AreEqual(NativeContract.Designate.Hash.ToString(), "0x7ab39c37afd995f2f947a7ecbf40e91307058595"); - Assert.AreEqual(NativeContract.Oracle.Hash.ToString(), "0x479f134275b36d64f31fdbdc557cd2aa2f32d8d1"); - Assert.AreEqual(NativeContract.NEO.Hash.ToString(), "0x74c21a1ca66b7a190bf2a65db83ba6fe550cea64"); - Assert.AreEqual(NativeContract.GAS.Hash.ToString(), "0xb399c051778cf37a1e4ef88509b2e054d0420a32"); - Assert.AreEqual(NativeContract.Policy.Hash.ToString(), "0xe8ff1989c19526f4d8102f226e2c6c993b63efe9"); + Assert.AreEqual(NativeContract.Designation.Hash.ToString(), "0xc0073f4c7069bf38995780c9da065f9b3949ea7a"); + Assert.AreEqual(NativeContract.Oracle.Hash.ToString(), "0xb1c37d5847c2ae36bdde31d0cc833a7ad9667f8f"); + Assert.AreEqual(NativeContract.NEO.Hash.ToString(), "0x0a46e2e37c9987f570b4af253fb77e7eef0f72b6"); + Assert.AreEqual(NativeContract.GAS.Hash.ToString(), "0xa6a6c15dcdc9b997dac448b6926522d22efeedfb"); + Assert.AreEqual(NativeContract.Policy.Hash.ToString(), "0xdde31084c0fdbebc7f5ed5f53a38905305ccee14"); } [TestMethod] @@ -81,7 +81,7 @@ public void Test_NEO() Assert.AreEqual(1, result.Count); var entry = result.Pop(); - Assert.AreEqual("neo", entry.GetString()); + Assert.AreEqual("NEO", entry.GetString()); // NeoHash @@ -110,7 +110,7 @@ public void Test_GAS() var entry = result.Pop(); - Assert.AreEqual("gas", entry.GetString()); + Assert.AreEqual("GAS", entry.GetString()); } } } From c7d4ca82522891db35808b2393788fd166d4b22f Mon Sep 17 00:00:00 2001 From: Shargon Date: Mon, 14 Dec 2020 10:14:49 +0100 Subject: [PATCH 27/29] Remove private properties --- src/Neo.SmartContract.Framework/Services/Neo/GAS.cs | 2 -- .../Services/Neo/ManagementContract.cs | 2 -- src/Neo.SmartContract.Framework/Services/Neo/Oracle.cs | 2 -- src/Neo.SmartContract.Framework/Services/Neo/Policy.cs | 2 -- 4 files changed, 8 deletions(-) diff --git a/src/Neo.SmartContract.Framework/Services/Neo/GAS.cs b/src/Neo.SmartContract.Framework/Services/Neo/GAS.cs index 44a32cfc0..4b3c9cdd6 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/GAS.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/GAS.cs @@ -8,8 +8,6 @@ namespace Neo.SmartContract.Framework.Services.Neo public class GAS { public static extern UInt160 Hash { [ContractHash] get; } - public static extern int Id { get; } - public static extern uint ActiveBlockIndex { get; } public static extern string Symbol { get; } public static extern byte Decimals { get; } public static extern BigInteger TotalSupply(); diff --git a/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs b/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs index 27a627975..86899da07 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs @@ -5,8 +5,6 @@ public class ManagementContract { public static extern UInt160 Hash { [ContractHash] get; } public static extern string Name { get; } - public static extern int Id { get; } - public static extern uint ActiveBlockIndex { get; } public static extern Contract GetContract(UInt160 hash); public static extern Contract Deploy(byte[] nefFile, string manifest); public static extern void Update(byte[] nefFile, string manifest); diff --git a/src/Neo.SmartContract.Framework/Services/Neo/Oracle.cs b/src/Neo.SmartContract.Framework/Services/Neo/Oracle.cs index 9dfeafb39..ab170404f 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/Oracle.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/Oracle.cs @@ -7,8 +7,6 @@ public class Oracle { public static extern UInt160 Hash { [ContractHash] get; } public const uint MinimumResponseFee = 0_10000000; - public static extern int Id { get; } - public static extern uint ActiveBlockIndex { get; } public static extern void Request(string url, string filter, string callback, object userData, long gasForResponse); } } diff --git a/src/Neo.SmartContract.Framework/Services/Neo/Policy.cs b/src/Neo.SmartContract.Framework/Services/Neo/Policy.cs index f60c3c0e9..273c32c43 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/Policy.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/Policy.cs @@ -8,8 +8,6 @@ namespace Neo.SmartContract.Framework.Services.Neo public class Policy { public static extern UInt160 Hash { [ContractHash] get; } - public static extern int Id { get; } - public static extern uint ActiveBlockIndex { get; } public static extern uint GetMaxTransactionsPerBlock(); public static extern uint GetMaxBlockSize(); public static extern long GetMaxBlockSystemFee(); From b0770d267b386e43390b24102fd90cd3c70db544 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=BF=97=E5=90=8C?= Date: Tue, 15 Dec 2020 16:06:59 +0800 Subject: [PATCH 28/29] Fixed UT --- .../{Contract_ABIOSafe.cs => Contract_ABISafe.cs} | 0 tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ABI_Safe.cs | 8 ++++---- 2 files changed, 4 insertions(+), 4 deletions(-) rename tests/Neo.Compiler.MSIL.UnitTests/TestClasses/{Contract_ABIOSafe.cs => Contract_ABISafe.cs} (100%) diff --git a/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_ABIOSafe.cs b/tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_ABISafe.cs similarity index 100% rename from tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_ABIOSafe.cs rename to tests/Neo.Compiler.MSIL.UnitTests/TestClasses/Contract_ABISafe.cs diff --git a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ABI_Safe.cs b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ABI_Safe.cs index 0c58704d5..b95b934ea 100644 --- a/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ABI_Safe.cs +++ b/tests/Neo.Compiler.MSIL.UnitTests/UnitTest_ABI_Safe.cs @@ -10,13 +10,13 @@ public class UnitTest_ABI_Safe [TestMethod] public void UnitTest_TestSafe() { - var buildScript = NeonTestTool.BuildScript("./TestClasses/Contract_ABIOSafe.cs", true, false); + var buildScript = NeonTestTool.BuildScript("./TestClasses/Contract_ABISafe.cs", true, false); var abi = buildScript.finalABI; var methodsABI = abi["methods"] as JArray; - Assert.IsFalse(methodsABI[0]["safe"].AsBoolean()); - Assert.IsTrue(methodsABI[1]["safe"].AsBoolean()); - Assert.IsFalse(methodsABI[2]["safe"].AsBoolean()); + Assert.IsFalse(methodsABI[1]["safe"].AsBoolean()); + Assert.IsTrue(methodsABI[2]["safe"].AsBoolean()); + Assert.IsFalse(methodsABI[3]["safe"].AsBoolean()); } } } From aa997f3e5b49aaae216939c019ec8cf29f15d1cf Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Wed, 16 Dec 2020 13:21:45 +0800 Subject: [PATCH 29/29] disable warning CS0626 --- .../Services/Neo/ManagementContract.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs b/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs index 86899da07..fd5642103 100644 --- a/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs +++ b/src/Neo.SmartContract.Framework/Services/Neo/ManagementContract.cs @@ -1,3 +1,5 @@ +#pragma warning disable CS0626 + namespace Neo.SmartContract.Framework.Services.Neo { [Contract("0xcd97b70d82d69adfcd9165374109419fade8d6ab")]