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

Commit

Permalink
RPC: parity_allTransactionHashes (#9745)
Browse files Browse the repository at this point in the history
* rpc: add parity_allTransactionHashes

* Add light_all_transactionst to helpers

* Remove extra parentheses

* Move light_all_transactions to light_fetch

* Remove LightDispatcher import in light_fetch
  • Loading branch information
fanatid authored and niklasad1 committed Oct 27, 2018
1 parent 6643b6a commit f8f8bf0
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 12 deletions.
4 changes: 4 additions & 0 deletions ethcore/src/miner/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,10 @@ impl miner::MinerService for Miner {
self.transaction_queue.all_transactions()
}

fn queued_transaction_hashes(&self) -> Vec<H256> {
self.transaction_queue.all_transaction_hashes()
}

fn pending_transaction_hashes<C>(&self, chain: &C) -> BTreeSet<H256> where
C: ChainInfo + Sync,
{
Expand Down
3 changes: 3 additions & 0 deletions ethcore/src/miner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ pub trait MinerService : Send + Sync {
/// Get a list of all transactions in the pool (some of them might not be ready for inclusion yet).
fn queued_transactions(&self) -> Vec<Arc<VerifiedTransaction>>;

/// Get a list of all transaction hashes in the pool (some of them might not be ready for inclusion yet).
fn queued_transaction_hashes(&self) -> Vec<H256>;

/// Get a list of local transactions with statuses.
fn local_transactions(&self) -> BTreeMap<H256, local_transactions::Status>;

Expand Down
6 changes: 6 additions & 0 deletions miner/src/pool/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@ impl TransactionQueue {
self.pool.read().unordered_pending(ready).collect()
}

/// Returns all transaction hashes in the queue without explicit ordering.
pub fn all_transaction_hashes(&self) -> Vec<H256> {
let ready = |_tx: &pool::VerifiedTransaction| txpool::Readiness::Ready;
self.pool.read().unordered_pending(ready).map(|tx| tx.hash).collect()
}

/// Computes unordered set of pending hashes.
///
/// Since strict nonce-checking is not required, you may get some false positive future transactions as well.
Expand Down
11 changes: 10 additions & 1 deletion rpc/src/v1/helpers/light_fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use ethereum_types::{U256, Address};
use hash::H256;
use parking_lot::Mutex;
use fastmap::H256FastMap;
use transaction::{Action, Transaction as EthTransaction, SignedTransaction, LocalizedTransaction};
use transaction::{Action, Transaction as EthTransaction, PendingTransaction, SignedTransaction, LocalizedTransaction};

use v1::helpers::{CallRequest as CallRequestHelper, errors, dispatch};
use v1::types::{BlockNumber, CallRequest, Log, Transaction};
Expand All @@ -54,6 +54,15 @@ const NO_INVALID_BACK_REFS_PROOF: &str = "Fails only on invalid back-references;

const WRONG_RESPONSE_AMOUNT_TYPE_PROOF: &str = "responses correspond directly with requests in amount and type; qed";

pub fn light_all_transactions(dispatch: &Arc<dispatch::LightDispatcher>) -> impl Iterator<Item=PendingTransaction> {
let txq = dispatch.transaction_queue.read();
let chain_info = dispatch.client.chain_info();

let current = txq.ready_transactions(chain_info.best_block_number, chain_info.best_block_timestamp);
let future = txq.future_transactions(chain_info.best_block_number, chain_info.best_block_timestamp);
current.into_iter().chain(future.into_iter())
}

/// Helper for fetching blockchain data either from the light client or the network
/// as necessary.
#[derive(Clone)]
Expand Down
2 changes: 1 addition & 1 deletion rpc/src/v1/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod subscribers;
mod subscription_manager;
mod work;

pub use self::dispatch::{Dispatcher, FullDispatcher};
pub use self::dispatch::{Dispatcher, FullDispatcher, LightDispatcher};
pub use self::network_settings::NetworkSettings;
pub use self::poll_manager::PollManager;
pub use self::poll_filter::{PollFilter, SyncPollFilter, limit_logs};
Expand Down
21 changes: 11 additions & 10 deletions rpc/src/v1/impls/light/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use jsonrpc_core::futures::Future;
use jsonrpc_macros::Trailing;
use v1::helpers::{self, errors, ipfs, SigningQueue, SignerService, NetworkSettings};
use v1::helpers::dispatch::LightDispatcher;
use v1::helpers::light_fetch::LightFetch;
use v1::helpers::light_fetch::{LightFetch, light_all_transactions};
use v1::metadata::Metadata;
use v1::traits::Parity;
use v1::types::{
Expand Down Expand Up @@ -258,17 +258,18 @@ impl Parity for ParityClient {
}

fn all_transactions(&self) -> Result<Vec<Transaction>> {
let txq = self.light_dispatch.transaction_queue.read();
let chain_info = self.light_dispatch.client.chain_info();

let current = txq.ready_transactions(chain_info.best_block_number, chain_info.best_block_timestamp);
let future = txq.future_transactions(chain_info.best_block_number, chain_info.best_block_timestamp);
Ok(
current
.into_iter()
.chain(future.into_iter())
light_all_transactions(&self.light_dispatch)
.map(|tx| Transaction::from_pending(tx))
.collect::<Vec<_>>()
.collect()
)
}

fn all_transaction_hashes(&self) -> Result<Vec<H256>> {
Ok(
light_all_transactions(&self.light_dispatch)
.map(|tx| tx.transaction.hash().into())
.collect()
)
}

Expand Down
10 changes: 10 additions & 0 deletions rpc/src/v1/impls/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,16 @@ impl<C, M, U, S> Parity for ParityClient<C, M, U> where
)
}

fn all_transaction_hashes(&self) -> Result<Vec<H256>> {
let all_transaction_hashes = self.miner.queued_transaction_hashes();

Ok(all_transaction_hashes
.into_iter()
.map(|hash| hash.into())
.collect()
)
}

fn future_transactions(&self) -> Result<Vec<Transaction>> {
Err(errors::deprecated("Use `parity_allTransaction` instead."))
}
Expand Down
4 changes: 4 additions & 0 deletions rpc/src/v1/tests/helpers/miner_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ impl MinerService for TestMinerService {
}).collect()
}

fn queued_transaction_hashes(&self) -> Vec<H256> {
self.pending_transactions.lock().keys().cloned().map(|hash| hash).collect()
}

fn pending_receipts(&self, _best_block: BlockNumber) -> Option<Vec<RichReceipt>> {
Some(self.pending_receipts.lock().clone())
}
Expand Down
4 changes: 4 additions & 0 deletions rpc/src/v1/traits/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ build_rpc_trait! {
#[rpc(name = "parity_allTransactions")]
fn all_transactions(&self) -> Result<Vec<Transaction>>;

/// Same as parity_allTransactions, but return only transactions hashes.
#[rpc(name = "parity_allTransactionHashes")]
fn all_transaction_hashes(&self) -> Result<Vec<H256>>;

/// Returns all future transactions from transaction queue (deprecated)
#[rpc(name = "parity_futureTransactions")]
fn future_transactions(&self) -> Result<Vec<Transaction>>;
Expand Down

0 comments on commit f8f8bf0

Please sign in to comment.