From 254ac6f2538fa6e4d9f6153a64c81d9679477fe7 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Sun, 27 Mar 2016 16:12:21 +0300 Subject: [PATCH] checking tx queue --- miner/src/lib.rs | 3 +++ miner/src/miner.rs | 5 +++++ miner/src/transaction_queue.rs | 5 +++++ rpc/src/v1/impls/eth.rs | 8 +++++++- rpc/src/v1/tests/helpers/miner_service.rs | 4 ++++ rpc/src/v1/types/transaction.rs | 23 ++++++++++++++++++++++- 6 files changed, 46 insertions(+), 2 deletions(-) diff --git a/miner/src/lib.rs b/miner/src/lib.rs index 06faf057e..74c8e1a1a 100644 --- a/miner/src/lib.rs +++ b/miner/src/lib.rs @@ -105,6 +105,9 @@ pub trait MinerService : Send + Sync { /// Submit `seal` as a valid solution for the header of `pow_hash`. /// Will check the seal, but not actually insert the block into the chain. fn submit_seal(&self, chain: &BlockChainClient, pow_hash: H256, seal: Vec) -> Result<(), Error>; + + /// Query pending transactions for hash + fn transaction(&self, hash: &H256) -> Option; } /// Mining status diff --git a/miner/src/miner.rs b/miner/src/miner.rs index e989fa495..529e86bae 100644 --- a/miner/src/miner.rs +++ b/miner/src/miner.rs @@ -164,6 +164,11 @@ impl MinerService for Miner { transaction_queue.pending_hashes() } + fn transaction(&self, hash: &H256) -> Option { + let queue = self.transaction_queue.lock().unwrap(); + queue.find(hash) + } + fn update_sealing(&self, chain: &BlockChainClient) { if self.sealing_enabled.load(atomic::Ordering::Relaxed) { let current_no = chain.chain_info().best_block_number; diff --git a/miner/src/transaction_queue.rs b/miner/src/transaction_queue.rs index 9abe80f1c..8f2855836 100644 --- a/miner/src/transaction_queue.rs +++ b/miner/src/transaction_queue.rs @@ -504,6 +504,11 @@ impl TransactionQueue { .collect() } + /// Finds transaction in the queue by hash (if any) + pub fn find(&self, hash: &H256) -> Option { + match self.by_hash.get(hash) { Some(transaction_ref) => Some(transaction_ref.transaction.clone()), None => None } + } + /// Removes all elements (in any state) from the queue pub fn clear(&mut self) { self.current.clear(); diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index a68fc91fd..c042369bf 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -348,7 +348,13 @@ impl Eth for EthClient fn transaction_by_hash(&self, params: Params) -> Result { from_params::<(H256,)>(params) - .and_then(|(hash,)| self.transaction(TransactionId::Hash(hash))) + .and_then(|(hash,)| { + let miner = take_weak!(self.miner); + match miner.transaction(&hash) { + Some(pending_tx) => to_value(&Transaction::from(pending_tx)), + None => self.transaction(TransactionId::Hash(hash)) + } + }) } fn transaction_by_block_hash_and_index(&self, params: Params) -> Result { diff --git a/rpc/src/v1/tests/helpers/miner_service.rs b/rpc/src/v1/tests/helpers/miner_service.rs index 7f07341bf..1cf44f1db 100644 --- a/rpc/src/v1/tests/helpers/miner_service.rs +++ b/rpc/src/v1/tests/helpers/miner_service.rs @@ -73,6 +73,10 @@ impl MinerService for TestMinerService { &self.latest_closed_block } + fn transaction(&self, _hash: &H256) -> Option { + unimplemented!(); + } + /// Submit `seal` as a valid solution for the header of `pow_hash`. /// Will check the seal, but not actually insert the block into the chain. fn submit_seal(&self, _chain: &BlockChainClient, _pow_hash: H256, _seal: Vec) -> Result<(), Error> { unimplemented!(); } diff --git a/rpc/src/v1/types/transaction.rs b/rpc/src/v1/types/transaction.rs index d809d19b4..8a46d5e15 100644 --- a/rpc/src/v1/types/transaction.rs +++ b/rpc/src/v1/types/transaction.rs @@ -15,7 +15,7 @@ // along with Parity. If not, see . use util::numbers::*; -use ethcore::transaction::{LocalizedTransaction, Action}; +use ethcore::transaction::{LocalizedTransaction, Action, SignedTransaction}; use v1::types::{Bytes, OptionalValue}; #[derive(Debug, Default, Serialize)] @@ -58,6 +58,27 @@ impl From for Transaction { } } +impl From for Transaction { + fn from(t: SignedTransaction) -> Transaction { + Transaction { + hash: t.hash(), + nonce: t.nonce, + block_hash: OptionalValue::Null, + block_number: OptionalValue::Null, + transaction_index: OptionalValue::Null, + from: t.sender().unwrap(), + to: match t.action { + Action::Create => OptionalValue::Null, + Action::Call(ref address) => OptionalValue::Value(address.clone()) + }, + value: t.value, + gas_price: t.gas_price, + gas: t.gas, + input: Bytes::new(t.data.clone()) + } + } +} + #[cfg(test)] mod tests { use super::*;