mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 02:51:08 +00:00
Switch offchain workers to new futures (#3285)
* Switch offchain workers to new futures * Fix tests
This commit is contained in:
committed by
Gavin Wood
parent
b29277239a
commit
3d65753d48
Generated
+1
-2
@@ -4732,7 +4732,7 @@ name = "substrate-offchain"
|
|||||||
version = "2.0.0"
|
version = "2.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"env_logger 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"env_logger 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"futures 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)",
|
"futures-preview 0.3.0-alpha.17 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"log 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
"log 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"parity-scale-codec 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
"parity-scale-codec 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
@@ -4745,7 +4745,6 @@ dependencies = [
|
|||||||
"substrate-primitives 2.0.0",
|
"substrate-primitives 2.0.0",
|
||||||
"substrate-test-runtime-client 2.0.0",
|
"substrate-test-runtime-client 2.0.0",
|
||||||
"substrate-transaction-pool 2.0.0",
|
"substrate-transaction-pool 2.0.0",
|
||||||
"tokio 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ edition = "2018"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
client = { package = "substrate-client", path = "../../core/client" }
|
client = { package = "substrate-client", path = "../../core/client" }
|
||||||
futures = "0.1.25"
|
futures-preview = "0.3.0-alpha.17"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
offchain-primitives = { package = "substrate-offchain-primitives", path = "./primitives" }
|
offchain-primitives = { package = "substrate-offchain-primitives", path = "./primitives" }
|
||||||
codec = { package = "parity-scale-codec", version = "1.0.0", features = ["derive"] }
|
codec = { package = "parity-scale-codec", version = "1.0.0", features = ["derive"] }
|
||||||
@@ -23,7 +23,6 @@ keystore = { package = "substrate-keystore", path = "../keystore" }
|
|||||||
env_logger = "0.6"
|
env_logger = "0.6"
|
||||||
client-db = { package = "substrate-client-db", path = "../../core/client/db/", default-features = true }
|
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" }
|
test-client = { package = "substrate-test-runtime-client", path = "../../core/test-runtime/client" }
|
||||||
tokio = "0.1.7"
|
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use client::backend::OffchainStorage;
|
use client::backend::OffchainStorage;
|
||||||
use futures::{Stream, Future, sync::mpsc};
|
use futures::{StreamExt as _, Future, future, channel::mpsc};
|
||||||
use log::{info, debug, warn, error};
|
use log::{info, debug, warn, error};
|
||||||
use network::{PeerId, Multiaddr, NetworkStateInfo};
|
use network::{PeerId, Multiaddr, NetworkStateInfo};
|
||||||
use codec::{Encode, Decode};
|
use codec::{Encode, Decode};
|
||||||
@@ -297,14 +297,14 @@ impl<A: ChainApi> AsyncApi<A> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Run a processing task for the API
|
/// 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.");
|
let receiver = self.receiver.take().expect("Take invoked only once.");
|
||||||
|
|
||||||
receiver.for_each(move |msg| {
|
receiver.for_each(move |msg| {
|
||||||
match msg {
|
match msg {
|
||||||
ExtMessage::SubmitExtrinsic(ext) => self.submit_extrinsic(ext),
|
ExtMessage::SubmitExtrinsic(ext) => self.submit_extrinsic(ext),
|
||||||
}
|
}
|
||||||
Ok(())
|
future::ready(())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ impl<Client, Storage, Block> OffchainWorkers<
|
|||||||
number: &<Block::Header as traits::Header>::Number,
|
number: &<Block::Header as traits::Header>::Number,
|
||||||
pool: &Arc<Pool<A>>,
|
pool: &Arc<Pool<A>>,
|
||||||
network_state: Arc<dyn NetworkStateInfo + Send + Sync>,
|
network_state: Arc<dyn NetworkStateInfo + Send + Sync>,
|
||||||
) -> impl Future<Item = (), Error = ()> where
|
) -> impl Future<Output = ()> where
|
||||||
A: ChainApi<Block=Block> + 'static,
|
A: ChainApi<Block=Block> + 'static,
|
||||||
{
|
{
|
||||||
let runtime = self.client.runtime_api();
|
let runtime = self.client.runtime_api();
|
||||||
@@ -129,9 +129,9 @@ impl<Client, Storage, Block> OffchainWorkers<
|
|||||||
log::error!("Error running offchain workers at {:?}: {:?}", at, e);
|
log::error!("Error running offchain workers at {:?}: {:?}", at, e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
futures::future::Either::A(runner.process())
|
futures::future::Either::Left(runner.process())
|
||||||
} else {
|
} else {
|
||||||
futures::future::Either::B(futures::future::ok(()))
|
futures::future::Either::Right(futures::future::ready(()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -152,7 +152,6 @@ fn spawn_worker(f: impl FnOnce() -> () + Send + 'static) {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use futures::Future;
|
|
||||||
use network::{Multiaddr, PeerId};
|
use network::{Multiaddr, PeerId};
|
||||||
|
|
||||||
struct MockNetworkStateInfo();
|
struct MockNetworkStateInfo();
|
||||||
@@ -171,7 +170,6 @@ mod tests {
|
|||||||
fn should_call_into_runtime_and_produce_extrinsic() {
|
fn should_call_into_runtime_and_produce_extrinsic() {
|
||||||
// given
|
// given
|
||||||
let _ = env_logger::try_init();
|
let _ = env_logger::try_init();
|
||||||
let runtime = tokio::runtime::Runtime::new().unwrap();
|
|
||||||
let client = Arc::new(test_client::new());
|
let client = Arc::new(test_client::new());
|
||||||
let pool = Arc::new(Pool::new(Default::default(), transaction_pool::ChainApi::new(client.clone())));
|
let pool = Arc::new(Pool::new(Default::default(), transaction_pool::ChainApi::new(client.clone())));
|
||||||
let db = client_db::offchain::LocalStorage::new_test();
|
let db = client_db::offchain::LocalStorage::new_test();
|
||||||
@@ -179,10 +177,9 @@ mod tests {
|
|||||||
|
|
||||||
// when
|
// when
|
||||||
let offchain = OffchainWorkers::new(client, db);
|
let offchain = OffchainWorkers::new(client, db);
|
||||||
runtime.executor().spawn(offchain.on_block_imported(&0u64, &pool, network_state.clone()));
|
futures::executor::block_on(offchain.on_block_imported(&0u64, &pool, network_state));
|
||||||
|
|
||||||
// then
|
// then
|
||||||
runtime.shutdown_on_idle().wait().unwrap();
|
|
||||||
assert_eq!(pool.status().ready, 1);
|
assert_eq!(pool.status().ready, 1);
|
||||||
assert_eq!(pool.ready().next().unwrap().is_propagateable(), false);
|
assert_eq!(pool.ready().next().unwrap().is_propagateable(), false);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ use crate::config::Configuration;
|
|||||||
use primitives::{Blake2Hasher, H256, traits::BareCryptoStorePtr};
|
use primitives::{Blake2Hasher, H256, traits::BareCryptoStorePtr};
|
||||||
use rpc::{self, apis::system::SystemInfo};
|
use rpc::{self, apis::system::SystemInfo};
|
||||||
use futures::{prelude::*, future::Executor};
|
use futures::{prelude::*, future::Executor};
|
||||||
use futures03::channel::mpsc;
|
use futures03::{FutureExt as _, channel::mpsc, compat::Compat};
|
||||||
|
|
||||||
// Type aliases.
|
// Type aliases.
|
||||||
// These exist mainly to avoid typing `<F as Factory>::Foo` all over the code.
|
// These exist mainly to avoid typing `<F as Factory>::Foo` all over the code.
|
||||||
@@ -279,7 +279,9 @@ impl<C: Components> OffchainWorker<Self> for C where
|
|||||||
pool: &Arc<TransactionPool<C::TransactionPoolApi>>,
|
pool: &Arc<TransactionPool<C::TransactionPoolApi>>,
|
||||||
network_state: &Arc<dyn NetworkStateInfo + Send + Sync>,
|
network_state: &Arc<dyn NetworkStateInfo + Send + Sync>,
|
||||||
) -> error::Result<Box<dyn Future<Item = (), Error = ()> + Send>> {
|
) -> 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)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user