transaction by block number and index
This commit is contained in:
parent
df0fa06e8a
commit
93975be5e3
@ -37,15 +37,21 @@ use extras::TransactionAddress;
|
||||
pub use blockchain::TreeRoute;
|
||||
|
||||
/// Uniquely identifies block.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum BlockId {
|
||||
/// Block's sha3.
|
||||
/// Querying by hash is always faster.
|
||||
Hash(H256),
|
||||
/// Block number within canon blockchain.
|
||||
Number(BlockNumber)
|
||||
Number(BlockNumber),
|
||||
/// Earliest block (genesis).
|
||||
Earliest,
|
||||
/// Latest mined block.
|
||||
Latest
|
||||
}
|
||||
|
||||
/// Uniquely identifies transaction.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum TransactionId {
|
||||
/// Transaction's sha3.
|
||||
Hash(H256),
|
||||
@ -347,7 +353,9 @@ impl Client {
|
||||
fn block_hash(&self, id: BlockId) -> Option<H256> {
|
||||
match id {
|
||||
BlockId::Hash(hash) => Some(hash),
|
||||
BlockId::Number(number) => self.chain.read().unwrap().block_hash(number)
|
||||
BlockId::Number(number) => self.chain.read().unwrap().block_hash(number),
|
||||
BlockId::Earliest => self.chain.read().unwrap().block_hash(0),
|
||||
BlockId::Latest => Some(self.chain.read().unwrap().best_block_hash())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -183,8 +183,12 @@ impl Eth for EthClient {
|
||||
})
|
||||
}
|
||||
|
||||
fn transaction_by_block_number_and_index(&self, _params: Params) -> Result<Value, Error> {
|
||||
unimplemented!()
|
||||
fn transaction_by_block_number_and_index(&self, params: Params) -> Result<Value, Error> {
|
||||
from_params::<(BlockNumber, Index)>(params)
|
||||
.and_then(|(number, index)| match self.client.transaction(TransactionId::Location(number.into(), index.value())) {
|
||||
Some(t) => to_value(&Transaction::from(t)),
|
||||
None => Ok(Value::Null)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
use serde::{Deserialize, Deserializer, Error};
|
||||
use serde::de::Visitor;
|
||||
use ethcore::client::BlockId;
|
||||
|
||||
/// Represents rpc api block number param.
|
||||
#[derive(Debug, PartialEq)]
|
||||
@ -53,8 +54,20 @@ impl Visitor for BlockNumberVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<BlockId> for BlockNumber {
|
||||
fn into(self) -> BlockId {
|
||||
match self {
|
||||
BlockNumber::Num(n) => BlockId::Number(n),
|
||||
BlockNumber::Earliest => BlockId::Earliest,
|
||||
BlockNumber::Latest => BlockId::Latest,
|
||||
BlockNumber::Pending => BlockId::Latest // TODO: change this once blockid support pending
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use ethcore::client::BlockId;
|
||||
use super::*;
|
||||
use serde_json;
|
||||
|
||||
@ -64,5 +77,13 @@ mod tests {
|
||||
let deserialized: Vec<BlockNumber> = serde_json::from_str(s).unwrap();
|
||||
assert_eq!(deserialized, vec![BlockNumber::Num(10), BlockNumber::Num(10), BlockNumber::Latest, BlockNumber::Earliest, BlockNumber::Pending])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn block_number_into() {
|
||||
assert_eq!(BlockId::Number(100), BlockNumber::Num(100).into());
|
||||
assert_eq!(BlockId::Earliest, BlockNumber::Earliest.into());
|
||||
assert_eq!(BlockId::Latest, BlockNumber::Latest.into());
|
||||
assert_eq!(BlockId::Latest, BlockNumber::Pending.into());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,7 +80,9 @@ impl TestBlockChainClient {
|
||||
fn block_hash(&self, id: BlockId) -> Option<H256> {
|
||||
match id {
|
||||
BlockId::Hash(hash) => Some(hash),
|
||||
BlockId::Number(n) => self.numbers.read().unwrap().get(&(n as usize)).cloned()
|
||||
BlockId::Number(n) => self.numbers.read().unwrap().get(&(n as usize)).cloned(),
|
||||
BlockId::Earliest => self.numbers.read().unwrap().get(&0).cloned(),
|
||||
BlockId::Latest => self.numbers.read().unwrap().get(&(self.numbers.read().unwrap().len() - 1)).cloned()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user