chain_spec.rs 6.28 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Copyright 2021 Axiom-Team
//
// This file is part of Substrate-Libre-Currency.
//
// Substrate-Libre-Currency is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License.
//
// Substrate-Libre-Currency is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>.

Shawn Tabrizi's avatar
Shawn Tabrizi committed
17
use node_template_runtime::{
Pascal Engélibert's avatar
fmt    
Pascal Engélibert committed
18
19
    AccountId, AuraConfig, BalancesConfig, GenesisConfig, GrandpaConfig, Signature, SudoConfig,
    SystemConfig, WASM_BINARY,
Shawn Tabrizi's avatar
Shawn Tabrizi committed
20
};
Pascal Engélibert's avatar
fmt    
Pascal Engélibert committed
21
use sc_service::ChainType;
Ricardo Rius's avatar
Ricardo Rius committed
22
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
Pascal Engélibert's avatar
fmt    
Pascal Engélibert committed
23
use sp_core::{sr25519, Pair, Public};
Ricardo Rius's avatar
Ricardo Rius committed
24
use sp_finality_grandpa::AuthorityId as GrandpaId;
Pascal Engélibert's avatar
fmt    
Pascal Engélibert committed
25
use sp_runtime::traits::{IdentifyAccount, Verify};
Shawn Tabrizi's avatar
Shawn Tabrizi committed
26

Dan Forbes's avatar
Dan Forbes committed
27
28
// The URL for the telemetry server.
// const STAGING_TELEMETRY_URL: &str = "wss://telemetry.polkadot.io/submit/";
Shawn Tabrizi's avatar
Shawn Tabrizi committed
29
30

/// Specialized `ChainSpec`. This is a specialization of the general Substrate ChainSpec type.
31
pub type ChainSpec = sc_service::GenericChainSpec<GenesisConfig>;
Shawn Tabrizi's avatar
Shawn Tabrizi committed
32

Dan Forbes's avatar
Dan Forbes committed
33
/// Generate a crypto pair from seed.
Shawn Tabrizi's avatar
Shawn Tabrizi committed
34
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
Pascal Engélibert's avatar
fmt    
Pascal Engélibert committed
35
36
37
    TPublic::Pair::from_string(&format!("//{}", seed), None)
        .expect("static values are valid; qed")
        .public()
Shawn Tabrizi's avatar
Shawn Tabrizi committed
38
39
}

Joshy Orndorff's avatar
Joshy Orndorff committed
40
41
type AccountPublic = <Signature as Verify>::Signer;

Dan Forbes's avatar
Dan Forbes committed
42
/// Generate an account ID from seed.
Pascal Engélibert's avatar
fmt    
Pascal Engélibert committed
43
44
45
pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId
where
    AccountPublic: From<<TPublic::Pair as Pair>::Public>,
Joshy Orndorff's avatar
Joshy Orndorff committed
46
{
Pascal Engélibert's avatar
fmt    
Pascal Engélibert committed
47
    AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
Joshy Orndorff's avatar
Joshy Orndorff committed
48
49
}

Dan Forbes's avatar
Dan Forbes committed
50
/// Generate an Aura authority key.
Ricardo Rius's avatar
Ricardo Rius committed
51
pub fn authority_keys_from_seed(s: &str) -> (AuraId, GrandpaId) {
Pascal Engélibert's avatar
fmt    
Pascal Engélibert committed
52
    (get_from_seed::<AuraId>(s), get_from_seed::<GrandpaId>(s))
Shawn Tabrizi's avatar
Shawn Tabrizi committed
53
54
}

Dan Forbes's avatar
Dan Forbes committed
55
pub fn development_config() -> Result<ChainSpec, String> {
Pascal Engélibert's avatar
fmt    
Pascal Engélibert committed
56
    let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?;
Dan Forbes's avatar
Dan Forbes committed
57

Pascal Engélibert's avatar
fmt    
Pascal Engélibert committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    Ok(ChainSpec::from_genesis(
        // Name
        "Development",
        // ID
        "dev",
        ChainType::Development,
        move || {
            testnet_genesis(
                wasm_binary,
                // Initial PoA authorities
                vec![authority_keys_from_seed("Alice")],
                // Sudo account
                get_account_id_from_seed::<sr25519::Public>("Alice"),
                // Pre-funded accounts
                vec![
                    get_account_id_from_seed::<sr25519::Public>("Alice"),
                    get_account_id_from_seed::<sr25519::Public>("Bob"),
                    get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
                    get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
                ],
                true,
            )
        },
        // Bootnodes
        vec![],
        // Telemetry
        None,
        // Protocol ID
        None,
        // Properties
        None,
        // Extensions
        None,
    ))
Ricardo Rius's avatar
Ricardo Rius committed
92
}
Shawn Tabrizi's avatar
Shawn Tabrizi committed
93

