From 4df096fed3d68e0f44283e30352d5c543ddf95be Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 10 Feb 2016 00:12:09 +0100 Subject: [PATCH] optimize blockchains transaction_at --- ethcore/src/blockchain.rs | 2 +- ethcore/src/views.rs | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/ethcore/src/blockchain.rs b/ethcore/src/blockchain.rs index af7800870..c88a375a8 100644 --- a/ethcore/src/blockchain.rs +++ b/ethcore/src/blockchain.rs @@ -117,7 +117,7 @@ pub trait BlockProvider { /// Get transaction at given address. fn transaction_at(&self, address: &TransactionAddress) -> Option { - self.block(&address.block_hash).map(|bytes| BlockView::new(&bytes).localized_transactions()).and_then(|t| t.into_iter().nth(address.index)) + self.block(&address.block_hash).and_then(|bytes| BlockView::new(&bytes).localized_transaction_at(address.index)) } /// Get a list of transactions for a given block. diff --git a/ethcore/src/views.rs b/ethcore/src/views.rs index 3cfe5f183..4a7ff054d 100644 --- a/ethcore/src/views.rs +++ b/ethcore/src/views.rs @@ -160,7 +160,7 @@ impl<'a> BlockView<'a> { let header = self.header_view(); let block_hash = header.sha3(); let block_number = header.number(); - self.rlp.val_at::>(1) + self.transactions() .into_iter() .enumerate() .map(|(i, t)| LocalizedTransaction { @@ -186,6 +186,24 @@ impl<'a> BlockView<'a> { self.rlp.at(1).iter().map(|rlp| rlp.as_raw().sha3()).collect() } + /// Returns transaction at given index without deserializing unnecessary data. + pub fn transaction_at(&self, index: usize) -> Option { + self.rlp.at(1).iter().nth(index).map(|rlp| rlp.as_val()) + } + + /// Returns localized transaction at given index. + pub fn localized_transaction_at(&self, index: usize) -> Option { + let header = self.header_view(); + let block_hash = header.sha3(); + let block_number = header.number(); + self.transaction_at(index).map(|t| LocalizedTransaction { + signed: t, + block_hash: block_hash, + block_number: block_number, + transaction_index: index + }) + } + /// Return list of uncles of given block. pub fn uncles(&self) -> Vec
{ self.rlp.val_at(2)