1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
use std::{collections::BTreeMap, str::FromStr};
use clap::Args;
use fp_evm::GenesisAccount;
use phronesis_runtime::{
    AccountId, BalancesConfig, RuntimeGenesisConfig, Signature, EVMConfig, SudoConfig, SystemConfig, WASM_BINARY,
    phronesis::PHRON, Balance, SessionConfig, SessionKeys, FinalityVersion, PhronConfig, StakingConfig,
    ElectionsConfig, StakerStatus, CommitteeManagementConfig
};
use core_primitives::{AuthorityId as PhronId, LEGACY_FINALITY_VERSION, SessionValidators};
use sc_service::ChainType;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_core::{Pair, Public, H160, U256};
use sp_runtime::traits::{IdentifyAccount};
use hex_literal::hex;
use sc_cli::Error as CliError;

pub const DEFAULT_BACKUP_FOLDER: &str = "backup-phron";
pub const CHAINTYPE_DEV: &str = "dev";
pub const CHAINTYPE_LOCAL: &str = "local";
pub const CHAINTYPE_LIVE: &str = "live";
pub const DEFAULT_CHAIN_ID: &str = "phron01";
pub const DEFAULT_SUDO_ACCOUNT: &str = "0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac";

fn parse_chaintype(s: &str) -> Result<ChainType, CliError> {
    Ok(match s {
        CHAINTYPE_DEV => ChainType::Development,
        CHAINTYPE_LOCAL => ChainType::Local,
        CHAINTYPE_LIVE => ChainType::Live,
        s => panic!("Wrong chain type {s} Possible values: dev local live"),
    })
}

fn parse_account_id(s: &str) -> Result<AccountId, CliError> {
    let clean_hex = if let Some(stripped_account) = s.strip_prefix("0x") { stripped_account } else { s };

    let bytes = match hex::decode(clean_hex) {
        Ok(data) => data,
        Err(err) => panic!("Failed to decode hex string: {err}"),
    };
    Ok(AccountId::from(bytes))
}
#[derive(Debug, Clone)]
pub struct SerializablePeerId {
    peer_id: libp2p::PeerId,
}

impl SerializablePeerId {
    pub fn new(peer_id: libp2p::PeerId) -> Self {
        Self { peer_id }
    }
}

impl serde::Serialize for SerializablePeerId {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let s: String = format!("{}", self.peer_id);
        serializer.serialize_str(&s)
    }
}

impl<'de> serde::Deserialize<'de> for SerializablePeerId {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let s: String = serde::Deserialize::deserialize(deserializer)?;
        let peer_id = libp2p::PeerId::from_str(&s).map_err(
            |_| serde::de::Error::custom(format!("Could not deserialize as peer id: {s}")))?;
        Ok(Self::new(peer_id))
    }
}

#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
pub struct AuthorityKeys {
    pub account_id: AccountId,
    pub aura_id: AuraId,
    pub phron_id: PhronId,
    pub peer_id: SerializablePeerId,
}

// fn generate_peer_id() -> SerializablePeerId {
//     let keypair = libp2p_ed25519::Keypair::generate();
//     SerializablePeerId::new(PublicKey::from(keypair.public()).to_peer_id())
// }
fn to_account_ids(keys: &[AuthorityKeys]) -> impl Iterator<Item=AccountId> + '_ {
    keys.iter().map(|keys| keys.account_id)
}

fn unique_account_ids(keys: Vec<AccountId>) -> Vec<AccountId> {
    let set: std::collections::HashSet<_> = keys.into_iter().collect();
    set.into_iter().collect()
}

const ENDOWMENT: Balance = 1_000_000_000 * PHRON;
const STASH: Balance = ENDOWMENT / 1000;

fn calculate_endowment(keys: &[AccountId]) -> u128 {
    ENDOWMENT / (keys.len() as u128)
}

// The URL for the telemetry server.
const TELEMETRY_URL: &str = "wss://telemetry.polkadot.io/submit/";

