Clean up the basic-authorship crate (#3206)

* Switch consensus-common to new futures

* Fix tests

* More tests fixing

* Make Proposer, OnSlot and SyncOracle mut

* Make the Environment mut as well

* Fix test

* Fix Babe tests

* Babe fixes

* Fix CLI service tests

* Fix Babe tests

* Remove unnecessary trait bound

* Inline the code of BlockBuilder and AuthoringApi

* Remove warning lint

* Bounds simplification

* Imports simplification

* Don't panic on bad generated block

* Code style

* Add doc example

* Remove dependency on aura

* Order dependencies alphabetically

* Minor style
This commit is contained in:
Pierre Krieger
2019-07-28 02:24:51 +02:00
committed by DemiMarie-parity
parent ba55d31d44
commit 9370a4a6b6
13 changed files with 203 additions and 219 deletions
+8 -9
View File
@@ -135,7 +135,7 @@ pub fn start_aura<B, C, SC, E, I, P, SO, Error, H>(
client: Arc<C>,
select_chain: SC,
block_import: I,
env: Arc<E>,
env: E,
sync_oracle: SO,
inherent_data_providers: InherentDataProviders,
force_authoring: bool,
@@ -180,7 +180,7 @@ pub fn start_aura<B, C, SC, E, I, P, SO, Error, H>(
struct AuraWorker<C, E, I, P, SO> {
client: Arc<C>,
block_import: Arc<Mutex<I>>,
env: Arc<E>,
env: E,
local_key: Arc<P>,
sync_oracle: SO,
force_authoring: bool,
@@ -204,7 +204,7 @@ impl<H, B, C, E, I, P, Error, SO> SlotWorker<B> for AuraWorker<C, E, I, P, SO> w
type OnSlot = Pin<Box<dyn Future<Output = Result<(), consensus_common::Error>> + Send>>;
fn on_slot(
&self,
&mut self,
chain_head: B::Header,
slot_info: SlotInfo,
) -> Self::OnSlot {
@@ -212,7 +212,6 @@ impl<H, B, C, E, I, P, Error, SO> SlotWorker<B> for AuraWorker<C, E, I, P, SO> w
let public_key = self.local_key.public();
let client = self.client.clone();
let block_import = self.block_import.clone();
let env = self.env.clone();
let (timestamp, slot_num, slot_duration) =
(slot_info.timestamp, slot_info.number, slot_info.duration);
@@ -253,7 +252,7 @@ impl<H, B, C, E, I, P, Error, SO> SlotWorker<B> for AuraWorker<C, E, I, P, SO> w
);
// we are the slot author. make a block and sign it.
let proposer = match env.init(&chain_head) {
let mut proposer = match self.env.init(&chain_head) {
Ok(p) => p,
Err(e) => {
warn!("Unable to author block in slot {:?}: {:?}", slot_num, e);
@@ -742,7 +741,7 @@ mod tests {
type Proposer = DummyProposer;
type Error = Error;
fn init(&self, parent_header: &<TestBlock as BlockT>::Header)
fn init(&mut self, parent_header: &<TestBlock as BlockT>::Header)
-> Result<DummyProposer, Error>
{
Ok(DummyProposer(parent_header.number + 1, self.0.clone()))
@@ -754,7 +753,7 @@ mod tests {
type Create = future::Ready<Result<TestBlock, Error>>;
fn propose(
&self,
&mut self,
_: InherentData,
digests: DigestFor<TestBlock>,
_: Duration,
@@ -841,7 +840,7 @@ mod tests {
let select_chain = LongestChain::new(
client.backend().clone(),
);
let environ = Arc::new(DummyFactory(client.clone()));
let environ = DummyFactory(client.clone());
import_notifications.push(
client.import_notification_stream()
.take_while(|n| future::ready(!(n.origin != BlockOrigin::Own && n.header.number() < &5)))
@@ -862,7 +861,7 @@ mod tests {
client.clone(),
select_chain,
client,
environ.clone(),
environ,
DummyOracle,
inherent_data_providers,
false,
+4 -5
View File
@@ -154,7 +154,7 @@ pub struct BabeParams<C, E, I, SO, SC> {
pub block_import: I,
/// The environment
pub env: Arc<E>,
pub env: E,
/// A sync oracle
pub sync_oracle: SO,
@@ -220,7 +220,7 @@ pub fn start_babe<B, C, SC, E, I, SO, Error, H>(BabeParams {
struct BabeWorker<C, E, I, SO> {
client: Arc<C>,
block_import: Arc<Mutex<I>>,
env: Arc<E>,
env: E,
local_key: Arc<sr25519::Pair>,
sync_oracle: SO,
force_authoring: bool,
@@ -245,14 +245,13 @@ impl<Hash, H, B, C, E, I, Error, SO> SlotWorker<B> for BabeWorker<C, E, I, SO> w
type OnSlot = Pin<Box<dyn Future<Output = Result<(), consensus_common::Error>> + Send>>;
fn on_slot(
&self,
&mut self,
chain_head: B::Header,
slot_info: SlotInfo,
) -> Self::OnSlot {
let pair = self.local_key.clone();
let ref client = self.client;
let block_import = self.block_import.clone();
let ref env = self.env;
let (timestamp, slot_number, slot_duration) =
(slot_info.timestamp, slot_info.number, slot_info.duration);
@@ -305,7 +304,7 @@ impl<Hash, H, B, C, E, I, Error, SO> SlotWorker<B> for BabeWorker<C, E, I, SO> w
);
// we are the slot author. make a block and sign it.
let proposer = match env.init(&chain_head) {
let mut proposer = match self.env.init(&chain_head) {
Ok(p) => p,
Err(e) => {
warn!(target: "babe",
+5 -5
View File
@@ -52,7 +52,7 @@ impl Environment<TestBlock> for DummyFactory {
type Proposer = DummyProposer;
type Error = Error;
fn init(&self, parent_header: &<TestBlock as BlockT>::Header)
fn init(&mut self, parent_header: &<TestBlock as BlockT>::Header)
-> Result<DummyProposer, Error>
{
Ok(DummyProposer(parent_header.number + 1, self.0.clone()))
@@ -64,7 +64,7 @@ impl Proposer<TestBlock> for DummyProposer {
type Create = future::Ready<Result<TestBlock, Error>>;
fn propose(
&self,
&mut self,
_: InherentData,
digests: DigestFor<TestBlock>,
_: Duration,
@@ -199,7 +199,7 @@ fn run_one_test() {
let mut runtime = current_thread::Runtime::new().unwrap();
for (peer_id, key) in peers {
let client = net.lock().peer(*peer_id).client().as_full().unwrap();
let environ = Arc::new(DummyFactory(client.clone()));
let environ = DummyFactory(client.clone());
import_notifications.push(
client.import_notification_stream()
.take_while(|n| future::ready(!(n.origin != BlockOrigin::Own && n.header.number() < &5)))
@@ -224,7 +224,7 @@ fn run_one_test() {
block_import: client.clone(),
select_chain,
client,
env: environ.clone(),
env: environ,
sync_oracle: DummyOracle,
inherent_data_providers,
force_authoring: false,
@@ -236,7 +236,7 @@ fn run_one_test() {
net.lock().poll();
Ok::<_, ()>(futures01::Async::NotReady::<()>)
}));
runtime.block_on(future::join_all(import_notifications)
.map(|_| Ok::<(), ()>(())).compat()).unwrap();
}
+13 -11
View File
@@ -61,7 +61,7 @@ pub trait Environment<B: BlockT> {
/// Initialize the proposal logic on top of a specific header. Provide
/// the authorities at that header.
fn init(&self, parent_header: &B::Header)
fn init(&mut self, parent_header: &B::Header)
-> Result<Self::Proposer, Self::Error>;
}
@@ -78,7 +78,7 @@ pub trait Proposer<B: BlockT> {
type Create: Future<Output = Result<B, Self::Error>>;
/// Create a proposal.
fn propose(
&self,
&mut self,
inherent_data: InherentData,
inherent_digests: DigestFor<B>,
max_duration: Duration,
@@ -92,10 +92,10 @@ pub trait Proposer<B: BlockT> {
pub trait SyncOracle {
/// Whether the synchronization service is undergoing major sync.
/// Returns true if so.
fn is_major_syncing(&self) -> bool;
fn is_major_syncing(&mut self) -> bool;
/// Whether the synchronization service is offline.
/// Returns true if so.
fn is_offline(&self) -> bool;
fn is_offline(&mut self) -> bool;
}
/// A synchronization oracle for when there is no network.
@@ -103,16 +103,18 @@ pub trait SyncOracle {
pub struct NoNetwork;
impl SyncOracle for NoNetwork {
fn is_major_syncing(&self) -> bool { false }
fn is_offline(&self) -> bool { false }
fn is_major_syncing(&mut self) -> bool { false }
fn is_offline(&mut self) -> bool { false }
}
impl<T: SyncOracle> SyncOracle for Arc<T> {
fn is_major_syncing(&self) -> bool {
T::is_major_syncing(&*self)
impl<T> SyncOracle for Arc<T>
where T: ?Sized, for<'r> &'r T: SyncOracle
{
fn is_major_syncing(&mut self) -> bool {
<&T>::is_major_syncing(&mut &**self)
}
fn is_offline(&self) -> bool {
T::is_offline(&*self)
fn is_offline(&mut self) -> bool {
<&T>::is_offline(&mut &**self)
}
}
+1 -1
View File
@@ -1392,7 +1392,7 @@ mod tests {
type Proposer = DummyProposer;
type Error = Error;
fn init(&self, parent_header: &TestHeader, _authorities: &[AuthorityId], _sign_with: Arc<ed25519::Pair>)
fn init(&mut self, parent_header: &TestHeader, _authorities: &[AuthorityId], _sign_with: Arc<ed25519::Pair>)
-> Result<DummyProposer, Error>
{
Ok(DummyProposer(parent_header.number + 1))
+3 -3
View File
@@ -46,7 +46,7 @@ pub trait SlotWorker<B: BlockT> {
type OnSlot: Future<Output = Result<(), consensus_common::Error>>;
/// Called when a new slot is triggered.
fn on_slot(&self, chain_head: B::Header, slot_info: SlotInfo) -> Self::OnSlot;
fn on_slot(&mut self, chain_head: B::Header, slot_info: SlotInfo) -> Self::OnSlot;
}
/// Slot compatible inherent data.
@@ -69,8 +69,8 @@ pub trait SlotCompatible {
pub fn start_slot_worker<B, C, W, T, SO, SC>(
slot_duration: SlotDuration<T>,
client: C,
worker: W,
sync_oracle: SO,
mut worker: W,
mut sync_oracle: SO,
inherent_data_providers: InherentDataProviders,
timestamp_extractor: SC,
) -> impl Future<Output = ()>