jsonrpc optionals

This commit is contained in:
debris 2016-02-09 13:17:44 +01:00
parent 40068c1938
commit c50eb78ca1
9 changed files with 121 additions and 26 deletions

View File

@ -31,6 +31,7 @@ use service::{NetSyncMessage, SyncMessage};
use env_info::LastHashes; use env_info::LastHashes;
use verification::*; use verification::*;
use block::*; use block::*;
use transaction::SignedTransaction;
pub use blockchain::TreeRoute; pub use blockchain::TreeRoute;
/// General block status /// General block status
@ -104,6 +105,9 @@ pub trait BlockChainClient : Sync + Send {
/// Get block total difficulty. /// Get block total difficulty.
fn block_total_difficulty_at(&self, n: BlockNumber) -> Option<U256>; fn block_total_difficulty_at(&self, n: BlockNumber) -> Option<U256>;
/// Get transaction with given hash.
fn transaction(&self, hash: &H256) -> Option<SignedTransaction>;
/// Get a tree route between `from` and `to`. /// Get a tree route between `from` and `to`.
/// See `BlockChain::tree_route`. /// See `BlockChain::tree_route`.
fn tree_route(&self, from: &H256, to: &H256) -> Option<TreeRoute>; fn tree_route(&self, from: &H256, to: &H256) -> Option<TreeRoute>;
@ -388,6 +392,10 @@ impl BlockChainClient for Client {
self.chain.read().unwrap().block_hash(n).and_then(|h| self.block_total_difficulty(&h)) self.chain.read().unwrap().block_hash(n).and_then(|h| self.block_total_difficulty(&h))
} }
fn transaction(&self, hash: &H256) -> Option<SignedTransaction> {
self.chain.read().unwrap().transaction(hash)
}
fn tree_route(&self, from: &H256, to: &H256) -> Option<TreeRoute> { fn tree_route(&self, from: &H256, to: &H256) -> Option<TreeRoute> {
self.chain.read().unwrap().tree_route(from.clone(), to.clone()) self.chain.read().unwrap().tree_route(from.clone(), to.clone())
} }

View File

@ -98,6 +98,7 @@ pub mod ethereum;
pub mod header; pub mod header;
pub mod service; pub mod service;
pub mod spec; pub mod spec;
pub mod transaction;
pub mod views; pub mod views;
pub mod receipt; pub mod receipt;
@ -115,7 +116,6 @@ mod state;
mod account; mod account;
mod account_db; mod account_db;
mod action_params; mod action_params;
mod transaction;
mod null_engine; mod null_engine;
mod builtin; mod builtin;
mod extras; mod extras;

View File

