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

Commit

Permalink
Implement transaction queue RPC.
Browse files Browse the repository at this point in the history
  • Loading branch information
gavofyork committed Apr 5, 2018
1 parent b9983d1 commit 30ccc23
Show file tree
Hide file tree
Showing 19 changed files with 228 additions and 124 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions demo/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ substrate-state-machine = { path = "../../substrate/state-machine" }
substrate-executor = { path = "../../substrate/executor" }
substrate-primitives = { path = "../../substrate/primitives" }
substrate-rpc-servers = { path = "../../substrate/rpc-servers" }
substrate-rpc = { path = "../../substrate/rpc" }
demo-primitives = { path = "../primitives" }
demo-executor = { path = "../executor" }
demo-runtime = { path = "../runtime" }
10 changes: 9 additions & 1 deletion demo/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern crate substrate_runtime_io as runtime_io;
extern crate substrate_state_machine as state_machine;
extern crate substrate_client as client;
extern crate substrate_primitives as primitives;
extern crate substrate_rpc;
extern crate substrate_rpc_servers as rpc;
extern crate demo_primitives;
extern crate demo_executor;
Expand All @@ -49,6 +50,13 @@ use demo_runtime::{GenesisConfig, ConsensusConfig, CouncilConfig, DemocracyConfi
SessionConfig, StakingConfig, BuildExternalities};
use client::genesis;

struct DummyPool;
impl substrate_rpc::author::AuthorApi for DummyPool {
fn submit_extrinsic(&self, _: primitives::block::Extrinsic) -> substrate_rpc::author::error::Result<()> {
Err(substrate_rpc::author::error::ErrorKind::Unimplemented.into())
}
}

/// Parse command line arguments and start the node.
///
/// IANA unassigned port ranges that we could use:
Expand Down Expand Up @@ -126,7 +134,7 @@ pub fn run<I, T>(args: I) -> error::Result<()> where
let client = Arc::new(client::new_in_mem(executor, prepare_genesis)?);

let address = "127.0.0.1:9933".parse().unwrap();
let handler = rpc::rpc_handler(client);
let handler = rpc::rpc_handler(client, DummyPool);
let server = rpc::start_http(&address, handler)?;

if let Some(_) = matches.subcommand_matches("validator") {
Expand Down
3 changes: 2 additions & 1 deletion polkadot/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ pub fn run<I, T>(args: I) -> error::Result<()> where
let rpc_port: u16 = port.parse().expect("Invalid RPC port value specified.");
address.set_port(rpc_port);
}
let handler = rpc::rpc_handler(service.client());
let handler = rpc::rpc_handler(service.client(), service.transaction_pool());
let server = rpc::start_http(&address, handler)?;

server.wait();
Expand Down Expand Up @@ -147,6 +147,7 @@ fn default_base_path() -> PathBuf {
&app_info,
).expect("app directories exist on all supported platforms; qed")
}

fn init_logger(pattern: &str) {
let mut builder = env_logger::LogBuilder::new();
// Disable info logging by default for some modules:
Expand Down
2 changes: 2 additions & 0 deletions polkadot/transaction-pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ error-chain = "0.11"
polkadot-api = { path = "../api" }
polkadot-primitives = { path = "../primitives" }
polkadot-runtime = { path = "../runtime" }
substrate-client = { path = "../../substrate/client" }
substrate-rpc = { path = "../../substrate/rpc" }
substrate-primitives = { path = "../../substrate/primitives" }
substrate-runtime-primitives = { path = "../../substrate/runtime/primitives" }
substrate-codec = { path = "../../substrate/codec" }
Expand Down
13 changes: 13 additions & 0 deletions polkadot/transaction-pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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 @@ -31,8 +32,10 @@ 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 @@ -371,6 +374,16 @@ impl TransactionPool {
}
}

impl substrate_rpc::author::AsyncAuthorApi for TransactionPool {
fn submit_extrinsic(&mut self, xt: Extrinsic) -> substrate_rpc::author::error::Result<()> {
self.import(xt
.using_encoded(|ref mut s| UncheckedExtrinsic::decode(s))
.ok_or(substrate_rpc::author::error::ErrorKind::InvalidFormat)?
).map(|_| ())
.map_err(|_| substrate_rpc::author::error::ErrorKind::PoolError.into())
}
}

#[cfg(test)]
mod tests {
}
7 changes: 7 additions & 0 deletions substrate/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ impl<B, E> Client<B, E> where
})
}

/// Submit a transaction to this for potential inclusion in a block.
pub fn submit_transaction(&self, _tx: block::Transaction) -> error::Result<()> {
// TODO: introduce a tx queue and place the transaction in there. tx queue or some of its
// logic will need to be generic.
unimplemented!();
}

/// Get a reference to the state at a given block.
pub fn state_at(&self, block: &BlockId) -> error::Result<B::State> {
self.backend.state_at(*block)
Expand Down
6 changes: 6 additions & 0 deletions substrate/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ pub mod genesis;
pub mod block_builder;
mod client;

/// Something to which extrinsics may be submitted.
pub trait TransactionPool {
/// Submit an extrinsic into the pool.
fn submit(&self, e: primitives::block::Extrinsic);
}

pub use client::{Client, ClientInfo, CallResult, ImportResult,
BlockStatus, BlockOrigin, new_in_mem, BlockchainEventStream, BlockchainEvents};
pub use blockchain::Info as ChainInfo;
4 changes: 0 additions & 4 deletions substrate/environmental/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,3 @@
name = "environmental"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]

