Skip to content

Commit

Permalink
use babe
Browse files Browse the repository at this point in the history
  • Loading branch information
kaichaosun committed Jun 22, 2020
1 parent 84aa542 commit 6105325
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 60 deletions.
93 changes: 83 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down
20 changes: 11 additions & 9 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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),
)
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
54 changes: 31 additions & 23 deletions node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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();
Expand Down Expand Up @@ -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)
})?;
Expand All @@ -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");

Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 6105325

Please sign in to comment.