@ -156,8 +156,7 @@ impl Transaction {
} }
} }
/// Signed transaction information.
#[derive(Debug, Clone, Eq)] #[derive(Debug, Clone, Eq)]
pub struct SignedTransaction { pub struct SignedTransaction {
/// Plain Transaction. /// Plain Transaction.

View File

@ -22,8 +22,9 @@ use util::uint::*;
use util::sha3::*; use util::sha3::*;
use ethcore::client::*; use ethcore::client::*;
use ethcore::views::*; use ethcore::views::*;
use ethcore::transaction::Action;
use v1::traits::{Eth, EthFilter}; use v1::traits::{Eth, EthFilter};
use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus}; use v1::types::{Block, BlockTransactions, BlockNumber, Bytes, SyncStatus, Transaction, OptionalValue};
/// Eth rpc implementation. /// Eth rpc implementation.
pub struct EthClient { pub struct EthClient {
@ -129,7 +130,7 @@ impl Eth for EthClient {
(Some(bytes), Some(total_difficulty)) => { (Some(bytes), Some(total_difficulty)) => {
let view = HeaderView::new(&bytes); let view = HeaderView::new(&bytes);
let block = Block { let block = Block {
hash: view.sha3(), hash: OptionalValue::Value(view.sha3()),
parent_hash: view.parent_hash(), parent_hash: view.parent_hash(),
uncles_hash: view.uncles_hash(), uncles_hash: view.uncles_hash(),
author: view.author(), author: view.author(),
@ -137,7 +138,7 @@ impl Eth for EthClient {
state_root: view.state_root(), state_root: view.state_root(),
transactions_root: view.transactions_root(), transactions_root: view.transactions_root(),
receipts_root: view.receipts_root(), receipts_root: view.receipts_root(),
number: U256::from(view.number()), number: OptionalValue::Value(U256::from(view.number())),
gas_used: view.gas_used(), gas_used: view.gas_used(),
gas_limit: view.gas_limit(), gas_limit: view.gas_limit(),
logs_bloom: view.log_bloom(), logs_bloom: view.log_bloom(),
@ -161,8 +162,34 @@ impl Eth for EthClient {
Err(err) => Err(err) Err(err) => Err(err)
} }
} }
fn transaction_at(&self, params: Params) -> Result<Value, Error> {
match from_params::<H256>(params) {
Ok(hash) => match self.client.transaction(&hash) {
Some(t) => to_value(&Transaction {
hash: t.hash(),
nonce: t.nonce,
block_hash: OptionalValue::Value(H256::default()), // todo
block_number: OptionalValue::Value(U256::default()), // todo
transaction_index: U256::default(), // todo
from: t.sender().unwrap(),
to: match t.action {
Action::Create => OptionalValue::Null,
Action::Call(ref address) => OptionalValue::Value(address.clone())
},
value: t.value,
gas_price: t.gas_price,
gas: t.gas,
input: Bytes::new(t.data.clone())
}),
None => Ok(Value::Null)
},
Err(err) => Err(err)
}
}
} }
/// Eth filter rpc implementation. /// Eth filter rpc implementation.
pub struct EthFilterClient { pub struct EthFilterClient {
client: Arc<Client> client: Arc<Client>

View File

@ -131,6 +131,7 @@ pub trait Eth: Sized + Send + Sync + 'static {
delegate.add_method("eth_estimateGas", Eth::estimate_gas); delegate.add_method("eth_estimateGas", Eth::estimate_gas);
delegate.add_method("eth_getBlockByHash", Eth::block); delegate.add_method("eth_getBlockByHash", Eth::block);
delegate.add_method("eth_getBlockByNumber", Eth::block); delegate.add_method("eth_getBlockByNumber", Eth::block);
delegate.add_method("eth_getTransactionByHash", Eth::transaction_at);
delegate.add_method("eth_getTransactionByBlockHashAndIndex", Eth::transaction_at); delegate.add_method("eth_getTransactionByBlockHashAndIndex", Eth::transaction_at);
delegate.add_method("eth_getTransactionByBlockNumberAndIndex", Eth::transaction_at); delegate.add_method("eth_getTransactionByBlockNumberAndIndex", Eth::transaction_at);
delegate.add_method("eth_getTransactionReceipt", Eth::transaction_receipt); delegate.add_method("eth_getTransactionReceipt", Eth::transaction_receipt);

View File

@ -17,7 +17,7 @@
use serde::{Serialize, Serializer}; use serde::{Serialize, Serializer};
use util::hash::*; use util::hash::*;
use util::uint::*; use util::uint::*;
use v1::types::{Bytes, Transaction}; use v1::types::{Bytes, Transaction, OptionalValue};
#[derive(Debug)] #[derive(Debug)]
pub enum BlockTransactions { pub enum BlockTransactions {
@ -37,7 +37,7 @@ impl Serialize for BlockTransactions {
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub struct Block { pub struct Block {
pub hash: H256, pub hash: OptionalValue<H256>,
#[serde(rename="parentHash")] #[serde(rename="parentHash")]
pub parent_hash: H256, pub parent_hash: H256,
#[serde(rename="sha3Uncles")] #[serde(rename="sha3Uncles")]
@ -51,7 +51,7 @@ pub struct Block {
pub transactions_root: H256, pub transactions_root: H256,
#[serde(rename="receiptsRoot")] #[serde(rename="receiptsRoot")]
pub receipts_root: H256, pub receipts_root: H256,
pub number: U256, pub number: OptionalValue<U256>,
#[serde(rename="gasUsed")] #[serde(rename="gasUsed")]
pub gas_used: U256, pub gas_used: U256,
#[serde(rename="gasLimit")] #[serde(rename="gasLimit")]
@ -73,14 +73,14 @@ mod tests {
use serde_json; use serde_json;
use util::hash::*; use util::hash::*;
use util::uint::*; use util::uint::*;
use v1::types::{Transaction, Bytes}; use v1::types::{Transaction, Bytes, OptionalValue};
use super::*; use super::*;
#[test] #[test]
fn test_serialize_block_transactions() { fn test_serialize_block_transactions() {
let t = BlockTransactions::Full(vec![Transaction::default()]); let t = BlockTransactions::Full(vec![Transaction::default()]);
let serialized = serde_json::to_string(&t).unwrap(); let serialized = serde_json::to_string(&t).unwrap();
assert_eq!(serialized, r#"[{"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x00","blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","blockNumber":"0x00","transactionIndex":"0x00","from":"0x0000000000000000000000000000000000000000","to":"0x0000000000000000000000000000000000000000","value":"0x00","gasPrice":"0x00","gas":"0x00","input":"0x00"}]"#); assert_eq!(serialized, r#"[{"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x00","blockHash":null,"blockNumber":null,"transactionIndex":"0x00","from":"0x0000000000000000000000000000000000000000","to":null,"value":"0x00","gasPrice":"0x00","gas":"0x00","input":"0x00"}]"#);
let t = BlockTransactions::Hashes(vec![H256::default()]); let t = BlockTransactions::Hashes(vec![H256::default()]);
let serialized = serde_json::to_string(&t).unwrap(); let serialized = serde_json::to_string(&t).unwrap();
@ -90,7 +90,7 @@ mod tests {
#[test] #[test]
fn test_serialize_block() { fn test_serialize_block() {
let block = Block { let block = Block {
hash: H256::default(), hash: OptionalValue::Value(H256::default()),
parent_hash: H256::default(), parent_hash: H256::default(),
uncles_hash: H256::default(), uncles_hash: H256::default(),
author: Address::default(), author: Address::default(),
@ -98,7 +98,7 @@ mod tests {
state_root: H256::default(), state_root: H256::default(),
transactions_root: H256::default(), transactions_root: H256::default(),
receipts_root: H256::default(), receipts_root: H256::default(),
number: U256::default(), number: OptionalValue::Value(U256::default()),
gas_used: U256::default(), gas_used: U256::default(),
gas_limit: U256::default(), gas_limit: U256::default(),
extra_data: Bytes::default(), extra_data: Bytes::default(),

View File

@ -17,11 +17,13 @@
mod block; mod block;
mod block_number; mod block_number;
mod bytes; mod bytes;
mod optionals;
mod sync; mod sync;
mod transaction; mod transaction;
pub use self::block::{Block, BlockTransactions}; pub use self::block::{Block, BlockTransactions};
pub use self::block_number::BlockNumber; pub use self::block_number::BlockNumber;
pub use self::bytes::Bytes; pub use self::bytes::Bytes;
pub use self::optionals::OptionalValue;
pub use self::sync::SyncStatus; pub use self::sync::SyncStatus;
pub use self::transaction::Transaction; pub use self::transaction::Transaction;

View File

@ -0,0 +1,58 @@
// 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::{Serialize, Serializer};
use serde_json::Value;
#[derive(Debug)]
pub enum OptionalValue<T> where T: Serialize {
Value(T),
Null
}
impl<T> Default for OptionalValue<T> where T: Serialize {
fn default() -> Self {
OptionalValue::Null
}
}
impl<T> Serialize for OptionalValue<T> where T: Serialize {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer {
match *self {
OptionalValue::Value(ref value) => value.serialize(serializer),
OptionalValue::Null => Value::Null.serialize(serializer)
}
}
}
#[cfg(test)]
mod tests {
use serde_json;
use util::hash::*;
use super::*;
#[test]
fn test_serialize_optional_value() {
let v: OptionalValue<H256> = OptionalValue::Null;
let serialized = serde_json::to_string(&v).unwrap();
assert_eq!(serialized, r#"null"#);
let v = OptionalValue::Value(H256::default());
let serialized = serde_json::to_string(&v).unwrap();
assert_eq!(serialized, r#""0x0000000000000000000000000000000000000000000000000000000000000000""#);
}
}

View File

@ -16,25 +16,25 @@
use util::hash::*; use util::hash::*;
use util::uint::*; use util::uint::*;
use v1::types::Bytes; use v1::types::{Bytes, OptionalValue};
#[derive(Debug, Default, Serialize)] #[derive(Debug, Default, Serialize)]
pub struct Transaction { pub struct Transaction {
hash: H256, pub hash: H256,
nonce: U256, pub nonce: U256,
#[serde(rename="blockHash")] #[serde(rename="blockHash")]
block_hash: H256, pub block_hash: OptionalValue<H256>,
#[serde(rename="blockNumber")] #[serde(rename="blockNumber")]
block_number: U256, pub block_number: OptionalValue<U256>,
#[serde(rename="transactionIndex")] #[serde(rename="transactionIndex")]
transaction_index: U256, pub transaction_index: U256,
from: Address, pub from: Address,
to: Address, pub to: OptionalValue<Address>,
value: U256, pub value: U256,
#[serde(rename="gasPrice")] #[serde(rename="gasPrice")]
gas_price: U256, pub gas_price: U256,
gas: U256, pub gas: U256,
input: Bytes pub input: Bytes
} }
#[cfg(test)] #[cfg(test)]
@ -46,7 +46,7 @@ mod tests {
fn test_transaction_serialize() { fn test_transaction_serialize() {
let t = Transaction::default(); let t = Transaction::default();
let serialized = serde_json::to_string(&t).unwrap(); let serialized = serde_json::to_string(&t).unwrap();
assert_eq!(serialized, r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x00","blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000","blockNumber":"0x00","transactionIndex":"0x00","from":"0x0000000000000000000000000000000000000000","to":"0x0000000000000000000000000000000000000000","value":"0x00","gasPrice":"0x00","gas":"0x00","input":"0x00"}"#); assert_eq!(serialized, r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x00","blockHash":null,"blockNumber":null,"transactionIndex":"0x00","from":"0x0000000000000000000000000000000000000000","to":null,"value":"0x00","gasPrice":"0x00","gas":"0x00","input":"0x00"}"#);
} }
} }