Skip to content

Commit

Permalink
txpool module support (paritytech#171)
Browse files Browse the repository at this point in the history
* Add TxPool types and basic impl

* Service extend with TxPool handler

* Add TransactionPool bound and parameter

* Add `TxPoolRuntimeApi`

* Implement `TxPoolRuntimeApi`

* Add `TxPoolRuntimeApi` bound to service

* Set public fields

* Add txpool primitives as a dependency

* Renaming

* Implement `txpool_status` and WIP `txpool_content`

* Fix types

* Implement `txpool_content`

* Generic map build

* Implement `txpool_inspect`

* Add copyright

* Use `insert_or_with` instead `contains_key`

* Update Cargo.lock

* Fix Summary serializer

* Add tests

* Bump runtime spec

* Add custom `block_hash` serializer

* Add custom `to` serializer

* Cleanup

* Fix grumble

Co-authored-by: Crystalin <alan@purestake.com>
  • Loading branch information
tgmichel and Crystalin authored Jan 18, 2021
1 parent 9661b8a commit 74b274b
Show file tree
Hide file tree
Showing 20 changed files with 782 additions and 2 deletions.
48 changes: 48 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
members = [
'runtime',
'node/parachain',
'client/rpc/txpool',
'client/rpc-core/txpool',
# We do NOT include the standalone node in this main workspace because it builds the
# runtime with the `standalone` feature, which the parachain does not support.
]
Expand Down
19 changes: 19 additions & 0 deletions client/rpc-core/txpool/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "moonbeam-rpc-core-txpool"
version = '0.1.0'
authors = ['PureStake']
edition = '2018'
homepage = 'https://moonbeam.network'
license = 'GPL-3.0-only'
repository = 'https://github.com/PureStake/moonbeam/'

[dependencies]
ethereum = { git = "https://github.com/rust-blockchain/ethereum", branch = "master", features = ["with-codec"] }
ethereum-types = "0.10.0"
jsonrpc-core = "15.0.0"
jsonrpc-core-client = "14.0.3"
jsonrpc-derive = "14.0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

fc-rpc-core = { git = "https://github.com/purestake/frontier", branch = "v0.5-hotfixes" }
37 changes: 37 additions & 0 deletions client/rpc-core/txpool/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2019-2020 PureStake Inc.
// This file is part of Moonbeam.

// Moonbeam 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.

// Moonbeam 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 Moonbeam. If not, see <http://www.gnu.org/licenses/>.

use ethereum_types::U256;
use jsonrpc_core::Result;
use jsonrpc_derive::rpc;

mod types;

pub use crate::types::{Get as GetT, Summary, Transaction, TransactionMap, TxPoolResult};

pub use rpc_impl_TxPool::gen_server::TxPool as TxPoolServer;

#[rpc(server)]
pub trait TxPool {
#[rpc(name = "txpool_content")]
fn content(&self) -> Result<TxPoolResult<TransactionMap<Transaction>>>;

#[rpc(name = "txpool_inspect")]
fn inspect(&self) -> Result<TxPoolResult<TransactionMap<Summary>>>;

#[rpc(name = "txpool_status")]
fn status(&self) -> Result<TxPoolResult<U256>>;
}
82 changes: 82 additions & 0 deletions client/rpc-core/txpool/src/types/content.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2019-2020 PureStake Inc.
// This file is part of Moonbeam.

// Moonbeam 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.

// Moonbeam 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 Moonbeam. If not, see <http://www.gnu.org/licenses/>.

use crate::GetT;
use ethereum::{Transaction as EthereumTransaction, TransactionAction};
use ethereum_types::{H160, H256, U256};
use fc_rpc_core::types::Bytes;
use serde::{Serialize, Serializer};

#[derive(Debug, Default, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Transaction {
/// Hash
pub hash: H256,
/// Nonce
pub nonce: U256,
/// Block hash
#[serde(serialize_with = "block_hash_serialize")]
pub block_hash: Option<H256>,
/// Block number
pub block_number: Option<U256>,
/// Sender
pub from: H160,
/// Recipient
#[serde(serialize_with = "to_serialize")]
pub to: Option<H160>,
/// Transfered value
pub value: U256,
/// Gas Price
pub gas_price: U256,
/// Gas
pub gas: U256,
/// Data
pub input: Bytes,
}

