Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Trying to add grandpa back
  • Loading branch information
shawntabrizi committed Oct 16, 2019
commit d87ed073977763c17ac33bee1271e8181af6c82e
3 changes: 3 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions node-template/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ transaction-pool = { package = "substrate-transaction-pool", path = "../core/tra
network = { package = "substrate-network", path = "../core/network" }
aura = { package = "substrate-consensus-aura", path = "../core/consensus/aura" }
aura-primitives = { package = "substrate-consensus-aura-primitives", path = "../core/consensus/aura/primitives" }
grandpa = { package = "substrate-finality-grandpa", path = "../core/finality-grandpa" }
grandpa-primitives = { package = "substrate-finality-grandpa-primitives", path = "../core/finality-grandpa/primitives" }
substrate-client = { path = "../core/client" }
basic-authorship = { package = "substrate-basic-authorship", path = "../core/basic-authorship" }
node-template-runtime = { path = "runtime" }
Expand Down
2 changes: 2 additions & 0 deletions node-template/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ substrate-session = { path = "../../core/session", default-features = false }
balances = { package = "srml-balances", path = "../../srml/balances", default_features = false }
aura = { package = "srml-aura", path = "../../srml/aura", default_features = false }
aura-primitives = { package = "substrate-consensus-aura-primitives", path = "../../core/consensus/aura/primitives", default_features = false }
grandpa = { package = "srml-grandpa", path = "../../srml/grandpa", default_features = false }
executive = { package = "srml-executive", path = "../../srml/executive", default_features = false }
indices = { package = "srml-indices", path = "../../srml/indices", default_features = false }
randomness-collective-flip = { package = "srml-randomness-collective-flip", path = "../../srml/randomness-collective-flip", default_features = false }
Expand All @@ -41,6 +42,7 @@ std = [
"balances/std",
"aura/std",
"aura-primitives/std",
'grandpa/std',
"executive/std",
"indices/std",
"primitives/std",
Expand Down
15 changes: 15 additions & 0 deletions node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use client::{
runtime_api as client_api, impl_runtime_apis
};
use aura_primitives::sr25519::AuthorityId as AuraId;
use grandpa::{AuthorityId as GrandpaId, AuthorityWeight as GrandpaWeight};
use grandpa::fg_primitives;
use version::RuntimeVersion;
#[cfg(feature = "std")]
use version::NativeVersion;
Expand Down Expand Up @@ -82,6 +84,8 @@ pub mod opaque {
pub struct SessionKeys {
#[id(key_types::AURA)]
pub aura: AuraId,
#[id(key_types::GRANDPA)]
pub grandpa: GrandpaId,
}
}
}
Expand Down Expand Up @@ -165,6 +169,10 @@ impl aura::Trait for Runtime {
type AuthorityId = AuraId;
}

impl grandpa::Trait for Runtime {
type Event = Event;
}

impl indices::Trait for Runtime {
/// The type for recording indexing into the account enumeration. If this ever overflows, there
/// will be problems!
Expand Down Expand Up @@ -235,6 +243,7 @@ construct_runtime!(
System: system::{Module, Call, Storage, Config, Event},
Timestamp: timestamp::{Module, Call, Storage, Inherent},
Aura: aura::{Module, Config<T>, Inherent(Timestamp)},
Grandpa: grandpa::{Module, Call, Storage, Config, Event},
Indices: indices::{default, Config<T>},
Balances: balances::{default, Error},
Sudo: sudo,
Expand Down Expand Up @@ -341,4 +350,10 @@ impl_runtime_apis! {
opaque::SessionKeys::generate(seed)
}
}

impl fg_primitives::GrandpaApi<Block> for Runtime {
fn grandpa_authorities() -> Vec<(GrandpaId, GrandpaWeight)> {
Grandpa::grandpa_authorities()
}
}
}
29 changes: 18 additions & 11 deletions node-template/src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use primitives::{Pair, Public};
use node_template_runtime::{
AccountId, AuraConfig, BalancesConfig, GenesisConfig,
AccountId, AuraConfig, BalancesConfig, GenesisConfig, GrandpaConfig,
SudoConfig, IndicesConfig, SystemConfig, WASM_BINARY,
};
use aura_primitives::sr25519::{AuthorityId as AuraId, AuthorityPair as AuraPair};
use grandpa_primitives::{AuthorityId as GrandpaId};
use substrate_service;

// Note this is the URL for the telemetry server
Expand Down Expand Up @@ -31,10 +32,13 @@ pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Pu
}