/// Specialized `ChainSpec`. This is a specialization of the general Substrate ChainSpec type.
pub type ChainSpec = sc_service::GenericChainSpec<RuntimeGenesisConfig>;

/// Generate a crypto pair from seed.
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
    TPublic::Pair::from_string(&format!("//{}", seed), None)
        .expect("static values are valid; qed")
        .public()
}

#[allow(dead_code)]
type AccountPublic = <Signature as sp_runtime::traits::Verify>::Signer;

pub fn chainspec_properties() -> sc_service::Properties {
    let mut properties = sc_service::Properties::new();
    properties.insert("tokenDecimals".into(), 18.into());
    properties.insert("tokenSymbol".into(), "PHR".into());
    properties
}


/// Generate an account ID from seed.
/// For use with `AccountId32`, `dead_code` if `AccountId20`.
#[allow(dead_code)]
pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId
    where
        AccountPublic: From<<TPublic::Pair as Pair>::Public>,
{
    AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
}

#[derive(Debug, Args, Clone)]
pub struct ChainParams {
    /// Chain ID is a short identifier of the chain
    #[arg(long, value_name = "ID", default_value = DEFAULT_CHAIN_ID)]
    chain_id: String,

    /// The type of the chain. Possible values: "dev", "local", "live" (default)
    #[arg(long, value_name = "TYPE", value_parser = parse_chaintype, default_value = CHAINTYPE_DEV)]
    chain_type: ChainType,

    /// Chain name. Default is "Phronesis"
    #[arg(long, default_value = "Phronesis")]
    chain_name: String,

    /// AccountIds of authorities forming the committee at the genesis (comma delimited)
    #[arg(long, value_delimiter = ',', value_parser = parse_account_id, num_args=1..)]
    account_ids: Vec<AccountId>,

    /// AccountId of the sudo account
    #[arg(long, default_value(DEFAULT_SUDO_ACCOUNT), value_parser = parse_account_id)]
    sudo_account_id: AccountId,

    /// AccountIds of the optional rich accounts
    #[arg(long, value_delimiter = ',', num_args=1.., value_parser = parse_account_id)]
    rich_account_ids: Option<Vec<AccountId>>,

    /// Finality version at chain inception.
    #[arg(long, default_value = LEGACY_FINALITY_VERSION.to_string())]
    finality_version: FinalityVersion,
}

impl ChainParams {
    pub fn chain_id(&self) -> &str {
        &self.chain_id
    }

    pub fn chain_type(&self) -> ChainType {
        self.chain_type.clone()
    }

    pub fn chain_name(&self) -> &str {
        &self.chain_name
    }

    pub fn account_ids(&self) -> Vec<AccountId> {
        self.account_ids.clone()
    }

    pub fn sudo_account_id(&self) -> AccountId {
        self.sudo_account_id
    }

    pub fn rich_account_ids(&self) -> Option<Vec<AccountId>> {
        self.rich_account_ids.clone()
    }

    pub fn finality_version(&self) -> FinalityVersion {
        self.finality_version
    }
}

pub fn build_chain_spec(
    chain_params: ChainParams,
    authority_keys: Vec<AuthorityKeys>
) -> Result<ChainSpec, String> {
    let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?;
    let chain_name = chain_params.chain_name();
    let chain_id = chain_params.chain_id();
    let chain_type = chain_params.chain_type();
    let sudo_account_id = chain_params.sudo_account_id();
    let account_ids = chain_params.account_ids();
    let finality_version = chain_params.finality_version();


    Ok(ChainSpec::from_genesis(
        // Name
        chain_name,
        // ID
        chain_id,
        chain_type,
        move || {
            generate_genesis_config(
                wasm_binary,
                // Initial PoA authorities
                authority_keys.clone(),
                // Sudo account
                sudo_account_id,
                // Nominators
                vec![],
                // Pre-funded accounts
                account_ids.clone(),
                finality_version,
            )
        },
        // Bootnodes
        vec![],
        // Telemetry
        Some(sc_telemetry::TelemetryEndpoints::new(vec![(TELEMETRY_URL.into(), 1u8)]).unwrap()),
        // Protocol ID
        None,
        // Properties
        None,
        Some(chainspec_properties()),
        // Extensions
        None,
    ))
}

