Newer
Older
System::set_block_number(block);
Timestamp::set_timestamp(block*5); // a little late.
Session::check_rotate_session(System::block_number());
assert_eq!(Staking::current_era(), 0);
assert_eq!(Session::current_index(), 2);
assert_eq!(Staking::current_session_reward(), session_reward);
assert_eq!(Staking::current_era_reward(), 2*session_reward);
block = 9; // Block 9 => Session 3 => Era 1
System::set_block_number(block);
Session::check_rotate_session(System::block_number());
assert_eq!(Staking::current_era(), 1);
assert_eq!(Session::current_index(), 3);
// whats left to be shared is the sum of 3 rounds minus the validator's cut.
let shared_cut = 3 * session_reward - validator_cut;
// Validator's payee is Staked account, 11, reward will be paid here.
assert_eq!(Balances::total_balance(&11), validator_initial_balance + shared_cut/2 + validator_cut);
// Controller account will not get any reward.
assert_eq!(Balances::total_balance(&10), 1);
// Rest of the reward will be shared and paid to the nominator in stake.
assert_eq!(Balances::total_balance(&2), 20 + shared_cut/2);
});
}
#[test]
fn bond_extra_works() {
// Tests that extra `free_balance` in the stash can be added to stake
// NOTE: this tests only verifies `StakingLedger` for correct updates.
// See `bond_extra_and_withdraw_unbonded_works` for more details and updates on `Exposure`.
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
with_externalities(&mut ExtBuilder::default().build(),
|| {
// Check that account 10 is a validator
assert!(<Validators<Test>>::exists(10));
// Check that account 10 is bonded to account 11
assert_eq!(Staking::bonded(&11), Some(10));
// Check how much is at stake
assert_eq!(Staking::ledger(&10), Some(StakingLedger { stash: 11, total: 1000, active: 1000, unlocking: vec![] }));
// Give account 11 some large free balance greater than total
Balances::set_free_balance(&11, 1000000);
// Check the balance of the stash account
assert_eq!(Balances::free_balance(&11), 1000000);
// Call the bond_extra function from controller, add only 100
assert_ok!(Staking::bond_extra(Origin::signed(10), 100));
// There should be 100 more `total` and `active` in the ledger
assert_eq!(Staking::ledger(&10), Some(StakingLedger { stash: 11, total: 1000 + 100, active: 1000 + 100, unlocking: vec![] }));
// Call the bond_extra function with a large number, should handle it
assert_ok!(Staking::bond_extra(Origin::signed(10), u64::max_value()));
// The full amount of the funds should now be in the total and active
assert_eq!(Staking::ledger(&10), Some(StakingLedger { stash: 11, total: 1000000, active: 1000000, unlocking: vec![] }));
});
}
#[test]
fn bond_extra_and_withdraw_unbonded_works() {
// * Should test
// * Given an account being bonded [and chosen as a validator](not mandatory)
// * It can add extra funds to the bonded account.
// * it can unbond a portion of its funds from the stash account.
// * Once the unbonding period is done, it can actually take the funds out of the stash.
with_externalities(&mut ExtBuilder::default()
.reward(10) // it is the default, just for verbosity
.nominate(false)
.build(),
|| {
// Set payee to controller. avoids confusion
assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Controller));
// Set unbonding era (bonding_duration) to 2
assert_ok!(Staking::set_bonding_duration(2));
// Give account 11 some large free balance greater than total
Balances::set_free_balance(&11, 1000000);
// Check the balance of the stash account
assert_eq!(Balances::free_balance(&11), 1000000);
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
// Initial config should be correct
assert_eq!(Staking::sessions_per_era(), 1);
assert_eq!(Staking::current_era(), 0);
assert_eq!(Session::current_index(), 0);
assert_eq!(Staking::current_session_reward(), 10);
// check the balance of a validator accounts.
assert_eq!(Balances::total_balance(&10), 1);
// confirm that 10 is a normal validator and gets paid at the end of the era.
System::set_block_number(1);
Timestamp::set_timestamp(5);
Session::check_rotate_session(System::block_number());
assert_eq!(Staking::current_era(), 1);
assert_eq!(Session::current_index(), 1);
// NOTE: despite having .nominate() in extBuilder, 20 doesn't have a share since
// rewards are paid before election in new_era()
assert_eq!(Balances::total_balance(&10), 1 + 10);
// Initial state of 10
assert_eq!(Staking::ledger(&10), Some(StakingLedger { stash: 11, total: 1000, active: 1000, unlocking: vec![] }));
assert_eq!(Staking::stakers(&10), Exposure { total: 1000, own: 1000, others: vec![] });
// deposit the extra 100 units
Staking::bond_extra(Origin::signed(10), 100).unwrap();
assert_eq!(Staking::ledger(&10), Some(StakingLedger { stash: 11, total: 1000 + 100, active: 1000 + 100, unlocking: vec![] }));
// Exposure is a snapshot! only updated after the next era update.
assert_ne!(Staking::stakers(&10), Exposure { total: 1000 + 100, own: 1000 + 100, others: vec![] });
// trigger next era.
System::set_block_number(2);Timestamp::set_timestamp(10);Session::check_rotate_session(System::block_number());
assert_eq!(Staking::current_era(), 2);
assert_eq!(Session::current_index(), 2);
// ledger should be the same.
assert_eq!(Staking::ledger(&10), Some(StakingLedger { stash: 11, total: 1000 + 100, active: 1000 + 100, unlocking: vec![] }));
// Exposure is now updated.
assert_eq!(Staking::stakers(&10), Exposure { total: 1000 + 100, own: 1000 + 100, others: vec![] });
// Note that by this point 10 also have received more rewards, but we don't care now.
// assert_eq!(Balances::total_balance(&10), 1 + 10 + MORE_REWARD);
// Unbond almost all of the funds in stash.
Staking::unbond(Origin::signed(10), 1000).unwrap();
assert_eq!(Staking::ledger(&10), Some(StakingLedger {
stash: 11, total: 1000 + 100, active: 100, unlocking: vec![UnlockChunk{ value: 1000, era: 2 + 2}] }));
// Attempting to free the balances now will fail. 2 eras need to pass.
Staking::withdraw_unbonded(Origin::signed(10)).unwrap();
assert_eq!(Staking::ledger(&10), Some(StakingLedger {
stash: 11, total: 1000 + 100, active: 100, unlocking: vec![UnlockChunk{ value: 1000, era: 2 + 2}] }));
// trigger next era.
System::set_block_number(3);Timestamp::set_timestamp(15);Session::check_rotate_session(System::block_number());
assert_eq!(Staking::current_era(), 3);
assert_eq!(Session::current_index(), 3);
// nothing yet
Staking::withdraw_unbonded(Origin::signed(10)).unwrap();
assert_eq!(Staking::ledger(&10), Some(StakingLedger {
stash: 11, total: 1000 + 100, active: 100, unlocking: vec![UnlockChunk{ value: 1000, era: 2 + 2}] }));
// trigger next era.
System::set_block_number(4);Timestamp::set_timestamp(20);Session::check_rotate_session(System::block_number());
assert_eq!(Staking::current_era(), 4);
assert_eq!(Session::current_index(), 4);
Staking::withdraw_unbonded(Origin::signed(10)).unwrap();
// Now the value is free and the staking ledger is updated.
assert_eq!(Staking::ledger(&10), Some(StakingLedger {
stash: 11, total: 100, active: 100, unlocking: vec![] }));
})
}
#[test]
fn slot_stake_is_least_staked_validator_and_limits_maximum_punishment() {
// Test that slot_stake is determined by the least staked validator
// Test that slot_stake is the maximum punishment that can happen to a validator
// Note that rewardDestination is the stash account by default
// Note that unlike reward slash will affect free_balance, not the stash account.
with_externalities(&mut ExtBuilder::default().nominate(false).build(), || {
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
// Confirm validator count is 2
assert_eq!(Staking::validator_count(), 2);
// Confirm account 10 and 20 are validators
assert!(<Validators<Test>>::exists(&10) && <Validators<Test>>::exists(&20));
// Confirm 10 has less stake than 20
assert!(Staking::stakers(&10).total < Staking::stakers(&20).total);
assert_eq!(Staking::stakers(&10).total, 1000);
assert_eq!(Staking::stakers(&20).total, 2000);
// Give the man some money.
Balances::set_free_balance(&10, 1000);
Balances::set_free_balance(&20, 1000);
// Confirm initial free balance.
assert_eq!(Balances::free_balance(&10), 1000);
assert_eq!(Balances::free_balance(&20), 1000);
// We confirm initialized slot_stake is this value
assert_eq!(Staking::slot_stake(), Staking::stakers(&10).total);
// Now lets lower account 20 stake
<Stakers<Test>>::insert(&20, Exposure { total: 69, own: 69, others: vec![] });
assert_eq!(Staking::stakers(&20).total, 69);
<Ledger<Test>>::insert(&20, StakingLedger { stash: 22, total: 69, active: 69, unlocking: vec![] });
// New era --> rewards are paid --> stakes are changed
System::set_block_number(1);
Timestamp::set_timestamp(5);
Session::check_rotate_session(System::block_number());
assert_eq!(Staking::current_era(), 1);
// -- new balances + reward
assert_eq!(Staking::stakers(&10).total, 1000 + 10);
assert_eq!(Staking::stakers(&20).total, 69 + 10);
// -- Note that rewards are going directly to stash, not as free balance.
assert_eq!(Balances::free_balance(&10), 1000);
assert_eq!(Balances::free_balance(&20), 1000);
// -- slot stake should also be updated.
assert_eq!(Staking::slot_stake(), 79);
// // If 10 gets slashed now, despite having +1000 in stash, it will be slashed byt 79, which is the slot stake
Staking::on_offline_validator(10, 4);
// // Confirm user has been reported
assert_eq!(Staking::slash_count(&10), 4);
// // check the balance of 10 (slash will be deducted from free balance.)
assert_eq!(Balances::free_balance(&10), 1000 - 79);
#[test]
fn on_free_balance_zero_stash_removes_validator() {
// Tests that validator storage items are cleaned up when stash is empty
// Tests that storage items are untouched when controller is empty
with_externalities(&mut ExtBuilder::default()
.existential_deposit(10)
.build(),
|| {
// Check that account 10 is a validator
assert!(<Validators<Test>>::exists(10));
// Check the balance of the validator account
assert_eq!(Balances::free_balance(&10), 256);
// Check the balance of the stash account
assert_eq!(Balances::free_balance(&11), 256000);
// Check these two accounts are bonded
assert_eq!(Staking::bonded(&11), Some(10));
// Set some storage items which we expect to be cleaned up
// Initiate slash count storage item
Staking::on_offline_validator(10, 1);
// Set payee information
assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Stash));
// Check storage items that should be cleaned up
assert!(<Ledger<Test>>::exists(&10));
assert!(<Validators<Test>>::exists(&10));
assert!(<SlashCount<Test>>::exists(&10));
assert!(<Payee<Test>>::exists(&10));
// Reduce free_balance of controller to 0
Balances::set_free_balance(&10, 0);
// Check total balance of account 10
assert_eq!(Balances::total_balance(&10), 0);
// Check the balance of the stash account has not been touched
assert_eq!(Balances::free_balance(&11), 256000);
// Check these two accounts are still bonded
assert_eq!(Staking::bonded(&11), Some(10));
// Check storage items have not changed
assert!(<Ledger<Test>>::exists(&10));
assert!(<Validators<Test>>::exists(&10));
assert!(<SlashCount<Test>>::exists(&10));
assert!(<Payee<Test>>::exists(&10));
// Reduce free_balance of stash to 0
Balances::set_free_balance(&11, 0);
// Check total balance of stash
assert_eq!(Balances::total_balance(&11), 0);
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
// Check storage items do not exist
assert!(!<Ledger<Test>>::exists(&10));
assert!(!<Validators<Test>>::exists(&10));
assert!(!<Nominators<Test>>::exists(&10));
assert!(!<SlashCount<Test>>::exists(&10));
assert!(!<Payee<Test>>::exists(&10));
assert!(!<Bonded<Test>>::exists(&11));
});
}
#[test]
fn on_free_balance_zero_stash_removes_nominator() {
// Tests that nominator storage items are cleaned up when stash is empty
// Tests that storage items are untouched when controller is empty
with_externalities(&mut ExtBuilder::default()
.existential_deposit(10)
.build(),
|| {
// Make 10 a nominator
assert_ok!(Staking::nominate(Origin::signed(10), vec![20]));
// Check that account 10 is a nominator
assert!(<Nominators<Test>>::exists(10));
// Check the balance of the nominator account
assert_eq!(Balances::free_balance(&10), 256);
// Check the balance of the stash account
assert_eq!(Balances::free_balance(&11), 256000);
// Check these two accounts are bonded
assert_eq!(Staking::bonded(&11), Some(10));
// Set payee information
assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Stash));
// Check storage items that should be cleaned up
assert!(<Ledger<Test>>::exists(&10));
assert!(<Nominators<Test>>::exists(&10));
assert!(<Payee<Test>>::exists(&10));
// Reduce free_balance of controller to 0
Balances::set_free_balance(&10, 0);
// Check total balance of account 10
assert_eq!(Balances::total_balance(&10), 0);
// Check the balance of the stash account has not been touched
assert_eq!(Balances::free_balance(&11), 256000);
// Check these two accounts are still bonded
assert_eq!(Staking::bonded(&11), Some(10));
// Check storage items have not changed
assert!(<Ledger<Test>>::exists(&10));
assert!(<Nominators<Test>>::exists(&10));
assert!(<Payee<Test>>::exists(&10));
// Reduce free_balance of stash to 0
Balances::set_free_balance(&11, 0);
// Check total balance of stash
assert_eq!(Balances::total_balance(&11), 0);
// Check storage items do not exist
assert!(!<Ledger<Test>>::exists(&10));
assert!(!<Validators<Test>>::exists(&10));
assert!(!<Nominators<Test>>::exists(&10));
assert!(!<SlashCount<Test>>::exists(&10));
assert!(!<Payee<Test>>::exists(&10));
assert!(!<Bonded<Test>>::exists(&11));
});
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
}
#[test]
fn phragmen_poc_works() {
// Tests the POC test of the phragmen, mentioned in the paper and reference implementation.
// Initial votes:
// vote_list = [
// ("A", 10.0, ["X", "Y"]),
// ("B", 20.0, ["X", "Z"]),
// ("C", 30.0, ["Y", "Z"])
// ]
//
// Sequential Phragmén gives
// Z is elected with stake 35.0 and score 0.02
// Y is elected with stake 25.0 and score 0.04
//
// A has load 0.04 and supported
// X with stake 0.0 Y with stake 10.0
// B has load 0.02 and supported
// X with stake 0.0 Z with stake 20.0
// C has load 0.04 and supported
// Y with stake 15.0 Z with stake 15.0
//
// NOTE: doesn't X/Y/Z's stash value make a difference here in phragmen?
with_externalities(&mut ExtBuilder::default()
.nominate(false)
.build(),
|| {
// initial setup of 10 and 20, both validators.
assert_eq!(Session::validators(), vec![20, 10]);
assert_eq!(Staking::ledger(&10), Some(StakingLedger { stash: 11, total: 1000, active: 1000, unlocking: vec![] }));
assert_eq!(Staking::ledger(&20), Some(StakingLedger { stash: 21, total: 2000, active: 2000, unlocking: vec![] }));
assert_eq!(Staking::validators(10), ValidatorPrefs::default());
assert_eq!(Staking::validators(20), ValidatorPrefs::default());
assert_eq!(Balances::free_balance(10), 1);
assert_eq!(Balances::free_balance(20), 1);
// no one is a nominator
assert_eq!(<Nominators<Test>>::enumerate().count(), 0 as usize);
// Bond [30, 31] as the third validator
assert_ok!(Staking::bond(Origin::signed(31), 30, 1000, RewardDestination::default()));
assert_ok!(Staking::validate(Origin::signed(30), ValidatorPrefs::default()));
// bond [2,1](A), [4,3](B), [6,5](C) as the 3 nominators
// Give all of them some balance to be able to bond properly.
for i in &[1, 3, 5] { Balances::set_free_balance(i, 50); }
// Linking names to the above test:
// 10 => X
// 20 => Y
// 30 => Z
assert_ok!(Staking::bond(Origin::signed(1), 2, 10, RewardDestination::default()));
assert_ok!(Staking::nominate(Origin::signed(2), vec![10, 20]));
assert_ok!(Staking::bond(Origin::signed(3), 4, 20, RewardDestination::default()));
assert_ok!(Staking::nominate(Origin::signed(4), vec![10, 30]));
assert_ok!(Staking::bond(Origin::signed(5), 6, 30, RewardDestination::default()));
assert_ok!(Staking::nominate(Origin::signed(6), vec![20, 30]));
// New era => election algorithm will trigger
System::set_block_number(1);
Session::check_rotate_session(System::block_number());
// Z and Y are chosen
assert_eq!(Session::validators(), vec![30, 20]);
// with stake 35 and 25 respectively
// This is only because 30 has been bonded on the fly, exposures are stored at the very end of the era.
// 35 is the point, not 'own' Exposure.
assert_eq!(Staking::stakers(30).own, 0);
assert_eq!(Staking::stakers(30).total, 0 + 35);
// same as above. +25 is the point
assert_eq!(Staking::stakers(20).own, 2010);
assert_eq!(Staking::stakers(20).total, 2010 + 25);
// 30(Z) was supported by B-4 and C-6 with stake 20 and 15 respectively.
assert_eq!(Staking::stakers(30).others.iter().map(|e| e.value).collect::<Vec<BalanceOf<Test>>>(), vec![15, 20]);
assert_eq!(Staking::stakers(30).others.iter().map(|e| e.who).collect::<Vec<BalanceOf<Test>>>(), vec![6, 4]);
// 20(Y) was supported by A-2 and C-6 with stake 10 and 15 respectively.
assert_eq!(Staking::stakers(20).others.iter().map(|e| e.value).collect::<Vec<BalanceOf<Test>>>(), vec![15, 10]);
assert_eq!(Staking::stakers(20).others.iter().map(|e| e.who).collect::<Vec<BalanceOf<Test>>>(), vec![6, 2]);
});
}
#[test]
fn phragmen_election_works() {
// tests the encapsulated phragmen::elect function.
with_externalities(&mut ExtBuilder::default().nominate(false).build(), || {
// initial setup of 10 and 20, both validators
assert_eq!(Session::validators(), vec![20, 10]);
// no one is a nominator
assert_eq!(<Nominators<Test>>::enumerate().count(), 0 as usize);
// Bond [30, 31] as the third validator
assert_ok!(Staking::bond(Origin::signed(31), 30, 1000, RewardDestination::default()));
assert_ok!(Staking::validate(Origin::signed(30), ValidatorPrefs::default()));
// bond [2,1](A), [4,3](B), as 2 nominators
// Give all of them some balance to be able to bond properly.
for i in &[1, 3] { Balances::set_free_balance(i, 50); }
assert_ok!(Staking::bond(Origin::signed(1), 2, 5, RewardDestination::default()));
assert_ok!(Staking::nominate(Origin::signed(2), vec![10, 20]));
assert_ok!(Staking::bond(Origin::signed(3), 4, 45, RewardDestination::default()));
assert_ok!(Staking::nominate(Origin::signed(4), vec![10, 30]));
let rounds = || 2 as usize;
let validators = || <Validators<Test>>::enumerate();
let nominators = || <Nominators<Test>>::enumerate();
let stash_of = |w| Staking::stash_balance(&w);
let min_validator_count = Staking::minimum_validator_count() as usize;
let winners = phragmen::elect::<Test, _, _, _, _>(
rounds,
validators,
nominators,
stash_of,
min_validator_count
);
// 10 and 30 must be the winners
assert_eq!(winners.iter().map(|w| w.who).collect::<Vec<BalanceOf<Test>>>(), vec![10, 30]);
let winner_10 = winners.iter().filter(|w| w.who == 10).nth(0).unwrap();
let winner_30 = winners.iter().filter(|w| w.who == 30).nth(0).unwrap();
// python implementation output:
/*
10 is elected with stake 26.31578947368421 and score 0.02
30 is elected with stake 23.684210526315788 and score 0.042222222222222223
2 has load 0.02 and supported
10 with stake 5.0 20 with stake 0.0
4 has load 0.042222222222222223 and supported
10 with stake 21.31578947368421 30 with stake 23.684210526315788
*/
assert_eq!(winner_10.exposure.total, 1000 + 26);
assert_eq!(winner_10.score, Perquintill::from_fraction(0.02));
assert_eq!(winner_10.exposure.others[0].value, 21);
assert_eq!(winner_10.exposure.others[1].value, 5);
assert_eq!(winner_30.exposure.total, 23);
assert_eq!(winner_30.score, Perquintill::from_quintillionths(42222222222222222));
assert_eq!(winner_30.exposure.others[0].value, 23);
})
}
#[test]
fn switching_roles() {
// Show: It should be possible to switch between roles (nominator, validator, idle) with minimal overhead.
with_externalities(&mut ExtBuilder::default()
.nominate(false)
.sessions_per_era(3)
.build(),
|| {
assert_eq!(Session::validators(), vec![20, 10]);
// put some money in account that we'll use.
for i in 1..7 { Balances::set_free_balance(&i, 5000); }
// add 2 nominators
assert_ok!(Staking::bond(Origin::signed(1), 2, 2000, RewardDestination::default()));
assert_ok!(Staking::nominate(Origin::signed(2), vec![10, 6]));
assert_ok!(Staking::bond(Origin::signed(3), 4, 500, RewardDestination::default()));
assert_ok!(Staking::nominate(Origin::signed(4), vec![20, 2]));
// add a new validator candidate
assert_ok!(Staking::bond(Origin::signed(5), 6, 1500, RewardDestination::Controller));
assert_ok!(Staking::validate(Origin::signed(6), ValidatorPrefs::default()));
// new block
System::set_block_number(1);
Session::check_rotate_session(System::block_number());
// no change
assert_eq!(Session::validators(), vec![20, 10]);
// new block
System::set_block_number(2);
Session::check_rotate_session(System::block_number());
// no change
assert_eq!(Session::validators(), vec![20, 10]);
// new block --> ne era --> new validators
System::set_block_number(3);
Session::check_rotate_session(System::block_number());
// with current nominators 10 and 4 have the most stake
assert_eq!(Session::validators(), vec![6, 10]);
// 2 decides to be a validator. Consequences:
// 6 will not be chosen in the next round (no votes)
// 2 itself will be chosen + 20 who now has the higher votes
// 10 wil have no votes.
assert_ok!(Staking::validate(Origin::signed(2), ValidatorPrefs::default()));
System::set_block_number(4);
Session::check_rotate_session(System::block_number());
assert_eq!(Session::validators(), vec![6, 10]);
System::set_block_number(5);
Session::check_rotate_session(System::block_number());
assert_eq!(Session::validators(), vec![6, 10]);
// ne era
System::set_block_number(6);
Session::check_rotate_session(System::block_number());
assert_eq!(Session::validators(), vec![2, 20]);
});
}
#[test]
fn wrong_vote_is_null() {
with_externalities(&mut ExtBuilder::default()
.session_length(1)
.sessions_per_era(1)
.nominate(false)
.validator_pool(true)
.build(),
|| {
// from the first era onward, only two will be chosen
assert_eq!(Session::validators(), vec![40, 30, 20, 10]);
// put some money in account that we'll use.
for i in 1..3 { Balances::set_free_balance(&i, 5000); }
// add 1 nominators
assert_ok!(Staking::bond(Origin::signed(1), 2, 2000, RewardDestination::default()));
assert_ok!(Staking::nominate(Origin::signed(2), vec![
10, 20, // good votes
1, 2, 15, 1000, 25 // crap votes. No effect.
]));
// new block
System::set_block_number(1);
Session::check_rotate_session(System::block_number());
assert_eq!(Session::validators(), vec![20, 10]);
});
}