/// Helper function to generate an authority key for Aura
pub fn get_authority_key_from_seed(s: &str) -> AuraId {
AuraPair::from_string(&format!("//{}", s), None)
.expect("static values are valid; qed")
.public()
pub fn get_authority_keys_from_seed(s: &str) -> (AuraId, GrandpaId) {
(
AuraPair::from_string(&format!("//{}", s), None)
.expect("static values are valid; qed")
.public(),
get_from_seed::<GrandpaId>(s),
)
}

impl Alternative {
Expand All @@ -45,7 +49,7 @@ impl Alternative {
"Development",
"dev",
|| testnet_genesis(vec![
get_authority_key_from_seed("Alice"),
get_authority_keys_from_seed("Alice"),
],
get_from_seed::<AccountId>("Alice"),
vec![
Expand All @@ -65,8 +69,8 @@ impl Alternative {
"Local Testnet",
"local_testnet",
|| testnet_genesis(vec![
get_authority_key_from_seed("Alice"),
get_authority_key_from_seed("Bob"),
get_authority_keys_from_seed("Alice"),
get_authority_keys_from_seed("Bob"),
],
get_from_seed::<AccountId>("Alice"),
vec![
Expand Down Expand Up @@ -102,7 +106,7 @@ impl Alternative {
}
}

fn testnet_genesis(initial_authorities: Vec<AuraId>,
fn testnet_genesis(initial_authorities: Vec<(AuraId, GrandpaId)>,
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
_enable_println: bool) -> GenesisConfig {
Expand All @@ -122,7 +126,10 @@ fn testnet_genesis(initial_authorities: Vec<AuraId>,
key: root_key,
}),
aura: Some(AuraConfig {
authorities: initial_authorities.clone(),
})
authorities: initial_authorities.iter().map(|x| (x.0.clone())).collect(),
}),
grandpa: Some(GrandpaConfig {
authorities: initial_authorities.iter().map(|x| (x.1.clone(), 1)).collect(),
}),
}
}
101 changes: 88 additions & 13 deletions node-template/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
//! Service and ServiceFactory implementation. Specialized wrapper over substrate service.

use std::sync::Arc;
use std::time::Duration;
use substrate_client::LongestChain;
use futures::prelude::*;
use node_template_runtime::{self, GenesisConfig, opaque::Block, RuntimeApi};
use substrate_service::{error::{Error as ServiceError}, AbstractService, Configuration, ServiceBuilder};
use transaction_pool::{self, txpool::{Pool as TransactionPool}};
use inherents::InherentDataProviders;
use network::{construct_simple_protocol, config::DummyFinalityProofRequestBuilder};
use network::{construct_simple_protocol};
use substrate_executor::native_executor_instance;
pub use substrate_executor::NativeExecutor;
use aura_primitives::sr25519::{AuthorityPair as AuraPair};
use grandpa::{self, FinalityProofProvider as GrandpaFinalityProofProvider};

// Our native executor instance.
native_executor_instance!(
Expand All @@ -29,6 +32,7 @@ construct_simple_protocol! {
/// be able to perform chain operations.
macro_rules! new_full_start {
($config:expr) => {{
let mut import_setup = None;
let inherent_data_providers = inherents::InherentDataProviders::new();

let builder = substrate_service::ServiceBuilder::new_full::<
Expand All @@ -40,19 +44,31 @@ macro_rules! new_full_start {
.with_transaction_pool(|config, client|
Ok(transaction_pool::txpool::Pool::new(config, transaction_pool::FullChainApi::new(client)))
)?
.with_import_queue(|_config, client, _select_chain, transaction_pool| {
aura::import_queue::<_, _, AuraPair, _>(
.with_import_queue(|_config, client, mut select_chain, transaction_pool| {
let select_chain = select_chain.take()
.ok_or_else(|| substrate_service::Error::SelectChainRequired)?;

let (grandpa_block_import, grandpa_link) =
grandpa::block_import::<_, _, _, node_template_runtime::RuntimeApi, _, _>(
client.clone(), &*client, select_chain
)?;

let import_queue = aura::import_queue::<_, _, AuraPair, _>(
aura::SlotDuration::get_or_compute(&*client)?,
Box::new(client.clone()),
None,
Some(Box::new(grandpa_block_import)),
None,
client,
inherent_data_providers.clone(),
Some(transaction_pool),
).map_err(Into::into)
)?;

import_setup = Some(grandpa_link);

Ok(import_queue)
})?;

(builder, inherent_data_providers)
(builder, import_setup, inherent_data_providers)
}}
}

Expand All @@ -62,11 +78,19 @@ pub fn new_full<C: Send + Default + 'static>(config: Configuration<C, GenesisCon
{
let is_authority = config.roles.is_authority();
let force_authoring = config.force_authoring;
let name = config.name.clone();
let disable_grandpa = config.disable_grandpa;

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

let (builder, inherent_data_providers) = new_full_start!(config);
let grandpa_link =
import_setup.take()
.expect("Link is present for Full Services or setup failed before. qed");

let service = builder.with_network_protocol(|_| Ok(NodeProtocol::new()))?
.with_opt_finality_proof_provider(|_, _| Ok(None))?
.with_finality_proof_provider(|client, backend|
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, client)) as _)
)?
.build()?;

if is_authority {
Expand Down Expand Up @@ -98,6 +122,48 @@ pub fn new_full<C: Send + Default + 'static>(config: Configuration<C, GenesisCon
service.spawn_essential_task(select);
}

let grandpa_config = grandpa::Config {
// FIXME #1578 make this available through chainspec
gossip_duration: Duration::from_millis(333),
justification_period: 512,
name: Some(name),
keystore: Some(service.keystore()),
};

match (is_authority, disable_grandpa) {
(false, false) => {
// start the lightweight GRANDPA observer
service.spawn_task(Box::new(grandpa::run_grandpa_observer(
grandpa_config,
grandpa_link,
service.network(),
service.on_exit(),
)?));
},
(true, false) => {
// start the full GRANDPA voter
let voter_config = grandpa::GrandpaParams {
config: grandpa_config,
link: grandpa_link,
network: service.network(),
inherent_data_providers: inherent_data_providers.clone(),
on_exit: service.on_exit(),
telemetry_on_connect: Some(service.telemetry_on_connect_stream()),
};

// the GRANDPA voter task is considered infallible, i.e.
// if it fails we take down the service with it.
service.spawn_essential_task(grandpa::run_grandpa_voter(voter_config)?);
},
(_, true) => {
grandpa::setup_disabled_grandpa(
service.client(),
&inherent_data_providers,
service.network(),
)?;
},
}

Ok(service)
}

Expand All @@ -114,13 +180,22 @@ pub fn new_light<C: Send + Default + 'static>(config: Configuration<C, GenesisCo
.with_transaction_pool(|config, client|
Ok(TransactionPool::new(config, transaction_pool::FullChainApi::new(client)))
)?
.with_import_queue_and_fprb(|_config, client, _backend, _fetcher, _select_chain, _tx_pool| {
let finality_proof_request_builder = Box::new(DummyFinalityProofRequestBuilder::default()) as Box<_>;
.with_import_queue_and_fprb(|_config, client, backend, fetcher, _select_chain, _tx_pool| {
let fetch_checker = fetcher
.map(|fetcher| fetcher.checker().clone())
.ok_or_else(|| "Trying to start light import queue without active fetch checker")?;
let grandpa_block_import = grandpa::light_block_import::<_, _, _, RuntimeApi, _>(
client.clone(), backend, Arc::new(fetch_checker), client.clone()
)?;
let finality_proof_import = grandpa_block_import.clone();
let finality_proof_request_builder =
finality_proof_import.create_finality_proof_request_builder();

let import_queue = aura::import_queue::<_, _, AuraPair, ()>(
aura::SlotDuration::get_or_compute(&*client)?,
Box::new(client.clone()),
None,
None,
Some(Box::new(finality_proof_import)),
client,
inherent_data_providers.clone(),
None,
Expand All @@ -129,8 +204,8 @@ pub fn new_light<C: Send + Default + 'static>(config: Configuration<C, GenesisCo
Ok((import_queue, finality_proof_request_builder))
})?
.with_network_protocol(|_| Ok(NodeProtocol::new()))?
.with_opt_finality_proof_provider(|_client, _backend|
Ok(None)
.with_finality_proof_provider(|client, backend|
Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, client)) as _)
)?
.build()
}