Newer
Older
// This file is part of Duniter-v2S.
// Duniter-v2S 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, version 3 of the License.
// Duniter-v2S 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 Duniter-v2S. If not, see <https://www.gnu.org/licenses/>.
use super::*;
use crate::chain_spec::gen_genesis_data::{
AuthorityKeys, CommonParameters, GenesisIdentity, SessionKeysProvider,
};
use common_runtime::{constants::*, entities::IdtyData, GenesisIdty, IdtyStatus};
use g1_runtime::{opaque::SessionKeys, parameters, WASM_BINARY};
use sc_service::ChainType;
use serde::Deserialize;
use sp_core::sr25519;
use std::{env, fs};
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
92
93
94
95
96
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
142
143
144
145
146
147
148
149
150
151
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
pub type ChainSpec = sc_service::GenericChainSpec;
#[derive(Default, Clone, Deserialize)]
// No parameters for GTest (unlike GDev)
struct GenesisParameters {}
const TOKEN_DECIMALS: usize = 2;
const TOKEN_SYMBOL: &str = "Ğ";
static EXISTENTIAL_DEPOSIT: u64 = parameters::ExistentialDeposit::get();
struct G1SKP;
impl SessionKeysProvider<SessionKeys> for G1SKP {
fn session_keys(keys: &AuthorityKeys) -> SessionKeys {
let cloned = keys.clone();
SessionKeys {
grandpa: cloned.1,
babe: cloned.2,
im_online: cloned.3,
authority_discovery: cloned.4,
}
}
}
fn get_parameters(_parameters_from_file: &Option<GenesisParameters>) -> CommonParameters {
CommonParameters::default()
}
/// generate local network chainspects
pub fn local_testnet_config(
initial_authorities_len: usize,
initial_smiths_len: usize,
initial_identities_len: usize,
) -> Result<ChainSpec, String> {
Ok(ChainSpec::builder(
&get_wasm_binary().ok_or_else(|| "Development wasm not available".to_string())?,
None,
)
.with_name("Ğ1 Local Testnet")
.with_id("g1_local")
.with_chain_type(ChainType::Local)
.with_genesis_config_patch({
let genesis_data =
gen_genesis_data::generate_genesis_data_for_local_chain::<_, _, SessionKeys, G1SKP>(
// Initial authorities len
initial_authorities_len,
// Initial smiths len,
initial_smiths_len,
// Initial identities len
initial_identities_len,
EXISTENTIAL_DEPOSIT,
None,
// Sudo account
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_parameters,
)
.expect("Genesis Data must be buildable");
genesis_data_to_g1_genesis_conf(genesis_data)
})
.with_properties(
serde_json::json!({
"tokenDecimals": TOKEN_DECIMALS,
"tokenSymbol": TOKEN_SYMBOL,
})
.as_object()
.expect("must be a map")
.clone(),
)
.build())
}
/// custom genesis
fn genesis_data_to_g1_genesis_conf(
genesis_data: super::gen_genesis_data::GenesisData<GenesisParameters, SessionKeys>,
) -> serde_json::Value {
let super::gen_genesis_data::GenesisData {
accounts,
treasury_balance,
certs_by_receiver,
first_ud,
first_ud_reeval,
identities,
initial_authorities,
initial_monetary_mass,
memberships,
parameters: _,
common_parameters: _,
session_keys_map,
initial_smiths,
sudo_key,
technical_committee_members,
ud,
} = genesis_data;
serde_json::json!({
"account": {
"accounts": accounts,
"treasuryBalance": treasury_balance,
},
"authorityMembers": {
"initialAuthorities": initial_authorities,
},
"balances": {
"totalIssuance": initial_monetary_mass,
},
"babe": {
"epochConfig": Some(BABE_GENESIS_EPOCH_CONFIG),
},
"session": {
"keys": session_keys_map
.into_iter()
.map(|(account_id, session_keys)| (account_id.clone(), account_id, session_keys))
.collect::<Vec<_>>(),
},
"sudo": { "key": sudo_key },
"technicalCommittee": {
"members": technical_committee_members,
},
"quota": {
"identities": identities.iter().map(|i| i.idty_index).collect::<Vec<_>>(),
},
"identity": {
"identities": identities
.into_iter()
.map(
|GenesisIdentity {
idty_index,
name,
owner_key,
status,
expires_on,
revokes_on,
}| GenesisIdty {
index: idty_index,
name: common_runtime::IdtyName::from(name.as_str()),
value: common_runtime::IdtyValue {
data: IdtyData::new(),
next_creatable_identity_on: 0,
old_owner_key: None,
owner_key,
next_scheduled: match status {
IdtyStatus::Unconfirmed | IdtyStatus::Unvalidated => {
panic!("Unconfirmed or Unvalidated identity in genesis")
}
IdtyStatus::Member => expires_on.expect("must have expires_on set"),
IdtyStatus::Revoked => 0,
IdtyStatus::NotMember => {
revokes_on.expect("must have revokes_on set")
}
},
status,
},
},
)
.collect::<Vec<GenesisIdty<g1_runtime::Runtime>>>(),
},
"certification": {
"applyCertPeriodAtGenesis": false,
"certsByReceiver": certs_by_receiver,
},
"membership": { "memberships": memberships },
"smithMembers": { "initialSmiths": initial_smiths},
"universalDividend": {
"firstReeval": first_ud_reeval,
"firstUd": first_ud,
"initialMonetaryMass": initial_monetary_mass,
"ud": ud,
},
})
}
/// Get the WASM bytes either from filesytem (`WASM_FILE` env variable giving the path to the wasm blob)
/// or else get the one compiled from source code.
/// Goal: allow to provide the WASM built with srtool, which is reproductible.
fn get_wasm_binary() -> Option<Vec<u8>> {
let wasm_bytes_from_file = if let Ok(file_path) = env::var("WASM_FILE") {
Some(fs::read(file_path).unwrap_or_else(|e| panic!("Could not read wasm file: {}", e)))
} else {
None
};
wasm_bytes_from_file.or_else(|| WASM_BINARY.map(|bytes| bytes.to_vec()))