Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Hook RPC extrinsic import into propagation #158

Merged
merged 6 commits into from
May 15, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions polkadot/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ 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" }
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" }
41 changes: 40 additions & 1 deletion polkadot/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -57,10 +61,41 @@ 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<Mutex<txpool::TransactionPool>>,
network: Arc<network::Service>,
}

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, encoded_bytes) = xt.using_encoded(|s| {
UncheckedExtrinsic::decode(&mut &s[..]).map(|d| (d, s.to_vec()))
})
.ok_or(substrate_rpc::author::error::ErrorKind::InvalidFormat)?;
info!("Correctly formatted: {:?}", decoded);

let hash = self.inner.lock().import(decoded)
.map(|v| v.hash().0.into())
.map_err(|_| substrate_rpc::author::error::ErrorKind::PoolError)?;

self.network.on_new_transactions(&[(hash, encoded_bytes)]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would end up propagating stale transactions; we might want to ensure cull is called first and propagate only after it is clear that the tx wasn't discarded.

Ok(())
}
}

/// Parse command line arguments and start the node.
///
Expand Down Expand Up @@ -172,7 +207,11 @@ pub fn run<I, T>(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())),
Expand Down
16 changes: 0 additions & 16 deletions polkadot/transaction-pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};
Expand Down Expand Up @@ -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 {
}
2 changes: 1 addition & 1 deletion substrate/network/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ impl Protocol {
trace!(target: "sync", "{} Ignoring transactions while syncing", peer_id);
return;
}
trace!(target: "sync", "Received {} transactions from {}", peer_id, transactions.len());
trace!(target: "sync", "Received {} transactions from {}", transactions.len(), peer_id);
let mut peers = self.peers.write();
if let Some(ref mut peer) = peers.get_mut(&peer_id) {
for t in transactions {
Expand Down
14 changes: 0 additions & 14 deletions substrate/rpc/src/author/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

//! Substrate block-author/full-node API.

use std::sync::Arc;
use parking_lot::Mutex;
use primitives::block::Extrinsic;

pub mod error;
Expand All @@ -35,15 +33,3 @@ build_rpc_trait! {
fn submit_extrinsic(&self, Extrinsic) -> Result<()>;
}
}

/// Variant of the AuthorApi that doesn't need to be Sync + Send + 'static.
pub trait AsyncAuthorApi: Send + 'static {
/// Submit extrinsic for inclusion in block.
fn submit_extrinsic(&mut self, Extrinsic) -> Result<()>;
}

impl<T: AsyncAuthorApi> AuthorApi for Arc<Mutex<T>> {
fn submit_extrinsic(&self, xt: Extrinsic) -> Result<()> {
self.as_ref().lock().submit_extrinsic(xt)
}
}
14 changes: 9 additions & 5 deletions substrate/rpc/src/author/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

use primitives::block;
use super::*;
use super::error::*;

use std::sync::Arc;
use parking_lot::Mutex;
use primitives::block;

#[derive(Default)]
struct DummyTxPool {
submitted: Vec<block::Extrinsic>,
}

impl AsyncAuthorApi for DummyTxPool {
impl AuthorApi for Arc<Mutex<DummyTxPool>> {
/// Submit extrinsic for inclusion in block.
fn submit_extrinsic(&mut self, xt: Extrinsic) -> Result<()> {
if self.submitted.len() < 1 {
self.submitted.push(xt);
fn submit_extrinsic(&self, xt: Extrinsic) -> Result<()> {
let mut s = self.lock();
if s.submitted.len() < 1 {
s.submitted.push(xt);
Ok(())
} else {
Err(ErrorKind::PoolError.into())
Expand Down