Refactor key management (#3296)

* Add Call type to extensible transactions.

Cleanup some naming

* Merge Resource and BlockExhausted into just Exhausted

* Fix

* Another fix

* Call

* Some fixes

* Fix srml tests.

* Fix all tests.

* Refactor crypto so each application of it has its own type.

* Introduce new AuthorityProvider API into Aura

This will eventually allow for dynamic determination of authority
keys and avoid having to set them directly on CLI.

* Introduce authority determinator for Babe.

Experiment with modular consensus API.

* Work in progress to introduce KeyTypeId and avoid polluting API
with validator IDs

* Finish up drafting imonline

* Rework offchain workers API.

* Rework API implementation.

* Make it compile for wasm, simplify app_crypto.

* Fix compilation of im-online.

* Fix compilation of im-online.

* Fix more compilation errors.

* Make it compile.

* Fixing tests.

* Rewrite `keystore`

* Fix session tests

* Bring back `TryFrom`'s'

* Fix `srml-grandpa`

* Fix `srml-aura`

* Fix consensus babe

* More fixes

* Make service generate keys from dev_seed

* Build fixes

* Remove offchain tests

* More fixes and cleanups

* Fixes finality grandpa

* Fix `consensus-aura`

* Fix cli

* Fix `node-cli`

* Fix chain_spec builder

* Fix doc tests

* Add authority getter for grandpa.

* Test fix

* Fixes

* Make keystore accessible from the runtime

* Move app crypto to its own crate

* Update `Cargo.lock`

* Make the crypto stuff usable from the runtime

* Adds some runtime crypto tests

* Use last finalized block for grandpa authority

* Fix warning

* Adds `SessionKeys` runtime api

* Remove `FinalityPair` and `ConsensusPair`

* Minor governance tweaks to get it inline with docs.

* Make the governance be up to date with the docs.

* Build fixes.

* Generate the inital session keys

* Failing keystore is a hard error

* Make babe work again

* Fix grandpa

* Fix tests

* Disable `keystore` in consensus critical stuff

* Build fix.

* ImOnline supports multiple authorities at once.

* Update core/application-crypto/src/ed25519.rs

* Merge branch 'master' into gav-in-progress

* Remove unneeded code for now.

* Some `session` testing

* Support querying the public keys

* Cleanup offchain

* Remove warnings

* More cleanup

* Apply suggestions from code review

Co-Authored-By: Benjamin Kampmann <ben.kampmann@googlemail.com>

* More cleanups

* JSONRPC API for setting keys.

Also, rename traits::KeyStore* -> traits::BareCryptoStore*

* Bad merge

* Fix integration tests

* Fix test build

* Test fix

* Fixes

* Warnings

* Another warning

* Bump version.
This commit is contained in:
Gavin Wood
2019-08-07 20:47:48 +02:00
committed by GitHub
parent a6a6779f01
commit 1a524b8207
160 changed files with 4467 additions and 2769 deletions
+61 -56
View File
@@ -16,9 +16,8 @@
//! Substrate chain configurations.
use babe_primitives::AuthorityId as BabeId;
use primitives::{ed25519, sr25519, Pair, crypto::UncheckedInto};
use node_primitives::{AccountId, Balance};
use primitives::{Pair, Public, crypto::UncheckedInto};
pub use node_primitives::{AccountId, Balance};
use node_runtime::{
BabeConfig, BalancesConfig, ContractsConfig, CouncilConfig, DemocracyConfig,
ElectionsConfig, GrandpaConfig, ImOnlineConfig, IndicesConfig, Perbill,
@@ -30,7 +29,9 @@ pub use node_runtime::GenesisConfig;
use substrate_service;
use hex_literal::hex;
use substrate_telemetry::TelemetryEndpoints;
use grandpa::AuthorityId as GrandpaId;
use grandpa_primitives::{AuthorityId as GrandpaId};
use babe_primitives::{AuthorityId as BabeId};
use im_online::AuthorityId as ImOnlineId;
const STAGING_TELEMETRY_URL: &str = "wss://telemetry.polkadot.io/submit/";
@@ -42,11 +43,8 @@ pub fn flaming_fir_config() -> Result<ChainSpec, String> {
ChainSpec::from_json_bytes(&include_bytes!("../res/flaming-fir.json")[..])
}
fn session_keys(ed_key: ed25519::Public, sr_key: sr25519::Public) -> SessionKeys {
SessionKeys {
ed25519: ed_key,
sr25519: sr_key,
}
fn session_keys(grandpa: GrandpaId, babe: BabeId, im_online: ImOnlineId) -> SessionKeys {
SessionKeys { grandpa, babe, im_online, }
}
fn staging_testnet_config_genesis() -> GenesisConfig {
@@ -56,7 +54,7 @@ fn staging_testnet_config_genesis() -> GenesisConfig {
// and
// for i in 1 2 3 4 ; do for j in session; do subkey --ed25519 inspect "$secret"//fir//$j//$i; done; done
let initial_authorities: Vec<(AccountId, AccountId, BabeId, GrandpaId)> = vec![(
let initial_authorities: Vec<(AccountId, AccountId, GrandpaId, BabeId, ImOnlineId)> = vec![(
// 5Fbsd6WXDGiLTxunqeK5BATNiocfCqu9bS1yArVjCgeBLkVy
hex!["9c7a2ee14e565db0c69f78c7b4cd839fbf52b607d867e9e9c5a79042898a0d12"].unchecked_into(),
// 5EnCiV7wSHeNhjW3FSUwiJNkcc2SBkPLn5Nj93FmbLtBjQUq
@@ -65,6 +63,8 @@ fn staging_testnet_config_genesis() -> GenesisConfig {
hex!["6e7e4eb42cbd2e0ab4cae8708ce5509580b8c04d11f6758dbf686d50fe9f9106"].unchecked_into(),
// 5Fb9ayurnxnaXj56CjmyQLBiadfRCqUbL2VWNbbe1nZU6wiC
hex!["9becad03e6dcac03cee07edebca5475314861492cdfc96a2144a67bbe9699332"].unchecked_into(),
// 5Fb9ayurnxnaXj56CjmyQLBiadfRCqUbL2VWNbbe1nZU6wiC
hex!["9becad03e6dcac03cee07edebca5475314861492cdfc96a2144a67bbe9699332"].unchecked_into(),
),(
// 5ERawXCzCWkjVq3xz1W5KGNtVx2VdefvZ62Bw1FEuZW4Vny2
hex!["68655684472b743e456907b398d3a44c113f189e56d1bbfd55e889e295dfde78"].unchecked_into(),
@@ -74,6 +74,8 @@ fn staging_testnet_config_genesis() -> GenesisConfig {
hex!["482dbd7297a39fa145c570552249c2ca9dd47e281f0c500c971b59c9dcdcd82e"].unchecked_into(),
// 5EockCXN6YkiNCDjpqqnbcqd4ad35nU4RmA1ikM4YeRN4WcE
hex!["7932cff431e748892fa48e10c63c17d30f80ca42e4de3921e641249cd7fa3c2f"].unchecked_into(),
// 5EockCXN6YkiNCDjpqqnbcqd4ad35nU4RmA1ikM4YeRN4WcE
hex!["7932cff431e748892fa48e10c63c17d30f80ca42e4de3921e641249cd7fa3c2f"].unchecked_into(),
),(
// 5DyVtKWPidondEu8iHZgi6Ffv9yrJJ1NDNLom3X9cTDi98qp
hex!["547ff0ab649283a7ae01dbc2eb73932eba2fb09075e9485ff369082a2ff38d65"].unchecked_into(),
@@ -83,6 +85,8 @@ fn staging_testnet_config_genesis() -> GenesisConfig {
hex!["482a3389a6cf42d8ed83888cfd920fec738ea30f97e44699ada7323f08c3380a"].unchecked_into(),
// 5E1jLYfLdUQKrFrtqoKgFrRvxM3oQPMbf6DfcsrugZZ5Bn8d
hex!["5633b70b80a6c8bb16270f82cca6d56b27ed7b76c8fd5af2986a25a4788ce440"].unchecked_into(),
// 5E1jLYfLdUQKrFrtqoKgFrRvxM3oQPMbf6DfcsrugZZ5Bn8d
hex!["5633b70b80a6c8bb16270f82cca6d56b27ed7b76c8fd5af2986a25a4788ce440"].unchecked_into(),
),(
// 5HYZnKWe5FVZQ33ZRJK1rG3WaLMztxWrrNDb1JRwaHHVWyP9
hex!["f26cdb14b5aec7b2789fd5ca80f979cef3761897ae1f37ffb3e154cbcc1c2663"].unchecked_into(),
@@ -92,6 +96,8 @@ fn staging_testnet_config_genesis() -> GenesisConfig {
hex!["00299981a2b92f878baaf5dbeba5c18d4e70f2a1fcd9c61b32ea18daf38f4378"].unchecked_into(),
// 5DMa31Hd5u1dwoRKgC4uvqyrdK45RHv3CpwvpUC1EzuwDit4
hex!["3919132b851ef0fd2dae42a7e734fe547af5a6b809006100f48944d7fae8e8ef"].unchecked_into(),
// 5DMa31Hd5u1dwoRKgC4uvqyrdK45RHv3CpwvpUC1EzuwDit4
hex!["3919132b851ef0fd2dae42a7e734fe547af5a6b809006100f48944d7fae8e8ef"].unchecked_into(),
)];
// generated with secret: subkey inspect "$secret"/fir
@@ -122,7 +128,7 @@ fn staging_testnet_config_genesis() -> GenesisConfig {
}),
session: Some(SessionConfig {
keys: initial_authorities.iter().map(|x| {
(x.0.clone(), session_keys(x.3.clone(), x.2.clone()))
(x.0.clone(), session_keys(x.2.clone(), x.3.clone(), x.4.clone()))
}).collect::<Vec<_>>(),
}),
staking: Some(StakingConfig {
@@ -159,15 +165,16 @@ fn staging_testnet_config_genesis() -> GenesisConfig {
key: endowed_accounts[0].clone(),
}),
babe: Some(BabeConfig {
authorities: initial_authorities.iter().map(|x| (x.2.clone(), 1)).collect(),
authorities: initial_authorities.iter().map(|x| (x.3.clone(), 1)).collect(),
}),
im_online: Some(ImOnlineConfig {
gossip_at: 0,
last_new_era_start: 0,
keys: initial_authorities.iter().map(|x| x.4.clone()).collect(),
}),
grandpa: Some(GrandpaConfig {
authorities: initial_authorities.iter().map(|x| (x.3.clone(), 1)).collect(),
authorities: initial_authorities.iter().map(|x| (x.2.clone(), 1)).collect(),
}),
membership_Instance1: Some(Default::default()),
}
}
@@ -186,58 +193,46 @@ pub fn staging_testnet_config() -> ChainSpec {
)
}
/// Helper function to generate AccountId from seed
pub fn get_account_id_from_seed(seed: &str) -> AccountId {
sr25519::Pair::from_string(&format!("//{}", seed), None)
/// Helper function to generate a crypto pair from seed
pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
TPublic::Pair::from_string(&format!("//{}", seed), None)
.expect("static values are valid; qed")
.public()
}
/// Helper function to generate BabeId from seed
pub fn get_babe_id_from_seed(seed: &str) -> BabeId {
sr25519::Pair::from_string(&format!("//{}", seed), None)
.expect("static values are valid; qed")
.public()
}
/// Helper function to generate GrandpaId from seed
pub fn get_grandpa_id_from_seed(seed: &str) -> GrandpaId {
ed25519::Pair::from_string(&format!("//{}", seed), None)
.expect("static values are valid; qed")
.public()
}
/// Helper function to generate stash, controller and session key from seed
pub fn get_authority_keys_from_seed(seed: &str) -> (AccountId, AccountId, BabeId, GrandpaId) {
pub fn get_authority_keys_from_seed(seed: &str) -> (AccountId, AccountId, GrandpaId, BabeId, ImOnlineId) {
(
get_account_id_from_seed(&format!("{}//stash", seed)),
get_account_id_from_seed(seed),
get_babe_id_from_seed(seed),
get_grandpa_id_from_seed(seed)
get_from_seed::<AccountId>(&format!("{}//stash", seed)),
get_from_seed::<AccountId>(seed),
get_from_seed::<GrandpaId>(seed),
get_from_seed::<BabeId>(seed),
get_from_seed::<ImOnlineId>(seed),
)
}
/// Helper function to create GenesisConfig for testing
pub fn testnet_genesis(
initial_authorities: Vec<(AccountId, AccountId, BabeId, GrandpaId)>,
initial_authorities: Vec<(AccountId, AccountId, GrandpaId, BabeId, ImOnlineId)>,
root_key: AccountId,
endowed_accounts: Option<Vec<AccountId>>,
enable_println: bool,
) -> GenesisConfig {
let endowed_accounts: Vec<AccountId> = endowed_accounts.unwrap_or_else(|| {
vec![
get_account_id_from_seed("Alice"),
get_account_id_from_seed("Bob"),
get_account_id_from_seed("Charlie"),
get_account_id_from_seed("Dave"),
get_account_id_from_seed("Eve"),
get_account_id_from_seed("Ferdie"),
get_account_id_from_seed("Alice//stash"),
get_account_id_from_seed("Bob//stash"),
get_account_id_from_seed("Charlie//stash"),
get_account_id_from_seed("Dave//stash"),
get_account_id_from_seed("Eve//stash"),
get_account_id_from_seed("Ferdie//stash"),
get_from_seed::<AccountId>("Alice"),
get_from_seed::<AccountId>("Bob"),
get_from_seed::<AccountId>("Charlie"),
get_from_seed::<AccountId>("Dave"),
get_from_seed::<AccountId>("Eve"),
get_from_seed::<AccountId>("Ferdie"),
get_from_seed::<AccountId>("Alice//stash"),
get_from_seed::<AccountId>("Bob//stash"),
get_from_seed::<AccountId>("Charlie//stash"),
get_from_seed::<AccountId>("Dave//stash"),
get_from_seed::<AccountId>("Eve//stash"),
get_from_seed::<AccountId>("Ferdie//stash"),
]
});
@@ -260,7 +255,7 @@ pub fn testnet_genesis(
}),
session: Some(SessionConfig {
keys: initial_authorities.iter().map(|x| {
(x.0.clone(), session_keys(x.3.clone(), x.2.clone()))
(x.0.clone(), session_keys(x.2.clone(), x.3.clone(), x.4.clone()))
}).collect::<Vec<_>>(),
}),
staking: Some(StakingConfig {
@@ -302,15 +297,16 @@ pub fn testnet_genesis(
key: root_key,
}),
babe: Some(BabeConfig {
authorities: initial_authorities.iter().map(|x| (x.2.clone(), 1)).collect(),
authorities: initial_authorities.iter().map(|x| (x.3.clone(), 1)).collect(),
}),
im_online: Some(ImOnlineConfig{
gossip_at: 0,
last_new_era_start: 0,
keys: initial_authorities.iter().map(|x| x.4.clone()).collect(),
}),
grandpa: Some(GrandpaConfig {
authorities: initial_authorities.iter().map(|x| (x.3.clone(), 1)).collect(),
authorities: initial_authorities.iter().map(|x| (x.2.clone(), 1)).collect(),
}),
membership_Instance1: Some(Default::default()),
}
}
@@ -319,7 +315,7 @@ fn development_config_genesis() -> GenesisConfig {
vec![
get_authority_keys_from_seed("Alice"),
],
get_account_id_from_seed("Alice"),
get_from_seed::<AccountId>("Alice"),
None,
true,
)
@@ -336,7 +332,7 @@ fn local_testnet_genesis() -> GenesisConfig {
get_authority_keys_from_seed("Alice"),
get_authority_keys_from_seed("Bob"),
],
get_account_id_from_seed("Alice"),
get_from_seed::<AccountId>("Alice"),
None,
false,
)
@@ -358,7 +354,7 @@ pub(crate) mod tests {
vec![
get_authority_keys_from_seed("Alice"),
],
get_account_id_from_seed("Alice"),
get_from_seed::<AccountId>("Alice"),
None,
false,
)
@@ -380,7 +376,16 @@ pub(crate) mod tests {
/// Local testnet config (multivalidator Alice + Bob)
pub fn integration_test_config_with_two_authorities() -> ChainSpec {
ChainSpec::from_genesis("Integration Test", "test", local_testnet_genesis, vec![], None, None, None, None)
ChainSpec::from_genesis(
"Integration Test",
"test",
local_testnet_genesis,
vec![],
None,
None,
None,
None,
)
}
#[test]
+53 -70
View File
@@ -22,12 +22,9 @@ use std::sync::Arc;
use std::time::Duration;
use babe::{import_queue, start_babe, BabeImportQueue, Config};
use babe_primitives::AuthorityPair as BabePair;
use client::{self, LongestChain};
use grandpa::{self, FinalityProofProvider as GrandpaFinalityProofProvider};
use node_executor;
use primitives::Pair;
use grandpa_primitives::AuthorityPair as GrandpaPair;
use futures::prelude::*;
use node_primitives::Block;
use node_runtime::{GenesisConfig, RuntimeApi};
@@ -40,7 +37,6 @@ use transaction_pool::{self, txpool::{Pool as TransactionPool}};
use inherents::InherentDataProviders;
use network::construct_simple_protocol;
use substrate_service::construct_service_factory;
use log::info;
use substrate_service::TelemetryOnConnect;
construct_simple_protocol! {
@@ -89,8 +85,6 @@ impl<F> Default for NodeConfig<F> where F: substrate_service::ServiceFactory {
construct_service_factory! {
struct Factory {
Block = Block,
ConsensusPair = BabePair,
FinalityPair = GrandpaPair,
RuntimeApi = RuntimeApi,
NetworkProtocol = NodeProtocol { |config| Ok(NodeProtocol::new()) },
RuntimeDispatch = node_executor::Executor,
@@ -119,74 +113,59 @@ construct_service_factory! {
}
}
if let Some(babe_key) = service.authority_key() {
info!("Using BABE key {}", babe_key.public());
let proposer = substrate_basic_authorship::ProposerFactory {
client: service.client(),
transaction_pool: service.transaction_pool(),
};
let client = service.client();
let select_chain = service.select_chain()
.ok_or(ServiceError::SelectChainRequired)?;
let babe_config = babe::BabeParams {
config: Config::get_or_compute(&*client)?,
local_key: Arc::new(babe_key),
client,
select_chain,
block_import,
env: proposer,
sync_oracle: service.network(),
inherent_data_providers: service.config.custom.inherent_data_providers.clone(),
force_authoring: service.config.force_authoring,
time_source: babe_link,
};
let babe = start_babe(babe_config)?;
let select = babe.select(service.on_exit()).then(|_| Ok(()));
service.spawn_task(Box::new(select));
}
let grandpa_key = if service.config.disable_grandpa {
None
} else {
service.fg_authority_key()
let proposer = substrate_basic_authorship::ProposerFactory {
client: service.client(),
transaction_pool: service.transaction_pool(),
};
let client = service.client();
let select_chain = service.select_chain().ok_or(ServiceError::SelectChainRequired)?;
let babe_config = babe::BabeParams {
config: Config::get_or_compute(&*client)?,
keystore: service.keystore(),
client,
select_chain,
block_import,
env: proposer,
sync_oracle: service.network(),
inherent_data_providers: service.config.custom.inherent_data_providers.clone(),
force_authoring: service.config.force_authoring,
time_source: babe_link,
};
let babe = start_babe(babe_config)?;
let select = babe.select(service.on_exit()).then(|_| Ok(()));
service.spawn_task(Box::new(select));
let config = grandpa::Config {
local_key: grandpa_key.map(Arc::new),
// FIXME #1578 make this available through chainspec
gossip_duration: Duration::from_millis(333),
justification_period: 4096,
name: Some(service.config.name.clone())
name: Some(service.config.name.clone()),
keystore: Some(service.keystore()),
};
match config.local_key {
None if !service.config.grandpa_voter => {
service.spawn_task(Box::new(grandpa::run_grandpa_observer(
config,
link_half,
service.network(),
service.on_exit(),
)?));
},
// Either config.local_key is set, or user forced voter service via `--grandpa-voter` flag.
_ => {
let telemetry_on_connect = TelemetryOnConnect {
telemetry_connection_sinks: service.telemetry_on_connect_stream(),
};
let grandpa_config = grandpa::GrandpaParams {
config: config,
link: link_half,
network: service.network(),
inherent_data_providers: service.config.custom.inherent_data_providers.clone(),
on_exit: service.on_exit(),
telemetry_on_connect: Some(telemetry_on_connect),
};
service.spawn_task(Box::new(grandpa::run_grandpa_voter(grandpa_config)?));
},
if service.config.roles.is_authority() {
let telemetry_on_connect = TelemetryOnConnect {
telemetry_connection_sinks: service.telemetry_on_connect_stream(),
};
let grandpa_config = grandpa::GrandpaParams {
config: config,
link: link_half,
network: service.network(),
inherent_data_providers: service.config.custom.inherent_data_providers.clone(),
on_exit: service.on_exit(),
telemetry_on_connect: Some(telemetry_on_connect),
};
service.spawn_task(Box::new(grandpa::run_grandpa_voter(grandpa_config)?));
} else if !service.config.grandpa_voter {
service.spawn_task(Box::new(grandpa::run_grandpa_observer(
config,
link_half,
service.network(),
service.on_exit(),
)?));
}
Ok(service)
@@ -273,7 +252,7 @@ mod tests {
use sr_primitives::{generic::{BlockId, Era, Digest}, traits::Block, OpaqueExtrinsic};
use timestamp;
use finality_tracker;
use keyring::{AccountKeyring, Sr25519Keyring};
use keyring::AccountKeyring;
use substrate_service::ServiceFactory;
use service_test::SyncService;
use crate::service::Factory;
@@ -339,9 +318,13 @@ mod tests {
#[test]
#[ignore]
fn test_sync() {
let keystore_path = tempfile::tempdir().expect("Creates keystore path");
let keystore = keystore::Store::open(keystore_path.path(), None).expect("Creates keystore");
let alice = keystore.write().insert_ephemeral_from_seed::<babe::AuthorityPair>("//Alice")
.expect("Creates authority pair");
let chain_spec = crate::chain_spec::tests::integration_test_config_with_single_authority();
let alice = Arc::new(Sr25519Keyring::Alice.pair());
let mut slot_num = 1u64;
let block_factory = |service: &SyncService<<Factory as ServiceFactory>::FullService>| {
let service = service.get();
@@ -370,8 +353,8 @@ mod tests {
&*service.client(),
&parent_id,
slot_num,
&alice,
(278, 1000),
&keystore,
) {
break babe_pre_digest;
}
@@ -395,7 +378,7 @@ mod tests {
let to_sign = pre_hash.encode();
let signature = alice.sign(&to_sign[..]);
let item = <DigestItem as CompatibleDigestItem>::babe_seal(
signature,
signature.into(),
);
slot_num += 1;