This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 454
/
method.spec.ts
895 lines (810 loc) · 30.8 KB
/
method.spec.ts
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
/*
* Copyright © 2021 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/
import { codec } from '@liskhq/lisk-codec';
import { utils } from '@liskhq/lisk-cryptography';
import { ValidatorsMethod, ValidatorsModule } from '../../../../src/modules/validators';
import {
MODULE_NAME_VALIDATORS,
EMPTY_KEY,
INVALID_BLS_KEY,
KeyRegResult,
ADDRESS_LENGTH,
BLS_PUBLIC_KEY_LENGTH,
ED25519_PUBLIC_KEY_LENGTH,
} from '../../../../src/modules/validators/constants';
import * as generatorList from '../../../fixtures/config/devnet/validators_for_first_round.json';
import {
MethodContext,
createNewMethodContext,
} from '../../../../src/state_machine/method_context';
import { EventQueue } from '../../../../src/state_machine';
import { InMemoryPrefixedStateDB } from '../../../../src/testing/in_memory_prefixed_state';
import { PrefixedStateReadWriter } from '../../../../src/state_machine/prefixed_state_read_writer';
import {
ValidatorKeys,
ValidatorKeysStore,
} from '../../../../src/modules/validators/stores/validator_keys';
import { BLSKeyStore } from '../../../../src/modules/validators/stores/bls_keys';
import {
generatorKeyRegDataSchema,
GeneratorKeyRegistrationEvent,
} from '../../../../src/modules/validators/events/generator_key_registration';
import {
blsKeyRegDataSchema,
BLSKeyRegistrationEvent,
} from '../../../../src/modules/validators/events/bls_key_registration';
import { ValidatorsParamsStore } from '../../../../src/modules/validators/stores/validators_params';
describe('ValidatorsModuleMethod', () => {
let validatorsMethod: ValidatorsMethod;
let validatorsModule: ValidatorsModule;
let methodContext: MethodContext;
let stateStore: PrefixedStateReadWriter;
let validatorsSubStore: ValidatorKeysStore;
let blsKeysSubStore: BLSKeyStore;
let validatorsParamsSubStore: ValidatorsParamsStore;
const blockTime = 10;
const genesisConfig: any = { blockTime };
const moduleConfig: any = {};
const address = utils.getRandomBytes(20);
const generatorKey = utils.getRandomBytes(32);
const proofOfPossession = Buffer.from(
'88bb31b27eae23038e14f9d9d1b628a39f5881b5278c3c6f0249f81ba0deb1f68aa5f8847854d6554051aa810fdf1cdb02df4af7a5647b1aa4afb60ec6d446ee17af24a8a50876ffdaf9bf475038ec5f8ebeda1c1c6a3220293e23b13a9a5d26',
'hex',
);
const blsKey = Buffer.from(
'b301803f8b5ac4a1133581fc676dfedc60d891dd5fa99028805e5ea5b08d3491af75d0707adab3b70c6a6a580217bf81',
'hex',
);
const invalidProofOfPossession = Buffer.from(
'0x88bb31b27eae23038e14f9d9d1b628a39f5881b5278c3c6f0249f81ba0deb1f68aa5f8847854d6554051aa810fdf1cdb02df4af7a5647b1aa4afb60ec6d446ee17af24a8a50876ffdaf9bf475038ec5f8ebeda1c1c6a3220293e23b13a9a5d26',
'hex',
);
const invalidAddressShort = utils.getRandomBytes(19);
const invalidAddressLong = utils.getRandomBytes(21);
const invalidGeneratorKeyShort = utils.getRandomBytes(31);
const invalidGeneratorKeyLong = utils.getRandomBytes(33);
const invalidBlsKeyShort = utils.getRandomBytes(47);
const invalidBlsKeyLong = utils.getRandomBytes(49);
beforeAll(async () => {
validatorsModule = new ValidatorsModule();
await validatorsModule.init({ genesisConfig, moduleConfig });
});
beforeEach(() => {
validatorsMethod = new ValidatorsMethod(validatorsModule.stores, validatorsModule.events);
stateStore = new PrefixedStateReadWriter(new InMemoryPrefixedStateDB());
validatorsSubStore = validatorsModule.stores.get(ValidatorKeysStore);
blsKeysSubStore = validatorsModule.stores.get(BLSKeyStore);
validatorsParamsSubStore = validatorsModule.stores.get(ValidatorsParamsStore);
methodContext = new MethodContext({
stateStore,
eventQueue: new EventQueue(0),
contextStore: new Map<string, unknown>(),
});
});
describe('registerValidatorKeys', () => {
beforeEach(() => {
jest.spyOn(methodContext.eventQueue, 'add');
});
it('should be able to create new validator account if validator address does not exist, bls key is not registered and proof of possession is valid', async () => {
const generatorEventData = codec.encode(generatorKeyRegDataSchema, {
generatorKey,
result: KeyRegResult.SUCCESS,
});
const blsEventData = codec.encode(blsKeyRegDataSchema, {
blsKey,
proofOfPossession,
result: KeyRegResult.SUCCESS,
});
await expect(
validatorsModule.method.registerValidatorKeys(
methodContext,
address,
blsKey,
generatorKey,
proofOfPossession,
),
).resolves.toBe(true);
const returnedAccount = await validatorsSubStore.get(methodContext, address);
const returnedAddress = await blsKeysSubStore.get(methodContext, blsKey);
expect(returnedAccount).toStrictEqual({ generatorKey, blsKey });
expect(returnedAddress).toStrictEqual({ address });
expect(methodContext.eventQueue.add).toHaveBeenNthCalledWith(
1,
MODULE_NAME_VALIDATORS,
validatorsModule.events.get(GeneratorKeyRegistrationEvent).name,
generatorEventData,
[address],
false,
);
expect(methodContext.eventQueue.add).toHaveBeenNthCalledWith(
2,
MODULE_NAME_VALIDATORS,
validatorsModule.events.get(BLSKeyRegistrationEvent).name,
blsEventData,
[address],
false,
);
});
it('should not be able to create new validator account if validator address already exists, bls key is not registered and proof of possession is valid', async () => {
const validatorAccount = {
generatorKey,
blsKey,
};
await validatorsSubStore.set(methodContext, address, validatorAccount);
const generatorEventData = codec.encode(generatorKeyRegDataSchema, {
generatorKey,
result: KeyRegResult.ALREADY_VALIDATOR,
});
await expect(
validatorsModule.method.registerValidatorKeys(
methodContext,
address,
blsKey,
generatorKey,
proofOfPossession,
),
).rejects.toThrow('This address is already registered as validator.');
expect(methodContext.eventQueue.add).toHaveBeenCalledWith(
MODULE_NAME_VALIDATORS,
validatorsModule.events.get(GeneratorKeyRegistrationEvent).name,
generatorEventData,
[address],
true,
);
});
it('should not be able to create new validator account if validator address does not exist, bls key is already registered and proof of possession is valid', async () => {
await blsKeysSubStore.set(methodContext, blsKey, { address });
const blsEventData = codec.encode(blsKeyRegDataSchema, {
blsKey,
proofOfPossession,
result: KeyRegResult.DUPLICATE_BLS_KEY,
});
await expect(
validatorsModule.method.registerValidatorKeys(
methodContext,
address,
blsKey,
generatorKey,
proofOfPossession,
),
).rejects.toThrow(
`The BLS key ${blsKey.toString('hex')} has already been registered in the chain.`,
);
expect(methodContext.eventQueue.add).toHaveBeenCalledWith(
MODULE_NAME_VALIDATORS,
validatorsModule.events.get(BLSKeyRegistrationEvent).name,
blsEventData,
[address],
true,
);
});
it('should not be able to create new validator account if validator address does not exist, bls key is not registered and proof of possession is invalid', async () => {
const blsEventData = codec.encode(blsKeyRegDataSchema, {
blsKey,
proofOfPossession: invalidProofOfPossession,
result: KeyRegResult.INVALID_POP,
});
await expect(
validatorsModule.method.registerValidatorKeys(
methodContext,
address,
blsKey,
generatorKey,
invalidProofOfPossession,
),
).rejects.toThrow('Invalid proof of possession for the given BLS key.');
expect(methodContext.eventQueue.add).toHaveBeenCalledWith(
MODULE_NAME_VALIDATORS,
validatorsModule.events.get(BLSKeyRegistrationEvent).name,
blsEventData,
[address],
true,
);
});
it('should not be able to register validator keys if validator address does not exist, bls key is not registered and proof of possession is valid but validatorAddress is shorter than 20 bytes', async () => {
await expect(
validatorsModule.method.registerValidatorKeys(
methodContext,
invalidAddressShort,
blsKey,
generatorKey,
proofOfPossession,
),
).rejects.toThrow(`Validator address must be ${ADDRESS_LENGTH} bytes long.`);
await expect(validatorsSubStore.get(methodContext, invalidAddressShort)).rejects.toThrow();
await expect(blsKeysSubStore.get(methodContext, blsKey)).rejects.toThrow();
});
it('should not be able to register validator keys if validator address does not exist, bls key is not registered and proof of possession is valid but validatorAddress is longer than 20 bytes', async () => {
await expect(
validatorsModule.method.registerValidatorKeys(
methodContext,
invalidAddressLong,
blsKey,
generatorKey,
proofOfPossession,
),
).rejects.toThrow(`Validator address must be ${ADDRESS_LENGTH} bytes long.`);
await expect(validatorsSubStore.get(methodContext, invalidAddressLong)).rejects.toThrow();
await expect(blsKeysSubStore.get(methodContext, blsKey)).rejects.toThrow();
});
it('should not be able to register validator keys if validator address does not exist, bls key is not registered and proof of possession is valid but generator key is shorter than 32 bytes', async () => {
await expect(
validatorsModule.method.registerValidatorKeys(
methodContext,
address,
blsKey,
invalidGeneratorKeyShort,
proofOfPossession,
),
).rejects.toThrow();
await expect(validatorsSubStore.get(methodContext, address)).rejects.toThrow();
await expect(blsKeysSubStore.get(methodContext, blsKey)).rejects.toThrow();
});
it('should not be able to register validator keys if validator address does not exist, bls key is not registered and proof of possession is valid but generator key is longer than 32 bytes', async () => {
await expect(
validatorsModule.method.registerValidatorKeys(
methodContext,
address,
blsKey,
invalidGeneratorKeyLong,
proofOfPossession,
),
).rejects.toThrow();
await expect(validatorsSubStore.get(methodContext, address)).rejects.toThrow();
await expect(blsKeysSubStore.get(methodContext, blsKey)).rejects.toThrow();
});
it('should not be able to register validator keys if validator address does not exist, bls key is not registered and proof of possession is valid but bls key is shorter than 48 bytes', async () => {
await expect(
validatorsModule.method.registerValidatorKeys(
methodContext,
address,
invalidBlsKeyShort,
generatorKey,
proofOfPossession,
),
).rejects.toThrow();
await expect(validatorsSubStore.get(methodContext, address)).rejects.toThrow();
await expect(blsKeysSubStore.get(methodContext, invalidBlsKeyShort)).rejects.toThrow();
});
it('should not be able to register validator keys if validator address does not exist, bls key is not registered and proof of possession is valid but bls key is longer than 48 bytes', async () => {
await expect(
validatorsModule.method.registerValidatorKeys(
methodContext,
address,
invalidBlsKeyLong,
generatorKey,
proofOfPossession,
),
).rejects.toThrow();
await expect(validatorsSubStore.get(methodContext, address)).rejects.toThrow();
await expect(blsKeysSubStore.get(methodContext, invalidBlsKeyLong)).rejects.toThrow();
});
});
describe('setValidatorBLSKey', () => {
beforeEach(() => {
jest.spyOn(methodContext.eventQueue, 'add');
});
it('should be able to correctly set bls key for validator if address exists, key is not registered and proof of possession is valid', async () => {
const blsEventData = codec.encode(blsKeyRegDataSchema, {
blsKey,
proofOfPossession,
result: KeyRegResult.SUCCESS,
});
const validatorAccount = {
generatorKey,
blsKey: INVALID_BLS_KEY,
};
await validatorsSubStore.set(methodContext, address, validatorAccount);
const isSet = await validatorsModule.method.setValidatorBLSKey(
methodContext,
address,
blsKey,
proofOfPossession,
);
const setValidatorAccount = await validatorsSubStore.get(methodContext, address);
const hasKey = await blsKeysSubStore.has(methodContext, blsKey);
expect(isSet).toBe(true);
expect(setValidatorAccount.blsKey.toString('hex')).toBe(blsKey.toString('hex'));
expect(hasKey).toBe(true);
expect(methodContext.eventQueue.add).toHaveBeenCalledWith(
MODULE_NAME_VALIDATORS,
validatorsModule.events.get(BLSKeyRegistrationEvent).name,
blsEventData,
[address],
false,
);
});
it('should not be able to set bls key for validator if address does not exist', async () => {
const blsEventData = codec.encode(blsKeyRegDataSchema, {
blsKey,
proofOfPossession,
result: KeyRegResult.NO_VALIDATOR,
});
await expect(
validatorsModule.method.setValidatorBLSKey(
methodContext,
address,
blsKey,
proofOfPossession,
),
).rejects.toThrow(
'This address is not registered as validator. Only validators can register a BLS key.',
);
expect(methodContext.eventQueue.add).toHaveBeenCalledWith(
MODULE_NAME_VALIDATORS,
validatorsModule.events.get(BLSKeyRegistrationEvent).name,
blsEventData,
[address],
true,
);
});
it('should not be able to set bls key for validator if address exists but bls key is already registered', async () => {
const blsEventData = codec.encode(blsKeyRegDataSchema, {
blsKey,
proofOfPossession,
result: KeyRegResult.DUPLICATE_BLS_KEY,
});
const validatorAccount = {
generatorKey,
blsKey,
};
await validatorsSubStore.set(methodContext, address, validatorAccount);
await blsKeysSubStore.set(methodContext, blsKey, { address });
await expect(
validatorsModule.method.setValidatorBLSKey(
methodContext,
address,
blsKey,
proofOfPossession,
),
).rejects.toThrow(
`The BLS key ${blsKey.toString('hex')} has already been registered in the chain.`,
);
expect(methodContext.eventQueue.add).toHaveBeenCalledWith(
MODULE_NAME_VALIDATORS,
validatorsModule.events.get(BLSKeyRegistrationEvent).name,
blsEventData,
[address],
true,
);
});
it('should not be able to set bls key for validator if address exists, key is not registered but proof of possession is invalid', async () => {
const blsEventData = codec.encode(blsKeyRegDataSchema, {
blsKey,
proofOfPossession: invalidProofOfPossession,
result: KeyRegResult.INVALID_POP,
});
const validatorAccount = {
generatorKey,
blsKey: INVALID_BLS_KEY,
};
await validatorsSubStore.set(methodContext, address, validatorAccount);
await expect(
validatorsModule.method.setValidatorBLSKey(
methodContext,
address,
blsKey,
invalidProofOfPossession,
),
).rejects.toThrow('Invalid proof of possession for the given BLS key.');
expect(methodContext.eventQueue.add).toHaveBeenCalledWith(
MODULE_NAME_VALIDATORS,
validatorsModule.events.get(BLSKeyRegistrationEvent).name,
blsEventData,
[address],
true,
);
});
it('should throw and be unable to set bls key for validator if address is shorter than 20 bytes', async () => {
await expect(
validatorsModule.method.setValidatorBLSKey(
methodContext,
invalidAddressShort,
blsKey,
proofOfPossession,
),
).rejects.toThrow(`Validator address must be ${ADDRESS_LENGTH} bytes long.`);
await expect(blsKeysSubStore.get(methodContext, blsKey)).rejects.toThrow();
});
it('should throw and be unable to set bls key for validator if address is longer than 20 bytes', async () => {
await expect(
validatorsModule.method.setValidatorBLSKey(
methodContext,
invalidAddressLong,
blsKey,
proofOfPossession,
),
).rejects.toThrow(`Validator address must be ${ADDRESS_LENGTH} bytes long.`);
await expect(blsKeysSubStore.get(methodContext, blsKey)).rejects.toThrow();
});
it('should throw and be unable to set bls key for validator if bls key is shorter than 48 bytes', async () => {
await expect(
validatorsModule.method.setValidatorBLSKey(
methodContext,
address,
invalidBlsKeyShort,
proofOfPossession,
),
).rejects.toThrow(`BLS public key must be ${BLS_PUBLIC_KEY_LENGTH} bytes long.`);
await expect(blsKeysSubStore.get(methodContext, invalidBlsKeyShort)).rejects.toThrow();
});
it('should throw and be unable to set bls key for validator if bls key is longer than 48 bytes', async () => {
await expect(
validatorsModule.method.setValidatorBLSKey(
methodContext,
address,
invalidBlsKeyLong,
proofOfPossession,
),
).rejects.toThrow(`BLS public key must be ${BLS_PUBLIC_KEY_LENGTH} bytes long.`);
await expect(blsKeysSubStore.get(methodContext, invalidBlsKeyLong)).rejects.toThrow();
});
});
describe('setValidatorGeneratorKey', () => {
beforeEach(() => {
jest.spyOn(methodContext.eventQueue, 'add');
});
it('should be able to correctly set generator key for validator if address exists', async () => {
const generatorEventData = codec.encode(generatorKeyRegDataSchema, {
generatorKey,
result: KeyRegResult.SUCCESS,
});
const validatorAccount = {
generatorKey,
blsKey,
};
await validatorsSubStore.set(methodContext, address, validatorAccount);
const isSet = await validatorsModule.method.setValidatorGeneratorKey(
methodContext,
address,
generatorKey,
);
const setValidatorAccount = await validatorsSubStore.get(methodContext, address);
expect(isSet).toBe(true);
expect(setValidatorAccount.generatorKey.equals(generatorKey)).toBe(true);
expect(methodContext.eventQueue.add).toHaveBeenCalledWith(
MODULE_NAME_VALIDATORS,
validatorsModule.events.get(GeneratorKeyRegistrationEvent).name,
generatorEventData,
[address],
false,
);
});
it('should not be able to set generator key for validator if address does not exist', async () => {
const generatorEventData = codec.encode(generatorKeyRegDataSchema, {
generatorKey,
result: KeyRegResult.NO_VALIDATOR,
});
await expect(
validatorsModule.method.setValidatorGeneratorKey(methodContext, address, generatorKey),
).rejects.toThrow(
'This address is not registered as validator. Only validators can register a generator key.',
);
expect(methodContext.eventQueue.add).toHaveBeenCalledWith(
MODULE_NAME_VALIDATORS,
validatorsModule.events.get(GeneratorKeyRegistrationEvent).name,
generatorEventData,
[address],
true,
);
});
it('should throw error and be unable to set generator key for validator if address is shorter than 20 bytes', async () => {
await expect(
validatorsModule.method.setValidatorGeneratorKey(
methodContext,
invalidAddressShort,
generatorKey,
),
).rejects.toThrow(`Validator address must be ${ADDRESS_LENGTH} bytes long.`);
await expect(validatorsSubStore.get(methodContext, invalidAddressShort)).rejects.toThrow();
});
it('should throw error and be unable to set generator key for validator if address is longer than 20 bytes', async () => {
await expect(
validatorsModule.method.setValidatorGeneratorKey(
methodContext,
invalidAddressLong,
generatorKey,
),
).rejects.toThrow(`Validator address must be ${ADDRESS_LENGTH} bytes long.`);
await expect(validatorsSubStore.get(methodContext, invalidAddressLong)).rejects.toThrow();
});
it('should throw error and be unable to set generator key for validator if generator key is shorter than 32 bytes', async () => {
await expect(
validatorsModule.method.setValidatorGeneratorKey(
methodContext,
address,
invalidGeneratorKeyShort,
),
).rejects.toThrow(`Generator key must be ${ED25519_PUBLIC_KEY_LENGTH} bytes long.`);
await expect(validatorsSubStore.get(methodContext, address)).rejects.toThrow();
});
it('should throw error and be unable to set generator key for validator if generator key is longer than 32 bytes', async () => {
await expect(
validatorsModule.method.setValidatorGeneratorKey(
methodContext,
address,
invalidGeneratorKeyLong,
),
).rejects.toThrow(`Generator key must be ${ED25519_PUBLIC_KEY_LENGTH} bytes long.`);
await expect(validatorsSubStore.get(methodContext, address)).rejects.toThrow();
});
});
describe('isKeyRegistered', () => {
it('should return true if bls key is already registered', async () => {
await blsKeysSubStore.set(methodContext, blsKey, { address });
await expect(validatorsModule.method.isKeyRegistered(methodContext, blsKey)).resolves.toBe(
true,
);
});
it('should return false if bls key is not registered', async () => {
await expect(validatorsModule.method.isKeyRegistered(methodContext, blsKey)).resolves.toBe(
false,
);
});
});
describe('getGeneratorsBetweenTimestamps', () => {
beforeEach(async () => {
await validatorsParamsSubStore.set(methodContext, EMPTY_KEY, {
certificateThreshold: BigInt(68),
preCommitThreshold: BigInt(68),
validators: generatorList.map(addr => ({
address: Buffer.from(addr, 'hex'),
bftWeight: BigInt(1),
blsKey: Buffer.alloc(98),
generatorKey: Buffer.alloc(32),
})),
});
});
it('should be able to return if input timestamps are valid', async () => {
await validatorsParamsSubStore.set(methodContext, EMPTY_KEY, {
certificateThreshold: BigInt(68),
preCommitThreshold: BigInt(68),
validators: generatorList.map(addr => ({
address: Buffer.from(addr, 'hex'),
bftWeight: BigInt(1),
blsKey: Buffer.alloc(98),
generatorKey: Buffer.alloc(32),
})),
});
await expect(
validatorsModule.method.getGeneratorsBetweenTimestamps(methodContext, 5, 1834),
).resolves.toBeObject();
});
it('should be able to return generators with at least one generator assigned more than one slot if input timestamps are valid and difference between input timestamps is greater than one round', async () => {
const validatorsPerRound = 101;
const timePerRound = validatorsPerRound * blockTime;
const result = await validatorsModule.method.getGeneratorsBetweenTimestamps(
methodContext,
0,
timePerRound + 2 * blockTime + 1,
);
let genWithCountGreaterThanOne = 0;
for (const generatorAddress of Object.keys(result)) {
if (result[generatorAddress] > 1) {
genWithCountGreaterThanOne += 1;
}
}
expect(genWithCountGreaterThanOne).toBeGreaterThan(0);
});
it('should be able to return with all generators assigned at least 2 slots and at least one generator assigned more than 2 slots if input timestamps are valid and difference between input timestamps is greater than 2 rounds', async () => {
const validatorsPerRound = 101;
const timePerRound = validatorsPerRound * blockTime;
const result = await validatorsModule.method.getGeneratorsBetweenTimestamps(
methodContext,
0,
timePerRound * 2 + 2 * blockTime + 1,
);
let genWithCountGreaterThanOne = 0;
for (const generatorAddress of Object.keys(result)) {
if (result[generatorAddress] > 1) {
genWithCountGreaterThanOne += 1;
}
}
let genWithCountGreaterThanTwo = 0;
for (const generatorAddress of Object.keys(result)) {
if (result[generatorAddress] > 2) {
genWithCountGreaterThanTwo += 1;
}
}
expect(generatorList).toHaveLength(genWithCountGreaterThanOne);
expect(genWithCountGreaterThanTwo).toBeGreaterThan(0);
});
it('should be able to return no generator if input timestamps are valid and difference between input timestamps is zero', async () => {
const result = await validatorsModule.method.getGeneratorsBetweenTimestamps(
methodContext,
100,
100,
);
expect(Object.keys(result)).toHaveLength(0);
});
it('should be able to return no generator if input timestamps are valid and difference between input timestamps is less than block time ', async () => {
const result = await validatorsModule.method.getGeneratorsBetweenTimestamps(
methodContext,
0,
blockTime - 1,
);
expect(Object.keys(result)).toHaveLength(0);
});
it('should be able to return no generator if input timestamps are valid and difference between input timestamps is equal to block time ', async () => {
const result = await validatorsModule.method.getGeneratorsBetweenTimestamps(
methodContext,
0,
blockTime,
);
expect(Object.keys(result)).toHaveLength(0);
});
it('should throw error if the end timestamp is less than the start timestamp', async () => {
await expect(
validatorsModule.method.getGeneratorsBetweenTimestamps(methodContext, 10, 1),
).rejects.toThrow('End timestamp must be greater than start timestamp.');
});
it('should return empty result when startSlotNumber is lower than endSlotNumber but in the same block slot', async () => {
await expect(
validatorsModule.method.getGeneratorsBetweenTimestamps(methodContext, 2, 3),
).resolves.toEqual({});
});
it('should return empty result when startSlotNumber equals endSlotNumber but in the same block slot', async () => {
await expect(
validatorsModule.method.getGeneratorsBetweenTimestamps(methodContext, 2, 2),
).resolves.toEqual({});
});
});
describe('getValidatorKeys', () => {
const validAddress = utils.getRandomBytes(20);
let validatorAccount: ValidatorKeys;
beforeEach(async () => {
methodContext = createNewMethodContext(new InMemoryPrefixedStateDB());
validatorAccount = {
generatorKey: utils.getRandomBytes(48),
blsKey: utils.getRandomBytes(32),
};
const validatorsStore = validatorsModule.stores.get(ValidatorKeysStore);
await validatorsStore.set(methodContext, validAddress, validatorAccount);
});
it('should get validator from store', async () => {
const receivedValidatorAccount = await validatorsMethod.getValidatorKeys(
methodContext,
validAddress,
);
expect(receivedValidatorAccount).toEqual(validatorAccount);
});
it('should throw when address length is not 20', async () => {
const invalidAddress = utils.getRandomBytes(19);
await expect(
validatorsMethod.getValidatorKeys(methodContext, invalidAddress),
).rejects.toThrow('Address is not valid');
});
it('should throw if address does not exist', async () => {
const nonExistingAddress = utils.getRandomBytes(20);
await expect(
validatorsMethod.getValidatorKeys(methodContext, nonExistingAddress),
).rejects.toThrow('No validator account found for the input address.');
});
});
describe('getAddressFromBLSKey', () => {
beforeEach(() => {});
it('should get address if it exists in store', async () => {
await blsKeysSubStore.set(methodContext, blsKey, { address });
await expect(validatorsMethod.getAddressFromBLSKey(methodContext, blsKey)).resolves.toEqual({
address,
});
});
it('should throw if address does not exist in store', async () => {
await expect(validatorsMethod.getAddressFromBLSKey(methodContext, blsKey)).rejects.toThrow(
`The BLS key ${blsKey.toString('hex')} has not been registered in the chain.`,
);
});
it('should throw if bls key is shorter than 48 bytes', async () => {
await expect(
validatorsMethod.getAddressFromBLSKey(methodContext, invalidBlsKeyShort),
).rejects.toThrow(`BLS public key must be ${BLS_PUBLIC_KEY_LENGTH} bytes long.`);
});
it('should throw if bls key is longer than 48 bytes', async () => {
await expect(
validatorsMethod.getAddressFromBLSKey(methodContext, invalidBlsKeyLong),
).rejects.toThrow(`BLS public key must be ${BLS_PUBLIC_KEY_LENGTH} bytes long.`);
});
});
describe('registerValidatorWithoutBLSKey', () => {
beforeEach(() => {
jest.spyOn(methodContext.eventQueue, 'add');
});
it('should be able to register validator key for validator if address does not exist', async () => {
const generatorEventData = codec.encode(generatorKeyRegDataSchema, {
generatorKey,
result: KeyRegResult.SUCCESS,
});
const isSet = await validatorsModule.method.registerValidatorWithoutBLSKey(
methodContext,
address,
generatorKey,
);
const setValidatorAccount = await validatorsSubStore.get(methodContext, address);
expect(isSet).toBe(true);
expect(setValidatorAccount.generatorKey.equals(generatorKey)).toBe(true);
expect(methodContext.eventQueue.add).toHaveBeenCalledWith(
MODULE_NAME_VALIDATORS,
validatorsModule.events.get(GeneratorKeyRegistrationEvent).name,
generatorEventData,
[address],
false,
);
});
it('should throw error if address already registered as validator', async () => {
const generatorEventData = codec.encode(generatorKeyRegDataSchema, {
generatorKey,
result: KeyRegResult.ALREADY_VALIDATOR,
});
const validatorAccount = {
generatorKey,
blsKey,
};
await validatorsSubStore.set(methodContext, address, validatorAccount);
await expect(
validatorsModule.method.registerValidatorWithoutBLSKey(
methodContext,
address,
generatorKey,
),
).rejects.toThrow('This address is already registered as validator.');
expect(methodContext.eventQueue.add).toHaveBeenCalledWith(
MODULE_NAME_VALIDATORS,
validatorsModule.events.get(GeneratorKeyRegistrationEvent).name,
generatorEventData,
[address],
true,
);
});
it('should throw error and be unable to register validator if address length is less than 20 bytes', async () => {
await expect(
validatorsModule.method.registerValidatorWithoutBLSKey(
methodContext,
invalidAddressShort,
generatorKey,
),
).rejects.toThrow(`Validator address must be ${ADDRESS_LENGTH} bytes long.`);
await expect(validatorsSubStore.get(methodContext, invalidAddressShort)).rejects.toThrow();
});
it('should throw error and be unable to register validator if address length is greater than 20 bytes', async () => {
await expect(
validatorsModule.method.registerValidatorWithoutBLSKey(
methodContext,
invalidAddressLong,
generatorKey,
),
).rejects.toThrow(`Validator address must be ${ADDRESS_LENGTH} bytes long.`);
await expect(validatorsSubStore.get(methodContext, invalidAddressLong)).rejects.toThrow();
});
it('should throw error and be unable to register validator if generator key is less than 20 bytes', async () => {
await expect(
validatorsModule.method.registerValidatorWithoutBLSKey(
methodContext,
address,
invalidGeneratorKeyShort,
),
).rejects.toThrow();
await expect(validatorsSubStore.get(methodContext, address)).rejects.toThrow();
});
it('should throw error and be unable to register validator if generator key is greater than 20 bytes', async () => {
await expect(
validatorsModule.method.registerValidatorWithoutBLSKey(
methodContext,
address,
invalidGeneratorKeyLong,
),
).rejects.toThrow();
await expect(validatorsSubStore.get(methodContext, address)).rejects.toThrow();
});
});
});