Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use transaction graph interface for future transactions #386

Closed
wants to merge 62 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
ed562db
Use transaction graph interface for future transactions
tgmichel Apr 27, 2021
3a263d6
Update to polkadot-v0.9.1
notlesh May 17, 2021
1b1303a
Bump grandpa-bridge-gadget from b0e5f2d -> 5005aa8
notlesh May 18, 2021
f7c4e44
Point polkadot to branch 'release-v0.9.1'
notlesh May 18, 2021
6da74d5
Merge branch 'master' into notlesh-polkadot-0.9.1
notlesh May 19, 2021
06c47e0
Point substrate to branch polkadot-v0.9.1 after merge
notlesh May 19, 2021
1c13595
Merge branch 'master' into notlesh-polkadot-0.9.1
notlesh May 20, 2021
bc980a8
Bump substrate branch from v0.9.1 to v0.9.2
notlesh May 20, 2021
ac5cdec
Point other rococo-v1 references to polkadot-v0.9.2
notlesh May 20, 2021
f67fdb7
Update Cargo.lock
notlesh May 20, 2021
cb83cec
Remove 'polkadot-parachain' from Cargo (temporarily)
notlesh May 20, 2021
edd780e
Fix a couple dependency versions
notlesh May 20, 2021
229522d
Set grandpa-bridge commit to e9231dd
notlesh May 20, 2021
0d2acf6
Point cumulus to branch notlesh-nimbus-merge-polkadot-v0.9.2
notlesh May 20, 2021
a9d031f
Point crowdloan-rewards at branch notlesh-update-deps-polkadot-v0.9.2
notlesh May 20, 2021
1679865
Point frontier at branch moonbeam-v0.8
notlesh May 20, 2021
d60115d
Remove cursed nimbus references
notlesh May 20, 2021
5bcfc4d
Don't use ancient ethereum version
notlesh May 20, 2021
525387a
Update Cargo.lock
notlesh May 20, 2021
4e9030a
Update Cargo.lock
notlesh May 20, 2021
b5683ad
Update crowdloan-rewards @ Cargo.lock
notlesh May 21, 2021
eb52ea0
Finally, a sane Cargo.lock
notlesh May 21, 2021
1a1a5e1
Reflect changes to XCMP trait
notlesh May 21, 2021
7dfe235
Remove random_seed()
notlesh May 21, 2021
1e9fe29
Point cumulus back to freshly-updated nimbus branch
notlesh May 21, 2021
fa04af5
Bump crowdloan-rewards commit
notlesh May 21, 2021
1f13b32
checkpoint
JoshOrndorff May 21, 2021
e13a1a6
Add collation info runtime api to all runtimes
JoshOrndorff May 21, 2021
0864ac6
Percent in chain_spec
JoshOrndorff May 21, 2021
8afb900
marker trait
JoshOrndorff May 21, 2021
d99f0f3
little cleanups
JoshOrndorff May 21, 2021
fbc3bf1
temporary hex peer count param
JoshOrndorff May 21, 2021
944ba75
unused ipmorts
JoshOrndorff May 21, 2021
fe8679f
Insert inherents stub for manual seal
notlesh May 21, 2021
6814406
Remove collator argument to start_node()
notlesh May 21, 2021
143d5ce
Temporarilly reinstate new_chain_ops to make things build
notlesh May 21, 2021
9a97b6a
Bump polkadotjs
notlesh May 22, 2021
37205a8
One more unused import
JoshOrndorff May 22, 2021
0b3d701
remove dead code. All runtimes use the same import queue
JoshOrndorff May 22, 2021
12d2ee7
Setup inherents for dev service
JoshOrndorff May 22, 2021
b625f13
Reflect modified error message
notlesh May 22, 2021
4f5a3d7
Change AccountInfo type to AccountInfoWithTripleRefCount
notlesh May 22, 2021
f18341b
Update @polkadotjs/types in moonbeam-types-bundle
notlesh May 22, 2021
50e1e6e
format
May 23, 2021
cc721b9
Merge branch 'master' into joshy-polkadot-v0.9.2
May 23, 2021
1d7cd26
Restore treasury
May 23, 2021
5a46e7b
Fixes rust tests
May 23, 2021
b77c945
Merge branch 'joshy-polkadot-v0.9.2' of github.com:purestake/moonbeam…
May 23, 2021
3da8989
Extra formatting
May 23, 2021
be3a417
Better formatting
May 23, 2021
4972065
play with spec names
JoshOrndorff May 23, 2021
bbbd826
Fixes test for genesis events
May 23, 2021
4fcd9b1
Merge branch 'joshy-polkadot-v0.9.2' of github.com:PureStake/moonbeam…
JoshOrndorff May 23, 2021
081b1aa
Don't be generic of build_consensus function
JoshOrndorff May 23, 2021
4fdd50c
Updates moonteam types package number
May 23, 2021
2c80d7d
Merge branch 'joshy-polkadot-v0.9.2' of github.com:purestake/moonbeam…
May 23, 2021
d340acf
Merge remote-tracking branch 'origin/joshy-polkadot-v0.9.2' into tgm-…
tgmichel May 24, 2021
3e7ea73
Update runtimes
tgmichel May 24, 2021
9c05a50
Reintroduce graph
tgmichel May 24, 2021
06c4e79
Ts test
tgmichel May 24, 2021
d14b139
fmt
tgmichel May 24, 2021
4e6d52a
Merge branch 'master' into tgm-txpool-queued
tgmichel May 24, 2021
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
1 change: 1 addition & 0 deletions client/rpc/txpool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.2" }
sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.2" }
sp-transaction-pool = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.2" }
sc-transaction-graph = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.2" }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.2" }
serde = { version = "1.0", features = ["derive"] }