Dan Forbes's avatar
Dan Forbes committed
94
pub fn local_testnet_config() -> Result<ChainSpec, String> {
Pascal Engélibert's avatar
fmt    
Pascal Engélibert committed
95
    let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?;
Dan Forbes's avatar
Dan Forbes committed
96

Pascal Engélibert's avatar
fmt    
Pascal Engélibert committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
    Ok(ChainSpec::from_genesis(
        // Name
        "Local Testnet",
        // ID
        "local_testnet",
        ChainType::Local,
        move || {
            testnet_genesis(
                wasm_binary,
                // Initial PoA authorities
                vec![
                    authority_keys_from_seed("Alice"),
                    authority_keys_from_seed("Bob"),
                ],
                // Sudo account
                get_account_id_from_seed::<sr25519::Public>("Alice"),
                // Pre-funded accounts
                vec![
                    get_account_id_from_seed::<sr25519::Public>("Alice"),
                    get_account_id_from_seed::<sr25519::Public>("Bob"),
                    get_account_id_from_seed::<sr25519::Public>("Charlie"),
                    get_account_id_from_seed::<sr25519::Public>("Dave"),
                    get_account_id_from_seed::<sr25519::Public>("Eve"),
                    get_account_id_from_seed::<sr25519::Public>("Ferdie"),
                    get_account_id_from_seed::<sr25519::Public>("Alice//stash"),
                    get_account_id_from_seed::<sr25519::Public>("Bob//stash"),
                    get_account_id_from_seed::<sr25519::Public>("Charlie//stash"),
                    get_account_id_from_seed::<sr25519::Public>("Dave//stash"),
                    get_account_id_from_seed::<sr25519::Public>("Eve//stash"),
                    get_account_id_from_seed::<sr25519::Public>("Ferdie//stash"),
                ],
                true,
            )
        },
        // Bootnodes
        vec![],
        // Telemetry
        None,
        // Protocol ID
        None,
        // Properties
        None,
        // Extensions
        None,
    ))
Shawn Tabrizi's avatar
Shawn Tabrizi committed
142
143
}

Dan Forbes's avatar
Dan Forbes committed
144
145
/// Configure initial storage state for FRAME modules.
fn testnet_genesis(
Pascal Engélibert's avatar
fmt    
Pascal Engélibert committed
146
147
148
149
150
    wasm_binary: &[u8],
    initial_authorities: Vec<(AuraId, GrandpaId)>,
    root_key: AccountId,
    endowed_accounts: Vec<AccountId>,
    _enable_println: bool,
Dan Forbes's avatar
Dan Forbes committed
151
) -> GenesisConfig {
Pascal Engélibert's avatar
fmt    
Pascal Engélibert committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    GenesisConfig {
        frame_system: SystemConfig {
            // Add Wasm runtime to storage.
            code: wasm_binary.to_vec(),
            changes_trie_config: Default::default(),
        },
        pallet_balances: BalancesConfig {
            // Configure endowed accounts with initial balance of 1 << 60.
            balances: endowed_accounts
                .iter()
                .cloned()
                .map(|k| (k, 1 << 60))
                .collect(),
        },
        pallet_aura: AuraConfig {
            authorities: initial_authorities.iter().map(|x| (x.0.clone())).collect(),
        },
        pallet_grandpa: GrandpaConfig {
            authorities: initial_authorities
                .iter()
                .map(|x| (x.1.clone(), 1))
                .collect(),
        },
        pallet_sudo: SudoConfig {
            // Assign network admin rights.
            key: root_key,
        },
    }
Shawn Tabrizi's avatar
Shawn Tabrizi committed
180
}