Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/fix msgpack exception formatter #2963

Merged
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
22 changes: 22 additions & 0 deletions .Lib9c.Tests/Action/ExceptionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,28 @@ public void Exception_Serializable(Type excType)
}
}

/// <summary>
/// Libplanet Exception을 수정하기 위한 임시 테스트 코드입니다
/// TODO: Libplanet Exception을 수정하고 테스트 코드 케이스를 추가해야 합니다
/// </summary>
/// <param name="excType">예외타입.</param>
[Theory]
[InlineData(typeof(Libplanet.Action.State.InsufficientBalanceException))]
public void Libplanet_Exception_Serializable(Type excType)
{
// TODO: 테스트 받는 방식 수정
var customAddress = new Address("399bddF9F7B6d902ea27037B907B2486C9910730");
var customFav = new Libplanet.Types.Assets.FungibleAssetValue(Currencies.Crystal);
if (Activator.CreateInstance(excType, "for testing", customAddress, customFav) is Exception exc)
{
AssertException(excType, exc);
}
else
{
throw new InvalidCastException();
}
}

[Fact(Skip = "FIXME: Cannot serialize AdminState with MessagePackSerializer")]
public void AdminPermissionExceptionSerializable()
{
Expand Down
44 changes: 31 additions & 13 deletions Lib9c.MessagePack/Formatters/ExceptionFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

namespace Lib9c.Formatters
{
// FIXME: This class must be removed and replaced with other way for serialization.
// https://github.com/dotnet/designs/blob/main/accepted/2020/better-obsoletion/binaryformatter-obsoletion.md
public class ExceptionFormatter<T> : IMessagePackFormatter<T?> where T : Exception
{
public void Serialize(ref MessagePackWriter writer, T? value,
Expand Down Expand Up @@ -55,43 +53,43 @@ public void Serialize(ref MessagePackWriter writer, T? value,
for (int i = 0; i < count; i++)
{
var name = reader.ReadString();
if (name == null)
if (string.IsNullOrWhiteSpace(name))
{
throw new MessagePackSerializationException("Exception Name is missing.");
}

if (name == "ExceptionType")
{
typeName = reader.ReadString();
}
else
{
var readType = reader.ReadString();
if (readType == null)
if (string.IsNullOrWhiteSpace(readType))
{
throw new MessagePackSerializationException("Exception type information is missing.");
}
var type = Type.GetType(readType);

var type = GetType(readType);
if (type == null)
{
throw new MessagePackSerializationException("Exception type information is missing.");
{
throw new MessagePackSerializationException($"Exception type cannot be found for '{readType}'.");
}

var value = MessagePackSerializer.Deserialize(type, ref reader, options);
info.AddValue(name, value);
}
}

if (typeName == null)
if (string.IsNullOrWhiteSpace(typeName))
{
throw new MessagePackSerializationException("Exception type information is missing.");
throw new MessagePackSerializationException("Exception exception type name is missing.");
}

var exceptionType = Type.GetType(typeName);
if (exceptionType == null)
{
throw new MessagePackSerializationException($"Exception type '{typeName}' not found.");
throw new MessagePackSerializationException($"Exception exception type cannot be found for '{typeName}'.");
}

var ctor = exceptionType.GetConstructor(
Expand Down Expand Up @@ -137,5 +135,25 @@ public void Serialize(ref MessagePackWriter writer, T? value,

return exception;
}

private Type? GetType(string typeName)
{
var type = Type.GetType(typeName);
if (type != null)
{
return type;
}

foreach (var assembly in NineChroniclesResolverGetFormatterHelper.GetAssemblies())
{
type = assembly.GetType(typeName);
if (type != null)
{
return type;
}
}

return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Collections.Generic;
using Bencodex.Types;
using Libplanet.Crypto;
Expand All @@ -8,6 +9,7 @@

namespace Lib9c.Formatters
{

public static class NineChroniclesResolverGetFormatterHelper
{
// If type is concrete type, use type-formatter map
Expand All @@ -19,7 +21,7 @@ public static class NineChroniclesResolverGetFormatterHelper
{typeof(PublicKey), new PublicKeyFormatter()},
{typeof(Dictionary), new BencodexFormatter<Dictionary>()},
{typeof(IValue), new BencodexFormatter<IValue>()},
{typeof(ActionBase), new NCActionFormatter()}
{typeof(ActionBase), new NCActionFormatter()},
// add more your own custom serializers.
};

Expand All @@ -39,5 +41,12 @@ public static class NineChroniclesResolverGetFormatterHelper
// If type can not get, must return null for fallback mechanism.
return null;
}

public static System.Reflection.Assembly[] GetAssemblies() => _assemblies ??= FormatterMap.Keys
.Select(t => t.Assembly)
.Distinct()
.ToArray();

private static System.Reflection.Assembly[]? _assemblies = null;
}
}
Loading