[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
@@ -28,7 +28,6 @@ use crate::{
};
use assert_matches::assert_matches;
use frame_support::assert_noop;
use futures::executor::block_on;
use keyring::Sr25519Keyring;
use primitives::{
BlockNumber, CandidateCommitments, CandidateDescriptor, CollatorId,
@@ -37,7 +36,7 @@ use primitives::{
PARACHAIN_KEY_TYPE_ID,
};
use sc_keystore::LocalKeystore;
use sp_keystore::{SyncCryptoStore, SyncCryptoStorePtr};
use sp_keystore::{Keystore, KeystorePtr};
use std::sync::Arc;
use test_helpers::{
dummy_candidate_receipt, dummy_collator, dummy_collator_signature, dummy_hash,
@@ -103,11 +102,11 @@ pub(crate) fn collator_sign_candidate(
assert!(candidate.descriptor().check_collator_signature().is_ok());
}
pub(crate) async fn back_candidate(
pub(crate) fn back_candidate(
candidate: CommittedCandidateReceipt,
validators: &[Sr25519Keyring],
group: &[ValidatorIndex],
keystore: &SyncCryptoStorePtr,
keystore: &KeystorePtr,
signing_context: &SigningContext,
kind: BackingKind,
) -> BackedCandidate {
@@ -134,7 +133,6 @@ pub(crate) async fn back_candidate(
*val_idx,
&key.public().into(),
)
.await
.unwrap()
.unwrap()
.signature()
@@ -222,8 +220,8 @@ pub(crate) fn validator_pubkeys(val_ids: &[Sr25519Keyring]) -> Vec<ValidatorId>
val_ids.iter().map(|v| v.public().into()).collect()
}
pub(crate) async fn sign_bitfield(
keystore: &SyncCryptoStorePtr,
pub(crate) fn sign_bitfield(
keystore: &KeystorePtr,
key: &Sr25519Keyring,
validator_index: ValidatorIndex,
bitfield: AvailabilityBitfield,
@@ -236,7 +234,6 @@ pub(crate) async fn sign_bitfield(
validator_index,
&key.public().into(),
)
.await
.unwrap()
.unwrap()
}
@@ -384,9 +381,9 @@ fn bitfield_checks() {
Sr25519Keyring::Dave,
Sr25519Keyring::Ferdie,
];
let keystore: SyncCryptoStorePtr = Arc::new(LocalKeystore::in_memory());
let keystore: KeystorePtr = Arc::new(LocalKeystore::in_memory());
for validator in validators.iter() {
SyncCryptoStore::sr25519_generate_new(
Keystore::sr25519_generate_new(
&*keystore,
PARACHAIN_KEY_TYPE_ID,
Some(&validator.to_seed()),
@@ -434,13 +431,13 @@ fn bitfield_checks() {
{
let mut bare_bitfield = default_bitfield();
bare_bitfield.0.push(false);
let signed = block_on(sign_bitfield(
let signed = sign_bitfield(
&keystore,
&validators[0],
ValidatorIndex(0),
bare_bitfield,
&signing_context,
));
);
assert_matches!(
ParaInclusion::process_bitfields(
@@ -457,13 +454,13 @@ fn bitfield_checks() {
// not enough bits
{
let bare_bitfield = default_bitfield();
let signed = block_on(sign_bitfield(
let signed = sign_bitfield(
&keystore,
&validators[0],
ValidatorIndex(0),
bare_bitfield,
&signing_context,
));
);
assert_matches!(
ParaInclusion::process_bitfields(
@@ -485,13 +482,13 @@ fn bitfield_checks() {
b.0.set(0, true);
b
};
let signed: UncheckedSignedAvailabilityBitfield = block_on(sign_bitfield(
let signed: UncheckedSignedAvailabilityBitfield = sign_bitfield(
&keystore,
&validators[0],
ValidatorIndex(0),
back_core_0_bitfield,
&signing_context,
))
)
.into();
assert_eq!(
@@ -536,22 +533,22 @@ fn bitfield_checks() {
b.0.set(0, true);
b
};
let signed_0 = block_on(sign_bitfield(
let signed_0 = sign_bitfield(
&keystore,
&validators[0],
ValidatorIndex(0),
back_core_0_bitfield.clone(),
&signing_context,
))
)
.into();
let signed_1 = block_on(sign_bitfield(
let signed_1 = sign_bitfield(
&keystore,
&validators[1],
ValidatorIndex(1),
back_core_0_bitfield,
&signing_context,
))
)
.into();
assert_eq!(
@@ -591,13 +588,13 @@ fn bitfield_checks() {
{
let mut bare_bitfield = default_bitfield();
*bare_bitfield.0.get_mut(0).unwrap() = true;
let signed = block_on(sign_bitfield(
let signed = sign_bitfield(
&keystore,
&validators[0],
ValidatorIndex(0),
bare_bitfield,
&signing_context,
));
);
assert_matches!(ParaInclusion::process_bitfields(
expected_bits(),
@@ -611,13 +608,13 @@ fn bitfield_checks() {
// empty bitfield signed: always ok, but kind of useless.
{
let bare_bitfield = default_bitfield();
let signed = block_on(sign_bitfield(
let signed = sign_bitfield(
&keystore,
&validators[0],
ValidatorIndex(0),
bare_bitfield,
&signing_context,
));
);
assert_matches!(ParaInclusion::process_bitfields(
expected_bits(),
@@ -651,13 +648,13 @@ fn bitfield_checks() {
PendingAvailabilityCommitments::<Test>::insert(chain_a, default_candidate.commitments);
*bare_bitfield.0.get_mut(0).unwrap() = true;
let signed = block_on(sign_bitfield(
let signed = sign_bitfield(
&keystore,
&validators[0],
ValidatorIndex(0),
bare_bitfield,
&signing_context,
));
);
assert_matches!(ParaInclusion::process_bitfields(
expected_bits(),
@@ -693,13 +690,13 @@ fn bitfield_checks() {
);
*bare_bitfield.0.get_mut(0).unwrap() = true;
let signed = block_on(sign_bitfield(
let signed = sign_bitfield(
&keystore,
&validators[0],
ValidatorIndex(0),
bare_bitfield,
&signing_context,
));
);
// no core is freed
assert_matches!(ParaInclusion::process_bitfields(
@@ -731,9 +728,9 @@ fn supermajority_bitfields_trigger_availability() {
Sr25519Keyring::Dave,
Sr25519Keyring::Ferdie,
];
let keystore: SyncCryptoStorePtr = Arc::new(LocalKeystore::in_memory());
let keystore: KeystorePtr = Arc::new(LocalKeystore::in_memory());
for validator in validators.iter() {
SyncCryptoStore::sr25519_generate_new(
Keystore::sr25519_generate_new(
&*keystore,
PARACHAIN_KEY_TYPE_ID,
Some(&validator.to_seed()),
@@ -836,13 +833,13 @@ fn supermajority_bitfields_trigger_availability() {
};
Some(
block_on(sign_bitfield(
sign_bitfield(
&keystore,
key,
ValidatorIndex(i as _),
to_sign,
&signing_context,
))
)
.into(),
)
})
@@ -923,9 +920,9 @@ fn candidate_checks() {
Sr25519Keyring::Dave,
Sr25519Keyring::Ferdie,
];
let keystore: SyncCryptoStorePtr = Arc::new(LocalKeystore::in_memory());
let keystore: KeystorePtr = Arc::new(LocalKeystore::in_memory());
for validator in validators.iter() {
SyncCryptoStore::sr25519_generate_new(
Keystore::sr25519_generate_new(
&*keystore,
PARACHAIN_KEY_TYPE_ID,
Some(&validator.to_seed()),
@@ -989,14 +986,14 @@ fn candidate_checks() {
.build();
collator_sign_candidate(Sr25519Keyring::One, &mut candidate);
let backed = block_on(back_candidate(
let backed = back_candidate(
candidate,
&validators,
group_validators(GroupIndex::from(0)).unwrap().as_ref(),
&keystore,
&signing_context,
BackingKind::Threshold,
));
);
assert_noop!(
ParaInclusion::process_candidates(
@@ -1034,23 +1031,23 @@ fn candidate_checks() {
collator_sign_candidate(Sr25519Keyring::Two, &mut candidate_b);
let backed_a = block_on(back_candidate(
let backed_a = back_candidate(
candidate_a,
&validators,
group_validators(GroupIndex::from(0)).unwrap().as_ref(),
&keystore,
&signing_context,
BackingKind::Threshold,
));
);
let backed_b = block_on(back_candidate(
let backed_b = back_candidate(
candidate_b,
&validators,
group_validators(GroupIndex::from(1)).unwrap().as_ref(),
&keystore,
&signing_context,
BackingKind::Threshold,
));
);
// out-of-order manifests as unscheduled.
assert_noop!(
@@ -1077,14 +1074,14 @@ fn candidate_checks() {
.build();
collator_sign_candidate(Sr25519Keyring::One, &mut candidate);
let backed = block_on(back_candidate(
let backed = back_candidate(
candidate,
&validators,
group_validators(GroupIndex::from(0)).unwrap().as_ref(),
&keystore,
&signing_context,
BackingKind::Lacking,
));
);
assert_noop!(
ParaInclusion::process_candidates(
@@ -1112,14 +1109,14 @@ fn candidate_checks() {
.build();
collator_sign_candidate(Sr25519Keyring::One, &mut candidate);
let backed = block_on(back_candidate(
let backed = back_candidate(
candidate,
&validators,
group_validators(GroupIndex::from(0)).unwrap().as_ref(),
&keystore,
&signing_context,
BackingKind::Threshold,
));
);
assert_noop!(
ParaInclusion::process_candidates(
@@ -1147,14 +1144,14 @@ fn candidate_checks() {
assert!(CollatorId::from(Sr25519Keyring::One.public()) != thread_collator);
collator_sign_candidate(Sr25519Keyring::One, &mut candidate);
let backed = block_on(back_candidate(
let backed = back_candidate(
candidate,
&validators,
group_validators(GroupIndex::from(2)).unwrap().as_ref(),
&keystore,
&signing_context,
BackingKind::Threshold,
));
);
assert_noop!(
ParaInclusion::process_candidates(
@@ -1189,14 +1186,14 @@ fn candidate_checks() {
// change the candidate after signing.
candidate.descriptor.pov_hash = Hash::repeat_byte(2);
let backed = block_on(back_candidate(
let backed = back_candidate(
candidate,
&validators,
group_validators(GroupIndex::from(2)).unwrap().as_ref(),
&keystore,
&signing_context,
BackingKind::Threshold,
));
);
assert_noop!(
ParaInclusion::process_candidates(
@@ -1223,14 +1220,14 @@ fn candidate_checks() {
collator_sign_candidate(Sr25519Keyring::One, &mut candidate);
let backed = block_on(back_candidate(
let backed = back_candidate(
candidate,
&validators,
group_validators(GroupIndex::from(0)).unwrap().as_ref(),
&keystore,
&signing_context,
BackingKind::Threshold,
));
);
let candidate = TestCandidateBuilder::default().build();
<PendingAvailability<Test>>::insert(
@@ -1279,14 +1276,14 @@ fn candidate_checks() {
// this is not supposed to happen
<PendingAvailabilityCommitments<Test>>::insert(&chain_a, candidate.commitments.clone());
let backed = block_on(back_candidate(
let backed = back_candidate(
candidate,
&validators,
group_validators(GroupIndex::from(0)).unwrap().as_ref(),
&keystore,
&signing_context,
BackingKind::Threshold,
));
);
assert_noop!(
ParaInclusion::process_candidates(
@@ -1316,14 +1313,14 @@ fn candidate_checks() {
collator_sign_candidate(Sr25519Keyring::One, &mut candidate);
let backed = block_on(back_candidate(
let backed = back_candidate(
candidate,
&validators,
group_validators(GroupIndex::from(0)).unwrap().as_ref(),
&keystore,
&signing_context,
BackingKind::Threshold,
));
);
{
let cfg = Configuration::config();
@@ -1357,14 +1354,14 @@ fn candidate_checks() {
collator_sign_candidate(Sr25519Keyring::One, &mut candidate);
let backed = block_on(back_candidate(
let backed = back_candidate(
candidate,
&validators,
group_validators(GroupIndex::from(0)).unwrap().as_ref(),
&keystore,
&signing_context,
BackingKind::Threshold,
));
);
assert_eq!(
ParaInclusion::process_candidates(
@@ -1392,14 +1389,14 @@ fn candidate_checks() {
collator_sign_candidate(Sr25519Keyring::One, &mut candidate);
let backed = block_on(back_candidate(
let backed = back_candidate(
candidate,
&validators,
group_validators(GroupIndex::from(0)).unwrap().as_ref(),
&keystore,
&signing_context,
BackingKind::Threshold,
));
);
assert_noop!(
ParaInclusion::process_candidates(
@@ -1427,14 +1424,14 @@ fn candidate_checks() {
collator_sign_candidate(Sr25519Keyring::One, &mut candidate);
let backed = block_on(back_candidate(
let backed = back_candidate(
candidate,
&validators,
group_validators(GroupIndex::from(0)).unwrap().as_ref(),
&keystore,
&signing_context,
BackingKind::Threshold,
));
);
assert_noop!(
ParaInclusion::process_candidates(
@@ -1470,9 +1467,9 @@ fn backing_works() {
Sr25519Keyring::Dave,
Sr25519Keyring::Ferdie,
];
let keystore: SyncCryptoStorePtr = Arc::new(LocalKeystore::in_memory());
let keystore: KeystorePtr = Arc::new(LocalKeystore::in_memory());
for validator in validators.iter() {
SyncCryptoStore::sr25519_generate_new(
Keystore::sr25519_generate_new(
&*keystore,
PARACHAIN_KEY_TYPE_ID,
Some(&validator.to_seed()),
@@ -1556,32 +1553,32 @@ fn backing_works() {
.build();
collator_sign_candidate(Sr25519Keyring::Two, &mut candidate_c);
let backed_a = block_on(back_candidate(
let backed_a = back_candidate(
candidate_a.clone(),
&validators,
group_validators(GroupIndex::from(0)).unwrap().as_ref(),
&keystore,
&signing_context,
BackingKind::Threshold,
));
);
let backed_b = block_on(back_candidate(
let backed_b = back_candidate(
candidate_b.clone(),
&validators,
group_validators(GroupIndex::from(1)).unwrap().as_ref(),
&keystore,
&signing_context,
BackingKind::Threshold,
));
);
let backed_c = block_on(back_candidate(
let backed_c = back_candidate(
candidate_c.clone(),
&validators,
group_validators(GroupIndex::from(2)).unwrap().as_ref(),
&keystore,
&signing_context,
BackingKind::Threshold,
));
);
let backed_candidates = vec![backed_a, backed_b, backed_c];
let get_backing_group_idx = {
@@ -1750,9 +1747,9 @@ fn can_include_candidate_with_ok_code_upgrade() {
Sr25519Keyring::Dave,
Sr25519Keyring::Ferdie,
];
let keystore: SyncCryptoStorePtr = Arc::new(LocalKeystore::in_memory());
let keystore: KeystorePtr = Arc::new(LocalKeystore::in_memory());
for validator in validators.iter() {
SyncCryptoStore::sr25519_generate_new(
Keystore::sr25519_generate_new(
&*keystore,
PARACHAIN_KEY_TYPE_ID,
Some(&validator.to_seed()),
@@ -1797,14 +1794,14 @@ fn can_include_candidate_with_ok_code_upgrade() {
.build();
collator_sign_candidate(Sr25519Keyring::One, &mut candidate_a);
let backed_a = block_on(back_candidate(
let backed_a = back_candidate(
candidate_a.clone(),
&validators,
group_validators(GroupIndex::from(0)).unwrap().as_ref(),
&keystore,
&signing_context,
BackingKind::Threshold,
));
);
let ProcessedCandidates { core_indices: occupied_cores, .. } =
ParaInclusion::process_candidates(
@@ -1859,9 +1856,9 @@ fn session_change_wipes() {
Sr25519Keyring::Dave,
Sr25519Keyring::Ferdie,
];
let keystore: SyncCryptoStorePtr = Arc::new(LocalKeystore::in_memory());
let keystore: KeystorePtr = Arc::new(LocalKeystore::in_memory());
for validator in validators.iter() {
SyncCryptoStore::sr25519_generate_new(
Keystore::sr25519_generate_new(
&*keystore,
PARACHAIN_KEY_TYPE_ID,
Some(&validator.to_seed()),
+2 -2
View File
@@ -427,7 +427,7 @@ impl inclusion::RewardValidators for TestRewardValidators {
/// Create a new set of test externalities.
pub fn new_test_ext(state: MockGenesisConfig) -> TestExternalities {
use sp_keystore::{testing::KeyStore, KeystoreExt, SyncCryptoStorePtr};
use sp_keystore::{testing::MemoryKeystore, KeystoreExt, KeystorePtr};
use sp_std::sync::Arc;
sp_tracing::try_init_simple();
@@ -440,7 +440,7 @@ pub fn new_test_ext(state: MockGenesisConfig) -> TestExternalities {
GenesisBuild::<Test>::assimilate_storage(&state.paras, &mut t).unwrap();
let mut ext: TestExternalities = t.into();
ext.register_extension(KeystoreExt(Arc::new(KeyStore::new()) as SyncCryptoStorePtr));
ext.register_extension(KeystoreExt(Arc::new(MemoryKeystore::new()) as KeystorePtr));
ext
}
@@ -19,7 +19,7 @@ use frame_support::{assert_err, assert_ok, assert_storage_noop};
use keyring::Sr25519Keyring;
use primitives::{BlockNumber, ValidatorId, PARACHAIN_KEY_TYPE_ID};
use sc_keystore::LocalKeystore;
use sp_keystore::{SyncCryptoStore, SyncCryptoStorePtr};
use sp_keystore::{Keystore, KeystorePtr};
use std::sync::Arc;
use test_helpers::{dummy_head_data, dummy_validation_code};
@@ -56,9 +56,9 @@ fn sign_and_include_pvf_check_statement(stmt: PvfCheckStatement) {
}
fn run_to_block(to: BlockNumber, new_session: Option<Vec<BlockNumber>>) {
let keystore: SyncCryptoStorePtr = Arc::new(LocalKeystore::in_memory());
let keystore: KeystorePtr = Arc::new(LocalKeystore::in_memory());
for validator in VALIDATORS.iter() {
SyncCryptoStore::sr25519_generate_new(
Keystore::sr25519_generate_new(
&*keystore,
PARACHAIN_KEY_TYPE_ID,
Some(&validator.to_seed()),
@@ -883,11 +883,10 @@ mod sanitizers {
use sp_core::crypto::UncheckedFrom;
use crate::mock::Test;
use futures::executor::block_on;
use keyring::Sr25519Keyring;
use primitives::PARACHAIN_KEY_TYPE_ID;
use sc_keystore::LocalKeystore;
use sp_keystore::{SyncCryptoStore, SyncCryptoStorePtr};
use sp_keystore::{Keystore, KeystorePtr};
use std::sync::Arc;
fn validator_pubkeys(val_ids: &[keyring::Sr25519Keyring]) -> Vec<ValidatorId> {
@@ -903,7 +902,7 @@ mod sanitizers {
let session_index = SessionIndex::from(0_u32);
let crypto_store = LocalKeystore::in_memory();
let crypto_store = Arc::new(crypto_store) as SyncCryptoStorePtr;
let crypto_store = Arc::new(crypto_store) as KeystorePtr;
let signing_context = SigningContext { parent_hash, session_index };
let validators = vec![
@@ -913,7 +912,7 @@ mod sanitizers {
keyring::Sr25519Keyring::Dave,
];
for validator in validators.iter() {
SyncCryptoStore::sr25519_generate_new(
Keystore::sr25519_generate_new(
&*crypto_store,
PARACHAIN_KEY_TYPE_ID,
Some(&validator.to_seed()),
@@ -935,13 +934,13 @@ mod sanitizers {
.enumerate()
.map(|(vi, ab)| {
let validator_index = ValidatorIndex::from(vi as u32);
block_on(SignedAvailabilityBitfield::sign(
SignedAvailabilityBitfield::sign(
&crypto_store,
AvailabilityBitfield::from(ab.clone()),
&signing_context,
validator_index,
&validator_public[vi],
))
)
.unwrap()
.unwrap()
.into_unchecked()
@@ -1142,7 +1141,7 @@ mod sanitizers {
let session_index = SessionIndex::from(0_u32);
let keystore = LocalKeystore::in_memory();
let keystore = Arc::new(keystore) as SyncCryptoStorePtr;
let keystore = Arc::new(keystore) as KeystorePtr;
let signing_context = SigningContext { parent_hash: relay_parent, session_index };
let validators = vec![
@@ -1152,7 +1151,7 @@ mod sanitizers {
keyring::Sr25519Keyring::Dave,
];
for validator in validators.iter() {
SyncCryptoStore::sr25519_generate_new(
Keystore::sr25519_generate_new(
&*keystore,
PARACHAIN_KEY_TYPE_ID,
Some(&validator.to_seed()),
@@ -1202,14 +1201,14 @@ mod sanitizers {
collator_sign_candidate(Sr25519Keyring::One, &mut candidate);
let backed = block_on(back_candidate(
let backed = back_candidate(
candidate,
&validators,
group_validators(GroupIndex::from(idx0 as u32)).unwrap().as_ref(),
&keystore,
&signing_context,
BackingKind::Threshold,
));
);
backed
})
.collect::<Vec<_>>();