Skip to content

Commit

Permalink
Do not use Option to wrap GenesisConfig fields (paritytech#8275)
Browse files Browse the repository at this point in the history
Currently we wrap every `GenesisConfig` field in an `Option`, while
we require `Default` being implemented for all pallet genesisconfigs.
Passing `None` also results in the genesis not being initialized, which
is a bug as seen from the perspective of a pallet developer?

This pr changes the fields of the `GenesisConfig` to non `Option` types.
  • Loading branch information
bkchr authored Mar 6, 2021
1 parent 79ddc79 commit af998b2
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 113 deletions.
20 changes: 10 additions & 10 deletions bin/node-template/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,24 @@ fn testnet_genesis(
_enable_println: bool,
) -> GenesisConfig {
GenesisConfig {
frame_system: Some(SystemConfig {
frame_system: SystemConfig {
// Add Wasm runtime to storage.
code: wasm_binary.to_vec(),
changes_trie_config: Default::default(),
}),
pallet_balances: Some(BalancesConfig {
},
pallet_balances: BalancesConfig {
// Configure endowed accounts with initial balance of 1 << 60.
balances: endowed_accounts.iter().cloned().map(|k|(k, 1 << 60)).collect(),
}),
pallet_aura: Some(AuraConfig {
},
pallet_aura: AuraConfig {
authorities: initial_authorities.iter().map(|x| (x.0.clone())).collect(),
}),
pallet_grandpa: Some(GrandpaConfig {
},
pallet_grandpa: GrandpaConfig {
authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect(),
}),
pallet_sudo: Some(SudoConfig {
},
pallet_sudo: SudoConfig {
// Assign network admin rights.
key: root_key,
}),
},
}
}
68 changes: 34 additions & 34 deletions bin/node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,19 +246,19 @@ pub fn testnet_genesis(
const STASH: Balance = ENDOWMENT / 1000;

GenesisConfig {
frame_system: Some(SystemConfig {
frame_system: SystemConfig {
code: wasm_binary_unwrap().to_vec(),
changes_trie_config: Default::default(),
}),
pallet_balances: Some(BalancesConfig {
},
pallet_balances: BalancesConfig {
balances: endowed_accounts.iter().cloned()
.map(|x| (x, ENDOWMENT))
.collect()
}),
pallet_indices: Some(IndicesConfig {
},
pallet_indices: IndicesConfig {
indices: vec![],
}),
pallet_session: Some(SessionConfig {
},
pallet_session: SessionConfig {
keys: initial_authorities.iter().map(|x| {
(x.0.clone(), x.0.clone(), session_keys(
x.2.clone(),
Expand All @@ -267,8 +267,8 @@ pub fn testnet_genesis(
x.5.clone(),
))
}).collect::<Vec<_>>(),
}),
pallet_staking: Some(StakingConfig {
},
pallet_staking: StakingConfig {
validator_count: initial_authorities.len() as u32 * 2,
minimum_validator_count: initial_authorities.len() as u32,
stakers: initial_authorities.iter().map(|x| {
Expand All @@ -277,56 +277,56 @@ pub fn testnet_genesis(
invulnerables: initial_authorities.iter().map(|x| x.0.clone()).collect(),
slash_reward_fraction: Perbill::from_percent(10),
.. Default::default()
}),
pallet_democracy: Some(DemocracyConfig::default()),
pallet_elections_phragmen: Some(ElectionsConfig {
},
pallet_democracy: DemocracyConfig::default(),
pallet_elections_phragmen: ElectionsConfig {
members: endowed_accounts.iter()
.take((num_endowed_accounts + 1) / 2)
.cloned()
.map(|member| (member, STASH))
.collect(),
}),
pallet_collective_Instance1: Some(CouncilConfig::default()),
pallet_collective_Instance2: Some(TechnicalCommitteeConfig {
},
pallet_collective_Instance1: CouncilConfig::default(),
pallet_collective_Instance2: TechnicalCommitteeConfig {
members: endowed_accounts.iter()
.take((num_endowed_accounts + 1) / 2)
.cloned()
.collect(),
phantom: Default::default(),
}),
pallet_contracts: Some(ContractsConfig {
},
pallet_contracts: ContractsConfig {
current_schedule: pallet_contracts::Schedule {
enable_println, // this should only be enabled on development chains
..Default::default()
},
}),
pallet_sudo: Some(SudoConfig {
},
pallet_sudo: SudoConfig {
key: root_key,
}),
pallet_babe: Some(BabeConfig {
},
pallet_babe: BabeConfig {
authorities: vec![],
}),
pallet_im_online: Some(ImOnlineConfig {
},
pallet_im_online: ImOnlineConfig {
keys: vec![],
}),
pallet_authority_discovery: Some(AuthorityDiscoveryConfig {
},
pallet_authority_discovery: AuthorityDiscoveryConfig {
keys: vec![],
}),
pallet_grandpa: Some(GrandpaConfig {
},
pallet_grandpa: GrandpaConfig {
authorities: vec![],
}),
pallet_membership_Instance1: Some(Default::default()),
pallet_treasury: Some(Default::default()),
pallet_society: Some(SocietyConfig {
},
pallet_membership_Instance1: Default::default(),
pallet_treasury: Default::default(),
pallet_society: SocietyConfig {
members: endowed_accounts.iter()
.take((num_endowed_accounts + 1) / 2)
.cloned()
.collect(),
pot: 0,
max_members: 999,
}),
pallet_vesting: Some(Default::default()),
pallet_gilt: Some(Default::default()),
},
pallet_vesting: Default::default(),
pallet_gilt: Default::default(),
}
}

Expand Down
56 changes: 28 additions & 28 deletions bin/node/testing/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,20 @@ pub fn config_endowed(
);

GenesisConfig {
frame_system: Some(SystemConfig {
frame_system: SystemConfig {
changes_trie_config: if support_changes_trie { Some(ChangesTrieConfiguration {
digest_interval: 2,
digest_levels: 2,
}) } else { None },
code: code.map(|x| x.to_vec()).unwrap_or_else(|| wasm_binary_unwrap().to_vec()),
}),
pallet_indices: Some(IndicesConfig {
},
pallet_indices: IndicesConfig {
indices: vec![],
}),
pallet_balances: Some(BalancesConfig {
},
pallet_balances: BalancesConfig {
balances: endowed,
}),
pallet_session: Some(SessionConfig {
},
pallet_session: SessionConfig {
keys: vec![
(dave(), alice(), to_session_keys(
&Ed25519Keyring::Alice,
Expand All @@ -84,8 +84,8 @@ pub fn config_endowed(
&Sr25519Keyring::Charlie,
)),
]
}),
pallet_staking: Some(StakingConfig {
},
pallet_staking: StakingConfig {
stakers: vec![
(dave(), alice(), 111 * DOLLARS, StakerStatus::Validator),
(eve(), bob(), 100 * DOLLARS, StakerStatus::Validator),
Expand All @@ -96,29 +96,29 @@ pub fn config_endowed(
slash_reward_fraction: Perbill::from_percent(10),
invulnerables: vec![alice(), bob(), charlie()],
.. Default::default()
}),
pallet_contracts: Some(ContractsConfig {
},
pallet_contracts: ContractsConfig {
current_schedule: Default::default(),
}),
pallet_babe: Some(Default::default()),
pallet_grandpa: Some(GrandpaConfig {
},
pallet_babe: Default::default(),
pallet_grandpa: GrandpaConfig {
authorities: vec![],
}),
pallet_im_online: Some(Default::default()),
pallet_authority_discovery: Some(Default::default()),
pallet_democracy: Some(Default::default()),
pallet_collective_Instance1: Some(Default::default()),
pallet_collective_Instance2: Some(Default::default()),
pallet_membership_Instance1: Some(Default::default()),
pallet_elections_phragmen: Some(Default::default()),
pallet_sudo: Some(Default::default()),
pallet_treasury: Some(Default::default()),
pallet_society: Some(SocietyConfig {
},
pallet_im_online: Default::default(),
pallet_authority_discovery: Default::default(),
pallet_democracy: Default::default(),
pallet_collective_Instance1: Default::default(),
pallet_collective_Instance2: Default::default(),
pallet_membership_Instance1: Default::default(),
pallet_elections_phragmen: Default::default(),
pallet_sudo: Default::default(),
pallet_treasury: Default::default(),
pallet_society: SocietyConfig {
members: vec![alice(), bob()],
pot: 0,
max_members: 999,
}),
pallet_vesting: Some(Default::default()),
pallet_gilt: Some(Default::default()),
},
pallet_vesting: Default::default(),
pallet_gilt: Default::default(),
}
}
10 changes: 5 additions & 5 deletions frame/collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1054,15 +1054,15 @@ mod tests {

pub fn new_test_ext() -> sp_io::TestExternalities {
let mut ext: sp_io::TestExternalities = GenesisConfig {
collective_Instance1: Some(collective::GenesisConfig {
collective_Instance1: collective::GenesisConfig {
members: vec![1, 2, 3],
phantom: Default::default(),
}),
collective_Instance2: Some(collective::GenesisConfig {
},
collective_Instance2: collective::GenesisConfig {
members: vec![1, 2, 3, 4, 5],
phantom: Default::default(),
}),
collective: None,
},
collective: Default::default(),
}.build_storage().unwrap().into();
ext.execute_with(|| System::set_block_number(1));
ext
Expand Down
8 changes: 4 additions & 4 deletions frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,7 @@ mod tests {
pub fn build_and_execute(self, test: impl FnOnce() -> ()) {
MEMBERS.with(|m| *m.borrow_mut() = self.genesis_members.iter().map(|(m, _)| m.clone()).collect::<Vec<_>>());
let mut ext: sp_io::TestExternalities = GenesisConfig {
pallet_balances: Some(pallet_balances::GenesisConfig::<Test>{
pallet_balances: pallet_balances::GenesisConfig::<Test>{
balances: vec![
(1, 10 * self.balance_factor),
(2, 20 * self.balance_factor),
Expand All @@ -1254,10 +1254,10 @@ mod tests {
(5, 50 * self.balance_factor),
(6, 60 * self.balance_factor)
],
}),
elections_phragmen: Some(elections_phragmen::GenesisConfig::<Test> {
},
elections_phragmen: elections_phragmen::GenesisConfig::<Test> {
members: self.genesis_members
}),
},
}.build_storage().unwrap().into();
ext.execute_with(pre_conditions);
ext.execute_with(test);
Expand Down
8 changes: 4 additions & 4 deletions frame/elections/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl ExtBuilder {
PRESENT_SLASH_PER_VOTER.with(|v| *v.borrow_mut() = self.bad_presentation_punishment);
DECAY_RATIO.with(|v| *v.borrow_mut() = self.decay_ratio);
let mut ext: sp_io::TestExternalities = GenesisConfig {
pallet_balances: Some(pallet_balances::GenesisConfig::<Test>{
pallet_balances: pallet_balances::GenesisConfig::<Test>{
balances: vec![
(1, 10 * self.balance_factor),
(2, 20 * self.balance_factor),
Expand All @@ -203,13 +203,13 @@ impl ExtBuilder {
(5, 50 * self.balance_factor),
(6, 60 * self.balance_factor)
],
}),
elections: Some(elections::GenesisConfig::<Test>{
},
elections: elections::GenesisConfig::<Test>{
members: vec![],
desired_seats: self.desired_seats,
presentation_duration: 2,
term_duration: 5,
}),
},
}.build_storage().unwrap().into();
ext.execute_with(|| System::set_block_number(1));
ext
Expand Down
8 changes: 4 additions & 4 deletions frame/example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -814,14 +814,14 @@ mod tests {
pub fn new_test_ext() -> sp_io::TestExternalities {
let t = GenesisConfig {
// We use default for brevity, but you can configure as desired if needed.
frame_system: Some(Default::default()),
pallet_balances: Some(Default::default()),
pallet_example: Some(pallet_example::GenesisConfig {
frame_system: Default::default(),
pallet_balances: Default::default(),
pallet_example: pallet_example::GenesisConfig {
dummy: 42,
// we configure the map with (key, value) pairs.
bar: vec![(1, 2), (2, 3)],
foo: 24,
}),
},
}.build_storage().unwrap();
t.into()
}
Expand Down
22 changes: 10 additions & 12 deletions frame/support/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ macro_rules! impl_outer_config {
#[serde(deny_unknown_fields)]
pub struct $main {
$(
pub [< $snake $(_ $instance )? >]: Option<$config>,
pub [< $snake $(_ $instance )? >]: $config,
)*
}
#[cfg(any(feature = "std", test))]
Expand All @@ -92,15 +92,13 @@ macro_rules! impl_outer_config {
storage: &mut $crate::sp_runtime::Storage,
) -> std::result::Result<(), String> {
$(
if let Some(ref extra) = self.[< $snake $(_ $instance )? >] {
$crate::impl_outer_config! {
@CALL_FN
$concrete;
$snake;
$( $instance )?;
extra;
storage;
}
$crate::impl_outer_config! {
@CALL_FN
$concrete;
$snake;
$( $instance )?;
&self.[< $snake $(_ $instance )? >];
storage;
}
)*

Expand All @@ -117,7 +115,7 @@ macro_rules! impl_outer_config {
$runtime:ident;
$module:ident;
$instance:ident;
$extra:ident;
$extra:expr;
$storage:ident;
) => {
$crate::sp_runtime::BuildModuleGenesisStorage::<$runtime, $module::$instance>::build_module_genesis_storage(
Expand All @@ -129,7 +127,7 @@ macro_rules! impl_outer_config {
$runtime:ident;
$module:ident;
;
$extra:ident;
$extra:expr;
$storage:ident;
) => {
$crate::sp_runtime::BuildModuleGenesisStorage::
Expand Down
Loading

0 comments on commit af998b2

Please sign in to comment.