fn block_hash_serialize<S>(hash: &Option<H256>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&format!("0x{:x}", hash.unwrap_or(H256::default())))
}

fn to_serialize<S>(hash: &Option<H160>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&format!("0x{:x}", hash.unwrap_or(H160::default())))
}

impl GetT for Transaction {
fn get(hash: H256, from_address: H160, txn: &EthereumTransaction) -> Self {
Self {
hash,
nonce: txn.nonce,
block_hash: None,
block_number: None,
from: from_address,
to: match txn.action {
TransactionAction::Call(to) => Some(to),
_ => None,
},
value: txn.value,
gas_price: txn.gas_price,
gas: txn.gas_limit,
input: Bytes(txn.input.clone()),
}
}
}
58 changes: 58 additions & 0 deletions client/rpc-core/txpool/src/types/inspect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2019-2020 PureStake Inc.
// This file is part of Moonbeam.

// Moonbeam 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.

// Moonbeam 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 Moonbeam. If not, see <http://www.gnu.org/licenses/>.

use crate::GetT;
use ethereum::{Transaction as EthereumTransaction, TransactionAction};
use ethereum_types::{H160, H256, U256};
use serde::{Serialize, Serializer};

#[derive(Clone, Debug)]
pub struct Summary {
pub to: Option<H160>,
pub value: U256,
pub gas: U256,
pub gas_price: U256,
}

impl Serialize for Summary {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let res = format!(
"0x{:x}: {} wei + {} gas x {} wei",
self.to.unwrap_or(H160::default()),
self.value,
self.gas,
self.gas_price
);
serializer.serialize_str(&res)
}
}

impl GetT for Summary {
fn get(_hash: H256, _from_address: H160, txn: &EthereumTransaction) -> Self {
Self {
to: match txn.action {
TransactionAction::Call(to) => Some(to),
_ => None,
},
value: txn.value,
gas_price: txn.gas_price,
gas: txn.gas_limit,
}
}
}
38 changes: 38 additions & 0 deletions client/rpc-core/txpool/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2019-2020 PureStake Inc.
// This file is part of Moonbeam.

// Moonbeam 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.

// Moonbeam 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 Moonbeam. If not, see <http://www.gnu.org/licenses/>.

mod content;
mod inspect;

use ethereum::Transaction as EthereumTransaction;
use ethereum_types::{H160, H256, U256};
use serde::Serialize;
use std::collections::HashMap;

pub use self::content::Transaction;
pub use self::inspect::Summary;

pub type TransactionMap<T> = HashMap<H160, HashMap<U256, T>>;

#[derive(Debug, Serialize)]
pub struct TxPoolResult<T: Serialize> {
pub pending: T,
pub queued: T,
}

pub trait Get {
fn get(hash: H256, from_address: H160, txn: &EthereumTransaction) -> Self;
}
25 changes: 25 additions & 0 deletions client/rpc/txpool/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "moonbeam-rpc-txpool"
version = '0.1.0'
authors = ['PureStake']
edition = '2018'
homepage = 'https://moonbeam.network'
license = 'GPL-3.0-only'
repository = 'https://github.com/PureStake/moonbeam/'

[dependencies]
sha3 = "0.8"
jsonrpc-core = "15.0.0"
ethereum-types = "0.10.0"
ethereum = { git = "https://github.com/rust-blockchain/ethereum", branch = "master", features = ["with-codec"] }
moonbeam-rpc-core-txpool = { path = "../../rpc-core/txpool" }
sp-runtime = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-api = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-io = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-std = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-blockchain = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
sp-transaction-pool = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
frame-system = { git = "https://github.com/paritytech/substrate.git", branch = "master" }
serde = { version = "1.0", features = ["derive"] }

moonbeam-rpc-primitives-txpool = { path = "../../../primitives/rpc/txpool" }
Loading

0 comments on commit 74b274b

Please sign in to comment.