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

Switch offchain workers to new futures #3285

Merged
merged 4 commits into from
Aug 8, 2019
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
3 changes: 1 addition & 2 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions core/offchain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2018"

[dependencies]
client = { package = "substrate-client", path = "../../core/client" }
futures = "0.1.25"
futures-preview = "0.3.0-alpha.17"
log = "0.4"
offchain-primitives = { package = "substrate-offchain-primitives", path = "./primitives" }
parity-codec = { version = "4.1.1", features = ["derive"] }
Expand All @@ -22,7 +22,6 @@ network = { package = "substrate-network", path = "../../core/network" }
env_logger = "0.6"
client-db = { package = "substrate-client-db", path = "../../core/client/db/", default-features = true }
test-client = { package = "substrate-test-runtime-client", path = "../../core/test-runtime/client" }
tokio = "0.1.7"

[features]
default = []
6 changes: 3 additions & 3 deletions core/offchain/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::{
};
use client::backend::OffchainStorage;
use crate::AuthorityKeyProvider;
use futures::{Stream, Future, sync::mpsc};
use futures::{StreamExt as _, Future, future, channel::mpsc};
use log::{info, debug, warn, error};
use parity_codec::{Encode, Decode};
use primitives::offchain::{
Expand Down Expand Up @@ -535,14 +535,14 @@ impl<A: ChainApi> AsyncApi<A> {
}

/// Run a processing task for the API
pub fn process(mut self) -> impl Future<Item=(), Error=()> {
pub fn process(mut self) -> impl Future<Output = ()> {
let receiver = self.receiver.take().expect("Take invoked only once.");

receiver.for_each(move |msg| {
match msg {
ExtMessage::SubmitExtrinsic(ext) => self.submit_extrinsic(ext),
}
Ok(())
future::ready(())
})
}

Expand Down
11 changes: 4 additions & 7 deletions core/offchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl<Client, Storage, KeyProvider, Block> OffchainWorkers<
number: &<Block::Header as traits::Header>::Number,
pool: &Arc<Pool<A>>,
network_state: Arc<dyn NetworkStateInfo + Send + Sync>,
) -> impl Future<Item = (), Error = ()> where
) -> impl Future<Output = ()> where
A: ChainApi<Block=Block> + 'static,
{
let runtime = self.client.runtime_api();
Expand Down Expand Up @@ -173,9 +173,9 @@ impl<Client, Storage, KeyProvider, Block> OffchainWorkers<
log::error!("Error running offchain workers at {:?}: {:?}", at, e);
}
});
futures::future::Either::A(runner.process())
futures::future::Either::Left(runner.process())
} else {
futures::future::Either::B(futures::future::ok(()))
futures::future::Either::Right(futures::future::ready(()))
}
}
}
Expand All @@ -196,7 +196,6 @@ fn spawn_worker(f: impl FnOnce() -> () + Send + 'static) {
#[cfg(test)]
mod tests {
use super::*;
use futures::Future;
use primitives::{ed25519, sr25519};
use network::{Multiaddr, PeerId};

Expand Down Expand Up @@ -246,18 +245,16 @@ mod tests {
fn should_call_into_runtime_and_produce_extrinsic() {
// given
let _ = env_logger::try_init();
let runtime = tokio::runtime::Runtime::new().unwrap();
let client = Arc::new(test_client::new());
let pool = Arc::new(Pool::new(Default::default(), ::transaction_pool::ChainApi::new(client.clone())));
let db = client_db::offchain::LocalStorage::new_test();
let mock = Arc::new(MockNetworkStateInfo());

// when
let offchain = OffchainWorkers::new(client, db, TestProvider::default(), "".to_owned().into());
runtime.executor().spawn(offchain.on_block_imported(&0u64, &pool, mock.clone()));
futures::executor::block_on(offchain.on_block_imported(&0u64, &pool, mock.clone()));

// then
runtime.shutdown_on_idle().wait().unwrap();
assert_eq!(pool.status().ready, 1);
assert_eq!(pool.ready().next().unwrap().is_propagateable(), false);
}
Expand Down
5 changes: 4 additions & 1 deletion core/service/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::config::Configuration;
use primitives::{Blake2Hasher, H256, Pair};
use rpc::{self, apis::system::SystemInfo};
use futures::{prelude::*, future::Executor, sync::mpsc};
use futures03::{FutureExt as _, compat::Compat};

// Type aliases.
// These exist mainly to avoid typing `<F as Factory>::Foo` all over the code.
Expand Down Expand Up @@ -264,7 +265,9 @@ impl<C: Components> OffchainWorker<Self> for C where
pool: &Arc<TransactionPool<C::TransactionPoolApi>>,
network_state: &Arc<dyn NetworkStateInfo + Send + Sync>,
) -> error::Result<Box<dyn Future<Item = (), Error = ()> + Send>> {
Ok(Box::new(offchain.on_block_imported(number, pool, network_state.clone())))
let future = offchain.on_block_imported(number, pool, network_state.clone())
.map(|()| Ok(()));
Ok(Box::new(Compat::new(future)))
}
}

Expand Down