Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Fetch Babe configuration from runtime state #11760

Merged
merged 11 commits into from
Sep 5, 2022

Conversation

davxy
Copy link
Member

@davxy davxy commented Jun 30, 2022

BabeApi::configuration method semantic was not clearly defined.

Almost everywhere, within the codebase, this API method is used to fetch the protocol configuration data as defined on genesis.
But the runtime apis are generally defined to act on the state at an arbitrary block.

The (upstream) BabeApi::configuration implementation that ships with Substrate node was fetching some of the data from the current best block state and some other from a global constant containing a couple of genesis values (for c and allowed_slots params).
In other words the configuration API doesn't return the genesis value nor the values at the requested block... but an unclear hybrid of the two.

This PR tries to clean-up the definition by:

  1. changing the BabeApi::configuration implementation within the substrate node to fetch the data at the requested block;
  2. remove some over-engineering by removing the sc_consensus_babe::Config newtype. We're going to directly use the configuration type defined within the Babe's primitives (sp_consensus_babe::BabeConfiguration).
  3. rename BabeGenesisConfiguration to BabeConfiguration. This type is not used only to store the genesis configuration, but it is used to store the protocol configuration in general.

The modification should not alter the current behavior since the configuration cached by the various Babe components is almost only used as a fallback to get genesis data. In other words is used on the very first run and in this case the values returned by the Babe's configuration Api are equal to the constant values used before this PR.


This section contains a summary of (previous and current) components holding a cached Babe configuration.

sc_consensus_babe::{BabeLink, BabeBlockImport, BabeSlotWorker}, sc_consensus_babe_rpc::Babe

Cached configuration used only on genesis.
Thus the values read from the runtime are equal to the genesis values.
In other words, should be functionally equivalent to the previous implementation.

sc_consensus_manual_seal::BabeConsensusDataProvider

Cached configuration used only on genesis (as prev cases).

But it is also used to get slot_duration and epoch_length values in the append_block_import implementation to set up the necessary import parameters.
Here the end-result is still "correct" just because these values are not allowed to change between epoch changes.

We should keep an eye on this if in the future these are allowed to change between epochs.
(A NOTE in the code has been added just as a reminder and make this explicit)

Migration of EpochChanges

The old version config is migrated to the current one by using the configuration read during startup using the BabeApi.

Previously the BabeApi::configuration was constructed using some constant values (in particular the c and allowed_slots).
(see the BABE_GENESIS_EPOCH_CONFIG in node runtime)

This is not technically correct since the c and allowed_slots may have changed during an epoch change;
Here we prefer to use the most up to date values on migration.


polkadot companion: paritytech/polkadot#5842

@davxy davxy self-assigned this Jun 30, 2022
@github-actions github-actions bot added the A3-in_progress Pull request is in progress. No review needed at this stage. label Jun 30, 2022
@davxy davxy added B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. and removed A3-in_progress Pull request is in progress. No review needed at this stage. labels Jun 30, 2022
@davxy davxy marked this pull request as ready for review June 30, 2022 15:01
@davxy davxy requested a review from andresilva as a code owner June 30, 2022 15:01
@davxy davxy requested a review from a team June 30, 2022 15:01
@github-actions github-actions bot added the A0-please_review Pull request needs code review. label Jun 30, 2022
@davxy davxy added the D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit label Jun 30, 2022
@davxy davxy changed the title Fetch babe config data from runtime state Fetch Babe configuration from runtime state Jun 30, 2022
@davxy davxy marked this pull request as draft July 1, 2022 08:57
@davxy davxy marked this pull request as ready for review July 1, 2022 09:27
Copy link
Contributor

@andresilva andresilva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All looks good! 👍

@andresilva
Copy link
Contributor

Could you please prepare a companion PR for polkadot to introduce the same runtime API changes?

@davxy
Copy link
Member Author

davxy commented Jul 31, 2022

Could you please prepare a companion PR for polkadot to introduce the same runtime API changes?

@andresilva done

@davxy
Copy link
Member Author

davxy commented Sep 5, 2022

bot merge

@paritytech-processbot
Copy link

Error: "Check reviews" status is not passing for paritytech/polkadot#5842

@bkchr
Copy link
Member

bkchr commented Sep 5, 2022

bot merge

@paritytech-processbot paritytech-processbot bot merged commit e24e9ad into master Sep 5, 2022
@paritytech-processbot paritytech-processbot bot deleted the davxy-babe-genesis-config branch September 5, 2022 17:41
ark0f pushed a commit to gear-tech/substrate that referenced this pull request Feb 27, 2023
* 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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants