[Companion #13615] Keystore overhaul (#6892)

* Remove not required async calls

* Fixed missing renaming

* make_keystore can be sync

* More fixes

* Trivial nitpicks

* Cherry pick test fix from master

* Fixes after master merge

* update lockfile for {"substrate"}

---------

Co-authored-by: parity-processbot <>
This commit is contained in:
Davide Galassi
2023-03-17 13:09:15 +01:00
committed by GitHub
parent 4d904951fd
commit 46c36e5a4f
38 changed files with 546 additions and 648 deletions
+17 -25
View File
@@ -51,7 +51,7 @@ use polkadot_primitives::{
pub use rand;
use sp_application_crypto::AppKey;
use sp_core::ByteArray;
use sp_keystore::{CryptoStore, Error as KeystoreError, SyncCryptoStorePtr};
use sp_keystore::{Error as KeystoreError, Keystore, KeystorePtr};
use std::time::Duration;
use thiserror::Error;
@@ -265,21 +265,18 @@ pub async fn executor_params_at_relay_parent(
}
/// From the given set of validators, find the first key we can sign with, if any.
pub async fn signing_key(
validators: &[ValidatorId],
keystore: &SyncCryptoStorePtr,
) -> Option<ValidatorId> {
signing_key_and_index(validators, keystore).await.map(|(k, _)| k)
pub fn signing_key(validators: &[ValidatorId], keystore: &KeystorePtr) -> Option<ValidatorId> {
signing_key_and_index(validators, keystore).map(|(k, _)| k)
}
/// From the given set of validators, find the first key we can sign with, if any, and return it
/// along with the validator index.
pub async fn signing_key_and_index(
pub fn signing_key_and_index(
validators: &[ValidatorId],
keystore: &SyncCryptoStorePtr,
keystore: &KeystorePtr,
) -> Option<(ValidatorId, ValidatorIndex)> {
for (i, v) in validators.iter().enumerate() {
if CryptoStore::has_keys(&**keystore, &[(v.to_raw_vec(), ValidatorId::ID)]).await {
if Keystore::has_keys(&**keystore, &[(v.to_raw_vec(), ValidatorId::ID)]) {
return Some((v.clone(), ValidatorIndex(i as _)))
}
}
@@ -290,13 +287,12 @@ pub async fn signing_key_and_index(
///
/// Returns `Ok(None)` if the private key that correponds to that validator ID is not found in the
/// given keystore. Returns an error if the key could not be used for signing.
pub async fn sign(
keystore: &SyncCryptoStorePtr,
pub fn sign(
keystore: &KeystorePtr,
key: &ValidatorId,
data: &[u8],
) -> Result<Option<ValidatorSignature>, KeystoreError> {
let signature =
CryptoStore::sign_with(&**keystore, ValidatorId::ID, &key.into(), &data).await?;
let signature = Keystore::sign_with(&**keystore, ValidatorId::ID, &key.into(), &data)?;
match signature {
Some(sig) =>
@@ -372,11 +368,7 @@ pub struct Validator {
impl Validator {
/// Get a struct representing this node's validator if this node is in fact a validator in the context of the given block.
pub async fn new<S>(
parent: Hash,
keystore: SyncCryptoStorePtr,
sender: &mut S,
) -> Result<Self, Error>
pub async fn new<S>(parent: Hash, keystore: KeystorePtr, sender: &mut S) -> Result<Self, Error>
where
S: SubsystemSender<RuntimeApiMessage>,
{
@@ -392,19 +384,19 @@ impl Validator {
let validators = validators?;
Self::construct(&validators, signing_context, keystore).await
Self::construct(&validators, signing_context, keystore)
}
/// Construct a validator instance without performing runtime fetches.
///
/// This can be useful if external code also needs the same data.
pub async fn construct(
pub fn construct(
validators: &[ValidatorId],
signing_context: SigningContext,
keystore: SyncCryptoStorePtr,
keystore: KeystorePtr,
) -> Result<Self, Error> {
let (key, index) =
signing_key_and_index(validators, &keystore).await.ok_or(Error::NotAValidator)?;
signing_key_and_index(validators, &keystore).ok_or(Error::NotAValidator)?;
Ok(Validator { signing_context, key, index })
}
@@ -425,11 +417,11 @@ impl Validator {
}
/// Sign a payload with this validator
pub async fn sign<Payload: EncodeAs<RealPayload>, RealPayload: Encode>(
pub fn sign<Payload: EncodeAs<RealPayload>, RealPayload: Encode>(
&self,
keystore: SyncCryptoStorePtr,
keystore: KeystorePtr,
payload: Payload,
) -> Result<Option<Signed<Payload, RealPayload>>, KeystoreError> {
Signed::sign(&keystore, payload, &self.signing_context, self.index, &self.key).await
Signed::sign(&keystore, payload, &self.signing_context, self.index, &self.key)
}
}
@@ -23,7 +23,7 @@ use lru::LruCache;
use parity_scale_codec::Encode;
use sp_application_crypto::AppKey;
use sp_core::crypto::ByteArray;
use sp_keystore::{CryptoStore, SyncCryptoStorePtr};
use sp_keystore::{Keystore, KeystorePtr};
use polkadot_node_subsystem::{messages::RuntimeApiMessage, overseer, SubsystemSender};
use polkadot_primitives::{
@@ -49,7 +49,7 @@ pub struct Config {
/// Needed for retrieval of `ValidatorInfo`
///
/// Pass `None` if you are not interested.
pub keystore: Option<SyncCryptoStorePtr>,
pub keystore: Option<KeystorePtr>,
/// How many sessions should we keep in the cache?
pub session_cache_lru_size: NonZeroUsize,
@@ -69,7 +69,7 @@ pub struct RuntimeInfo {
session_info_cache: LruCache<SessionIndex, ExtendedSessionInfo>,
/// Key store for determining whether we are a validator and what `ValidatorIndex` we have.
keystore: Option<SyncCryptoStorePtr>,
keystore: Option<KeystorePtr>,
}
/// `SessionInfo` with additional useful data for validator nodes.
@@ -102,7 +102,7 @@ impl Default for Config {
impl RuntimeInfo {
/// Create a new `RuntimeInfo` for convenient runtime fetches.
pub fn new(keystore: Option<SyncCryptoStorePtr>) -> Self {
pub fn new(keystore: Option<KeystorePtr>) -> Self {
Self::new_with_config(Config { keystore, ..Default::default() })
}
@@ -171,7 +171,7 @@ impl RuntimeInfo {
recv_runtime(request_session_info(parent, session_index, sender).await)
.await?
.ok_or(JfyiError::NoSuchSession(session_index))?;
let validator_info = self.get_validator_info(&session_info).await?;
let validator_info = self.get_validator_info(&session_info)?;
let full_info = ExtendedSessionInfo { session_info, validator_info };
@@ -206,8 +206,8 @@ impl RuntimeInfo {
///
///
/// Returns: `None` if not a parachain validator.
async fn get_validator_info(&self, session_info: &SessionInfo) -> Result<ValidatorInfo> {
if let Some(our_index) = self.get_our_index(&session_info.validators).await {
fn get_validator_info(&self, session_info: &SessionInfo) -> Result<ValidatorInfo> {
if let Some(our_index) = self.get_our_index(&session_info.validators) {
// Get our group index:
let our_group =
session_info.validator_groups.iter().enumerate().find_map(|(i, g)| {
@@ -228,13 +228,13 @@ impl RuntimeInfo {
/// Get our `ValidatorIndex`.
///
/// Returns: None if we are not a validator.
async fn get_our_index(
fn get_our_index(
&self,
validators: &IndexedVec<ValidatorIndex, ValidatorId>,
) -> Option<ValidatorIndex> {
let keystore = self.keystore.as_ref()?;
for (i, v) in validators.iter().enumerate() {
if CryptoStore::has_keys(&**keystore, &[(v.to_raw_vec(), ValidatorId::ID)]).await {
if Keystore::has_keys(&**keystore, &[(v.to_raw_vec(), ValidatorId::ID)]) {
return Some(ValidatorIndex(i as u32))
}
}