From 222c235d6b4b1d8fbca9804dd35c8e5b56d43e8a Mon Sep 17 00:00:00 2001 From: eugene-hong <58686228+eugene-doobu@users.noreply.github.com> Date: Mon, 4 Nov 2024 21:07:50 +0900 Subject: [PATCH 1/3] add currency formatter --- .../Formatters/CurrencyFormatter.cs | 42 +++++++++++++++++++ ...ineChroniclesResolverGetFormatterHelper.cs | 4 +- 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 Lib9c.MessagePack/Formatters/CurrencyFormatter.cs diff --git a/Lib9c.MessagePack/Formatters/CurrencyFormatter.cs b/Lib9c.MessagePack/Formatters/CurrencyFormatter.cs new file mode 100644 index 0000000000..d0815927fc --- /dev/null +++ b/Lib9c.MessagePack/Formatters/CurrencyFormatter.cs @@ -0,0 +1,42 @@ +using System; +using System.Buffers; +using MessagePack; +using Libplanet.Types.Assets; +using MessagePack.Formatters; +using Bencodex; + +namespace Lib9c.Formatters +{ + public class CurrencyFormatter : IMessagePackFormatter + { + public void Serialize(ref MessagePackWriter writer, Currency value, MessagePackSerializerOptions options) + { + if (value.Equals(default)) + { + writer.WriteNil(); + return; + } + + writer.Write(new Codec().Encode(value.Serialize())); + } + + public Currency Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) + { + if (reader.TryReadNil()) + { + return default; + } + + options.Security.DepthStep(ref reader); + + var bytes = reader.ReadBytes()?.ToArray(); + if (bytes is null) + { + throw new InvalidOperationException(); + } + + var currency = new Currency(new Codec().Decode(bytes)); + return new Currency(new Codec().Decode(bytes)); + } + } +} diff --git a/Lib9c.MessagePack/Formatters/NineChroniclesResolverGetFormatterHelper.cs b/Lib9c.MessagePack/Formatters/NineChroniclesResolverGetFormatterHelper.cs index c4ae026642..be300fa8b4 100644 --- a/Lib9c.MessagePack/Formatters/NineChroniclesResolverGetFormatterHelper.cs +++ b/Lib9c.MessagePack/Formatters/NineChroniclesResolverGetFormatterHelper.cs @@ -5,6 +5,7 @@ using Libplanet.Types.Assets; using MessagePack.Formatters; using Nekoyume.Action; +using Nekoyume.Model.State; namespace Lib9c.Formatters { @@ -19,7 +20,8 @@ public static class NineChroniclesResolverGetFormatterHelper {typeof(PublicKey), new PublicKeyFormatter()}, {typeof(Dictionary), new BencodexFormatter()}, {typeof(IValue), new BencodexFormatter()}, - {typeof(ActionBase), new NCActionFormatter()} + {typeof(ActionBase), new NCActionFormatter()}, + {typeof(Currency), new CurrencyFormatter()}, // add more your own custom serializers. }; From 7e2664eb02b6d5854d79fee708e75015458f32ae Mon Sep 17 00:00:00 2001 From: eugene-hong <58686228+eugene-doobu@users.noreply.github.com> Date: Mon, 4 Nov 2024 21:07:59 +0900 Subject: [PATCH 2/3] fix InvalidSignatureException --- Lib9c/Action/InvalidSignatureException.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib9c/Action/InvalidSignatureException.cs b/Lib9c/Action/InvalidSignatureException.cs index 5bdabd57be..d44f5bf9e2 100644 --- a/Lib9c/Action/InvalidSignatureException.cs +++ b/Lib9c/Action/InvalidSignatureException.cs @@ -34,7 +34,8 @@ StreamingContext context ) { base.GetObjectData(info, context); - info.AddValue(nameof(Pending), new Codec().Encode(Pending.Serialize())); + var pendingData = Pending is null ? null : new Codec().Encode(Pending.Serialize()); + info.AddValue(nameof(Pending), pendingData); info.AddValue(nameof(Signature), Signature); } } From 64248f23b26e74d7904d92572653637fe03ff2bf Mon Sep 17 00:00:00 2001 From: eugene-hong <58686228+eugene-doobu@users.noreply.github.com> Date: Tue, 5 Nov 2024 10:25:14 +0900 Subject: [PATCH 3/3] remove redundant line --- Lib9c.MessagePack/Formatters/CurrencyFormatter.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib9c.MessagePack/Formatters/CurrencyFormatter.cs b/Lib9c.MessagePack/Formatters/CurrencyFormatter.cs index d0815927fc..0a090da5f7 100644 --- a/Lib9c.MessagePack/Formatters/CurrencyFormatter.cs +++ b/Lib9c.MessagePack/Formatters/CurrencyFormatter.cs @@ -35,7 +35,6 @@ public Currency Deserialize(ref MessagePackReader reader, MessagePackSerializerO throw new InvalidOperationException(); } - var currency = new Currency(new Codec().Decode(bytes)); return new Currency(new Codec().Decode(bytes)); } }