Skip to content

Commit

Permalink
Fetch Babe configuration from runtime state (#11760)
Browse files Browse the repository at this point in the history
* Fetch babe config data from runtime state

* Some renaming

* More renaming

* Final nits

* Fix tests and benches

* Rename  to  in BabeConfiguration

* Remove duplicate babe parameter description

Already specified over the 'PRIMARY_PROBABILITY' constant value

* trigger pipeline

* trigger pipeline
  • Loading branch information
davxy authored Sep 5, 2022
1 parent 5f18a8b commit e24e9ad
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 155 deletions.
7 changes: 2 additions & 5 deletions bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ pub fn new_partial(
let justification_import = grandpa_block_import.clone();

let (block_import, babe_link) = sc_consensus_babe::block_import(
sc_consensus_babe::Config::get(&*client)?,
sc_consensus_babe::configuration(&*client)?,
grandpa_block_import,
client.clone(),
)?;
Expand Down Expand Up @@ -682,10 +682,7 @@ mod tests {
.epoch_changes()
.shared_data()
.epoch_data(&epoch_descriptor, |slot| {
sc_consensus_babe::Epoch::genesis(
babe_link.config().genesis_config(),
slot,
)
sc_consensus_babe::Epoch::genesis(babe_link.config(), slot)
})
.unwrap();

Expand Down
4 changes: 2 additions & 2 deletions bin/node/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use std::sync::Arc;
use jsonrpsee::RpcModule;
use node_primitives::{AccountId, Balance, Block, BlockNumber, Hash, Index};
use sc_client_api::AuxStore;
use sc_consensus_babe::{Config, Epoch};
use sc_consensus_babe::{BabeConfiguration, Epoch};
use sc_consensus_epochs::SharedEpochChanges;
use sc_finality_grandpa::{
FinalityProofProvider, GrandpaJustificationStream, SharedAuthoritySet, SharedVoterState,
Expand All @@ -54,7 +54,7 @@ use sp_keystore::SyncCryptoStorePtr;
/// Extra dependencies for BABE.
pub struct BabeDeps {
/// BABE protocol config.
pub babe_config: Config,
pub babe_config: BabeConfiguration,
/// BABE pending epoch changes.
pub shared_epoch_changes: SharedEpochChanges<Block, Epoch>,
/// The keystore that manages the keys of the node.
Expand Down
16 changes: 6 additions & 10 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1859,19 +1859,15 @@ impl_runtime_apis! {
}

impl sp_consensus_babe::BabeApi<Block> for Runtime {
fn configuration() -> sp_consensus_babe::BabeGenesisConfiguration {
// The choice of `c` parameter (where `1 - c` represents the
// probability of a slot being empty), is done in accordance to the
// slot duration and expected target block time, for safely
// resisting network delays of maximum two seconds.
// <https://research.web3.foundation/en/latest/polkadot/BABE/Babe/#6-practical-results>
sp_consensus_babe::BabeGenesisConfiguration {
fn configuration() -> sp_consensus_babe::BabeConfiguration {
let epoch_config = Babe::epoch_config().unwrap_or(BABE_GENESIS_EPOCH_CONFIG);
sp_consensus_babe::BabeConfiguration {
slot_duration: Babe::slot_duration(),
epoch_length: EpochDuration::get(),
c: BABE_GENESIS_EPOCH_CONFIG.c,
genesis_authorities: Babe::authorities().to_vec(),
c: epoch_config.c,
authorities: Babe::authorities().to_vec(),
randomness: Babe::randomness(),
allowed_slots: BABE_GENESIS_EPOCH_CONFIG.allowed_slots,
allowed_slots: epoch_config.allowed_slots,
}
}

Expand Down
18 changes: 10 additions & 8 deletions client/consensus/babe/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ use jsonrpsee::{
types::{error::CallError, ErrorObject},
};

use sc_consensus_babe::{authorship, Config, Epoch};
use sc_consensus_babe::{authorship, Epoch};
use sc_consensus_epochs::{descendent_query, Epoch as EpochT, SharedEpochChanges};
use sc_rpc_api::DenyUnsafe;
use serde::{Deserialize, Serialize};
use sp_api::{BlockId, ProvideRuntimeApi};
use sp_application_crypto::AppKey;
use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};
use sp_consensus::{Error as ConsensusError, SelectChain};
use sp_consensus_babe::{digests::PreDigest, AuthorityId, BabeApi as BabeRuntimeApi};
use sp_consensus_babe::{
digests::PreDigest, AuthorityId, BabeApi as BabeRuntimeApi, BabeConfiguration,
};
use sp_core::crypto::ByteArray;
use sp_keystore::{SyncCryptoStore, SyncCryptoStorePtr};
use sp_runtime::traits::{Block as BlockT, Header as _};
Expand All @@ -57,7 +59,7 @@ pub struct Babe<B: BlockT, C, SC> {
/// shared reference to the Keystore
keystore: SyncCryptoStorePtr,
/// config (actually holds the slot duration)
babe_config: Config,
babe_config: BabeConfiguration,
/// The SelectChain strategy
select_chain: SC,
/// Whether to deny unsafe calls
Expand All @@ -70,7 +72,7 @@ impl<B: BlockT, C, SC> Babe<B, C, SC> {
client: Arc<C>,
shared_epoch_changes: SharedEpochChanges<B, Epoch>,
keystore: SyncCryptoStorePtr,
babe_config: Config,
babe_config: BabeConfiguration,
select_chain: SC,
deny_unsafe: DenyUnsafe,
) -> Self {
Expand Down Expand Up @@ -185,7 +187,7 @@ impl From<Error> for JsonRpseeError {
async fn epoch_data<B, C, SC>(
epoch_changes: &SharedEpochChanges<B, Epoch>,
client: &Arc<C>,
babe_config: &Config,
babe_config: &BabeConfiguration,
slot: u64,
select_chain: &SC,
) -> Result<Epoch, Error>
Expand All @@ -202,7 +204,7 @@ where
&parent.hash(),
*parent.number(),
slot.into(),
|slot| Epoch::genesis(babe_config.genesis_config(), slot),
|slot| Epoch::genesis(babe_config, slot),
)
.map_err(|e| Error::Consensus(ConsensusError::ChainLookup(e.to_string())))?
.ok_or(Error::Consensus(ConsensusError::InvalidAuthoritiesSet))
Expand All @@ -221,7 +223,7 @@ mod tests {
TestClientBuilderExt,
};

use sc_consensus_babe::{block_import, AuthorityPair, Config};
use sc_consensus_babe::{block_import, AuthorityPair};
use std::sync::Arc;

/// creates keystore backed by a temp file
Expand All @@ -243,7 +245,7 @@ mod tests {
let builder = TestClientBuilder::new();
let (client, longest_chain) = builder.build_with_longest_chain();
let client = Arc::new(client);
let config = Config::get(&*client).expect("config available");
let config = sc_consensus_babe::configuration(&*client).expect("config available");
let (_, link) = block_import(config.clone(), client.clone(), client.clone())
.expect("can initialize block-import");

Expand Down
10 changes: 5 additions & 5 deletions client/consensus/babe/src/aux_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use sc_consensus_epochs::{
EpochChangesFor, SharedEpochChanges,
};
use sp_blockchain::{Error as ClientError, Result as ClientResult};
use sp_consensus_babe::{BabeBlockWeight, BabeGenesisConfiguration};
use sp_consensus_babe::{BabeBlockWeight, BabeConfiguration};
use sp_runtime::traits::Block as BlockT;

const BABE_EPOCH_CHANGES_VERSION: &[u8] = b"babe_epoch_changes_version";
Expand Down Expand Up @@ -57,7 +57,7 @@ where
/// Load or initialize persistent epoch change data from backend.
pub fn load_epoch_changes<Block: BlockT, B: AuxStore>(
backend: &B,
config: &BabeGenesisConfiguration,
config: &BabeConfiguration,
) -> ClientResult<SharedEpochChanges<Block, Epoch>> {
let version = load_decode::<_, u32>(backend, BABE_EPOCH_CHANGES_VERSION)?;

Expand Down Expand Up @@ -143,7 +143,7 @@ mod test {
use sc_consensus_epochs::{EpochHeader, PersistedEpoch, PersistedEpochHeader};
use sc_network_test::Block as TestBlock;
use sp_consensus::Error as ConsensusError;
use sp_consensus_babe::{AllowedSlots, BabeGenesisConfiguration};
use sp_consensus_babe::AllowedSlots;
use sp_core::H256;
use sp_runtime::traits::NumberFor;
use substrate_test_runtime_client;
Expand Down Expand Up @@ -182,11 +182,11 @@ mod test {

let epoch_changes = load_epoch_changes::<TestBlock, _>(
&client,
&BabeGenesisConfiguration {
&BabeConfiguration {
slot_duration: 10,
epoch_length: 4,
c: (3, 10),
genesis_authorities: Vec::new(),
authorities: Vec::new(),
randomness: Default::default(),
allowed_slots: AllowedSlots::PrimaryAndSecondaryPlainSlots,
},
Expand Down
Loading

0 comments on commit e24e9ad

Please sign in to comment.