Skip to content

Commit

Permalink
Merge pull request #3 from gleb-urvanov/feature/babe
Browse files Browse the repository at this point in the history
babe consensus its and dependencies introduced
  • Loading branch information
gleb-urvanov authored Oct 19, 2020
2 parents 41944df + e799eb2 commit 30f985a
Show file tree
Hide file tree
Showing 9 changed files with 795 additions and 82 deletions.
261 changes: 245 additions & 16 deletions Cargo.lock

Large diffs are not rendered by default.

27 changes: 25 additions & 2 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,34 @@ node-template-runtime = { path = '../runtime', version = '2.0.0' }
frame-benchmarking = '2.0.0'
frame-benchmarking-cli = '2.0.0'
pallet-transaction-payment-rpc = '2.0.0'
pallet-transaction-payment = '2.0.0'
sc-basic-authorship = '0.8.0'
sc-cli = { features = ['wasmtime'], version = '0.8.0' }
sc-client-api = '2.0.0'
sc-consensus = '0.8.0'
sc-consensus-aura = '0.8.0'
sc-consensus-babe = '0.8.0'
sp-consensus-babe = '0.8.0'
sp-authority-discovery = '2.0.0'
#grandpa-primitives = {package = 'sp-finality-grandpa', version = '2.0.0'}
sp-timestamp = '2.0.0'
sp-finality-tracker = '2.0.0'
sp-keyring = '2.0.0'
sp-io = '2.0.0'
sc-chain-spec = '2.0.0'
sc-network = '0.8.0'
#grandpa = {package = 'sc-finality-grandpa', version = '0.8.0'}
sc-client-db = '0.8.0'
sc-offchain = '2.0.0'
sc-tracing = '2.0.0'
sc-telemetry = '2.0.0'
sc-authority-discovery = '0.8.0'
frame-system = '2.0.0'
pallet-balances = '2.0.0'
frame-support = '2.0.0'
pallet-authority-discovery = '2.0.0'
pallet-staking = '2.0.0'
pallet-grandpa = '2.0.0'

sc-executor = { features = ['wasmtime'], version = '0.8.0' }
sc-finality-grandpa = '0.8.0'
sc-rpc = '2.0.0'
Expand All @@ -44,7 +67,6 @@ sp-api = '2.0.0'
sp-block-builder = '2.0.0'
sp-blockchain = '2.0.0'
sp-consensus = '0.8.0'
sp-consensus-aura = '0.8.0'
sp-core = '2.0.0'
sp-finality-grandpa = '2.0.0'
sp-inherents = '2.0.0'
Expand All @@ -53,6 +75,7 @@ sp-transaction-pool = '2.0.0'
substrate-frame-rpc-system = '2.0.0'
pallet-assets = { default-features = false, version = '2.0.0' }


[features]
default = []
runtime-benchmarks = ['node-template-runtime/runtime-benchmarks']
48 changes: 38 additions & 10 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use sp_core::{Pair, Public, sr25519};
use node_template_runtime::{
AccountId, AuraConfig, BalancesConfig, GenesisConfig, GrandpaConfig,
SudoConfig, SystemConfig, WASM_BINARY, Signature
AccountId, BabeConfig, BalancesConfig, GenesisConfig, GrandpaConfig, SessionConfig,
SudoConfig, SystemConfig, WASM_BINARY, Signature, StakerStatus, StakingConfig, SessionKeys
};
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 sp_runtime::{Perbill, traits::{Verify, IdentifyAccount}};
use sc_service::ChainType;

// The URL for the telemetry server.
Expand All @@ -31,13 +31,23 @@ pub fn get_account_id_from_seed<TPublic: Public>(seed: &str) -> AccountId where
}

/// Generate an Aura authority key.
pub fn authority_keys_from_seed(s: &str) -> (AuraId, GrandpaId) {
pub fn authority_keys_from_seed(s: &str) -> (BabeId, GrandpaId, AccountId) {
(
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),
get_account_id_from_seed::<sr25519::Public>(s),
)
}

fn session_keys(
grandpa: GrandpaId,
babe: BabeId
) -> SessionKeys {
SessionKeys { grandpa, babe }
}

