Skip to content

Commit

Permalink
feat: overtrue testnet chain spec
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkingLee committed Jan 23, 2024
1 parent ca4822e commit 3405ccb
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 73 deletions.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: run-dev build-release build-default build-meloxt build-light purge-dev init test e2e run-light run-light-e2e bs build-farmer run-farmer
.PHONY: run-dev build-release build-default build-meloxt build-light purge-dev init test e2e run-light run-light-e2e bs build-farmer run-farmer run-overtrue build-node

run-light-dev: init
./target/release/melodot-light --dev-mode
Expand All @@ -9,6 +9,9 @@ run-light-e2e: init
run-dev:
./target/release/melodot-node --dev --ws-external

run-overtrue:
./target/release/melodot-node --overtrue --ws-external

run-farmer:
./target/release/melodot-farmer

Expand All @@ -18,6 +21,9 @@ build-release:
build-default:
cargo build --release -p melodot-node -p melodot-runtime

build-node:
cargo build --release -p melodot-node

build-meloxt:
cargo build --release -p meloxt

Expand Down
153 changes: 81 additions & 72 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,7 @@ pub type ChainSpec = sc_service::GenericChainSpec<GenesisConfig, Extensions>;

/// Helper function to create GenesisConfig for testing
pub fn testnet_genesis(
initial_authorities: Vec<(
AccountId,
AccountId,
GrandpaId,
BabeId,
ImOnlineId,
AuthorityDiscoveryId,
)>,
initial_authorities: Vec<AuthorityKeys>,
initial_nominators: Vec<AccountId>,
root_key: AccountId,
endowed_accounts: Option<Vec<AccountId>>,
Expand All @@ -66,7 +59,7 @@ pub fn testnet_genesis(
// endow all authorities and nominators.
initial_authorities
.iter()
.map(|x| &x.0)
.map(|x| &x.controller)
.chain(initial_nominators.iter())
.for_each(|x| {
if !endowed_accounts.contains(x) {
Expand All @@ -78,15 +71,15 @@ pub fn testnet_genesis(
let mut rng = rand::thread_rng();
let stakers = initial_authorities
.iter()
.map(|x| (x.0.clone(), x.1.clone(), STASH, StakerStatus::Validator))
.map(|x| (x.stash.clone(), x.controller.clone(), STASH, StakerStatus::Validator))
.chain(initial_nominators.iter().map(|x| {
use rand::{seq::SliceRandom, Rng};
let limit = (MaxNominations::get() as usize).min(initial_authorities.len());
let count = rng.gen::<usize>() % limit;
let nominations = initial_authorities
.as_slice()
.choose_multiple(&mut rng, count)
.map(|choice| choice.0.clone())
.map(|choice| choice.stash.clone())
.collect::<Vec<_>>();
(x.clone(), x.clone(), STASH, StakerStatus::Nominator(nominations))
}))
Expand All @@ -103,19 +96,13 @@ pub fn testnet_genesis(
session: SessionConfig {
keys: initial_authorities
.iter()
.map(|x| {
(
x.0.clone(),
x.0.clone(),
session_keys(x.2.clone(), x.3.clone(), x.4.clone(), x.5.clone()),
)
})
.map(|x| (x.stash.clone(), x.stash.clone(), x.session_keys.clone()))
.collect::<Vec<_>>(),
},
staking: StakingConfig {
validator_count: initial_authorities.len() as u32,
minimum_validator_count: initial_authorities.len() as u32,
invulnerables: initial_authorities.iter().map(|x| x.0.clone()).collect(),
invulnerables: initial_authorities.iter().map(|x| x.stash.clone()).collect(),
slash_reward_fraction: Perbill::from_percent(10),
stakers,
..Default::default()
Expand Down Expand Up @@ -183,7 +170,12 @@ pub fn development_config() -> ChainSpec {
}

fn local_testnet_genesis() -> GenesisConfig {
testnet_genesis(authority_keys_from_seeds(&[ALICE, BOB]), vec![], sr25519_id(ALICE), None)
testnet_genesis(
authority_keys_from_seeds(&[ALICE, BOB, CHARLIE]),
vec![],
sr25519_id(ALICE),
None,
)
}

/// Local testnet config (multivalidator Alice + Bob)
Expand All @@ -203,37 +195,39 @@ pub fn local_testnet_config() -> ChainSpec {
}

fn overtrue_testnet_genesis() -> GenesisConfig {
// ./melodot-node key inspect "$secret"//$j//$i//stash;
// ./melodot-node key inspect "$secret"//$j//$i;
// ./melodot-node key inspect --scheme ed25519 "$secret"//$j//$i;
// ./melodot-node key inspect "$secret"//$j//$i;
let initial_authorities: Vec<(
AccountId,
AccountId,
GrandpaId,
BabeId,
ImOnlineId,
AuthorityDiscoveryId,
)> = vec![(
// 5DhXG4yusZ98m7Tfm3doqvasUD2en6omxtLPXsW2hdS4rCcK
hex!["4850a46ca0afedc00ad8239f903fe0348fc7f2dfb6af5100ef85f1d934871b1c"].into(),
// 5DoUdqyEZsgwsQ1YprLKDSGupMN1PVjTcgvP9jqHpTt5Mb2j
hex!["4cdb4797d1d237f7e7ea5207140a290bd16cc5df23a9dcfe7c6841e4a41a0358"].into(),
// 5DueQE9NG2GeRL4VQY5VpbyRweje5J6Wtiyre7vgvX6icMfL
hex!["518f9fedc6b65cee2bfbe750e8fd92b24266e1b624c6359730e63081233ebcd3"].unchecked_into(),
// 5FCRAaNfM8wuYg6jQBHryeCXZSG68eLvHLdAYA6eWxukrbsG
hex!["8a9679d0624a555c9c1088578b0c488b03c5150fe6c021f4968d338a6ebfb24b"].unchecked_into(),
// 5FCRAaNfM8wuYg6jQBHryeCXZSG68eLvHLdAYA6eWxukrbsG
hex!["8a9679d0624a555c9c1088578b0c488b03c5150fe6c021f4968d338a6ebfb24b"].unchecked_into(),
// 5FCRAaNfM8wuYg6jQBHryeCXZSG68eLvHLdAYA6eWxukrbsG
hex!["8a9679d0624a555c9c1088578b0c488b03c5150fe6c021f4968d338a6ebfb24b"].unchecked_into(),
)];
let initial_authorities = vec![
AuthorityKeys::from_accounts(
// 5DhXG4yusZ98m7Tfm3doqvasUD2en6omxtLPXsW2hdS4rCcK
hex!["4850a46ca0afedc00ad8239f903fe0348fc7f2dfb6af5100ef85f1d934871b1c"].into(),
// 5DueQE9NG2GeRL4VQY5VpbyRweje5J6Wtiyre7vgvX6icMfL
hex!["518f9fedc6b65cee2bfbe750e8fd92b24266e1b624c6359730e63081233ebcd3"]
.unchecked_into(),
None,
),
AuthorityKeys::from_accounts(
// 5DUy3z85ad9s6qNZ16NjyeBRJzU1nfkGpUpxJoBKWzgcAr1W
hex!["3ebd4644efe640497ffa8d6421bdf4f4d297b9e725a8bba08d61edad809bb871"].into(),
// 5Hjyt9vrQ6Ljh1Th9tu1TMoR7U2vxa6FxSYgQ256jwQzYa1u
hex!["fb21af619fe27e9963afe208f04d57d141624170a141a8fae2ada560941f44d5"]
.unchecked_into(),
None,
),
AuthorityKeys::from_accounts(
// 5Gn5QQ5KaM98zgu4aDh1hZawpTg2wDfEDT7ope77MtYaBVwK
hex!["d07e877f36ba61175dc769463fc131354338c382c95b8d6e9e36252ca05c6f79"].into(),
// 5HZGUdqVinXiFQyTL6bFDuLqUhPyVnEE9iGonGHeGP8GdHDc
hex!["f2f5d903a18828e3d4cb2dd1da7882900dbc9825eade91dcf61bcd1e25b0b817"]
.unchecked_into(),
None,
),
];

testnet_genesis(
initial_authorities,
vec![],
// 5F1NhF2fdsvBwFxAhQga9WjB344ECAJ8hDQJ99p1gWqpuzY1
hex!["822a75157ca69ee2ef0c04d9b7c45e5532edc87a4bc642ab531155df665b7266"].into(),
Some(vec![hex!["822a75157ca69ee2ef0c04d9b7c45e5532edc87a4bc642ab531155df665b7266"].into()]),
hex!["4850a46ca0afedc00ad8239f903fe0348fc7f2dfb6af5100ef85f1d934871b1c"].into(),
Some(vec![]),
)
}

Expand All @@ -255,13 +249,44 @@ pub fn overtrue_testnet_config() -> ChainSpec {
mod helper {
use super::*;

pub fn session_keys(
grandpa: GrandpaId,
babe: BabeId,
im_online: ImOnlineId,
authority_discovery: AuthorityDiscoveryId,
) -> SessionKeys {
SessionKeys { grandpa, babe, im_online, authority_discovery }
#[derive(Clone)]
pub struct AuthorityKeys {
pub controller: AccountId,
pub stash: AccountId,
pub session_keys: SessionKeys,
}

impl AuthorityKeys {
/// Helper function to generate stash, controller and session key from seed
pub fn from_seed(seed: &str) -> Self {
let controller = account_id_from_seed::<sr25519::Public>(seed);
let stash = account_id_from_seed::<sr25519::Public>(&format!("{}//stash", seed));
let session_keys = SessionKeys {
babe: get_from_seed::<BabeId>(seed),
grandpa: get_from_seed::<GrandpaId>(seed),
im_online: get_from_seed::<ImOnlineId>(seed),
authority_discovery: get_from_seed::<AuthorityDiscoveryId>(seed),
};

Self { controller, stash, session_keys }
}

pub fn from_accounts(
controller: AccountId,
grandpa: GrandpaId,
stash: Option<AccountId>,
) -> Self {
let raw: [u8; 32] = controller.clone().into();
let stash = stash.unwrap_or_else(|| controller.clone());
let session_keys = SessionKeys {
babe: raw.unchecked_into(),
grandpa,
im_online: raw.unchecked_into(),
authority_discovery: raw.unchecked_into(),
};

Self { controller, stash, session_keys }
}
}

// Constants for default seeds and values
Expand Down Expand Up @@ -308,23 +333,7 @@ mod helper {
.collect()
}

/// Helper function to generate stash, controller and session key from seed
pub fn authority_keys_from_seed(
seed: &str,
) -> (AccountId, AccountId, GrandpaId, BabeId, ImOnlineId, AuthorityDiscoveryId) {
(
account_id_from_seed::<sr25519::Public>(&format!("{}//stash", seed)),
account_id_from_seed::<sr25519::Public>(seed),
get_from_seed::<GrandpaId>(seed),
get_from_seed::<BabeId>(seed),
get_from_seed::<ImOnlineId>(seed),
get_from_seed::<AuthorityDiscoveryId>(seed),
)
}

pub fn authority_keys_from_seeds(
seeds: &[&str],
) -> Vec<(AccountId, AccountId, GrandpaId, BabeId, ImOnlineId, AuthorityDiscoveryId)> {
seeds.iter().map(|x| authority_keys_from_seed(x)).collect()
pub fn authority_keys_from_seeds(seeds: &[&str]) -> Vec<AuthorityKeys> {
seeds.iter().map(|x| AuthorityKeys::from_seed(x)).collect()
}
}

0 comments on commit 3405ccb

Please sign in to comment.