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

Commit 95a6ff5

Browse files
committed
some more refactoring
1 parent 11b8062 commit 95a6ff5

File tree

5 files changed

+63
-19
lines changed

5 files changed

+63
-19
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/consensus/aura/src/collator.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
2727
use codec::{Decode, Encode};
2828
use cumulus_client_collator::service::ServiceInterface as CollatorServiceInterface;
29-
use cumulus_client_consensus_common::{ParachainBlockImportMarker, ParachainCandidate};
29+
use cumulus_client_consensus_common::{self as consensus_common, ParachainBlockImportMarker, ParachainCandidate};
3030
use cumulus_client_consensus_proposer::ProposerInterface;
3131
use cumulus_primitives_core::{
3232
relay_chain::Hash as PHash, DigestItem, ParachainBlockData, PersistedValidationData,
@@ -249,6 +249,22 @@ pub struct SlotClaim<Pub> {
249249
}
250250

251251
impl<Pub> SlotClaim<Pub> {
252+
/// Create a slot-claim from the given author public key, slot, and timestamp.
253+
///
254+
/// This does not check whether the author actually owns the slot or the timestamp
255+
/// falls within the slot.
256+
pub fn unchecked<P>(author_pub: Pub, slot: Slot, timestamp: Timestamp) -> Self where
257+
P: Pair<Public = Pub>,
258+
P::Public: Encode + Decode,
259+
P::Signature: Encode + Decode
260+
{
261+
SlotClaim {
262+
author_pub,
263+
timestamp,
264+
pre_digest: aura_internal::pre_digest::<P>(slot),
265+
}
266+
}
267+
252268
/// Get the author's public key.
253269
pub fn author_pub(&self) -> &Pub {
254270
&self.author_pub
@@ -287,17 +303,13 @@ where
287303
let authorities = client.runtime_api().authorities(parent_hash).map_err(Box::new)?;
288304

289305
// Determine the current slot and timestamp based on the relay-parent's.
290-
let (slot_now, timestamp) =
291-
match sc_consensus_babe::find_pre_digest::<PBlock>(relay_parent_header) {
292-
Ok(babe_pre_digest) => {
293-
let t =
294-
Timestamp::new(relay_chain_slot_duration.as_millis() * *babe_pre_digest.slot());
295-
let slot = Slot::from_timestamp(t, slot_duration);
296-
297-
(slot, t)
298-
},
299-
Err(_) => return Ok(None),
300-
};
306+
let (slot_now, timestamp) = match consensus_common::relay_slot_and_timestamp(
307+
relay_parent_header,
308+
relay_chain_slot_duration,
309+
) {
310+
Some((_, t)) => (Slot::from_timestamp(t, slot_duration), t),
311+
None => return Ok(None),
312+
};
301313

302314
// Try to claim the slot locally.
303315
let author_pub = {
@@ -308,10 +320,7 @@ where
308320
}
309321
};
310322

311-
// Produce the pre-digest.
312-
let pre_digest = aura_internal::pre_digest::<P>(slot_now);
313-
314-
Ok(Some(SlotClaim { author_pub, pre_digest, timestamp }))
323+
Ok(Some(SlotClaim::unchecked::<P>(author_pub, slot_now, timestamp)))
315324
}
316325

317326
/// Seal a block with a signature in the header.

client/consensus/aura/src/collators/lookahead.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ use sp_state_machine::StorageChanges;
7272
use sp_timestamp::Timestamp;
7373
use std::{convert::TryFrom, error::Error, fmt::Debug, hash::Hash, sync::Arc, time::Duration};
7474

75+
use crate::collator as collator_util;
76+
7577
/// Parameters for [`run`].
7678
pub struct Params<BI, CIDP, Client, RClient, SO, Proposer, CS> {
7779
pub create_inherent_data_providers: CIDP,
@@ -129,14 +131,26 @@ pub async fn run<Block, P, BI, CIDP, Client, RClient, SO, Proposer, CS>(
129131
},
130132
};
131133

134+
let mut collator = {
135+
let params = collator_util::Params {
136+
create_inherent_data_providers: params.create_inherent_data_providers,
137+
block_import: params.block_import,
138+
relay_client: params.relay_client.clone(),
139+
keystore: params.keystore.clone(),
140+
para_id: params.para_id,
141+
proposer: params.proposer,
142+
collator_service: params.collator_service,
143+
};
144+
145+
collator_util::Collator::<Block, P, _, _, _, _, _>::new(params)
146+
};
147+
132148
while let Some(relay_parent_header) = import_notifications.next().await {
133149
let relay_parent = relay_parent_header.hash();
134150

135151
// TODO [now]: get asynchronous backing parameters from the relay-chain
136152
// runtime. why?
137153

138-
// TOOD [now]: get slot from relay parent header
139-
140154
let parent_search_params = ParentSearchParams {
141155
relay_parent,
142156
para_id: params.para_id,

client/consensus/common/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ tracing = "0.1.37"
1616
# Substrate
1717
sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "master" }
1818
sc-consensus = { git = "https://github.com/paritytech/substrate", branch = "master" }
19+
sc-consensus-babe = { git = "https://github.com/paritytech/substrate", branch = "master" }
1920
sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "master" }
2021
sp-consensus = { git = "https://github.com/paritytech/substrate", branch = "master" }
22+
sp-consensus-slots = { git = "https://github.com/paritytech/substrate", branch = "master" }
2123
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
2224
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master" }
25+
sp-timestamp = { git = "https://github.com/paritytech/substrate", branch = "master" }
2326
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "master" }
2427
substrate-prometheus-endpoint = { git = "https://github.com/paritytech/substrate", branch = "master" }
2528

client/consensus/common/src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.
1616

1717
use codec::Decode;
18-
use polkadot_primitives::{Hash as PHash, PersistedValidationData};
18+
use polkadot_primitives::{Block as PBlock, Hash as PHash, Header as PHeader, PersistedValidationData};
1919

2020
use cumulus_primitives_core::{relay_chain::OccupiedCoreAssumption, ParaId};
2121
use cumulus_relay_chain_interface::{RelayChainError, RelayChainInterface};
2222

2323
use sc_client_api::Backend;
2424
use sc_consensus::{shared_data::SharedData, BlockImport, ImportResult};
25+
use sp_consensus_slots::{Slot, SlotDuration};
26+
use sp_timestamp::Timestamp;
2527
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
2628

2729
use std::sync::Arc;
@@ -350,3 +352,16 @@ pub async fn find_potential_parents<B: BlockT>(
350352

351353
Ok(potential_parents)
352354
}
355+
356+
/// Get the relay-parent slot and timestamp from a header.
357+
pub fn relay_slot_and_timestamp(
358+
relay_parent_header: &PHeader,
359+
relay_chain_slot_duration: SlotDuration,
360+
) -> Option<(Slot, Timestamp)> {
361+
sc_consensus_babe::find_pre_digest::<PBlock>(relay_parent_header).map(|babe_pre_digest| {
362+
let slot = babe_pre_digest.slot();
363+
let t = Timestamp::new(relay_chain_slot_duration.as_millis() * *slot);
364+
365+
(slot, t)
366+
}).ok()
367+
}

0 commit comments

Comments
 (0)