Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ByteString #376

Merged
merged 3 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Neo.SmartContract.Framework/ByteString.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace Neo.SmartContract.Framework
{
public abstract class ByteString
{
public extern byte this[int index]
{
[OpCode(OpCode.PICKITEM)]
get;
}

public extern int Length
{
[OpCode(OpCode.SIZE)]
get;
}

[Script]
public static extern implicit operator string(ByteString str);

[Script]
public static extern implicit operator ByteString(string str);

[OpCode(OpCode.CONVERT, StackItemType.Buffer)]
public static extern explicit operator byte[](ByteString str);

[OpCode(OpCode.CONVERT, StackItemType.ByteString)]
public static extern explicit operator ByteString(byte[] buffer);
}
}
12 changes: 4 additions & 8 deletions src/Neo.SmartContract.Framework/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ namespace Neo.SmartContract.Framework
{
public static class Helper
{
internal const string StackItemType_Integer = "0x21";
internal const string StackItemType_ByteString = "0x28";
internal const string StackItemType_Buffer = "0x30";

/// <summary>
/// Converts byte to byte[] considering the byte as a BigInteger (0x00 at the end)
/// </summary>
Expand All @@ -19,7 +15,7 @@ public static class Helper
/// <summary>
/// Converts sbyte to byte[].
/// </summary>
[OpCode(OpCode.CONVERT, StackItemType_Buffer)]
[OpCode(OpCode.CONVERT, StackItemType.Buffer)]
public extern static byte[] ToByteArray(this sbyte source);

/// <summary>
Expand All @@ -33,7 +29,7 @@ public static class Helper
[OpCode(OpCode.PUSH0)]
[OpCode(OpCode.SWAP)]
[OpCode(OpCode.DROP)]
[OpCode(OpCode.CONVERT, StackItemType_Integer)]
[OpCode(OpCode.CONVERT, StackItemType.Integer)]
public extern static BigInteger ToBigInteger(this byte[] source);
//{
// if (value == null) return 0;
Expand All @@ -43,13 +39,13 @@ public static class Helper
/// <summary>
/// Converts string to byte[]. Examples: "hello" -> [0x68656c6c6f]; "" -> []; "Neo" -> [0x4e656f]
/// </summary>
[OpCode(OpCode.CONVERT, StackItemType_Buffer)]
[OpCode(OpCode.CONVERT, StackItemType.Buffer)]
public extern static byte[] ToByteArray(this string source);

/// <summary>
/// Converts byte[] to string. Examples: [0x68656c6c6f] -> "hello"; [] -> ""; [0x4e656f] -> "Neo"
/// </summary>
[OpCode(OpCode.CONVERT, StackItemType_ByteString)]
[OpCode(OpCode.CONVERT, StackItemType.ByteString)]
public extern static string ToByteString(this byte[] source);

/// <summary>
Expand Down
9 changes: 9 additions & 0 deletions src/Neo.SmartContract.Framework/StackItemType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Neo.SmartContract.Framework
{
internal static class StackItemType
{
public const string Integer = "0x21";
public const string ByteString = "0x28";
public const string Buffer = "0x30";
}
}
26 changes: 13 additions & 13 deletions tests/Neo.SmartContract.Framework.UnitTests/HelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public void TestHexToBytes()
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
Assert.AreEqual("0a0b0c0d0e0f", (item as ByteString).GetSpan().ToHexString());
Assert.IsInstanceOfType(item, typeof(VM.Types.ByteString));
Assert.AreEqual("0a0b0c0d0e0f", (item as VM.Types.ByteString).GetSpan().ToHexString());
}

[TestMethod]
Expand All @@ -46,7 +46,7 @@ public void TestToBigInteger()
Assert.AreEqual(0, item.GetInteger());

_engine.Reset();
result = _engine.ExecuteTestCaseStandard("testToBigInteger", new ByteString(new byte[0]));
result = _engine.ExecuteTestCaseStandard("testToBigInteger", new VM.Types.ByteString(new byte[0]));
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

Expand All @@ -57,7 +57,7 @@ public void TestToBigInteger()
// Value

_engine.Reset();
result = _engine.ExecuteTestCaseStandard("testToBigInteger", new ByteString(new byte[] { 123 }));
result = _engine.ExecuteTestCaseStandard("testToBigInteger", new VM.Types.ByteString(new byte[] { 123 }));
Assert.AreEqual(VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

Expand Down Expand Up @@ -102,7 +102,7 @@ public void Test_ByteToByteArray()
var result = _engine.GetMethod("testByteToByteArray").Run();

StackItem wantResult = new byte[] { 0x01 };
Assert.AreEqual(wantResult.ConvertTo(StackItemType.ByteString), result.ConvertTo(StackItemType.ByteString));
Assert.AreEqual(wantResult.ConvertTo(VM.Types.StackItemType.ByteString), result.ConvertTo(VM.Types.StackItemType.ByteString));
}

[TestMethod]
Expand All @@ -111,7 +111,7 @@ public void Test_Reverse()
var result = _engine.GetMethod("testReverse").Run();

StackItem wantResult = new byte[] { 0x03, 0x02, 0x01 };
Assert.AreEqual(wantResult.ConvertTo(StackItemType.ByteString), result.ConvertTo(StackItemType.ByteString));
Assert.AreEqual(wantResult.ConvertTo(VM.Types.StackItemType.ByteString), result.ConvertTo(VM.Types.StackItemType.ByteString));
}

