Newer
Older
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
#[test]
fn offchain_window_is_triggered_when_force_always() {
ExtBuilder::default()
.session_per_era(5)
.session_length(10)
.election_lookahead(3)
.build()
.execute_with(|| {
ForceEra::put(Forcing::ForceAlways);
run_to_block(16);
assert_eq!(Staking::era_election_status(), ElectionStatus::Closed);
run_to_block(17); // instead of 37
assert_eq!(Staking::era_election_status(), ElectionStatus::Open(17));
run_to_block(20);
assert_eq!(Staking::era_election_status(), ElectionStatus::Closed);
run_to_block(26);
assert_eq!(Staking::era_election_status(), ElectionStatus::Closed);
run_to_block(27); // next one again
assert_eq!(Staking::era_election_status(), ElectionStatus::Open(27));
})
}
#[test]
fn offchain_window_closes_when_forcenone() {
ExtBuilder::default()
.session_per_era(5)
.session_length(10)
.election_lookahead(3)
.build()
.execute_with(|| {
ForceEra::put(Forcing::ForceNone);
run_to_block(36);
assert_session_era!(3, 0);
assert_eq!(Staking::era_election_status(), ElectionStatus::Closed);
// opens
run_to_block(37);
assert_eq!(Staking::era_election_status(), ElectionStatus::Open(37));
assert!(Staking::is_current_session_final());
assert!(Staking::snapshot_validators().is_some());
// closes normally
run_to_block(40);
assert_eq!(Staking::era_election_status(), ElectionStatus::Closed);
assert!(!Staking::is_current_session_final());
assert!(Staking::snapshot_validators().is_none());
assert_session_era!(4, 0);
run_to_block(47);
assert_eq!(Staking::era_election_status(), ElectionStatus::Closed);
assert_session_era!(4, 0);
run_to_block(57);
assert_eq!(Staking::era_election_status(), ElectionStatus::Closed);
assert_session_era!(5, 0);
run_to_block(67);
assert_eq!(Staking::era_election_status(), ElectionStatus::Closed);
// Will not open again as scheduled
run_to_block(87);
assert_eq!(Staking::era_election_status(), ElectionStatus::Closed);
assert_session_era!(8, 0);
run_to_block(90);
assert_eq!(Staking::era_election_status(), ElectionStatus::Closed);
assert_session_era!(9, 0);
fn offchain_window_on_chain_fallback_works() {
ExtBuilder::default().build_and_execute(|| {
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
start_session(1);
start_session(2);
assert_eq!(Staking::era_election_status(), ElectionStatus::Closed);
// some election must have happened by now.
assert_eq!(
System::events()
.into_iter()
.map(|r| r.event)
.filter_map(|e| {
if let MetaEvent::staking(inner) = e {
Some(inner)
} else {
None
}
})
.last()
.unwrap(),
RawEvent::StakingElection(ElectionCompute::OnChain),
);
})
}
#[test]
#[ignore] // This takes a few mins
fn offchain_wont_work_if_snapshot_fails() {
ExtBuilder::default()
.offchain_phragmen_ext()
.build()
.execute_with(|| {
run_to_block(12);
assert!(Staking::snapshot_validators().is_some());
assert_eq!(Staking::era_election_status(), ElectionStatus::Open(12));
// validate more than the limit
let limit: NominatorIndex = ValidatorIndex::max_value() as NominatorIndex + 1;
let ctrl = 1_000_000;
for i in 0..limit {
bond_validator((1000 + i).into(), (1000 + i + ctrl).into(), 100);
}
// window stays closed since no snapshot was taken.
run_to_block(27);
assert!(Staking::snapshot_validators().is_none());
assert_eq!(Staking::era_election_status(), ElectionStatus::Closed);
})
}
#[test]
fn staking_is_locked_when_election_window_open() {
ExtBuilder::default()
.offchain_phragmen_ext()
.election_lookahead(3)
.build()
.execute_with(|| {
run_to_block(12);
assert!(Staking::snapshot_validators().is_some());
assert_eq!(Staking::era_election_status(), ElectionStatus::Open(12));
// chill et. al. are now not allowed.
assert_noop!(
Staking::chill(Origin::signed(10)),
Error::<Test>::CallNotAllowed,
);
})
}
#[test]
fn signed_result_can_be_submitted() {
// should check that we have a new validator set normally, event says that it comes from
// offchain.
ExtBuilder::default()
.offchain_phragmen_ext()
.build()
.execute_with(|| {
run_to_block(12);
assert_eq!(Staking::era_election_status(), ElectionStatus::Open(12));
assert!(Staking::snapshot_validators().is_some());
let (compact, winners, score) = prepare_submission_with(true, 2, |_| {});
assert_ok!(submit_solution(
Origin::signed(10),
winners,
compact,
score,
));
let queued_result = Staking::queued_elected().unwrap();
assert_eq!(queued_result.compute, ElectionCompute::Signed);
assert_eq!(
System::events()
.into_iter()
.map(|r| r.event)
.filter_map(|e| {
if let MetaEvent::staking(inner) = e {
Some(inner)
} else {
None
}
})
.last()
.unwrap(),
RawEvent::SolutionStored(ElectionCompute::Signed),
);
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
run_to_block(15);
assert_eq!(Staking::era_election_status(), ElectionStatus::Closed);
assert_eq!(
System::events()
.into_iter()
.map(|r| r.event)
.filter_map(|e| {
if let MetaEvent::staking(inner) = e {
Some(inner)
} else {
None
}
})
.last()
.unwrap(),
RawEvent::StakingElection(ElectionCompute::Signed),
);
})
}
#[test]
fn signed_result_can_be_submitted_later() {
// same as `signed_result_can_be_submitted` but at a later block.
ExtBuilder::default()
.offchain_phragmen_ext()
.build()
.execute_with(|| {
run_to_block(14);
assert_eq!(Staking::era_election_status(), ElectionStatus::Open(12));
let (compact, winners, score) = prepare_submission_with(true, 2, |_| {});
assert_ok!(submit_solution(Origin::signed(10), winners, compact, score));
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
let queued_result = Staking::queued_elected().unwrap();
assert_eq!(queued_result.compute, ElectionCompute::Signed);
run_to_block(15);
assert_eq!(Staking::era_election_status(), ElectionStatus::Closed);
assert_eq!(
System::events()
.into_iter()
.map(|r| r.event)
.filter_map(|e| {
if let MetaEvent::staking(inner) = e {
Some(inner)
} else {
None
}
})
.last()
.unwrap(),
RawEvent::StakingElection(ElectionCompute::Signed),
);
})
}
#[test]
fn early_solution_submission_is_rejected() {
// should check that we have a new validator set normally, event says that it comes from
// offchain.
ExtBuilder::default()
.offchain_phragmen_ext()
.build()
.execute_with(|| {
run_to_block(11);
// submission is not yet allowed
assert_eq!(Staking::era_election_status(), ElectionStatus::Closed);
// create all the indices just to build the solution.
Staking::create_stakers_snapshot();
let (compact, winners, score) = prepare_submission_with(true, 2, |_| {});
Staking::kill_stakers_snapshot();
assert_err_with_weight!(
Staking::submit_election_solution(
Origin::signed(10),
winners.clone(),
compact.clone(),
current_era(),
ElectionSize::default(),
),
Error::<Test>::PhragmenEarlySubmission,
Some(<Test as frame_system::Trait>::DbWeight::get().reads(1)),
);
})
}
#[test]
fn weak_solution_is_rejected() {
// A solution which is weaker than what we currently have on-chain is rejected.
ExtBuilder::default()
.offchain_phragmen_ext()
.has_stakers(false)
.validator_count(4)
.build()
.execute_with(|| {
build_offchain_phragmen_test_ext();
run_to_block(12);
// a good solution
let (compact, winners, score) = prepare_submission_with(true, 2, |_| {});
assert_ok!(submit_solution(
Origin::signed(10),
winners,
compact,
score,
));
// a bad solution
let (compact, winners, score) = horrible_phragmen_with_post_processing(false);
assert_err_with_weight!(
submit_solution(
winners.clone(),
compact.clone(),
score,
),
Error::<Test>::PhragmenWeakSubmission,
Some(<Test as frame_system::Trait>::DbWeight::get().reads(3))
);
})
}
#[test]
fn better_solution_is_accepted() {
// A solution which is better than what we currently have on-chain is accepted.
ExtBuilder::default()
.offchain_phragmen_ext()
.validator_count(4)
.has_stakers(false)
.build()
.execute_with(|| {
build_offchain_phragmen_test_ext();
run_to_block(12);
// a meeeeh solution
let (compact, winners, score) = horrible_phragmen_with_post_processing(false);
assert_ok!(submit_solution(
Origin::signed(10),
winners,
compact,
score,
));
// a better solution
let (compact, winners, score) = prepare_submission_with(true, 2, |_| {});
assert_ok!(submit_solution(
Origin::signed(10),
winners,
compact,
score,
));
})
}
#[test]
fn offchain_worker_runs_when_window_open() {
// at the end of the first finalized block with ElectionStatus::open(_), it should execute.
let mut ext = ExtBuilder::default()
.offchain_phragmen_ext()
.validator_count(2)
.build();
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
ext.execute_with(|| {
run_to_block(12);
// local key 11 is in the elected set.
assert_eq_uvec!(Session::validators(), vec![11, 21]);
assert_eq!(state.read().transactions.len(), 0);
Staking::offchain_worker(12);
assert_eq!(state.read().transactions.len(), 1);
let encoded = state.read().transactions[0].clone();
let extrinsic: Extrinsic = Decode::decode(&mut &*encoded).unwrap();
let call = extrinsic.call;
let inner = match call {
mock::Call::Staking(inner) => inner,
};
assert_eq!(
<Staking as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
TransactionSource::Local,
&inner,
),
TransactionValidity::Ok(ValidTransaction {
priority: UnsignedPriority::get() + 1125, // the proposed slot stake.
provides: vec![("StakingOffchain", current_era()).encode()],
longevity: 3,
propagate: false,
})
)
})
}
#[test]
fn offchain_worker_runs_with_equalise() {
// Offchain worker equalises based on the number provided by randomness. See the difference
// in the priority, which comes from the computed score.
let mut ext = ExtBuilder::default()
.offchain_phragmen_ext()
.validator_count(2)
.max_offchain_iterations(2)
.build();
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
ext.execute_with(|| {
run_to_block(12);
// local key 11 is in the elected set.
assert_eq_uvec!(Session::validators(), vec![11, 21]);
assert_eq!(state.read().transactions.len(), 0);
Staking::offchain_worker(12);
assert_eq!(state.read().transactions.len(), 1);
let encoded = state.read().transactions[0].clone();
let extrinsic: Extrinsic = Decode::decode(&mut &*encoded).unwrap();
let call = extrinsic.call;
let inner = match call {
mock::Call::Staking(inner) => inner,
};
assert_eq!(
<Staking as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
TransactionSource::Local,
&inner,
),
TransactionValidity::Ok(ValidTransaction {
// the proposed slot stake, with balance_solution.
priority: UnsignedPriority::get() + 1250,
requires: vec![],
provides: vec![("StakingOffchain", active_era()).encode()],
longevity: 3,
propagate: false,
})
)
})
}
#[test]
fn mediocre_submission_from_authority_is_early_rejected() {
let mut ext = ExtBuilder::default()
.offchain_phragmen_ext()
.validator_count(4)
.build();
ext.execute_with(|| {
run_to_block(12);
// put a good solution on-chain
let (compact, winners, score) = prepare_submission_with(true, 2, |_| {});
assert_ok!(submit_solution(
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
Origin::signed(10),
winners,
compact,
score,
),);
// now run the offchain worker in the same chain state.
Staking::offchain_worker(12);
assert_eq!(state.read().transactions.len(), 1);
let encoded = state.read().transactions[0].clone();
let extrinsic: Extrinsic = Decode::decode(&mut &*encoded).unwrap();
let call = extrinsic.call;
let inner = match call {
mock::Call::Staking(inner) => inner,
};
// pass this call to ValidateUnsigned
assert_eq!(
<Staking as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
TransactionSource::Local,
&inner,
),
TransactionValidity::Err(
InvalidTransaction::Custom(<Error<Test>>::PhragmenWeakSubmission.as_u8()).into(),
),
)
})
}
#[test]
fn invalid_phragmen_result_correct_number_of_winners() {
ExtBuilder::default()
.offchain_phragmen_ext()
.validator_count(4)
.has_stakers(false)
.build()
.execute_with(|| {
build_offchain_phragmen_test_ext();
run_to_block(12);
ValidatorCount::put(3);
let (compact, winners, score) = prepare_submission_with(true, 2, |_| {});
ValidatorCount::put(4);
assert_eq!(winners.len(), 3);
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
assert_noop!(
submit_solution(
Origin::signed(10),
winners,
compact,
score,
),
Error::<Test>::PhragmenBogusWinnerCount,
);
})
}
#[test]
fn invalid_phragmen_result_solution_size() {
ExtBuilder::default()
.offchain_phragmen_ext()
.build()
.execute_with(|| {
run_to_block(12);
let (compact, winners, score) = prepare_submission_with(true, 2, |_| {});
assert_noop!(
Staking::submit_election_solution(
Origin::signed(10),
winners,
compact,
score,
current_era(),
ElectionSize::default(),
Error::<Test>::PhragmenBogusElectionSize,
);
})
}
#[test]
fn invalid_phragmen_result_correct_number_of_winners_1() {
// if we have too little validators, then the number of candidates is the bound.
ExtBuilder::default()
.offchain_phragmen_ext()
.validator_count(8) // we simply cannot elect 8
.has_stakers(false)
.build()
.execute_with(|| {
build_offchain_phragmen_test_ext();
run_to_block(12);
ValidatorCount::put(3);
let (compact, winners, score) = prepare_submission_with(true, 2, |_| {});
ValidatorCount::put(4);
assert_eq!(winners.len(), 3);
assert_noop!(
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
Origin::signed(10),
winners,
compact,
score,
),
Error::<Test>::PhragmenBogusWinnerCount,
);
})
}
#[test]
fn invalid_phragmen_result_correct_number_of_winners_2() {
// if we have too little validators, then the number of candidates is the bound.
ExtBuilder::default()
.offchain_phragmen_ext()
.validator_count(8) // we simply cannot elect 8
.has_stakers(false)
.build()
.execute_with(|| {
build_offchain_phragmen_test_ext();
run_to_block(12);
let (compact, winners, score) = prepare_submission_with(true, 2, |_| {});
assert_eq!(winners.len(), 4);
// all good. We chose 4 and it works.
assert_ok!(submit_solution(
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
Origin::signed(10),
winners,
compact,
score,
),);
})
}
#[test]
fn invalid_phragmen_result_out_of_bound_nominator_index() {
// A nominator index which is simply invalid
ExtBuilder::default()
.offchain_phragmen_ext()
.validator_count(4)
.has_stakers(false)
.build()
.execute_with(|| {
build_offchain_phragmen_test_ext();
run_to_block(12);
assert_eq!(Staking::snapshot_nominators().unwrap().len(), 5 + 4);
assert_eq!(Staking::snapshot_validators().unwrap().len(), 4);
let (mut compact, winners, score) = prepare_submission_with(true, 2, |_| {});
// index 9 doesn't exist.
compact.votes1.push((9, 2));
// The error type sadly cannot be more specific now.
assert_noop!(
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
Origin::signed(10),
winners,
compact,
score,
),
Error::<Test>::PhragmenBogusCompact,
);
})
}
#[test]
fn invalid_phragmen_result_out_of_bound_validator_index() {
// A validator index which is out of bound
ExtBuilder::default()
.offchain_phragmen_ext()
.validator_count(4)
.has_stakers(false)
.build()
.execute_with(|| {
build_offchain_phragmen_test_ext();
run_to_block(12);
assert_eq!(Staking::snapshot_nominators().unwrap().len(), 5 + 4);
assert_eq!(Staking::snapshot_validators().unwrap().len(), 4);
let (mut compact, winners, score) = prepare_submission_with(true, 2, |_| {});
// index 4 doesn't exist.
compact.votes1.push((3, 4));
// The error type sadly cannot be more specific now.
assert_noop!(
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
Origin::signed(10),
winners,
compact,
score,
),
Error::<Test>::PhragmenBogusCompact,
);
})
}
#[test]
fn invalid_phragmen_result_out_of_bound_winner_index() {
// A winner index which is simply invalid
ExtBuilder::default()
.offchain_phragmen_ext()
.validator_count(4)
.has_stakers(false)
.build()
.execute_with(|| {
build_offchain_phragmen_test_ext();
run_to_block(12);
assert_eq!(Staking::snapshot_nominators().unwrap().len(), 5 + 4);
assert_eq!(Staking::snapshot_validators().unwrap().len(), 4);
let (compact, _, score) = prepare_submission_with(true, 2, |_| {});
// index 4 doesn't exist.
let winners = vec![0, 1, 2, 4];
assert_noop!(
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
Origin::signed(10),
winners,
compact,
score,
),
Error::<Test>::PhragmenBogusWinner,
);
})
}
#[test]
fn invalid_phragmen_result_non_winner_validator_index() {
// An edge that points to a correct validator index who is NOT a winner. This is very
// similar to the test that raises `PhragmenBogusNomination`.
ExtBuilder::default()
.offchain_phragmen_ext()
.validator_count(2) // we select only 2.
.has_stakers(false)
.build()
.execute_with(|| {
build_offchain_phragmen_test_ext();
run_to_block(12);
assert_eq!(Staking::snapshot_nominators().unwrap().len(), 5 + 4);
assert_eq!(Staking::snapshot_validators().unwrap().len(), 4);
let (compact, winners, score) = prepare_submission_with(true, 2, |a| {
a.iter_mut()
.find(|x| x.who == 5)
// all 3 cannot be among the winners. Although, all of them are validator
// candidates.
.map(|x| x.distribution = vec![(21, 50), (41, 30), (31, 20)]);
});
assert_noop!(
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
Origin::signed(10),
winners,
compact,
score,
),
Error::<Test>::PhragmenBogusEdge,
);
})
}
#[test]
fn invalid_phragmen_result_wrong_self_vote() {
// A self vote for someone else.
ExtBuilder::default()
.offchain_phragmen_ext()
.validator_count(4)
.has_stakers(false)
.build()
.execute_with(|| {
build_offchain_phragmen_test_ext();
run_to_block(12);
let (compact, winners, score) = prepare_submission_with(true, 2, |a| {
// mutate a self vote to target someone else. That someone else is still among the
// winners
a.iter_mut().find(|x| x.who == 11).map(|x| {
x.distribution
.iter_mut()
.find(|y| y.0 == 11)
.map(|y| y.0 = 21)
});
});
assert_noop!(
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
Origin::signed(10),
winners,
compact,
score,
),
Error::<Test>::PhragmenBogusSelfVote,
);
})
}
#[test]
fn invalid_phragmen_result_wrong_self_vote_2() {
// A self validator voting for someone else next to self vote.
ExtBuilder::default()
.offchain_phragmen_ext()
.validator_count(4)
.has_stakers(false)
.build()
.execute_with(|| {
build_offchain_phragmen_test_ext();
run_to_block(12);
let (compact, winners, score) = prepare_submission_with(true, 2, |a| {
// Remove the self vote.
a.retain(|x| x.who != 11);
// add is as a new double vote
a.push(StakedAssignment {
who: 11,
distribution: vec![(11, 50), (21, 50)],
});
});
// This raises score issue.
assert_noop!(
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
Origin::signed(10),
winners,
compact,
score,
),
Error::<Test>::PhragmenBogusSelfVote,
);
})
}
#[test]
fn invalid_phragmen_result_over_stake() {
// Someone's edge ratios sums to more than 100%.
ExtBuilder::default()
.offchain_phragmen_ext()
.validator_count(4)
.has_stakers(false)
.build()
.execute_with(|| {
build_offchain_phragmen_test_ext();
run_to_block(12);
// Note: we don't reduce here to be able to tweak votes3. votes3 will vanish if you
// reduce.
let (mut compact, winners, score) = prepare_submission_with(false, 0, |_| {});
if let Some(c) = compact.votes3.iter_mut().find(|x| x.0 == 0) {
// by default it should have been (0, [(2, 33%), (1, 33%)], 0)
// now the sum is above 100%
c.1 = [(2, percent(66)), (1, percent(66))];
}
assert_noop!(
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
Origin::signed(10),
winners,
compact,
score,
),
Error::<Test>::PhragmenBogusCompact,
);
})
}
#[test]
fn invalid_phragmen_result_under_stake() {
// at the time of this writing, we cannot under stake someone. The compact assignment works
// in a way that some of the stakes are presented by the submitter, and the last one is read
// from chain by subtracting the rest from total. Hence, the sum is always correct.
// This test is only here as a demonstration.
}
#[test]
fn invalid_phragmen_result_invalid_target_stealing() {
// A valid voter who voted for someone who is a candidate, and is a correct winner, but is
// actually NOT nominated by this nominator.
ExtBuilder::default()
.offchain_phragmen_ext()
.validator_count(4)
.has_stakers(false)
.build()
.execute_with(|| {
build_offchain_phragmen_test_ext();
run_to_block(12);
let (compact, winners, score) = prepare_submission_with(false, 0, |a| {
// 3 only voted for 20 and 40. We add a fake vote to 30. The stake sum is still
// correctly 100.
a.iter_mut()
.find(|x| x.who == 3)
.map(|x| x.distribution = vec![(21, 50), (41, 30), (31, 20)]);
});
assert_noop!(
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
Origin::signed(10),
winners,
compact,
score,
),
Error::<Test>::PhragmenBogusNomination,
);
})
}
#[test]
fn nomination_slash_filter_is_checked() {
// If a nominator has voted for someone who has been recently slashed, that particular
// nomination should be disabled for the upcoming election. A solution must respect this
// rule.
ExtBuilder::default()
.offchain_phragmen_ext()
.validator_count(4)
.has_stakers(false)
.build()
.execute_with(|| {
build_offchain_phragmen_test_ext();
// finalize the round with fallback. This is needed since all nominator submission
// are in era zero and we want this one to pass with no problems.
run_to_block(15);
// go to the next session to trigger mock::start_era and bump the active era
run_to_block(20);
// slash 10. This must happen outside of the election window.
let offender_expo = Staking::eras_stakers(Staking::active_era().unwrap().index, 11);
on_offence_now(
&[OffenceDetails {
offender: (11, offender_expo.clone()),
reporters: vec![],
}],
&[Perbill::from_percent(50)],
);
// validate 10 again for the next round. But this guy will not have the votes that
// it should have had from 1 and 2.
assert_ok!(Staking::validate(
Origin::signed(10),
Default::default()
));
// open the election window and create snapshots.
run_to_block(32);
// a solution that has been prepared after the slash.
let (compact, winners, score) = prepare_submission_with(false, 0, |a| {
// no one is allowed to vote for 10, except for itself.
a.into_iter()
.filter(|s| s.who != 11)
.for_each(|s|
assert!(s.distribution.iter().find(|(t, _)| *t == 11).is_none())
);
});
// can be submitted.
assert_ok!(submit_solution(
Origin::signed(10),
winners,
compact,
score,
));
// a wrong solution.
let (compact, winners, score) = prepare_submission_with(false, 0, |a| {
// add back the vote that has been filtered out.
a.push(StakedAssignment {
who: 1,
distribution: vec![(11, 100)]
});
});
// is rejected.
assert_noop!(
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
Origin::signed(10),
winners,
compact,
score,
),
Error::<Test>::PhragmenSlashedNomination,
);
})
}
#[test]
fn invalid_phragmen_result_wrong_score() {
// A valid voter who's total distributed stake is more than what they bond
ExtBuilder::default()
.offchain_phragmen_ext()
.validator_count(4)
.has_stakers(false)
.build()
.execute_with(|| {
build_offchain_phragmen_test_ext();
run_to_block(12);
let (compact, winners, mut score) = prepare_submission_with(true, 2, |_| {});
score[0] += 1;
assert_noop!(
Origin::signed(10),
winners,
compact,
score,
),
Error::<Test>::PhragmenBogusScore,
);
})
}
#[test]
fn offchain_storage_is_set() {
let mut ext = ExtBuilder::default()
.offchain_phragmen_ext()
.validator_count(4)
.build();
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
ext.execute_with(|| {
use offchain_election::OFFCHAIN_HEAD_DB;
use sp_runtime::offchain::storage::StorageValueRef;
run_to_block(12);
Staking::offchain_worker(12);
// it works
assert_eq!(state.read().transactions.len(), 1);
// and it is set
let storage = StorageValueRef::persistent(&OFFCHAIN_HEAD_DB);
assert_eq!(storage.get::<BlockNumber>().unwrap().unwrap(), 12);
})
}
#[test]
fn offchain_storage_prevents_duplicate() {
let mut ext = ExtBuilder::default()
.offchain_phragmen_ext()
.validator_count(4)
.build();
ext.execute_with(|| {
use offchain_election::OFFCHAIN_HEAD_DB;
use sp_runtime::offchain::storage::StorageValueRef;
let storage = StorageValueRef::persistent(&OFFCHAIN_HEAD_DB);
run_to_block(12);