Skip to content

Commit

Permalink
Merge branch 'nullable' of github.com:Jim8y/neo into nullable
Browse files Browse the repository at this point in the history
* 'nullable' of github.com:Jim8y/neo:
  [Neo Fix] fix the store crash issue (neo-project#3124)
  [Neo VM: FIX] the GetString() methods of bytestring requires strictUTF8 (neo-project#3110)
  • Loading branch information
Jim8y committed Feb 9, 2024
2 parents ba7b50d + 921010c commit 103ad43
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 35 deletions.
3 changes: 1 addition & 2 deletions src/Neo.CLI/CLI/MainService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,7 @@ public async void Start(CommandLineOptions options)
ProtocolSettings protocol = ProtocolSettings.Load("config.json");
CustomProtocolSettings(options, protocol);
CustomApplicationSettings(options, Settings.Default);
var store = StoreFactory.GetStore(Settings.Default.Storage.Engine, string.Format(Settings.Default.Storage.Path, protocol.Network.ToString("X8")));
NeoSystem = new NeoSystem(protocol, store);
NeoSystem = new NeoSystem(protocol, Settings.Default.Storage.Engine, string.Format(Settings.Default.Storage.Path, protocol.Network.ToString("X8")));
NeoSystem.AddService(this);

LocalNode = NeoSystem.LocalNode.Ask<LocalNode>(new LocalNode.GetInstance()).Result;
Expand Down
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
20 changes: 10 additions & 10 deletions src/Neo/NeoSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ static NeoSystem()
Plugin.LoadPlugins();
}

/// <summary>
/// Initializes a new instance of the <see cref="NeoSystem"/> class.
/// </summary>
/// <param name="settings">The protocol settings of the <see cref="NeoSystem"/>.</param>
/// <param name="storageEngine">The storage engine used to create the <see cref="IStore"/> objects. If this parameter is <see langword="null"/>, a default in-memory storage engine will be used.</param>
/// <param name="storagePath">The path of the storage. If <paramref name="storageEngine"/> is the default in-memory storage engine, this parameter is ignored.</param>
public NeoSystem(ProtocolSettings settings, string? storageEngine = null, string? storagePath = null) : this(settings, StoreFactory.GetStore(storageEngine ?? nameof(MemoryStore), storagePath))
{
}

/// <summary>
/// Initializes a new instance of the <see cref="NeoSystem"/> class.
/// </summary>
Expand Down Expand Up @@ -208,16 +218,6 @@ public void EnsureStopped(IActorRef actor)
inbox.Receive(TimeSpan.FromMinutes(5));
}

/// <summary>
/// Loads an <see cref="IStore"/> at the specified path.
/// </summary>
/// <param name="path">The path of the storage.</param>
/// <returns>The loaded <see cref="IStore"/>.</returns>
public IStore LoadStore(string path)
{
return StoreFactory.GetStore(store.GetType().Name, path);
}

/// <summary>
/// Resumes the startup process of <see cref="LocalNode"/>.
/// </summary>
Expand Down
2 changes: 0 additions & 2 deletions src/Neo/Persistence/StoreFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ static StoreFactory()
RegisterProvider(memProvider);

// Default cases

providers.Add("", memProvider);
providers.Add(null, memProvider);
}

public static void RegisterProvider(IStoreProvider provider)
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 103ad43

Please sign in to comment.