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

Use sign_with in consensus #6008

Merged
20 commits merged into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from 18 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
2 changes: 2 additions & 0 deletions Cargo.lock

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

57 changes: 36 additions & 21 deletions client/consensus/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#![forbid(missing_docs, unsafe_code)]
use std::{
sync::Arc, time::Duration, thread, marker::PhantomData, hash::Hash, fmt::Debug, pin::Pin,
collections::HashMap
collections::HashMap, convert::{TryFrom, TryInto},
};

use futures::prelude::*;
Expand All @@ -52,11 +52,15 @@ use sp_blockchain::{
ProvideCache, HeaderBackend,
};
use sp_block_builder::BlockBuilder as BlockBuilderApi;
use sp_runtime::{generic::{BlockId, OpaqueDigestItemId}, Justification};
use sp_core::crypto::Public;
use sp_application_crypto::{AppKey, AppPublic};
use sp_runtime::{
generic::{BlockId, OpaqueDigestItemId},
Justification,
};
use sp_runtime::traits::{Block as BlockT, Header, DigestItemFor, Zero, Member};
use sp_api::ProvideRuntimeApi;

use sp_core::crypto::Pair;
use sp_core::{traits::BareCryptoStore, crypto::Pair};
use sp_inherents::{InherentDataProviders, InherentData};
use sp_timestamp::{
TimestampInherentData, InherentType as TimestampInherent, InherentError as TIError
Expand Down Expand Up @@ -150,8 +154,8 @@ pub fn start_aura<B, C, SC, E, I, P, SO, CAW, Error>(
E: Environment<B, Error = Error> + Send + Sync + 'static,
E::Proposer: Proposer<B, Error = Error, Transaction = sp_api::TransactionFor<C, B>>,
P: Pair + Send + Sync,
P::Public: Hash + Member + Encode + Decode,
P::Signature: Hash + Member + Encode + Decode,
P::Public: AppPublic + Hash + Member + Encode + Decode,
P::Signature: TryFrom<Vec<u8>> + Hash + Member + Encode + Decode,
I: BlockImport<B, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync + 'static,
Error: std::error::Error + Send + From<sp_consensus::Error> + 'static,
SO: SyncOracle + Send + Sync + Clone,
Expand Down Expand Up @@ -199,8 +203,8 @@ impl<B, C, E, I, P, Error, SO> sc_consensus_slots::SimpleSlotWorker<B> for AuraW
E::Proposer: Proposer<B, Error = Error, Transaction = sp_api::TransactionFor<C, B>>,
I: BlockImport<B, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync + 'static,
P: Pair + Send + Sync,
P::Public: Member + Encode + Decode + Hash,
P::Signature: Member + Encode + Decode + Hash + Debug,
P::Public: AppPublic + Public + Member + Encode + Decode + Hash,
P::Signature: TryFrom<Vec<u8>> + Member + Encode + Decode + Hash + Debug,
SO: SyncOracle + Send + Clone,
Error: std::error::Error + Send + From<sp_consensus::Error> + 'static,
{
Expand All @@ -210,7 +214,7 @@ impl<B, C, E, I, P, Error, SO> sc_consensus_slots::SimpleSlotWorker<B> for AuraW
dyn Future<Output = Result<E::Proposer, sp_consensus::Error>> + Send + 'static
>>;
type Proposer = E::Proposer;
type Claim = P;
type Claim = P::Public;
type EpochData = Vec<AuthorityId<P>>;

fn logging_target(&self) -> &'static str {
Expand Down Expand Up @@ -239,12 +243,7 @@ impl<B, C, E, I, P, Error, SO> sc_consensus_slots::SimpleSlotWorker<B> for AuraW
slot_number: u64,
epoch_data: &Self::EpochData,
) -> Option<Self::Claim> {
let expected_author = slot_author::<P>(slot_number, epoch_data);

expected_author.and_then(|p| {
self.keystore.read()
.key_pair_by_type::<P>(&p, sp_application_crypto::key_types::AURA).ok()
})
slot_author::<P>(slot_number, epoch_data).cloned()
}

fn pre_digest_data(
Expand All @@ -264,11 +263,27 @@ impl<B, C, E, I, P, Error, SO> sc_consensus_slots::SimpleSlotWorker<B> for AuraW
StorageChanges<sp_api::TransactionFor<C, B>, B>,
Self::Claim,
Self::EpochData,
) -> sp_consensus::BlockImportParams<B, sp_api::TransactionFor<C, B>> + Send> {
Box::new(|header, header_hash, body, storage_changes, pair, _epoch| {
) -> Result<sp_consensus::BlockImportParams<B, sp_api::TransactionFor<C, B>>, sp_consensus::Error> + Send + 'static> {
let keystore = self.keystore.clone();
Box::new(move |header, header_hash, body, storage_changes, public, _epoch| {
// sign the pre-sealed hash of the block and then
// add it to a digest item.
let signature = pair.sign(header_hash.as_ref());
let public_type_pair = public.to_public_crypto_pair();
let public = public.to_raw_vec();
let signature = keystore.read()
.sign_with(
bkchr marked this conversation as resolved.
Show resolved Hide resolved
<AuthorityId<P> as AppKey>::ID,
&public_type_pair,
header_hash.as_ref()
)
.map_err(|e| sp_consensus::Error::CannotSign(
public.clone(), e.to_string(),
))?;
let signature = signature.clone().try_into()
.map_err(|_| sp_consensus::Error::InvalidSignature(
signature, public
))?;

let signature_digest_item = <DigestItemFor<B> as CompatibleDigestItem<P>>::aura_seal(signature);

let mut import_block = BlockImportParams::new(BlockOrigin::Own, header);
Expand All @@ -277,7 +292,7 @@ impl<B, C, E, I, P, Error, SO> sc_consensus_slots::SimpleSlotWorker<B> for AuraW
import_block.storage_changes = Some(storage_changes);
import_block.fork_choice = Some(ForkChoiceStrategy::LongestChain);

import_block
Ok(import_block)
})
}

Expand Down Expand Up @@ -331,8 +346,8 @@ impl<B: BlockT, C, E, I, P, Error, SO> SlotWorker<B> for AuraWorker<C, E, I, P,
E::Proposer: Proposer<B, Error = Error, Transaction = sp_api::TransactionFor<C, B>>,
I: BlockImport<B, Transaction = sp_api::TransactionFor<C, B>> + Send + Sync + 'static,
P: Pair + Send + Sync,
P::Public: Member + Encode + Decode + Hash,
P::Signature: Member + Encode + Decode + Hash + Debug,
P::Public: AppPublic + Member + Encode + Decode + Hash,
P::Signature: TryFrom<Vec<u8>> + Member + Encode + Decode + Hash + Debug,
SO: SyncOracle + Send + Sync + Clone,
Error: std::error::Error + Send + From<sp_consensus::Error> + 'static,
{
Expand Down
7 changes: 3 additions & 4 deletions client/consensus/babe/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use sp_consensus_babe::{
use serde::{Deserialize, Serialize};
use sc_keystore::KeyStorePtr;
use sp_api::{ProvideRuntimeApi, BlockId};
use sp_core::crypto::Pair;
use sp_runtime::traits::{Block as BlockT, Header as _};
use sp_consensus::{SelectChain, Error as ConsensusError};
use sp_blockchain::{HeaderBackend, HeaderMetadata, Error as BlockChainError};
Expand Down Expand Up @@ -135,13 +134,13 @@ impl<B, C, SC> BabeApi for BabeRPCHandler<B, C, SC>
{
match claim {
PreDigest::Primary { .. } => {
claims.entry(key.public()).or_default().primary.push(slot_number);
claims.entry(key).or_default().primary.push(slot_number);
}
PreDigest::SecondaryPlain { .. } => {
claims.entry(key.public()).or_default().secondary.push(slot_number);
claims.entry(key).or_default().secondary.push(slot_number);
}
PreDigest::SecondaryVRF { .. } => {
claims.entry(key.public()).or_default().secondary_vrf.push(slot_number);
claims.entry(key).or_default().secondary_vrf.push(slot_number);
},
};
}
Expand Down
12 changes: 6 additions & 6 deletions client/consensus/babe/src/authorship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn claim_secondary_slot(
epoch: &Epoch,
key_pairs: &[(AuthorityPair, usize)],
author_secondary_vrf: bool,
) -> Option<(PreDigest, AuthorityPair)> {
) -> Option<(PreDigest, AuthorityId)> {
let Epoch { authorities, randomness, epoch_index, .. } = epoch;

if authorities.is_empty() {
Expand Down Expand Up @@ -163,7 +163,7 @@ fn claim_secondary_slot(
})
};

return Some((pre_digest, pair.clone()));
return Some((pre_digest, pair.public()));
}
}

Expand All @@ -178,7 +178,7 @@ pub fn claim_slot(
slot_number: SlotNumber,
epoch: &Epoch,
keystore: &KeyStorePtr,
) -> Option<(PreDigest, AuthorityPair)> {
) -> Option<(PreDigest, AuthorityId)> {
let key_pairs = {
let keystore = keystore.read();
epoch.authorities.iter()
Expand All @@ -197,7 +197,7 @@ pub fn claim_slot_using_key_pairs(
slot_number: SlotNumber,
epoch: &Epoch,
key_pairs: &[(AuthorityPair, usize)],
) -> Option<(PreDigest, AuthorityPair)> {
) -> Option<(PreDigest, AuthorityId)> {
claim_primary_slot(slot_number, epoch, epoch.config.c, &key_pairs)
.or_else(|| {
if epoch.config.allowed_slots.is_secondary_plain_slots_allowed() ||
Expand Down Expand Up @@ -229,7 +229,7 @@ fn claim_primary_slot(
epoch: &Epoch,
c: (u64, u64),
key_pairs: &[(AuthorityPair, usize)],
) -> Option<(PreDigest, AuthorityPair)> {
) -> Option<(PreDigest, AuthorityId)> {
let Epoch { authorities, randomness, epoch_index, .. } = epoch;

for (pair, authority_index) in key_pairs {
Expand All @@ -254,7 +254,7 @@ fn claim_primary_slot(

// early exit on first successful claim
if let Some(pre_digest) = pre_digest {
return Some((pre_digest, pair.clone()));
return Some((pre_digest, pair.public()));
}
}

Expand Down
32 changes: 24 additions & 8 deletions client/consensus/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,21 @@ pub use sp_consensus_babe::{
pub use sp_consensus::SyncOracle;
use std::{
collections::HashMap, sync::Arc, u64, pin::Pin, time::{Instant, Duration},
any::Any, borrow::Cow
any::Any, borrow::Cow, convert::TryInto,
};
use sp_consensus::{ImportResult, CanAuthorWith};
use sp_consensus::import_queue::{
BoxJustificationImport, BoxFinalityProofImport,
};
use sp_core::{crypto::Public, traits::BareCryptoStore};
use sp_application_crypto::AppKey;
use sp_runtime::{
generic::{BlockId, OpaqueDigestItemId}, Justification,
traits::{Block as BlockT, Header, DigestItemFor, Zero},
};
use sp_api::{ProvideRuntimeApi, NumberFor};
use sc_keystore::KeyStorePtr;
use parking_lot::Mutex;
use sp_core::Pair;
use sp_inherents::{InherentDataProviders, InherentData};
use sc_telemetry::{telemetry, CONSENSUS_TRACE, CONSENSUS_DEBUG};
use sp_consensus::{
Expand Down Expand Up @@ -440,7 +441,7 @@ impl<B, C, E, I, Error, SO> sc_consensus_slots::SimpleSlotWorker<B> for BabeWork
Error: std::error::Error + Send + From<ConsensusError> + From<I::Error> + 'static,
{
type EpochData = ViableEpochDescriptor<B::Hash, NumberFor<B>, Epoch>;
type Claim = (PreDigest, AuthorityPair);
type Claim = (PreDigest, AuthorityId);
type SyncOracle = SO;
type CreateProposer = Pin<Box<
dyn Future<Output = Result<E::Proposer, sp_consensus::Error>> + Send + 'static
Expand Down Expand Up @@ -517,12 +518,27 @@ impl<B, C, E, I, Error, SO> sc_consensus_slots::SimpleSlotWorker<B> for BabeWork
StorageChanges<I::Transaction, B>,
Self::Claim,
Self::EpochData,
) -> sp_consensus::BlockImportParams<B, I::Transaction> + Send> {
Box::new(|header, header_hash, body, storage_changes, (_, pair), epoch_descriptor| {
) -> Result<sp_consensus::BlockImportParams<B, I::Transaction>, sp_consensus::Error> + Send + 'static> {
let keystore = self.keystore.clone();
Box::new(move |header, header_hash, body, storage_changes, (_, public), epoch_descriptor| {
// sign the pre-sealed hash of the block and then
// add it to a digest item.
let signature = pair.sign(header_hash.as_ref());
let digest_item = <DigestItemFor<B> as CompatibleDigestItem>::babe_seal(signature);
let public_type_pair = public.clone().into();
let public = public.to_raw_vec();
let signature = keystore.read()
.sign_with(
<AuthorityId as AppKey>::ID,
&public_type_pair,
header_hash.as_ref()
)
.map_err(|e| sp_consensus::Error::CannotSign(
public.clone(), e.to_string(),
))?;
let signature: AuthoritySignature = signature.clone().try_into()
.map_err(|_| sp_consensus::Error::InvalidSignature(
signature, public
))?;
let digest_item = <DigestItemFor<B> as CompatibleDigestItem>::babe_seal(signature.into());

let mut import_block = BlockImportParams::new(BlockOrigin::Own, header);
import_block.post_digests.push(digest_item);
Expand All @@ -533,7 +549,7 @@ impl<B, C, E, I, Error, SO> sc_consensus_slots::SimpleSlotWorker<B> for BabeWork
Box::new(BabeIntermediate::<B> { epoch_descriptor }) as Box<dyn Any>,
);

import_block
Ok(import_block)
})
}

Expand Down
2 changes: 1 addition & 1 deletion client/consensus/babe/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#![allow(deprecated)]
use super::*;
use authorship::claim_slot;

use sp_core::crypto::Pair;
use sp_consensus_babe::{AuthorityPair, SlotNumber, AllowedSlots};
use sc_block_builder::{BlockBuilder, BlockBuilderProvider};
use sp_consensus::{
Expand Down
1 change: 1 addition & 0 deletions client/consensus/slots/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ targets = ["x86_64-unknown-linux-gnu"]
codec = { package = "parity-scale-codec", version = "1.3.0" }
sc-client-api = { version = "2.0.0-dev", path = "../../api" }
sp-core = { version = "2.0.0-dev", path = "../../../primitives/core" }
sp-application-crypto = { version = "2.0.0-dev", path = "../../../primitives/application-crypto" }
sp-blockchain = { version = "2.0.0-dev", path = "../../../primitives/blockchain" }
sp-runtime = { version = "2.0.0-dev", path = "../../../primitives/runtime" }
sp-state-machine = { version = "0.8.0-dev", path = "../../../primitives/state-machine" }
Expand Down
17 changes: 11 additions & 6 deletions client/consensus/slots/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,10 @@ pub trait SimpleSlotWorker<B: BlockT> {
StorageChanges<<Self::BlockImport as BlockImport<B>>::Transaction, B>,
Self::Claim,
Self::EpochData,
) -> sp_consensus::BlockImportParams<
B,
<Self::BlockImport as BlockImport<B>>::Transaction
>
+ Send
) -> Result<
gnunicorn marked this conversation as resolved.
Show resolved Hide resolved
sp_consensus::BlockImportParams<B, <Self::BlockImport as BlockImport<B>>::Transaction>,
sp_consensus::Error
> + Send + 'static
>;

/// Whether to force authoring if offline.
Expand Down Expand Up @@ -273,7 +272,7 @@ pub trait SimpleSlotWorker<B: BlockT> {
let block_import = self.block_import();
let logging_target = self.logging_target();

Box::pin(proposal_work.map_ok(move |(proposal, claim)| {
Box::pin(proposal_work.and_then(move |(proposal, claim)| {
let (header, body) = proposal.block.deconstruct();
let header_num = *header.number();
let header_hash = header.hash();
Expand All @@ -288,6 +287,11 @@ pub trait SimpleSlotWorker<B: BlockT> {
epoch_data,
);

let block_import_params = match block_import_params {
Ok(params) => params,
Err(e) => return future::err(e),
};

info!(
"🔖 Pre-sealed block for proposal at {}. Hash now {:?}, previously {:?}.",
header_num,
Expand All @@ -312,6 +316,7 @@ pub trait SimpleSlotWorker<B: BlockT> {
"hash" => ?parent_hash, "err" => ?err,
);
}
future::ready(Ok(()))
}))
}
}
Expand Down
14 changes: 13 additions & 1 deletion primitives/application-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ pub use codec;
#[cfg(feature = "std")]
pub use serde;
#[doc(hidden)]
pub use sp_std::{ops::Deref, vec::Vec};
pub use sp_std::{
convert::TryFrom,
ops::Deref,
vec::Vec,
};

pub mod ed25519;
pub mod sr25519;
Expand Down Expand Up @@ -456,6 +460,14 @@ macro_rules! app_crypto_signature_common {
impl $crate::AppSignature for Signature {
type Generic = $sig;
}

impl $crate::TryFrom<$crate::Vec<u8>> for Signature {
type Error = ();

fn try_from(data: $crate::Vec<u8>) -> Result<Self, Self::Error> {
Ok(<$sig>::try_from(data.as_slice())?.into())
}
}
}
}

Expand Down
Loading