optimize blockchains transaction_at

This commit is contained in:
debris 2016-02-10 00:12:09 +01:00
parent d0792dff31
commit 4df096fed3
2 changed files with 20 additions and 2 deletions

View File

@ -117,7 +117,7 @@ pub trait BlockProvider {
/// Get transaction at given address.
fn transaction_at(&self, address: &TransactionAddress) -> Option<LocalizedTransaction> {
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.

View File

@ -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::<Vec<SignedTransaction>>(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<SignedTransaction> {
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<LocalizedTransaction> {
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<Header> {
self.rlp.val_at(2)