From 9cc6dee6db738662003bdedcec218b9d76b47bdc Mon Sep 17 00:00:00 2001 From: Fernando Diaz Toledano Date: Thu, 21 Nov 2024 14:28:56 +0100 Subject: [PATCH 1/2] Test optimizer --- .../Contract_Types_BigInteger.cs | 4 ++++ .../TestingArtifacts/Contract_Types_BigInteger.cs | 15 +++++++++++++-- .../UnitTest_Types_BigInteger.cs | 7 +++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/tests/Neo.Compiler.CSharp.TestContracts/Contract_Types_BigInteger.cs b/tests/Neo.Compiler.CSharp.TestContracts/Contract_Types_BigInteger.cs index 8be380c86..3e74e2014 100644 --- a/tests/Neo.Compiler.CSharp.TestContracts/Contract_Types_BigInteger.cs +++ b/tests/Neo.Compiler.CSharp.TestContracts/Contract_Types_BigInteger.cs @@ -8,6 +8,10 @@ public class Contract_Types_BigInteger : SmartContract.Framework.SmartContract [Integer("100000000000000000000000000")] private static readonly BigInteger publicBigInteger = default!; + public static BigInteger SumOne() + { + return 1 + 1; + } public static BigInteger Attribute() { return publicBigInteger; } public static BigInteger Zero() { return BigInteger.Zero; } public static BigInteger One() { return BigInteger.One; } diff --git a/tests/Neo.Compiler.CSharp.UnitTests/TestingArtifacts/Contract_Types_BigInteger.cs b/tests/Neo.Compiler.CSharp.UnitTests/TestingArtifacts/Contract_Types_BigInteger.cs index 78b41f80c..ddd376446 100644 --- a/tests/Neo.Compiler.CSharp.UnitTests/TestingArtifacts/Contract_Types_BigInteger.cs +++ b/tests/Neo.Compiler.CSharp.UnitTests/TestingArtifacts/Contract_Types_BigInteger.cs @@ -10,12 +10,12 @@ public abstract class Contract_Types_BigInteger(Neo.SmartContract.Testing.SmartC { #region Compiled data - public static Neo.SmartContract.Manifest.ContractManifest Manifest => Neo.SmartContract.Manifest.ContractManifest.Parse(@"{""name"":""Contract_Types_BigInteger"",""groups"":[],""features"":{},""supportedstandards"":[],""abi"":{""methods"":[{""name"":""attribute"",""parameters"":[],""returntype"":""Integer"",""offset"":0,""safe"":false},{""name"":""zero"",""parameters"":[],""returntype"":""Integer"",""offset"":18,""safe"":false},{""name"":""one"",""parameters"":[],""returntype"":""Integer"",""offset"":20,""safe"":false},{""name"":""minusOne"",""parameters"":[],""returntype"":""Integer"",""offset"":22,""safe"":false},{""name"":""parse"",""parameters"":[{""name"":""value"",""type"":""String""}],""returntype"":""Integer"",""offset"":24,""safe"":false},{""name"":""convertFromChar"",""parameters"":[],""returntype"":""Integer"",""offset"":32,""safe"":false}],""events"":[]},""permissions"":[{""contract"":""0xacce6fd80d44e1796aa0c2c625e9e4e0ce39efc0"",""methods"":[""atoi""]}],""trusts"":[],""extra"":{""nef"":{""optimization"":""All""}}}"); + public static Neo.SmartContract.Manifest.ContractManifest Manifest => Neo.SmartContract.Manifest.ContractManifest.Parse(@"{""name"":""Contract_Types_BigInteger"",""groups"":[],""features"":{},""supportedstandards"":[],""abi"":{""methods"":[{""name"":""sumOne"",""parameters"":[],""returntype"":""Integer"",""offset"":0,""safe"":false},{""name"":""attribute"",""parameters"":[],""returntype"":""Integer"",""offset"":2,""safe"":false},{""name"":""zero"",""parameters"":[],""returntype"":""Integer"",""offset"":20,""safe"":false},{""name"":""one"",""parameters"":[],""returntype"":""Integer"",""offset"":22,""safe"":false},{""name"":""minusOne"",""parameters"":[],""returntype"":""Integer"",""offset"":24,""safe"":false},{""name"":""parse"",""parameters"":[{""name"":""value"",""type"":""String""}],""returntype"":""Integer"",""offset"":26,""safe"":false},{""name"":""convertFromChar"",""parameters"":[],""returntype"":""Integer"",""offset"":34,""safe"":false}],""events"":[]},""permissions"":[{""contract"":""0xacce6fd80d44e1796aa0c2c625e9e4e0ce39efc0"",""methods"":[""atoi""]}],""trusts"":[],""extra"":{""nef"":{""optimization"":""All""}}}"); /// /// Optimization: "All" /// - public static Neo.SmartContract.NefFile Nef => Neo.IO.Helper.AsSerializable(Convert.FromBase64String(@"TkVGM1Rlc3RpbmdFbmdpbmUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHA7znO4OTpJcbCoGp54UQN2G/OrARhdG9pAQABDwAAIwQAAADk0gzI3NK3UgAAAAAAQBBAEUAPQFcAAXg3AABAAEFASEeaMA==")); + public static Neo.SmartContract.NefFile Nef => Neo.IO.Helper.AsSerializable(Convert.FromBase64String(@"TkVGM1Rlc3RpbmdFbmdpbmUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHA7znO4OTpJcbCoGp54UQN2G/OrARhdG9pAQABDwAAJRJABAAAAOTSDMjc0rdSAAAAAABAEEARQA9AVwABeDcAAEAAQUBkJDKz")); #endregion @@ -78,6 +78,17 @@ public abstract class Contract_Types_BigInteger(Neo.SmartContract.Testing.SmartC [DisplayName("parse")] public abstract BigInteger? Parse(string? value); + /// + /// Unsafe method + /// + /// + /// Script: EkA= + /// 00 : PUSH2 [1 datoshi] + /// 01 : RET [0 datoshi] + /// + [DisplayName("sumOne")] + public abstract BigInteger? SumOne(); + /// /// Unsafe method /// diff --git a/tests/Neo.Compiler.CSharp.UnitTests/UnitTest_Types_BigInteger.cs b/tests/Neo.Compiler.CSharp.UnitTests/UnitTest_Types_BigInteger.cs index d6bf39094..82eb51187 100644 --- a/tests/Neo.Compiler.CSharp.UnitTests/UnitTest_Types_BigInteger.cs +++ b/tests/Neo.Compiler.CSharp.UnitTests/UnitTest_Types_BigInteger.cs @@ -7,6 +7,13 @@ namespace Neo.Compiler.CSharp.UnitTests [TestClass] public class UnitTest_Types_BigInteger : DebugAndTestBase { + [TestMethod] + public void BigInteger_SumOne() + { + Assert.AreEqual(2, Contract.SumOne()); + AssertGasConsumed(984060); + } + [TestMethod] public void BigInteger_Test() { From 895af43a81eee4ab238c60aed1192c3560d03e1c Mon Sep 17 00:00:00 2001 From: Fernando Diaz Toledano Date: Thu, 21 Nov 2024 14:32:04 +0100 Subject: [PATCH 2/2] Add option --- .../Contract_Types_BigInteger.cs | 5 +++ .../Contract_Types_BigInteger.cs | 34 +++++++++++++++++-- .../UnitTest_Types_BigInteger.cs | 3 ++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/tests/Neo.Compiler.CSharp.TestContracts/Contract_Types_BigInteger.cs b/tests/Neo.Compiler.CSharp.TestContracts/Contract_Types_BigInteger.cs index 3e74e2014..c5cddcfb2 100644 --- a/tests/Neo.Compiler.CSharp.TestContracts/Contract_Types_BigInteger.cs +++ b/tests/Neo.Compiler.CSharp.TestContracts/Contract_Types_BigInteger.cs @@ -12,6 +12,11 @@ public static BigInteger SumOne() { return 1 + 1; } + public static BigInteger SumOverflow() + { + var i = int.MaxValue; + return i + 1; + } public static BigInteger Attribute() { return publicBigInteger; } public static BigInteger Zero() { return BigInteger.Zero; } public static BigInteger One() { return BigInteger.One; } diff --git a/tests/Neo.Compiler.CSharp.UnitTests/TestingArtifacts/Contract_Types_BigInteger.cs b/tests/Neo.Compiler.CSharp.UnitTests/TestingArtifacts/Contract_Types_BigInteger.cs index ddd376446..8bf00e743 100644 --- a/tests/Neo.Compiler.CSharp.UnitTests/TestingArtifacts/Contract_Types_BigInteger.cs +++ b/tests/Neo.Compiler.CSharp.UnitTests/TestingArtifacts/Contract_Types_BigInteger.cs @@ -10,12 +10,12 @@ public abstract class Contract_Types_BigInteger(Neo.SmartContract.Testing.SmartC { #region Compiled data - public static Neo.SmartContract.Manifest.ContractManifest Manifest => Neo.SmartContract.Manifest.ContractManifest.Parse(@"{""name"":""Contract_Types_BigInteger"",""groups"":[],""features"":{},""supportedstandards"":[],""abi"":{""methods"":[{""name"":""sumOne"",""parameters"":[],""returntype"":""Integer"",""offset"":0,""safe"":false},{""name"":""attribute"",""parameters"":[],""returntype"":""Integer"",""offset"":2,""safe"":false},{""name"":""zero"",""parameters"":[],""returntype"":""Integer"",""offset"":20,""safe"":false},{""name"":""one"",""parameters"":[],""returntype"":""Integer"",""offset"":22,""safe"":false},{""name"":""minusOne"",""parameters"":[],""returntype"":""Integer"",""offset"":24,""safe"":false},{""name"":""parse"",""parameters"":[{""name"":""value"",""type"":""String""}],""returntype"":""Integer"",""offset"":26,""safe"":false},{""name"":""convertFromChar"",""parameters"":[],""returntype"":""Integer"",""offset"":34,""safe"":false}],""events"":[]},""permissions"":[{""contract"":""0xacce6fd80d44e1796aa0c2c625e9e4e0ce39efc0"",""methods"":[""atoi""]}],""trusts"":[],""extra"":{""nef"":{""optimization"":""All""}}}"); + public static Neo.SmartContract.Manifest.ContractManifest Manifest => Neo.SmartContract.Manifest.ContractManifest.Parse(@"{""name"":""Contract_Types_BigInteger"",""groups"":[],""features"":{},""supportedstandards"":[],""abi"":{""methods"":[{""name"":""sumOne"",""parameters"":[],""returntype"":""Integer"",""offset"":0,""safe"":false},{""name"":""sumOverflow"",""parameters"":[],""returntype"":""Integer"",""offset"":2,""safe"":false},{""name"":""attribute"",""parameters"":[],""returntype"":""Integer"",""offset"":61,""safe"":false},{""name"":""zero"",""parameters"":[],""returntype"":""Integer"",""offset"":79,""safe"":false},{""name"":""one"",""parameters"":[],""returntype"":""Integer"",""offset"":81,""safe"":false},{""name"":""minusOne"",""parameters"":[],""returntype"":""Integer"",""offset"":83,""safe"":false},{""name"":""parse"",""parameters"":[{""name"":""value"",""type"":""String""}],""returntype"":""Integer"",""offset"":85,""safe"":false},{""name"":""convertFromChar"",""parameters"":[],""returntype"":""Integer"",""offset"":93,""safe"":false}],""events"":[]},""permissions"":[{""contract"":""0xacce6fd80d44e1796aa0c2c625e9e4e0ce39efc0"",""methods"":[""atoi""]}],""trusts"":[],""extra"":{""nef"":{""optimization"":""All""}}}"); /// /// Optimization: "All" /// - public static Neo.SmartContract.NefFile Nef => Neo.IO.Helper.AsSerializable(Convert.FromBase64String(@"TkVGM1Rlc3RpbmdFbmdpbmUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHA7znO4OTpJcbCoGp54UQN2G/OrARhdG9pAQABDwAAJRJABAAAAOTSDMjc0rdSAAAAAABAEEARQA9AVwABeDcAAEAAQUBkJDKz")); + public static Neo.SmartContract.NefFile Nef => Neo.IO.Helper.AsSerializable(Convert.FromBase64String(@"TkVGM1Rlc3RpbmdFbmdpbmUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHA7znO4OTpJcbCoGp54UQN2G/OrARhdG9pAQABDwAAYBJAVwEAAv///39waBGeSgIAAACALgQiCkoC////fzIeA/////8AAAAAkUoC////fzIMAwAAAAABAAAAn0AEAAAA5NIMyNzSt1IAAAAAAEAQQBFAD0BXAAF4NwAAQABBQLKPnJE=")); #endregion @@ -89,6 +89,36 @@ public abstract class Contract_Types_BigInteger(Neo.SmartContract.Testing.SmartC [DisplayName("sumOne")] public abstract BigInteger? SumOne(); + /// + /// Unsafe method + /// + /// + /// Script: VwEAAv///39waBGeSgIAAACALgQiCkoC////fzIeA/////8AAAAAkUoC////fzIMAwAAAAABAAAAn0A= + /// 00 : INITSLOT 0100 [64 datoshi] + /// 03 : PUSHINT32 FFFFFF7F [1 datoshi] + /// 08 : STLOC0 [2 datoshi] + /// 09 : LDLOC0 [2 datoshi] + /// 0A : PUSH1 [1 datoshi] + /// 0B : ADD [8 datoshi] + /// 0C : DUP [2 datoshi] + /// 0D : PUSHINT32 00000080 [1 datoshi] + /// 12 : JMPGE 04 [2 datoshi] + /// 14 : JMP 0A [2 datoshi] + /// 16 : DUP [2 datoshi] + /// 17 : PUSHINT32 FFFFFF7F [1 datoshi] + /// 1C : JMPLE 1E [2 datoshi] + /// 1E : PUSHINT64 FFFFFFFF00000000 [1 datoshi] + /// 27 : AND [8 datoshi] + /// 28 : DUP [2 datoshi] + /// 29 : PUSHINT32 FFFFFF7F [1 datoshi] + /// 2E : JMPLE 0C [2 datoshi] + /// 30 : PUSHINT64 0000000001000000 [1 datoshi] + /// 39 : SUB [8 datoshi] + /// 3A : RET [0 datoshi] + /// + [DisplayName("sumOverflow")] + public abstract BigInteger? SumOverflow(); + /// /// Unsafe method /// diff --git a/tests/Neo.Compiler.CSharp.UnitTests/UnitTest_Types_BigInteger.cs b/tests/Neo.Compiler.CSharp.UnitTests/UnitTest_Types_BigInteger.cs index 82eb51187..966354e6c 100644 --- a/tests/Neo.Compiler.CSharp.UnitTests/UnitTest_Types_BigInteger.cs +++ b/tests/Neo.Compiler.CSharp.UnitTests/UnitTest_Types_BigInteger.cs @@ -12,6 +12,9 @@ public void BigInteger_SumOne() { Assert.AreEqual(2, Contract.SumOne()); AssertGasConsumed(984060); + + Assert.AreEqual(-2147483648, Contract.SumOverflow()); + AssertGasConsumed(987360); } [TestMethod]