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
use crate::tests::common::ExtBuilder;
18
use crate::{BeefyMmrLeaf, ExternalValidators, PalletInfo, Runtime, Session};
19
use beefy_primitives::mmr::BeefyAuthoritySet;
20
use frame_support::migration::clear_storage_prefix;
21
use frame_support::storage::unhashed;
22
use frame_support::traits::PalletInfo as _;
23
use pallet_migrations::Migration;
24
use tanssi_runtime_common::migrations::{ExternalValidatorsInitialMigration, MigrateMMRLeafPallet};
25
use xcm::v3::Weight;
26

            
27
#[test]
28
1
fn test_migration_mmr_leaf_pallet_renaming() {
29
1
    ExtBuilder::default().build().execute_with(|| {
30
1
        let migrate_mmr_leaf_pallet = MigrateMMRLeafPallet::<Runtime>(Default::default());
31
1
        let old_pallet_name = MigrateMMRLeafPallet::<Runtime>::old_pallet_name();
32
1
        let old_storage_1 = frame_support::storage::storage_prefix(
33
1
            old_pallet_name.as_bytes(),
34
1
            b"example_storage_1",
35
1
        );
36
1
        let new_storage_1 = frame_support::storage::storage_prefix(
37
1
            PalletInfo::name::<BeefyMmrLeaf>()
38
1
                .expect("BeefyMMRLeaf pallet must be part of the runtime")
39
1
                .as_bytes(),
40
1
            b"example_storage_1",
41
1
        );
42
1
        unhashed::put(&old_storage_1, &1u64);
43
1

            
44
1
        let beefy_authority_set: BeefyAuthoritySet<()> = BeefyAuthoritySet {
45
1
            len: 5,
46
1
            ..Default::default()
47
1
        };
48
1
        let old_storage_2 = frame_support::storage::storage_prefix(
49
1
            old_pallet_name.as_bytes(),
50
1
            b"example_storage_2",
51
1
        );
52
1
        let new_storage_2 = frame_support::storage::storage_prefix(
53
1
            PalletInfo::name::<BeefyMmrLeaf>()
54
1
                .expect("BeefyMMRLeaf pallet must be part of the runtime")
55
1
                .as_bytes(),
56
1
            b"example_storage_2",
57
1
        );
58
1
        unhashed::put(&old_storage_2, &beefy_authority_set);
59
1

            
60
1
        let used_weight = migrate_mmr_leaf_pallet.migrate(Weight::MAX);
61
1
        assert_eq!(used_weight, Weight::MAX);
62

            
63
1
        assert_eq!(unhashed::get::<u64>(&old_storage_1), None);
64
1
        assert_eq!(unhashed::get::<BeefyAuthoritySet<()>>(&old_storage_2), None);
65

            
66
1
        assert_eq!(unhashed::get::<u64>(&new_storage_1), Some(1u64));
67
1
        assert_eq!(
68
1
            unhashed::get::<BeefyAuthoritySet<()>>(&new_storage_2),
69
1
            Some(BeefyAuthoritySet {
70
1
                len: 5,
71
1
                ..Default::default()
72
1
            })
73
1
        );
74
1
    });
75
1
}
76

            
77
#[test]
78
1
fn test_migration_external_validators_pallet() {
79
1
    ExtBuilder::default().build().execute_with(|| {
80
1
        let migrate_external_validators =
81
1
            ExternalValidatorsInitialMigration::<Runtime>(Default::default());
82
1
        let old_pallet_name = b"ValidatorManager";
83
1

            
84
1
        // Kill storage of ExternalValidators pallet, because this migration will initialize this pallet
85
1
        let _ = clear_storage_prefix(b"ExternalValidators", b"", b"", None, None);
86
1

            
87
1
        // Simulate adding data to the old pallet storage
88
1
        // The value is not used for anything, we only care that it is removed by the migration.
89
1
        let old_storage_key =
90
1
            frame_support::storage::storage_prefix(old_pallet_name, b"ValidatorsToAdd");
91
1
        let expected_validators: Vec<u64> = vec![5, 6];
92
1
        unhashed::put(&old_storage_key, &expected_validators);
93
1

            
94
1
        // Run migration
95
1
        let _used_weight = migrate_external_validators.migrate(Weight::MAX);
96
1

            
97
1
        // Assert that ValidatorManager pallet prefix is empty after migration
98
1
        let old_pallet_key = frame_support::storage::storage_prefix(old_pallet_name, b"");
99
1
        let old_storage_exists = unhashed::contains_prefixed_key(&old_pallet_key);
100
1
        assert!(
101
1
            !old_storage_exists,
102
            "Old pallet storage should be cleared after migration"
103
        );
104

            
105
        // Assert that ExternalValidators has the validators from ValidatorManager
106
1
        let migrated_validators = ExternalValidators::validators();
107
1
        let empty = vec![];
108
1
        assert_ne!(
109
            migrated_validators, empty,
110
            "ExternalValidators should not be empty after migration"
111
        );
112

            
113
        // ExternalValidators should be equal to validators from Session::queued_keys
114
2
        let expected_validators: Vec<_> = Session::queued_keys().into_iter().map(|x| x.0).collect();
115
1
        assert_eq!(migrated_validators, expected_validators);
116
1
    });
117
1
}