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

Mark Address and HashDigest as readonly #605 #610

Merged
merged 5 commits into from
Oct 24, 2019
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
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ To be released.
- `Swarm<T>.PreloadAsync()` became to ignore peers with lower tip. [[#592]]
- `KademliaProtocol<T>.RefreshTableAsync()` became to validate only stale
peers. [[#568], [#593]]
- Marked `Address` and `HashDigest` as readonly. [[#610]]

### Bug fixes

Expand Down Expand Up @@ -113,6 +114,8 @@ To be released.
[#599]: https://github.com/planetarium/libplanet/pull/599
[#602]: https://github.com/planetarium/libplanet/pull/602
[#609]: https://github.com/planetarium/libplanet/pull/609
[#610]: https://github.com/planetarium/libplanet/pull/610


Version 0.6.0
-------------
Expand Down
12 changes: 8 additions & 4 deletions Libplanet/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,17 @@ namespace Libplanet
/// <seealso cref="PublicKey"/>
[Serializable]
[Equals]
public struct Address : ISerializable, IComparable<Address>, IComparable
public readonly struct Address : ISerializable, IComparable<Address>, IComparable
{
/// <summary>
/// The <see cref="byte"/>s size that each <see cref="Address"/> takes.
/// <para>It is 20 <see cref="byte"/>s.</para>
/// </summary>
public const int Size = 20;

private ImmutableArray<byte> _byteArray;
private static readonly byte[] _defaultByteArray = new byte[Size];

private readonly ImmutableArray<byte> _byteArray;

/// <summary>
/// Creates an <see cref="Address"/> instance from the given immutable <see
Expand Down Expand Up @@ -148,7 +150,7 @@ public ImmutableArray<byte> ByteArray
{
if (_byteArray.IsDefault)
{
_byteArray = new byte[Size].ToImmutableArray();
return _defaultByteArray.ToImmutableArray();
}

return _byteArray;
Expand All @@ -166,7 +168,9 @@ public ImmutableArray<byte> ByteArray
/// <seealso cref="ByteArray"/>
/// <seealso cref="Address(byte[])"/>
[Pure]
public byte[] ToByteArray() => ByteArray.ToArray();
public byte[] ToByteArray() => ByteArray.IsDefault
? _defaultByteArray
: ByteArray.ToArray();

/// <summary>
/// Gets a mixed-case hexadecimal string of 40 letters that represent
Expand Down
14 changes: 10 additions & 4 deletions Libplanet/HashDigest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Libplanet
/// <typeparam name="T">A <see cref="HashAlgorithm"/> which corresponds to
/// a digest. This determines <see cref="Size"/> of a digest.</typeparam>
/// <seealso cref="HashAlgorithm"/>
public struct HashDigest<T> : IEquatable<HashDigest<T>>
public readonly struct HashDigest<T> : IEquatable<HashDigest<T>>
where T : HashAlgorithm
{
/// <summary>
Expand All @@ -29,13 +29,17 @@ public struct HashDigest<T> : IEquatable<HashDigest<T>>
/// </summary>
public static readonly int Size;

private ImmutableArray<byte> _byteArray;
private static readonly byte[] _defaultByteArray;

private readonly ImmutableArray<byte> _byteArray;

static HashDigest()
{
var thunk = (T)typeof(T).GetMethod("Create", new Type[0]).Invoke(
null, new object[0]);
Size = thunk.HashSize / 8;

_defaultByteArray = new byte[Size];
}

/// <summary>
Expand Down Expand Up @@ -85,7 +89,7 @@ public ImmutableArray<byte> ByteArray
{
if (_byteArray.IsDefault)
{
_byteArray = new byte[Size].ToImmutableArray();
return _defaultByteArray.ToImmutableArray();
}

return _byteArray;
Expand Down Expand Up @@ -169,7 +173,9 @@ public bool Satisfies(long difficulty)
/// </returns>
/// <seealso cref="ByteArray"/>
[Pure]
public byte[] ToByteArray() => ByteArray.ToArray();
public byte[] ToByteArray() => ByteArray.IsDefault
? _defaultByteArray
: ByteArray.ToArray();

/// <summary>
/// Gets a hexadecimal representation of a digest.
Expand Down