forked from planetarium/libplanet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestUtils.cs
729 lines (666 loc) · 28.9 KB
/
TestUtils.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Numerics;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Text.Json.JsonDiffPatch.Xunit;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Bencodex.Types;
using DiffPlex.DiffBuilder;
using DiffPlex.DiffBuilder.Model;
using Libplanet.Action;
using Libplanet.Action.Sys;
using Libplanet.Assets;
using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies;
using Libplanet.Blockchain.Renderers;
using Libplanet.Blockchain.Renderers.Debug;
using Libplanet.Blocks;
using Libplanet.Consensus;
using Libplanet.Crypto;
using Libplanet.Store;
using Libplanet.Store.Trie;
using Libplanet.Tx;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;
using Random = System.Random;
namespace Libplanet.Tests
{
public static class TestUtils
{
public static readonly PrivateKey GenesisProposer = PrivateKey.FromString(
"2a15e7deaac09ce631e1faa184efadb175b6b90989cf1faed9dfc321ad1db5ac");
public static readonly PrivateKey ChainPrivateKey = PrivateKey.FromString(
"cf36ecf9e47c879a0dbf46b2ecd83fd276182ade0265825e3b8c6ba214467b76");
public static readonly List<PrivateKey> ValidatorPrivateKeys = new List<PrivateKey>
{
PrivateKey.FromString(
"e5792a1518d9c7f7ecc35cd352899211a05164c9dde059c9811e0654860549ef"),
PrivateKey.FromString(
"91d61834be824c952754510fcf545180eca38e036d3d9b66564f0667b30d5b93"),
PrivateKey.FromString(
"b17c919b07320edfb3e6da2f1cfed75910322de2e49377d6d4d226505afca550"),
PrivateKey.FromString(
"91602d7091c5c7837ac8e71a8d6b1ed1355cfe311914d9a76107899add0ad56a"),
}; // The ordering here should match the ordering by address.
public static readonly ValidatorSet ValidatorSet = new ValidatorSet(
ValidatorPrivateKeys.Select(
privateKey => new Validator(privateKey.PublicKey, BigInteger.One)).ToList());
// These keys are used to generate a situation that two adjacent peers generated by these
// keys are in different buckets in routing table of Kademlia Protocol.
public static readonly PrivateKey[] AdjacentKeys = new PrivateKey[10]
{
new PrivateKey(new byte[]
{
0x98, 0x66, 0x98, 0x50, 0x72, 0x8c, 0x6c, 0x41, 0x0b, 0xf4,
0x2c, 0x45, 0xfe, 0x7c, 0x49, 0x23, 0x2d, 0x14, 0xcf, 0xb5,
0x5b, 0x78, 0x4d, 0x81, 0x35, 0xae, 0x40, 0x4c, 0x7c, 0x24,
0x3f, 0xc7,
}),
new PrivateKey(new byte[]
{
0xd2, 0x47, 0x6f, 0xf3, 0x1a, 0xf3, 0x4f, 0x00, 0x5a, 0xe2,
0xd9, 0x24, 0x18, 0x60, 0xe9, 0xb9, 0xd0, 0x42, 0x9a, 0x30,
0x67, 0x81, 0x2b, 0x00, 0xf0, 0x45, 0x87, 0x70, 0x3f, 0xd5,
0x51, 0x93,
}),
new PrivateKey(new byte[]
{
0x9e, 0xd4, 0xdb, 0x20, 0xfd, 0x4d, 0x1c, 0x52, 0x55, 0x24,
0x80, 0x52, 0xc6, 0x1f, 0x95, 0x1c, 0xf1, 0x49, 0x4a, 0xd6,
0xf9, 0x1d, 0x29, 0xb9, 0xa3, 0x0b, 0x0e, 0x0c, 0xc8, 0xaa,
0xb0, 0x79,
}),
new PrivateKey(new byte[]
{
0x0a, 0x4f, 0x84, 0xeb, 0x69, 0x4d, 0xc1, 0xf0, 0xf3, 0x15,
0x97, 0xcc, 0x95, 0x53, 0x66, 0x01, 0x27, 0x2a, 0xc1, 0xcd,
0x0f, 0xf6, 0x02, 0x6f, 0x08, 0x29, 0x1d, 0xd0, 0x79, 0xda,
0xcc, 0x36,
}),
new PrivateKey(new byte[]
{
0x68, 0xbd, 0xc3, 0xda, 0xf1, 0xa1, 0x67, 0x9c, 0xa1, 0x1e,
0x5a, 0x64, 0x10, 0xe6, 0x74, 0x95, 0x77, 0xbc, 0x47, 0x1c,
0x55, 0xd7, 0x38, 0xa3, 0x67, 0x48, 0x73, 0x08, 0xcd, 0x74,
0x3c, 0x4b,
}),
new PrivateKey(new byte[]
{
0x02, 0x40, 0xa6, 0x72, 0xdd, 0xc0, 0x65, 0x04, 0x54, 0xfb,
0x34, 0x29, 0x05, 0xaa, 0xa6, 0x1e, 0x94, 0x30, 0x89, 0x26,
0xfd, 0x30, 0xd1, 0x61, 0x8c, 0x1b, 0x75, 0x79, 0x86, 0xf8,
0x8a, 0x6a,
}),
new PrivateKey(new byte[]
{
0x74, 0x6d, 0x07, 0xb0, 0xb9, 0x7e, 0x0d, 0xb9, 0x1f, 0x96,
0x59, 0xe1, 0x20, 0x8d, 0x31, 0xac, 0x94, 0xcd, 0xc8, 0xaa,
0x0c, 0x0d, 0xeb, 0x35, 0xab, 0x93, 0x95, 0x65, 0xae, 0x5f,
0xc1, 0x4b,
}),
new PrivateKey(new byte[]
{
0xc5, 0x1e, 0xc4, 0x6c, 0x81, 0x6a, 0x9d, 0x41, 0xc2, 0xae,
0x61, 0x51, 0x0f, 0x97, 0xd0, 0x0e, 0x3a, 0x7b, 0x86, 0xd3,
0xbd, 0xf1, 0x1e, 0xfe, 0x55, 0x67, 0x8f, 0x31, 0x92, 0xc1,
0xdf, 0xe4,
}),
new PrivateKey(new byte[]
{
0x0a, 0x9e, 0x67, 0x59, 0x4b, 0xfc, 0xdd, 0x81, 0xcd, 0x86,
0xa0, 0xf1, 0x79, 0x74, 0x56, 0x0f, 0x56, 0x85, 0xff, 0x3f,
0x75, 0xd3, 0xbc, 0xaa, 0xf0, 0xa2, 0xec, 0xdb, 0x05, 0xa5,
0x59, 0x79,
}),
new PrivateKey(new byte[]
{
0x3d, 0xae, 0x2d, 0x2f, 0x87, 0x1b, 0x11, 0xaa, 0x41, 0xbd,
0xec, 0x81, 0x4c, 0x4d, 0x27, 0xf3, 0xba, 0xd9, 0x1f, 0x61,
0xc3, 0x57, 0xab, 0x43, 0xa0, 0x0c, 0x63, 0x1b, 0x2b, 0x15,
0x13, 0xf2,
}),
};
private static readonly Random _random = new Random();
public static void AssertBytesEqual(byte[] expected, byte[] actual)
{
if (expected is null)
{
Assert.Null(actual);
return;
}
Assert.NotNull(actual);
string msg;
if (expected.LongLength < 1024 && actual.LongLength < 1024 &&
expected.All(b => b < 0x80) && actual.All(b => b < 0x80))
{
// If both arrays can be ASCII encoding, print them directly.
string expectedStr = Encoding.ASCII.GetString(expected);
string actualStr = Encoding.ASCII.GetString(actual);
msg = $@"Two byte arrays do not equal
Expected length: ({expected.LongLength}) {expectedStr}
Actual length: ({actual.LongLength}) {actualStr}";
}
else
{
string expectedRepr = Repr(expected);
string actualRepr = Repr(actual);
msg = $@"Two byte arrays do not equal
Expected (C# array lit): new byte[{expected.LongLength}] {{ {expectedRepr} }}
Actual (C# array lit): new byte[{actual.LongLength}] {{ {actualRepr} }}";
}
if (!expected.SequenceEqual(actual))
{
throw new AssertActualExpectedException(
ByteUtil.Hex(expected),
ByteUtil.Hex(actual),
msg,
"Expected (hex)",
"Actual (hex)"
);
}
string Repr(byte[] bytes)
{
const int limit = 1024;
if (bytes.LongLength > limit)
{
bytes = bytes.Take(limit).ToArray();
}
string s = string.Join(
", ",
bytes.Select(b => b < 0x10 ? $"0x0{b:x}" : $"0x{b:x}")
);
return bytes.LongLength > limit ? $"{s}, ..." : s;
}
}
public static void AssertBytesEqual(
string expectedHex,
ImmutableArray<byte> actual
) =>
AssertBytesEqual(ByteUtil.ParseHex(expectedHex), actual.ToArray());
public static void AssertBytesEqual(
ImmutableArray<byte> expected,
ImmutableArray<byte> actual
) =>
AssertBytesEqual(expected.ToArray(), actual.ToArray());
public static void AssertBytesEqual(
ImmutableArray<byte>? expected,
ImmutableArray<byte>? actual
) =>
AssertBytesEqual(expected?.ToArray(), actual?.ToArray());
public static void AssertBytesEqual(string expectedHex, TxId actual) =>
AssertBytesEqual(ByteUtil.ParseHex(expectedHex), actual.ToByteArray());
public static void AssertBytesEqual(TxId expected, TxId actual) =>
AssertBytesEqual(expected.ToByteArray(), actual.ToByteArray());
public static void AssertBytesEqual(TxId? expected, TxId? actual) =>
AssertBytesEqual(expected?.ToByteArray(), actual?.ToByteArray());
public static void AssertBytesEqual(BlockHash expected, BlockHash actual) =>
AssertBytesEqual(expected.ToByteArray(), actual.ToByteArray());
public static void AssertBytesEqual(BlockHash? expected, BlockHash? actual) =>
AssertBytesEqual(expected?.ToByteArray(), actual?.ToByteArray());
public static void AssertBytesEqual<T>(
HashDigest<T> expected,
HashDigest<T> actual
)
where T : HashAlgorithm
{
AssertBytesEqual(expected.ToByteArray(), actual.ToByteArray());
}
public static void AssertBytesEqual<T>(
HashDigest<T>? expected,
HashDigest<T>? actual
)
where T : HashAlgorithm
{
AssertBytesEqual(expected?.ToByteArray(), actual?.ToByteArray());
}
public static void AssertBytesEqual(Nonce expected, Nonce actual) =>
AssertBytesEqual(expected.ToByteArray(), actual.ToByteArray());
public static void AssertBytesEqual(Nonce? expected, Nonce? actual) =>
AssertBytesEqual(expected?.ToByteArray(), actual?.ToByteArray());
public static void AssertBytesEqual(Address expected, Address actual) =>
AssertBytesEqual(expected.ToByteArray(), actual.ToByteArray());
public static void AssertBytesEqual(Address? expected, Address? actual) =>
AssertBytesEqual(expected?.ToByteArray(), actual?.ToByteArray());
public static void AssertBytesEqual(KeyBytes expected, KeyBytes actual) =>
AssertBytesEqual(expected.ToByteArray(), actual.ToByteArray());
public static void AssertBytesEqual(KeyBytes? expected, KeyBytes? actual) =>
AssertBytesEqual(expected?.ToByteArray(), actual?.ToByteArray());
public static void AssertBencodexEqual(IValue expected, IValue actual)
{
bool equal = (expected is null && actual is null) ||
(expected is Null && actual is Null) ||
(expected is Bencodex.Types.Boolean && actual is Bencodex.Types.Boolean &&
expected.Equals(actual)) ||
(expected is Integer && actual is Integer && expected.Equals(actual)) ||
(expected is Binary && actual is Binary && expected.Equals(actual)) ||
(expected is Text && actual is Text && expected.Equals(actual)) ||
(expected is List && actual is List && expected.Equals(actual)) ||
(expected is Dictionary && actual is Dictionary && expected.Equals(actual));
if (equal)
{
return;
}
string expectedInspection = expected?.ToString() ?? "(null)";
string actualInspection = actual?.ToString() ?? "(null)";
DiffPaneModel diffModel = InlineDiffBuilder.Diff(expectedInspection, actualInspection);
var prefixes = new Dictionary<ChangeType, string>
{
[ChangeType.Deleted] = "-",
[ChangeType.Inserted] = "+",
[ChangeType.Unchanged] = " ",
};
string diff = string.Join(
Environment.NewLine,
diffModel.Lines.Select(line =>
(prefixes.TryGetValue(line.Type, out string prefix) ? prefix : " ") + line.Text
)
);
throw new XunitException(
"Two Bencodex values are not equal.\n--- Expected\n+++ Actual\n\n" + diff
);
}
public static void AssertSorted<T>(IEnumerable<T> expected)
where T : IComparable<T>
{
// Was implemented by `.Zip()`, but the method has different
// overloads in .NET Framework, so cannot be used.
T[] arr = expected.ToArray();
foreach ((int i, (T first, T second)) in Enumerable
.Range(0, arr.Length - 1)
.Select(i => (i, (arr[i], arr[i + 1]))))
{
string errorMessage =
$"Given list is not sorted since item {first} at {i} " +
$"is greater than item {second} at {i + 1}";
Assert.True(first.CompareTo(second) <= 0, errorMessage);
}
}
public static void AssertBlockMetadataEqual(IBlockMetadata expected, IBlockMetadata actual)
{
Assert.NotNull(expected);
Assert.NotNull(actual);
Assert.Equal(expected.ProtocolVersion, actual.ProtocolVersion);
Assert.Equal(expected.Index, actual.Index);
Assert.Equal(expected.Timestamp, actual.Timestamp);
AssertBytesEqual(expected.Miner, actual.Miner);
AssertBytesEqual(expected.PreviousHash, actual.PreviousHash);
AssertBytesEqual(expected.TxHash, actual.TxHash);
}
public static void AssertBlockContentsEqual<T>(
IBlockContent<T> expected,
IBlockContent<T> actual
)
where T : IAction, new()
{
AssertBlockMetadataEqual(expected, actual);
Assert.Equal(expected.Transactions, actual.Transactions);
}
public static void AssertPreEvaluationBlockHeadersEqual(
IPreEvaluationBlockHeader expected,
IPreEvaluationBlockHeader actual
)
{
AssertBlockMetadataEqual(expected, actual);
AssertBytesEqual(expected.PreEvaluationHash, actual.PreEvaluationHash);
}
public static void AssertPreEvaluationBlocksEqual<T>(
IPreEvaluationBlock<T> expected,
IPreEvaluationBlock<T> actual
)
where T : IAction, new()
{
AssertPreEvaluationBlockHeadersEqual(expected, actual);
AssertBlockContentsEqual(expected, actual);
}
public static byte[] GetRandomBytes(int size)
{
var bytes = new byte[size];
_random.NextBytes(bytes);
return bytes;
}
public static BlockCommit CreateBlockCommit<T>(
Block<T> block,
bool deterministicTimestamp = false)
where T : IAction, new() =>
block.Index > 0 &&
block.ProtocolVersion > BlockMetadata.PoWProtocolVersion
? CreateBlockCommit(block.Hash, block.Index, 0, deterministicTimestamp)
: null;
public static BlockCommit CreateBlockCommit(
BlockHash blockHash,
long height,
int round,
bool deterministicTimestamp = false)
{
// Index #1 block cannot have lastCommit: There was no consensus of genesis block.
if (height == 0)
{
return null;
}
// Using the unix epoch time as the timestamp of the vote if deterministicTimestamp is
// flagged for getting a deterministic random value from PreEvaluationHash.
var votes = ValidatorPrivateKeys.Select(key => new VoteMetadata(
height,
round,
blockHash,
deterministicTimestamp ? DateTimeOffset.UnixEpoch : DateTimeOffset.UtcNow,
key.PublicKey,
VoteFlag.PreCommit).Sign(key)).ToImmutableArray();
return new BlockCommit(
height, round, blockHash, votes);
}
public static PreEvaluationBlock<T> ProposeGenesis<T>(
PublicKey proposer = null,
IReadOnlyList<Transaction<T>> transactions = null,
ValidatorSet validatorSet = null,
DateTimeOffset? timestamp = null,
int protocolVersion = Block<T>.CurrentProtocolVersion
)
where T : IAction, new()
{
var txs = transactions?.ToList() ?? new List<Transaction<T>>();
long nonce = 0;
validatorSet = validatorSet ?? ValidatorSet;
txs.AddRange(
validatorSet.Validators.Select(
validator => Transaction<T>.Create(
nonce++,
GenesisProposer,
null,
systemAction: new Initialize(
validatorSet: validatorSet,
states: ImmutableDictionary.Create<Address, IValue>()
),
timestamp: DateTimeOffset.MinValue)));
txs = txs.OrderBy(tx => tx.Id).ToList();
var content = new BlockContent<T>(
new BlockMetadata(
protocolVersion: protocolVersion,
index: 0,
timestamp: timestamp ??
new DateTimeOffset(2018, 11, 29, 0, 0, 0, TimeSpan.Zero),
miner: (proposer ?? GenesisProposer.PublicKey).ToAddress(),
publicKey: protocolVersion >= 2 ? proposer ?? GenesisProposer.PublicKey : null,
previousHash: null,
txHash: BlockContent<T>.DeriveTxHash(txs),
lastCommit: null),
transactions: txs);
return content.Propose();
}
public static Block<T> ProposeGenesisBlock<T>(
PrivateKey miner,
IReadOnlyList<Transaction<T>> transactions = null,
DateTimeOffset? timestamp = null,
int protocolVersion = Block<T>.CurrentProtocolVersion,
HashDigest<SHA256> stateRootHash = default
)
where T : IAction, new()
{
PreEvaluationBlock<T> preEval = ProposeGenesis(
miner?.PublicKey,
transactions,
null,
timestamp,
protocolVersion
);
return preEval.Sign(miner, stateRootHash);
}
public static Block<T> ProposeGenesisBlock<T>(
PreEvaluationBlock<T> preEval,
PrivateKey privateKey,
IAction blockAction = null,
Predicate<Currency> nativeTokenPredicate = null)
where T : IAction, new()
{
return preEval.Sign(
privateKey,
BlockChain<T>.DetermineGenesisStateRootHash(
preEval,
blockAction,
nativeTokenPredicate ?? (Predicate<Currency>)(_ => true),
out _));
}
public static PreEvaluationBlock<T> ProposeNext<T>(
Block<T> previousBlock,
IReadOnlyList<Transaction<T>> transactions = null,
PublicKey miner = null,
TimeSpan? blockInterval = null,
int protocolVersion = Block<T>.CurrentProtocolVersion,
BlockCommit lastCommit = null
)
where T : IAction, new()
{
var txs = transactions is null
? new List<Transaction<T>>()
: transactions.OrderBy(tx => tx.Id).ToList();
var content = new BlockContent<T>(
new BlockMetadata(
protocolVersion: protocolVersion,
index: previousBlock.Index + 1,
timestamp: previousBlock.Timestamp.Add(
blockInterval ?? TimeSpan.FromSeconds(15)),
miner: miner?.ToAddress() ?? previousBlock.Miner,
publicKey: protocolVersion >= 2 ? miner ?? previousBlock.PublicKey : null,
previousHash: previousBlock.Hash,
txHash: BlockContent<T>.DeriveTxHash(txs),
lastCommit: lastCommit),
transactions: txs);
var preEval = content.Propose();
preEval.ValidateTimestamp();
return preEval;
}
public static Block<T> ProposeNextBlock<T>(
Block<T> previousBlock,
PrivateKey miner,
IReadOnlyList<Transaction<T>> txs = null,
TimeSpan? blockInterval = null,
int protocolVersion = Block<T>.CurrentProtocolVersion,
HashDigest<SHA256> stateRootHash = default,
BlockCommit lastCommit = null
)
where T : IAction, new()
{
Skip.IfNot(
Environment.GetEnvironmentVariable("XUNIT_UNITY_RUNNER") is null,
"Flaky test : Libplanet.Blocks.InvalidBlockSignatureException"
);
PreEvaluationBlock<T> preEval = ProposeNext(
previousBlock,
txs,
miner?.PublicKey,
blockInterval,
protocolVersion,
lastCommit);
return preEval.Sign(miner, stateRootHash);
}
/// <summary>
/// Creates a <see cref="BlockChain{T}"/> instance.
/// </summary>
/// <param name="policy">A <see cref="BlockPolicy{T}"/> of the chain.</param>
/// <param name="store">An <see cref="IStore"/> instance to store blocks and txs.</param>
/// <param name="stateStore">An <see cref="IStateStore"/> instance to store states.</param>
/// <param name="actions"><see cref="Action{T}"/>s to be included in genesis block.
/// Works only if <paramref name="genesisBlock"/> is null.</param>
/// <param name="validatorSet"><see cref="ValidatorSet"/> to be included in genesis block.
/// Works only if <paramref name="genesisBlock"/> is null.</param>
/// <param name="privateKey">A <see cref="PrivateKey"/> to sign genesis actions.
/// Works only if <paramref name="genesisBlock"/> is null.</param>
/// <param name="timestamp"><see cref="DateTimeOffset"/> of the genesis block.
/// Works only if <paramref name="genesisBlock"/> is null.</param>
/// <param name="renderers">
/// An <see cref="IEnumerable{T}"/> of <see cref="IRenderer{T}"/>s.</param>
/// <param name="genesisBlock">Genesis <see cref="Block{T}"/> of the chain.
/// If null is given, a genesis will be generated.</param>
/// <param name="protocolVersion">Block protocol version of genesis block.</param>
/// <typeparam name="T">An <see cref="IAction"/> type.</typeparam>
/// <returns>A <see cref="BlockChain{T}"/> instance.</returns>
public static BlockChain<T> MakeBlockChain<T>(
IBlockPolicy<T> policy,
IStore store,
IStateStore stateStore,
IEnumerable<T> actions = null,
ValidatorSet validatorSet = null,
PrivateKey privateKey = null,
DateTimeOffset? timestamp = null,
IEnumerable<IRenderer<T>> renderers = null,
Block<T> genesisBlock = null,
int protocolVersion = Block<T>.CurrentProtocolVersion
)
where T : IAction, new()
{
actions = actions ?? ImmutableArray<T>.Empty;
privateKey = privateKey ?? ChainPrivateKey;
var txs = new[]
{
Transaction<T>.Create(
0,
privateKey,
null,
actions,
timestamp: timestamp ?? DateTimeOffset.MinValue),
};
if (genesisBlock is null)
{
var preEval = ProposeGenesis(
GenesisProposer.PublicKey,
txs,
validatorSet,
timestamp,
protocolVersion);
var stateRootHash = BlockChain<T>.DetermineGenesisStateRootHash(
preEval,
policy.BlockAction,
policy.NativeTokens.Contains,
out _);
genesisBlock = protocolVersion < 2
? new Block<T>(
preEval,
(
stateRootHash,
null,
preEval.Header.DeriveBlockHash(stateRootHash, null)
))
: preEval.Sign(GenesisProposer, stateRootHash);
}
ValidatingActionRenderer<T> validator = null;
#pragma warning disable S1121
var chain = BlockChain<T>.Create(
policy,
new VolatileStagePolicy<T>(),
store,
stateStore,
genesisBlock,
renderers: renderers ?? new[] { validator = new ValidatingActionRenderer<T>() }
);
#pragma warning restore S1121
if (validator != null)
{
validator.BlockChain = chain;
}
return chain;
}
public static async Task AssertThatEventually(
Expression<Func<bool>> condition,
TimeSpan timeout,
TimeSpan delay,
ITestOutputHelper output = null,
string conditionLabel = null
)
{
Func<bool> conditionFunc = condition.Compile();
DateTimeOffset started = DateTimeOffset.UtcNow;
DateTimeOffset until = started + timeout;
while (!conditionFunc() && DateTimeOffset.UtcNow <= until)
{
output?.WriteLine(
"[{0}/{1}] Waiting for {2}...",
DateTimeOffset.UtcNow - started,
timeout,
conditionLabel is string c1
? c1
: $"satisfying the condition ({condition.Body})"
);
await Task.Delay(delay);
}
Assert.True(
conditionFunc(),
$"Waited {timeout} but the condition (" +
(conditionLabel is string l ? l : condition.Body.ToString()) +
") has never been satisfied."
);
output?.WriteLine(
"[{0}/{1}] Done {2}...",
DateTimeOffset.UtcNow - started,
timeout,
conditionLabel is string c2 ? c2 : $"satisfying the condition ({condition.Body})"
);
}
public static Task AssertThatEventually(
Expression<Func<bool>> condition,
int timeoutMilliseconds,
int delayMilliseconds = 100,
ITestOutputHelper output = null,
string conditionLabel = null
) =>
AssertThatEventually(
condition,
TimeSpan.FromMilliseconds(timeoutMilliseconds),
TimeSpan.FromMilliseconds(delayMilliseconds),
output,
conditionLabel
);
public static T BinarySerializeDeserialize<T>(T obj)
where T : ISerializable
{
var formatter = new BinaryFormatter();
using (var memoryStream = new MemoryStream())
{
formatter.Serialize(memoryStream, obj);
memoryStream.Seek(0, SeekOrigin.Begin);
return (T)formatter.Deserialize(memoryStream);
}
}
public static void AssertJsonSerializable<T>(
T obj,
string expectedJson,
bool testDeserializable = true)
where T : IEquatable<T>
{
Skip.IfNot(
Environment.GetEnvironmentVariable("XUNIT_UNITY_RUNNER") is null,
"System.Text.Json 6.0.0+ does not work well with Unity/Mono."
);
var buffer = new MemoryStream();
JsonSerializer.Serialize(buffer, obj);
buffer.Seek(0L, SeekOrigin.Begin);
var options = new JsonSerializerOptions
{
AllowTrailingCommas = true,
ReadCommentHandling = JsonCommentHandling.Skip,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
JsonNode actual = JsonSerializer.SerializeToNode(obj, options);
JsonNode expected = JsonNode.Parse(expectedJson, null, new JsonDocumentOptions
{
AllowTrailingCommas = true,
CommentHandling = JsonCommentHandling.Skip,
});
JsonAssert.Equal(expected, actual, true);
if (testDeserializable)
{
var deserialized = JsonSerializer.Deserialize<T>(expectedJson, options);
Assert.Equal(obj, deserialized);
}
}
}
}