From 5f53e51dae19d72c30711f400317e4eaea2253a7 Mon Sep 17 00:00:00 2001 From: Francesco Bonizzi Date: Thu, 24 Oct 2019 08:21:09 +0200 Subject: [PATCH 1/5] Mark Address and HashDigest as readonly #605 --- Libplanet/Address.cs | 21 ++++----------------- Libplanet/HashDigest.cs | 21 ++++----------------- 2 files changed, 8 insertions(+), 34 deletions(-) diff --git a/Libplanet/Address.cs b/Libplanet/Address.cs index f10cbb65cf..a6c5008258 100644 --- a/Libplanet/Address.cs +++ b/Libplanet/Address.cs @@ -38,7 +38,7 @@ namespace Libplanet /// [Serializable] [Equals] - public struct Address : ISerializable, IComparable
, IComparable + public readonly struct Address : ISerializable, IComparable
, IComparable { /// /// The s size that each takes. @@ -46,8 +46,6 @@ public struct Address : ISerializable, IComparable
, IComparable ///
public const int Size = 20; - private ImmutableArray _byteArray; - /// /// Creates an instance from the given immutable array (i.e., ). @@ -67,7 +65,7 @@ public Address(ImmutableArray address) throw new ArgumentException("address must be 20 bytes", nameof(address)); } - _byteArray = address; + ByteArray = address; } /// @@ -142,18 +140,7 @@ public Address(string hex) /// This is immutable. For a mutable array, call method. /// - public ImmutableArray ByteArray - { - get - { - if (_byteArray.IsDefault) - { - _byteArray = new byte[Size].ToImmutableArray(); - } - - return _byteArray; - } - } + public ImmutableArray ByteArray { get; } /// /// Gets a mutable array of 20 s that represent @@ -166,7 +153,7 @@ public ImmutableArray ByteArray /// /// [Pure] - public byte[] ToByteArray() => ByteArray.ToArray(); + public byte[] ToByteArray() => ByteArray == default ? new byte[Size] : ByteArray.ToArray(); /// /// Gets a mixed-case hexadecimal string of 40 letters that represent diff --git a/Libplanet/HashDigest.cs b/Libplanet/HashDigest.cs index 013623c4ec..d81063ae78 100644 --- a/Libplanet/HashDigest.cs +++ b/Libplanet/HashDigest.cs @@ -16,7 +16,7 @@ namespace Libplanet /// A which corresponds to /// a digest. This determines of a digest. /// - public struct HashDigest : IEquatable> + public readonly struct HashDigest : IEquatable> where T : HashAlgorithm { /// @@ -29,8 +29,6 @@ public struct HashDigest : IEquatable> /// public static readonly int Size; - private ImmutableArray _byteArray; - static HashDigest() { var thunk = (T)typeof(T).GetMethod("Create", new Type[0]).Invoke( @@ -70,7 +68,7 @@ public HashDigest(byte[] hashDigest) ); } - _byteArray = hashDigest.ToImmutableArray(); + ByteArray = hashDigest.ToImmutableArray(); } /// @@ -79,18 +77,7 @@ public HashDigest(byte[] hashDigest) /// It is immutable. For a mutable array, use /// method instead. /// - public ImmutableArray ByteArray - { - get - { - if (_byteArray.IsDefault) - { - _byteArray = new byte[Size].ToImmutableArray(); - } - - return _byteArray; - } - } + public ImmutableArray ByteArray { get; } /// /// Converts a given hexadecimal representation of a digest into @@ -169,7 +156,7 @@ public bool Satisfies(long difficulty) /// /// [Pure] - public byte[] ToByteArray() => ByteArray.ToArray(); + public byte[] ToByteArray() => ByteArray == default ? new byte[Size] : ByteArray.ToArray(); /// /// Gets a hexadecimal representation of a digest. From 71594c8142996c124ee1cae75bbebdea7adab4d2 Mon Sep 17 00:00:00 2001 From: Francesco Bonizzi Date: Thu, 24 Oct 2019 08:45:22 +0200 Subject: [PATCH 2/5] Mark Address and HashDigest as readonly #605 --- Libplanet/Address.cs | 17 +++++++++++++++-- Libplanet/HashDigest.cs | 17 +++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Libplanet/Address.cs b/Libplanet/Address.cs index a6c5008258..65deb39583 100644 --- a/Libplanet/Address.cs +++ b/Libplanet/Address.cs @@ -46,6 +46,8 @@ namespace Libplanet /// public const int Size = 20; + private readonly ImmutableArray _byteArray; + /// /// Creates an instance from the given immutable array (i.e., ). @@ -65,7 +67,7 @@ public Address(ImmutableArray address) throw new ArgumentException("address must be 20 bytes", nameof(address)); } - ByteArray = address; + _byteArray = address; } /// @@ -140,7 +142,18 @@ public Address(string hex) /// This is immutable. For a mutable array, call method. /// - public ImmutableArray ByteArray { get; } + public ImmutableArray ByteArray + { + get + { + if (_byteArray == default) + { + return (new byte[Size]).ToImmutableArray(); + } + + return _byteArray; + } + } /// /// Gets a mutable array of 20 s that represent diff --git a/Libplanet/HashDigest.cs b/Libplanet/HashDigest.cs index d81063ae78..c9152f4858 100644 --- a/Libplanet/HashDigest.cs +++ b/Libplanet/HashDigest.cs @@ -29,6 +29,8 @@ namespace Libplanet /// public static readonly int Size; + private readonly ImmutableArray _byteArray; + static HashDigest() { var thunk = (T)typeof(T).GetMethod("Create", new Type[0]).Invoke( @@ -68,7 +70,7 @@ public HashDigest(byte[] hashDigest) ); } - ByteArray = hashDigest.ToImmutableArray(); + _byteArray = hashDigest.ToImmutableArray(); } /// @@ -77,7 +79,18 @@ public HashDigest(byte[] hashDigest) /// It is immutable. For a mutable array, use /// method instead. /// - public ImmutableArray ByteArray { get; } + public ImmutableArray ByteArray + { + get + { + if (_byteArray == default) + { + return (new byte[Size]).ToImmutableArray(); + } + + return _byteArray; + } + } /// /// Converts a given hexadecimal representation of a digest into From b88fec19a425d14d4da55b4ac9971040fd973f59 Mon Sep 17 00:00:00 2001 From: Francesco Bonizzi Date: Thu, 24 Oct 2019 08:49:01 +0200 Subject: [PATCH 3/5] Added CHANGES.md notes --- CHANGES.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index b19d8cefe0..a34db36970 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -54,6 +54,7 @@ To be released. - `Swarm.PreloadAsync()` became to ignore peers with lower tip. [[#592]] - `KademliaProtocol.RefreshTableAsync()` became to validate only stale peers. [[#568], [#593]] + - Mark Address and HashDigest as readonly [[#610]]. ### Bug fixes @@ -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 ------------- From 2c57962c5accece8e39e0bb012c9319455c8b3fa Mon Sep 17 00:00:00 2001 From: Francesco Bonizzi Date: Thu, 24 Oct 2019 08:50:21 +0200 Subject: [PATCH 4/5] Added CHANGES.md notes --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index a34db36970..9f6c7e23d1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -54,7 +54,7 @@ To be released. - `Swarm.PreloadAsync()` became to ignore peers with lower tip. [[#592]] - `KademliaProtocol.RefreshTableAsync()` became to validate only stale peers. [[#568], [#593]] - - Mark Address and HashDigest as readonly [[#610]]. + - Marked `Address` and `HashDigest` as readonly and made some initialization changes. [[#610]] ### Bug fixes From 9985d4b02448d78e56fe437492a99cc33f74d400 Mon Sep 17 00:00:00 2001 From: Francesco Bonizzi Date: Thu, 24 Oct 2019 09:24:35 +0200 Subject: [PATCH 5/5] Mark Address and HashDigest as readonly #605 --- CHANGES.md | 2 +- Libplanet/Address.cs | 10 +++++++--- Libplanet/HashDigest.cs | 12 +++++++++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 9f6c7e23d1..30383a8dd5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -54,7 +54,7 @@ To be released. - `Swarm.PreloadAsync()` became to ignore peers with lower tip. [[#592]] - `KademliaProtocol.RefreshTableAsync()` became to validate only stale peers. [[#568], [#593]] - - Marked `Address` and `HashDigest` as readonly and made some initialization changes. [[#610]] + - Marked `Address` and `HashDigest` as readonly. [[#610]] ### Bug fixes diff --git a/Libplanet/Address.cs b/Libplanet/Address.cs index 65deb39583..76fe595654 100644 --- a/Libplanet/Address.cs +++ b/Libplanet/Address.cs @@ -46,6 +46,8 @@ namespace Libplanet /// public const int Size = 20; + private static readonly byte[] _defaultByteArray = new byte[Size]; + private readonly ImmutableArray _byteArray; /// @@ -146,9 +148,9 @@ public ImmutableArray ByteArray { get { - if (_byteArray == default) + if (_byteArray.IsDefault) { - return (new byte[Size]).ToImmutableArray(); + return _defaultByteArray.ToImmutableArray(); } return _byteArray; @@ -166,7 +168,9 @@ public ImmutableArray ByteArray /// /// [Pure] - public byte[] ToByteArray() => ByteArray == default ? new byte[Size] : ByteArray.ToArray(); + public byte[] ToByteArray() => ByteArray.IsDefault + ? _defaultByteArray + : ByteArray.ToArray(); /// /// Gets a mixed-case hexadecimal string of 40 letters that represent diff --git a/Libplanet/HashDigest.cs b/Libplanet/HashDigest.cs index c9152f4858..c916b915cd 100644 --- a/Libplanet/HashDigest.cs +++ b/Libplanet/HashDigest.cs @@ -29,6 +29,8 @@ namespace Libplanet /// public static readonly int Size; + private static readonly byte[] _defaultByteArray; + private readonly ImmutableArray _byteArray; static HashDigest() @@ -36,6 +38,8 @@ 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]; } /// @@ -83,9 +87,9 @@ public ImmutableArray ByteArray { get { - if (_byteArray == default) + if (_byteArray.IsDefault) { - return (new byte[Size]).ToImmutableArray(); + return _defaultByteArray.ToImmutableArray(); } return _byteArray; @@ -169,7 +173,9 @@ public bool Satisfies(long difficulty) /// /// [Pure] - public byte[] ToByteArray() => ByteArray == default ? new byte[Size] : ByteArray.ToArray(); + public byte[] ToByteArray() => ByteArray.IsDefault + ? _defaultByteArray + : ByteArray.ToArray(); /// /// Gets a hexadecimal representation of a digest.