ghost-node/service/src/chain_spec.rs
2024-10-03 16:44:00 +03:00

504 lines
19 KiB
Rust
Executable File

//! Ghost chain configuration.
use codec::Encode;
use ghost_slow_clap::sr25519::AuthorityId as SlowClapId;
use primitives::{AccountId, AccountPublic};
use authority_discovery_primitives::AuthorityId as AuthorityDiscoveryId;
use grandpa_primitives::AuthorityId as GrandpaId;
use babe_primitives::AuthorityId as BabeId;
#[cfg(feature = "casper-native")]
use pallet_staking::Forcing;
#[cfg(feature = "casper-native")]
use casper_runtime as casper;
#[cfg(feature = "casper-native")]
use casper_runtime_constants::currency::CSPR;
use sc_chain_spec::ChainSpecExtension;
#[cfg(feature = "casper-native")]
use sc_chain_spec::ChainType;
use serde::{Deserialize, Serialize};
use sp_core::{sr25519, Pair, Public};
use sp_runtime::traits::IdentifyAccount;
#[cfg(feature = "casper-native")]
use sp_runtime::Perbill;
#[cfg(feature = "casper-native")]
use telemetry::TelemetryEndpoints;
#[cfg(feature = "casper-native")]
const CASPER_TELEMETRY_URL: &str = "wss://telemetry.ghostchain.io/submit/";
#[cfg(feature = "casper-native")]
const DEFAULT_PROTOCOL_ID: &str = "cspr";
/// Node `ChainSpec` extensions.
///
/// Additional parameters for some substrate core modules,
/// customizable from the chain spec.
#[derive(Default, Clone, Serialize, Deserialize, ChainSpecExtension)]
#[serde(rename_all = "camelCase")]
pub struct Extensions {
/// Block number with known hashes.
pub fork_blocks: sc_client_api::ForkBlocks<primitives::Block>,
/// Known bad block hashes.
pub bad_blocks: sc_client_api::BadBlocks<primitives::Block>,
/// The light sync state.
/// This value will be set by the `sync-state rpc` implementation.
pub light_sync_state: sc_sync_state_rpc::LightSyncStateExtension,
}
// Generic chain spec, in case when we don't have the native runtime.
pub type GenericChainSpec = sc_service::GenericChainSpec<(), Extensions>;
/// The `ChainSpec` parametrized for the ghost runtime.
#[cfg(feature = "casper-native")]
pub type CasperChainSpec = sc_service::GenericChainSpec<(), Extensions>;
#[cfg(not(feature = "casper-native"))]
pub type CasperChainSpec = GenericChainSpec;
pub fn casper_config() -> Result<CasperChainSpec, String> {
CasperChainSpec::from_json_bytes(&include_bytes!("../chain-specs/casper.json")[..])
}
#[cfg(feature = "casper-native")]
fn casper_session_keys(
babe: BabeId,
grandpa: GrandpaId,
authority_discovery: AuthorityDiscoveryId,
slow_clap: SlowClapId,
) -> casper::opaque::SessionKeys {
casper::opaque::SessionKeys {
babe,
grandpa,
authority_discovery,
slow_clap,
}
}
pub fn casper_chain_spec_properties() -> serde_json::map::Map<String, serde_json::Value> {
serde_json::json!({
"ss58Format": 1996,
"tokenDecimals": 18,
"tokenSymbol": "CSPR",
})
.as_object()
.expect("Map given; qed")
.clone()
}
/// Helper function to 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()
}
/// Helper function to generate account ID from seed.
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()
}
/// Helper function to generate stash, controller and session key from seed
pub fn generate_authority_keys_from_seed(
seed: &str,
) -> (
AccountId,
AccountId,
BabeId,
GrandpaId,
AuthorityDiscoveryId,
SlowClapId,
) {
let keys = get_authority_keys_from_seed(seed);
(keys.0, keys.1, keys.2, keys.3, keys.4, keys.5)
}
/// Helper function to generate stash, controller and session key from seed
pub fn get_authority_keys_from_seed(
seed: &str,
) -> (
AccountId,
AccountId,
BabeId,
GrandpaId,
AuthorityDiscoveryId,
SlowClapId,
) {
(
get_account_id_from_seed::<sr25519::Public>(&format!("{}//stash", seed)),
get_account_id_from_seed::<sr25519::Public>(seed),
get_from_seed::<BabeId>(seed),
get_from_seed::<GrandpaId>(seed),
get_from_seed::<AuthorityDiscoveryId>(seed),
get_from_seed::<SlowClapId>(seed),
)
}
#[cfg(feature = "casper-native")]
fn casper_testnet_accounts() -> Vec<AccountId> {
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>("Feride"),
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>("Feride//stash"),
]
}
#[cfg(feature = "casper-native")]
fn casper_testnet_evm_accounts() -> Vec<(AccountId, u128, u8)> {
vec![
// 01c928771aea942a1e7ac06adf2b73dfbc9a25d9eaa516e3673116af7f345198
(get_account_id_from_seed::<sr25519::Public>("1A69d2D5568D1878023EeB121a73d33B9116A760"), 1337 * CSPR, 1),
// b19a435901872f817185f7234a1484eae837613f9d10cf21927a23c2d8cb9139
(get_account_id_from_seed::<sr25519::Public>("2f86cfBED3fbc1eCf2989B9aE5fc019a837A9C12"), 1337 * CSPR, 2),
// d3baf57b74d65719b2dc33f5a464176022d0cc5edbca002234229f3e733875fc
(get_account_id_from_seed::<sr25519::Public>("e83f67361Ac74D42A48E2DAfb6706eb047D8218D"), 69 * CSPR, 3),
// c4683d566436af6b58b4a59c8f501319226e85b21869bf93d5eeb4596d4791d4
(get_account_id_from_seed::<sr25519::Public>("827ee4ad9b259b6fa1390ed60921508c78befd63"), 69 * CSPR, 4),
]
}
#[cfg(feature = "casper-native")]
fn casper_testnet_evm_networks() -> Vec<(u32, Vec<u8>)> {
vec![
(1, ghost_networks::NetworkData {
chain_name: "ethereum-mainnet".into(),
default_endpoint: "https://nd-422-757-666.p2pify.com/0a9d79d93fb2f4a4b1e04695da2b77a7/".into(),
finality_delay: Some(40u64),
release_delay: Some(80u64),
network_type: ghost_networks::NetworkType::Evm,
gatekeeper: "0x4d224452801aced8b2f0aebe155379bb5d594381".into(),
topic_name: "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef".into(),
incoming_fee: 0u32,
outgoing_fee: 0u32,
}.encode()),
(56, ghost_networks::NetworkData {
chain_name: "bnb-mainnet".into(),
default_endpoint: "https://bsc-mainnet.core.chainstack.com/35848e183f3e3303c8cfeacbea831cab/".into(),
finality_delay: Some(20u64),
release_delay: Some(40u64),
network_type: ghost_networks::NetworkType::Evm,
gatekeeper: "0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82".into(),
topic_name: "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef".into(),
incoming_fee: 0u32,
outgoing_fee: 0u32,
}.encode())
]
}
/// Helper function to create casper `GenesisConfig` for testing
#[cfg(feature = "casper-native")]
pub fn testnet_config_genesis(
initial_authorities: Vec<(
AccountId,
AccountId,
BabeId,
GrandpaId,
AuthorityDiscoveryId,
SlowClapId,
)>,
endowed_accounts: Option<Vec<AccountId>>,
ghost_accounts: Option<Vec<(AccountId, u128, u8)>>,
evm_networks: Option<Vec<(u32, Vec<u8>)>>,
) -> serde_json::Value {
let endowed_accounts: Vec<AccountId> = endowed_accounts
.unwrap_or_else(casper_testnet_accounts);
let ghost_accounts: Vec<(AccountId, u128, u8)> = ghost_accounts
.unwrap_or_default();
let evm_networks: Vec<(u32, Vec<u8>)> = evm_networks
.unwrap_or_default();
const ENDOWMENT: u128 = 1_000 * CSPR;
const STASH: u128 = 500 * CSPR;
serde_json::json!({
"balances": {
"balances": endowed_accounts
.iter()
.map(|k| (k.clone(), ENDOWMENT))
.chain(ghost_accounts
.iter()
.map(|k| (k.0.clone(), k.1.clone())))
.collect::<Vec<_>>(),
},
"session": {
"keys": initial_authorities
.iter()
.map(|x| {
(
x.0.clone(),
x.0.clone(),
casper_session_keys(
x.2.clone(),
x.3.clone(),
x.4.clone(),
x.5.clone(),
),
)
})
.collect::<Vec<_>>(),
},
"staking": {
"validatorCount": initial_authorities.len() as u32,
"minimumValidatorCount": 1,
"invulnerables": initial_authorities
.iter()
.map(|x| x.0.clone())
.collect::<Vec<_>>(),
"forceEra": Forcing::NotForcing,
"slashRewardFraction": Perbill::from_percent(10),
"stakers": initial_authorities
.iter()
.map(|x| (x.0.clone(), x.0.clone(), STASH, casper::StakerStatus::<AccountId>::Validator))
.collect::<Vec<_>>(),
},
"babe": {
"epochConfig": Some(casper::BABE_GENESIS_EPOCH_CONFIG),
},
"ghostNetworks": {
"networks": evm_networks,
},
"ghostClaims": {
"total": ghost_accounts.iter().fold(0, |acc, k| acc + k.1),
"membersAndRanks": ghost_accounts
.iter()
.map(|k| (k.0.clone(), k.2.clone()))
.collect::<Vec<_>>(),
},
})
}
#[cfg(feature = "casper-native")]
fn casper_staging_config_genesis() -> serde_json::Value {
use hex_literal::hex;
use sp_core::crypto::UncheckedInto;
// Following keys are used in genesis config for development chains.
// DO NOT use them in production chains as the secret seed is public.
//
// SECRET_SEED="fall cargo frown step audit cover various urge urge six pattern leisure"
// ghostkey inspect -n casper "$SECRET_SEED"
let endowed_accounts = vec![
// sfFmPT1hi3iySSwEdLpNRMJWo5pvFAKpfgjhDYAgL2i2qyEWx
hex!["5afaf4bba29ed7557a0112d9664c6c3d4acd96440f1b43f3bddeffdc2b3bc800"].into(),
];
// SECRET=$SECRET_SEED ./scripts/prepare-test-net.sh 4
let initial_authorities: Vec<(
AccountId,
AccountId,
BabeId,
GrandpaId,
AuthorityDiscoveryId,
SlowClapId,
)> = vec![
(
//sfEdnhKt7YcjUP8iuvxNqTqUu7YpbARBeqFjAPRMH6yFuuPco
hex!["28f303d3b1f821edda19bf6c97ee886edd85d7dc80e2500ab5f4888a19e40a50"].into(),
//sfJxZKUmPStRqKF1sq5dpdgeCSCx5zRVa53c4q6d8SgfUUf7W
hex!["e83494e12531b122043315d780c4b5b1153c52b7a94c42b5d61cd91b1f188e31"].into(),
//sfFJMwRUaHrHPFL48SqQbvr39HFYvhgX4YKRmrN61i5vqmxnJ
hex!["465e3f1220c774ba014defd7273c3a6e57336eb87c2bea9f04419c2eb6ee895c"].unchecked_into(),
//sfEpnU74Gr7bRfTBR7pEKZdxx6SRXmw55atd9x4uJSWRJMGby
hex!["3155f271b9ca868a2eb980e35f17ee13525cb59e90848c1b0cc2ff306956c916"].unchecked_into(),
//sfEM1Bj59N9gnscE7t6fqBF1xvR4wvhmrxfviCQYDfrTY9dvJ
hex!["1c25e311eade248d63ff2103d3a0bd8909ad618e5878b423c99bd043b47fd535"].unchecked_into(),
//sfFyrauZLXLR44mBB58i73ZTnsgSHBjbgGxtXwoDTH751LaHE
hex!["647d3fc5c00e3dd975838a92a72e06edc6d060c2e79e23672425da61812a8011"].unchecked_into(),
),
(
//sfJEs3nDW3aemaCWeSjeuFtiGR3Zx5nD3uMAPvLoRkATdUicY
hex!["c867f4f1417b0aa0c1cf9805866ea3e75d810a47a1ee8d302477768d95306542"].into(),
//sfHf6BwnPaRkMqLivmsTrHGWPQ1DsPM5pVzaeodUvpUGDEuhT
hex!["aea5d94e3837022ddd3b006d77d38b70b87ebaa5423b358b871a3e05d9157e77"].into(),
//sfDzomQME6PRxB5LTEJsLYz7wxJUNNAnMop88Uj4E3wWVyu9x
hex!["0cbe89b4ea4fd1d618f9d0db15522f2ca335893abb806a638c3e9bc43f022c78"].unchecked_into(),
//sfK6CJTmSa5cGk51KFibhvbm1di6EPBnefGeCzXcprUQVokeV
hex!["ee07cb1fd2fbc6b079ec3abb6c4866a35a3f1a25aef0bd5e50d8c188a98148c9"].unchecked_into(),
//sfK9Kq2pXUW5JZBMYSFRwKCWZoMEzqkq8wRVTtpo6zxdDENqG
hex!["f06ade37706f898adce54b09f7e973e718b20fbec06eebf34e6d23046aa51d63"].unchecked_into(),
//sfEBQtB12x7xi5RPGXJiLWugdwAdDBsYePwssMLFBtr43dZiC
hex!["14d534940143dc260894c7ebfd013a0d5c65c2cd85ae89decb32d663a2628e66"].unchecked_into(),
),
(
//sfF4sfGLNyVtYMYZbte6A6kcncFi4hG65vPpbftQZxcBVbavS
hex!["3c14dca7697cd193184708b00abf3c148c2d78eae6d8c102b84683ec6936123b"].into(),
//sfJUHCESTjHwXKmHbzhApCDRgzUTcP5kcUUCATeBaTUwtyx8X
hex!["d2a36dc75ef7933c2934b0855e2568a85c11347a48f0a1bfbcf1fc3757db1a07"].into(),
//sfGS5ypMSqG3VSWyhKghZzCxSUdp4JC3Qaw3eu2oe8iaSy1TM
hex!["787eb8a2780f422382474cfa7e44493fffd86824963a0da22d0f0c82f9d39860"].unchecked_into(),
//sfFH1EKd6KCqCJ9HPPW5oBr5gigsEKBkHtFT4GoBqu5iUdYzZ
hex!["45554a1b748fed1f2f51f06970c8cd764edbdefd82d697993ddfe7f97290918c"].unchecked_into(),
//sfG28QRqEgkAgvpjZc3N86xDTebiwKiSvKYfg1NdwW2GNDbP6
hex!["6638fe19b4331656dacf3be9d6a81506fa71db1fbe4bd9db0ab8c9cc0e364f1b"].unchecked_into(),
//sfFUfGqByAJGfe3e4vxiDJv5A1v1dp2piSNGpR8Kz5U9dFvMp
hex!["4e390dd2dad59ff8709e0cb714bfccdbadde8b4e91d5931cb98ba6e6eb48a93b"].unchecked_into(),
),
(
//sfG7zdnf5t6JHbi9tBfkKesz13x7B9mSiYwDJfnDQ1LQwXwAi
hex!["6ab24f598a38e1d91004487faedadc8c338a7cb2a07e29899eccc7d106a2195e"].into(),
//sfDu1voz5vmiKXeiCHutmCp96C6LqLogsWE4rkaUZeZyhdt1B
hex!["085401d14ff3c620c76347a9e9a7e020fd3575d081c8352728db56ea6cc52677"].into(),
//sfH9hH4P98SohVs3Aj4cBoDXZvgYbF8dPXi3NfyEng5eMry9G
hex!["983a9aa148f94a54f313aca8537fb00f3c3353f70fafa8c14d4de22b44366f3c"].unchecked_into(),
//sfJQChmUKaDDQgzsAyZvuRNQZxV4CQ2qmxzmZk27tdWczKtRa
hex!["cf875474e8dfe94f4e9e0e2a4743e02c18cebf87b2c763710c9630f3fc94c9c4"].unchecked_into(),
//sfEMDH3w5xvZCwhXaMKYV7hsSL36evqHhqianntoiNDEtewLT
hex!["1c4e97776053b47b5b48035bab5f5f4afb3e6e13b15d1964195502cf40124c2a"].unchecked_into(),
//sfEPb5wWLKAHZH8k8KXpBtfnbBQPBBTkyv2ZQ1YP3GRKKpP1w
hex!["1e1e7f0781828178bd1237d9a8c2e057bbb15f2f46d1fc5fb316ebd9f5d1ad04"].unchecked_into(),
),
];
let ghost_accounts: Vec<(AccountId, u128, u8)> = casper_testnet_evm_accounts();
let evm_networks = casper_testnet_evm_networks();
const ENDOWMENT: u128 = 5_000 * CSPR;
const STASH: u128 = 500 * CSPR;
serde_json::json!({
"balances": {
"balances": endowed_accounts
.iter()
.map(|k: &AccountId| (k.clone(), ENDOWMENT))
.chain(initial_authorities.iter().map(|x| (x.0.clone(), STASH)))
.collect::<Vec<_>>(),
},
"session": {
"keys": initial_authorities
.iter()
.map(|x| {
(
x.0.clone(),
x.0.clone(),
casper_session_keys(
x.2.clone(),
x.3.clone(),
x.4.clone(),
x.5.clone(),
),
)
})
.collect::<Vec<_>>(),
},
"staking": {
"validatorCount": 50,
"minimumValidatorCount": 4,
"stakers": initial_authorities
.iter()
.map(|x| {
(
x.0.clone(),
x.0.clone(),
STASH,
casper::StakerStatus::<AccountId>::Validator,
)
})
.collect::<Vec<_>>(),
"invulnerables": initial_authorities.iter().map(|x| x.0.clone()).collect::<Vec<_>>(),
"forceEra": Forcing::ForceNone,
"slashRewardFraction": Perbill::from_percent(10)
},
"babe": {
"epochConfig": Some(casper::BABE_GENESIS_EPOCH_CONFIG),
},
"ghostNetworks": {
"networks": evm_networks,
},
"ghostClaims": {
"total": ghost_accounts
.iter()
.fold(0, |acc, k| acc + k.1),
"membersAndRanks": ghost_accounts
.iter()
.map(|k| (k.0.clone(), k.2.clone()))
.collect::<Vec<_>>(),
},
})
}
#[cfg(feature = "casper-native")]
fn casper_development_config_genesis() -> serde_json::Value {
testnet_config_genesis(
vec![get_authority_keys_from_seed("Alice")],
None, None, None,
)
}
#[cfg(feature = "casper-native")]
fn casper_local_config_genesis() -> serde_json::Value {
testnet_config_genesis(
vec![
get_authority_keys_from_seed("Alice"),
get_authority_keys_from_seed("Bob"),
],
Some(casper_testnet_accounts()),
Some(casper_testnet_evm_accounts()),
Some(casper_testnet_evm_networks()),
)
}
#[cfg(feature = "casper-native")]
pub fn casper_development_config() -> Result<CasperChainSpec, String> {
Ok(CasperChainSpec::builder(
casper::WASM_BINARY.ok_or("Casper development wasm not available")?,
Default::default(),
)
.with_name("Development")
.with_id("casper_dev")
.with_chain_type(ChainType::Development)
.with_genesis_config_patch(casper_development_config_genesis())
.with_protocol_id(DEFAULT_PROTOCOL_ID)
.with_properties(casper_chain_spec_properties())
.build())
}
#[cfg(feature = "casper-native")]
pub fn casper_local_testnet_config() -> Result<CasperChainSpec, String> {
Ok(CasperChainSpec::builder(
casper::WASM_BINARY.ok_or("Casper local testnet wasm not available")?,
Default::default(),
)
.with_name("Casper Local Testnet")
.with_id("casper_local_testnet")
.with_chain_type(ChainType::Local)
.with_genesis_config_patch(casper_local_config_genesis())
.with_protocol_id(DEFAULT_PROTOCOL_ID)
.with_properties(casper_chain_spec_properties())
.build())
}
#[cfg(feature = "casper-native")]
pub fn casper_staging_testnet_config() -> Result<CasperChainSpec, String> {
Ok(CasperChainSpec::builder(
casper::WASM_BINARY.ok_or("Casper staging testnet wasm not available")?,
Default::default(),
)
.with_name("Casper Staging Testnet")
.with_id("casper_staging_testnet")
.with_chain_type(ChainType::Live)
.with_genesis_config_patch(casper_staging_config_genesis())
.with_telemetry_endpoints(
TelemetryEndpoints::new(vec![(CASPER_TELEMETRY_URL.to_string(), 0)])
.expect("Casper Staging telemetry url is valid; qed"),
)
.with_protocol_id(DEFAULT_PROTOCOL_ID)
.with_properties(casper_chain_spec_properties())
.build())
}