block and transaction ids, jsonrpcs eth_getTransactionByHash
This commit is contained in:
@@ -23,7 +23,7 @@ use util::sha3::*;
|
||||
use ethcore::client::*;
|
||||
use ethcore::views::*;
|
||||
use v1::traits::{Eth, EthFilter};
|
||||
use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, Transaction, OptionalValue};
|
||||
use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, Transaction, OptionalValue, Index};
|
||||
|
||||
/// Eth rpc implementation.
|
||||
pub struct EthClient {
|
||||
@@ -152,15 +152,27 @@ impl Eth for EthClient {
|
||||
to_value(&block)
|
||||
},
|
||||
_ => Ok(Value::Null)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn transaction_by_hash(&self, params: Params) -> Result<Value, Error> {
|
||||
from_params::<(H256,)>(params)
|
||||
.and_then(|(hash,)| match self.client.transaction(&hash) {
|
||||
.and_then(|(hash,)| match self.client.transaction(TransactionId::Hash(hash)) {
|
||||
Some(t) => to_value(&Transaction::from(t)),
|
||||
None => Ok(Value::Null)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn transaction_by_block_hash_and_index(&self, params: Params) -> Result<Value, Error> {
|
||||
from_params::<(H256, Index)>(params)
|
||||
.and_then(|(hash, index)| match self.client.transaction(TransactionId::BlockPosition(BlockId::Hash(hash), index.value())) {
|
||||
Some(t) => to_value(&Transaction::from(t)),
|
||||
None => Ok(Value::Null)
|
||||
})
|
||||
}
|
||||
|
||||
fn transaction_by_block_number_and_index(&self, _params: Params) -> Result<Value, Error> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
66
rpc/src/v1/types/index.rs
Normal file
66
rpc/src/v1/types/index.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
||||
// This file is part of Parity.
|
||||
|
||||
// Parity is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Parity is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use serde::{Deserialize, Deserializer, Error};
|
||||
use serde::de::Visitor;
|
||||
|
||||
/// Represents usize.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct Index(usize);
|
||||
|
||||
impl Index {
|
||||
pub fn value(&self) -> usize {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Deserialize for Index {
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Index, D::Error>
|
||||
where D: Deserializer {
|
||||
deserializer.visit(IndexVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
struct IndexVisitor;
|
||||
|
||||
impl Visitor for IndexVisitor {
|
||||
type Value = Index;
|
||||
|
||||
fn visit_str<E>(&mut self, value: &str) -> Result<Self::Value, E> where E: Error {
|
||||
match value {
|
||||
_ if value.starts_with("0x") => usize::from_str_radix(&value[2..], 16).map(Index).map_err(|_| Error::syntax("invalid index")),
|
||||
_ => value.parse::<usize>().map(Index).map_err(|_| Error::syntax("invalid index"))
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_string<E>(&mut self, value: String) -> Result<Self::Value, E> where E: Error {
|
||||
self.visit_str(value.as_ref())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json;
|
||||
|
||||
#[test]
|
||||
fn block_number_deserialization() {
|
||||
let s = r#"["0xa", "10"]"#;
|
||||
let deserialized: Vec<Index> = serde_json::from_str(s).unwrap();
|
||||
assert_eq!(deserialized, vec![Index(10), Index(10)]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ mod block;
|
||||
mod block_number;
|
||||
mod bytes;
|
||||
mod filter;
|
||||
mod index;
|
||||
mod optionals;
|
||||
mod sync;
|
||||
mod transaction;
|
||||
@@ -26,6 +27,7 @@ pub use self::block::{Block, BlockTransactions};
|
||||
pub use self::block_number::BlockNumber;
|
||||
pub use self::bytes::Bytes;
|
||||
pub use self::filter::Filter;
|
||||
pub use self::index::Index;
|
||||
pub use self::optionals::OptionalValue;
|
||||
pub use self::sync::SyncStatus;
|
||||
pub use self::transaction::Transaction;
|
||||
|
||||
Reference in New Issue
Block a user