[features]
default = ["std"]
std = []
27 changes: 10 additions & 17 deletions substrate/environmental/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,12 @@
//! }
//! ```

#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), feature(const_fn))]

#[cfg(feature = "std")]
include!("../with_std.rs");

#[cfg(not(feature = "std"))]
include!("../without_std.rs");
use std::cell::RefCell;
use std::thread::LocalKey;

#[doc(hidden)]
pub fn using<T: ?Sized, R, F: FnOnce() -> R>(
global: &'static imp::LocalKey<imp::RefCell<Option<*mut T>>>,
global: &'static LocalKey<RefCell<Option<*mut T>>>,
protected: &mut T,
f: F
) -> R {
Expand All @@ -65,13 +59,13 @@ pub fn using<T: ?Sized, R, F: FnOnce() -> R>(
global.with(|r| {
let original = {
let mut global = r.borrow_mut();
imp::replace(&mut *global, Some(protected as _))
::std::mem::replace(&mut *global, Some(protected as _))
};

// even if `f` panics the original will be replaced.
struct ReplaceOriginal<'a, T: 'a + ?Sized> {
original: Option<*mut T>,
global: &'a imp::RefCell<Option<*mut T>>,
global: &'a RefCell<Option<*mut T>>,
}

impl<'a, T: 'a + ?Sized> Drop for ReplaceOriginal<'a, T> {
Expand All @@ -91,7 +85,7 @@ pub fn using<T: ?Sized, R, F: FnOnce() -> R>(

#[doc(hidden)]
pub fn with<T: ?Sized, R, F: FnOnce(&mut T) -> R>(
global: &'static imp::LocalKey<imp::RefCell<Option<*mut T>>>,
global: &'static LocalKey<RefCell<Option<*mut T>>>,
mutator: F,
) -> Option<R> {
global.with(|r| unsafe {
Expand All @@ -110,7 +104,6 @@ pub fn with<T: ?Sized, R, F: FnOnce(&mut T) -> R>(
/// Declare a new global reference module whose underlying value does not contain references.
///
/// Will create a module of a given name that contains two functions:
///
/// * `pub fn using<R, F: FnOnce() -> R>(protected: &mut $t, f: F) -> R`
/// This executes `f`, returning its value. During the call, the module's reference is set to
/// be equal to `protected`.
Expand Down Expand Up @@ -170,7 +163,7 @@ macro_rules! environmental {
#[allow(non_camel_case_types)]
struct $name { __private_field: () }

thread_local_impl!(static GLOBAL: ::std::cell::RefCell<Option<*mut $t>>
thread_local!(static GLOBAL: ::std::cell::RefCell<Option<*mut $t>>
= ::std::cell::RefCell::new(None));

impl $name {
Expand All @@ -194,8 +187,8 @@ macro_rules! environmental {
#[allow(non_camel_case_types)]
struct $name { __private_field: () }

thread_local_impl!(static GLOBAL: $crate::imp::RefCell<Option<*mut ($t + 'static)>>
= $crate::imp::RefCell::new(None));
thread_local!(static GLOBAL: ::std::cell::RefCell<Option<*mut ($t + 'static)>>
= ::std::cell::RefCell::new(None));

impl $name {
#[allow(unused_imports)]
Expand All @@ -205,7 +198,7 @@ macro_rules! environmental {
f: F
) -> R {
let lifetime_extended = unsafe {
$crate::imp::transmute::<&mut $t, &mut ($t + 'static)>(protected)
::std::mem::transmute::<&mut $t, &mut ($t + 'static)>(protected)
};
$crate::using(&GLOBAL, lifetime_extended, f)
}
Expand Down
31 changes: 0 additions & 31 deletions substrate/environmental/with_std.rs

This file was deleted.

69 changes: 0 additions & 69 deletions substrate/environmental/without_std.rs

This file was deleted.

15 changes: 15 additions & 0 deletions substrate/primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ impl Slicable for Transaction {
}
}

/// Simple generic extrinsic type.
#[derive(PartialEq, Eq, Clone)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
pub struct Extrinsic(#[cfg_attr(feature = "std", serde(with="bytes"))] pub Vec<u8>);

impl Slicable for Extrinsic {
fn decode<I: Input>(input: &mut I) -> Option<Self> {
Vec::<u8>::decode(input).map(Extrinsic)
}

fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.0.using_encoded(f)
}
}

/// Execution log (event)
#[derive(PartialEq, Eq, Clone, Default)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
Expand Down
4 changes: 3 additions & 1 deletion substrate/rpc-servers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ extern crate jsonrpc_http_server as http;
use std::io;

/// Construct rpc `IoHandler`
pub fn rpc_handler<S>(state: S) -> rpc::IoHandler where
pub fn rpc_handler<S, T>(state: S, transaction_pool: T) -> rpc::IoHandler where
S: apis::state::StateApi,
T: apis::author::AuthorApi,
{
let mut io = rpc::IoHandler::new();
io.extend_with(state.to_delegate());
io.extend_with(transaction_pool.to_delegate());
io
}

Expand Down
1 change: 1 addition & 0 deletions substrate/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]

[dependencies]
parking_lot = "0.4"
error-chain = "0.11"
jsonrpc-core = { git="https://github.com/paritytech/jsonrpc.git" }
jsonrpc-macros = { git="https://github.com/paritytech/jsonrpc.git" }
Expand Down
Loading

0 comments on commit 30ccc23

Please sign in to comment.