1
// Copyright (C) Moondance Labs Ltd.
2
// This file is part of Tanssi.
3

            
4
// Tanssi is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Tanssi is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Tanssi.  If not, see <http://www.gnu.org/licenses/>
16

            
17
#![cfg(test)]
18

            
19
use {crate::tests::common::*, frame_support::assert_ok, sp_std::vec};
20

            
21
#[test]
22
1
fn session_key_changes_are_reflected_after_two_sessions() {
23
1
    ExtBuilder::default()
24
1
        .with_balances(vec![
25
1
            // Alice gets 10k extra tokens for her mapping deposit
26
1
            (AccountId::from(ALICE), 210_000 * UNIT),
27
1
            (AccountId::from(BOB), 100_000 * UNIT),
28
1
            (AccountId::from(CHARLIE), 100_000 * UNIT),
29
1
            (AccountId::from(DAVE), 100_000 * UNIT),
30
1
        ])
31
1
        .build()
32
1
        .execute_with(|| {
33
1
            run_to_block(2);
34
1
            let alice_keys = get_authority_keys_from_seed(&AccountId::from(ALICE).to_string());
35
1
            let dave_keys = get_authority_keys_from_seed(&AccountId::from(DAVE).to_string());
36
1

            
37
1
            // let's assert that session keys in all pallets are this
38
1
            // Babe
39
1
            assert!(babe_authorities().contains(&alice_keys.babe.clone()));
40
1
            assert!(grandpa_authorities().contains(&alice_keys.grandpa.clone()));
41
1
            assert!(!babe_authorities().contains(&dave_keys.babe.clone()));
42
1
            assert!(!grandpa_authorities().contains(&dave_keys.grandpa.clone()));
43

            
44
1
            assert_ok!(Session::set_keys(
45
1
                origin_of(ALICE.into()),
46
1
                crate::SessionKeys {
47
1
                    babe: dave_keys.babe.clone(),
48
1
                    grandpa: dave_keys.grandpa.clone(),
49
1
                    para_validator: dave_keys.para_validator.clone(),
50
1
                    para_assignment: dave_keys.para_assignment.clone(),
51
1
                    authority_discovery: dave_keys.authority_discovery.clone(),
52
1
                    beefy: dave_keys.beefy.clone(),
53
1
                    nimbus: dave_keys.nimbus.clone(),
54
1
                },
55
1
                vec![]
56
1
            ));
57

            
58
            // In session one keys are not yet set
59
1
            run_to_session(1u32);
60
1
            assert!(babe_authorities().contains(&alice_keys.babe.clone()));
61
1
            assert!(grandpa_authorities().contains(&alice_keys.grandpa.clone()));
62
1
            assert!(!babe_authorities().contains(&dave_keys.babe.clone()));
63
1
            assert!(!grandpa_authorities().contains(&dave_keys.grandpa.clone()));
64

            
65
            // In session  2 they should be set
66
1
            run_to_session(2u32);
67
1

            
68
1
            // While Babe changes are applied immediately (on_initialize)
69
1
            // Grandpa changes are applied on-finalize
70
1
            // Our tests only stop at on_initialize of the target block,
71
1
            // thus we need to create one more block
72
1
            assert!(babe_authorities().contains(&dave_keys.babe.clone()));
73
1
            assert!(!babe_authorities().contains(&alice_keys.babe.clone()));
74
1
            assert!(grandpa_authorities().contains(&alice_keys.grandpa.clone()));
75
1
            assert!(!grandpa_authorities().contains(&dave_keys.grandpa.clone()));
76

            
77
1
            let block_number = System::block_number();
78
1
            run_to_block(block_number + 1);
79
1
            assert!(babe_authorities().contains(&dave_keys.babe.clone()));
80
1
            assert!(!babe_authorities().contains(&alice_keys.babe.clone()));
81
1
            assert!(grandpa_authorities().contains(&dave_keys.grandpa.clone()));
82
1
            assert!(!grandpa_authorities().contains(&alice_keys.grandpa.clone()));
83
1
        });
84
1
}