Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

[Companion #13615] Keystore overhaul #6892

Merged
merged 10 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
17 changes: 7 additions & 10 deletions node/core/approval-voting/src/criteria.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,17 +557,14 @@ mod tests {
use sp_application_crypto::sr25519;
use sp_core::crypto::Pair as PairT;
use sp_keyring::sr25519::Keyring as Sr25519Keyring;
use sp_keystore::CryptoStore;
use sp_keystore::Keystore;

// sets up a keystore with the given keyring accounts.
async fn make_keystore(accounts: &[Sr25519Keyring]) -> LocalKeystore {
fn make_keystore(accounts: &[Sr25519Keyring]) -> LocalKeystore {
let store = LocalKeystore::in_memory();

for s in accounts.iter().copied().map(|k| k.to_seed()) {
store
.sr25519_generate_new(ASSIGNMENT_KEY_TYPE_ID, Some(s.as_str()))
.await
.unwrap();
store.sr25519_generate_new(ASSIGNMENT_KEY_TYPE_ID, Some(s.as_str())).unwrap();
}

store
Expand Down Expand Up @@ -620,7 +617,7 @@ mod tests {

#[test]
fn assignments_produced_for_non_backing() {
let keystore = futures::executor::block_on(make_keystore(&[Sr25519Keyring::Alice]));
let keystore = make_keystore(&[Sr25519Keyring::Alice]);

let c_a = CandidateHash(Hash::repeat_byte(0));
let c_b = CandidateHash(Hash::repeat_byte(1));
Expand Down Expand Up @@ -655,7 +652,7 @@ mod tests {

#[test]
fn assign_to_nonzero_core() {
let keystore = futures::executor::block_on(make_keystore(&[Sr25519Keyring::Alice]));
let keystore = make_keystore(&[Sr25519Keyring::Alice]);

let c_a = CandidateHash(Hash::repeat_byte(0));
let c_b = CandidateHash(Hash::repeat_byte(1));
Expand Down Expand Up @@ -688,7 +685,7 @@ mod tests {

#[test]
fn succeeds_empty_for_0_cores() {
let keystore = futures::executor::block_on(make_keystore(&[Sr25519Keyring::Alice]));
let keystore = make_keystore(&[Sr25519Keyring::Alice]);

let relay_vrf_story = RelayVRFStory([42u8; 32]);
let assignments = compute_assignments(
Expand Down Expand Up @@ -728,7 +725,7 @@ mod tests {
rotation_offset: usize,
f: impl Fn(&mut MutatedAssignment) -> Option<bool>, // None = skip
) {
let keystore = futures::executor::block_on(make_keystore(&[Sr25519Keyring::Alice]));
let keystore = make_keystore(&[Sr25519Keyring::Alice]);

let group_for_core = |i| GroupIndex(((i + rotation_offset) % n_cores) as _);

Expand Down
2 changes: 1 addition & 1 deletion node/core/approval-voting/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use assert_matches::assert_matches;
use async_trait::async_trait;
use parking_lot::Mutex;
use sp_keyring::sr25519::Keyring as Sr25519Keyring;
use sp_keystore::CryptoStore;
use sp_keystore::Keystore;
use std::{
pin::Pin,
sync::{
Expand Down
33 changes: 15 additions & 18 deletions node/core/backing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ use polkadot_primitives::{
CommittedCandidateReceipt, CoreIndex, CoreState, Hash, Id as ParaId, PvfExecTimeoutKind,
SigningContext, ValidatorId, ValidatorIndex, ValidatorSignature, ValidityAttestation,
};
use sp_keystore::SyncCryptoStorePtr;
use sp_keystore::KeystorePtr;
use statement_table::{
generic::AttestedCandidate as TableAttestedCandidate,
v2::{
Expand Down Expand Up @@ -118,13 +118,13 @@ impl ValidatedCandidateCommand {

/// The candidate backing subsystem.
pub struct CandidateBackingSubsystem {
keystore: SyncCryptoStorePtr,
keystore: KeystorePtr,
metrics: Metrics,
}

impl CandidateBackingSubsystem {
/// Create a new instance of the `CandidateBackingSubsystem`.
pub fn new(keystore: SyncCryptoStorePtr, metrics: Metrics) -> Self {
pub fn new(keystore: KeystorePtr, metrics: Metrics) -> Self {
Self { keystore, metrics }
}
}
Expand All @@ -149,7 +149,7 @@ where
#[overseer::contextbounds(CandidateBacking, prefix = self::overseer)]
async fn run<Context>(
mut ctx: Context,
keystore: SyncCryptoStorePtr,
keystore: KeystorePtr,
metrics: Metrics,
) -> FatalResult<()> {
let (background_validation_tx, mut background_validation_rx) = mpsc::channel(16);
Expand Down Expand Up @@ -178,7 +178,7 @@ async fn run<Context>(
#[overseer::contextbounds(CandidateBacking, prefix = self::overseer)]
async fn run_iteration<Context>(
ctx: &mut Context,
keystore: SyncCryptoStorePtr,
keystore: KeystorePtr,
metrics: &Metrics,
jobs: &mut HashMap<Hash, JobAndSpan<Context>>,
background_validation_tx: mpsc::Sender<(Hash, ValidatedCandidateCommand)>,
Expand Down Expand Up @@ -265,7 +265,7 @@ async fn handle_active_leaves_update<Context>(
ctx: &mut Context,
update: ActiveLeavesUpdate,
jobs: &mut HashMap<Hash, JobAndSpan<Context>>,
keystore: &SyncCryptoStorePtr,
keystore: &KeystorePtr,
background_validation_tx: &mpsc::Sender<(Hash, ValidatedCandidateCommand)>,
metrics: &Metrics,
) -> Result<(), Error> {
Expand Down Expand Up @@ -426,7 +426,7 @@ struct CandidateBackingJob<Context> {
/// The candidates that are includable, by hash. Each entry here indicates
/// that we've sent the provisioner the backed candidate.
backed: HashSet<CandidateHash>,
keystore: SyncCryptoStorePtr,
keystore: KeystorePtr,
table: Table<TableContext>,
table_context: TableContext,
background_validation_tx: mpsc::Sender<(Hash, ValidatedCandidateCommand)>,
Expand Down Expand Up @@ -814,8 +814,7 @@ impl<Context> CandidateBackingJob<Context> {
commitments,
});
if let Some(stmt) = self
.sign_import_and_distribute_statement(ctx, statement, root_span)
.await?
.sign_import_and_distribute_statement(ctx, statement, root_span)?
{
// Break cycle - bounded as there is only one candidate to
// second per block.
Expand Down Expand Up @@ -843,8 +842,7 @@ impl<Context> CandidateBackingJob<Context> {
if !self.issued_statements.contains(&candidate_hash) {
if res.is_ok() {
let statement = Statement::Valid(candidate_hash);
self.sign_import_and_distribute_statement(ctx, statement, &root_span)
.await?;
self.sign_import_and_distribute_statement(ctx, statement, &root_span)?;
}
self.issued_statements.insert(candidate_hash);
}
Expand Down Expand Up @@ -966,14 +964,14 @@ impl<Context> CandidateBackingJob<Context> {
Ok(())
}

async fn sign_import_and_distribute_statement(
fn sign_import_and_distribute_statement(
&mut self,
ctx: &mut Context,
statement: Statement,
root_span: &jaeger::Span,
) -> Result<Option<SignedFullStatement>, Error> {
if let Some(signed_statement) = self.sign_statement(statement).await {
self.import_statement(ctx, &signed_statement, root_span).await?;
if let Some(signed_statement) = self.sign_statement(statement) {
self.import_statement(ctx, &signed_statement, root_span)?;
let smsg = StatementDistributionMessage::Share(self.parent, signed_statement.clone());
ctx.send_unbounded_message(smsg);

Expand Down Expand Up @@ -1001,7 +999,7 @@ impl<Context> CandidateBackingJob<Context> {
}

/// Import a statement into the statement table and return the summary of the import.
async fn import_statement(
fn import_statement(
&mut self,
ctx: &mut Context,
statement: &SignedFullStatement,
Expand Down Expand Up @@ -1222,7 +1220,7 @@ impl<Context> CandidateBackingJob<Context> {
ctx: &mut Context,
statement: SignedFullStatement,
) -> Result<(), Error> {
if let Some(summary) = self.import_statement(ctx, &statement, root_span).await? {
if let Some(summary) = self.import_statement(ctx, &statement, root_span)? {
if Some(summary.group_id) != self.assignment {
return Ok(())
}
Expand Down Expand Up @@ -1277,13 +1275,12 @@ impl<Context> CandidateBackingJob<Context> {
Ok(())
}

async fn sign_statement(&mut self, statement: Statement) -> Option<SignedFullStatement> {
fn sign_statement(&mut self, statement: Statement) -> Option<SignedFullStatement> {
let signed = self
.table_context
.validator
.as_ref()?
.sign(self.keystore.clone(), statement)
.await
.ok()
.flatten()?;
self.metrics.on_statement_signed();
Expand Down
Loading