forked from paritytech/substrate
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
txpool module support (paritytech#171)
* 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
Showing
20 changed files
with
782 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } |
Oops, something went wrong.