diff --git a/.Lib9c.Tests/Action/ActionEvaluationTest.cs b/.Lib9c.Tests/Action/ActionEvaluationTest.cs index 4cb2dbcfa3..1f2b132505 100644 --- a/.Lib9c.Tests/Action/ActionEvaluationTest.cs +++ b/.Lib9c.Tests/Action/ActionEvaluationTest.cs @@ -104,7 +104,8 @@ public void Serialize_With_MessagePack(Type actionType) null, _states.Trie.Hash, 0, - new Dictionary() + new Dictionary(), + null ); var evaluation = ncEval.ToActionEvaluation(); var b = MessagePackSerializer.Serialize(ncEval); diff --git a/Lib9c.MessagePack/Action/NCActionEvaluation.cs b/Lib9c.MessagePack/Action/NCActionEvaluation.cs index 46e4b347d6..35f2b94a68 100644 --- a/Lib9c.MessagePack/Action/NCActionEvaluation.cs +++ b/Lib9c.MessagePack/Action/NCActionEvaluation.cs @@ -7,6 +7,7 @@ using Lib9c.Renderers; using Libplanet.Crypto; using Libplanet.Common; +using Libplanet.Types.Tx; using MessagePack; namespace Nekoyume.Action @@ -16,6 +17,7 @@ public struct NCActionEvaluation { #pragma warning disable MsgPack003 [Key(0)] + [MessagePackFormatter(typeof(NCActionFormatter))] public ActionBase? Action { get; set; } @@ -45,6 +47,9 @@ public struct NCActionEvaluation [Key(7)] public Dictionary Extra { get; set; } + [Key(8)] + [MessagePackFormatter(typeof(TxIdFormatter))] + public TxId? TxId { get; set; } [SerializationConstructor] public NCActionEvaluation( @@ -55,7 +60,8 @@ public NCActionEvaluation( Exception? exception, HashDigest previousStates, int randomSeed, - Dictionary extra + Dictionary extra, + TxId? txId ) { Action = action; @@ -66,6 +72,7 @@ Dictionary extra PreviousState = previousStates; RandomSeed = randomSeed; Extra = extra; + TxId = txId; } public ActionEvaluation ToActionEvaluation() @@ -79,7 +86,8 @@ public ActionEvaluation ToActionEvaluation() Exception = Exception, PreviousState = PreviousState, RandomSeed = RandomSeed, - Extra = Extra + Extra = Extra, + TxId = TxId }; } } diff --git a/Lib9c.MessagePack/Formatters/TxIdFormatter.cs b/Lib9c.MessagePack/Formatters/TxIdFormatter.cs new file mode 100644 index 0000000000..1bc8f30fe9 --- /dev/null +++ b/Lib9c.MessagePack/Formatters/TxIdFormatter.cs @@ -0,0 +1,41 @@ +using System; +using System.Buffers; +using System.Collections.Immutable; +using Libplanet.Types.Tx; +using MessagePack; +using MessagePack.Formatters; + +namespace Lib9c.Formatters +{ + public class TxIdFormatter : IMessagePackFormatter + { + public void Serialize(ref MessagePackWriter writer, TxId? value, MessagePackSerializerOptions options) + { + if (value is null) + { + writer.WriteNil(); + return; + } + + writer.Write(value.Value.ToByteArray()); + } + + public TxId? 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(); + } + + return new TxId(bytes.ToImmutableArray()); + } + } +}