mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-30 02:37:58 +00:00
Fetch Babe configuration from runtime state (#11760)
* 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
This commit is contained in:
@@ -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";
|
||||
@@ -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)?;
|
||||
|
||||
@@ -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;
|
||||
@@ -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,
|
||||
},
|
||||
|
||||
@@ -119,7 +119,7 @@ use sp_consensus::{
|
||||
SelectChain,
|
||||
};
|
||||
use sp_consensus_babe::inherents::BabeInherentData;
|
||||
use sp_consensus_slots::{Slot, SlotDuration};
|
||||
use sp_consensus_slots::Slot;
|
||||
use sp_core::{crypto::ByteArray, ExecutionContext};
|
||||
use sp_inherents::{CreateInherentDataProviders, InherentData, InherentDataProvider};
|
||||
use sp_keystore::{SyncCryptoStore, SyncCryptoStorePtr};
|
||||
@@ -137,8 +137,7 @@ pub use sp_consensus_babe::{
|
||||
PrimaryPreDigest, SecondaryPlainPreDigest,
|
||||
},
|
||||
AuthorityId, AuthorityPair, AuthoritySignature, BabeApi, BabeAuthorityWeight, BabeBlockWeight,
|
||||
BabeEpochConfiguration, BabeGenesisConfiguration, ConsensusLog, BABE_ENGINE_ID,
|
||||
VRF_OUTPUT_LENGTH,
|
||||
BabeConfiguration, BabeEpochConfiguration, ConsensusLog, BABE_ENGINE_ID, VRF_OUTPUT_LENGTH,
|
||||
};
|
||||
|
||||
pub use aux_schema::load_block_weight as block_weight;
|
||||
@@ -211,12 +210,12 @@ impl From<sp_consensus_babe::Epoch> for Epoch {
|
||||
impl Epoch {
|
||||
/// Create the genesis epoch (epoch #0). This is defined to start at the slot of
|
||||
/// the first block, so that has to be provided.
|
||||
pub fn genesis(genesis_config: &BabeGenesisConfiguration, slot: Slot) -> Epoch {
|
||||
pub fn genesis(genesis_config: &BabeConfiguration, slot: Slot) -> Epoch {
|
||||
Epoch {
|
||||
epoch_index: 0,
|
||||
start_slot: slot,
|
||||
duration: genesis_config.epoch_length,
|
||||
authorities: genesis_config.genesis_authorities.clone(),
|
||||
authorities: genesis_config.authorities.clone(),
|
||||
randomness: genesis_config.randomness,
|
||||
config: BabeEpochConfiguration {
|
||||
c: genesis_config.c,
|
||||
@@ -338,56 +337,36 @@ pub struct BabeIntermediate<B: BlockT> {
|
||||
/// Intermediate key for Babe engine.
|
||||
pub static INTERMEDIATE_KEY: &[u8] = b"babe1";
|
||||
|
||||
/// Configuration for BABE used for defining block verification parameters as
|
||||
/// well as authoring (e.g. the slot duration).
|
||||
#[derive(Clone)]
|
||||
pub struct Config {
|
||||
genesis_config: BabeGenesisConfiguration,
|
||||
}
|
||||
/// Read configuration from the runtime state at current best block.
|
||||
pub fn configuration<B: BlockT, C>(client: &C) -> ClientResult<BabeConfiguration>
|
||||
where
|
||||
C: AuxStore + ProvideRuntimeApi<B> + UsageProvider<B>,
|
||||
C::Api: BabeApi<B>,
|
||||
{
|
||||
let block_id = if client.usage_info().chain.finalized_state.is_some() {
|
||||
BlockId::Hash(client.usage_info().chain.best_hash)
|
||||
} else {
|
||||
debug!(target: "babe", "No finalized state is available. Reading config from genesis");
|
||||
BlockId::Hash(client.usage_info().chain.genesis_hash)
|
||||
};
|
||||
|
||||
impl Config {
|
||||
/// Create a new config by reading the genesis configuration from the runtime.
|
||||
pub fn get<B: BlockT, C>(client: &C) -> ClientResult<Self>
|
||||
where
|
||||
C: AuxStore + ProvideRuntimeApi<B> + UsageProvider<B>,
|
||||
C::Api: BabeApi<B>,
|
||||
{
|
||||
trace!(target: "babe", "Getting slot duration");
|
||||
let runtime_api = client.runtime_api();
|
||||
let version = runtime_api.api_version::<dyn BabeApi<B>>(&block_id)?;
|
||||
|
||||
let mut best_block_id = BlockId::Hash(client.usage_info().chain.best_hash);
|
||||
if client.usage_info().chain.finalized_state.is_none() {
|
||||
debug!(target: "babe", "No finalized state is available. Reading config from genesis");
|
||||
best_block_id = BlockId::Hash(client.usage_info().chain.genesis_hash);
|
||||
}
|
||||
let runtime_api = client.runtime_api();
|
||||
|
||||
let version = runtime_api.api_version::<dyn BabeApi<B>>(&best_block_id)?;
|
||||
|
||||
let genesis_config = if version == Some(1) {
|
||||
let config = match version {
|
||||
Some(1) => {
|
||||
#[allow(deprecated)]
|
||||
{
|
||||
runtime_api.configuration_before_version_2(&best_block_id)?.into()
|
||||
runtime_api.configuration_before_version_2(&block_id)?.into()
|
||||
}
|
||||
} else if version == Some(2) {
|
||||
runtime_api.configuration(&best_block_id)?
|
||||
} else {
|
||||
},
|
||||
Some(2) => runtime_api.configuration(&block_id)?,
|
||||
_ =>
|
||||
return Err(sp_blockchain::Error::VersionInvalid(
|
||||
"Unsupported or invalid BabeApi version".to_string(),
|
||||
))
|
||||
};
|
||||
|
||||
Ok(Config { genesis_config })
|
||||
}
|
||||
|
||||
/// Get the genesis configuration.
|
||||
pub fn genesis_config(&self) -> &BabeGenesisConfiguration {
|
||||
&self.genesis_config
|
||||
}
|
||||
|
||||
/// Get the slot duration defined in the genesis configuration.
|
||||
pub fn slot_duration(&self) -> SlotDuration {
|
||||
SlotDuration::from_millis(self.genesis_config.slot_duration)
|
||||
}
|
||||
)),
|
||||
};
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
/// Parameters for BABE.
|
||||
@@ -611,7 +590,7 @@ fn aux_storage_cleanup<C: HeaderMetadata<Block> + HeaderBackend<Block>, Block: B
|
||||
|
||||
async fn answer_requests<B: BlockT, C>(
|
||||
mut request_rx: Receiver<BabeRequest<B>>,
|
||||
config: Config,
|
||||
config: BabeConfiguration,
|
||||
client: Arc<C>,
|
||||
epoch_changes: SharedEpochChanges<B, Epoch>,
|
||||
) where
|
||||
@@ -640,9 +619,7 @@ async fn answer_requests<B: BlockT, C>(
|
||||
.ok_or(Error::<B>::FetchEpoch(parent_hash))?;
|
||||
|
||||
let viable_epoch = epoch_changes
|
||||
.viable_epoch(&epoch_descriptor, |slot| {
|
||||
Epoch::genesis(&config.genesis_config, slot)
|
||||
})
|
||||
.viable_epoch(&epoch_descriptor, |slot| Epoch::genesis(&config, slot))
|
||||
.ok_or(Error::<B>::FetchEpoch(parent_hash))?;
|
||||
|
||||
Ok(sp_consensus_babe::Epoch {
|
||||
@@ -739,7 +716,7 @@ struct BabeSlotWorker<B: BlockT, C, E, I, SO, L, BS> {
|
||||
keystore: SyncCryptoStorePtr,
|
||||
epoch_changes: SharedEpochChanges<B, Epoch>,
|
||||
slot_notification_sinks: SlotNotificationSinks<B>,
|
||||
config: Config,
|
||||
config: BabeConfiguration,
|
||||
block_proposal_slot_portion: SlotProportion,
|
||||
max_block_proposal_slot_portion: Option<SlotProportion>,
|
||||
telemetry: Option<TelemetryHandle>,
|
||||
@@ -797,9 +774,7 @@ where
|
||||
fn authorities_len(&self, epoch_descriptor: &Self::EpochData) -> Option<usize> {
|
||||
self.epoch_changes
|
||||
.shared_data()
|
||||
.viable_epoch(epoch_descriptor, |slot| {
|
||||
Epoch::genesis(&self.config.genesis_config, slot)
|
||||
})
|
||||
.viable_epoch(epoch_descriptor, |slot| Epoch::genesis(&self.config, slot))
|
||||
.map(|epoch| epoch.as_ref().authorities.len())
|
||||
}
|
||||
|
||||
@@ -814,9 +789,7 @@ where
|
||||
slot,
|
||||
self.epoch_changes
|
||||
.shared_data()
|
||||
.viable_epoch(epoch_descriptor, |slot| {
|
||||
Epoch::genesis(&self.config.genesis_config, slot)
|
||||
})?
|
||||
.viable_epoch(epoch_descriptor, |slot| Epoch::genesis(&self.config, slot))?
|
||||
.as_ref(),
|
||||
&self.keystore,
|
||||
);
|
||||
@@ -1020,7 +993,7 @@ fn find_next_config_digest<B: BlockT>(
|
||||
#[derive(Clone)]
|
||||
pub struct BabeLink<Block: BlockT> {
|
||||
epoch_changes: SharedEpochChanges<Block, Epoch>,
|
||||
config: Config,
|
||||
config: BabeConfiguration,
|
||||
}
|
||||
|
||||
impl<Block: BlockT> BabeLink<Block> {
|
||||
@@ -1030,7 +1003,7 @@ impl<Block: BlockT> BabeLink<Block> {
|
||||
}
|
||||
|
||||
/// Get the config of this link.
|
||||
pub fn config(&self) -> &Config {
|
||||
pub fn config(&self) -> &BabeConfiguration {
|
||||
&self.config
|
||||
}
|
||||
}
|
||||
@@ -1040,7 +1013,7 @@ pub struct BabeVerifier<Block: BlockT, Client, SelectChain, CAW, CIDP> {
|
||||
client: Arc<Client>,
|
||||
select_chain: SelectChain,
|
||||
create_inherent_data_providers: CIDP,
|
||||
config: Config,
|
||||
config: BabeConfiguration,
|
||||
epoch_changes: SharedEpochChanges<Block, Epoch>,
|
||||
can_author_with: CAW,
|
||||
telemetry: Option<TelemetryHandle>,
|
||||
@@ -1245,9 +1218,7 @@ where
|
||||
.map_err(|e| Error::<Block>::ForkTree(Box::new(e)))?
|
||||
.ok_or(Error::<Block>::FetchEpoch(parent_hash))?;
|
||||
let viable_epoch = epoch_changes
|
||||
.viable_epoch(&epoch_descriptor, |slot| {
|
||||
Epoch::genesis(&self.config.genesis_config, slot)
|
||||
})
|
||||
.viable_epoch(&epoch_descriptor, |slot| Epoch::genesis(&self.config, slot))
|
||||
.ok_or(Error::<Block>::FetchEpoch(parent_hash))?;
|
||||
|
||||
// We add one to the current slot to allow for some small drift.
|
||||
@@ -1353,7 +1324,7 @@ pub struct BabeBlockImport<Block: BlockT, Client, I> {
|
||||
inner: I,
|
||||
client: Arc<Client>,
|
||||
epoch_changes: SharedEpochChanges<Block, Epoch>,
|
||||
config: Config,
|
||||
config: BabeConfiguration,
|
||||
}
|
||||
|
||||
impl<Block: BlockT, I: Clone, Client> Clone for BabeBlockImport<Block, Client, I> {
|
||||
@@ -1372,7 +1343,7 @@ impl<Block: BlockT, Client, I> BabeBlockImport<Block, Client, I> {
|
||||
client: Arc<Client>,
|
||||
epoch_changes: SharedEpochChanges<Block, Epoch>,
|
||||
block_import: I,
|
||||
config: Config,
|
||||
config: BabeConfiguration,
|
||||
) -> Self {
|
||||
BabeBlockImport { client, inner: block_import, epoch_changes, config }
|
||||
}
|
||||
@@ -1580,9 +1551,7 @@ where
|
||||
old_epoch_changes = Some((*epoch_changes).clone());
|
||||
|
||||
let viable_epoch = epoch_changes
|
||||
.viable_epoch(&epoch_descriptor, |slot| {
|
||||
Epoch::genesis(&self.config.genesis_config, slot)
|
||||
})
|
||||
.viable_epoch(&epoch_descriptor, |slot| Epoch::genesis(&self.config, slot))
|
||||
.ok_or_else(|| {
|
||||
ConsensusError::ClientImport(Error::<Block>::FetchEpoch(parent_hash).into())
|
||||
})?;
|
||||
@@ -1761,7 +1730,7 @@ where
|
||||
/// Also returns a link object used to correctly instantiate the import queue
|
||||
/// and background worker.
|
||||
pub fn block_import<Client, Block: BlockT, I>(
|
||||
config: Config,
|
||||
config: BabeConfiguration,
|
||||
wrapped_block_import: I,
|
||||
client: Arc<Client>,
|
||||
) -> ClientResult<(BabeBlockImport<Block, Client, I>, BabeLink<Block>)>
|
||||
@@ -1772,8 +1741,7 @@ where
|
||||
+ PreCommitActions<Block>
|
||||
+ 'static,
|
||||
{
|
||||
let epoch_changes =
|
||||
aux_schema::load_epoch_changes::<Block, _>(&*client, &config.genesis_config)?;
|
||||
let epoch_changes = aux_schema::load_epoch_changes::<Block, _>(&*client, &config)?;
|
||||
let link = BabeLink { epoch_changes: epoch_changes.clone(), config: config.clone() };
|
||||
|
||||
// NOTE: this isn't entirely necessary, but since we didn't use to prune the
|
||||
@@ -1884,9 +1852,9 @@ where
|
||||
|
||||
// Revert epoch changes tree.
|
||||
|
||||
let config = Config::get(&*client)?;
|
||||
let epoch_changes =
|
||||
aux_schema::load_epoch_changes::<Block, Client>(&*client, config.genesis_config())?;
|
||||
// This config is only used on-genesis.
|
||||
let config = configuration(&*client)?;
|
||||
let epoch_changes = aux_schema::load_epoch_changes::<Block, Client>(&*client, &config)?;
|
||||
let mut epoch_changes = epoch_changes.shared_data();
|
||||
|
||||
if revert_up_to_number == Zero::zero() {
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{
|
||||
AuthorityId, BabeAuthorityWeight, BabeEpochConfiguration, BabeGenesisConfiguration, Epoch,
|
||||
AuthorityId, BabeAuthorityWeight, BabeConfiguration, BabeEpochConfiguration, Epoch,
|
||||
NextEpochDescriptor, VRF_OUTPUT_LENGTH,
|
||||
};
|
||||
use codec::{Decode, Encode};
|
||||
@@ -64,7 +64,7 @@ impl EpochT for EpochV0 {
|
||||
|
||||
impl EpochV0 {
|
||||
/// Migrate the sturct to current epoch version.
|
||||
pub fn migrate(self, config: &BabeGenesisConfiguration) -> Epoch {
|
||||
pub fn migrate(self, config: &BabeConfiguration) -> Epoch {
|
||||
Epoch {
|
||||
epoch_index: self.epoch_index,
|
||||
start_slot: self.start_slot,
|
||||
|
||||
@@ -36,6 +36,7 @@ use sp_consensus_babe::{
|
||||
inherents::InherentDataProvider, make_transcript, make_transcript_data, AllowedSlots,
|
||||
AuthorityPair, Slot,
|
||||
};
|
||||
use sp_consensus_slots::SlotDuration;
|
||||
use sp_core::crypto::Pair;
|
||||
use sp_keystore::{vrf::make_transcript as transcript_from_data, SyncCryptoStore};
|
||||
use sp_runtime::{
|
||||
@@ -71,7 +72,7 @@ type BabeBlockImport =
|
||||
struct DummyFactory {
|
||||
client: Arc<TestClient>,
|
||||
epoch_changes: SharedEpochChanges<TestBlock, Epoch>,
|
||||
config: Config,
|
||||
config: BabeConfiguration,
|
||||
mutator: Mutator,
|
||||
}
|
||||
|
||||
@@ -139,7 +140,7 @@ impl DummyProposer {
|
||||
&self.parent_hash,
|
||||
self.parent_number,
|
||||
this_slot,
|
||||
|slot| Epoch::genesis(self.factory.config.genesis_config(), slot),
|
||||
|slot| Epoch::genesis(&self.factory.config, slot),
|
||||
)
|
||||
.expect("client has data to find epoch")
|
||||
.expect("can compute epoch for baked block");
|
||||
@@ -288,7 +289,7 @@ impl TestNetFactory for BabeTestNet {
|
||||
) {
|
||||
let client = client.as_client();
|
||||
|
||||
let config = Config::get(&*client).expect("config available");
|
||||
let config = crate::configuration(&*client).expect("config available");
|
||||
let (block_import, link) = crate::block_import(config, client.clone(), client.clone())
|
||||
.expect("can initialize block-import");
|
||||
|
||||
@@ -559,11 +560,11 @@ fn can_author_block() {
|
||||
},
|
||||
};
|
||||
|
||||
let mut config = crate::BabeGenesisConfiguration {
|
||||
let mut config = crate::BabeConfiguration {
|
||||
slot_duration: 1000,
|
||||
epoch_length: 100,
|
||||
c: (3, 10),
|
||||
genesis_authorities: Vec::new(),
|
||||
authorities: Vec::new(),
|
||||
randomness: [0; 32],
|
||||
allowed_slots: AllowedSlots::PrimaryAndSecondaryPlainSlots,
|
||||
};
|
||||
@@ -708,12 +709,12 @@ fn importing_block_one_sets_genesis_epoch() {
|
||||
&mut block_import,
|
||||
);
|
||||
|
||||
let genesis_epoch = Epoch::genesis(data.link.config.genesis_config(), 999.into());
|
||||
let genesis_epoch = Epoch::genesis(&data.link.config, 999.into());
|
||||
|
||||
let epoch_changes = data.link.epoch_changes.shared_data();
|
||||
let epoch_for_second_block = epoch_changes
|
||||
.epoch_data_for_child_of(descendent_query(&*client), &block_hash, 1, 1000.into(), |slot| {
|
||||
Epoch::genesis(data.link.config.genesis_config(), slot)
|
||||
Epoch::genesis(&data.link.config, slot)
|
||||
})
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
@@ -771,16 +772,14 @@ fn revert_prunes_epoch_changes_and_removes_weights() {
|
||||
|
||||
// Load and check epoch changes.
|
||||
|
||||
let actual_nodes = aux_schema::load_epoch_changes::<Block, TestClient>(
|
||||
&*client,
|
||||
data.link.config.genesis_config(),
|
||||
)
|
||||
.expect("load epoch changes")
|
||||
.shared_data()
|
||||
.tree()
|
||||
.iter()
|
||||
.map(|(h, _, _)| *h)
|
||||
.collect::<Vec<_>>();
|
||||
let actual_nodes =
|
||||
aux_schema::load_epoch_changes::<Block, TestClient>(&*client, &data.link.config)
|
||||
.expect("load epoch changes")
|
||||
.shared_data()
|
||||
.tree()
|
||||
.iter()
|
||||
.map(|(h, _, _)| *h)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let expected_nodes = vec![
|
||||
canon[0], // A
|
||||
|
||||
Reference in New Issue
Block a user