Skip to content

Commit

Permalink
[Neo VM: FIX] the GetString() methods of bytestring requires strictUT…
Browse files Browse the repository at this point in the history
…F8 (#3110)

* the GetString() methods of bytestring requires strictUTF8, but we can set it differently.

* update UTF8 rule and add UT

* add comment and prefix to base64 string

* fix UT

* Rename ToString to Print and move it to the unit test where it is needed. As it is a test method and only unit test will need it.

* Update tests/Neo.VM.Tests/UtEvaluationStack.cs

* Update src/Neo.VM/Utility.cs

* Update tests/Neo.VM.Tests/UtEvaluationStack.cs

* move try string to vm test

* Centralize print

* fix UT

* fix base64 format

* Override ToString

* Clean using

---------

Co-authored-by: Shargon <shargon@gmail.com>
  • Loading branch information
Jim8y and shargon authored Feb 8, 2024
1 parent 76c368e commit 3e52756
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 21 deletions.
25 changes: 5 additions & 20 deletions src/Neo.VM/EvaluationStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,6 @@ internal EvaluationStack(ReferenceCounter referenceCounter)
/// </summary>
public int Count => innerList.Count;

public override string ToString()
{
return $@"[{string.Join(", ", innerList.Select(p =>
{
var value = p.Type switch
{
StackItemType.Pointer => $"({((Pointer)p).Position})",
StackItemType.Boolean => $"({p.GetBoolean()})",
StackItemType.Integer => $"({p.GetInteger()})",
StackItemType.ByteString => $"(\"{p.GetString()}\")",
StackItemType.Array
or StackItemType.Map
or StackItemType.Struct => $"({((CompoundType)p).Count})",
_ => ""
};
return $"{p.Type.ToString()}{value}";
}
))}]";
}

internal void Clear()
{
foreach (StackItem item in innerList)
Expand Down Expand Up @@ -179,5 +159,10 @@ internal T Remove<T>(int index) where T : StackItem
referenceCounter.RemoveStackReference(item);
return item;
}

public override string ToString()
{
return $"[{string.Join(", ", innerList.Select(p => $"{p.Type}({p})"))}]";
}
}
}
5 changes: 5 additions & 0 deletions src/Neo.VM/Types/Boolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,10 @@ public static implicit operator Boolean(bool value)
{
return value ? True : False;
}

public override string ToString()
{
return value.ToString();
}
}
}
5 changes: 5 additions & 0 deletions src/Neo.VM/Types/Buffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,10 @@ public override ReadOnlySpan<byte> GetSpan()
{
return InnerBuffer.Span;
}

public override string ToString()
{
return GetSpan().TryGetString(out var str) ? $"(\"{str}\")" : $"(\"Base64: {Convert.ToBase64String(GetSpan())}\")";
}
}
}
5 changes: 5 additions & 0 deletions src/Neo.VM/Types/ByteString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,10 @@ public static implicit operator ByteString(string value)
{
return new ByteString(Utility.StrictUTF8.GetBytes(value));
}

public override string ToString()
{
return GetSpan().TryGetString(out var str) ? $"\"{str}\"" : $"\"Base64: {Convert.ToBase64String(GetSpan())}\"";
}
}
}
5 changes: 5 additions & 0 deletions src/Neo.VM/Types/CompoundType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,10 @@ public override int GetHashCode()
{
throw new NotSupportedException();
}

public override string ToString()
{
return Count.ToString();
}
}
}
5 changes: 5 additions & 0 deletions src/Neo.VM/Types/Integer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,10 @@ public static implicit operator Integer(BigInteger value)
{
return new Integer(value);
}

public override string ToString()
{
return value.ToString();
}
}
}
5 changes: 5 additions & 0 deletions src/Neo.VM/Types/InteropInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,10 @@ public override T GetInterface<T>()
if (_object is T t) return t;
throw new InvalidCastException($"The item can't be casted to type {typeof(T)}");
}

public override string ToString()
{
return _object.ToString() ?? "NULL";
}
}
}
5 changes: 5 additions & 0 deletions src/Neo.VM/Types/Null.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,10 @@ public override T GetInterface<T>()
{
return null;
}

public override string ToString()
{
return "NULL";
}
}
}
5 changes: 5 additions & 0 deletions src/Neo.VM/Types/Pointer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,10 @@ public override int GetHashCode()
{
return HashCode.Combine(Script, Position);
}

public override string ToString()
{
return Position.ToString();
}
}
}
14 changes: 14 additions & 0 deletions src/Neo.VM/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ static Utility()
StrictUTF8.EncoderFallback = EncoderFallback.ExceptionFallback;
}

public static bool TryGetString(this ReadOnlySpan<byte> bytes, out string? value)
{
try
{
value = StrictUTF8.GetString(bytes);
return true;
}
catch
{
value = default;
return false;
}
}

public static BigInteger ModInverse(this BigInteger value, BigInteger modulus)
{
if (value <= 0) throw new ArgumentOutOfRangeException(nameof(value));
Expand Down
12 changes: 11 additions & 1 deletion tests/Neo.VM.Tests/UT_EvaluationStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
// modifications are permitted.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Test.Extensions;
using Neo.Test.Helpers;
using Neo.VM;
using Neo.VM.Types;
using System;
Expand Down Expand Up @@ -200,7 +202,7 @@ public void TestReverse()
}

[TestMethod]
public void TestToString()
public void TestEvaluationStackPrint()
{
var stack = new EvaluationStack(new ReferenceCounter());

Expand All @@ -211,5 +213,13 @@ public void TestToString()

Assert.AreEqual("[Boolean(True), ByteString(\"test\"), Integer(1), Integer(3)]", stack.ToString());
}

[TestMethod]
public void TestPrintInvalidUTF8()
{
var stack = new EvaluationStack(new ReferenceCounter());
stack.Insert(0, "4CC95219999D421243C8161E3FC0F4290C067845".FromHexString());
Assert.AreEqual("[ByteString(\"Base64: TMlSGZmdQhJDyBYeP8D0KQwGeEU=\")]", stack.ToString());
}
}
}

0 comments on commit 3e52756

Please sign in to comment.