Offchain execution extensions (#4145)

* Pass Extensions instead of individual objects.

* Move TransactionPool to a separate ExternalitiesExtension.

* Fix compilation.?

* Clean up.

* Refactor testing utilities.

* Add docs, fix tests.

* Fix doctest.

* Fix formatting and add some logs.

* Add some docs.

* Remove unused files.
This commit is contained in:
Tomasz Drwięga
2019-11-22 17:10:23 +01:00
committed by Gavin Wood
parent f000392cc0
commit 86b6ac5571
39 changed files with 554 additions and 360 deletions
+12 -12
View File
@@ -41,15 +41,11 @@ use sr_api::ApiExt;
use futures::future::Future;
use log::{debug, warn};
use network::NetworkStateInfo;
use primitives::{offchain, ExecutionContext};
use primitives::{offchain::{self, OffchainStorage}, ExecutionContext};
use sr_primitives::{generic::BlockId, traits::{self, ProvideRuntimeApi}};
use transaction_pool::txpool::{Pool, ChainApi};
use client_api::{OffchainStorage};
mod api;
pub mod testing;
pub use offchain_primitives::{OffchainWorkerApi, STORAGE_PREFIX};
/// An offchain workers manager.
@@ -94,13 +90,12 @@ impl<Client, Storage, Block> OffchainWorkers<
{
/// Start the offchain workers after given block.
#[must_use]
pub fn on_block_imported<A>(
pub fn on_block_imported(
&self,
number: &<Block::Header as traits::Header>::Number,
pool: &Arc<Pool<A>>,
network_state: Arc<dyn NetworkStateInfo + Send + Sync>,
is_validator: bool,
) -> impl Future<Output = ()> where A: ChainApi<Block=Block> + 'static {
) -> impl Future<Output = ()> {
let runtime = self.client.runtime_api();
let at = BlockId::number(*number);
let has_api = runtime.has_api::<dyn OffchainWorkerApi<Block, Error = ()>>(&at);
@@ -108,9 +103,7 @@ impl<Client, Storage, Block> OffchainWorkers<
if has_api.unwrap_or(false) {
let (api, runner) = api::AsyncApi::new(
pool.clone(),
self.db.clone(),
at.clone(),
network_state.clone(),
is_validator,
);
@@ -153,6 +146,8 @@ impl<Client, Storage, Block> OffchainWorkers<
mod tests {
use super::*;
use network::{Multiaddr, PeerId};
use std::sync::Arc;
use transaction_pool::txpool::Pool;
struct MockNetworkStateInfo();
@@ -171,13 +166,18 @@ mod tests {
// given
let _ = env_logger::try_init();
let client = Arc::new(test_client::new());
let pool = Arc::new(Pool::new(Default::default(), transaction_pool::FullChainApi::new(client.clone())));
let pool = Arc::new(Pool::new(
Default::default(),
transaction_pool::FullChainApi::new(client.clone())
));
client.execution_extensions()
.register_transaction_pool(Arc::downgrade(&pool.clone()) as _);
let db = client_db::offchain::LocalStorage::new_test();
let network_state = Arc::new(MockNetworkStateInfo());
// when
let offchain = OffchainWorkers::new(client, db);
futures::executor::block_on(offchain.on_block_imported(&0u64, &pool, network_state, false));
futures::executor::block_on(offchain.on_block_imported(&0u64, network_state, false));
// then
assert_eq!(pool.status().ready, 1);