From 88a73faa7e7ad5dc084271aa16ce57bfdcf8179b Mon Sep 17 00:00:00 2001 From: Luchuan Date: Thu, 16 Apr 2020 14:55:46 +0800 Subject: [PATCH 1/2] 3.0.0-preview2 --- neo-cli/neo-cli.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/neo-cli/neo-cli.csproj b/neo-cli/neo-cli.csproj index ec4dc82a6..cacdbaef5 100644 --- a/neo-cli/neo-cli.csproj +++ b/neo-cli/neo-cli.csproj @@ -3,7 +3,7 @@ 2016-2019 The Neo Project Neo.CLI - 3.0.0-preview1 + 3.0.0-preview2 The Neo Project netcoreapp3.0 neo-cli @@ -28,7 +28,7 @@ - + From d53c70d2856c0e08df82f1467615c37fcac2dc19 Mon Sep 17 00:00:00 2001 From: Luchuan Date: Thu, 16 Apr 2020 15:02:56 +0800 Subject: [PATCH 2/2] fix StackItem.ToJson --- neo-cli/CLI/Helper.cs | 56 ++++++++++++++++++++++++ neo-cli/CLI/MainService.Contracts.cs | 2 +- neo-cli/CLI/ReferenceEqualityComparer.cs | 24 ++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 neo-cli/CLI/ReferenceEqualityComparer.cs diff --git a/neo-cli/CLI/Helper.cs b/neo-cli/CLI/Helper.cs index 3579f57d7..b40e51e19 100644 --- a/neo-cli/CLI/Helper.cs +++ b/neo-cli/CLI/Helper.cs @@ -1,3 +1,13 @@ +using Neo.IO.Json; +using Neo.SmartContract; +using Neo.VM.Types; +using System; +using System.Collections.Generic; +using System.Linq; +using Array = Neo.VM.Types.Array; +using Boolean = Neo.VM.Types.Boolean; +using Buffer = Neo.VM.Types.Buffer; + namespace Neo.CLI { internal static class Helper @@ -19,5 +29,51 @@ public static bool IsYes(this string input) return input == "yes" || input == "y"; } + + public static JObject ToJson(this StackItem item) + { + return ToJson(item, null); + } + + private static JObject ToJson(StackItem item, HashSet context) + { + JObject json = new JObject(); + json["type"] = item.Type; + switch (item) + { + case Array array: + context ??= new HashSet(ReferenceEqualityComparer.Default); + if (!context.Add(array)) throw new InvalidOperationException(); + json["value"] = new JArray(array.Select(p => ToJson(p, context))); + break; + case Boolean boolean: + json["value"] = boolean.ToBoolean(); + break; + case Buffer buffer: + json["value"] = Convert.ToBase64String(buffer.InnerBuffer); + break; + case ByteString byteString: + json["value"] = Convert.ToBase64String(byteString.Span); + break; + case Integer integer: + json["value"] = integer.ToBigInteger().ToString(); + break; + case Map map: + context ??= new HashSet(ReferenceEqualityComparer.Default); + if (!context.Add(map)) throw new InvalidOperationException(); + json["value"] = new JArray(map.Select(p => + { + JObject item = new JObject(); + item["key"] = ToJson(p.Key, context); + item["value"] = ToJson(p.Value, context); + return item; + })); + break; + case Pointer pointer: + json["value"] = pointer.Position; + break; + } + return json; + } } } diff --git a/neo-cli/CLI/MainService.Contracts.cs b/neo-cli/CLI/MainService.Contracts.cs index f3f4f3d56..b7ab38503 100644 --- a/neo-cli/CLI/MainService.Contracts.cs +++ b/neo-cli/CLI/MainService.Contracts.cs @@ -104,7 +104,7 @@ private void OnInvokeCommand(UInt160 scriptHash, string operation, JArray contra { Console.WriteLine($"VM State: {engine.State}"); Console.WriteLine($"Gas Consumed: {new BigDecimal(engine.GasConsumed, NativeContract.GAS.Decimals)}"); - Console.WriteLine($"Evaluation Stack: {new JArray(engine.ResultStack.Select(p => p.ToParameter().ToJson()))}"); + Console.WriteLine($"Evaluation Stack: {new JArray(engine.ResultStack.Select(p => p.ToJson()))}"); Console.WriteLine(); if (engine.State.HasFlag(VMState.FAULT)) { diff --git a/neo-cli/CLI/ReferenceEqualityComparer.cs b/neo-cli/CLI/ReferenceEqualityComparer.cs new file mode 100644 index 000000000..c4641fc50 --- /dev/null +++ b/neo-cli/CLI/ReferenceEqualityComparer.cs @@ -0,0 +1,24 @@ +using System.Collections; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +namespace Neo.CLI +{ + internal sealed class ReferenceEqualityComparer : IEqualityComparer, IEqualityComparer + { + public static readonly ReferenceEqualityComparer Default = new ReferenceEqualityComparer(); + + private ReferenceEqualityComparer() + { + } + + public new bool Equals(object x, object y) + { + return x == y; + } + + public int GetHashCode(object obj) + { + return RuntimeHelpers.GetHashCode(obj); + } + } +}