Skip to content

Commit

Permalink
add txid to messagepack
Browse files Browse the repository at this point in the history
  • Loading branch information
area363 committed Dec 21, 2023
1 parent b606e21 commit 80b5f9a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .Lib9c.Tests/Action/ActionEvaluationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public void Serialize_With_MessagePack(Type actionType)
null,
_states.Trie.Hash,
0,
new Dictionary<string, IValue>()
new Dictionary<string, IValue>(),
null
);
var evaluation = ncEval.ToActionEvaluation();
var b = MessagePackSerializer.Serialize(ncEval);
Expand Down
12 changes: 10 additions & 2 deletions Lib9c.MessagePack/Action/NCActionEvaluation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Lib9c.Renderers;
using Libplanet.Crypto;
using Libplanet.Common;
using Libplanet.Types.Tx;
using MessagePack;

namespace Nekoyume.Action
Expand All @@ -16,6 +17,7 @@ public struct NCActionEvaluation
{
#pragma warning disable MsgPack003
[Key(0)]

[MessagePackFormatter(typeof(NCActionFormatter))]
public ActionBase? Action { get; set; }

Expand Down Expand Up @@ -45,6 +47,9 @@ public struct NCActionEvaluation
[Key(7)]
public Dictionary<string, IValue> Extra { get; set; }

[Key(8)]
[MessagePackFormatter(typeof(TxIdFormatter))]
public TxId? TxId { get; set; }

[SerializationConstructor]
public NCActionEvaluation(
Expand All @@ -55,7 +60,8 @@ public NCActionEvaluation(
Exception? exception,
HashDigest<SHA256> previousStates,
int randomSeed,
Dictionary<string, IValue> extra
Dictionary<string, IValue> extra,
TxId? txId
)
{
Action = action;
Expand All @@ -66,6 +72,7 @@ Dictionary<string, IValue> extra
PreviousState = previousStates;
RandomSeed = randomSeed;
Extra = extra;
TxId = txId;
}

public ActionEvaluation<ActionBase> ToActionEvaluation()
Expand All @@ -79,7 +86,8 @@ public ActionEvaluation<ActionBase> ToActionEvaluation()
Exception = Exception,
PreviousState = PreviousState,
RandomSeed = RandomSeed,
Extra = Extra
Extra = Extra,
TxId = TxId
};
}
}
Expand Down
41 changes: 41 additions & 0 deletions Lib9c.MessagePack/Formatters/TxIdFormatter.cs
Original file line number Diff line number Diff line change
@@ -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<TxId?>
{
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());
}
}
}

0 comments on commit 80b5f9a

Please sign in to comment.