block and transaction ids, jsonrpcs eth_getTransactionByHash

This commit is contained in:
debris
2016-02-10 11:28:40 +01:00
parent b86ddbb923
commit 626277ef9a
6 changed files with 119 additions and 17 deletions

View File

@@ -111,17 +111,22 @@ pub trait BlockProvider {
}
/// Get transaction with given transaction hash.
fn transaction(&self, hash: &H256) -> Option<LocalizedTransaction> {
self.transaction_address(hash).and_then(|address| self.transaction_at(&address))
}
/// Get transaction at given address.
fn transaction_at(&self, address: &TransactionAddress) -> Option<LocalizedTransaction> {
self.block(&address.block_hash).and_then(|bytes| BlockView::new(&bytes).localized_transaction_at(address.index))
fn transaction(&self, id: TransactionId) -> Option<LocalizedTransaction> {
match id {
TransactionId::Hash(ref hash) => self.transaction_address(hash),
TransactionId::BlockPosition(BlockId::Hash(hash), index) => Some(TransactionAddress {
block_hash: hash,
index: index
}),
TransactionId::BlockPosition(BlockId::Number(number), index) => self.block_hash(number).map(|hash| TransactionAddress {
block_hash: hash,
index: index
})
}.and_then(|address| 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.
/// Returns None if block deos not exist.
/// Returns None if block does not exist.
fn transactions(&self, hash: &H256) -> Option<Vec<LocalizedTransaction>> {
self.block(hash).map(|bytes| BlockView::new(&bytes).localized_transactions())
}
@@ -669,6 +674,7 @@ mod tests {
use util::hash::*;
use blockchain::*;
use tests::helpers::*;
use views::TransactionId;
#[test]
fn valid_tests_extra32() {
@@ -864,7 +870,7 @@ mod tests {
let transactions = bc.transactions(&b1_hash).unwrap();
assert_eq!(transactions.len(), 7);
for t in transactions {
assert_eq!(bc.transaction(&t.hash()).unwrap(), t);
assert_eq!(bc.transaction(TransactionId::Hash(t.hash())).unwrap(), t);
}
}
}

View File

@@ -19,7 +19,7 @@
use util::*;
use rocksdb::{Options, DB, DBCompactionStyle};
use blockchain::{BlockChain, BlockProvider, CacheSize};
use views::BlockView;
use views::{BlockView, TransactionId};
use error::*;
use header::BlockNumber;
use state::State;
@@ -106,7 +106,7 @@ pub trait BlockChainClient : Sync + Send {
fn block_total_difficulty_at(&self, n: BlockNumber) -> Option<U256>;
/// Get transaction with given hash.
fn transaction(&self, hash: &H256) -> Option<LocalizedTransaction>;
fn transaction(&self, id: TransactionId) -> Option<LocalizedTransaction>;
/// Get a tree route between `from` and `to`.
/// See `BlockChain::tree_route`.
@@ -392,8 +392,8 @@ impl BlockChainClient for Client {
self.chain.read().unwrap().block_hash(n).and_then(|h| self.block_total_difficulty(&h))
}
fn transaction(&self, hash: &H256) -> Option<LocalizedTransaction> {
self.chain.read().unwrap().transaction(hash)
fn transaction(&self, id: TransactionId) -> Option<LocalizedTransaction> {
self.chain.read().unwrap().transaction(id)
}
fn tree_route(&self, from: &H256, to: &H256) -> Option<TreeRoute> {

View File

@@ -19,6 +19,22 @@ use util::*;
use header::*;
use transaction::*;
/// Uniqly identifies block in canon blockchain.
pub enum BlockId {
/// Block's sha3.
Hash(H256),
/// Block number.
Number(BlockNumber)
}
/// Uniqly identifies transaction in canon blockchain.
pub enum TransactionId {
/// Transaction's sha3.
Hash(H256),
/// Block id and transaction index within this block.
BlockPosition(BlockId, usize)
}
/// View onto transaction rlp.
pub struct TransactionView<'a> {
rlp: Rlp<'a>