use super::{
currency, substrate_test_pallet, wasm_binary_unwrap, AccountId, Balance, RuntimeGenesisConfig,
};
use codec::Encode;
use sc_service::construct_genesis_block;
use sp_core::{
sr25519,
storage::{well_known_keys, StateVersion, Storage},
Pair,
};
use sp_keyring::{AccountKeyring, Sr25519Keyring};
use sp_runtime::{
traits::{Block as BlockT, Hash as HashT, Header as HeaderT},
BuildStorage,
};
pub struct GenesisStorageBuilder {
authorities: Vec<AccountId>,
balances: Vec<(AccountId, u64)>,
heap_pages_override: Option<u64>,
extra_storage: Storage,
wasm_code: Option<Vec<u8>>,
}
impl Default for GenesisStorageBuilder {
fn default() -> Self {
Self::new(
vec![
Sr25519Keyring::Alice.into(),
Sr25519Keyring::Bob.into(),
Sr25519Keyring::Charlie.into(),
],
(0..16_usize)
.into_iter()
.map(|i| AccountKeyring::numeric(i).public())
.chain(vec![
AccountKeyring::Alice.into(),
AccountKeyring::Bob.into(),
AccountKeyring::Charlie.into(),
])
.collect(),
1000 * currency::DOLLARS,
)
}
}
impl GenesisStorageBuilder {
pub fn new(
authorities: Vec<AccountId>,
endowed_accounts: Vec<AccountId>,
balance: Balance,
) -> Self {
GenesisStorageBuilder {
authorities,
balances: endowed_accounts.into_iter().map(|a| (a, balance)).collect(),
heap_pages_override: None,
extra_storage: Default::default(),
wasm_code: None,
}
}
pub fn with_wasm_code(mut self, wasm_code: &Option<Vec<u8>>) -> Self {
self.wasm_code = wasm_code.clone();
self
}
pub fn with_heap_pages(mut self, heap_pages_override: Option<u64>) -> Self {
self.heap_pages_override = heap_pages_override;
self
}
pub fn with_extra_storage(mut self, storage: Storage) -> Self {
self.extra_storage = storage;
self
}
pub fn genesis_config(&self) -> RuntimeGenesisConfig {
let authorities_sr25519: Vec<_> = self
.authorities
.clone()
.into_iter()
.map(|id| sr25519::Public::from(id))
.collect();
RuntimeGenesisConfig {
system: Default::default(),
babe: pallet_babe::GenesisConfig {
authorities: authorities_sr25519
.clone()
.into_iter()
.map(|x| (x.into(), 1))
.collect(),
..Default::default()
},
substrate_test: substrate_test_pallet::GenesisConfig {
authorities: authorities_sr25519.clone(),
..Default::default()
},
balances: pallet_balances::GenesisConfig { balances: self.balances.clone() },
}
}
pub fn build(self) -> Storage {
let mut storage = self
.genesis_config()
.build_storage()
.expect("Build storage from substrate-test-runtime RuntimeGenesisConfig");
if let Some(heap_pages) = self.heap_pages_override {
storage.top.insert(well_known_keys::HEAP_PAGES.into(), heap_pages.encode());
}
storage.top.insert(
well_known_keys::CODE.into(),
self.wasm_code.clone().unwrap_or(wasm_binary_unwrap().to_vec()),
);
storage.top.extend(self.extra_storage.top.clone());
storage.children_default.extend(self.extra_storage.children_default.clone());
storage
}
}
pub fn insert_genesis_block(storage: &mut Storage) -> sp_core::hash::H256 {
let child_roots = storage.children_default.iter().map(|(sk, child_content)| {
let state_root =
<<<crate::Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
child_content.data.clone().into_iter().collect(),
sp_runtime::StateVersion::V1,
);
(sk.clone(), state_root.encode())
});
storage.top.extend(child_roots);
let state_root = <<<crate::Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root(
storage.top.clone().into_iter().collect(),
sp_runtime::StateVersion::V1,
);
let block: crate::Block = construct_genesis_block(state_root, StateVersion::V1);
let genesis_hash = block.header.hash();
genesis_hash
}