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

Make Environment, Proposer, OnSlot and SyncOracle mut #3175

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions core/basic-authorship/src/basic_authorship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl<C, A> consensus_common::Environment<<C as AuthoringApi>::Block> for Propose
type Error = error::Error;

fn init(
&self,
&mut self,
parent_header: &<<C as AuthoringApi>::Block as BlockT>::Header,
) -> Result<Self::Proposer, error::Error> {
let parent_hash = parent_header.hash();
Expand Down Expand Up @@ -175,7 +175,7 @@ impl<Block, C, A> consensus_common::Proposer<<C as AuthoringApi>::Block> for Pro
type Error = error::Error;

fn propose(
&self,
&mut self,
inherent_data: InherentData,
inherent_digests: DigestFor<Block>,
max_duration: time::Duration,
Expand Down Expand Up @@ -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(),
};
Expand Down
17 changes: 8 additions & 9 deletions core/consensus/aura/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -204,15 +204,14 @@ 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 {
let pair = self.local_key.clone();
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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()))
Expand All @@ -754,7 +753,7 @@ mod tests {
type Create = future::Ready<Result<TestBlock, Error>>;

fn propose(
&self,
&mut self,
_: InherentData,
digests: DigestFor<TestBlock>,
_: Duration,
Expand Down Expand Up @@ -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)))
Expand All @@ -862,7 +861,7 @@ mod tests {
client.clone(),
select_chain,
client,
environ.clone(),
environ,
DummyOracle,
inherent_data_providers,
false,
Expand Down
9 changes: 4 additions & 5 deletions core/consensus/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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",
Expand Down
9 changes: 4 additions & 5 deletions core/consensus/babe/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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,
Expand Down Expand Up @@ -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)))
Expand All @@ -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,
Expand All @@ -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();
}
Expand Down
24 changes: 13 additions & 11 deletions core/consensus/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
}

Expand All @@ -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,
Expand All @@ -92,27 +92,29 @@ 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.
#[derive(Clone, Copy, Debug)]
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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/consensus/rhd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions core/consensus/slots/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 = ()>
Expand Down
17 changes: 14 additions & 3 deletions core/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,11 +505,22 @@ impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT> NetworkServic

impl<B: BlockT + 'static, S: NetworkSpecialization<B>, H: ExHashT>
::consensus::SyncOracle for NetworkService<B, S, H> {
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<B>, H: ExHashT>
::consensus::SyncOracle for &'a NetworkService<B, S, H> {
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
}
}
Expand Down
4 changes: 2 additions & 2 deletions node-template/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down
10 changes: 5 additions & 5 deletions node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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::<H256>::default();

Expand All @@ -381,7 +381,7 @@ mod tests {

digest.push(<DigestItem as CompatibleDigestItem>::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,
Expand Down