-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Compiler Add]allow direct assignment to UInt160, UInt256, and ECPoin…
…t. (#974)
- Loading branch information
Showing
12 changed files
with
252 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
tests/Neo.Compiler.CSharp.TestContracts/Contract_DirectInit.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using Neo.SmartContract.Framework; | ||
using Neo.SmartContract.Framework.Attributes; | ||
using System.Numerics; | ||
|
||
namespace Neo.Compiler.CSharp.UnitTests.TestClasses | ||
{ | ||
|
||
public class Contract_DirectInit : SmartContract.Framework.SmartContract | ||
{ | ||
|
||
/// <summary> | ||
/// A static field of type ECPoint initialized directly from a string. This is used to demonstrate initializing | ||
/// complex types like ECPoint at compile time to avoid runtime overhead. | ||
/// </summary> | ||
// [PublicKey("024700db2e90d9f02c4f9fc862abaca92725f95b4fddcc8d7ffa538693ecf463a9")] | ||
private static readonly ECPoint eCPoint = "024700db2e90d9f02c4f9fc862abaca92725f95b4fddcc8d7ffa538693ecf463a9"; | ||
|
||
/// <summary> | ||
/// A static field of type UInt160 initialized directly from a string. This allows for compile-time | ||
/// initialization of blockchain-specific types like addresses, represented here as Hash160. | ||
/// </summary> | ||
// [Hash160("NXV7ZhHiyM1aHXwpVsRZC6BwNFP2jghXAq")] | ||
private static readonly UInt160 uInt160 = "NXV7ZhHiyM1aHXwpVsRZC6BwNFP2jghXAq"; | ||
|
||
/// <summary> | ||
/// A static field of type UInt160 initialized directly from a hex string. This allows for compile-time | ||
/// initialization of blockchain-specific types like addresses, represented here as Hash256. | ||
/// </summary> | ||
// [ByteArray("edcf8679104ec2911a4fe29ad7db232a493e5b990fb1da7af0c7b989948c8925")] | ||
private static readonly UInt256 validUInt256 = "edcf8679104ec2911a4fe29ad7db232a493e5b990fb1da7af0c7b989948c8925"; | ||
|
||
/// <summary> | ||
/// A static string field initialized directly. | ||
/// This demonstrates initializing contract fields that cannot be directly assigned with their value at compile time. | ||
/// </summary> | ||
// [String("hello world")] | ||
public static readonly string a4 = "hello world"; | ||
|
||
public static UInt160 testGetUInt160() | ||
{ | ||
return uInt160; | ||
} | ||
|
||
public static ECPoint testGetECPoint() | ||
{ | ||
return eCPoint; | ||
} | ||
|
||
public static UInt256 testGetUInt256() | ||
{ | ||
return validUInt256; | ||
} | ||
|
||
public static string testGetString() | ||
{ | ||
return a4; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
tests/Neo.Compiler.CSharp.UnitTests/UnitTest_DirectInit.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neo.VM.Types; | ||
using System; | ||
using Neo.SmartContract.TestEngine; | ||
|
||
namespace Neo.Compiler.CSharp.UnitTests | ||
{ | ||
[TestClass] | ||
public class UnitTest_DirectInit | ||
{ | ||
|
||
[TestMethod] | ||
public void Test_GetUInt160() | ||
{ | ||
using var testengine = new TestEngine(snapshot: new TestDataCache()); | ||
testengine.AddEntryScript(Utils.Extensions.TestContractRoot + "Contract_DirectInit.cs"); | ||
var result = testengine.ExecuteTestCaseStandard("testGetUInt160"); | ||
var value = result.Pop().GetSpan(); | ||
|
||
Assert.AreEqual(value.ToArray().ToHexString(), "7eee1aabeb67ed1d791d44e4f5fcf3ae9171a871"); | ||
} | ||
|
||
[TestMethod] | ||
public void Test_GetECPoint() | ||
{ | ||
using var testengine = new TestEngine(snapshot: new TestDataCache()); | ||
testengine.AddEntryScript(Utils.Extensions.TestContractRoot + "Contract_DirectInit.cs"); | ||
var result = testengine.ExecuteTestCaseStandard("testGetECPoint"); | ||
var value = result.Pop().GetSpan(); | ||
Assert.AreEqual(value.ToHexString(), "024700db2e90d9f02c4f9fc862abaca92725f95b4fddcc8d7ffa538693ecf463a9"); | ||
} | ||
|
||
[TestMethod] | ||
public void Test_GetUInt256() | ||
{ | ||
using var testengine = new TestEngine(snapshot: new TestDataCache()); | ||
testengine.AddEntryScript(Utils.Extensions.TestContractRoot + "Contract_DirectInit.cs"); | ||
var result = testengine.ExecuteTestCaseStandard("testGetUInt256"); | ||
var value = result.Pop().GetSpan(); | ||
|
||
Assert.AreEqual(value.ToArray().ToHexString(), "edcf8679104ec2911a4fe29ad7db232a493e5b990fb1da7af0c7b989948c8925"); | ||
} | ||
|
||
[TestMethod] | ||
public void Test_GetString() | ||
{ | ||
using var testengine = new TestEngine(snapshot: new TestDataCache()); | ||
testengine.AddEntryScript(Utils.Extensions.TestContractRoot + "Contract_DirectInit.cs"); | ||
var result = testengine.ExecuteTestCaseStandard("testGetString"); | ||
var value = result.Pop().GetString(); | ||
|
||
Assert.AreEqual(value, "hello world"); | ||
} | ||
} | ||
} |
Oops, something went wrong.