/
TxId.cs
161 lines (142 loc) · 5.16 KB
/
TxId.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections.Immutable;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Runtime.Serialization;
using Libplanet.Serialization;
namespace Libplanet.Tx
{
/// <summary>
/// <see cref="TxId"/>, abbreviation of transaction identifier,
/// is a SHA-256 digest derived from a <see cref="Transaction{T}"/>'s
/// content.
/// <para>As it is a SHA-256 digest, it consists of 32 <see cref="byte"/>s,
/// and 64 characters in hexadecimal.
/// (See also <see cref="Size"/> constant.)</para>
/// </summary>
/// <seealso cref="Transaction{T}.Id"/>
[Serializable]
[Equals]
public struct TxId : ISerializable, IComparable<TxId>, IComparable
{
/// <summary>
/// The <see cref="byte"/>s size that each <see cref="TxId"/> takes.
/// <para>As a txid is a SHA-256 digest, it is 32 <see cref="byte"/>s.
/// </para>
/// </summary>
public const int Size = 32;
private ImmutableArray<byte> _byteArray;
/// <summary>
/// Converts a <see cref="byte"/> array into a <see cref="TxId"/>.
/// </summary>
/// <param name="txid">A <see cref="byte"/> array that encodes
/// a <see cref="TxId"/>. It must not be <c>null</c>,
/// and its <see cref="Array.Length"/> must be the same to
/// <see cref="Size"/>.</param>
/// <exception cref="ArgumentNullException">Thrown when the given
/// <paramref name="txid"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the given
/// <paramref name="txid"/>'s <see cref="Array.Length"/> is not
/// the same to the required <see cref="Size"/>.</exception>
public TxId(byte[] txid)
{
if (txid == null)
{
throw new ArgumentNullException(nameof(txid));
}
if (txid.Length != Size)
{
throw new ArgumentOutOfRangeException(
$"TxId must be {Size} bytes.",
nameof(txid)
);
}
_byteArray = txid.ToImmutableArray();
}
public TxId(
SerializationInfo info,
StreamingContext context)
: this(info.GetValue<byte[]>("tx_id"))
{
}
/// <summary>
/// A bare immutable <see cref="byte"/> array of
/// this <see cref="TxId"/>.
/// </summary>
/// <remarks>It is immutable. For a mutable array, use
/// <see cref="ToByteArray()"/> method instead.</remarks>
/// <seealso cref="ToByteArray()"/>
public ImmutableArray<byte> ByteArray
{
get
{
if (_byteArray.IsDefault)
{
_byteArray = new byte[Size].ToImmutableArray();
}
return _byteArray;
}
}
public static bool operator ==(TxId left, TxId right) => Operator.Weave(left, right);
public static bool operator !=(TxId left, TxId right) => Operator.Weave(left, right);
/// <summary>
/// Gets a bare mutable <see cref="byte"/> array of
/// this <see cref="TxId"/>.
/// </summary>
/// <returns>A new mutable <see cref="byte"/> array of
/// this <see cref="TxId"/>.
/// Since a returned array is created every time the method is called,
/// any mutations on that array does not affect to
/// the <see cref="TxId"/> object.
/// </returns>
/// <seealso cref="ByteArray"/>
[Pure]
public byte[] ToByteArray() => ByteArray.ToArray();
/// <summary>
/// Gets a hexadecimal form of a <see cref="TxId"/>.
/// </summary>
/// <returns>64 hexadecimal characters.</returns>
[Pure]
public string ToHex() => ByteUtil.Hex(ToByteArray());
/// <summary>
/// Gets a <see cref="TxId"/>'s representative string.
/// </summary>
/// <returns>A string which represents this <see cref="TxId"/>.
/// </returns>
[Pure]
public override string ToString() => ToHex();
/// <inheritdoc cref="IComparable{T}.CompareTo(T)"/>
public int CompareTo(TxId other)
{
for (int i = 0; i < Size; ++i)
{
int cmp = ByteArray[i].CompareTo(other.ByteArray[i]);
if (cmp != 0)
{
return cmp;
}
}
return 0;
}
/// <inheritdoc cref="IComparable.CompareTo(object)"/>
public int CompareTo(object obj)
{
if (obj is TxId other)
{
return ((IComparable<TxId>)this).CompareTo(other);
}
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
throw new ArgumentException(nameof(obj));
}
/// <inheritdoc />
public void GetObjectData(
SerializationInfo info,
StreamingContext context)
{
info.AddValue("tx_id", _byteArray.ToArray());
}
}
}