/
BlockContent.cs
312 lines (295 loc) · 16.4 KB
/
BlockContent.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Security.Cryptography;
using System.Threading;
using Libplanet.Action;
using Libplanet.Tx;
namespace Libplanet.Blocks
{
/// <summary>
/// A block content without any proofs like nonce or hash. This represents contents of a
/// block that is not yet mined.
/// </summary>
/// <typeparam name="T">A class implementing <see cref="IAction"/> to include. This type
/// parameter is aligned with <see cref="Transaction{T}"/>'s type parameter.</typeparam>
/// <remarks>Unlike other model types like <see cref="Block{T}"/> or
/// <see cref="Transaction{T}"/>, this type is mutable. To get a distinct instance with
/// partly changed fields, use <see cref="BlockContent{T}(IBlockContent{T})"/> constructor and
/// property setters on a copy instead.</remarks>
public sealed class BlockContent<T> : BlockMetadata, IBlockContent<T>
where T : IAction, new()
{
private IReadOnlyList<Transaction<T>> _transactions = ImmutableArray<Transaction<T>>.Empty;
/// <summary>
/// Creates a new <see cref="BlockContent{T}"/> instance filled with the given
/// <paramref name="metadata"/>'s contents and <paramref name="transactions"/>.
/// </summary>
/// <param name="metadata">The <see cref="IBlockMetadata"/> to copy.</param>
/// <param name="transactions">The transactions to include in the block.</param>
/// <exception cref="InvalidBlockProtocolVersionException">Thrown when
/// the <paramref name="metadata"/>'s <see cref="IBlockMetadata.ProtocolVersion"/>
/// is less than 0, or greater than <see cref="BlockMetadata.CurrentProtocolVersion"/>,
/// the latest known protocol version.</exception>
/// <exception cref="InvalidBlockIndexException">Thrown when the <paramref name="metadata"/>
/// has a negative <see cref="IBlockMetadata.Index"/>.</exception>
/// <exception cref="InvalidBlockDifficultyException">Thrown when
/// the <paramref name="metadata"/>'s <see cref="IBlockMetadata.Difficulty"/> is negative.
/// </exception>
/// <exception cref="InvalidBlockTotalDifficultyException">Thrown when
/// the <paramref name="metadata"/>'s <see cref="IBlockMetadata.TotalDifficulty"/> is less
/// than its <see cref="IBlockMetadata.Difficulty"/>.</exception>
/// <exception cref="InvalidTxSignatureException">Thrown when any tx signature is invalid or
/// not signed by its signer.</exception>
/// <exception cref="InvalidTxPublicKeyException">Thrown when any tx signer is not derived
/// from its its public key.</exception>
/// <exception cref="InvalidTxNonceException">Thrown when the same tx nonce is used by
/// a signer twice or more, or a tx nonce is used without its previous nonce by a signer.
/// Note that this validates only a block's intrinsic integrity between its transactions,
/// but does not guarantee integrity between blocks. Such validation needs to be conducted
/// by <see cref="Blockchain.BlockChain{T}"/>.</exception>
/// <exception cref="InvalidTxGenesisHashException">Thrown when transactions to set have
/// inconsistent genesis hashes.</exception>
/// <exception cref="InvalidBlockTxHashException">Thrown when the given
/// <paramref name="metadata"/>'s <see cref="IBlockMetadata.TxHash"/> is inconsistent with
/// <paramref name="transactions"/>.</exception>
public BlockContent(IBlockMetadata metadata, IEnumerable<Transaction<T>> transactions)
: base(metadata)
{
Transactions = transactions.ToImmutableArray();
TxHash = metadata.TxHash;
}
/// <summary>
/// Creates a new <see cref="BlockContent{T}"/> instance filled with the given
/// <paramref name="metadata"/>'s contents and zero transactions.
/// </summary>
/// <param name="metadata">The <see cref="IBlockMetadata"/> to copy.</param>
/// <exception cref="InvalidBlockProtocolVersionException">Thrown when
/// the <paramref name="metadata"/>'s to set is <see cref="IBlockMetadata.ProtocolVersion"/>
/// is less than 0, or greater than <see cref="BlockMetadata.CurrentProtocolVersion"/>,
/// the latest known protocol version.</exception>
/// <exception cref="InvalidBlockIndexException">Thrown when the value to set is negative.
/// </exception>
/// <exception cref="InvalidBlockDifficultyException">Thrown when the value to set is
/// is negative.</exception>
/// <exception cref="InvalidBlockTotalDifficultyException">Thrown when
/// the <paramref name="metadata"/>'s <see cref="IBlockMetadata.TotalDifficulty"/> is less
/// than its <see cref="IBlockMetadata.Difficulty"/>.</exception>
/// <exception cref="InvalidTxSignatureException">Thrown when any tx signature is invalid or
/// not signed by its signer.</exception>
/// <exception cref="InvalidTxPublicKeyException">Thrown when any tx signer is not derived
/// from its its public key.</exception>
/// <exception cref="InvalidTxNonceException">Thrown when the same tx nonce is used by
/// a signer twice or more, or a tx nonce is used without its previous nonce by a signer.
/// Note that this validates only a block's intrinsic integrity between its transactions,
/// but does not guarantee integrity between blocks. Such validation needs to be conducted
/// by <see cref="Blockchain.BlockChain{T}"/>.</exception>
/// <exception cref="InvalidTxGenesisHashException">Thrown when transactions to set have
/// inconsistent genesis hashes.</exception>
/// <exception cref="InvalidBlockTxHashException">Thrown when the given
/// <paramref name="metadata"/>'s <see cref="IBlockMetadata.TxHash"/> is not <c>null</c>.
/// </exception>
public BlockContent(IBlockMetadata metadata)
: this(metadata, Enumerable.Empty<Transaction<T>>())
{
}
/// <summary>
/// Creates a <see cref="BlockContent{T}"/> by copying the fields of another block
/// <paramref name="content"/>.
/// </summary>
/// <param name="content">This source of the block content to copy. This hasn't be
/// a actual <see cref="BlockContent{T}"/> instance, but can be any object which implements
/// <see cref="IBlockContent{T}"/> instance.</param>
/// <exception cref="InvalidBlockProtocolVersionException">Thrown when
/// the <paramref name="content"/>'s <see cref="IBlockMetadata.ProtocolVersion"/>
/// is less than 0, or greater than <see cref="BlockMetadata.CurrentProtocolVersion"/>,
/// the latest known protocol version.</exception>
/// <exception cref="InvalidBlockIndexException">Thrown when the value to set is negative.
/// </exception>
/// <exception cref="InvalidBlockDifficultyException">Thrown when the value to set is
/// is negative.</exception>
/// <exception cref="InvalidBlockTotalDifficultyException">Thrown when
/// the <paramref name="content"/>'s <see cref="IBlockMetadata.TotalDifficulty"/> is less
/// than its <see cref="IBlockMetadata.Difficulty"/>.</exception>
/// <exception cref="InvalidTxSignatureException">Thrown when any tx signature is invalid or
/// not signed by its signer.</exception>
/// <exception cref="InvalidTxPublicKeyException">Thrown when any tx signer is not derived
/// from its its public key.</exception>
/// <exception cref="InvalidTxNonceException">Thrown when the same tx nonce is used by
/// a signer twice or more, or a tx nonce is used without its previous nonce by a signer.
/// Note that this validates only a block's intrinsic integrity between its transactions,
/// but does not guarantee integrity between blocks. Such validation needs to be conducted
/// by <see cref="Blockchain.BlockChain{T}"/>.</exception>
/// <exception cref="InvalidTxGenesisHashException">Thrown when transactions to set have
/// inconsistent genesis hashes.</exception>
/// <exception cref="InvalidBlockTxHashException">Thrown when the given block
/// <paramref name="content"/>'s <see cref="IBlockMetadata.TxHash"/> is not consistent with
/// its <see cref="IBlockContent{T}.Transactions"/>.</exception>
public BlockContent(IBlockContent<T> content)
: this(content, content.Transactions)
{
}
/// <summary>
/// Creates a new empty <see cref="BlockContent{T}"/> instance.
/// </summary>
public BlockContent()
{
}
/// <summary>
/// Transactions belonging to the block.
/// </summary>
/// <remarks>This is always ordered by <see cref="Transaction{T}.Id"/>.
/// <para>When <see cref="Transactions"/> is updated, <see cref="TxHash"/> is also updated
/// together.</para></remarks>
/// <exception cref="InvalidTxSignatureException">Thrown when any tx signature is invalid or
/// not signed by its signer.</exception>
/// <exception cref="InvalidTxPublicKeyException">Thrown when any tx signer is not derived
/// from its its public key.</exception>
/// <exception cref="InvalidTxNonceException">Thrown when the same tx nonce is used by
/// a signer twice or more, or a tx nonce is used without its previous nonce by a signer.
/// Note that this validates only a block's intrinsic integrity between its transactions,
/// but does not guarantee integrity between blocks. Such validation needs to be conducted
/// by <see cref="Blockchain.BlockChain{T}"/>.</exception>
/// <exception cref="InvalidTxGenesisHashException">Thrown when transactions to set have
/// inconsistent genesis hashes.</exception>
public IReadOnlyList<Transaction<T>> Transactions
{
get => _transactions;
set
{
IEnumerable<IGrouping<Address, Transaction<T>>> signerTxs =
value.OrderBy(tx => tx.Nonce).GroupBy(tx => tx.Signer);
BlockHash? genesisHash = null;
foreach (IGrouping<Address, Transaction<T>> txs in signerTxs)
{
long lastNonce = -1L;
foreach (Transaction<T> tx in txs)
{
// FIXME: Transaction<T> should disallow illegal states to be represented
// as its instances. https://github.com/planetarium/libplanet/issues/1164
tx.Validate();
long nonce = tx.Nonce;
if (lastNonce >= 0 && lastNonce + 1 != nonce)
{
Address s = tx.Signer;
string msg = nonce <= lastNonce
? $"The signer {s}'s nonce {nonce} was already consumed before."
: $"The signer {s}'s nonce {lastNonce} has to be added first.";
throw new InvalidTxNonceException(tx.Id, lastNonce + 1, tx.Nonce, msg);
}
if (genesisHash is { } g && !tx.GenesisHash.Equals(g))
{
throw new InvalidTxGenesisHashException(
tx.Id,
g,
tx.GenesisHash,
$"Transactions in the block #{Index} are inconsistent."
);
}
lastNonce = nonce;
genesisHash = tx.GenesisHash;
}
}
_transactions = value.OrderBy(tx => tx.Id).ToImmutableArray();
base.TxHash = DeriveTxHash(_transactions);
}
}
/// <summary>
/// The hash of all transactions in the block. This is <c>null</c> if the block has no
/// transactions.
/// </summary>
/// <exception cref="InvalidBlockTxHashException">Thrown when the <see cref="TxHash"/> is
/// inconsistent with <see cref="Transactions"/>.</exception>
// FIXME: Remove Block<T>.CalculateTxHashes() method, and move the logic behind it to here.
public override HashDigest<SHA256>? TxHash
{
get => base.TxHash;
set
{
HashDigest<SHA256>? prevValue = TxHash;
if (prevValue is { } prev)
{
if (!(value is { } v))
{
string msg =
$"As the block #{Index} has transactions, " +
$"its {nameof(TxHash)} must not be null.";
throw new InvalidBlockTxHashException(msg, value, prev);
}
else if (!v.Equals(prev))
{
throw new InvalidBlockTxHashException(
$"The block #{Index}'s {nameof(TxHash)} is invalid.",
value,
prev
);
}
}
else if (value is { })
{
string msg =
$"As the block #{Index} has no transactions, " +
$"its {nameof(TxHash)} must be null.";
throw new InvalidBlockTxHashException(msg, value, prevValue);
}
}
}
/// <summary>
/// Mines the PoW (proof-of-work) nonce satisfying the block
/// <see cref="BlockMetadata.Difficulty"/>, and returns a valid
/// <see cref="PreEvaluationBlock{T}"/> instance.
/// </summary>
/// <param name="hashAlgorithm">The hash algorithm to use for calculating pre-evaluation
/// hash.</param>
/// <param name="cancellationToken">An optional cancellation token used to propagate signal
/// that this operation should be cancelled.</param>
/// <returns>A <see cref="PreEvaluationBlock{T}"/> instance with a valid proof-of-work.
/// </returns>
/// <exception cref="OperationCanceledException">Thrown when the specified
/// <paramref name="cancellationToken"/> received a cancellation request.</exception>
public PreEvaluationBlock<T> Mine(
HashAlgorithmType hashAlgorithm,
CancellationToken cancellationToken = default
) =>
new PreEvaluationBlock<T>(
this,
hashAlgorithm,
MineNonce(hashAlgorithm, cancellationToken)
);
/// <summary>
/// Derives <see cref="TxHash"/> from the given <paramref name="transactions"/>.
/// </summary>
/// <param name="transactions">The transactions to derive <see cref="TxHash"/> from.
/// This must be ordered by <see cref="Transaction{T}.Id"/>.</param>
/// <returns>The derived <see cref="TxHash"/>.</returns>
/// <exception cref="ArgumentException">Thrown when the <paramref name="transactions"/> are
/// not ordered by their <see cref="Transaction{T}.Id"/>s.</exception>
internal static HashDigest<SHA256>? DeriveTxHash(IEnumerable<Transaction<T>> transactions)
{
TxId? prevId = null;
SHA256 hasher = SHA256.Create();
// Bencodex lists look like: l...e
hasher.TransformBlock(new byte[] { 0x6c }, 0, 1, null, 0); // "l"
foreach (Transaction<T> tx in transactions)
{
if (prevId is { } prev && prev.CompareTo(tx.Id) > 0)
{
throw new ArgumentException(
$"Transactions must be ordered by their {nameof(Transaction<T>.Id)}s.",
nameof(transactions)
);
}
byte[] payload = tx.Serialize(true);
hasher.TransformBlock(payload, 0, payload.Length, null, 0);
prevId = tx.Id;
}
if (prevId is null)
{
return null;
}
hasher.TransformFinalBlock(new byte[] { 0x65 }, 0, 1); // "e"
return new HashDigest<SHA256>(hasher.Hash);
}
}
}