Skip to content

Commit

Permalink
Check runtime for grandpa authorities if fast runtime flag enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwhit authored and Ada committed Aug 14, 2023
1 parent e14cbc6 commit 652873c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,4 @@ try-runtime = [
"try-runtime-cli/try-runtime",
"sp-io",
]
fast-runtime = ["creditcoin-node-runtime/fast-runtime"]
19 changes: 15 additions & 4 deletions node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,23 @@ pub(crate) fn new_partial(
.unwrap();

log::debug!("initial grandpa authorities: {initial_authorities:?}");
let auth_provider = consensus_switcher::GrandpaAuthorityProvider::new(
initial_authorities
let auth_provider = {
let grandpa_authorities = initial_authorities
.grandpa_initial_authorities
.clone()
.expect("No initial authorities configured for GRANDPA"),
);
.expect("No initial authorities configured for GRANDPA");
#[cfg(feature = "fast-runtime")]
{
consensus_switcher::GrandpaAuthorityProvider::with_client(
client.clone(),
grandpa_authorities,
)
}
#[cfg(not(feature = "fast-runtime"))]
{
consensus_switcher::GrandpaAuthorityProvider::new(grandpa_authorities)
}
};
let (grandpa_import, grandpa_link) = sc_consensus_grandpa::block_import(
client.clone(),
&auth_provider,
Expand Down
26 changes: 26 additions & 0 deletions node/src/service/consensus_switcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,42 @@ pub(crate) type OneshotSender<T> = tokio::sync::oneshot::Sender<T>;
pub(crate) type MpscSender<T> = tokio::sync::mpsc::Sender<T>;

pub(crate) struct GrandpaAuthorityProvider {
#[cfg(feature = "fast-runtime")]
client: Arc<FullClient>,
initial_authorities: Vec<sp_consensus_grandpa::AuthorityId>,
}
impl GrandpaAuthorityProvider {
#[cfg(not(feature = "fast-runtime"))]
pub(crate) fn new(initial_authorities: Vec<sp_consensus_grandpa::AuthorityId>) -> Self {
Self { initial_authorities }
}
#[cfg(feature = "fast-runtime")]
pub(crate) fn with_client(
client: Arc<FullClient>,
initial_authorities: Vec<sp_consensus_grandpa::AuthorityId>,
) -> Self {
Self { client, initial_authorities }
}
}

impl sc_consensus_grandpa::GenesisAuthoritySetProvider<BlockTy> for GrandpaAuthorityProvider {
fn get(&self) -> Result<runtime::GrandpaAuthorityList, sp_blockchain::Error> {
#[cfg(feature = "fast-runtime")]
{
use sc_client_api::UsageProvider;
use sp_api::ProvideRuntimeApi;
use sp_consensus_grandpa::GrandpaApi;
let at_hash = if self.client.usage_info().chain.finalized_state.is_some() {
self.client.usage_info().chain.best_hash
} else {
log::debug!("No finalized state is available. Reading config from genesis");
self.client.usage_info().chain.genesis_hash
};
if let Ok(authorities) = self.client.runtime_api().grandpa_authorities(at_hash) {
log::debug!("successfully read grandpa authorities from runtime");
return Ok(authorities);
}
}
if self.initial_authorities.is_empty() {
log::warn!("No initial grandpa authorities provided. Make sure this is configured correctly in the chain spec. Using a dummy authority for now.");
return Ok(vec![(
Expand Down

0 comments on commit 652873c

Please sign in to comment.