Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ mod tests {
descendent_query(&*service.client()),
&parent_hash,
parent_number,
slot_num,
slot_num.into(),
).unwrap().unwrap();

let mut digest = Digest::<H256>::default();
Expand All @@ -585,7 +585,7 @@ mod tests {
let babe_pre_digest = loop {
inherent_data.replace_data(sp_timestamp::INHERENT_IDENTIFIER, &(slot_num * SLOT_DURATION));
if let Some(babe_pre_digest) = sc_consensus_babe::test_helpers::claim_slot(
slot_num,
slot_num.into(),
&parent_header,
&*service.client(),
keystore.clone(),
Expand Down
4 changes: 2 additions & 2 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ impl_runtime_apis! {
}
}

fn current_epoch_start() -> sp_consensus_babe::SlotNumber {
fn current_epoch_start() -> sp_consensus_babe::Slot {
Babe::current_epoch_start()
}

Expand All @@ -1199,7 +1199,7 @@ impl_runtime_apis! {
}

fn generate_key_ownership_proof(
_slot_number: sp_consensus_babe::SlotNumber,
_slot: sp_consensus_babe::Slot,
authority_id: sp_consensus_babe::AuthorityId,
) -> Option<sp_consensus_babe::OpaqueKeyOwnershipProof> {
use codec::Encode;
Expand Down
1 change: 1 addition & 0 deletions client/consensus/aura/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ sc-block-builder = { version = "0.8.0", path = "../../block-builder" }
sc-client-api = { version = "2.0.0", path = "../../api" }
codec = { package = "parity-scale-codec", version = "1.3.6" }
sp-consensus = { version = "0.8.0", path = "../../../primitives/consensus/common" }
sp-consensus-slots = { version = "0.8.0", path = "../../../primitives/consensus/slots" }
derive_more = "0.99.2"
futures = "0.3.9"
futures-timer = "3.0.1"
Expand Down
9 changes: 5 additions & 4 deletions client/consensus/aura/src/digests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use sp_core::Pair;
use sp_consensus_aura::AURA_ENGINE_ID;
use sp_runtime::generic::{DigestItem, OpaqueDigestItemId};
use sp_consensus_slots::Slot;
use codec::{Encode, Codec};
use std::fmt::Debug;

Expand All @@ -38,10 +39,10 @@ pub trait CompatibleDigestItem<P: Pair>: Sized {
fn as_aura_seal(&self) -> Option<Signature<P>>;

/// Construct a digest item which contains the slot number
fn aura_pre_digest(slot_num: u64) -> Self;
fn aura_pre_digest(slot_num: Slot) -> Self;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn aura_pre_digest(slot_num: Slot) -> Self;
fn aura_pre_digest(slot: Slot) -> Self;


/// If this item is an AuRa pre-digest, return the slot number
fn as_aura_pre_digest(&self) -> Option<u64>;
fn as_aura_pre_digest(&self) -> Option<Slot>;
}

impl<P, Hash> CompatibleDigestItem<P> for DigestItem<Hash> where
Expand All @@ -57,11 +58,11 @@ impl<P, Hash> CompatibleDigestItem<P> for DigestItem<Hash> where
self.try_to(OpaqueDigestItemId::Seal(&AURA_ENGINE_ID))
}

fn aura_pre_digest(slot_num: u64) -> Self {
fn aura_pre_digest(slot_num: Slot) -> Self {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn aura_pre_digest(slot_num: Slot) -> Self {
fn aura_pre_digest(slot: Slot) -> Self {

DigestItem::PreRuntime(AURA_ENGINE_ID, slot_num.encode())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DigestItem::PreRuntime(AURA_ENGINE_ID, slot_num.encode())
DigestItem::PreRuntime(AURA_ENGINE_ID, slot.encode())

}

fn as_aura_pre_digest(&self) -> Option<u64> {
fn as_aura_pre_digest(&self) -> Option<Slot> {
self.try_to(OpaqueDigestItemId::PreRuntime(&AURA_ENGINE_ID))
}
}
63 changes: 31 additions & 32 deletions client/consensus/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ use prometheus_endpoint::Registry;
use codec::{Encode, Decode, Codec};

use sp_consensus::{
self, BlockImport, Environment, Proposer, CanAuthorWith, ForkChoiceStrategy, BlockImportParams,
BlockOrigin, Error as ConsensusError, SelectChain, SlotData, BlockCheckParams, ImportResult
};
use sp_consensus::import_queue::{
Verifier, BasicQueue, DefaultImportQueue, BoxJustificationImport,
BlockImport, Environment, Proposer, CanAuthorWith, ForkChoiceStrategy, BlockImportParams,
BlockOrigin, Error as ConsensusError, SelectChain, SlotData, BlockCheckParams, ImportResult,
import_queue::{
Verifier, BasicQueue, DefaultImportQueue, BoxJustificationImport,
},
};
use sc_client_api::{backend::AuxStore, BlockOf};
use sp_blockchain::{
Expand All @@ -57,10 +57,7 @@ use sp_blockchain::{
use sp_block_builder::BlockBuilder as BlockBuilderApi;
use sp_core::crypto::Public;
use sp_application_crypto::{AppKey, AppPublic};
use sp_runtime::{
generic::{BlockId, OpaqueDigestItemId},
traits::NumberFor, Justification,
};
use sp_runtime::{generic::{BlockId, OpaqueDigestItemId}, traits::NumberFor, Justification};
use sp_runtime::traits::{Block as BlockT, Header, DigestItemFor, Zero, Member};
use sp_api::ProvideRuntimeApi;
use sp_core::crypto::Pair;
Expand All @@ -75,6 +72,7 @@ use sc_consensus_slots::{
CheckedHeader, SlotInfo, SlotCompatible, StorageChanges, check_equivocation,
BackoffAuthoringBlocksStrategy,
};
use sp_consensus_slots::Slot;

use sp_api::ApiExt;

Expand Down Expand Up @@ -106,10 +104,10 @@ pub fn slot_duration<A, B, C>(client: &C) -> CResult<SlotDuration> where
}

/// Get slot author for given block along with authorities.
fn slot_author<P: Pair>(slot_num: u64, authorities: &[AuthorityId<P>]) -> Option<&AuthorityId<P>> {
fn slot_author<P: Pair>(slot_num: Slot, authorities: &[AuthorityId<P>]) -> Option<&AuthorityId<P>> {
if authorities.is_empty() { return None }

let idx = slot_num % (authorities.len() as u64);
let idx = slot_num.0 % (authorities.len() as u64);
assert!(
idx <= usize::max_value() as u64,
"It is impossible to have a vector with length beyond the address space; qed",
Expand Down Expand Up @@ -239,7 +237,7 @@ where
fn epoch_data(
&self,
header: &B::Header,
_slot_number: u64,
_slot_number: Slot,
) -> Result<Self::EpochData, sp_consensus::Error> {
authorities(self.client.as_ref(), &BlockId::Hash(header.hash()))
}
Expand All @@ -251,7 +249,7 @@ where
fn claim_slot(
&self,
_header: &B::Header,
slot_number: u64,
slot_number: Slot,
epoch_data: &Self::EpochData,
) -> Option<Self::Claim> {
let expected_author = slot_author::<P>(slot_number, epoch_data);
Expand All @@ -269,7 +267,7 @@ where

fn pre_digest_data(
&self,
slot_number: u64,
slot_number: Slot,
_claim: &Self::Claim,
) -> Vec<sp_runtime::DigestItem<B::Hash>> {
vec![
Expand Down Expand Up @@ -323,7 +321,7 @@ where
self.force_authoring
}

fn should_backoff(&self, slot_number: u64, chain_head: &B::Header) -> bool {
fn should_backoff(&self, slot_number: Slot, chain_head: &B::Header) -> bool {
if let Some(ref strategy) = self.backoff_authoring_blocks {
if let Ok(chain_head_slot) = find_pre_digest::<B, P>(chain_head) {
return strategy.should_backoff(
Expand Down Expand Up @@ -363,9 +361,10 @@ where
if let Some(slot_lenience) =
sc_consensus_slots::slot_lenience_exponential(parent_slot, slot_info)
{
debug!(target: "aura",
debug!(
target: "aura",
"No block for {} slots. Applying linear lenience of {}s",
slot_info.number.saturating_sub(parent_slot + 1),
slot_info.number.saturating_sub(parent_slot.0 + 1),
slot_lenience.as_secs(),
);

Expand Down Expand Up @@ -401,7 +400,7 @@ enum Error<B: BlockT> {
DataProvider(String),
Runtime(String),
#[display(fmt = "Slot number must increase: parent slot: {}, this slot: {}", _0, _1)]
SlotNumberMustIncrease(u64, u64),
SlotNumberMustIncrease(Slot, Slot),
#[display(fmt = "Parent ({}) of {} unavailable. Cannot import", _0, _1)]
ParentUnavailable(B::Hash, B::Hash),
}
Expand All @@ -412,16 +411,16 @@ impl<B: BlockT> std::convert::From<Error<B>> for String {
}
}

fn find_pre_digest<B: BlockT, P: Pair>(header: &B::Header) -> Result<u64, Error<B>>
fn find_pre_digest<B: BlockT, P: Pair>(header: &B::Header) -> Result<Slot, Error<B>>
where DigestItemFor<B>: CompatibleDigestItem<P>,
P::Signature: Decode,
P::Public: Encode + Decode + PartialEq + Clone,
{
if header.number().is_zero() {
return Ok(0);
return Ok(Slot(0));
}

let mut pre_digest: Option<u64> = None;
let mut pre_digest: Option<Slot> = None;
for log in header.digest().logs() {
trace!(target: "aura", "Checking log {:?}", log);
match (log.as_aura_pre_digest(), pre_digest.is_some()) {
Expand All @@ -440,11 +439,11 @@ fn find_pre_digest<B: BlockT, P: Pair>(header: &B::Header) -> Result<u64, Error<
//
fn check_header<C, B: BlockT, P: Pair>(
client: &C,
slot_now: u64,
slot_now: Slot,
mut header: B::Header,
hash: B::Hash,
authorities: &[AuthorityId<P>],
) -> Result<CheckedHeader<B::Header, (u64, DigestItemFor<B>)>, Error<B>> where
) -> Result<CheckedHeader<B::Header, (Slot, DigestItemFor<B>)>, Error<B>> where
DigestItemFor<B>: CompatibleDigestItem<P>,
P::Signature: Decode,
C: sc_client_api::backend::AuxStore,
Expand Down Expand Up @@ -608,7 +607,7 @@ impl<B: BlockT, C, P, CAW> Verifier<B> for AuraVerifier<C, P, CAW> where
// headers
let checked_header = check_header::<C, B, P>(
&self.client,
slot_now + 1,
Slot(slot_now.0 + 1),
header,
hash,
&authorities[..],
Expand Down Expand Up @@ -1113,13 +1112,13 @@ mod tests {
Default::default(),
Default::default()
);
assert!(worker.claim_slot(&head, 0, &authorities).is_none());
assert!(worker.claim_slot(&head, 1, &authorities).is_none());
assert!(worker.claim_slot(&head, 2, &authorities).is_none());
assert!(worker.claim_slot(&head, 3, &authorities).is_some());
assert!(worker.claim_slot(&head, 4, &authorities).is_none());
assert!(worker.claim_slot(&head, 5, &authorities).is_none());
assert!(worker.claim_slot(&head, 6, &authorities).is_none());
assert!(worker.claim_slot(&head, 7, &authorities).is_some());
assert!(worker.claim_slot(&head, 0.into(), &authorities).is_none());
assert!(worker.claim_slot(&head, 1.into(), &authorities).is_none());
assert!(worker.claim_slot(&head, 2.into(), &authorities).is_none());
assert!(worker.claim_slot(&head, 3.into(), &authorities).is_some());
assert!(worker.claim_slot(&head, 4.into(), &authorities).is_none());
assert!(worker.claim_slot(&head, 5.into(), &authorities).is_none());
assert!(worker.claim_slot(&head, 6.into(), &authorities).is_none());
assert!(worker.claim_slot(&head, 7.into(), &authorities).is_some());
}
}
1 change: 1 addition & 0 deletions client/consensus/babe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ sp-api = { version = "2.0.0", path = "../../../primitives/api" }
sp-block-builder = { version = "2.0.0", path = "../../../primitives/block-builder" }
sp-blockchain = { version = "2.0.0", path = "../../../primitives/blockchain" }
sp-consensus = { version = "0.8.0", path = "../../../primitives/consensus/common" }
sp-consensus-slots = { version = "0.8.0", path = "../../../primitives/consensus/slots" }
sp-consensus-vrf = { version = "0.8.0", path = "../../../primitives/consensus/vrf" }
sc-consensus-uncles = { version = "0.8.0", path = "../uncles" }
sc-consensus-slots = { version = "0.8.0", path = "../slots" }
Expand Down
16 changes: 11 additions & 5 deletions client/consensus/babe/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,13 @@ impl<B, C, SC> BabeApi for BabeRpcHandler<B, C, SC>
.map_err(|err| {
Error::StringError(format!("{:?}", err))
})?;
let epoch = epoch_data(&shared_epoch, &client, &babe_config, epoch_start, &select_chain)?;
let epoch = epoch_data(
&shared_epoch,
&client,
&babe_config,
epoch_start.0,
&select_chain,
)?;
let (epoch_start, epoch_end) = (epoch.start_slot(), epoch.end_slot());

let mut claims: HashMap<AuthorityId, EpochAuthorship> = HashMap::new();
Expand All @@ -142,9 +148,9 @@ impl<B, C, SC> BabeApi for BabeRpcHandler<B, C, SC>
.collect::<Vec<_>>()
};

for slot_number in epoch_start..epoch_end {
for slot_number in epoch_start.0..epoch_end.0 {
if let Some((claim, key)) =
authorship::claim_slot_using_keys(slot_number, &epoch, &keystore, &keys)
authorship::claim_slot_using_keys(slot_number.into(), &epoch, &keystore, &keys)
{
match claim {
PreDigest::Primary { .. } => {
Expand All @@ -154,7 +160,7 @@ impl<B, C, SC> BabeApi for BabeRpcHandler<B, C, SC>
claims.entry(key).or_default().secondary.push(slot_number);
}
PreDigest::SecondaryVRF { .. } => {
claims.entry(key).or_default().secondary_vrf.push(slot_number);
claims.entry(key).or_default().secondary_vrf.push(slot_number.into());
},
};
}
Expand Down Expand Up @@ -215,7 +221,7 @@ fn epoch_data<B, C, SC>(
descendent_query(&**client),
&parent.hash(),
parent.number().clone(),
slot_number,
slot_number.into(),
|slot| Epoch::genesis(&babe_config, slot),
)
.map_err(|e| Error::Consensus(ConsensusError::ChainLookup(format!("{:?}", e))))?
Expand Down
Loading