pub fn development_config() -> Result<ChainSpec, String> {
let wasm_binary = WASM_BINARY.ok_or("Development wasm binary not available".to_string())?;

Expand Down Expand Up @@ -128,7 +138,7 @@ pub fn local_testnet_config() -> Result<ChainSpec, String> {
/// Configure initial storage state for FRAME modules.
fn testnet_genesis(
wasm_binary: &[u8],
initial_authorities: Vec<(AuraId, GrandpaId)>,
initial_authorities: Vec<(BabeId, GrandpaId, AccountId)>,
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
_enable_println: bool,
Expand All @@ -143,11 +153,29 @@ fn testnet_genesis(
// Configure endowed accounts with initial balance of 1 << 60.
balances: endowed_accounts.iter().cloned().map(|k|(k, 1 << 60)).collect(),
}),
pallet_aura: Some(AuraConfig {
authorities: initial_authorities.iter().map(|x| (x.0.clone())).collect(),
pallet_session: Some(SessionConfig {
keys: initial_authorities.iter().map(|x| {
(x.2.clone(), x.2.clone(), session_keys(
x.1.clone(),
x.0.clone(),
))
}).collect::<Vec<_>>(),
}),
pallet_staking: Some(StakingConfig {
validator_count: initial_authorities.len() as u32 * 2,
minimum_validator_count: initial_authorities.len() as u32,
stakers: initial_authorities.iter().map(|x| {
(x.2.clone(), x.2.clone(), 0_u128, StakerStatus::Validator)
}).collect(),
invulnerables: initial_authorities.iter().map(|x| x.2.clone()).collect(),
slash_reward_fraction: Perbill::from_percent(10),
.. Default::default()
}),
pallet_babe: Some(BabeConfig {
authorities: vec![],
}),
pallet_grandpa: Some(GrandpaConfig {
authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect(),
authorities: vec![],
}),
pallet_sudo: Some(SudoConfig {
// Assign network admin rights.
Expand Down
82 changes: 50 additions & 32 deletions node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use sc_service::{error::Error as ServiceError, Configuration, TaskManager};
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_consensus_babe;
use sc_finality_grandpa::{FinalityProofProvider as GrandpaFinalityProofProvider, SharedVoterState};

// Our native executor instance.
Expand All @@ -22,19 +22,17 @@ native_executor_instance!(
type FullClient = sc_service::TFullClient<Block, RuntimeApi, Executor>;
type FullBackend = sc_service::TFullBackend<Block>;
type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;
type FullGrandpaBlockImport =
sc_finality_grandpa::GrandpaBlockImport<FullBackend, Block, FullClient, FullSelectChain>;

pub fn new_partial(config: &Configuration) -> Result<sc_service::PartialComponents<
FullClient, FullBackend, FullSelectChain,
sp_consensus::DefaultImportQueue<Block, FullClient>,
sc_transaction_pool::FullPool<Block, FullClient>,
(
sc_consensus_aura::AuraBlockImport<
Block,
FullClient,
sc_finality_grandpa::GrandpaBlockImport<FullBackend, Block, FullClient, FullSelectChain>,
AuraPair
>,
sc_finality_grandpa::LinkHalf<Block, FullClient, FullSelectChain>
sc_consensus_babe::BabeBlockImport<Block, FullClient, FullGrandpaBlockImport>,
sc_finality_grandpa::LinkHalf<Block, FullClient, FullSelectChain>,
sc_consensus_babe::BabeLink<Block>,
)
>, ServiceError> {
let inherent_data_providers = sp_inherents::InherentDataProviders::new();
Expand All @@ -56,26 +54,33 @@ pub fn new_partial(config: &Configuration) -> Result<sc_service::PartialComponen
client.clone(), &(client.clone() as Arc<_>), select_chain.clone(),
)?;

let aura_block_import = sc_consensus_aura::AuraBlockImport::<_, _, _, AuraPair>::new(
grandpa_block_import.clone(), client.clone(),
);
let justification_import = grandpa_block_import.clone();

let (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_aura::import_queue::<_, _, _, AuraPair, _, _>(
sc_consensus_aura::slot_duration(&*client)?,
aura_block_import.clone(),
Some(Box::new(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.clone(),
select_chain.clone(),
inherent_data_providers.clone(),
&task_manager.spawn_handle(),
config.prometheus_registry(),
sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone()),
)?;

let import_setup = (block_import, grandpa_link, babe_link);

Ok(sc_service::PartialComponents {
client, backend, task_manager, import_queue, keystore, select_chain, transaction_pool,
client, backend, task_manager, keystore, select_chain, import_queue, transaction_pool,
inherent_data_providers,
other: (aura_block_import, grandpa_link),
other: (import_setup)
})
}

Expand All @@ -84,7 +89,7 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
let sc_service::PartialComponents {
client, backend, mut task_manager, import_queue, keystore, select_chain, transaction_pool,
inherent_data_providers,
other: (block_import, grandpa_link),
other: (import_setup),
} = new_partial(&config)?;

let finality_proof_provider =
Expand Down Expand Up @@ -116,6 +121,8 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
let prometheus_registry = config.prometheus_registry().cloned();
let telemetry_connection_sinks = sc_service::TelemetryConnectionSinks::default();

let (block_import, grandpa_link, babe_link) = import_setup;

let rpc_extensions_builder = {
let client = client.clone();
let pool = transaction_pool.clone();
Expand Down Expand Up @@ -154,22 +161,22 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
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)?,
client.clone(),
let babe_config = sc_consensus_babe::BabeParams {
keystore: keystore.clone(),
client: client.clone(),
select_chain,
env: proposer,
block_import,
proposer,
network.clone(),
inherent_data_providers.clone(),
sync_oracle: network.clone(),
inherent_data_providers: inherent_data_providers.clone(),
force_authoring,
keystore.clone(),
babe_link,
can_author_with,
)?;
};

let babe = sc_consensus_babe::start_babe(babe_config)?;

// the AURA authoring task is considered essential, i.e. if it
// fails we take down the service with it.
task_manager.spawn_essential_handle().spawn_blocking("aura", aura);
task_manager.spawn_essential_handle().spawn_blocking("babe-proposer", babe);
}

// if the node isn't actively participating in consensus then it doesn't
Expand Down Expand Up @@ -231,6 +238,8 @@ pub fn new_light(config: Configuration) -> Result<TaskManager, ServiceError> {
let (client, backend, keystore, mut task_manager, on_demand) =
sc_service::new_light_parts::<Block, RuntimeApi, Executor>(&config)?;

let select_chain = sc_consensus::LongestChain::new(backend.clone());

let transaction_pool = Arc::new(sc_transaction_pool::BasicPool::new_light(
config.transaction_pool.clone(),
config.prometheus_registry(),
Expand All @@ -247,13 +256,22 @@ pub fn new_light(config: Configuration) -> Result<TaskManager, ServiceError> {
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 inherent_data_providers = sp_inherents::InherentDataProviders::new();

let import_queue = sc_consensus_babe::import_queue(
babe_link,
babe_block_import,
None,
Some(Box::new(finality_proof_import)),
client.clone(),
InherentDataProviders::new(),
select_chain.clone(),
inherent_data_providers.clone(),
&task_manager.spawn_handle(),
config.prometheus_registry(),
sp_consensus::NeverCanAuthor,
Expand Down
16 changes: 12 additions & 4 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ frame-support = { default-features = false, version = '2.0.0' }
frame-system = { default-features = false, version = '2.0.0' }
frame-system-benchmarking = { default-features = false, optional = true, version = '2.0.0' }
frame-system-rpc-runtime-api = { default-features = false, version = '2.0.0' }
pallet-aura = { default-features = false, version = '2.0.0' }
pallet-babe = { default-features = false, version = "2.0.0" }
pallet-offences = { default-features = false, version = "2.0.0" }
pallet-session = { default-features = false, version = "2.0.0" }
pallet-staking = { default-features = false, version = "2.0.0" }
pallet-staking-reward-curve = { default-features = false, version = "2.0.0" }
pallet-authorship = { default-features = false, version = "2.0.0" }
pallet-balances = { default-features = false, version = '2.0.0' }
pallet-grandpa = { default-features = false, version = '2.0.0' }
pallet-randomness-collective-flip = { default-features = false, version = '2.0.0' }
Expand All @@ -46,7 +51,7 @@ pallet-transaction-payment = { default-features = false, version = '2.0.0' }
pallet-transaction-payment-rpc-runtime-api = { default-features = false, version = '2.0.0' }
sp-api = { default-features = false, version = '2.0.0' }
sp-block-builder = { default-features = false, version = '2.0.0' }
sp-consensus-aura = { default-features = false, version = '0.8.0' }
sp-consensus-babe = { default-features = false, version = "0.8.0" }
sp-core = { default-features = false, version = '2.0.0' }
sp-inherents = { default-features = false, version = '2.0.0' }
sp-offchain = { default-features = false, version = '2.0.0' }
Expand Down Expand Up @@ -75,7 +80,11 @@ std = [
'frame-support/std',
'frame-system/std',
'frame-system-rpc-runtime-api/std',
'pallet-aura/std',
"sp-consensus-babe/std",
"pallet-babe/std",
"pallet-offences/std",
"pallet-staking/std",
"pallet-authorship/std",
'pallet-balances/std',
'pallet-grandpa/std',
'pallet-randomness-collective-flip/std',
Expand All @@ -86,7 +95,6 @@ std = [
'pallet-transaction-payment-rpc-runtime-api/std',
'sp-api/std',
'sp-block-builder/std',
'sp-consensus-aura/std',
'sp-core/std',
'sp-inherents/std',
'sp-offchain/std',
Expand Down
Loading

0 comments on commit 30f985a

Please sign in to comment.