Skip to content
Permalink
Browse files
use babe
  • Loading branch information
kaichaosun committed Jun 22, 2020
1 parent 84aa542 commit 610532546d9801f6ba46a6bd325bfbff29629919
Showing 6 changed files with 171 additions and 60 deletions.

Some generated files are not rendered by default. Learn more.

@@ -50,7 +50,7 @@ git = 'https://github.com/paritytech/substrate.git'
tag = 'v2.0.0-rc3'
version = '0.8.0-rc3'

[dependencies.sc-consensus-aura]
[dependencies.sc-consensus-babe]
git = 'https://github.com/paritytech/substrate.git'
tag = 'v2.0.0-rc3'
version = '0.8.0-rc3'
@@ -85,7 +85,7 @@ git = 'https://github.com/paritytech/substrate.git'
tag = 'v2.0.0-rc3'
version = '0.8.0-rc3'

[dependencies.sp-consensus-aura]
[dependencies.sp-consensus-babe]
git = 'https://github.com/paritytech/substrate.git'
tag = 'v2.0.0-rc3'
version = '0.8.0-rc3'
@@ -1,9 +1,9 @@
use sp_core::{Pair, Public, sr25519};
use node_template_runtime::{
AccountId, AuraConfig, BalancesConfig, GenesisConfig, GrandpaConfig,
AccountId, BabeConfig, BalancesConfig, GenesisConfig, GrandpaConfig,
SudoConfig, SystemConfig, WASM_BINARY, Signature
};
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_consensus_babe::{AuthorityId as BabeId};
use sp_finality_grandpa::AuthorityId as GrandpaId;
use sp_runtime::traits::{Verify, IdentifyAccount};
use sc_service::ChainType;
@@ -30,10 +30,12 @@ pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId where
AccountPublic::from(get_from_seed::<TPublic>(seed)).into_account()
}

/// Helper function to generate an authority key for Aura
pub fn authority_keys_from_seed(s: &str) -> (AuraId, GrandpaId) {
/// Helper function to generate an authority key for Babe
pub fn authority_keys_from_seed(s: &str) -> (AccountId, AccountId, BabeId, GrandpaId) {
(
get_from_seed::<AuraId>(s),
get_account_id_from_seed::<sr25519::Public>(&format!("{}//stash", s)),
get_account_id_from_seed::<sr25519::Public>(s),
get_from_seed::<BabeId>(s),
get_from_seed::<GrandpaId>(s),
)
}
@@ -99,7 +101,7 @@ pub fn local_testnet_config() -> ChainSpec {
)
}

fn testnet_genesis(initial_authorities: Vec<(AuraId, GrandpaId)>,
fn testnet_genesis(initial_authorities: Vec<(AccountId, AccountId, BabeId, GrandpaId)>,
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
_enable_println: bool) -> GenesisConfig {
@@ -111,11 +113,11 @@ fn testnet_genesis(initial_authorities: Vec<(AuraId, GrandpaId)>,
balances: Some(BalancesConfig {
balances: endowed_accounts.iter().cloned().map(|k|(k, 1 << 60)).collect(),
}),
aura: Some(AuraConfig {
authorities: initial_authorities.iter().map(|x| (x.0.clone())).collect(),
babe: Some(BabeConfig {
authorities: initial_authorities.iter().map(|x| (x.2.clone(), 1)).collect(),
}),
grandpa: Some(GrandpaConfig {
authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect(),
authorities: initial_authorities.iter().map(|x| (x.3.clone(), 1)).collect(),
}),
sudo: Some(SudoConfig {
key: root_key,
@@ -9,7 +9,6 @@ use sc_service::{error::{Error as ServiceError}, AbstractService, Configuration,
use sp_inherents::InherentDataProviders;
use sc_executor::native_executor_instance;
pub use sc_executor::NativeExecutor;
use sp_consensus_aura::sr25519::{AuthorityPair as AuraPair};
use sc_finality_grandpa::{
FinalityProofProvider as GrandpaFinalityProofProvider, StorageAndProofProvider, SharedVoterState,
};
@@ -28,7 +27,6 @@ native_executor_instance!(
macro_rules! new_full_start {
($config:expr) => {{
use std::sync::Arc;
use sp_consensus_aura::sr25519::AuthorityPair as AuraPair;

let mut import_setup = None;
let inherent_data_providers = sp_inherents::InherentDataProviders::new();
@@ -68,22 +66,25 @@ macro_rules! new_full_start {
select_chain,
)?;

let aura_block_import = sc_consensus_aura::AuraBlockImport::<_, _, _, AuraPair>::new(
grandpa_block_import.clone(), client.clone(),
);
let (block_import, babe_link) = sc_consensus_babe::block_import(
sc_consensus_babe::Config::get_or_compute(&*client)?,
grandpa_block_import.clone(),
client.clone(),
)?;

let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _>(
sc_consensus_aura::slot_duration(&*client)?,
aura_block_import,
Some(Box::new(grandpa_block_import.clone())),
let justification_import = grandpa_block_import.clone();
let import_queue = sc_consensus_babe::import_queue(
babe_link.clone(),
block_import.clone(),
Some(Box::new(justification_import)),
None,
client,
inherent_data_providers.clone(),
spawn_task_handle,
registry,
)?;

import_setup = Some((grandpa_block_import, grandpa_link));
import_setup = Some((block_import, grandpa_link, babe_link));

Ok(import_queue)
})?;
@@ -101,7 +102,7 @@ pub fn new_full(config: Configuration) -> Result<impl AbstractService, ServiceEr

let (builder, mut import_setup, inherent_data_providers) = new_full_start!(config);

let (block_import, grandpa_link) =
let (block_import, grandpa_link, babe_link) =
import_setup.take()
.expect("Link Half and Block Import are present for Full Services or setup failed before. qed");

@@ -127,22 +128,23 @@ pub fn new_full(config: Configuration) -> Result<impl AbstractService, ServiceEr
let can_author_with =
sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone());

let aura = sc_consensus_aura::start_aura::<_, _, _, _, _, AuraPair, _, _, _>(
sc_consensus_aura::slot_duration(&*client)?,
let babe_config = sc_consensus_babe::BabeParams {
keystore: service.keystore(),
client,
select_chain,
env: proposer,
block_import,
proposer,
service.network(),
inherent_data_providers.clone(),
sync_oracle: service.network(),
inherent_data_providers: inherent_data_providers.clone(),
force_authoring,
service.keystore(),
babe_link,
can_author_with,
)?;
};

// the AURA authoring task is considered essential, i.e. if it
let babe = sc_consensus_babe::start_babe(babe_config)?;
// the BABE authoring task is considered infallible, i.e. if it
// fails we take down the service with it.
service.spawn_essential_task("aura", aura);
service.spawn_essential_task("babe-proposer", babe);
}

// if the node isn't actively participating in consensus then it doesn't
@@ -246,12 +248,18 @@ pub fn new_light(config: Configuration) -> Result<impl AbstractService, ServiceE
let finality_proof_request_builder =
finality_proof_import.create_finality_proof_request_builder();

let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _>(
sc_consensus_aura::slot_duration(&*client)?,
let (babe_block_import, babe_link) = sc_consensus_babe::block_import(
sc_consensus_babe::Config::get_or_compute(&*client)?,
grandpa_block_import,
client.clone(),
)?;

let import_queue = sc_consensus_babe::import_queue(
babe_link,
babe_block_import,
None,
Some(Box::new(finality_proof_import)),
client,
client.clone(),
inherent_data_providers.clone(),
spawn_task_handle,
prometheus_registry,

0 comments on commit 6105325

Please sign in to comment.