From 5897e5fec229506d4728e1b532db4124c74efdc4 Mon Sep 17 00:00:00 2001 From: erikzhang Date: Thu, 19 Dec 2019 17:07:52 +0800 Subject: [PATCH 1/2] Neo.VM.3.0.0-CI00200 --- src/neo/IO/Helper.cs | 10 ++ .../ApplicationEngine.OpCodePrices.cs | 92 +++++++++++++++---- src/neo/SmartContract/ApplicationEngine.cs | 15 +++ src/neo/SmartContract/BinarySerializer.cs | 80 +++++++--------- src/neo/SmartContract/ContainerPlaceholder.cs | 12 ++- .../SmartContract/InteropService.Binary.cs | 17 +--- .../Iterators/ByteArrayWrapper.cs | 2 +- src/neo/SmartContract/JsonSerializer.cs | 10 +- .../SmartContract/Native/NativeContract.cs | 3 +- .../Native/Tokens/Nep5AccountState.cs | 2 +- src/neo/SmartContract/StackItemType.cs | 14 --- src/neo/VM/Helper.cs | 37 ++++---- src/neo/neo.csproj | 2 +- .../Nep5NativeContractExtensions.cs | 3 +- .../Enumerators/UT_IteratorKeysWrapper.cs | 4 +- .../Enumerators/UT_IteratorValuesWrapper.cs | 6 +- .../Native/Tokens/UT_GasToken.cs | 4 +- .../Native/Tokens/UT_NeoToken.cs | 12 +-- .../Native/Tokens/UT_Nep5Token.cs | 3 +- .../SmartContract/Native/UT_NativeContract.cs | 6 +- .../SmartContract/Native/UT_PolicyContract.cs | 24 ++--- .../SmartContract/UT_BinarySerializer.cs | 38 +++----- .../SmartContract/UT_ContainerPlaceholder.cs | 6 +- .../SmartContract/UT_InteropService.NEO.cs | 38 ++++---- .../SmartContract/UT_InteropService.cs | 57 +++++------- .../SmartContract/UT_JsonSerializer.cs | 4 +- .../SmartContract/UT_NotifyEventArgs.cs | 4 +- .../SmartContract/UT_Syscalls.cs | 2 +- tests/neo.UnitTests/VM/UT_Helper.cs | 17 +--- 29 files changed, 260 insertions(+), 264 deletions(-) delete mode 100644 src/neo/SmartContract/StackItemType.cs diff --git a/src/neo/IO/Helper.cs b/src/neo/IO/Helper.cs index df74602cdd..ed27503648 100644 --- a/src/neo/IO/Helper.cs +++ b/src/neo/IO/Helper.cs @@ -88,6 +88,16 @@ public static byte[] DecompressLz4(this byte[] data, int maxOutput) return result; } + public static void FillBuffer(this BinaryReader reader, Span buffer) + { + while (!buffer.IsEmpty) + { + int count = reader.Read(buffer); + if (count == 0) throw new EndOfStreamException(); + buffer = buffer[count..]; + } + } + public static int GetVarSize(int value) { if (value < 0xFD) diff --git a/src/neo/SmartContract/ApplicationEngine.OpCodePrices.cs b/src/neo/SmartContract/ApplicationEngine.OpCodePrices.cs index a1a2ffd400..6cbfba2228 100644 --- a/src/neo/SmartContract/ApplicationEngine.OpCodePrices.cs +++ b/src/neo/SmartContract/ApplicationEngine.OpCodePrices.cs @@ -78,28 +78,73 @@ partial class ApplicationEngine [OpCode.REVERSE3] = 60, [OpCode.REVERSE4] = 60, [OpCode.REVERSEN] = 400, - [OpCode.TOALTSTACK] = 60, - [OpCode.FROMALTSTACK] = 60, - [OpCode.DUPFROMALTSTACK] = 60, - [OpCode.DUPFROMALTSTACKBOTTOM] = 60, - [OpCode.ISNULL] = 60, + [OpCode.INITSSLOT] = 400, + [OpCode.INITSLOT] = 800, + [OpCode.LDSFLD0] = 60, + [OpCode.LDSFLD1] = 60, + [OpCode.LDSFLD2] = 60, + [OpCode.LDSFLD3] = 60, + [OpCode.LDSFLD4] = 60, + [OpCode.LDSFLD5] = 60, + [OpCode.LDSFLD6] = 60, + [OpCode.LDSFLD] = 60, + [OpCode.STSFLD0] = 60, + [OpCode.STSFLD1] = 60, + [OpCode.STSFLD2] = 60, + [OpCode.STSFLD3] = 60, + [OpCode.STSFLD4] = 60, + [OpCode.STSFLD5] = 60, + [OpCode.STSFLD6] = 60, + [OpCode.STSFLD] = 60, + [OpCode.LDLOC0] = 60, + [OpCode.LDLOC1] = 60, + [OpCode.LDLOC2] = 60, + [OpCode.LDLOC3] = 60, + [OpCode.LDLOC4] = 60, + [OpCode.LDLOC5] = 60, + [OpCode.LDLOC6] = 60, + [OpCode.LDLOC] = 60, + [OpCode.STLOC0] = 60, + [OpCode.STLOC1] = 60, + [OpCode.STLOC2] = 60, + [OpCode.STLOC3] = 60, + [OpCode.STLOC4] = 60, + [OpCode.STLOC5] = 60, + [OpCode.STLOC6] = 60, + [OpCode.STLOC] = 60, + [OpCode.LDARG0] = 60, + [OpCode.LDARG1] = 60, + [OpCode.LDARG2] = 60, + [OpCode.LDARG3] = 60, + [OpCode.LDARG4] = 60, + [OpCode.LDARG5] = 60, + [OpCode.LDARG6] = 60, + [OpCode.LDARG] = 60, + [OpCode.STARG0] = 60, + [OpCode.STARG1] = 60, + [OpCode.STARG2] = 60, + [OpCode.STARG3] = 60, + [OpCode.STARG4] = 60, + [OpCode.STARG5] = 60, + [OpCode.STARG6] = 60, + [OpCode.STARG] = 60, + [OpCode.NEWBUFFER] = 80000, + [OpCode.MEMCPY] = 80000, [OpCode.CAT] = 80000, [OpCode.SUBSTR] = 80000, [OpCode.LEFT] = 80000, [OpCode.RIGHT] = 80000, - [OpCode.SIZE] = 60, [OpCode.INVERT] = 100, [OpCode.AND] = 200, [OpCode.OR] = 200, [OpCode.XOR] = 200, [OpCode.EQUAL] = 200, - [OpCode.INC] = 100, - [OpCode.DEC] = 100, + [OpCode.NOTEQUAL] = 200, [OpCode.SIGN] = 100, - [OpCode.NEGATE] = 100, [OpCode.ABS] = 100, - [OpCode.NOT] = 100, - [OpCode.NZ] = 100, + [OpCode.NEGATE] = 100, + [OpCode.INC] = 100, + [OpCode.DEC] = 100, [OpCode.ADD] = 200, [OpCode.SUB] = 200, [OpCode.MUL] = 300, @@ -107,31 +152,40 @@ partial class ApplicationEngine [OpCode.MOD] = 300, [OpCode.SHL] = 300, [OpCode.SHR] = 300, + [OpCode.NOT] = 100, [OpCode.BOOLAND] = 200, [OpCode.BOOLOR] = 200, + [OpCode.NZ] = 100, [OpCode.NUMEQUAL] = 200, [OpCode.NUMNOTEQUAL] = 200, [OpCode.LT] = 200, + [OpCode.LE] = 200, [OpCode.GT] = 200, - [OpCode.LTE] = 200, - [OpCode.GTE] = 200, + [OpCode.GE] = 200, [OpCode.MIN] = 200, [OpCode.MAX] = 200, [OpCode.WITHIN] = 200, - [OpCode.ARRAYSIZE] = 150, [OpCode.PACK] = 7000, [OpCode.UNPACK] = 7000, - [OpCode.PICKITEM] = 270000, - [OpCode.SETITEM] = 270000, + [OpCode.NEWARRAY0] = 400, [OpCode.NEWARRAY] = 15000, + [OpCode.NEWARRAY_T] = 15000, + [OpCode.NEWSTRUCT0] = 400, [OpCode.NEWSTRUCT] = 15000, [OpCode.NEWMAP] = 200, - [OpCode.APPEND] = 15000, - [OpCode.REVERSE] = 500, - [OpCode.REMOVE] = 500, + [OpCode.SIZE] = 150, [OpCode.HASKEY] = 270000, [OpCode.KEYS] = 500, [OpCode.VALUES] = 7000, + [OpCode.PICKITEM] = 270000, + [OpCode.APPEND] = 15000, + [OpCode.SETITEM] = 270000, + [OpCode.REVERSEITEMS] = 500, + [OpCode.REMOVE] = 500, + [OpCode.CLEARITEMS] = 400, + [OpCode.ISNULL] = 60, + [OpCode.ISTYPE] = 60, + [OpCode.CONVERT] = 80000, }; } } diff --git a/src/neo/SmartContract/ApplicationEngine.cs b/src/neo/SmartContract/ApplicationEngine.cs index 29a8d9a414..53bc8c68a8 100644 --- a/src/neo/SmartContract/ApplicationEngine.cs +++ b/src/neo/SmartContract/ApplicationEngine.cs @@ -5,6 +5,7 @@ using Neo.VM.Types; using System; using System.Collections.Generic; +using System.Text; using Array = System.Array; namespace Neo.SmartContract @@ -140,5 +141,19 @@ internal void SendNotification(UInt160 script_hash, StackItem state) Notify?.Invoke(this, notification); notifications.Add(notification); } + + public bool TryPop(out string s) + { + if (TryPop(out ReadOnlySpan b)) + { + s = Encoding.UTF8.GetString(b); + return true; + } + else + { + s = default; + return false; + } + } } } diff --git a/src/neo/SmartContract/BinarySerializer.cs b/src/neo/SmartContract/BinarySerializer.cs index 1c8f2f9088..48841384e8 100644 --- a/src/neo/SmartContract/BinarySerializer.cs +++ b/src/neo/SmartContract/BinarySerializer.cs @@ -7,31 +7,31 @@ using System.Linq; using System.Numerics; using Array = Neo.VM.Types.Array; -using Boolean = Neo.VM.Types.Boolean; +using Buffer = Neo.VM.Types.Buffer; namespace Neo.SmartContract { internal static class BinarySerializer { - public static StackItem Deserialize(byte[] data, uint maxItemSize, ReferenceCounter referenceCounter = null) + public static StackItem Deserialize(byte[] data, uint maxArraySize, uint maxItemSize, ReferenceCounter referenceCounter = null) { using MemoryStream ms = new MemoryStream(data, false); using BinaryReader reader = new BinaryReader(ms); - return Deserialize(reader, maxItemSize, referenceCounter); + return Deserialize(reader, maxArraySize, maxItemSize, referenceCounter); } - public static unsafe StackItem Deserialize(ReadOnlySpan data, uint maxItemSize, ReferenceCounter referenceCounter = null) + public static unsafe StackItem Deserialize(ReadOnlySpan data, uint maxArraySize, uint maxItemSize, ReferenceCounter referenceCounter = null) { if (data.IsEmpty) throw new FormatException(); fixed (byte* pointer = data) { using UnmanagedMemoryStream ms = new UnmanagedMemoryStream(pointer, data.Length); using BinaryReader reader = new BinaryReader(ms); - return Deserialize(reader, maxItemSize, referenceCounter); + return Deserialize(reader, maxArraySize, maxItemSize, referenceCounter); } } - private static StackItem Deserialize(BinaryReader reader, uint maxItemSize, ReferenceCounter referenceCounter) + private static StackItem Deserialize(BinaryReader reader, uint maxArraySize, uint maxItemSize, ReferenceCounter referenceCounter) { Stack deserialized = new Stack(); int undeserialized = 1; @@ -40,41 +40,35 @@ private static StackItem Deserialize(BinaryReader reader, uint maxItemSize, Refe StackItemType type = (StackItemType)reader.ReadByte(); switch (type) { - case StackItemType.ByteArray: - deserialized.Push(new ByteArray(reader.ReadVarBytes((int)maxItemSize))); - break; - case StackItemType.Boolean: - deserialized.Push(new Boolean(reader.ReadBoolean())); + case StackItemType.Any: + deserialized.Push(StackItem.Null); break; case StackItemType.Integer: - deserialized.Push(new Integer(new BigInteger(reader.ReadVarBytes(Integer.MaxSize)))); + deserialized.Push(new BigInteger(reader.ReadVarBytes(Integer.MaxSize))); + break; + case StackItemType.ByteArray: + deserialized.Push(reader.ReadVarBytes((int)maxItemSize)); + break; + case StackItemType.Buffer: + Buffer buffer = new Buffer((int)reader.ReadVarInt(maxItemSize)); + reader.FillBuffer(buffer.InnerBuffer); + deserialized.Push(buffer); break; case StackItemType.Array: case StackItemType.Struct: { - int count = (int)reader.ReadVarInt(maxItemSize); - deserialized.Push(new ContainerPlaceholder - { - Type = type, - ElementCount = count - }); + int count = (int)reader.ReadVarInt(maxArraySize); + deserialized.Push(new ContainerPlaceholder(type, count)); undeserialized += count; } break; case StackItemType.Map: { - int count = (int)reader.ReadVarInt(maxItemSize); - deserialized.Push(new ContainerPlaceholder - { - Type = type, - ElementCount = count - }); + int count = (int)reader.ReadVarInt(maxArraySize); + deserialized.Push(new ContainerPlaceholder(type, count)); undeserialized += count * 2; } break; - case StackItemType.Null: - deserialized.Push(StackItem.Null); - break; default: throw new FormatException(); } @@ -127,36 +121,30 @@ public static byte[] Serialize(StackItem item, uint maxSize) private static void Serialize(StackItem item, BinaryWriter writer, uint maxSize) { - List serialized = new List(); + List serialized = new List(); Stack unserialized = new Stack(); unserialized.Push(item); while (unserialized.Count > 0) { item = unserialized.Pop(); + writer.Write((byte)item.Type); switch (item) { - case ByteArray bytes: - writer.Write((byte)StackItemType.ByteArray); - writer.WriteVarBytes(bytes.ToByteArray()); - break; - case Boolean _: - writer.Write((byte)StackItemType.Boolean); - writer.Write(item.ToBoolean()); + case Null _: break; case Integer integer: - writer.Write((byte)StackItemType.Integer); - writer.WriteVarBytes(integer.ToByteArray()); + writer.WriteVarBytes(integer.Span); + break; + case ByteArray bytes: + writer.WriteVarBytes(bytes.Span); + break; + case Buffer buffer: + writer.WriteVarBytes(buffer.InnerBuffer); break; - case InteropInterface _: - throw new NotSupportedException(); case Array array: if (serialized.Any(p => ReferenceEquals(p, array))) throw new NotSupportedException(); serialized.Add(array); - if (array is Struct) - writer.Write((byte)StackItemType.Struct); - else - writer.Write((byte)StackItemType.Array); writer.WriteVarInt(array.Count); for (int i = array.Count - 1; i >= 0; i--) unserialized.Push(array[i]); @@ -165,7 +153,6 @@ private static void Serialize(StackItem item, BinaryWriter writer, uint maxSize) if (serialized.Any(p => ReferenceEquals(p, map))) throw new NotSupportedException(); serialized.Add(map); - writer.Write((byte)StackItemType.Map); writer.WriteVarInt(map.Count); foreach (var pair in map.Reverse()) { @@ -173,9 +160,8 @@ private static void Serialize(StackItem item, BinaryWriter writer, uint maxSize) unserialized.Push(pair.Key); } break; - case Null _: - writer.Write((byte)StackItemType.Null); - break; + default: + throw new NotSupportedException(); } if (writer.BaseStream.Position > maxSize) throw new InvalidOperationException(); diff --git a/src/neo/SmartContract/ContainerPlaceholder.cs b/src/neo/SmartContract/ContainerPlaceholder.cs index 29fa0f6e6e..72eb68e48d 100644 --- a/src/neo/SmartContract/ContainerPlaceholder.cs +++ b/src/neo/SmartContract/ContainerPlaceholder.cs @@ -5,10 +5,16 @@ namespace Neo.SmartContract { internal class ContainerPlaceholder : StackItem { - public StackItemType Type; - public int ElementCount; + public override StackItemType Type { get; } + public int ElementCount { get; } - public override bool Equals(StackItem other) => throw new NotSupportedException(); + public ContainerPlaceholder(StackItemType type, int count) + { + Type = type; + ElementCount = count; + } + + public override bool Equals(object obj) => throw new NotSupportedException(); public override int GetHashCode() => throw new NotSupportedException(); diff --git a/src/neo/SmartContract/InteropService.Binary.cs b/src/neo/SmartContract/InteropService.Binary.cs index f2e2d22a7b..fa33dbe4ba 100644 --- a/src/neo/SmartContract/InteropService.Binary.cs +++ b/src/neo/SmartContract/InteropService.Binary.cs @@ -1,7 +1,5 @@ -using Neo.VM; using Neo.VM.Types; using System; -using System.IO; namespace Neo.SmartContract { @@ -29,19 +27,8 @@ private static bool Binary_Serialize(ApplicationEngine engine) private static bool Binary_Deserialize(ApplicationEngine engine) { - StackItem item; - try - { - item = BinarySerializer.Deserialize(engine.CurrentContext.EvaluationStack.Pop().GetSpan(), engine.MaxItemSize, engine.ReferenceCounter); - } - catch (FormatException) - { - return false; - } - catch (IOException) - { - return false; - } + if (!engine.TryPop(out ReadOnlySpan data)) return false; + StackItem item = BinarySerializer.Deserialize(data, engine.MaxStackSize, engine.MaxItemSize, engine.ReferenceCounter); engine.CurrentContext.EvaluationStack.Push(item); return true; } diff --git a/src/neo/SmartContract/Iterators/ByteArrayWrapper.cs b/src/neo/SmartContract/Iterators/ByteArrayWrapper.cs index d5baff7da0..c60c839543 100644 --- a/src/neo/SmartContract/Iterators/ByteArrayWrapper.cs +++ b/src/neo/SmartContract/Iterators/ByteArrayWrapper.cs @@ -10,7 +10,7 @@ internal class ByteArrayWrapper : IIterator public ByteArrayWrapper(PrimitiveType value) { - this.array = value.ToByteArray().ToArray(); + this.array = value.Span.ToArray(); } public void Dispose() { } diff --git a/src/neo/SmartContract/JsonSerializer.cs b/src/neo/SmartContract/JsonSerializer.cs index 8bf3e6758f..db08617cee 100644 --- a/src/neo/SmartContract/JsonSerializer.cs +++ b/src/neo/SmartContract/JsonSerializer.cs @@ -8,7 +8,6 @@ using System.Numerics; using System.Text.Json; using Array = Neo.VM.Types.Array; -using Boolean = Neo.VM.Types.Boolean; namespace Neo.SmartContract { @@ -38,10 +37,6 @@ public static JObject Serialize(StackItem item) return integer.ToString(); return (double)num.GetBigInteger(); } - case Boolean boolean: - { - return boolean.ToBoolean(); - } case Map map: { var ret = new JObject(); @@ -98,9 +93,6 @@ public static byte[] SerializeToByteArray(StackItem item, uint maxSize) writer.WriteNumberValue((double)num.GetBigInteger()); break; } - case Boolean boolean: - writer.WriteBooleanValue(boolean.ToBoolean()); - break; case Map map: writer.WriteStartObject(); stack.Push(JsonTokenType.EndObject); @@ -159,7 +151,7 @@ public static StackItem Deserialize(JObject json, ReferenceCounter referenceCoun } case JBoolean boolean: { - return new Boolean(boolean.Value); + return boolean.Value ? 1 : 0; } case JObject obj: { diff --git a/src/neo/SmartContract/Native/NativeContract.cs b/src/neo/SmartContract/Native/NativeContract.cs index 17f053ddf8..60c1d6c8db 100644 --- a/src/neo/SmartContract/Native/NativeContract.cs +++ b/src/neo/SmartContract/Native/NativeContract.cs @@ -75,8 +75,7 @@ protected StorageKey CreateStorageKey(byte prefix, byte[] key = null) Key = new byte[sizeof(byte) + (key?.Length ?? 0)] }; storageKey.Key[0] = prefix; - if (key != null) - Buffer.BlockCopy(key, 0, storageKey.Key, 1, key.Length); + key?.CopyTo(storageKey.Key.AsSpan(1)); return storageKey; } diff --git a/src/neo/SmartContract/Native/Tokens/Nep5AccountState.cs b/src/neo/SmartContract/Native/Tokens/Nep5AccountState.cs index 76671d92c1..b2d62be6d1 100644 --- a/src/neo/SmartContract/Native/Tokens/Nep5AccountState.cs +++ b/src/neo/SmartContract/Native/Tokens/Nep5AccountState.cs @@ -19,7 +19,7 @@ public Nep5AccountState(byte[] data) public void FromByteArray(byte[] data) { - FromStruct((Struct)BinarySerializer.Deserialize(data, 34)); + FromStruct((Struct)BinarySerializer.Deserialize(data, 16, 34)); } protected virtual void FromStruct(Struct @struct) diff --git a/src/neo/SmartContract/StackItemType.cs b/src/neo/SmartContract/StackItemType.cs deleted file mode 100644 index 7e19c2982b..0000000000 --- a/src/neo/SmartContract/StackItemType.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace Neo.SmartContract -{ - internal enum StackItemType : byte - { - ByteArray = 0x00, - Boolean = 0x01, - Integer = 0x02, - InteropInterface = 0x40, - Array = 0x80, - Struct = 0x81, - Map = 0x82, - Null = 0xff - } -} diff --git a/src/neo/VM/Helper.cs b/src/neo/VM/Helper.cs index 28dcb11904..9aa8b9b7b9 100644 --- a/src/neo/VM/Helper.cs +++ b/src/neo/VM/Helper.cs @@ -8,7 +8,7 @@ using System.Numerics; using System.Text; using Array = Neo.VM.Types.Array; -using Boolean = Neo.VM.Types.Boolean; +using Buffer = Neo.VM.Types.Buffer; namespace Neo.VM { @@ -175,16 +175,22 @@ public static BigInteger GetBigInteger(this StackItem item) public static int GetByteLength(this StackItem item) { - if (!(item is PrimitiveType primitive)) - throw new ArgumentException(); - return primitive.GetByteLength(); + return item switch + { + PrimitiveType p => p.Size, + Buffer b => b.Size, + _ => throw new ArgumentException(), + }; } public static ReadOnlySpan GetSpan(this StackItem item) { - if (!(item is PrimitiveType primitive)) - throw new ArgumentException(); - return primitive.ToByteArray(); + return item switch + { + PrimitiveType p => p.Span, + Buffer b => b.InnerBuffer, + _ => throw new ArgumentException(), + }; } public static string GetString(this StackItem item) @@ -245,25 +251,18 @@ private static ContractParameter ToParameter(StackItem item, List<(StackItem, Co parameter.Value = map.Select(p => new KeyValuePair(ToParameter(p.Key, context), ToParameter(p.Value, context))).ToList(); } break; - case Boolean _: - parameter = new ContractParameter - { - Type = ContractParameterType.Boolean, - Value = item.ToBoolean() - }; - break; - case ByteArray _: + case ByteArray array: parameter = new ContractParameter { Type = ContractParameterType.ByteArray, - Value = item.GetSpan().ToArray() + Value = array.Span.ToArray() }; break; - case Integer _: + case Integer i: parameter = new ContractParameter { Type = ContractParameterType.Integer, - Value = item.GetBigInteger() + Value = i.ToBigInteger() }; break; case InteropInterface _: @@ -278,7 +277,7 @@ private static ContractParameter ToParameter(StackItem item, List<(StackItem, Co Type = ContractParameterType.Any }; break; - default: // Null included + default: throw new ArgumentException(); } return parameter; diff --git a/src/neo/neo.csproj b/src/neo/neo.csproj index ebac81c4bc..0d0cf1bb2f 100644 --- a/src/neo/neo.csproj +++ b/src/neo/neo.csproj @@ -27,7 +27,7 @@ - + diff --git a/tests/neo.UnitTests/Extensions/Nep5NativeContractExtensions.cs b/tests/neo.UnitTests/Extensions/Nep5NativeContractExtensions.cs index 4a04d97c84..a722583aea 100644 --- a/tests/neo.UnitTests/Extensions/Nep5NativeContractExtensions.cs +++ b/tests/neo.UnitTests/Extensions/Nep5NativeContractExtensions.cs @@ -4,6 +4,7 @@ using Neo.SmartContract; using Neo.SmartContract.Native; using Neo.VM; +using Neo.VM.Types; using System; using System.IO; using System.Linq; @@ -63,7 +64,7 @@ public static bool Transfer(this NativeContract contract, StoreView snapshot, by } var result = engine.ResultStack.Pop(); - result.Should().BeOfType(typeof(VM.Types.Boolean)); + result.Should().BeOfType(typeof(Integer)); return result.ToBoolean(); } diff --git a/tests/neo.UnitTests/SmartContract/Enumerators/UT_IteratorKeysWrapper.cs b/tests/neo.UnitTests/SmartContract/Enumerators/UT_IteratorKeysWrapper.cs index 12b82be599..eb7375b161 100644 --- a/tests/neo.UnitTests/SmartContract/Enumerators/UT_IteratorKeysWrapper.cs +++ b/tests/neo.UnitTests/SmartContract/Enumerators/UT_IteratorKeysWrapper.cs @@ -23,9 +23,7 @@ public void TestGeneratorAndDispose() [TestMethod] public void TestNextAndValue() { - StackItem stackItem = new VM.Types.Boolean(true); - List list = new List(); - list.Add(stackItem); + List list = new List { StackItem.True }; ArrayWrapper wrapper = new ArrayWrapper(list); IteratorKeysWrapper iteratorKeysWrapper = new IteratorKeysWrapper(wrapper); Action action = () => iteratorKeysWrapper.Next(); diff --git a/tests/neo.UnitTests/SmartContract/Enumerators/UT_IteratorValuesWrapper.cs b/tests/neo.UnitTests/SmartContract/Enumerators/UT_IteratorValuesWrapper.cs index 2e8bf5709b..088ac993b0 100644 --- a/tests/neo.UnitTests/SmartContract/Enumerators/UT_IteratorValuesWrapper.cs +++ b/tests/neo.UnitTests/SmartContract/Enumerators/UT_IteratorValuesWrapper.cs @@ -24,14 +24,12 @@ public void TestGeneratorAndDispose() [TestMethod] public void TestNextAndValue() { - StackItem stackItem = new VM.Types.Boolean(true); - List list = new List(); - list.Add(stackItem); + List list = new List { StackItem.True }; ArrayWrapper wrapper = new ArrayWrapper(list); IteratorValuesWrapper iteratorValuesWrapper = new IteratorValuesWrapper(wrapper); Action action = () => iteratorValuesWrapper.Next(); action.Should().NotThrow(); - Assert.AreEqual(stackItem, iteratorValuesWrapper.Value()); + Assert.AreEqual(StackItem.True, iteratorValuesWrapper.Value()); } } } diff --git a/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_GasToken.cs b/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_GasToken.cs index 2693a40690..51caa8db9c 100644 --- a/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_GasToken.cs +++ b/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_GasToken.cs @@ -165,10 +165,10 @@ public void TestGetSysFeeAmount2() StorageKey storageKey = new StorageKey { ScriptHash = NativeContract.GAS.Hash, - Key = new byte[sizeof(byte) + (key?.Length ?? 0)] + Key = new byte[sizeof(byte) + key.Length] }; storageKey.Key[0] = 15; - Buffer.BlockCopy(key, 0, storageKey.Key, 1, key.Length); + key.CopyTo(storageKey.Key.AsSpan(1)); BigInteger sys_fee = new BigInteger(10); snapshot.Storages.Add(storageKey, new StorageItem diff --git a/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_NeoToken.cs b/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_NeoToken.cs index 411a2139ca..2734bc141b 100644 --- a/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_NeoToken.cs +++ b/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_NeoToken.cs @@ -11,6 +11,7 @@ using Neo.SmartContract.Native; using Neo.UnitTests.Extensions; using Neo.VM; +using Neo.VM.Types; using System; using System.Linq; using System.Numerics; @@ -573,7 +574,7 @@ public void TestValidatorState_ToByteArray() engine.LoadScript(sb.ToArray()); engine.Execute(); var result = engine.ResultStack.Peek(); - result.GetType().Should().Be(typeof(VM.Types.Boolean)); + result.GetType().Should().Be(typeof(Integer)); return (true, result.ToBoolean()); } @@ -602,7 +603,7 @@ internal static (bool State, bool Result) Check_Vote(StoreView snapshot, byte[] } var result = engine.ResultStack.Pop(); - result.Should().BeOfType(typeof(VM.Types.Boolean)); + result.Should().BeOfType(typeof(Integer)); return (true, result.ToBoolean()); } @@ -626,7 +627,7 @@ internal static (bool State, bool Result) Check_RegisterValidator(StoreView snap } var result = engine.ResultStack.Pop(); - result.Should().BeOfType(typeof(VM.Types.Boolean)); + result.Should().BeOfType(typeof(Integer)); return (true, result.ToBoolean()); } @@ -687,7 +688,7 @@ internal static void CheckValidator(ECPoint eCPoint, DataCache.Trackable trackable, BigInteger balance, BigInteger height, ECPoint[] votes) { - var st = (VM.Types.Struct)BinarySerializer.Deserialize(trackable.Item.Value, 32); + var st = (VM.Types.Struct)BinarySerializer.Deserialize(trackable.Item.Value, 16, 32); st.Count.Should().Be(3); st.Select(u => u.GetType()).ToArray().Should().BeEquivalentTo(new Type[] { typeof(VM.Types.Integer), typeof(VM.Types.Integer), typeof(VM.Types.ByteArray) }); // Balance @@ -708,8 +709,7 @@ internal static StorageKey CreateStorageKey(byte prefix, byte[] key = null) Key = new byte[sizeof(byte) + (key?.Length ?? 0)] }; storageKey.Key[0] = prefix; - if (key != null) - Buffer.BlockCopy(key, 0, storageKey.Key, 1, key.Length); + key?.CopyTo(storageKey.Key.AsSpan(1)); return storageKey; } } diff --git a/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_Nep5Token.cs b/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_Nep5Token.cs index 5e9dbe9617..3cb10defe3 100644 --- a/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_Nep5Token.cs +++ b/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_Nep5Token.cs @@ -82,8 +82,7 @@ public StorageKey CreateStorageKey(byte prefix, byte[] key = null) Key = new byte[sizeof(byte) + (key?.Length ?? 0)] }; storageKey.Key[0] = prefix; - if (key != null) - Buffer.BlockCopy(key, 0, storageKey.Key, 1, key.Length); + key?.CopyTo(storageKey.Key.AsSpan(1)); return storageKey; } } diff --git a/tests/neo.UnitTests/SmartContract/Native/UT_NativeContract.cs b/tests/neo.UnitTests/SmartContract/Native/UT_NativeContract.cs index 71cfd3c093..8c6c0c669b 100644 --- a/tests/neo.UnitTests/SmartContract/Native/UT_NativeContract.cs +++ b/tests/neo.UnitTests/SmartContract/Native/UT_NativeContract.cs @@ -69,12 +69,10 @@ public void TestOnPersistWithArgs() TestNativeContract testNativeContract = new TestNativeContract(); VMArray args = new VMArray(); - VM.Types.Boolean result1 = new VM.Types.Boolean(false); - testNativeContract.TestOnPersist(engine1, args).Should().Be(result1); + testNativeContract.TestOnPersist(engine1, args).Should().Be(StackItem.False); ApplicationEngine engine2 = new ApplicationEngine(TriggerType.System, null, Blockchain.Singleton.GetSnapshot(), 0); - VM.Types.Boolean result2 = new VM.Types.Boolean(true); - testNativeContract.TestOnPersist(engine2, args).Should().Be(result2); + testNativeContract.TestOnPersist(engine2, args).Should().Be(StackItem.True); } [TestMethod] diff --git a/tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs b/tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs index 0b15d0732d..f959a937bf 100644 --- a/tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs +++ b/tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs @@ -3,11 +3,11 @@ using Neo.IO; using Neo.Ledger; using Neo.Network.P2P.Payloads; -using Neo.Persistence; using Neo.SmartContract; using Neo.SmartContract.Native; using Neo.UnitTests.Extensions; using Neo.VM; +using Neo.VM.Types; using System.Linq; namespace Neo.UnitTests.SmartContract.Native @@ -67,7 +67,7 @@ public void Check_SetMaxBlockSize() var ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(null), "setMaxBlockSize", new ContractParameter(ContractParameterType.Integer) { Value = 1024 }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeFalse(); ret = NativeContract.Policy.Call(snapshot, "getMaxBlockSize"); @@ -78,7 +78,7 @@ public void Check_SetMaxBlockSize() ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(UInt160.Zero), "setMaxBlockSize", new ContractParameter(ContractParameterType.Integer) { Value = Neo.Network.P2P.Message.PayloadMaxSize }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeFalse(); ret = NativeContract.Policy.Call(snapshot, "getMaxBlockSize"); @@ -89,7 +89,7 @@ public void Check_SetMaxBlockSize() ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(UInt160.Zero), "setMaxBlockSize", new ContractParameter(ContractParameterType.Integer) { Value = 1024 }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeTrue(); ret = NativeContract.Policy.Call(snapshot, "getMaxBlockSize"); @@ -113,7 +113,7 @@ public void Check_SetMaxTransactionsPerBlock() var ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(), "setMaxTransactionsPerBlock", new ContractParameter(ContractParameterType.Integer) { Value = 1 }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeFalse(); ret = NativeContract.Policy.Call(snapshot, "getMaxTransactionsPerBlock"); @@ -124,7 +124,7 @@ public void Check_SetMaxTransactionsPerBlock() ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(UInt160.Zero), "setMaxTransactionsPerBlock", new ContractParameter(ContractParameterType.Integer) { Value = 1 }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeTrue(); ret = NativeContract.Policy.Call(snapshot, "getMaxTransactionsPerBlock"); @@ -148,7 +148,7 @@ public void Check_SetFeePerByte() var ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(), "setFeePerByte", new ContractParameter(ContractParameterType.Integer) { Value = 1 }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeFalse(); ret = NativeContract.Policy.Call(snapshot, "getFeePerByte"); @@ -159,7 +159,7 @@ public void Check_SetFeePerByte() ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(UInt160.Zero), "setFeePerByte", new ContractParameter(ContractParameterType.Integer) { Value = 1 }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeTrue(); ret = NativeContract.Policy.Call(snapshot, "getFeePerByte"); @@ -183,7 +183,7 @@ public void Check_Block_UnblockAccount() var ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(), "blockAccount", new ContractParameter(ContractParameterType.Hash160) { Value = UInt160.Zero }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeFalse(); ret = NativeContract.Policy.Call(snapshot, "getBlockedAccounts"); @@ -194,7 +194,7 @@ public void Check_Block_UnblockAccount() ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(UInt160.Zero), "blockAccount", new ContractParameter(ContractParameterType.Hash160) { Value = UInt160.Zero }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeTrue(); ret = NativeContract.Policy.Call(snapshot, "getBlockedAccounts"); @@ -206,7 +206,7 @@ public void Check_Block_UnblockAccount() ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(), "unblockAccount", new ContractParameter(ContractParameterType.Hash160) { Value = UInt160.Zero }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeFalse(); ret = NativeContract.Policy.Call(snapshot, "getBlockedAccounts"); @@ -218,7 +218,7 @@ public void Check_Block_UnblockAccount() ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(UInt160.Zero), "unblockAccount", new ContractParameter(ContractParameterType.Hash160) { Value = UInt160.Zero }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeTrue(); ret = NativeContract.Policy.Call(snapshot, "getBlockedAccounts"); diff --git a/tests/neo.UnitTests/SmartContract/UT_BinarySerializer.cs b/tests/neo.UnitTests/SmartContract/UT_BinarySerializer.cs index 888af9a23a..c34fdcf31d 100644 --- a/tests/neo.UnitTests/SmartContract/UT_BinarySerializer.cs +++ b/tests/neo.UnitTests/SmartContract/UT_BinarySerializer.cs @@ -19,38 +19,31 @@ public void TestSerialize() { byte[] result1 = BinarySerializer.Serialize(new byte[5], MaxItemSize); byte[] expectedArray1 = new byte[] { - 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00 + 0x28, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray1), Encoding.Default.GetString(result1)); byte[] result2 = BinarySerializer.Serialize(true, MaxItemSize); byte[] expectedArray2 = new byte[] { - 0x01, 0x01 + 0x20, 0x01, 0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray2), Encoding.Default.GetString(result2)); byte[] result3 = BinarySerializer.Serialize(1, MaxItemSize); byte[] expectedArray3 = new byte[] { - 0x02, 0x01, 0x01 + 0x20, 0x01, 0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray3), Encoding.Default.GetString(result3)); - StackItem stackItem4 = new InteropInterface(new object()); + StackItem stackItem4 = new InteropInterface(new object()); Action action4 = () => BinarySerializer.Serialize(stackItem4, MaxItemSize); action4.Should().Throw(); - byte[] result5 = BinarySerializer.Serialize(1, MaxItemSize); - byte[] expectedArray5 = new byte[] { - 0x02, 0x01, 0x01 - }; - Assert.AreEqual(Encoding.Default.GetString(expectedArray5), Encoding.Default.GetString(result5)); - - List list6 = new List { 1 }; StackItem stackItem62 = new VM.Types.Array(list6); byte[] result6 = BinarySerializer.Serialize(stackItem62, MaxItemSize); byte[] expectedArray6 = new byte[] { - 0x80,0x01,0x02,0x01,0x01 + 0x40,0x01,0x20,0x01,0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray6), Encoding.Default.GetString(result6)); @@ -58,14 +51,14 @@ public void TestSerialize() StackItem stackItem72 = new Struct(list7); byte[] result7 = BinarySerializer.Serialize(stackItem72, MaxItemSize); byte[] expectedArray7 = new byte[] { - 0x81,0x01,0x02,0x01,0x01 + 0x41,0x01,0x20,0x01,0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray7), Encoding.Default.GetString(result7)); StackItem stackItem82 = new Map { [2] = 1 }; byte[] result8 = BinarySerializer.Serialize(stackItem82, MaxItemSize); byte[] expectedArray8 = new byte[] { - 0x82,0x01,0x02,0x01,0x02,0x02,0x01,0x01 + 0x48,0x01,0x20,0x01,0x02,0x20,0x01,0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray8), Encoding.Default.GetString(result8)); @@ -85,41 +78,36 @@ public void TestDeserializeStackItem() { StackItem stackItem1 = new ByteArray(new byte[5]); byte[] byteArray1 = BinarySerializer.Serialize(stackItem1, MaxItemSize); - StackItem result1 = BinarySerializer.Deserialize(byteArray1, (uint)byteArray1.Length); + StackItem result1 = BinarySerializer.Deserialize(byteArray1, 2048, (uint)byteArray1.Length); Assert.AreEqual(stackItem1, result1); - StackItem stackItem2 = new VM.Types.Boolean(true); - byte[] byteArray2 = BinarySerializer.Serialize(stackItem2, MaxItemSize); - StackItem result2 = BinarySerializer.Deserialize(byteArray2, (uint)byteArray2.Length); - Assert.AreEqual(stackItem2, result2); - StackItem stackItem3 = new Integer(1); byte[] byteArray3 = BinarySerializer.Serialize(stackItem3, MaxItemSize); - StackItem result3 = BinarySerializer.Deserialize(byteArray3, (uint)byteArray3.Length); + StackItem result3 = BinarySerializer.Deserialize(byteArray3, 2048, (uint)byteArray3.Length); Assert.AreEqual(stackItem3, result3); byte[] byteArray4 = BinarySerializer.Serialize(1, MaxItemSize); byteArray4[0] = 0x40; - Action action4 = () => BinarySerializer.Deserialize(byteArray4, (uint)byteArray4.Length); + Action action4 = () => BinarySerializer.Deserialize(byteArray4, 2048, (uint)byteArray4.Length); action4.Should().Throw(); List list5 = new List { 1 }; StackItem stackItem52 = new VM.Types.Array(list5); byte[] byteArray5 = BinarySerializer.Serialize(stackItem52, MaxItemSize); - StackItem result5 = BinarySerializer.Deserialize(byteArray5, (uint)byteArray5.Length); + StackItem result5 = BinarySerializer.Deserialize(byteArray5, 2048, (uint)byteArray5.Length); Assert.AreEqual(((VM.Types.Array)stackItem52).Count, ((VM.Types.Array)result5).Count); Assert.AreEqual(((VM.Types.Array)stackItem52).GetEnumerator().Current, ((VM.Types.Array)result5).GetEnumerator().Current); List list6 = new List { 1 }; StackItem stackItem62 = new Struct(list6); byte[] byteArray6 = BinarySerializer.Serialize(stackItem62, MaxItemSize); - StackItem result6 = BinarySerializer.Deserialize(byteArray6, (uint)byteArray6.Length); + StackItem result6 = BinarySerializer.Deserialize(byteArray6, 2048, (uint)byteArray6.Length); Assert.AreEqual(((Struct)stackItem62).Count, ((Struct)result6).Count); Assert.AreEqual(((Struct)stackItem62).GetEnumerator().Current, ((Struct)result6).GetEnumerator().Current); StackItem stackItem72 = new Map { [2] = 1 }; byte[] byteArray7 = BinarySerializer.Serialize(stackItem72, MaxItemSize); - StackItem result7 = BinarySerializer.Deserialize(byteArray7, (uint)byteArray7.Length); + StackItem result7 = BinarySerializer.Deserialize(byteArray7, 2048, (uint)byteArray7.Length); Assert.AreEqual(((Map)stackItem72).Count, ((Map)result7).Count); CollectionAssert.AreEqual(((Map)stackItem72).Keys.ToArray(), ((Map)result7).Keys.ToArray()); CollectionAssert.AreEqual(((Map)stackItem72).Values.ToArray(), ((Map)result7).Values.ToArray()); diff --git a/tests/neo.UnitTests/SmartContract/UT_ContainerPlaceholder.cs b/tests/neo.UnitTests/SmartContract/UT_ContainerPlaceholder.cs index 0ba48e78a9..73023d6db8 100644 --- a/tests/neo.UnitTests/SmartContract/UT_ContainerPlaceholder.cs +++ b/tests/neo.UnitTests/SmartContract/UT_ContainerPlaceholder.cs @@ -12,14 +12,14 @@ public class UT_ContainerPlaceholder [TestMethod] public void TestGenerator() { - ContainerPlaceholder containerPlaceholder = new ContainerPlaceholder(); + ContainerPlaceholder containerPlaceholder = new ContainerPlaceholder(StackItemType.Any, 0); Assert.IsNotNull(containerPlaceholder); } [TestMethod] public void TestEquals() { - ContainerPlaceholder containerPlaceholder = new ContainerPlaceholder(); + ContainerPlaceholder containerPlaceholder = new ContainerPlaceholder(StackItemType.Any, 0); Action action = () => containerPlaceholder.Equals(new Integer(0)); action.Should().Throw(); } @@ -27,7 +27,7 @@ public void TestEquals() [TestMethod] public void TestGetBoolean() { - ContainerPlaceholder containerPlaceholder = new ContainerPlaceholder(); + ContainerPlaceholder containerPlaceholder = new ContainerPlaceholder(StackItemType.Any, 0); Action action = () => containerPlaceholder.ToBoolean(); action.Should().Throw(); } diff --git a/tests/neo.UnitTests/SmartContract/UT_InteropService.NEO.cs b/tests/neo.UnitTests/SmartContract/UT_InteropService.NEO.cs index 83700f33a0..959eeecae8 100644 --- a/tests/neo.UnitTests/SmartContract/UT_InteropService.NEO.cs +++ b/tests/neo.UnitTests/SmartContract/UT_InteropService.NEO.cs @@ -276,13 +276,13 @@ public void TestStorage_Find() engine.LoadScript(new byte[] { 0x01 }); engine.CurrentContext.EvaluationStack.Push(new byte[] { 0x01 }); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(new StorageContext + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(new StorageContext { ScriptHash = state.ScriptHash, IsReadOnly = false })); InteropService.Invoke(engine, InteropService.Storage.Find).Should().BeTrue(); - var iterator = ((InteropInterface)engine.CurrentContext.EvaluationStack.Pop()).GetInterface(); + var iterator = ((InteropInterface)engine.CurrentContext.EvaluationStack.Pop()).GetInterface(); iterator.Next(); var ele = iterator.Value(); ele.GetSpan().ToHexString().Should().Be(storageItem.Value.ToHexString()); @@ -301,7 +301,7 @@ public void TestEnumerator_Create() }; engine.CurrentContext.EvaluationStack.Push(arr); InteropService.Invoke(engine, InteropService.Enumerator.Create).Should().BeTrue(); - var ret = (InteropInterface)engine.CurrentContext.EvaluationStack.Pop(); + var ret = (InteropInterface)engine.CurrentContext.EvaluationStack.Pop(); ret.GetInterface().Next(); ret.GetInterface().Value().GetSpan().ToHexString() .Should().Be(new byte[] { 0x01 }.ToHexString()); @@ -318,7 +318,7 @@ public void TestEnumerator_Next() new byte[]{ 0x01 }, new byte[]{ 0x02 } }; - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(new ArrayWrapper(arr))); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(new ArrayWrapper(arr))); InteropService.Invoke(engine, InteropService.Enumerator.Next).Should().BeTrue(); engine.CurrentContext.EvaluationStack.Pop().ToBoolean().Should().BeTrue(); @@ -336,7 +336,7 @@ public void TestEnumerator_Value() }; var wrapper = new ArrayWrapper(arr); wrapper.Next(); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(wrapper)); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(wrapper)); InteropService.Invoke(engine, InteropService.Enumerator.Value).Should().BeTrue(); engine.CurrentContext.EvaluationStack.Pop().GetSpan().ToHexString().Should().Be(new byte[] { 0x01 }.ToHexString()); @@ -358,10 +358,10 @@ public void TestEnumerator_Concat() }; var wrapper1 = new ArrayWrapper(arr1); var wrapper2 = new ArrayWrapper(arr2); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(wrapper2)); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(wrapper1)); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(wrapper2)); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(wrapper1)); InteropService.Invoke(engine, InteropService.Enumerator.Concat).Should().BeTrue(); - var ret = ((InteropInterface)engine.CurrentContext.EvaluationStack.Pop()).GetInterface(); + var ret = ((InteropInterface)engine.CurrentContext.EvaluationStack.Pop()).GetInterface(); ret.Next().Should().BeTrue(); ret.Value().GetSpan().ToHexString().Should().Be(new byte[] { 0x01 }.ToHexString()); } @@ -376,12 +376,12 @@ public void TestIterator_Create() }; engine.CurrentContext.EvaluationStack.Push(arr); InteropService.Invoke(engine, InteropService.Iterator.Create).Should().BeTrue(); - var ret = (InteropInterface)engine.CurrentContext.EvaluationStack.Pop(); + var ret = (InteropInterface)engine.CurrentContext.EvaluationStack.Pop(); ret.GetInterface().Next(); ret.GetInterface().Value().GetSpan().ToHexString() .Should().Be(new byte[] { 0x01 }.ToHexString()); - var interop = new InteropInterface(1); + var interop = new InteropInterface(1); engine.CurrentContext.EvaluationStack.Push(interop); InteropService.Invoke(engine, InteropService.Iterator.Create).Should().BeFalse(); @@ -392,7 +392,7 @@ public void TestIterator_Create() }; engine.CurrentContext.EvaluationStack.Push(map); InteropService.Invoke(engine, InteropService.Iterator.Create).Should().BeTrue(); - ret = (InteropInterface)engine.CurrentContext.EvaluationStack.Pop(); + ret = (InteropInterface)engine.CurrentContext.EvaluationStack.Pop(); ret.GetInterface().Next(); ret.GetInterface().Key().GetBigInteger().Should().Be(1); ret.GetInterface().Value().GetBigInteger().Should().Be(2); @@ -411,7 +411,7 @@ public void TestIterator_Key() }; var wrapper = new ArrayWrapper(arr); wrapper.Next(); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(wrapper)); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(wrapper)); InteropService.Invoke(engine, InteropService.Iterator.Key).Should().BeTrue(); engine.CurrentContext.EvaluationStack.Pop().GetBigInteger().Should().Be(0); @@ -428,9 +428,9 @@ public void TestIterator_Keys() new byte[]{ 0x02 } }; var wrapper = new ArrayWrapper(arr); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(wrapper)); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(wrapper)); InteropService.Invoke(engine, InteropService.Iterator.Keys).Should().BeTrue(); - var ret = ((InteropInterface)engine.CurrentContext.EvaluationStack.Pop()).GetInterface(); + var ret = ((InteropInterface)engine.CurrentContext.EvaluationStack.Pop()).GetInterface(); ret.Next(); ret.Value().GetBigInteger().Should().Be(0); @@ -447,9 +447,9 @@ public void TestIterator_Values() new byte[]{ 0x02 } }; var wrapper = new ArrayWrapper(arr); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(wrapper)); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(wrapper)); InteropService.Invoke(engine, InteropService.Iterator.Values).Should().BeTrue(); - var ret = ((InteropInterface)engine.CurrentContext.EvaluationStack.Pop()).GetInterface(); + var ret = ((InteropInterface)engine.CurrentContext.EvaluationStack.Pop()).GetInterface(); ret.Next(); ret.Value().GetSpan().ToHexString().Should().Be(new byte[] { 0x01 }.ToHexString()); @@ -471,10 +471,10 @@ public void TestIterator_Concat() }; var wrapper1 = new ArrayWrapper(arr1); var wrapper2 = new ArrayWrapper(arr2); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(wrapper2)); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(wrapper1)); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(wrapper2)); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(wrapper1)); InteropService.Invoke(engine, InteropService.Iterator.Concat).Should().BeTrue(); - var ret = ((InteropInterface)engine.CurrentContext.EvaluationStack.Pop()).GetInterface(); + var ret = ((InteropInterface)engine.CurrentContext.EvaluationStack.Pop()).GetInterface(); ret.Next().Should().BeTrue(); ret.Value().GetSpan().ToHexString().Should().Be(new byte[] { 0x01 }.ToHexString()); } diff --git a/tests/neo.UnitTests/SmartContract/UT_InteropService.cs b/tests/neo.UnitTests/SmartContract/UT_InteropService.cs index 597da60adc..eb335e2619 100644 --- a/tests/neo.UnitTests/SmartContract/UT_InteropService.cs +++ b/tests/neo.UnitTests/SmartContract/UT_InteropService.cs @@ -36,9 +36,7 @@ public void Runtime_GetNotifications_Test() { // Drop arguments - script.Emit(VM.OpCode.TOALTSTACK); - script.Emit(VM.OpCode.DROP); - script.Emit(VM.OpCode.FROMALTSTACK); + script.Emit(OpCode.NIP); // Notify method @@ -294,12 +292,12 @@ public void TestRuntime_CheckWitness() engine.CurrentContext.EvaluationStack.Push(pubkey.EncodePoint(true)); InteropService.Invoke(engine, InteropService.Runtime.CheckWitness).Should().BeTrue(); - engine.CurrentContext.EvaluationStack.Peek().GetType().Should().Be(typeof(Neo.VM.Types.Boolean)); + engine.CurrentContext.EvaluationStack.Peek().GetType().Should().Be(typeof(Integer)); engine.CurrentContext.EvaluationStack.Pop().ToBoolean().Should().Be(false); engine.CurrentContext.EvaluationStack.Push(((Transaction)engine.ScriptContainer).Sender.ToArray()); InteropService.Invoke(engine, InteropService.Runtime.CheckWitness).Should().BeTrue(); - engine.CurrentContext.EvaluationStack.Peek().GetType().Should().Be(typeof(Neo.VM.Types.Boolean)); + engine.CurrentContext.EvaluationStack.Peek().GetType().Should().Be(typeof(Integer)); engine.CurrentContext.EvaluationStack.Pop().ToBoolean().Should().Be(false); engine.CurrentContext.EvaluationStack.Push(new byte[0]); @@ -337,12 +335,12 @@ public void TestRuntime_Serialize() engine.CurrentContext.EvaluationStack.Push(100); InteropService.Invoke(engine, InteropService.Binary.Serialize).Should().BeTrue(); engine.CurrentContext.EvaluationStack.Pop().GetSpan().ToHexString() - .Should().Be(new byte[] { 0x02, 0x01, 0x64 }.ToHexString()); + .Should().Be(new byte[] { 0x20, 0x01, 0x64 }.ToHexString()); engine.CurrentContext.EvaluationStack.Push(new byte[1024 * 1024 * 2]); //Larger than MaxItemSize InteropService.Invoke(engine, InteropService.Binary.Serialize).Should().BeFalse(); - engine.CurrentContext.EvaluationStack.Push(new TestInteropInterface()); //NotSupportedException + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(new object())); //NotSupportedException InteropService.Invoke(engine, InteropService.Binary.Serialize).Should().BeFalse(); } @@ -356,7 +354,7 @@ public void TestRuntime_Deserialize() engine.CurrentContext.EvaluationStack.Pop().GetBigInteger().Should().Be(100); engine.CurrentContext.EvaluationStack.Push(new byte[] { 0xfa, 0x01 }); //FormatException - InteropService.Invoke(engine, InteropService.Binary.Deserialize).Should().BeFalse(); + Assert.ThrowsException(() => InteropService.Invoke(engine, InteropService.Binary.Deserialize)); } [TestMethod] @@ -391,7 +389,7 @@ public void TestCrypto_Verify() wrongkey[0] = 5; engine.CurrentContext.EvaluationStack.Push(signature); engine.CurrentContext.EvaluationStack.Push(wrongkey); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(engine.ScriptContainer)); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(engine.ScriptContainer)); InteropService.Invoke(engine, InteropService.Crypto.ECDsaVerify).Should().BeTrue(); engine.CurrentContext.EvaluationStack.Peek().ToBoolean().Should().BeFalse(); } @@ -484,7 +482,7 @@ public void TestStorage_GetContext() { var engine = GetEngine(); InteropService.Invoke(engine, InteropService.Storage.GetContext).Should().BeTrue(); - var ret = (InteropInterface)engine.CurrentContext.EvaluationStack.Pop(); + var ret = (InteropInterface)engine.CurrentContext.EvaluationStack.Pop(); ret.GetInterface().ScriptHash.Should().Be(engine.CurrentScriptHash); ret.GetInterface().IsReadOnly.Should().BeFalse(); } @@ -494,7 +492,7 @@ public void TestStorage_GetReadOnlyContext() { var engine = GetEngine(); InteropService.Invoke(engine, InteropService.Storage.GetReadOnlyContext).Should().BeTrue(); - var ret = (InteropInterface)engine.CurrentContext.EvaluationStack.Pop(); + var ret = (InteropInterface)engine.CurrentContext.EvaluationStack.Pop(); ret.GetInterface().ScriptHash.Should().Be(engine.CurrentScriptHash); ret.GetInterface().IsReadOnly.Should().BeTrue(); } @@ -523,7 +521,7 @@ public void TestStorage_Get() engine.LoadScript(new byte[] { 0x01 }); engine.CurrentContext.EvaluationStack.Push(new byte[] { 0x01 }); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(new StorageContext + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(new StorageContext { ScriptHash = state.ScriptHash, IsReadOnly = false @@ -535,7 +533,7 @@ public void TestStorage_Get() engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0); engine.LoadScript(new byte[] { 0x01 }); engine.CurrentContext.EvaluationStack.Push(new byte[] { 0x01 }); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(new StorageContext + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(new StorageContext { ScriptHash = state.ScriptHash, IsReadOnly = false @@ -564,7 +562,7 @@ public void TestStorage_Put() ScriptHash = state.ScriptHash, IsReadOnly = false }; - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); InteropService.Invoke(engine, InteropService.Storage.Put).Should().BeFalse(); //key.Length > MaxStorageKeySize @@ -572,7 +570,7 @@ public void TestStorage_Put() value = new byte[] { 0x02 }; engine.CurrentContext.EvaluationStack.Push(value); engine.CurrentContext.EvaluationStack.Push(key); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); InteropService.Invoke(engine, InteropService.Storage.Put).Should().BeFalse(); //value.Length > MaxStorageValueSize @@ -580,7 +578,7 @@ public void TestStorage_Put() value = new byte[ushort.MaxValue + 1]; engine.CurrentContext.EvaluationStack.Push(value); engine.CurrentContext.EvaluationStack.Push(key); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); InteropService.Invoke(engine, InteropService.Storage.Put).Should().BeFalse(); //context.IsReadOnly @@ -589,7 +587,7 @@ public void TestStorage_Put() storageContext.IsReadOnly = true; engine.CurrentContext.EvaluationStack.Push(value); engine.CurrentContext.EvaluationStack.Push(key); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); InteropService.Invoke(engine, InteropService.Storage.Put).Should().BeFalse(); //storage value is constant @@ -615,14 +613,14 @@ public void TestStorage_Put() storageContext.IsReadOnly = false; engine.CurrentContext.EvaluationStack.Push(value); engine.CurrentContext.EvaluationStack.Push(key); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); InteropService.Invoke(engine, InteropService.Storage.Put).Should().BeFalse(); //success storageItem.IsConstant = false; engine.CurrentContext.EvaluationStack.Push(value); engine.CurrentContext.EvaluationStack.Push(key); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); InteropService.Invoke(engine, InteropService.Storage.Put).Should().BeTrue(); //value length == 0 @@ -630,7 +628,7 @@ public void TestStorage_Put() value = new byte[0]; engine.CurrentContext.EvaluationStack.Push(value); engine.CurrentContext.EvaluationStack.Push(key); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); InteropService.Invoke(engine, InteropService.Storage.Put).Should().BeTrue(); } @@ -668,7 +666,7 @@ public void TestStorage_PutEx() engine.CurrentContext.EvaluationStack.Push((int)StorageFlags.None); engine.CurrentContext.EvaluationStack.Push(value); engine.CurrentContext.EvaluationStack.Push(key); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); InteropService.Invoke(engine, InteropService.Storage.PutEx).Should().BeTrue(); } @@ -705,20 +703,20 @@ public void TestStorage_Delete() IsReadOnly = false }; engine.CurrentContext.EvaluationStack.Push(key); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); InteropService.Invoke(engine, InteropService.Storage.Delete).Should().BeTrue(); //context is readonly storageContext.IsReadOnly = true; engine.CurrentContext.EvaluationStack.Push(key); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); InteropService.Invoke(engine, InteropService.Storage.Delete).Should().BeFalse(); //CheckStorageContext fail storageContext.IsReadOnly = false; state.Manifest.Features = ContractFeatures.NoProperty; engine.CurrentContext.EvaluationStack.Push(key); - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); InteropService.Invoke(engine, InteropService.Storage.Delete).Should().BeFalse(); } @@ -735,9 +733,9 @@ public void TestStorageContext_AsReadOnly() ScriptHash = state.ScriptHash, IsReadOnly = false }; - engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); + engine.CurrentContext.EvaluationStack.Push(new InteropInterface(storageContext)); InteropService.Invoke(engine, InteropService.Storage.AsReadOnly).Should().BeTrue(); - var ret = (InteropInterface)engine.CurrentContext.EvaluationStack.Pop(); + var ret = (InteropInterface)engine.CurrentContext.EvaluationStack.Pop(); ret.GetInterface().IsReadOnly.Should().Be(true); } @@ -909,11 +907,4 @@ private static ApplicationEngine GetEngine(bool hasContainer = false, bool hasSn return engine; } } - - internal class TestInteropInterface : InteropInterface - { - public override bool Equals(StackItem other) => true; - public override bool ToBoolean() => true; - public override T GetInterface() => throw new NotImplementedException(); - } } diff --git a/tests/neo.UnitTests/SmartContract/UT_JsonSerializer.cs b/tests/neo.UnitTests/SmartContract/UT_JsonSerializer.cs index 5f2758ad36..076acbad42 100644 --- a/tests/neo.UnitTests/SmartContract/UT_JsonSerializer.cs +++ b/tests/neo.UnitTests/SmartContract/UT_JsonSerializer.cs @@ -265,7 +265,7 @@ public void Serialize_Array_Bool_Str_Num() var json = JsonSerializer.Serialize(entry).ToString(); - Assert.AreEqual(json, "[true,\"dGVzdA==\",123]"); + Assert.AreEqual(json, "[1,\"dGVzdA==\",123]"); } [TestMethod] @@ -294,7 +294,7 @@ public void Serialize_Array_OfArray() var json = JsonSerializer.Serialize(entry).ToString(); - Assert.AreEqual(json, "[[true,\"dGVzdDE=\",123],[true,\"dGVzdDI=\",321]]"); + Assert.AreEqual(json, "[[1,\"dGVzdDE=\",123],[1,\"dGVzdDI=\",321]]"); } [TestMethod] diff --git a/tests/neo.UnitTests/SmartContract/UT_NotifyEventArgs.cs b/tests/neo.UnitTests/SmartContract/UT_NotifyEventArgs.cs index 1dfd6a6ec5..d86a9d208e 100644 --- a/tests/neo.UnitTests/SmartContract/UT_NotifyEventArgs.cs +++ b/tests/neo.UnitTests/SmartContract/UT_NotifyEventArgs.cs @@ -2,7 +2,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Neo.Network.P2P.Payloads; using Neo.SmartContract; -using Neo.VM.Types; namespace Neo.UnitTests.SmartContract { @@ -14,8 +13,7 @@ public void TestGetScriptContainer() { IVerifiable container = new TestVerifiable(); UInt160 script_hash = new byte[] { 0x00 }.ToScriptHash(); - StackItem state = new ContainerPlaceholder(); - NotifyEventArgs args = new NotifyEventArgs(container, script_hash, state); + NotifyEventArgs args = new NotifyEventArgs(container, script_hash, 0); args.ScriptContainer.Should().Be(container); } } diff --git a/tests/neo.UnitTests/SmartContract/UT_Syscalls.cs b/tests/neo.UnitTests/SmartContract/UT_Syscalls.cs index 04f60db7ff..3f005e87aa 100644 --- a/tests/neo.UnitTests/SmartContract/UT_Syscalls.cs +++ b/tests/neo.UnitTests/SmartContract/UT_Syscalls.cs @@ -182,7 +182,7 @@ public void Json_Serialize() Assert.IsTrue(engine.ResultStack.TryPop(out var m) && m.GetString() == "{\"key\":\"dmFsdWU=\"}"); Assert.IsTrue(engine.ResultStack.TryPop(out var n) && n.GetString() == "null"); Assert.IsTrue(engine.ResultStack.TryPop(out var s) && s.GetString() == "\"dGVzdA==\""); - Assert.IsTrue(engine.ResultStack.TryPop(out var b) && b.GetString() == "true"); + Assert.IsTrue(engine.ResultStack.TryPop(out var b) && b.GetString() == "1"); Assert.IsTrue(engine.ResultStack.TryPop(out var i) && i.GetString() == "5"); } } diff --git a/tests/neo.UnitTests/VM/UT_Helper.cs b/tests/neo.UnitTests/VM/UT_Helper.cs index 559f9013ae..d37e3de5d0 100644 --- a/tests/neo.UnitTests/VM/UT_Helper.cs +++ b/tests/neo.UnitTests/VM/UT_Helper.cs @@ -96,7 +96,7 @@ public void TestMakeScript() { byte[] testScript = NativeContract.GAS.Hash.MakeScript("balanceOf", UInt160.Zero); - Assert.AreEqual("0c14000000000000000000000000000000000000000011c10c0962616c616e63654f660c143b7d3711c6f0ccf9b1dca903d1bfa1d896f1238c41627d5b52", + Assert.AreEqual("0c14000000000000000000000000000000000000000011c00c0962616c616e63654f660c143b7d3711c6f0ccf9b1dca903d1bfa1d896f1238c41627d5b52", testScript.ToHexString()); } @@ -107,12 +107,12 @@ public void TestToParameter() Assert.AreEqual(30000000000000L, (long)new BigInteger(byteItem.ToParameter().Value as byte[])); StackItem boolItem = false; - Assert.AreEqual(false, (bool)boolItem.ToParameter().Value); + Assert.AreEqual(BigInteger.Zero, (BigInteger)boolItem.ToParameter().Value); StackItem intItem = new BigInteger(1000); Assert.AreEqual(1000, (BigInteger)intItem.ToParameter().Value); - StackItem interopItem = new VM.Types.InteropInterface("test"); + StackItem interopItem = new VM.Types.InteropInterface("test"); Assert.AreEqual(null, interopItem.ToParameter().Value); StackItem arrayItem = new VM.Types.Array(new[] { byteItem, boolItem, intItem, interopItem }); @@ -482,7 +482,6 @@ public void TestToParameter2() { TestToParaMeter2VMArray(); TestToParameter2Map(); - TestToParameter2VMBoolean(); TestToParameter2ByteArray(); TestToParameter2Integer(); TestToParameter2InteropInterface(); @@ -493,7 +492,7 @@ public void TestToParameter2() private void TestToParameter2InteropInterface() { - StackItem item = new VM.Types.InteropInterface(new VM.Types.Boolean(true)); + StackItem item = new InteropInterface(new object()); ContractParameter parameter = VM.Helper.ToParameter(item); Assert.AreEqual(ContractParameterType.InteropInterface, parameter.Type); } @@ -514,14 +513,6 @@ private void TestToParameter2ByteArray() Assert.AreEqual(Encoding.Default.GetString(new byte[] { 0x00 }), Encoding.Default.GetString((byte[])parameter.Value)); } - private void TestToParameter2VMBoolean() - { - StackItem item = new VM.Types.Boolean(true); - ContractParameter parameter = VM.Helper.ToParameter(item); - Assert.AreEqual(ContractParameterType.Boolean, parameter.Type); - Assert.AreEqual(true, parameter.Value); - } - private void TestToParameter2Map() { StackItem item = new VM.Types.Map(); From a437db8b1ab34193a8aa6d55f664979e4b767068 Mon Sep 17 00:00:00 2001 From: erikzhang Date: Wed, 25 Dec 2019 15:42:46 +0800 Subject: [PATCH 2/2] Neo.VM.3.0.0-CI00201 --- src/neo/SmartContract/BinarySerializer.cs | 7 ++++++ src/neo/SmartContract/JsonSerializer.cs | 10 +++++++- src/neo/VM/Helper.cs | 8 +++++++ src/neo/neo.csproj | 2 +- .../Nep5NativeContractExtensions.cs | 3 +-- .../Enumerators/UT_IteratorKeysWrapper.cs | 4 +++- .../Enumerators/UT_IteratorValuesWrapper.cs | 6 +++-- .../Native/Tokens/UT_NeoToken.cs | 7 +++--- .../SmartContract/Native/UT_NativeContract.cs | 6 +++-- .../SmartContract/Native/UT_PolicyContract.cs | 24 +++++++++---------- .../SmartContract/UT_BinarySerializer.cs | 15 ++++++++---- .../SmartContract/UT_InteropService.cs | 6 ++--- .../SmartContract/UT_JsonSerializer.cs | 4 ++-- .../SmartContract/UT_Syscalls.cs | 2 +- tests/neo.UnitTests/VM/UT_Helper.cs | 11 ++++++++- 15 files changed, 78 insertions(+), 37 deletions(-) diff --git a/src/neo/SmartContract/BinarySerializer.cs b/src/neo/SmartContract/BinarySerializer.cs index 48841384e8..3f4d9b44bd 100644 --- a/src/neo/SmartContract/BinarySerializer.cs +++ b/src/neo/SmartContract/BinarySerializer.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Numerics; using Array = Neo.VM.Types.Array; +using Boolean = Neo.VM.Types.Boolean; using Buffer = Neo.VM.Types.Buffer; namespace Neo.SmartContract @@ -43,6 +44,9 @@ private static StackItem Deserialize(BinaryReader reader, uint maxArraySize, uin case StackItemType.Any: deserialized.Push(StackItem.Null); break; + case StackItemType.Boolean: + deserialized.Push(reader.ReadBoolean()); + break; case StackItemType.Integer: deserialized.Push(new BigInteger(reader.ReadVarBytes(Integer.MaxSize))); break; @@ -132,6 +136,9 @@ private static void Serialize(StackItem item, BinaryWriter writer, uint maxSize) { case Null _: break; + case Boolean _: + writer.Write(item.ToBoolean()); + break; case Integer integer: writer.WriteVarBytes(integer.Span); break; diff --git a/src/neo/SmartContract/JsonSerializer.cs b/src/neo/SmartContract/JsonSerializer.cs index db08617cee..8bf3e6758f 100644 --- a/src/neo/SmartContract/JsonSerializer.cs +++ b/src/neo/SmartContract/JsonSerializer.cs @@ -8,6 +8,7 @@ using System.Numerics; using System.Text.Json; using Array = Neo.VM.Types.Array; +using Boolean = Neo.VM.Types.Boolean; namespace Neo.SmartContract { @@ -37,6 +38,10 @@ public static JObject Serialize(StackItem item) return integer.ToString(); return (double)num.GetBigInteger(); } + case Boolean boolean: + { + return boolean.ToBoolean(); + } case Map map: { var ret = new JObject(); @@ -93,6 +98,9 @@ public static byte[] SerializeToByteArray(StackItem item, uint maxSize) writer.WriteNumberValue((double)num.GetBigInteger()); break; } + case Boolean boolean: + writer.WriteBooleanValue(boolean.ToBoolean()); + break; case Map map: writer.WriteStartObject(); stack.Push(JsonTokenType.EndObject); @@ -151,7 +159,7 @@ public static StackItem Deserialize(JObject json, ReferenceCounter referenceCoun } case JBoolean boolean: { - return boolean.Value ? 1 : 0; + return new Boolean(boolean.Value); } case JObject obj: { diff --git a/src/neo/VM/Helper.cs b/src/neo/VM/Helper.cs index 9aa8b9b7b9..83b4aaf3e2 100644 --- a/src/neo/VM/Helper.cs +++ b/src/neo/VM/Helper.cs @@ -8,6 +8,7 @@ using System.Numerics; using System.Text; using Array = Neo.VM.Types.Array; +using Boolean = Neo.VM.Types.Boolean; using Buffer = Neo.VM.Types.Buffer; namespace Neo.VM @@ -251,6 +252,13 @@ private static ContractParameter ToParameter(StackItem item, List<(StackItem, Co parameter.Value = map.Select(p => new KeyValuePair(ToParameter(p.Key, context), ToParameter(p.Value, context))).ToList(); } break; + case Boolean _: + parameter = new ContractParameter + { + Type = ContractParameterType.Boolean, + Value = item.ToBoolean() + }; + break; case ByteArray array: parameter = new ContractParameter { diff --git a/src/neo/neo.csproj b/src/neo/neo.csproj index 0d0cf1bb2f..075d17fb7f 100644 --- a/src/neo/neo.csproj +++ b/src/neo/neo.csproj @@ -27,7 +27,7 @@ - + diff --git a/tests/neo.UnitTests/Extensions/Nep5NativeContractExtensions.cs b/tests/neo.UnitTests/Extensions/Nep5NativeContractExtensions.cs index a722583aea..4a04d97c84 100644 --- a/tests/neo.UnitTests/Extensions/Nep5NativeContractExtensions.cs +++ b/tests/neo.UnitTests/Extensions/Nep5NativeContractExtensions.cs @@ -4,7 +4,6 @@ using Neo.SmartContract; using Neo.SmartContract.Native; using Neo.VM; -using Neo.VM.Types; using System; using System.IO; using System.Linq; @@ -64,7 +63,7 @@ public static bool Transfer(this NativeContract contract, StoreView snapshot, by } var result = engine.ResultStack.Pop(); - result.Should().BeOfType(typeof(Integer)); + result.Should().BeOfType(typeof(VM.Types.Boolean)); return result.ToBoolean(); } diff --git a/tests/neo.UnitTests/SmartContract/Enumerators/UT_IteratorKeysWrapper.cs b/tests/neo.UnitTests/SmartContract/Enumerators/UT_IteratorKeysWrapper.cs index eb7375b161..12b82be599 100644 --- a/tests/neo.UnitTests/SmartContract/Enumerators/UT_IteratorKeysWrapper.cs +++ b/tests/neo.UnitTests/SmartContract/Enumerators/UT_IteratorKeysWrapper.cs @@ -23,7 +23,9 @@ public void TestGeneratorAndDispose() [TestMethod] public void TestNextAndValue() { - List list = new List { StackItem.True }; + StackItem stackItem = new VM.Types.Boolean(true); + List list = new List(); + list.Add(stackItem); ArrayWrapper wrapper = new ArrayWrapper(list); IteratorKeysWrapper iteratorKeysWrapper = new IteratorKeysWrapper(wrapper); Action action = () => iteratorKeysWrapper.Next(); diff --git a/tests/neo.UnitTests/SmartContract/Enumerators/UT_IteratorValuesWrapper.cs b/tests/neo.UnitTests/SmartContract/Enumerators/UT_IteratorValuesWrapper.cs index 088ac993b0..2e8bf5709b 100644 --- a/tests/neo.UnitTests/SmartContract/Enumerators/UT_IteratorValuesWrapper.cs +++ b/tests/neo.UnitTests/SmartContract/Enumerators/UT_IteratorValuesWrapper.cs @@ -24,12 +24,14 @@ public void TestGeneratorAndDispose() [TestMethod] public void TestNextAndValue() { - List list = new List { StackItem.True }; + StackItem stackItem = new VM.Types.Boolean(true); + List list = new List(); + list.Add(stackItem); ArrayWrapper wrapper = new ArrayWrapper(list); IteratorValuesWrapper iteratorValuesWrapper = new IteratorValuesWrapper(wrapper); Action action = () => iteratorValuesWrapper.Next(); action.Should().NotThrow(); - Assert.AreEqual(StackItem.True, iteratorValuesWrapper.Value()); + Assert.AreEqual(stackItem, iteratorValuesWrapper.Value()); } } } diff --git a/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_NeoToken.cs b/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_NeoToken.cs index 2734bc141b..6111c9b052 100644 --- a/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_NeoToken.cs +++ b/tests/neo.UnitTests/SmartContract/Native/Tokens/UT_NeoToken.cs @@ -11,7 +11,6 @@ using Neo.SmartContract.Native; using Neo.UnitTests.Extensions; using Neo.VM; -using Neo.VM.Types; using System; using System.Linq; using System.Numerics; @@ -574,7 +573,7 @@ public void TestValidatorState_ToByteArray() engine.LoadScript(sb.ToArray()); engine.Execute(); var result = engine.ResultStack.Peek(); - result.GetType().Should().Be(typeof(Integer)); + result.GetType().Should().Be(typeof(VM.Types.Boolean)); return (true, result.ToBoolean()); } @@ -603,7 +602,7 @@ internal static (bool State, bool Result) Check_Vote(StoreView snapshot, byte[] } var result = engine.ResultStack.Pop(); - result.Should().BeOfType(typeof(Integer)); + result.Should().BeOfType(typeof(VM.Types.Boolean)); return (true, result.ToBoolean()); } @@ -627,7 +626,7 @@ internal static (bool State, bool Result) Check_RegisterValidator(StoreView snap } var result = engine.ResultStack.Pop(); - result.Should().BeOfType(typeof(Integer)); + result.Should().BeOfType(typeof(VM.Types.Boolean)); return (true, result.ToBoolean()); } diff --git a/tests/neo.UnitTests/SmartContract/Native/UT_NativeContract.cs b/tests/neo.UnitTests/SmartContract/Native/UT_NativeContract.cs index 8c6c0c669b..71cfd3c093 100644 --- a/tests/neo.UnitTests/SmartContract/Native/UT_NativeContract.cs +++ b/tests/neo.UnitTests/SmartContract/Native/UT_NativeContract.cs @@ -69,10 +69,12 @@ public void TestOnPersistWithArgs() TestNativeContract testNativeContract = new TestNativeContract(); VMArray args = new VMArray(); - testNativeContract.TestOnPersist(engine1, args).Should().Be(StackItem.False); + VM.Types.Boolean result1 = new VM.Types.Boolean(false); + testNativeContract.TestOnPersist(engine1, args).Should().Be(result1); ApplicationEngine engine2 = new ApplicationEngine(TriggerType.System, null, Blockchain.Singleton.GetSnapshot(), 0); - testNativeContract.TestOnPersist(engine2, args).Should().Be(StackItem.True); + VM.Types.Boolean result2 = new VM.Types.Boolean(true); + testNativeContract.TestOnPersist(engine2, args).Should().Be(result2); } [TestMethod] diff --git a/tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs b/tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs index f959a937bf..0b15d0732d 100644 --- a/tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs +++ b/tests/neo.UnitTests/SmartContract/Native/UT_PolicyContract.cs @@ -3,11 +3,11 @@ using Neo.IO; using Neo.Ledger; using Neo.Network.P2P.Payloads; +using Neo.Persistence; using Neo.SmartContract; using Neo.SmartContract.Native; using Neo.UnitTests.Extensions; using Neo.VM; -using Neo.VM.Types; using System.Linq; namespace Neo.UnitTests.SmartContract.Native @@ -67,7 +67,7 @@ public void Check_SetMaxBlockSize() var ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(null), "setMaxBlockSize", new ContractParameter(ContractParameterType.Integer) { Value = 1024 }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeFalse(); ret = NativeContract.Policy.Call(snapshot, "getMaxBlockSize"); @@ -78,7 +78,7 @@ public void Check_SetMaxBlockSize() ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(UInt160.Zero), "setMaxBlockSize", new ContractParameter(ContractParameterType.Integer) { Value = Neo.Network.P2P.Message.PayloadMaxSize }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeFalse(); ret = NativeContract.Policy.Call(snapshot, "getMaxBlockSize"); @@ -89,7 +89,7 @@ public void Check_SetMaxBlockSize() ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(UInt160.Zero), "setMaxBlockSize", new ContractParameter(ContractParameterType.Integer) { Value = 1024 }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeTrue(); ret = NativeContract.Policy.Call(snapshot, "getMaxBlockSize"); @@ -113,7 +113,7 @@ public void Check_SetMaxTransactionsPerBlock() var ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(), "setMaxTransactionsPerBlock", new ContractParameter(ContractParameterType.Integer) { Value = 1 }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeFalse(); ret = NativeContract.Policy.Call(snapshot, "getMaxTransactionsPerBlock"); @@ -124,7 +124,7 @@ public void Check_SetMaxTransactionsPerBlock() ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(UInt160.Zero), "setMaxTransactionsPerBlock", new ContractParameter(ContractParameterType.Integer) { Value = 1 }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeTrue(); ret = NativeContract.Policy.Call(snapshot, "getMaxTransactionsPerBlock"); @@ -148,7 +148,7 @@ public void Check_SetFeePerByte() var ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(), "setFeePerByte", new ContractParameter(ContractParameterType.Integer) { Value = 1 }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeFalse(); ret = NativeContract.Policy.Call(snapshot, "getFeePerByte"); @@ -159,7 +159,7 @@ public void Check_SetFeePerByte() ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(UInt160.Zero), "setFeePerByte", new ContractParameter(ContractParameterType.Integer) { Value = 1 }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeTrue(); ret = NativeContract.Policy.Call(snapshot, "getFeePerByte"); @@ -183,7 +183,7 @@ public void Check_Block_UnblockAccount() var ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(), "blockAccount", new ContractParameter(ContractParameterType.Hash160) { Value = UInt160.Zero }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeFalse(); ret = NativeContract.Policy.Call(snapshot, "getBlockedAccounts"); @@ -194,7 +194,7 @@ public void Check_Block_UnblockAccount() ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(UInt160.Zero), "blockAccount", new ContractParameter(ContractParameterType.Hash160) { Value = UInt160.Zero }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeTrue(); ret = NativeContract.Policy.Call(snapshot, "getBlockedAccounts"); @@ -206,7 +206,7 @@ public void Check_Block_UnblockAccount() ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(), "unblockAccount", new ContractParameter(ContractParameterType.Hash160) { Value = UInt160.Zero }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeFalse(); ret = NativeContract.Policy.Call(snapshot, "getBlockedAccounts"); @@ -218,7 +218,7 @@ public void Check_Block_UnblockAccount() ret = NativeContract.Policy.Call(snapshot, new Nep5NativeContractExtensions.ManualWitness(UInt160.Zero), "unblockAccount", new ContractParameter(ContractParameterType.Hash160) { Value = UInt160.Zero }); - ret.Should().BeOfType(); + ret.Should().BeOfType(); ret.ToBoolean().Should().BeTrue(); ret = NativeContract.Policy.Call(snapshot, "getBlockedAccounts"); diff --git a/tests/neo.UnitTests/SmartContract/UT_BinarySerializer.cs b/tests/neo.UnitTests/SmartContract/UT_BinarySerializer.cs index c34fdcf31d..39330334d9 100644 --- a/tests/neo.UnitTests/SmartContract/UT_BinarySerializer.cs +++ b/tests/neo.UnitTests/SmartContract/UT_BinarySerializer.cs @@ -25,13 +25,13 @@ public void TestSerialize() byte[] result2 = BinarySerializer.Serialize(true, MaxItemSize); byte[] expectedArray2 = new byte[] { - 0x20, 0x01, 0x01 + 0x20, 0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray2), Encoding.Default.GetString(result2)); byte[] result3 = BinarySerializer.Serialize(1, MaxItemSize); byte[] expectedArray3 = new byte[] { - 0x20, 0x01, 0x01 + 0x21, 0x01, 0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray3), Encoding.Default.GetString(result3)); @@ -43,7 +43,7 @@ public void TestSerialize() StackItem stackItem62 = new VM.Types.Array(list6); byte[] result6 = BinarySerializer.Serialize(stackItem62, MaxItemSize); byte[] expectedArray6 = new byte[] { - 0x40,0x01,0x20,0x01,0x01 + 0x40,0x01,0x21,0x01,0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray6), Encoding.Default.GetString(result6)); @@ -51,14 +51,14 @@ public void TestSerialize() StackItem stackItem72 = new Struct(list7); byte[] result7 = BinarySerializer.Serialize(stackItem72, MaxItemSize); byte[] expectedArray7 = new byte[] { - 0x41,0x01,0x20,0x01,0x01 + 0x41,0x01,0x21,0x01,0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray7), Encoding.Default.GetString(result7)); StackItem stackItem82 = new Map { [2] = 1 }; byte[] result8 = BinarySerializer.Serialize(stackItem82, MaxItemSize); byte[] expectedArray8 = new byte[] { - 0x48,0x01,0x20,0x01,0x02,0x20,0x01,0x01 + 0x48,0x01,0x21,0x01,0x02,0x21,0x01,0x01 }; Assert.AreEqual(Encoding.Default.GetString(expectedArray8), Encoding.Default.GetString(result8)); @@ -81,6 +81,11 @@ public void TestDeserializeStackItem() StackItem result1 = BinarySerializer.Deserialize(byteArray1, 2048, (uint)byteArray1.Length); Assert.AreEqual(stackItem1, result1); + StackItem stackItem2 = new VM.Types.Boolean(true); + byte[] byteArray2 = BinarySerializer.Serialize(stackItem2, MaxItemSize); + StackItem result2 = BinarySerializer.Deserialize(byteArray2, 2048, (uint)byteArray2.Length); + Assert.AreEqual(stackItem2, result2); + StackItem stackItem3 = new Integer(1); byte[] byteArray3 = BinarySerializer.Serialize(stackItem3, MaxItemSize); StackItem result3 = BinarySerializer.Deserialize(byteArray3, 2048, (uint)byteArray3.Length); diff --git a/tests/neo.UnitTests/SmartContract/UT_InteropService.cs b/tests/neo.UnitTests/SmartContract/UT_InteropService.cs index eb335e2619..1fa676af60 100644 --- a/tests/neo.UnitTests/SmartContract/UT_InteropService.cs +++ b/tests/neo.UnitTests/SmartContract/UT_InteropService.cs @@ -292,12 +292,12 @@ public void TestRuntime_CheckWitness() engine.CurrentContext.EvaluationStack.Push(pubkey.EncodePoint(true)); InteropService.Invoke(engine, InteropService.Runtime.CheckWitness).Should().BeTrue(); - engine.CurrentContext.EvaluationStack.Peek().GetType().Should().Be(typeof(Integer)); + engine.CurrentContext.EvaluationStack.Peek().GetType().Should().Be(typeof(Neo.VM.Types.Boolean)); engine.CurrentContext.EvaluationStack.Pop().ToBoolean().Should().Be(false); engine.CurrentContext.EvaluationStack.Push(((Transaction)engine.ScriptContainer).Sender.ToArray()); InteropService.Invoke(engine, InteropService.Runtime.CheckWitness).Should().BeTrue(); - engine.CurrentContext.EvaluationStack.Peek().GetType().Should().Be(typeof(Integer)); + engine.CurrentContext.EvaluationStack.Peek().GetType().Should().Be(typeof(Neo.VM.Types.Boolean)); engine.CurrentContext.EvaluationStack.Pop().ToBoolean().Should().Be(false); engine.CurrentContext.EvaluationStack.Push(new byte[0]); @@ -335,7 +335,7 @@ public void TestRuntime_Serialize() engine.CurrentContext.EvaluationStack.Push(100); InteropService.Invoke(engine, InteropService.Binary.Serialize).Should().BeTrue(); engine.CurrentContext.EvaluationStack.Pop().GetSpan().ToHexString() - .Should().Be(new byte[] { 0x20, 0x01, 0x64 }.ToHexString()); + .Should().Be(new byte[] { 0x21, 0x01, 0x64 }.ToHexString()); engine.CurrentContext.EvaluationStack.Push(new byte[1024 * 1024 * 2]); //Larger than MaxItemSize InteropService.Invoke(engine, InteropService.Binary.Serialize).Should().BeFalse(); diff --git a/tests/neo.UnitTests/SmartContract/UT_JsonSerializer.cs b/tests/neo.UnitTests/SmartContract/UT_JsonSerializer.cs index 076acbad42..5f2758ad36 100644 --- a/tests/neo.UnitTests/SmartContract/UT_JsonSerializer.cs +++ b/tests/neo.UnitTests/SmartContract/UT_JsonSerializer.cs @@ -265,7 +265,7 @@ public void Serialize_Array_Bool_Str_Num() var json = JsonSerializer.Serialize(entry).ToString(); - Assert.AreEqual(json, "[1,\"dGVzdA==\",123]"); + Assert.AreEqual(json, "[true,\"dGVzdA==\",123]"); } [TestMethod] @@ -294,7 +294,7 @@ public void Serialize_Array_OfArray() var json = JsonSerializer.Serialize(entry).ToString(); - Assert.AreEqual(json, "[[1,\"dGVzdDE=\",123],[1,\"dGVzdDI=\",321]]"); + Assert.AreEqual(json, "[[true,\"dGVzdDE=\",123],[true,\"dGVzdDI=\",321]]"); } [TestMethod] diff --git a/tests/neo.UnitTests/SmartContract/UT_Syscalls.cs b/tests/neo.UnitTests/SmartContract/UT_Syscalls.cs index 3f005e87aa..04f60db7ff 100644 --- a/tests/neo.UnitTests/SmartContract/UT_Syscalls.cs +++ b/tests/neo.UnitTests/SmartContract/UT_Syscalls.cs @@ -182,7 +182,7 @@ public void Json_Serialize() Assert.IsTrue(engine.ResultStack.TryPop(out var m) && m.GetString() == "{\"key\":\"dmFsdWU=\"}"); Assert.IsTrue(engine.ResultStack.TryPop(out var n) && n.GetString() == "null"); Assert.IsTrue(engine.ResultStack.TryPop(out var s) && s.GetString() == "\"dGVzdA==\""); - Assert.IsTrue(engine.ResultStack.TryPop(out var b) && b.GetString() == "1"); + Assert.IsTrue(engine.ResultStack.TryPop(out var b) && b.GetString() == "true"); Assert.IsTrue(engine.ResultStack.TryPop(out var i) && i.GetString() == "5"); } } diff --git a/tests/neo.UnitTests/VM/UT_Helper.cs b/tests/neo.UnitTests/VM/UT_Helper.cs index d37e3de5d0..809e00c94d 100644 --- a/tests/neo.UnitTests/VM/UT_Helper.cs +++ b/tests/neo.UnitTests/VM/UT_Helper.cs @@ -107,7 +107,7 @@ public void TestToParameter() Assert.AreEqual(30000000000000L, (long)new BigInteger(byteItem.ToParameter().Value as byte[])); StackItem boolItem = false; - Assert.AreEqual(BigInteger.Zero, (BigInteger)boolItem.ToParameter().Value); + Assert.AreEqual(false, (bool)boolItem.ToParameter().Value); StackItem intItem = new BigInteger(1000); Assert.AreEqual(1000, (BigInteger)intItem.ToParameter().Value); @@ -482,6 +482,7 @@ public void TestToParameter2() { TestToParaMeter2VMArray(); TestToParameter2Map(); + TestToParameter2VMBoolean(); TestToParameter2ByteArray(); TestToParameter2Integer(); TestToParameter2InteropInterface(); @@ -513,6 +514,14 @@ private void TestToParameter2ByteArray() Assert.AreEqual(Encoding.Default.GetString(new byte[] { 0x00 }), Encoding.Default.GetString((byte[])parameter.Value)); } + private void TestToParameter2VMBoolean() + { + StackItem item = new VM.Types.Boolean(true); + ContractParameter parameter = VM.Helper.ToParameter(item); + Assert.AreEqual(ContractParameterType.Boolean, parameter.Type); + Assert.AreEqual(true, parameter.Value); + } + private void TestToParameter2Map() { StackItem item = new VM.Types.Map();