[TestMethod]
Expand All @@ -120,7 +120,7 @@ public void Test_SbyteToByteArray()
var result = _engine.GetMethod("testSbyteToByteArray").Run();

StackItem wantResult = new byte[] { 255 };
Assert.AreEqual(wantResult.ConvertTo(StackItemType.ByteString), result.ConvertTo(StackItemType.ByteString));
Assert.AreEqual(wantResult.ConvertTo(VM.Types.StackItemType.ByteString), result.ConvertTo(VM.Types.StackItemType.ByteString));
}

[TestMethod]
Expand All @@ -129,7 +129,7 @@ public void Test_StringToByteArray()
var result = _engine.GetMethod("testStringToByteArray").Run();

StackItem wantResult = new byte[] { 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100 };
Assert.AreEqual(wantResult.ConvertTo(StackItemType.ByteString), result.ConvertTo(StackItemType.ByteString));
Assert.AreEqual(wantResult.ConvertTo(VM.Types.StackItemType.ByteString), result.ConvertTo(VM.Types.StackItemType.ByteString));
}

[TestMethod]
Expand All @@ -138,7 +138,7 @@ public void Test_Concat()
var result = _engine.GetMethod("testConcat").Run();

StackItem wantResult = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
Assert.AreEqual(wantResult.ConvertTo(StackItemType.ByteString), result.ConvertTo(StackItemType.ByteString));
Assert.AreEqual(wantResult.ConvertTo(VM.Types.StackItemType.ByteString), result.ConvertTo(VM.Types.StackItemType.ByteString));
}

[TestMethod]
Expand All @@ -147,7 +147,7 @@ public void Test_Range()
var result = _engine.GetMethod("testRange").Run();

StackItem wantResult = new byte[] { 0x02 };
Assert.AreEqual(wantResult.ConvertTo(StackItemType.ByteString), result.ConvertTo(StackItemType.ByteString));
Assert.AreEqual(wantResult.ConvertTo(VM.Types.StackItemType.ByteString), result.ConvertTo(VM.Types.StackItemType.ByteString));
}

[TestMethod]
Expand All @@ -156,7 +156,7 @@ public void Test_Take()
var result = _engine.GetMethod("testTake").Run();

StackItem wantResult = new byte[] { 0x01, 0x02 };
Assert.AreEqual(wantResult.ConvertTo(StackItemType.ByteString), result.ConvertTo(StackItemType.ByteString));
Assert.AreEqual(wantResult.ConvertTo(VM.Types.StackItemType.ByteString), result.ConvertTo(VM.Types.StackItemType.ByteString));
}

[TestMethod]
Expand All @@ -165,7 +165,7 @@ public void Test_Last()
var result = _engine.GetMethod("testLast").Run();

StackItem wantResult = new byte[] { 0x02, 0x03 };
Assert.AreEqual(wantResult.ConvertTo(StackItemType.ByteString), result.ConvertTo(StackItemType.ByteString));
Assert.AreEqual(wantResult.ConvertTo(VM.Types.StackItemType.ByteString), result.ConvertTo(VM.Types.StackItemType.ByteString));
}

[TestMethod]
Expand All @@ -174,7 +174,7 @@ public void Test_ToScriptHash()
var result = _engine.GetMethod("testToScriptHash").Run();

StackItem wantResult = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xaa, 0xbb, 0xcc, 0xdd, 0xee };
Assert.AreEqual(wantResult.ConvertTo(StackItemType.ByteString), result.ConvertTo(StackItemType.ByteString));
Assert.AreEqual(wantResult.ConvertTo(VM.Types.StackItemType.ByteString), result.ConvertTo(VM.Types.StackItemType.ByteString));
}
}
}
2 changes: 1 addition & 1 deletion tests/Neo.SmartContract.Framework.UnitTests/ListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void TestArrayConvert()

