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
//! Generate mocked values of relay chain storage, for dev tests
18

            
19
use {
20
    frame_support::Hashable,
21
    sp_core::{ed25519, Pair},
22
    tc_consensus::{Decode, Encode, ParaId},
23
};
24

            
25
7810
pub fn get_mocked_registrar_paras(para_id: ParaId) -> (Vec<u8>, Vec<u8>) {
26
7810
    let bytes = para_id.twox_64_concat();
27
7810

            
28
7810
    let pairs = get_ed25519_pairs(1);
29
7810
    //panic!("relay manager private key: {:?}", pairs[0].seed());
30
7810
    let registrar_paras_key = [REGISTRAR_PARAS_INDEX, bytes.as_slice()].concat();
31
7810
    let para_info: ParaInfo<
32
7810
        cumulus_primitives_core::relay_chain::AccountId,
33
7810
        cumulus_primitives_core::relay_chain::Balance,
34
7810
    > = ParaInfo {
35
7810
        manager: pairs[0].public().into(),
36
7810
        deposit: Default::default(),
37
7810
        locked: None,
38
7810
    };
39
7810

            
40
7810
    (registrar_paras_key, para_info.encode())
41
7810
}
42

            
43
// TODO: import this from dancekit
44
pub const REGISTRAR_PARAS_INDEX: &[u8] =
45
    &hex_literal::hex!["3fba98689ebed1138735e0e7a5a790abcd710b30bd2eab0352ddcc26417aa194"];
46

            
47
// Need to copy ParaInfo from
48
// polkadot-sdk/polkadot/runtime/common/src/paras_registrar/mod.rs
49
// Because its fields are not public...
50
// TODO: import this from dancekit
51
#[derive(Encode, Decode, Clone, PartialEq, Eq, Default)]
52
pub struct ParaInfo<Account, Balance> {
53
    /// The account that has placed a deposit for registering this para.
54
    manager: Account,
55
    /// The amount reserved by the `manager` account for the registration.
56
    deposit: Balance,
57
    /// Whether the para registration should be locked from being controlled by the manager.
58
    /// None means the lock had not been explicitly set, and should be treated as false.
59
    locked: Option<bool>,
60
}
61

            
62
7810
pub fn get_ed25519_pairs(num: u32) -> Vec<ed25519::Pair> {
63
7810
    let seed: u128 = 12345678901234567890123456789012;
64
7810
    let mut pairs = Vec::new();
65
7810
    for i in 0..num {
66
7810
        pairs.push(ed25519::Pair::from_seed(
67
7810
            (seed + u128::from(i))
68
7810
                .to_string()
69
7810
                .as_bytes()
70
7810
                .try_into()
71
7810
                .unwrap(),
72
7810
        ))
73
    }
74
7810
    pairs
75
7810
}