This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathtests.rs
More file actions
793 lines (636 loc) · 25.5 KB
/
tests.rs
File metadata and controls
793 lines (636 loc) · 25.5 KB
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
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::vec;
use codec::Encode;
use sp_consensus_beefy::{
check_equivocation_proof, generate_equivocation_proof, known_payloads::MMR_ROOT_ID,
Keyring as BeefyKeyring, Payload, ValidatorSet, KEY_TYPE as BEEFY_KEY_TYPE,
};
use sp_runtime::DigestItem;
use frame_support::{
assert_err, assert_ok,
dispatch::{GetDispatchInfo, Pays},
traits::{Currency, KeyOwnerProofSystem, OnInitialize},
};
use crate::{mock::*, Call, Config, Error, Weight, WeightInfo};
fn init_block(block: u64) {
System::set_block_number(block);
Session::on_initialize(block);
}
pub fn beefy_log(log: ConsensusLog<BeefyId>) -> DigestItem {
DigestItem::Consensus(BEEFY_ENGINE_ID, log.encode())
}
#[test]
fn genesis_session_initializes_authorities() {
let authorities = mock_authorities(vec![1, 2, 3, 4]);
let want = authorities.clone();
new_test_ext_raw_authorities(authorities).execute_with(|| {
let authorities = Beefy::authorities();
assert_eq!(authorities.len(), 4);
assert_eq!(want[0], authorities[0]);
assert_eq!(want[1], authorities[1]);
assert!(Beefy::validator_set_id() == 0);
let next_authorities = Beefy::next_authorities();
assert_eq!(next_authorities.len(), 4);
assert_eq!(want[0], next_authorities[0]);
assert_eq!(want[1], next_authorities[1]);
});
}
#[test]
fn session_change_updates_authorities() {
let authorities = mock_authorities(vec![1, 2, 3, 4]);
let want_validators = authorities.clone();
new_test_ext(vec![1, 2, 3, 4]).execute_with(|| {
assert!(0 == Beefy::validator_set_id());
init_block(1);
assert!(1 == Beefy::validator_set_id());
let want = beefy_log(ConsensusLog::AuthoritiesChange(
ValidatorSet::new(want_validators, 1).unwrap(),
));
let log = System::digest().logs[0].clone();
assert_eq!(want, log);
init_block(2);
assert!(2 == Beefy::validator_set_id());
let want = beefy_log(ConsensusLog::AuthoritiesChange(
ValidatorSet::new(vec![mock_beefy_id(2), mock_beefy_id(4)], 2).unwrap(),
));
let log = System::digest().logs[1].clone();
assert_eq!(want, log);
});
}
#[test]
fn session_change_updates_next_authorities() {
let want = vec![mock_beefy_id(1), mock_beefy_id(2), mock_beefy_id(3), mock_beefy_id(4)];
new_test_ext(vec![1, 2, 3, 4]).execute_with(|| {
let next_authorities = Beefy::next_authorities();
assert_eq!(next_authorities.len(), 4);
assert_eq!(want[0], next_authorities[0]);
assert_eq!(want[1], next_authorities[1]);
assert_eq!(want[2], next_authorities[2]);
assert_eq!(want[3], next_authorities[3]);
init_block(1);
let next_authorities = Beefy::next_authorities();
assert_eq!(next_authorities.len(), 2);
assert_eq!(want[1], next_authorities[0]);
assert_eq!(want[3], next_authorities[1]);
});
}
#[test]
fn validator_set_at_genesis() {
let want = vec![mock_beefy_id(1), mock_beefy_id(2)];
new_test_ext(vec![1, 2, 3, 4]).execute_with(|| {
let vs = Beefy::validator_set().unwrap();
assert_eq!(vs.id(), 0u64);
assert_eq!(vs.validators()[0], want[0]);
assert_eq!(vs.validators()[1], want[1]);
});
}
#[test]
fn validator_set_updates_work() {
let want = vec![mock_beefy_id(1), mock_beefy_id(2), mock_beefy_id(3), mock_beefy_id(4)];
new_test_ext(vec![1, 2, 3, 4]).execute_with(|| {
let vs = Beefy::validator_set().unwrap();
assert_eq!(vs.id(), 0u64);
assert_eq!(want[0], vs.validators()[0]);
assert_eq!(want[1], vs.validators()[1]);
assert_eq!(want[2], vs.validators()[2]);
assert_eq!(want[3], vs.validators()[3]);
init_block(1);
let vs = Beefy::validator_set().unwrap();
assert_eq!(vs.id(), 1u64);
assert_eq!(want[0], vs.validators()[0]);
assert_eq!(want[1], vs.validators()[1]);
init_block(2);
let vs = Beefy::validator_set().unwrap();
assert_eq!(vs.id(), 2u64);
assert_eq!(want[1], vs.validators()[0]);
assert_eq!(want[3], vs.validators()[1]);
});
}
#[test]
fn cleans_up_old_set_id_session_mappings() {
new_test_ext(vec![1, 2, 3, 4]).execute_with(|| {
let max_set_id_session_entries = MaxSetIdSessionEntries::get();
// we have 3 sessions per era
let era_limit = max_set_id_session_entries / 3;
// sanity check against division precision loss
assert_eq!(0, max_set_id_session_entries % 3);
// go through `max_set_id_session_entries` sessions
start_era(era_limit);
// we should have a session id mapping for all the set ids from
// `max_set_id_session_entries` eras we have observed
for i in 1..=max_set_id_session_entries {
assert!(Beefy::session_for_set(i as u64).is_some());
}
// go through another `max_set_id_session_entries` sessions
start_era(era_limit * 2);
// we should keep tracking the new mappings for new sessions
for i in max_set_id_session_entries + 1..=max_set_id_session_entries * 2 {
assert!(Beefy::session_for_set(i as u64).is_some());
}
// but the old ones should have been pruned by now
for i in 1..=max_set_id_session_entries {
assert!(Beefy::session_for_set(i as u64).is_none());
}
});
}
/// Returns a list with 3 authorities with known keys:
/// Alice, Bob and Charlie.
pub fn test_authorities() -> Vec<BeefyId> {
let authorities = vec![BeefyKeyring::Alice, BeefyKeyring::Bob, BeefyKeyring::Charlie];
authorities.into_iter().map(|id| id.public()).collect()
}
#[test]
fn should_sign_and_verify() {
use sp_runtime::traits::Keccak256;
let set_id = 3;
let payload1 = Payload::from_single_entry(MMR_ROOT_ID, vec![42]);
let payload2 = Payload::from_single_entry(MMR_ROOT_ID, vec![128]);
// generate an equivocation proof, with two votes in the same round for
// same payload signed by the same key
let equivocation_proof = generate_equivocation_proof(
(1, payload1.clone(), set_id, &BeefyKeyring::Bob),
(1, payload1.clone(), set_id, &BeefyKeyring::Bob),
);
// expect invalid equivocation proof
assert!(!check_equivocation_proof::<_, _, Keccak256>(&equivocation_proof));
// generate an equivocation proof, with two votes in different rounds for
// different payloads signed by the same key
let equivocation_proof = generate_equivocation_proof(
(1, payload1.clone(), set_id, &BeefyKeyring::Bob),
(2, payload2.clone(), set_id, &BeefyKeyring::Bob),
);
// expect invalid equivocation proof
assert!(!check_equivocation_proof::<_, _, Keccak256>(&equivocation_proof));
// generate an equivocation proof, with two votes by different authorities
let equivocation_proof = generate_equivocation_proof(
(1, payload1.clone(), set_id, &BeefyKeyring::Alice),
(1, payload2.clone(), set_id, &BeefyKeyring::Bob),
);
// expect invalid equivocation proof
assert!(!check_equivocation_proof::<_, _, Keccak256>(&equivocation_proof));
// generate an equivocation proof, with two votes in different set ids
let equivocation_proof = generate_equivocation_proof(
(1, payload1.clone(), set_id, &BeefyKeyring::Bob),
(1, payload2.clone(), set_id + 1, &BeefyKeyring::Bob),
);
// expect invalid equivocation proof
assert!(!check_equivocation_proof::<_, _, Keccak256>(&equivocation_proof));
// generate an equivocation proof, with two votes in the same round for
// different payloads signed by the same key
let payload2 = Payload::from_single_entry(MMR_ROOT_ID, vec![128]);
let equivocation_proof = generate_equivocation_proof(
(1, payload1, set_id, &BeefyKeyring::Bob),
(1, payload2, set_id, &BeefyKeyring::Bob),
);
// expect valid equivocation proof
assert!(check_equivocation_proof::<_, _, Keccak256>(&equivocation_proof));
}
#[test]
fn report_equivocation_current_set_works() {
let authorities = test_authorities();
new_test_ext_raw_authorities(authorities).execute_with(|| {
assert_eq!(Staking::current_era(), Some(0));
assert_eq!(Session::current_index(), 0);
start_era(1);
let block_num = System::block_number();
let validator_set = Beefy::validator_set().unwrap();
let authorities = validator_set.validators();
let set_id = validator_set.id();
let validators = Session::validators();
// make sure that all validators have the same balance
for validator in &validators {
assert_eq!(Balances::total_balance(validator), 10_000_000);
assert_eq!(Staking::slashable_balance_of(validator), 10_000);
assert_eq!(
Staking::eras_stakers(1, &validator),
pallet_staking::Exposure { total: 10_000, own: 10_000, others: vec![] },
);
}
assert_eq!(authorities.len(), 2);
let equivocation_authority_index = 1;
let equivocation_key = &authorities[equivocation_authority_index];
let equivocation_keyring = BeefyKeyring::from_public(equivocation_key).unwrap();
let payload1 = Payload::from_single_entry(MMR_ROOT_ID, vec![42]);
let payload2 = Payload::from_single_entry(MMR_ROOT_ID, vec![128]);
// generate an equivocation proof, with two votes in the same round for
// different payloads signed by the same key
let equivocation_proof = generate_equivocation_proof(
(block_num, payload1, set_id, &equivocation_keyring),
(block_num, payload2, set_id, &equivocation_keyring),
);
// create the key ownership proof
let key_owner_proof = Historical::prove((BEEFY_KEY_TYPE, &equivocation_key)).unwrap();
// report the equivocation and the tx should be dispatched successfully
assert_ok!(Beefy::report_equivocation_unsigned(
RuntimeOrigin::none(),
Box::new(equivocation_proof),
key_owner_proof,
),);
start_era(2);
// check that the balance of 0-th validator is slashed 100%.
let equivocation_validator_id = validators[equivocation_authority_index];
assert_eq!(Balances::total_balance(&equivocation_validator_id), 10_000_000 - 10_000);
assert_eq!(Staking::slashable_balance_of(&equivocation_validator_id), 0);
assert_eq!(
Staking::eras_stakers(2, &equivocation_validator_id),
pallet_staking::Exposure { total: 0, own: 0, others: vec![] },
);
// check that the balances of all other validators are left intact.
for validator in &validators {
if *validator == equivocation_validator_id {
continue
}
assert_eq!(Balances::total_balance(validator), 10_000_000);
assert_eq!(Staking::slashable_balance_of(validator), 10_000);
assert_eq!(
Staking::eras_stakers(2, &validator),
pallet_staking::Exposure { total: 10_000, own: 10_000, others: vec![] },
);
}
});
}
#[test]
fn report_equivocation_old_set_works() {
let authorities = test_authorities();
new_test_ext_raw_authorities(authorities).execute_with(|| {
start_era(1);
let block_num = System::block_number();
let validator_set = Beefy::validator_set().unwrap();
let authorities = validator_set.validators();
let validators = Session::validators();
let old_set_id = validator_set.id();
assert_eq!(authorities.len(), 2);
let equivocation_authority_index = 0;
let equivocation_key = &authorities[equivocation_authority_index];
// create the key ownership proof in the "old" set
let key_owner_proof = Historical::prove((BEEFY_KEY_TYPE, &equivocation_key)).unwrap();
start_era(2);
// make sure that all authorities have the same balance
for validator in &validators {
assert_eq!(Balances::total_balance(validator), 10_000_000);
assert_eq!(Staking::slashable_balance_of(validator), 10_000);
assert_eq!(
Staking::eras_stakers(2, &validator),
pallet_staking::Exposure { total: 10_000, own: 10_000, others: vec![] },
);
}
let validator_set = Beefy::validator_set().unwrap();
let new_set_id = validator_set.id();
assert_eq!(old_set_id + 3, new_set_id);
let equivocation_keyring = BeefyKeyring::from_public(equivocation_key).unwrap();
let payload1 = Payload::from_single_entry(MMR_ROOT_ID, vec![42]);
let payload2 = Payload::from_single_entry(MMR_ROOT_ID, vec![128]);
// generate an equivocation proof for the old set,
let equivocation_proof = generate_equivocation_proof(
(block_num, payload1, old_set_id, &equivocation_keyring),
(block_num, payload2, old_set_id, &equivocation_keyring),
);
// report the equivocation and the tx should be dispatched successfully
assert_ok!(Beefy::report_equivocation_unsigned(
RuntimeOrigin::none(),
Box::new(equivocation_proof),
key_owner_proof,
),);
start_era(3);
// check that the balance of 0-th validator is slashed 100%.
let equivocation_validator_id = validators[equivocation_authority_index];
assert_eq!(Balances::total_balance(&equivocation_validator_id), 10_000_000 - 10_000);
assert_eq!(Staking::slashable_balance_of(&equivocation_validator_id), 0);
assert_eq!(
Staking::eras_stakers(3, &equivocation_validator_id),
pallet_staking::Exposure { total: 0, own: 0, others: vec![] },
);
// check that the balances of all other validators are left intact.
for validator in &validators {
if *validator == equivocation_validator_id {
continue
}
assert_eq!(Balances::total_balance(validator), 10_000_000);
assert_eq!(Staking::slashable_balance_of(validator), 10_000);
assert_eq!(
Staking::eras_stakers(3, &validator),
pallet_staking::Exposure { total: 10_000, own: 10_000, others: vec![] },
);
}
});
}
#[test]
fn report_equivocation_invalid_set_id() {
let authorities = test_authorities();
new_test_ext_raw_authorities(authorities).execute_with(|| {
start_era(1);
let block_num = System::block_number();
let validator_set = Beefy::validator_set().unwrap();
let authorities = validator_set.validators();
let set_id = validator_set.id();
let equivocation_authority_index = 0;
let equivocation_key = &authorities[equivocation_authority_index];
let equivocation_keyring = BeefyKeyring::from_public(equivocation_key).unwrap();
let key_owner_proof = Historical::prove((BEEFY_KEY_TYPE, &equivocation_key)).unwrap();
let payload1 = Payload::from_single_entry(MMR_ROOT_ID, vec![42]);
let payload2 = Payload::from_single_entry(MMR_ROOT_ID, vec![128]);
// generate an equivocation for a future set
let equivocation_proof = generate_equivocation_proof(
(block_num, payload1, set_id + 1, &equivocation_keyring),
(block_num, payload2, set_id + 1, &equivocation_keyring),
);
// the call for reporting the equivocation should error
assert_err!(
Beefy::report_equivocation_unsigned(
RuntimeOrigin::none(),
Box::new(equivocation_proof),
key_owner_proof,
),
Error::<Test>::InvalidEquivocationProof,
);
});
}
#[test]
fn report_equivocation_invalid_session() {
let authorities = test_authorities();
new_test_ext_raw_authorities(authorities).execute_with(|| {
start_era(1);
let block_num = System::block_number();
let validator_set = Beefy::validator_set().unwrap();
let authorities = validator_set.validators();
let equivocation_authority_index = 0;
let equivocation_key = &authorities[equivocation_authority_index];
let equivocation_keyring = BeefyKeyring::from_public(equivocation_key).unwrap();
// generate a key ownership proof at current era set id
let key_owner_proof = Historical::prove((BEEFY_KEY_TYPE, &equivocation_key)).unwrap();
start_era(2);
let set_id = Beefy::validator_set().unwrap().id();
let payload1 = Payload::from_single_entry(MMR_ROOT_ID, vec![42]);
let payload2 = Payload::from_single_entry(MMR_ROOT_ID, vec![128]);
// generate an equivocation proof at following era set id = 2
let equivocation_proof = generate_equivocation_proof(
(block_num, payload1, set_id, &equivocation_keyring),
(block_num, payload2, set_id, &equivocation_keyring),
);
// report an equivocation for the current set using an key ownership
// proof from the previous set, the session should be invalid.
assert_err!(
Beefy::report_equivocation_unsigned(
RuntimeOrigin::none(),
Box::new(equivocation_proof),
key_owner_proof,
),
Error::<Test>::InvalidEquivocationProof,
);
});
}
#[test]
fn report_equivocation_invalid_key_owner_proof() {
let authorities = test_authorities();
new_test_ext_raw_authorities(authorities).execute_with(|| {
start_era(1);
let block_num = System::block_number();
let validator_set = Beefy::validator_set().unwrap();
let authorities = validator_set.validators();
let set_id = validator_set.id();
let invalid_owner_authority_index = 1;
let invalid_owner_key = &authorities[invalid_owner_authority_index];
// generate a key ownership proof for the authority at index 1
let invalid_key_owner_proof =
Historical::prove((BEEFY_KEY_TYPE, &invalid_owner_key)).unwrap();
let equivocation_authority_index = 0;
let equivocation_key = &authorities[equivocation_authority_index];
let equivocation_keyring = BeefyKeyring::from_public(equivocation_key).unwrap();
let payload1 = Payload::from_single_entry(MMR_ROOT_ID, vec![42]);
let payload2 = Payload::from_single_entry(MMR_ROOT_ID, vec![128]);
// generate an equivocation proof for the authority at index 0
let equivocation_proof = generate_equivocation_proof(
(block_num, payload1, set_id + 1, &equivocation_keyring),
(block_num, payload2, set_id + 1, &equivocation_keyring),
);
// we need to start a new era otherwise the key ownership proof won't be
// checked since the authorities are part of the current session
start_era(2);
// report an equivocation for the current set using a key ownership
// proof for a different key than the one in the equivocation proof.
assert_err!(
Beefy::report_equivocation_unsigned(
RuntimeOrigin::none(),
Box::new(equivocation_proof),
invalid_key_owner_proof,
),
Error::<Test>::InvalidKeyOwnershipProof,
);
});
}
#[test]
fn report_equivocation_invalid_equivocation_proof() {
let authorities = test_authorities();
new_test_ext_raw_authorities(authorities).execute_with(|| {
start_era(1);
let block_num = System::block_number();
let validator_set = Beefy::validator_set().unwrap();
let authorities = validator_set.validators();
let set_id = validator_set.id();
let equivocation_authority_index = 0;
let equivocation_key = &authorities[equivocation_authority_index];
let equivocation_keyring = BeefyKeyring::from_public(equivocation_key).unwrap();
// generate a key ownership proof at set id in era 1
let key_owner_proof = Historical::prove((BEEFY_KEY_TYPE, &equivocation_key)).unwrap();
let assert_invalid_equivocation_proof = |equivocation_proof| {
assert_err!(
Beefy::report_equivocation_unsigned(
RuntimeOrigin::none(),
Box::new(equivocation_proof),
key_owner_proof.clone(),
),
Error::<Test>::InvalidEquivocationProof,
);
};
start_era(2);
let payload1 = Payload::from_single_entry(MMR_ROOT_ID, vec![42]);
let payload2 = Payload::from_single_entry(MMR_ROOT_ID, vec![128]);
// both votes target the same block number and payload,
// there is no equivocation.
assert_invalid_equivocation_proof(generate_equivocation_proof(
(block_num, payload1.clone(), set_id, &equivocation_keyring),
(block_num, payload1.clone(), set_id, &equivocation_keyring),
));
// votes targeting different rounds, there is no equivocation.
assert_invalid_equivocation_proof(generate_equivocation_proof(
(block_num, payload1.clone(), set_id, &equivocation_keyring),
(block_num + 1, payload2.clone(), set_id, &equivocation_keyring),
));
// votes signed with different authority keys
assert_invalid_equivocation_proof(generate_equivocation_proof(
(block_num, payload1.clone(), set_id, &equivocation_keyring),
(block_num, payload1.clone(), set_id, &BeefyKeyring::Charlie),
));
// votes signed with a key that isn't part of the authority set
assert_invalid_equivocation_proof(generate_equivocation_proof(
(block_num, payload1.clone(), set_id, &equivocation_keyring),
(block_num, payload1.clone(), set_id, &BeefyKeyring::Dave),
));
// votes targeting different set ids
assert_invalid_equivocation_proof(generate_equivocation_proof(
(block_num, payload1, set_id, &equivocation_keyring),
(block_num, payload2, set_id + 1, &equivocation_keyring),
));
});
}
#[test]
fn report_equivocation_validate_unsigned_prevents_duplicates() {
use sp_runtime::transaction_validity::{
InvalidTransaction, TransactionPriority, TransactionSource, TransactionValidity,
ValidTransaction,
};
let authorities = test_authorities();
new_test_ext_raw_authorities(authorities).execute_with(|| {
start_era(1);
let block_num = System::block_number();
let validator_set = Beefy::validator_set().unwrap();
let authorities = validator_set.validators();
let set_id = validator_set.id();
// generate and report an equivocation for the validator at index 0
let equivocation_authority_index = 0;
let equivocation_key = &authorities[equivocation_authority_index];
let equivocation_keyring = BeefyKeyring::from_public(equivocation_key).unwrap();
let payload1 = Payload::from_single_entry(MMR_ROOT_ID, vec![42]);
let payload2 = Payload::from_single_entry(MMR_ROOT_ID, vec![128]);
let equivocation_proof = generate_equivocation_proof(
(block_num, payload1, set_id, &equivocation_keyring),
(block_num, payload2, set_id, &equivocation_keyring),
);
let key_owner_proof = Historical::prove((BEEFY_KEY_TYPE, &equivocation_key)).unwrap();
let call = Call::report_equivocation_unsigned {
equivocation_proof: Box::new(equivocation_proof.clone()),
key_owner_proof: key_owner_proof.clone(),
};
// only local/inblock reports are allowed
assert_eq!(
<Beefy as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
TransactionSource::External,
&call,
),
InvalidTransaction::Call.into(),
);
// the transaction is valid when passed as local
let tx_tag = (equivocation_key, set_id, 3u64);
assert_eq!(
<Beefy as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
TransactionSource::Local,
&call,
),
TransactionValidity::Ok(ValidTransaction {
priority: TransactionPriority::max_value(),
requires: vec![],
provides: vec![("BeefyEquivocation", tx_tag).encode()],
longevity: ReportLongevity::get(),
propagate: false,
})
);
// the pre dispatch checks should also pass
assert_ok!(<Beefy as sp_runtime::traits::ValidateUnsigned>::pre_dispatch(&call));
// we submit the report
Beefy::report_equivocation_unsigned(
RuntimeOrigin::none(),
Box::new(equivocation_proof),
key_owner_proof,
)
.unwrap();
// the report should now be considered stale and the transaction is invalid
// the check for staleness should be done on both `validate_unsigned` and on `pre_dispatch`
assert_err!(
<Beefy as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
TransactionSource::Local,
&call,
),
InvalidTransaction::Stale,
);
assert_err!(
<Beefy as sp_runtime::traits::ValidateUnsigned>::pre_dispatch(&call),
InvalidTransaction::Stale,
);
});
}
#[test]
fn report_equivocation_has_valid_weight() {
// the weight depends on the size of the validator set,
// but there's a lower bound of 100 validators.
assert!((1..=100)
.map(|validators| <Test as Config>::WeightInfo::report_equivocation(validators, 1000))
.collect::<Vec<_>>()
.windows(2)
.all(|w| w[0] == w[1]));
// after 100 validators the weight should keep increasing
// with every extra validator.
assert!((100..=1000)
.map(|validators| <Test as Config>::WeightInfo::report_equivocation(validators, 1000))
.collect::<Vec<_>>()
.windows(2)
.all(|w| w[0].ref_time() < w[1].ref_time()));
}
#[test]
fn valid_equivocation_reports_dont_pay_fees() {
let authorities = test_authorities();
new_test_ext_raw_authorities(authorities).execute_with(|| {
start_era(1);
let block_num = System::block_number();
let validator_set = Beefy::validator_set().unwrap();
let authorities = validator_set.validators();
let set_id = validator_set.id();
let equivocation_authority_index = 0;
let equivocation_key = &authorities[equivocation_authority_index];
let equivocation_keyring = BeefyKeyring::from_public(equivocation_key).unwrap();
// generate equivocation proof
let payload1 = Payload::from_single_entry(MMR_ROOT_ID, vec![42]);
let payload2 = Payload::from_single_entry(MMR_ROOT_ID, vec![128]);
let equivocation_proof = generate_equivocation_proof(
(block_num, payload1, set_id, &equivocation_keyring),
(block_num, payload2, set_id, &equivocation_keyring),
);
// create the key ownership proof.
let key_owner_proof = Historical::prove((BEEFY_KEY_TYPE, &equivocation_key)).unwrap();
// check the dispatch info for the call.
let info = Call::<Test>::report_equivocation_unsigned {
equivocation_proof: Box::new(equivocation_proof.clone()),
key_owner_proof: key_owner_proof.clone(),
}
.get_dispatch_info();
// it should have non-zero weight and the fee has to be paid.
assert!(info.weight.any_gt(Weight::zero()));
assert_eq!(info.pays_fee, Pays::Yes);
// report the equivocation.
let post_info = Beefy::report_equivocation_unsigned(
RuntimeOrigin::none(),
Box::new(equivocation_proof.clone()),
key_owner_proof.clone(),
)
.unwrap();
// the original weight should be kept, but given that the report
// is valid the fee is waived.
assert!(post_info.actual_weight.is_none());
assert_eq!(post_info.pays_fee, Pays::No);
// report the equivocation again which is invalid now since it is
// duplicate.
let post_info = Beefy::report_equivocation_unsigned(
RuntimeOrigin::none(),
Box::new(equivocation_proof),
key_owner_proof,
)
.err()
.unwrap()
.post_info;
// the fee is not waived and the original weight is kept.
assert!(post_info.actual_weight.is_none());
assert_eq!(post_info.pays_fee, Pays::Yes);
})
}