static JObject ParseJson(StackItem item)
{
Assert.IsInstanceOfType(item, typeof(ByteString));
Assert.IsInstanceOfType(item, typeof(VM.Types.ByteString));
var json = System.Text.Encoding.UTF8.GetString(item.GetSpan());
return JObject.Parse(json);
}
Expand Down
26 changes: 13 additions & 13 deletions tests/Neo.SmartContract.Framework.UnitTests/MapTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public void TestByteArray()
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
Assert.IsInstanceOfType(item, typeof(VM.Types.ByteString));
// Except: {"a":"teststring2"}
Assert.AreEqual("7b2261223a2274657374737472696e6732227d", (item as ByteString).GetSpan().ToHexString());
Assert.AreEqual("7b2261223a2274657374737472696e6732227d", (item as VM.Types.ByteString).GetSpan().ToHexString());
}

[TestMethod]
Expand All @@ -61,9 +61,9 @@ public void TestClear()
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
Assert.IsInstanceOfType(item, typeof(VM.Types.ByteString));
// Except: {}
Assert.AreEqual("7b7d", (item as ByteString).GetSpan().ToHexString());
Assert.AreEqual("7b7d", (item as VM.Types.ByteString).GetSpan().ToHexString());
}


Expand All @@ -76,9 +76,9 @@ public void TestByteArray2()
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
Assert.IsInstanceOfType(item, typeof(VM.Types.ByteString));
// Except: {"\u0001\u0001":"\u0022\u0022"}
Assert.AreEqual("7b225c75303030315c7530303031223a225c75303032325c7530303232227d", (item as ByteString).GetSpan().ToHexString());
Assert.AreEqual("7b225c75303030315c7530303031223a225c75303032325c7530303232227d", (item as VM.Types.ByteString).GetSpan().ToHexString());
}

[TestMethod]
Expand All @@ -91,9 +91,9 @@ public void TestUnicode()
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
Assert.IsInstanceOfType(item, typeof(VM.Types.ByteString));
// Except: {"\u4E2D":"129840test10022939"}
Assert.AreEqual("7b225c7534453244223a22313239383430746573743130303232393339227d", (item as ByteString).GetSpan().ToHexString());
Assert.AreEqual("7b225c7534453244223a22313239383430746573743130303232393339227d", (item as VM.Types.ByteString).GetSpan().ToHexString());
}

[TestMethod]
Expand All @@ -106,9 +106,9 @@ public void TestUnicodeValue()
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
Assert.IsInstanceOfType(item, typeof(VM.Types.ByteString));
// Except: {"ab":"\u6587"}
Assert.AreEqual("7b226162223a225c7536353837227d", (item as ByteString).GetSpan().ToHexString());
Assert.AreEqual("7b226162223a225c7536353837227d", (item as VM.Types.ByteString).GetSpan().ToHexString());
}

[TestMethod]
Expand All @@ -122,9 +122,9 @@ public void TestUnicodeKeyValue()
Assert.AreEqual(1, result.Count);

var item = result.Pop();
Assert.IsInstanceOfType(item, typeof(ByteString));
Assert.IsInstanceOfType(item, typeof(VM.Types.ByteString));
// Except: {"\u4E2D":"\u6587"}
Assert.AreEqual("7b225c7534453244223a225c7536353837227d", (item as ByteString).GetSpan().ToHexString());
Assert.AreEqual("7b225c7534453244223a225c7536353837227d", (item as VM.Types.ByteString).GetSpan().ToHexString());
}

[TestMethod]
Expand Down Expand Up @@ -160,7 +160,7 @@ public void TestDeserialize()
var map = item as Map;
Assert.AreEqual(1, map.Count);
Assert.IsTrue(map.ContainsKey("a"));
Assert.AreEqual((ByteString)"testdeserialize", map["a"]);
Assert.AreEqual((VM.Types.ByteString)"testdeserialize", map["a"]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public void Test_AccountIsStandard()

// Empty

var result = _engine.ExecuteTestCaseStandard("accountIsStandard", new ByteString(new byte[0]));
var result = _engine.ExecuteTestCaseStandard("accountIsStandard", new VM.Types.ByteString(new byte[0]));
Assert.AreEqual(VM.VMState.FAULT, _engine.State);
Assert.AreEqual(0, result.Count);

// No standard

_engine.Reset();
result = _engine.ExecuteTestCaseStandard("accountIsStandard", new ByteString(new byte[20]));
result = _engine.ExecuteTestCaseStandard("accountIsStandard", new VM.Types.ByteString(new byte[20]));
Assert.AreEqual(VM.VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

Expand All @@ -49,7 +49,7 @@ public void Test_AccountIsStandard()
Assert.AreEqual(false, item.GetBoolean());

_engine.Reset();
result = _engine.ExecuteTestCaseStandard("accountIsStandard", new ByteString(noStandard));
result = _engine.ExecuteTestCaseStandard("accountIsStandard", new VM.Types.ByteString(noStandard));
Assert.AreEqual(VM.VMState.HALT, _engine.State);
Assert.AreEqual(1, result.Count);

Expand Down
Loading