From 7c51525ad3207c88719777ad412286738043a964 Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Tue, 15 May 2018 10:27:18 +0200 Subject: [PATCH] Hook RPC extrinsic import into propagation (#158) * call `on_new_transactions` when we import * fix trace * pass correct bytes to network * clean up * cull before repropagating; repropagate on timer * add a little tracing --- polkadot/cli/Cargo.toml | 3 +++ polkadot/cli/src/lib.rs | 39 +++++++++++++++++++++++++++- polkadot/transaction-pool/src/lib.rs | 16 ------------ 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/polkadot/cli/Cargo.toml b/polkadot/cli/Cargo.toml index d956a0290b..5ec179b273 100644 --- a/polkadot/cli/Cargo.toml +++ b/polkadot/cli/Cargo.toml @@ -22,6 +22,7 @@ tokio-core = "0.1.12" futures = "0.1.17" ctrlc = { git = "https://github.com/paritytech/rust-ctrlc.git" } fdlimit = "0.1" +parking_lot = "0.4" substrate-client = { path = "../../substrate/client" } substrate-network = { path = "../../substrate/network" } substrate-codec = { path = "../../substrate/codec" } @@ -29,8 +30,10 @@ substrate-runtime-support = { path = "../../substrate/runtime-support" } substrate-state-machine = { path = "../../substrate/state-machine" } substrate-executor = { path = "../../substrate/executor" } substrate-primitives = { path = "../../substrate/primitives" } +substrate-rpc = { path = "../../substrate/rpc" } substrate-rpc-servers = { path = "../../substrate/rpc-servers" } polkadot-primitives = { path = "../primitives" } polkadot-executor = { path = "../executor" } polkadot-runtime = { path = "../runtime" } polkadot-service = { path = "../service" } +polkadot-transaction-pool = { path = "../transaction-pool" } diff --git a/polkadot/cli/src/lib.rs b/polkadot/cli/src/lib.rs index c469b7b504..604c665910 100644 --- a/polkadot/cli/src/lib.rs +++ b/polkadot/cli/src/lib.rs @@ -30,17 +30,21 @@ extern crate ctrlc; extern crate fdlimit; extern crate ed25519; extern crate triehash; +extern crate parking_lot; + extern crate substrate_codec as codec; extern crate substrate_state_machine as state_machine; extern crate substrate_client as client; extern crate substrate_primitives as primitives; extern crate substrate_network as network; +extern crate substrate_rpc; extern crate substrate_rpc_servers as rpc; extern crate substrate_runtime_support as runtime_support; extern crate polkadot_primitives; extern crate polkadot_executor; extern crate polkadot_runtime; extern crate polkadot_service as service; +extern crate polkadot_transaction_pool as txpool; #[macro_use] extern crate lazy_static; @@ -57,10 +61,39 @@ mod informant; use std::io; use std::net::SocketAddr; use std::path::{Path, PathBuf}; +use std::sync::Arc; + use futures::sync::mpsc; use futures::{Sink, Future, Stream}; use tokio_core::reactor; +use parking_lot::Mutex; use service::ChainSpec; +use primitives::block::Extrinsic; + +struct RpcTransactionPool { + inner: Arc>, + network: Arc, +} + +impl substrate_rpc::author::AuthorApi for RpcTransactionPool { + fn submit_extrinsic(&self, xt: Extrinsic) -> substrate_rpc::author::error::Result<()> { + use primitives::hexdisplay::HexDisplay; + use polkadot_runtime::UncheckedExtrinsic; + use codec::Slicable; + + info!("Extrinsic submitted: {}", HexDisplay::from(&xt.0)); + let decoded = xt.using_encoded(|ref mut s| UncheckedExtrinsic::decode(s)) + .ok_or(substrate_rpc::author::error::ErrorKind::InvalidFormat)?; + + info!("Correctly formatted: {:?}", decoded); + + self.inner.lock().import(decoded) + .map_err(|_| substrate_rpc::author::error::ErrorKind::PoolError)?; + + self.network.trigger_repropagate(); + Ok(()) + } +} /// Parse command line arguments and start the node. /// @@ -172,7 +205,11 @@ pub fn run(args: I) -> error::Result<()> where let handler = || { let chain = rpc::apis::chain::Chain::new(service.client(), core.remote()); - rpc::rpc_handler(service.client(), chain, service.transaction_pool()) + let pool = RpcTransactionPool { + inner: service.transaction_pool(), + network: service.network(), + }; + rpc::rpc_handler(service.client(), chain, pool) }; ( start_server(http_address, |address| rpc::start_http(address, handler())), diff --git a/polkadot/transaction-pool/src/lib.rs b/polkadot/transaction-pool/src/lib.rs index 555f2a8402..5e88a56be8 100644 --- a/polkadot/transaction-pool/src/lib.rs +++ b/polkadot/transaction-pool/src/lib.rs @@ -17,7 +17,6 @@ extern crate ed25519; extern crate ethereum_types; extern crate substrate_codec as codec; -extern crate substrate_rpc; extern crate substrate_primitives as substrate_primitives; extern crate substrate_runtime_primitives as substrate_runtime_primitives; extern crate polkadot_runtime as runtime; @@ -35,10 +34,8 @@ use std::collections::HashMap; use std::cmp::Ordering; use std::sync::Arc; -use codec::Slicable; use polkadot_api::PolkadotApi; use primitives::{AccountId, Timestamp}; -use substrate_primitives::block::Extrinsic; use runtime::{Block, UncheckedExtrinsic, TimestampCall, Call}; use substrate_runtime_primitives::traits::Checkable; use transaction_pool::{Pool, Readiness}; @@ -380,19 +377,6 @@ impl TransactionPool { } } -impl substrate_rpc::author::AsyncAuthorApi for TransactionPool { - fn submit_extrinsic(&mut self, xt: Extrinsic) -> substrate_rpc::author::error::Result<()> { - use substrate_primitives::hexdisplay::HexDisplay; - info!("Extrinsic submitted: {}", HexDisplay::from(&xt.0)); - let xt = xt.using_encoded(|ref mut s| UncheckedExtrinsic::decode(s)) - .ok_or(substrate_rpc::author::error::ErrorKind::InvalidFormat)?; - info!("Correctly formatted: {:?}", xt); - self.import(xt) - .map(|_| ()) - .map_err(|_| substrate_rpc::author::error::ErrorKind::PoolError.into()) - } -} - #[cfg(test)] mod tests { }