diff --git a/CHANGES.md b/CHANGES.md index 56ff7907e8..1374f91bb6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -29,6 +29,8 @@ To be released. - Removed `IRandom.NextDouble()` method, because [floating-point arithmetics, which is underspecified, likely introduce indeterminism][floating-point determinism]. [[#410], [#419]] + - `Address(byte[])` became to throw `ArgumentNullException` + instead of `NullReferenceException`. [[#443]] ### Added interfaces @@ -52,6 +54,7 @@ To be released. - Added `PreloadState`, `ActionExecutionState`, `StateReferenceDownloadState`, and `BlockStateDownloadState` classes to cover all phases in the entire preloading process. [[#397], [#400]] + - Added `Address(ImmutableArray)` constructor. [[#442], [#443]] ### Behavioral changes @@ -146,6 +149,8 @@ To be released. [#426]: https://github.com/planetarium/libplanet/pull/426 [#434]: https://github.com/planetarium/libplanet/pull/434 [#438]: https://github.com/planetarium/libplanet/pull/438 +[#442]: https://github.com/planetarium/libplanet/issues/442 +[#443]: https://github.com/planetarium/libplanet/pull/443 [LiteDB #1268]: https://github.com/mbdavid/LiteDB/issues/1268 [floating-point determinism]: https://wp.me/p1fTCO-kT diff --git a/Libplanet.Tests/AddressTest.cs b/Libplanet.Tests/AddressTest.cs index 368ce06639..6022fcbbb6 100644 --- a/Libplanet.Tests/AddressTest.cs +++ b/Libplanet.Tests/AddressTest.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Immutable; using System.IO; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; @@ -12,11 +13,27 @@ public class AddressTest [Fact] public void ConstructorDoesNotTakeNullValue() { - Assert.Throws( + Assert.Throws( () => new Address((byte[])null) ); } + [Fact] + public void ConstructWithImmutableArray() + { + byte[] addr = new byte[20] + { + 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xab, + 0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, + 0xcd, 0xef, + }; + + Assert.Equal( + new Address("0123456789ABcdefABcdEfABcdEFabcDEFabCDEF"), + new Address(addr.ToImmutableArray()) + ); + } + [Fact] public void DefaultConstructor() { diff --git a/Libplanet/Address.cs b/Libplanet/Address.cs index dd7c79e4cb..f10cbb65cf 100644 --- a/Libplanet/Address.cs +++ b/Libplanet/Address.cs @@ -1,9 +1,7 @@ using System; -using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics.Contracts; using System.Linq; -using System.Net.NetworkInformation; using System.Runtime.Serialization; using System.Text; using Libplanet.Crypto; @@ -50,6 +48,28 @@ public struct Address : ISerializable, IComparable
, IComparable private ImmutableArray _byteArray; + /// + /// Creates an instance from the given immutable array (i.e., ). + /// + /// An immutable array of 20 s which + /// represents an . + /// Thrown when the given array did not lengthen 20 bytes. + /// A valid array which represents an + /// can be gotten using method. + /// + public Address(ImmutableArray address) + { + if (address.Length != Size) + { + throw new ArgumentException("address must be 20 bytes", nameof(address)); + } + + _byteArray = address; + } + /// /// Creates an instance from the given array (i.e., ). @@ -57,7 +77,7 @@ public struct Address : ISerializable, IComparable
, IComparable /// An array of 20 s which /// represents an . This must not be null. /// - /// Thrown when null was + /// Thrown when null was /// passed to . /// Thrown when the given array did not lengthen 20 bytes. @@ -66,18 +86,8 @@ public struct Address : ISerializable, IComparable
, IComparable /// /> method. /// public Address(byte[] address) + : this(address?.ToImmutableArray() ?? throw new ArgumentNullException(nameof(address))) { - if (address == null) - { - throw new NullReferenceException("address must not be null"); - } - - if (address.Length != Size) - { - throw new ArgumentException("address must be 20 bytes"); - } - - _byteArray = address.ToImmutableArray(); } public Address(