diff --git a/core/basic-authorship/src/basic_authorship.rs b/core/basic-authorship/src/basic_authorship.rs index 00408a1545286..6af6b914139a9 100644 --- a/core/basic-authorship/src/basic_authorship.rs +++ b/core/basic-authorship/src/basic_authorship.rs @@ -132,7 +132,7 @@ impl consensus_common::Environment<::Block> for Propose type Error = error::Error; fn init( - &self, + &mut self, parent_header: &<::Block as BlockT>::Header, ) -> Result { let parent_hash = parent_header.hash(); @@ -175,7 +175,7 @@ impl consensus_common::Proposer<::Block> for Pro type Error = error::Error; fn propose( - &self, + &mut self, inherent_data: InherentData, inherent_digests: DigestFor, max_duration: time::Duration, @@ -311,7 +311,7 @@ mod tests { txpool.submit_at(&BlockId::number(0), vec![extrinsic(0), extrinsic(1)]).unwrap(); - let proposer_factory = ProposerFactory { + let mut proposer_factory = ProposerFactory { client: client.clone(), transaction_pool: txpool.clone(), }; diff --git a/core/consensus/aura/src/lib.rs b/core/consensus/aura/src/lib.rs index 964f7231b1714..8ba55b1f01595 100644 --- a/core/consensus/aura/src/lib.rs +++ b/core/consensus/aura/src/lib.rs @@ -135,7 +135,7 @@ pub fn start_aura( client: Arc, select_chain: SC, block_import: I, - env: Arc, + env: E, sync_oracle: SO, inherent_data_providers: InherentDataProviders, force_authoring: bool, @@ -180,7 +180,7 @@ pub fn start_aura( struct AuraWorker { client: Arc, block_import: Arc>, - env: Arc, + env: E, local_key: Arc

, sync_oracle: SO, force_authoring: bool, @@ -204,7 +204,7 @@ impl SlotWorker for AuraWorker w type OnSlot = Pin> + Send>>; fn on_slot( - &self, + &mut self, chain_head: B::Header, slot_info: SlotInfo, ) -> Self::OnSlot { @@ -212,7 +212,6 @@ impl SlotWorker for AuraWorker 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 SlotWorker for AuraWorker 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: &::Header) + fn init(&mut self, parent_header: &::Header) -> Result { Ok(DummyProposer(parent_header.number + 1, self.0.clone())) @@ -754,7 +753,7 @@ mod tests { type Create = future::Ready>; fn propose( - &self, + &mut self, _: InherentData, digests: DigestFor, _: 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, diff --git a/core/consensus/babe/src/lib.rs b/core/consensus/babe/src/lib.rs index 38b6257c8a239..4f5f9856ff0fb 100644 --- a/core/consensus/babe/src/lib.rs +++ b/core/consensus/babe/src/lib.rs @@ -154,7 +154,7 @@ pub struct BabeParams { pub block_import: I, /// The environment - pub env: Arc, + pub env: E, /// A sync oracle pub sync_oracle: SO, @@ -220,7 +220,7 @@ pub fn start_babe(BabeParams { struct BabeWorker { client: Arc, block_import: Arc>, - env: Arc, + env: E, local_key: Arc, sync_oracle: SO, force_authoring: bool, @@ -245,14 +245,13 @@ impl SlotWorker for BabeWorker w type OnSlot = Pin> + 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 SlotWorker for BabeWorker 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", diff --git a/core/consensus/babe/src/tests.rs b/core/consensus/babe/src/tests.rs index 1a68c35dbebd5..0d88061444c61 100644 --- a/core/consensus/babe/src/tests.rs +++ b/core/consensus/babe/src/tests.rs @@ -52,7 +52,7 @@ impl Environment for DummyFactory { type Proposer = DummyProposer; type Error = Error; - fn init(&self, parent_header: &::Header) + fn init(&mut self, parent_header: &::Header) -> Result { Ok(DummyProposer(parent_header.number + 1, self.0.clone())) @@ -64,7 +64,7 @@ impl Proposer for DummyProposer { type Create = future::Ready>; fn propose( - &self, + &mut self, _: InherentData, digests: DigestFor, _: 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,6 @@ fn run_one_test() { net.lock().poll(); Ok::<_, ()>(futures01::Async::NotReady::<()>) })); - runtime.block_on(future::join_all(import_notifications) .map(|_| Ok::<(), ()>(())).compat()).unwrap(); } diff --git a/core/consensus/common/src/lib.rs b/core/consensus/common/src/lib.rs index d901610df90f9..53816752b1282 100644 --- a/core/consensus/common/src/lib.rs +++ b/core/consensus/common/src/lib.rs @@ -61,7 +61,7 @@ pub trait Environment { /// 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; } @@ -78,7 +78,7 @@ pub trait Proposer { type Create: Future>; /// Create a proposal. fn propose( - &self, + &mut self, inherent_data: InherentData, inherent_digests: DigestFor, max_duration: Duration, @@ -92,10 +92,10 @@ pub trait Proposer { 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 SyncOracle for Arc { - fn is_major_syncing(&self) -> bool { - T::is_major_syncing(&*self) +impl SyncOracle for Arc +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) } } diff --git a/core/consensus/rhd/src/lib.rs b/core/consensus/rhd/src/lib.rs index 4670cb5deeae1..281f497cbc6f3 100644 --- a/core/consensus/rhd/src/lib.rs +++ b/core/consensus/rhd/src/lib.rs @@ -1392,7 +1392,7 @@ mod tests { type Proposer = DummyProposer; type Error = Error; - fn init(&self, parent_header: &TestHeader, _authorities: &[AuthorityId], _sign_with: Arc) + fn init(&mut self, parent_header: &TestHeader, _authorities: &[AuthorityId], _sign_with: Arc) -> Result { Ok(DummyProposer(parent_header.number + 1)) diff --git a/core/consensus/slots/src/lib.rs b/core/consensus/slots/src/lib.rs index 7e39d49651444..4cea9d82bf228 100644 --- a/core/consensus/slots/src/lib.rs +++ b/core/consensus/slots/src/lib.rs @@ -46,7 +46,7 @@ pub trait SlotWorker { type OnSlot: Future>; /// 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( slot_duration: SlotDuration, client: C, - worker: W, - sync_oracle: SO, + mut worker: W, + mut sync_oracle: SO, inherent_data_providers: InherentDataProviders, timestamp_extractor: SC, ) -> impl Future diff --git a/core/network/src/service.rs b/core/network/src/service.rs index 5b4c880c89c75..2075aef28685b 100644 --- a/core/network/src/service.rs +++ b/core/network/src/service.rs @@ -505,11 +505,22 @@ impl, H: ExHashT> NetworkServic impl, H: ExHashT> ::consensus::SyncOracle for NetworkService { - fn is_major_syncing(&self) -> bool { - self.is_major_syncing() + fn is_major_syncing(&mut self) -> bool { + NetworkService::is_major_syncing(self) } - fn is_offline(&self) -> bool { + fn is_offline(&mut self) -> bool { + self.num_connected.load(Ordering::Relaxed) == 0 + } +} + +impl<'a, B: BlockT + 'static, S: NetworkSpecialization, H: ExHashT> + ::consensus::SyncOracle for &'a NetworkService { + fn is_major_syncing(&mut self) -> bool { + NetworkService::is_major_syncing(self) + } + + fn is_offline(&mut self) -> bool { self.num_connected.load(Ordering::Relaxed) == 0 } } diff --git a/node-template/src/service.rs b/node-template/src/service.rs index 2a981c3bcc348..e5d83a189397d 100644 --- a/node-template/src/service.rs +++ b/node-template/src/service.rs @@ -70,10 +70,10 @@ construct_service_factory! { |service: Self::FullService| { if let Some(key) = service.authority_key() { info!("Using authority key {}", key.public()); - let proposer = Arc::new(ProposerFactory { + let proposer = ProposerFactory { client: service.client(), transaction_pool: service.transaction_pool(), - }); + }; let client = service.client(); let select_chain = service.select_chain() .ok_or_else(|| ServiceError::SelectChainRequired)?; diff --git a/node/cli/src/service.rs b/node/cli/src/service.rs index 29af557a3f5b8..839c73504f4ba 100644 --- a/node/cli/src/service.rs +++ b/node/cli/src/service.rs @@ -122,10 +122,10 @@ construct_service_factory! { if let Some(babe_key) = service.authority_key() { info!("Using BABE key {}", babe_key.public()); - let proposer = Arc::new(substrate_basic_authorship::ProposerFactory { + let proposer = substrate_basic_authorship::ProposerFactory { client: service.client(), transaction_pool: service.transaction_pool(), - }); + }; let client = service.client(); let select_chain = service.select_chain() @@ -355,10 +355,10 @@ mod tests { let parent_id = BlockId::number(service.client().info().chain.best_number); let parent_header = service.client().header(&parent_id).unwrap().unwrap(); - let proposer_factory = Arc::new(substrate_basic_authorship::ProposerFactory { + let mut proposer_factory = substrate_basic_authorship::ProposerFactory { client: service.client(), transaction_pool: service.transaction_pool(), - }); + }; let mut digest = Digest::::default(); @@ -381,7 +381,7 @@ mod tests { digest.push(::babe_pre_digest(babe_pre_digest)); - let proposer = proposer_factory.init(&parent_header).unwrap(); + let mut proposer = proposer_factory.init(&parent_header).unwrap(); let new_block = futures03::executor::block_on(proposer.propose( inherent_data, digest,