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

Fix an issue where currency does not work correctly in dotnet6 #3880

Merged
merged 2 commits into from
Jul 17, 2024
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
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ Version 5.1.2

To be released.

- Fix an issue where currency does not work correctly in dotnet6.
[[#3880]]

[#3880]: https://github.com/planetarium/libplanet/pull/3880


Version 5.1.1
-------------
Expand Down
66 changes: 26 additions & 40 deletions src/Libplanet.Types/Assets/Currency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,7 @@ namespace Libplanet.Types.Assets
/// The deterministic hash derived from other fields.
/// </summary>
[JsonInclude]
#if NETSTANDARD2_0_OR_GREATER
public readonly HashDigest<SHA1> Hash;
#else
#pragma warning disable SA1201
public HashDigest<SHA1> Hash => GetHash();
#endif

/// <summary>
/// Whether the total supply of this instance of <see cref="Currency"/> is trackable.
Expand Down Expand Up @@ -248,9 +243,7 @@ public Currency(IValue serialized)
Minters = null;
}

#if NETSTANDARD2_0_OR_GREATER
Hash = GetHash();
#endif // NETSTANDARD2_0_OR_GREATER
Hash = GetHash(Minters, Ticker, DecimalPlaces, _maximumSupply, TotalSupplyTrackable);
}

/// <summary>
Expand Down Expand Up @@ -280,8 +273,8 @@ maximumSupply is { } v
#pragma warning restore SA1118
{
TotalSupplyTrackable = totalSupplyTrackable;
#if NETSTANDARD2_0_OR_GREATER
HashDigest<SHA1> expectedHash = GetHash();
HashDigest<SHA1> expectedHash = GetHash(
Minters, Ticker, DecimalPlaces, _maximumSupply, TotalSupplyTrackable);
if (!expectedHash.Equals(hash))
{
var msg = $"Invalid currency hash; expected {expectedHash}, but got {hash}. " +
Expand All @@ -294,7 +287,6 @@ maximumSupply is { } v
}

Hash = hash;
#endif // NETSTANDARD2_0_OR_GREATER
}

private Currency(SerializationInfo info, StreamingContext context)
Expand Down Expand Up @@ -358,9 +350,7 @@ private Currency(SerializationInfo info, StreamingContext context)
}
}

#if NETSTANDARD2_0_OR_GREATER
Hash = GetHash();
#endif // NETSTANDARD2_0_OR_GREATER
Hash = GetHash(Minters, Ticker, DecimalPlaces, _maximumSupply, TotalSupplyTrackable);
}

/// <summary>
Expand Down Expand Up @@ -408,9 +398,7 @@ private Currency(
_maximumSupply = maximumSupply;
}

#if NETSTANDARD2_0_OR_GREATER
Hash = GetHash();
#endif // NETSTANDARD2_0_OR_GREATER
Hash = GetHash(Minters, Ticker, DecimalPlaces, _maximumSupply, TotalSupplyTrackable);
}

/// <summary>
Expand Down Expand Up @@ -448,9 +436,7 @@ private Currency(
DecimalPlaces = decimalPlaces;
_maximumSupply = null;
TotalSupplyTrackable = totalSupplyTrackable;
#if NETSTANDARD2_0_OR_GREATER
Hash = GetHash();
#endif // NETSTANDARD2_0_OR_GREATER
Hash = GetHash(Minters, Ticker, DecimalPlaces, _maximumSupply, TotalSupplyTrackable);
}

/// <summary>
Expand Down Expand Up @@ -742,40 +728,40 @@ private static SHA1 GetSHA1()
#endif
}

[Pure]
private IValue SerializeForHash()
// NOTE: This uses a different serialization scheme from
// Serialize() due to backward compatibility issues.
private static HashDigest<SHA1> GetHash(
IImmutableSet<Address>? minters,
string ticker,
byte decimalPlaces,
(BigInteger Major, BigInteger Minor)? maximumSupply,
bool totalSupplyTrackable
)
{
IValue minters = Minters is ImmutableHashSet<Address> a
using var buffer = new MemoryStream();
using var sha1 = GetSHA1();
using var stream = new CryptoStream(buffer, sha1, CryptoStreamMode.Write);
var codec = new Codec();
IValue mintersValue = minters is ImmutableHashSet<Address> a
? new List(a.OrderBy(m => m).Select(m => m.Bencoded))
: (IValue)Null.Value;

var serialized = Dictionary.Empty
.Add("ticker", Ticker)
.Add("decimals", (int)DecimalPlaces)
.Add("minters", minters);
.Add("ticker", ticker)
.Add("decimals", (int)decimalPlaces)
.Add("minters", mintersValue);

if (_maximumSupply is var (major, minor))
if (maximumSupply is var (major, minor))
{
serialized = serialized.Add("maximumSupplyMajor", new Integer(major))
.Add("maximumSupplyMinor", new Integer(minor));
}

if (TotalSupplyTrackable)
if (totalSupplyTrackable)
{
serialized = serialized.Add("totalSupplyTrackable", true);
}

return serialized;
}

[Pure]
private HashDigest<SHA1> GetHash()
{
using var buffer = new MemoryStream();
using var sha1 = GetSHA1();
using var stream = new CryptoStream(buffer, sha1, CryptoStreamMode.Write);
var codec = new Codec();
codec.Encode(SerializeForHash(), stream);
codec.Encode(serialized, stream);
stream.FlushFinalBlock();
if (sha1.Hash is { } hash)
{
Expand Down
Loading