/// Configure initial storage state for FRAME modules.
fn generate_genesis_config(
    wasm_binary: &[u8],
    initial_authorities: Vec<AuthorityKeys>,
    root_key: AccountId,
    _initial_nominators: Vec<AccountId>,
    endowed_accounts: Vec<AccountId>,
    finality_version: FinalityVersion,
) -> RuntimeGenesisConfig {
    let unique_accounts = unique_account_ids(
        to_account_ids(&initial_authorities)
            .chain(endowed_accounts.clone())
            .collect::<Vec<_>>());
    let initial_balances = unique_accounts
        .into_iter()
        .map(|account_id| (account_id, calculate_endowment(&endowed_accounts)))
        .collect::<Vec<_>>();

    let committee_members: Vec<AccountId> = to_account_ids(&initial_authorities).collect();

    // stakers: all validators and nominators.
    let stakers = initial_authorities
        .iter()
        .enumerate()
        .map(|(validator_idx, validator)| {
            (
                validator.account_id,
                validator.account_id,
                (validator_idx + 1) as u128 * STASH,
                StakerStatus::<AccountId>::Validator,
            )
        }).collect();
    RuntimeGenesisConfig {
        system: SystemConfig {
            // Add Wasm runtime to storage.
            code: wasm_binary.to_vec(),
            ..Default::default()
        },
        balances: BalancesConfig {
            balances: initial_balances
        },
        aura: Default::default(),
        phron: PhronConfig {
            finality_version,
            ..Default::default()
        },
        sudo: SudoConfig {
            // Assign network admin rights.
            key: Some(root_key),
        },
        transaction_payment: Default::default(),
        session: SessionConfig {
            keys: initial_authorities
                .iter()
                .map(|keys| {
                    (
                        keys.account_id,
                        keys.account_id,
                        SessionKeys {
                            aura: keys.aura_id.clone(),
                            phron: keys.phron_id.clone(),
                        },
                    )
                }).collect(),
        },
        evm: EVMConfig {
            accounts: {
                let mut accounts = BTreeMap::new();
                accounts.insert(
                    H160::from_slice(&hex!("C8742A5fBb4e5db67c50d326Ee7F7b846A842642")),
                    GenesisAccount {
                        nonce: U256::zero(),
                        // Using a larger number, so I can tell the accounts apart by balance.
                        balance: U256::from_str("0xffffffffffffffffffffffffffffffff")
                            .expect("internal U256 is valid; qed"),
                        code: vec![],
                        storage: BTreeMap::new(),
                    },
                );
                accounts
            },
            ..Default::default()
        },
        ethereum: Default::default(),
        base_fee: Default::default(),
        dynamic_fee: Default::default(),

        staking: StakingConfig {
            validator_count: initial_authorities.len() as u32,
            minimum_validator_count: initial_authorities.len() as u32,
            invulnerables: initial_authorities.iter().map(|keys| keys.account_id).collect(),
            slash_reward_fraction: sp_runtime::Perbill::from_percent(10),
            stakers,
            ..Default::default()
        },
        elections: ElectionsConfig {
            non_reserved_validators: vec![],
            reserved_validators: committee_members.clone(),
            committee_seats: Default::default(),
        },
        treasury: Default::default(),
        committee_management: CommitteeManagementConfig {
            committee_ban_config: Default::default(),
            session_validators: SessionValidators {
                committee: committee_members,
                non_committee: vec![],
            },
        }
    }
}

pub fn devnet_config() -> Result<ChainSpec, String> {
    ChainSpec::from_json_bytes(crate::chain_resources::devnet_chainspec())
}

pub fn local_config() -> Result<ChainSpec, String> {
    ChainSpec::from_json_bytes(crate::chain_resources::local_chainspec())
}

pub fn mainnet_config() -> Result<ChainSpec, String> {
    ChainSpec::from_json_bytes(crate::chain_resources::mainnet_chainspec())
}