Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

3.0.0-preview2 #569

Closed
wants to merge 2 commits into from
Closed
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
56 changes: 56 additions & 0 deletions neo-cli/CLI/Helper.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,5 +29,51 @@ public static bool IsYes(this string input)

return input == "yes" || input == "y";
}

public static JObject ToJson(this StackItem item)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't move this to neo it's duplicated in neo-modules, and neo-node

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was fixed in neo-project/neo#1569, but not merged in neo-preview2.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AQtk8ckQwnZTowVgs6zgCZEJyUQyM1z3ew

{
return ToJson(item, null);
}

private static JObject ToJson(StackItem item, HashSet<StackItem> context)
{
JObject json = new JObject();
json["type"] = item.Type;
switch (item)
{
case Array array:
context ??= new HashSet<StackItem>(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<StackItem>(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;
}
}
}
2 changes: 1 addition & 1 deletion neo-cli/CLI/MainService.Contracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
24 changes: 24 additions & 0 deletions neo-cli/CLI/ReferenceEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Neo.CLI
{
internal sealed class ReferenceEqualityComparer : IEqualityComparer, IEqualityComparer<object>
{
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);
}
}
}
4 changes: 2 additions & 2 deletions neo-cli/neo-cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Copyright>2016-2019 The Neo Project</Copyright>
<AssemblyTitle>Neo.CLI</AssemblyTitle>
<Version>3.0.0-preview1</Version>
<Version>3.0.0-preview2</Version>
<Authors>The Neo Project</Authors>
<TargetFramework>netcoreapp3.0</TargetFramework>
<AssemblyName>neo-cli</AssemblyName>
Expand All @@ -28,7 +28,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Neo" Version="3.0.0-CI00884" />
<PackageReference Include="Neo" Version="3.0.0-preview2-00" />
</ItemGroup>

<ItemGroup>
Expand Down