Remove InherentsPool and introduce non-propagateable transactions. (#2695)

* Remove InherentsPool.

* Introduce transactions that are not propagated over the network.

* Bump spec version.

* Use separate flag for propagation.

* Fix tests.
This commit is contained in:
Tomasz Drwięga
2019-05-29 16:06:49 +02:00
committed by Gavin Wood
parent 8d378cb293
commit 25b9c12c20
22 changed files with 250 additions and 171 deletions
+2 -7
View File
@@ -16,7 +16,6 @@
use std::sync::Arc;
use futures::{Stream, Future, sync::mpsc};
use inherents::pool::InherentsPool;
use log::{info, debug, warn};
use parity_codec::Decode;
use primitives::OffchainExt;
@@ -46,21 +45,18 @@ impl OffchainExt for AsyncApi {
pub(crate) struct Api<A: ChainApi> {
receiver: Option<mpsc::UnboundedReceiver<ExtMessage>>,
transaction_pool: Arc<Pool<A>>,
inherents_pool: Arc<InherentsPool<<A::Block as traits::Block>::Extrinsic>>,
at: BlockId<A::Block>,
}
impl<A: ChainApi> Api<A> {
pub fn new(
transaction_pool: Arc<Pool<A>>,
inherents_pool: Arc<InherentsPool<<A::Block as traits::Block>::Extrinsic>>,
at: BlockId<A::Block>,
) -> (AsyncApi, Self) {
let (tx, rx) = mpsc::unbounded();
let api = Self {
receiver: Some(rx),
transaction_pool,
inherents_pool,
at,
};
(AsyncApi(tx), api)
@@ -90,9 +86,8 @@ impl<A: ChainApi> Api<A> {
info!("Submitting to the pool: {:?} (isSigned: {:?})", xt, xt.is_signed());
match self.transaction_pool.submit_one(&self.at, xt.clone()) {
Ok(hash) => debug!("[{:?}] Offchain transaction added to the pool.", hash),
Err(_) => {
debug!("Offchain inherent added to the pool.");
self.inherents_pool.add(xt);
Err(e) => {
debug!("Couldn't submit transaction: {:?}", e);
},
}
}
+6 -10
View File
@@ -19,8 +19,8 @@
//! The offchain workers is a special function of the runtime that
//! gets executed after block is imported. During execution
//! it's able to asynchronously submit extrinsics that will either
//! be propagated to other nodes (transactions) or will be
//! added to the next block produced by the node as inherents.
//! be propagated to other nodes added to the next block
//! produced by the node as unsigned transactions.
//!
//! Offchain workers can be used for computation-heavy tasks
//! that are not feasible for execution during regular block processing.
@@ -39,7 +39,6 @@ use std::{
};
use client::runtime_api::ApiExt;
use inherents::pool::InherentsPool;
use log::{debug, warn};
use primitives::ExecutionContext;
use runtime_primitives::{
@@ -57,7 +56,6 @@ pub use offchain_primitives::OffchainWorkerApi;
#[derive(Debug)]
pub struct OffchainWorkers<C, Block: traits::Block> {
client: Arc<C>,
inherents_pool: Arc<InherentsPool<<Block as traits::Block>::Extrinsic>>,
executor: TaskExecutor,
_block: PhantomData<Block>,
}
@@ -66,12 +64,10 @@ impl<C, Block: traits::Block> OffchainWorkers<C, Block> {
/// Creates new `OffchainWorkers`.
pub fn new(
client: Arc<C>,
inherents_pool: Arc<InherentsPool<<Block as traits::Block>::Extrinsic>>,
executor: TaskExecutor,
) -> Self {
Self {
client,
inherents_pool,
executor,
_block: PhantomData,
}
@@ -97,7 +93,7 @@ impl<C, Block> OffchainWorkers<C, Block> where
debug!("Checking offchain workers at {:?}: {:?}", at, has_api);
if has_api.unwrap_or(false) {
let (api, runner) = api::Api::new(pool.clone(), self.inherents_pool.clone(), at.clone());
let (api, runner) = api::Api::new(pool.clone(), at.clone());
self.executor.spawn(runner.process());
debug!("Running offchain workers at {:?}", at);
@@ -119,14 +115,14 @@ mod tests {
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 inherents = Arc::new(InherentsPool::default());
// when
let offchain = OffchainWorkers::new(client, inherents.clone(), runtime.executor());
let offchain = OffchainWorkers::new(client, runtime.executor());
offchain.on_block_imported(&0u64, &pool);
// then
runtime.shutdown_on_idle().wait().unwrap();
assert_eq!(inherents.drain().len(), 1);
assert_eq!(pool.status().ready, 1);
assert_eq!(pool.ready().next().unwrap().is_propagateable(), false);
}
}