// Copyright 2018-2020 Parity Technologies (UK) Ltd. // This file is part of Substrate. // Substrate is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // Substrate is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with Substrate. If not, see . //! Substrate transaction pool implementation. #![warn(missing_docs)] #![warn(unused_extern_crates)] mod api; mod maintainer; pub mod error; #[cfg(test)] mod tests; pub use sc_transaction_graph as txpool; pub use crate::api::{FullChainApi, LightChainApi}; pub use crate::maintainer::{FullBasicPoolMaintainer, LightBasicPoolMaintainer}; use std::{collections::HashMap, sync::Arc}; use futures::{Future, FutureExt}; use sp_runtime::{ generic::BlockId, traits::Block as BlockT, }; use sp_transaction_pool::{ TransactionPool, PoolStatus, ImportNotificationStream, TxHash, TransactionFor, TransactionStatusStreamFor, }; /// Basic implementation of transaction pool that can be customized by providing PoolApi. pub struct BasicPool where Block: BlockT, PoolApi: sc_transaction_graph::ChainApi, { pool: Arc>, } impl BasicPool where Block: BlockT, PoolApi: sc_transaction_graph::ChainApi, { /// Create new basic transaction pool with provided api. pub fn new(options: sc_transaction_graph::Options, pool_api: PoolApi) -> Self { BasicPool { pool: Arc::new(sc_transaction_graph::Pool::new(options, pool_api)), } } /// Gets shared reference to the underlying pool. pub fn pool(&self) -> &Arc> { &self.pool } } impl TransactionPool for BasicPool where Block: BlockT, PoolApi: 'static + sc_transaction_graph::ChainApi, { type Block = PoolApi::Block; type Hash = sc_transaction_graph::ExHash; type InPoolTransaction = sc_transaction_graph::base_pool::Transaction, TransactionFor>; type Error = error::Error; fn submit_at( &self, at: &BlockId, xts: impl IntoIterator> + 'static, ) -> Box, Self::Error>>, Self::Error>> + Send + Unpin> { Box::new(self.pool.submit_at(at, xts, false)) } fn submit_one( &self, at: &BlockId, xt: TransactionFor, ) -> Box, Self::Error>> + Send + Unpin> { Box::new(self.pool.submit_one(at, xt)) } fn submit_and_watch( &self, at: &BlockId, xt: TransactionFor, ) -> Box>, Self::Error>> + Send + Unpin> { Box::new( self.pool.submit_and_watch(at, xt) .map(|result| result.map(|watcher| Box::new(watcher.into_stream()) as _)) ) } fn remove_invalid(&self, hashes: &[TxHash]) -> Vec> { self.pool.remove_invalid(hashes) } fn status(&self) -> PoolStatus { self.pool.status() } fn ready(&self) -> Box>> { Box::new(self.pool.ready()) } fn import_notification_stream(&self) -> ImportNotificationStream { self.pool.import_notification_stream() } fn hash_of(&self, xt: &TransactionFor) -> TxHash { self.pool.hash_of(xt) } fn on_broadcasted(&self, propagations: HashMap, Vec>) { self.pool.on_broadcasted(propagations) } }