Skip to content

Commit 034c8ab

Browse files
committed
Funneled ISerializable serialization to a private constructor
1 parent 318be87 commit 034c8ab

File tree

1 file changed

+4
-77
lines changed

1 file changed

+4
-77
lines changed

src/Libplanet.Types/Assets/Currency.cs

Lines changed: 4 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Collections.Immutable;
43
using System.Diagnostics.CodeAnalysis;
54
using System.Diagnostics.Contracts;
@@ -13,7 +12,6 @@
1312
using Bencodex;
1413
using Bencodex.Types;
1514
using Libplanet.Common;
16-
using Libplanet.Common.Serialization;
1715
using Libplanet.Crypto;
1816

1917
namespace Libplanet.Types.Assets
@@ -51,6 +49,8 @@ namespace Libplanet.Types.Assets
5149
[Serializable]
5250
public sealed class Currency : IEquatable<Currency>, ISerializable
5351
{
52+
private static Codec _codec = new Codec();
53+
5454
private readonly (BigInteger Major, BigInteger Minor)? _maximumSupply;
5555

5656
private HashDigest<SHA1>? _hash;
@@ -151,65 +151,8 @@ private Currency(Dictionary dictionary)
151151
}
152152

153153
private Currency(SerializationInfo info, StreamingContext context)
154+
: this(_codec.Decode((byte[])info.GetValue("ByteEncoded", typeof(byte[]))!))
154155
{
155-
Ticker = info.GetValue<string>(nameof(Ticker));
156-
DecimalPlaces = info.GetValue<byte>(nameof(DecimalPlaces));
157-
158-
if (info.TryGetValue(nameof(Minters), out List<byte[]> minters))
159-
{
160-
Minters = minters.Select(m => new Address(m)).ToImmutableHashSet();
161-
}
162-
else
163-
{
164-
Minters = default;
165-
}
166-
167-
TotalSupplyTrackable = false;
168-
if (info.TryGetValue(nameof(TotalSupplyTrackable), out bool totalSupplyTrackable))
169-
{
170-
if (!totalSupplyTrackable)
171-
{
172-
throw new ArgumentException(
173-
$"{nameof(TotalSupplyTrackable)} must be true if it exists in the"
174-
+ "SerializationInfo.",
175-
nameof(TotalSupplyTrackable));
176-
}
177-
178-
TotalSupplyTrackable = totalSupplyTrackable;
179-
}
180-
181-
if (info.TryGetValue(nameof(MaximumSupply), out (BigInteger, BigInteger) maximumSupply))
182-
{
183-
if (!TotalSupplyTrackable)
184-
{
185-
throw new ArgumentException(
186-
$"Maximum supply is not available for legacy untracked currencies.",
187-
nameof(info));
188-
}
189-
190-
_maximumSupply = maximumSupply;
191-
}
192-
else
193-
{
194-
_maximumSupply = null;
195-
}
196-
197-
if (_maximumSupply is var (major, minor))
198-
{
199-
if (major < 0 || minor < 0)
200-
{
201-
var msg = $"Both the major ({major}) and minor ({minor}) units of"
202-
+ $" {nameof(maximumSupply)} must not be a negative number.";
203-
throw new ArgumentException(msg, nameof(maximumSupply));
204-
}
205-
206-
if (minor > 0 && Math.Floor(BigInteger.Log10(minor)) >= DecimalPlaces)
207-
{
208-
var msg = $"The given minor unit {minor} of the maximum supply value is too"
209-
+ $" big for the given decimal places {DecimalPlaces}.";
210-
throw new ArgumentException(msg, nameof(minor));
211-
}
212-
}
213156
}
214157

215158
/// <summary>
@@ -455,23 +398,7 @@ public static Currency Legacy(
455398
/// <inheritdoc cref="ISerializable.GetObjectData(SerializationInfo, StreamingContext)"/>
456399
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
457400
{
458-
info.AddValue(nameof(Ticker), Ticker);
459-
info.AddValue(nameof(DecimalPlaces), DecimalPlaces);
460-
461-
if (Minters is IImmutableSet<Address> minters)
462-
{
463-
info.AddValue(nameof(Minters), minters.Select(m => m.ToByteArray()).ToList());
464-
}
465-
466-
if (_maximumSupply is { } maximumSupply)
467-
{
468-
info.AddValue(nameof(MaximumSupply), maximumSupply);
469-
}
470-
471-
if (TotalSupplyTrackable)
472-
{
473-
info.AddValue(nameof(TotalSupplyTrackable), TotalSupplyTrackable);
474-
}
401+
info.AddValue("ByteEncoded", _codec.Encode(Serialize()));
475402
}
476403

477404
/// <inheritdoc cref="object.ToString()"/>

0 commit comments

Comments
 (0)