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 {
18
    container_chain_template_simple_runtime::{
19
        AccountId, MaintenanceModeConfig, MigrationsConfig, PolkadotXcmConfig,
20
    },
21
    cumulus_primitives_core::ParaId,
22
    node_common::chain_spec::Extensions,
23
    sc_network::config::MultiaddrWithPeerId,
24
    sc_service::ChainType,
25
    sp_keyring::Sr25519Keyring,
26
};
27

            
28
/// Specialized `ChainSpec` for the normal parachain runtime.
29
pub type ChainSpec = sc_service::GenericChainSpec<Extensions>;
30

            
31
/// Orcherstrator's parachain id
32
pub const ORCHESTRATOR: ParaId = ParaId::new(1000);
33

            
34
2
pub fn development_config(para_id: ParaId, boot_nodes: Vec<String>) -> ChainSpec {
35
2
    // Give your base currency a unit name and decimal places
36
2
    let mut properties = sc_chain_spec::Properties::new();
37
2
    properties.insert("tokenSymbol".into(), "UNIT".into());
38
2
    properties.insert("tokenDecimals".into(), 12.into());
39
2
    properties.insert("ss58Format".into(), 42.into());
40
2
    properties.insert("isEthereum".into(), false.into());
41
2

            
42
2
    let mut default_funded_accounts = pre_funded_accounts();
43
2
    default_funded_accounts.sort();
44
2
    default_funded_accounts.dedup();
45
2
    let boot_nodes: Vec<MultiaddrWithPeerId> = boot_nodes
46
2
        .into_iter()
47
2
        .map(|x| {
48
            x.parse::<MultiaddrWithPeerId>()
49
                .unwrap_or_else(|e| panic!("invalid bootnode address format {:?}: {:?}", x, e))
50
2
        })
51
2
        .collect();
52
2

            
53
2
    ChainSpec::builder(
54
2
        container_chain_template_simple_runtime::WASM_BINARY
55
2
            .expect("WASM binary was not built, please build it!"),
56
2
        Extensions {
57
2
            relay_chain: "rococo-local".into(), // You MUST set this to the correct network!
58
2
            para_id: para_id.into(),
59
2
        },
60
2
    )
61
2
    .with_name("Development")
62
2
    .with_id("dev")
63
2
    .with_chain_type(ChainType::Development)
64
2
    .with_genesis_config(testnet_genesis(
65
2
        default_funded_accounts.clone(),
66
2
        para_id,
67
2
        Sr25519Keyring::Alice.to_account_id(),
68
2
    ))
69
2
    .with_properties(properties)
70
2
    .with_boot_nodes(boot_nodes)
71
2
    .build()
72
2
}
73

            
74
pub fn local_testnet_config(para_id: ParaId, boot_nodes: Vec<String>) -> ChainSpec {
75
    // Give your base currency a unit name and decimal places
76
    let mut properties = sc_chain_spec::Properties::new();
77
    properties.insert("tokenSymbol".into(), "UNIT".into());
78
    properties.insert("tokenDecimals".into(), 12.into());
79
    properties.insert("ss58Format".into(), 42.into());
80
    properties.insert("isEthereum".into(), false.into());
81
    let protocol_id = format!("container-chain-{}", para_id);
82

            
83
    let mut default_funded_accounts = pre_funded_accounts();
84
    default_funded_accounts.sort();
85
    default_funded_accounts.dedup();
86
    let boot_nodes: Vec<MultiaddrWithPeerId> = boot_nodes
87
        .into_iter()
88
        .map(|x| {
89
            x.parse::<MultiaddrWithPeerId>()
90
                .unwrap_or_else(|e| panic!("invalid bootnode address format {:?}: {:?}", x, e))
91
        })
92
        .collect();
93

            
94
    ChainSpec::builder(
95
        container_chain_template_simple_runtime::WASM_BINARY
96
            .expect("WASM binary was not built, please build it!"),
97
        Extensions {
98
            relay_chain: "rococo-local".into(), // You MUST set this to the correct network!
99
            para_id: para_id.into(),
100
        },
101
    )
102
    .with_name(&format!("Simple Container {}", para_id))
103
    .with_id(&format!("simple_container_{}", para_id))
104
    .with_chain_type(ChainType::Local)
105
    .with_genesis_config(testnet_genesis(
106
        default_funded_accounts.clone(),
107
        para_id,
108
        Sr25519Keyring::Alice.to_account_id(),
109
    ))
110
    .with_properties(properties)
111
    .with_protocol_id(&protocol_id)
112
    .with_boot_nodes(boot_nodes)
113
    .build()
114
}
115

            
116
2
fn testnet_genesis(
117
2
    endowed_accounts: Vec<AccountId>,
118
2
    id: ParaId,
119
2
    root_key: AccountId,
120
2
) -> serde_json::Value {
121
2
    let g = container_chain_template_simple_runtime::RuntimeGenesisConfig {
122
2
        balances: container_chain_template_simple_runtime::BalancesConfig {
123
2
            balances: endowed_accounts
124
2
                .iter()
125
2
                .cloned()
126
24
                .map(|k| (k, 1 << 60))
127
2
                .collect(),
128
2
        },
129
2
        parachain_info: container_chain_template_simple_runtime::ParachainInfoConfig {
130
2
            parachain_id: id,
131
2
            ..Default::default()
132
2
        },
133
2
        parachain_system: Default::default(),
134
2
        sudo: container_chain_template_simple_runtime::SudoConfig {
135
2
            key: Some(root_key),
136
2
        },
137
2
        authorities_noting: container_chain_template_simple_runtime::AuthoritiesNotingConfig {
138
2
            orchestrator_para_id: ORCHESTRATOR,
139
2
            ..Default::default()
140
2
        },
141
2
        migrations: MigrationsConfig::default(),
142
2
        maintenance_mode: MaintenanceModeConfig {
143
2
            start_in_maintenance_mode: false,
144
2
            ..Default::default()
145
2
        },
146
2
        // This should initialize it to whatever we have set in the pallet
147
2
        polkadot_xcm: PolkadotXcmConfig::default(),
148
2
        transaction_payment: Default::default(),
149
2
        tx_pause: Default::default(),
150
2
        system: Default::default(),
151
2
    };
152
2

            
153
2
    serde_json::to_value(g).unwrap()
154
2
}
155

            
156
/// Get pre-funded accounts
157
2
pub fn pre_funded_accounts() -> Vec<AccountId> {
158
2
    Sr25519Keyring::well_known()
159
24
        .map(|k| k.to_account_id())
160
2
        .collect()
161
2
}