Extrinsics PubSub (#349)

* Extrinsic subscriptions.

* Handle RPC errors better.

* Add tests for extrinsics and unignored others.

* Handle client errors.

* Fix compilation.
This commit is contained in:
Tomasz Drwięga
2018-07-17 23:57:08 +02:00
committed by Gav Wood
parent 7dcbf77c9d
commit 7ce2a8552f
19 changed files with 278 additions and 71 deletions
@@ -4,6 +4,8 @@ version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]
serde = "1.0"
serde_derive = "1.0"
error-chain = "0.12"
futures = "0.1"
log = "0.3"
@@ -19,6 +19,8 @@
use txpool;
use futures::sync::mpsc;
use watcher::Watcher;
/// Extrinsic pool error.
pub trait Error: ::std::error::Error + Send + Sized {
/// Try to extract original `txpool::Error`
@@ -44,6 +46,9 @@ pub trait ExtrinsicPool<Ex, BlockId, Hash>: Send + Sync + 'static {
/// Submit a collection of extrinsics to the pool.
fn submit(&self, block: BlockId, xt: Vec<Ex>) -> Result<Vec<Hash>, Self::Error>;
/// Submit an extrinsic to the pool and start watching it's progress.
fn submit_and_watch(&self, block: BlockId, xt: Ex) -> Result<Watcher<Hash>, Self::Error>;
/// Returns light status of the pool.
fn light_status(&self) -> txpool::LightStatus;
@@ -20,18 +20,20 @@
extern crate futures;
extern crate parking_lot;
extern crate serde;
#[macro_use]
extern crate log;
#[macro_use]
extern crate serde_derive;
pub extern crate transaction_pool as txpool;
pub mod api;
pub mod watcher;
mod listener;
mod pool;
mod watcher;
pub use self::listener::Listener;
pub use self::pool::Pool;
pub use self::watcher::Watcher;
@@ -14,10 +14,16 @@
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
use futures::sync::mpsc;
//! Extrinsics status updates.
use futures::{
Stream,
sync::mpsc,
};
/// Possible extrinsic status events
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Status<H> {
/// Extrinsic has been finalised in block with given hash.
Finalised(H),
@@ -37,8 +43,19 @@ pub struct Watcher<H> {
receiver: mpsc::UnboundedReceiver<Status<H>>,
}
impl<H> Watcher<H> {
/// Pipe the notifications to given sink.
///
/// Make sure to drive the future to completion.
pub fn into_stream(self) -> impl Stream<Item=Status<H>, Error=()> {
// we can safely ignore the error here, `UnboundedReceiver` never fails.
self.receiver.map_err(|_| ())
}
}
/// Sender part of the watcher. Exposed only for testing purposes.
#[derive(Debug, Default)]
pub(crate) struct Sender<H> {
pub struct Sender<H> {
receivers: Vec<mpsc::UnboundedSender<Status<H>>>,
finalised: bool,
}
@@ -74,6 +91,7 @@ impl<H: Clone> Sender<H> {
self.send(Status::Broadcast(peers))
}
/// Returns true if the are no more listeners for this extrinsic or it was finalised.
pub fn is_done(&self) -> bool {
self.finalised || self.receivers.is_empty()