Expand Down
90 changes: 55 additions & 35 deletions client/rpc/txpool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,105 +21,125 @@ use jsonrpc_core::Result as RpcResult;
pub use moonbeam_rpc_core_txpool::{
GetT, Summary, Transaction, TransactionMap, TxPool as TxPoolT, TxPoolResult, TxPoolServer,
};
use sc_transaction_graph::{ChainApi, Pool};
use serde::Serialize;
use sha3::{Digest, Keccak256};
use sp_api::{BlockId, ProvideRuntimeApi};
use sp_blockchain::{Error as BlockChainError, HeaderBackend, HeaderMetadata};
use sp_runtime::traits::Block as BlockT;
use sp_transaction_pool::{InPoolTransaction, TransactionPool};
use sp_transaction_pool::InPoolTransaction;
use std::collections::HashMap;
use std::{marker::PhantomData, sync::Arc};

use moonbeam_rpc_primitives_txpool::TxPoolRuntimeApi;
use moonbeam_rpc_primitives_txpool::{TxPoolResponse, TxPoolRuntimeApi};

pub struct TxPool<B: BlockT, C, P> {
pub struct TxPool<B: BlockT, C, A: ChainApi> {
client: Arc<C>,
pool: Arc<P>,
graph: Arc<Pool<A>>,
_marker: PhantomData<B>,
}

impl<B, C, P> TxPool<B, C, P>
impl<B, C, A> TxPool<B, C, A>
where
C: ProvideRuntimeApi<B>,
C: HeaderMetadata<B, Error = BlockChainError> + HeaderBackend<B> + 'static,
C: Send + Sync + 'static,
B: BlockT<Hash = H256> + Send + Sync + 'static,
P: TransactionPool<Block = B> + Send + Sync + 'static,
A: ChainApi<Block = B> + 'static,
C::Api: TxPoolRuntimeApi<B>,
{
fn map_build<T>(&self) -> RpcResult<TransactionMap<T>>
/// Use the transaction graph interface to get the extrinsics currently in the ready and future
/// queues.
fn map_build<T>(&self) -> RpcResult<TxPoolResult<TransactionMap<T>>>
where
T: GetT,
T: GetT + Serialize,
{
let txs: Vec<_> = self
.pool
// Collect transactions in the ready validated pool.
let txs_ready = self
.graph
.validated_pool()
.ready()
.map(|in_pool_tx| in_pool_tx.data().clone())
.collect();

// Collect transactions in the future validated pool.
let txs_future = self
.graph
.validated_pool()
.futures()
.iter()
.map(|(_hash, extrinsic)| extrinsic.clone())
.collect();

// Use the runtime to match the (here) opaque extrinsics against ethereum transactions.
let best_block: BlockId<B> = BlockId::Hash(self.client.info().best_hash);
let ethereum_txns = self
let ethereum_txns: TxPoolResponse = self
.client
.runtime_api()
.extrinsic_filter(&best_block, txs)
.extrinsic_filter(&best_block, txs_ready, txs_future)
.map_err(|err| {
internal_err(format!("fetch runtime extrinsic filter failed: {:?}", err))
})?;
let mut out = TransactionMap::<T>::new();
for txn in ethereum_txns.iter() {
// Build the T response.
let mut pending = TransactionMap::<T>::new();
for txn in ethereum_txns.ready.iter() {
let transaction_message = TransactionMessage::from(txn.clone());
let hash = transaction_message.hash();
let from_address = match public_key(txn) {
Ok(pk) => H160::from(H256::from_slice(Keccak256::digest(&pk).as_slice())),
Err(_e) => H160::default(),
};
out.entry(from_address)
pending
.entry(from_address)
.or_insert_with(HashMap::new)
.insert(txn.nonce, T::get(hash, from_address, txn));
}
Ok(out)
let mut queued = TransactionMap::<T>::new();
for txn in ethereum_txns.future.iter() {
let transaction_message = TransactionMessage::from(txn.clone());
let hash = transaction_message.hash();
let from_address = match public_key(txn) {
Ok(pk) => H160::from(H256::from_slice(Keccak256::digest(&pk).as_slice())),
Err(_e) => H160::default(),
};
queued
.entry(from_address)
.or_insert_with(HashMap::new)
.insert(txn.nonce, T::get(hash, from_address, txn));
}
Ok(TxPoolResult { pending, queued })
}
}

impl<B: BlockT, C, P> TxPool<B, C, P> {
pub fn new(client: Arc<C>, pool: Arc<P>) -> Self {
impl<B: BlockT, C, A: ChainApi> TxPool<B, C, A> {
pub fn new(client: Arc<C>, graph: Arc<Pool<A>>) -> Self {
Self {
client,
pool,
graph,
_marker: PhantomData,
}
}
}

impl<B, C, P> TxPoolT for TxPool<B, C, P>
impl<B, C, A> TxPoolT for TxPool<B, C, A>
where
C: ProvideRuntimeApi<B>,
C: HeaderMetadata<B, Error = BlockChainError> + HeaderBackend<B>,
C: Send + Sync + 'static,
B: BlockT<Hash = H256> + Send + Sync + 'static,
P: TransactionPool<Block = B> + Send + Sync + 'static,
A: ChainApi<Block = B> + 'static,
C::Api: TxPoolRuntimeApi<B>,
{
fn content(&self) -> RpcResult<TxPoolResult<TransactionMap<Transaction>>> {
let pending = self.map_build::<Transaction>()?;
Ok(TxPoolResult {
pending,
// Future queue not yet supported. We need to do something like:
// - Use InpoolTransaction::requires() to get the TransactionTag bytes.
// - Somehow decode and identify the tag to either add it to the future or pending pool.
queued: HashMap::new(),
})
self.map_build::<Transaction>()
}

fn inspect(&self) -> RpcResult<TxPoolResult<TransactionMap<Summary>>> {
let pending = self.map_build::<Summary>()?;
Ok(TxPoolResult {
pending,
queued: HashMap::new(),
})
self.map_build::<Summary>()
}

fn status(&self) -> RpcResult<TxPoolResult<U256>> {
let status = self.pool.status();
let status = self.graph.validated_pool().status();
Ok(TxPoolResult {
pending: U256::from(status.ready),
queued: U256::from(status.future),
Expand Down
1 change: 1 addition & 0 deletions node/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ sc-executor = { git = "https://github.com/paritytech/substrate", branch = "polka
sc-service = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.2" }
sc-telemetry = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.2" }
sc-transaction-pool = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.2" }
sc-transaction-graph = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.2" }
sp-transaction-pool = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.2" }
sc-network = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.2" }
sc-basic-authorship = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.2" }
Expand Down
2 changes: 2 additions & 0 deletions node/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ where
let deps = rpc::FullDeps {
client: client.clone(),
pool: pool.clone(),
graph: pool.pool().clone(),
deny_unsafe,
is_authority: collator,
network: network.clone(),
Expand Down Expand Up @@ -936,6 +937,7 @@ pub fn new_dev(
let deps = rpc::FullDeps {
client: client.clone(),
pool: pool.clone(),
graph: pool.pool().clone(),
deny_unsafe,
is_authority: collator,
network: network.clone(),
Expand Down
15 changes: 10 additions & 5 deletions node/service/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use sc_consensus_manual_seal::rpc::{EngineCommand, ManualSeal, ManualSealApi};
use sc_network::NetworkService;
use sc_rpc::SubscriptionTaskExecutor;
use sc_rpc_api::DenyUnsafe;
use sc_transaction_graph::{ChainApi, Pool};
use sp_api::ProvideRuntimeApi;
use sp_blockchain::{
Backend as BlockchainBackend, Error as BlockChainError, HeaderBackend, HeaderMetadata,
Expand All @@ -51,11 +52,13 @@ use std::collections::BTreeMap;
use substrate_frame_rpc_system::{FullSystem, SystemApi};

/// Full client dependencies.
pub struct FullDeps<C, P, BE> {
pub struct FullDeps<C, P, A: ChainApi, BE> {
/// The client instance to use.
pub client: Arc<C>,
/// Transaction pool instance.
pub pool: Arc<P>,
/// Graph pool instance.
pub graph: Arc<Pool<A>>,
/// Whether to deny unsafe calls
pub deny_unsafe: DenyUnsafe,
/// The Node authority flag
Expand Down Expand Up @@ -86,8 +89,8 @@ pub struct FullDeps<C, P, BE> {
pub transaction_converter: TransactionConverters,
}
/// Instantiate all Full RPC extensions.
pub fn create_full<C, P, BE>(
deps: FullDeps<C, P, BE>,
pub fn create_full<C, P, BE, A>(
deps: FullDeps<C, P, A, BE>,
subscription_task_executor: SubscriptionTaskExecutor,
) -> jsonrpc_core::IoHandler<sc_rpc::Metadata>
where
Expand All @@ -98,13 +101,15 @@ where
C: BlockchainEvents<Block>,
C: HeaderBackend<Block> + HeaderMetadata<Block, Error = BlockChainError> + 'static,
C: Send + Sync + 'static,
A: ChainApi<Block = Block> + 'static,
C::Api: RuntimeApiCollection<StateBackend = BE::State>,
P: TransactionPool<Block = Block> + 'static,
{
let mut io = jsonrpc_core::IoHandler::default();
let FullDeps {
client,
pool,
graph,
deny_unsafe,
is_authority,
network,
Expand Down Expand Up @@ -176,7 +181,7 @@ where
)));
io.extend_with(Web3ApiServer::to_delegate(Web3Api::new(client.clone())));
io.extend_with(EthPubSubApiServer::to_delegate(EthPubSubApi::new(
pool.clone(),
pool,
client.clone(),
network,
SubscriptionManager::<HexEncodedIdProvider>::with_id_provider(
Expand All @@ -188,7 +193,7 @@ where
if ethapi_cmd.contains(&EthApiCmd::Txpool) {
io.extend_with(TxPoolServer::to_delegate(TxPool::new(
Arc::clone(&client),
pool,
graph,
)));
}

Expand Down
1 change: 1 addition & 0 deletions primitives/rpc/txpool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license = 'GPL-3.0-only'
repository = 'https://github.com/PureStake/moonbeam/'

[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
ethereum = { version = "0.7.1", default-features = false, features = ["with-codec"] }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.2", default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.2", default-features = false }
Expand Down
12 changes: 11 additions & 1 deletion primitives/rpc/txpool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@
#![allow(clippy::unnecessary_mut_passed)]
#![allow(clippy::too_many_arguments)]

use codec::{Decode, Encode};
use ethereum::Transaction;
use sp_runtime::traits::Block as BlockT;
use sp_std::vec::Vec;

#[derive(Eq, PartialEq, Clone, Encode, Decode, sp_runtime::RuntimeDebug)]
pub struct TxPoolResponse {
pub ready: Vec<Transaction>,
pub future: Vec<Transaction>,
}

sp_api::decl_runtime_apis! {
pub trait TxPoolRuntimeApi {
fn extrinsic_filter(xt: Vec<<Block as BlockT>::Extrinsic>) -> Vec<Transaction>;
fn extrinsic_filter(
xt_ready: Vec<<Block as BlockT>::Extrinsic>,
xt_future: Vec<<Block as BlockT>::Extrinsic>,
) -> TxPoolResponse;
}
}
20 changes: 14 additions & 6 deletions runtime/moonbase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub use moonbeam_core_primitives::{
Signature,
};
use moonbeam_extensions_evm::runner::stack::TraceRunner as TraceRunnerT;
use moonbeam_rpc_primitives_txpool::TxPoolResponse;
use pallet_balances::NegativeImbalance;
use pallet_ethereum::Call::transact;
use pallet_ethereum::{Transaction as EthereumTransaction, TransactionAction};
Expand Down Expand Up @@ -1101,12 +1102,19 @@ impl_runtime_apis! {

impl moonbeam_rpc_primitives_txpool::TxPoolRuntimeApi<Block> for Runtime {
fn extrinsic_filter(
xts: Vec<<Block as BlockT>::Extrinsic>
) -> Vec<pallet_ethereum::Transaction> {
xts.into_iter().filter_map(|xt| match xt.function {
Call::Ethereum(transact(t)) => Some(t),
_ => None
}).collect()
xts_ready: Vec<<Block as BlockT>::Extrinsic>,
xts_future: Vec<<Block as BlockT>::Extrinsic>
) -> TxPoolResponse {
TxPoolResponse {
ready: xts_ready.into_iter().filter_map(|xt| match xt.function {
Call::Ethereum(transact(t)) => Some(t),
_ => None
}).collect(),
future: xts_future.into_iter().filter_map(|xt| match xt.function {
Call::Ethereum(transact(t)) => Some(t),
_ => None
}).collect(),
}
}
}

Expand Down
20 changes: 14 additions & 6 deletions runtime/moonbeam/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub use moonbeam_core_primitives::{
Signature,
};
use moonbeam_extensions_evm::runner::stack::TraceRunner as TraceRunnerT;
use moonbeam_rpc_primitives_txpool::TxPoolResponse;
use pallet_balances::NegativeImbalance;
use pallet_ethereum::Call::transact;
use pallet_ethereum::{Transaction as EthereumTransaction, TransactionAction};
Expand Down Expand Up @@ -1117,12 +1118,19 @@ impl_runtime_apis! {

impl moonbeam_rpc_primitives_txpool::TxPoolRuntimeApi<Block> for Runtime {
fn extrinsic_filter(
xts: Vec<<Block as BlockT>::Extrinsic>
) -> Vec<pallet_ethereum::Transaction> {
xts.into_iter().filter_map(|xt| match xt.function {
Call::Ethereum(transact(t)) => Some(t),
_ => None
}).collect()
xts_ready: Vec<<Block as BlockT>::Extrinsic>,
xts_future: Vec<<Block as BlockT>::Extrinsic>
) -> TxPoolResponse {
TxPoolResponse {
ready: xts_ready.into_iter().filter_map(|xt| match xt.function {
Call::Ethereum(transact(t)) => Some(t),
_ => None
}).collect(),
future: xts_future.into_iter().filter_map(|xt| match xt.function {
Call::Ethereum(transact(t)) => Some(t),
_ => None
}).collect(),
}
}
}

Expand Down
21 changes: 14 additions & 7 deletions runtime/moonriver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub use moonbeam_core_primitives::{
Signature,
};
use moonbeam_extensions_evm::runner::stack::TraceRunner as TraceRunnerT;
use moonbeam_rpc_primitives_txpool::TxPoolResponse;
use pallet_balances::NegativeImbalance;
use pallet_ethereum::Call::transact;
use pallet_ethereum::{Transaction as EthereumTransaction, TransactionAction};
Expand Down Expand Up @@ -1116,15 +1117,21 @@ impl_runtime_apis! {

impl moonbeam_rpc_primitives_txpool::TxPoolRuntimeApi<Block> for Runtime {
fn extrinsic_filter(
xts: Vec<<Block as BlockT>::Extrinsic>
) -> Vec<pallet_ethereum::Transaction> {
xts.into_iter().filter_map(|xt| match xt.function {
Call::Ethereum(transact(t)) => Some(t),
_ => None
}).collect()
xts_ready: Vec<<Block as BlockT>::Extrinsic>,
xts_future: Vec<<Block as BlockT>::Extrinsic>
) -> TxPoolResponse {
TxPoolResponse {
ready: xts_ready.into_iter().filter_map(|xt| match xt.function {
Call::Ethereum(transact(t)) => Some(t),
_ => None
}).collect(),
future: xts_future.into_iter().filter_map(|xt| match xt.function {
Call::Ethereum(transact(t)) => Some(t),
_ => None
}).collect(),
}
}
}

impl fp_rpc::EthereumRuntimeRPCApi<Block> for Runtime {
fn chain_id() -> u64 {
<Runtime as pallet_evm::Config>::ChainId::get()
Expand Down
Loading