/
Block.cs
407 lines (370 loc) · 14.6 KB
/
Block.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using Libplanet.Action;
using Libplanet.Serialization;
using Libplanet.Tx;
namespace Libplanet.Blocks
{
[Equals]
public class Block<T> : ISerializable
where T : IAction, new()
{
internal const string TimestampFormat = "yyyy-MM-ddTHH:mm:ss.ffffffZ";
private static readonly TimeSpan TimestampThreshold =
TimeSpan.FromSeconds(900);
public Block(
long index,
long difficulty,
Nonce nonce,
Address? miner,
HashDigest<SHA256>? previousHash,
DateTimeOffset timestamp,
IEnumerable<Transaction<T>> transactions)
{
Index = index;
Difficulty = difficulty;
Nonce = nonce;
Miner = miner;
PreviousHash = previousHash;
Timestamp = timestamp;
Transactions = transactions.ToArray();
Hash = Hashcash.Hash(ToBencodex(false, false));
}
protected Block(SerializationInfo info, StreamingContext context)
: this(new RawBlock(info, context))
{
}
private Block(RawBlock rb)
: this(
rb.Index,
rb.Difficulty,
new Nonce(rb.Nonce),
rb.Miner is null ? (Address?)null : new Address(rb.Miner),
#pragma warning disable MEN002 // Line is too long
rb.PreviousHash is null ? (HashDigest<SHA256>?)null : new HashDigest<SHA256>(rb.PreviousHash),
#pragma warning restore MEN002 // Line is too long
DateTimeOffset.ParseExact(
rb.Timestamp,
TimestampFormat,
CultureInfo.InvariantCulture).ToUniversalTime(),
rb.Transactions
.Cast<Dictionary<string, object>>()
.Select(d => new Transaction<T>(new RawTransaction(d)))
.ToList()
)
{
}
public HashDigest<SHA256> Hash { get; }
[IgnoreDuringEquals]
public long Index { get; }
[IgnoreDuringEquals]
public long Difficulty { get; }
[IgnoreDuringEquals]
public Nonce Nonce { get; }
[IgnoreDuringEquals]
public Address? Miner { get; }
[IgnoreDuringEquals]
public HashDigest<SHA256>? PreviousHash { get; }
[IgnoreDuringEquals]
public DateTimeOffset Timestamp { get; }
[IgnoreDuringEquals]
public IEnumerable<Transaction<T>> Transactions { get; }
public static Block<T> Mine(
long index,
long difficulty,
Address miner,
HashDigest<SHA256>? previousHash,
DateTimeOffset timestamp,
IEnumerable<Transaction<T>> transactions)
{
// FIXME: We need to fix this to solve
// https://github.com/planetarium/libplanet/issues/244.
Transaction<T>[] orderedTxs = transactions.OrderBy(tx => tx.Nonce).ToArray();
Block<T> MakeBlock(Nonce n) => new Block<T>(
index,
difficulty,
n,
miner,
previousHash,
timestamp,
orderedTxs
);
Nonce nonce = Hashcash.Answer(
n => MakeBlock(n).ToBencodex(false, false),
difficulty
);
return MakeBlock(nonce);
}
public static Block<T> FromBencodex(byte[] encoded)
{
var serializer = new BencodexFormatter<Block<T>>();
using (var stream = new MemoryStream(encoded))
{
return (Block<T>)serializer.Deserialize(stream);
}
}
public byte[] ToBencodex(bool hash, bool transactionData)
{
var serializer = new BencodexFormatter<Block<T>>
{
Context = new StreamingContext(
StreamingContextStates.All,
new BlockSerializationContext(hash, transactionData)
),
};
using (var stream = new MemoryStream())
{
serializer.Serialize(stream, this);
return stream.ToArray();
}
}
/// <summary>
/// Executes every <see cref="IAction"/> in the
/// <see cref="Transactions"/> step by step, and emits a pair of
/// a transaction, and an <see cref="ActionEvaluation{T}"/>
/// for each step.
/// </summary>
/// <param name="accountStateGetter">An <see cref="AccountStateGetter"/>
/// delegate to get a previous state.
/// A <c>null</c> value, which is default, means a constant function
/// that returns <c>null</c>.</param>
/// <returns>Enumerates pair of a transaction, and
/// <see cref="ActionEvaluation{T}"/> for each action.
/// The order of pairs are the same to
/// the <see cref="Transactions"/> and their
/// <see cref="Transaction{T}.Actions"/> (e.g., tx¹-act¹,
/// tx¹-act², tx²-act¹, tx²-act²,
/// …).
/// Note that each <see cref="IActionContext.Random"/> object has
/// a unconsumed state.
/// </returns>
[Pure]
public
IEnumerable<(Transaction<T>, ActionEvaluation<T>)>
EvaluateActionsPerTx(AccountStateGetter accountStateGetter = null)
{
IAccountStateDelta delta =
new AccountStateDeltaImpl(
accountStateGetter ?? (a => null)
);
foreach (Transaction<T> tx in Transactions)
{
IEnumerable<ActionEvaluation<T>> evaluations =
tx.EvaluateActionsGradually(
Hash,
Index,
delta,
Miner.Value);
foreach (var evaluation in evaluations)
{
yield return (tx, evaluation);
delta = evaluation.OutputStates;
}
delta = new AccountStateDeltaImpl(delta.GetState);
}
}
/// <summary>
/// Executes every <see cref="IAction"/> in the
/// <see cref="Transactions"/> and gets result states of each step of
/// every <see cref="Transaction{T}"/>.
/// <para>It throws an <see cref="InvalidBlockException"/> or
/// an <see cref="InvalidTxException"/> if there is any
/// integrity error.</para>
/// <para>Otherwise it enumerates an <see cref="ActionEvaluation{T}"/>
/// for each <see cref="IAction"/>.</para>
/// </summary>
/// <param name="currentTime">The current time to validate
/// time-wise conditions.</param>
/// <param name="accountStateGetter">The getter of previous states.
/// This affects the execution of <see cref="Transaction{T}.Actions"/>.
/// </param>
/// <returns>An <see cref="ActionEvaluation{T}"/> for each
/// <see cref="IAction"/>.</returns>
/// <exception cref="InvalidBlockTimestampException">Thrown when
/// the <see cref="Timestamp"/> is invalid, for example, it is the far
/// future than the given <paramref name="currentTime"/>.</exception>
/// <exception cref="InvalidBlockIndexException">Thrown when
/// the <see cref="Index"/>is invalid, for example, it is a negative
/// integer.</exception>
/// <exception cref="InvalidBlockDifficultyException">Thrown when
/// the <see cref="Difficulty"/> is not properly configured,
/// for example, it is too easy.</exception>
/// <exception cref="InvalidBlockPreviousHashException">Thrown when
/// <see cref="PreviousHash"/> is invalid so that
/// the <see cref="Block{T}"/>s are not continuous.</exception>
/// <exception cref="InvalidBlockNonceException">Thrown when
/// the <see cref="Nonce"/> does not satisfy its
/// <see cref="Difficulty"/> level.</exception>
/// <exception cref="InvalidTxSignatureException">Thrown when its
/// <see cref="Transaction{T}.Signature"/> is invalid or not signed by
/// the account who corresponds to its
/// <see cref="Transaction{T}.PublicKey"/>.</exception>
/// <exception cref="InvalidTxPublicKeyException">Thrown when its
/// <see cref="Transaction{T}.Signer"/> is not derived from its
/// <see cref="Transaction{T}.PublicKey"/>.</exception>
/// <exception cref="InvalidTxUpdatedAddressesException">Thrown when
/// any <see cref="IAction"/> of <see cref="Transactions"/> tries
/// to update the states of <see cref="Address"/>es not included
/// in <see cref="Transaction{T}.UpdatedAddresses"/>.</exception>
public IEnumerable<ActionEvaluation<T>> Evaluate(
DateTimeOffset currentTime,
AccountStateGetter accountStateGetter
)
{
Validate(currentTime);
(Transaction<T>, ActionEvaluation<T>)[] txEvaluations =
EvaluateActionsPerTx(accountStateGetter).ToArray();
var txUpdatedAddressesPairs = txEvaluations
.GroupBy(tuple => tuple.Item1)
.Select(
grp => (
grp.Key,
grp.Last().Item2.OutputStates.UpdatedAddresses
)
);
foreach (
(Transaction<T> tx, IImmutableSet<Address> updatedAddresses)
in txUpdatedAddressesPairs)
{
if (!tx.UpdatedAddresses.IsSupersetOf(updatedAddresses))
{
const string msg =
"Actions in the transaction try to update " +
"the addresses not granted.";
throw new InvalidTxUpdatedAddressesException(
tx.Id,
tx.UpdatedAddresses,
updatedAddresses,
msg
);
}
}
return txEvaluations.Select(te => te.Item2);
}
public override string ToString()
{
return Hash.ToString();
}
public void GetObjectData(
SerializationInfo info,
StreamingContext context
)
{
bool includeHash = false;
bool includeTransactionData = false;
if (context.Context is BlockSerializationContext serialCtx)
{
includeHash = serialCtx.IncludeHash;
includeTransactionData = serialCtx.IncludeTransactionData;
}
RawBlock rawBlock = ToRawBlock(includeHash, includeTransactionData);
rawBlock.GetObjectData(info, context);
}
internal void Validate(DateTimeOffset currentTime)
{
if (currentTime + TimestampThreshold < Timestamp)
{
throw new InvalidBlockTimestampException(
$"The block #{Index}'s timestamp ({Timestamp}) is " +
$"later than now ({currentTime}, " +
$"threshold: {TimestampThreshold})."
);
}
if (Index < 0)
{
throw new InvalidBlockIndexException(
$"index must be 0 or more, but its index is {Index}."
);
}
if (Index == 0)
{
if (Difficulty != 0)
{
throw new InvalidBlockDifficultyException(
"difficulty must be 0 for the genesis block, " +
$"but its difficulty is {Difficulty}."
);
}
if (PreviousHash != null)
{
throw new InvalidBlockPreviousHashException(
"previous hash must be empty for the genesis block."
);
}
}
else
{
if (Difficulty < 1)
{
throw new InvalidBlockDifficultyException(
"difficulty must be more than 0 (except of " +
"the genesis block), but its difficulty is " +
$"{Difficulty}."
);
}
if (PreviousHash == null)
{
throw new InvalidBlockPreviousHashException(
"previous hash must be present except of " +
"the genesis block."
);
}
}
if (!Hash.Satisfies(Difficulty))
{
throw new InvalidBlockNonceException(
$"hash ({Hash}) with the nonce ({Nonce}) does not " +
$"satisfy its difficulty level {Difficulty}."
);
}
foreach (Transaction<T> tx in Transactions)
{
tx.Validate();
}
}
internal RawBlock ToRawBlock(
bool includeHash,
bool includeTransactionData
)
{
IEnumerable transactions =
Transactions.Select(
tx => includeTransactionData ?
tx.ToRawTransaction(true) as object :
tx.Id.ToByteArray() as object
);
var rawBlock = new RawBlock(
index: Index,
timestamp: Timestamp.ToString(TimestampFormat),
nonce: Nonce.ToByteArray(),
miner: Miner?.ToByteArray(),
difficulty: Difficulty,
transactions: transactions,
previousHash: PreviousHash?.ToByteArray()
);
if (includeHash)
{
rawBlock = rawBlock.AddHash(Hash.ToByteArray());
}
return rawBlock;
}
private struct BlockSerializationContext
{
public BlockSerializationContext(bool hash, bool transactionData)
{
IncludeHash = hash;
IncludeTransactionData = transactionData;
}
internal bool IncludeHash { get; }
internal bool IncludeTransactionData { get; }
}
}
}