diff --git a/rpc/src/v1/impls/eth.rs b/rpc/src/v1/impls/eth.rs index d8bcf9540..f90995ffe 100644 --- a/rpc/src/v1/impls/eth.rs +++ b/rpc/src/v1/impls/eth.rs @@ -118,7 +118,7 @@ impl Eth for EthClient { // TODO: do not ignore block number param fn code_at(&self, params: Params) -> Result { match from_params::<(Address, BlockNumber)>(params) { - Ok((address, _block_number)) => to_value(&Bytes::new(self.client.code(&address).unwrap_or_else(|| vec![]))), + Ok((address, _block_number)) => to_value(&self.client.code(&address).map_or_else(Bytes::default, Bytes::new)), Err(err) => Err(err) } } diff --git a/rpc/src/v1/mod.rs b/rpc/src/v1/mod.rs index 11a5291b5..01635e872 100644 --- a/rpc/src/v1/mod.rs +++ b/rpc/src/v1/mod.rs @@ -21,6 +21,8 @@ pub mod traits; mod impls; mod types; +#[cfg(test)] +mod tests; pub use self::traits::{Web3, Eth, EthFilter, Net}; pub use self::impls::*; diff --git a/rpc/src/v1/tests/mod.rs b/rpc/src/v1/tests/mod.rs new file mode 100644 index 000000000..bdf4567b6 --- /dev/null +++ b/rpc/src/v1/tests/mod.rs @@ -0,0 +1 @@ +//TODO: load custom blockchain state and test diff --git a/rpc/src/v1/types/bytes.rs b/rpc/src/v1/types/bytes.rs index 51cb7f333..62aca8464 100644 --- a/rpc/src/v1/types/bytes.rs +++ b/rpc/src/v1/types/bytes.rs @@ -2,6 +2,7 @@ use rustc_serialize::hex::ToHex; use serde::{Serialize, Serializer}; /// Wrapper structure around vector of bytes. +#[derive(Debug)] pub struct Bytes(Vec); impl Bytes { @@ -11,6 +12,13 @@ impl Bytes { } } +impl Default for Bytes { + fn default() -> Self { + // default serialized value is 0x00 + Bytes(vec![0]) + } +} + impl Serialize for Bytes { fn serialize(&self, serializer: &mut S) -> Result<(), S::Error> where S: Serializer { diff --git a/rpc/src/v1/types/mod.rs b/rpc/src/v1/types/mod.rs index 0b8582910..226e7e9a6 100644 --- a/rpc/src/v1/types/mod.rs +++ b/rpc/src/v1/types/mod.rs @@ -18,8 +18,10 @@ mod block; mod block_number; mod bytes; mod sync; +mod transaction; pub use self::block::Block; pub use self::block_number::BlockNumber; pub use self::bytes::Bytes; pub use self::sync::SyncStatus; +pub use self::transaction::Transaction; diff --git a/rpc/src/v1/types/transaction.rs b/rpc/src/v1/types/transaction.rs new file mode 100644 index 000000000..de4490cbb --- /dev/null +++ b/rpc/src/v1/types/transaction.rs @@ -0,0 +1,36 @@ +use util::hash::*; +use util::uint::*; +use v1::types::Bytes; + +#[derive(Debug, Default, Serialize)] +pub struct Transaction { + hash: H256, + nonce: U256, + #[serde(rename="blockHash")] + block_hash: H256, + #[serde(rename="blockNumber")] + block_number: U256, + #[serde(rename="transactionIndex")] + transaction_index: U256, + from: Address, + to: Address, + value: U256, + #[serde(rename="gasPrice")] + gas_price: U256, + gas: U256, + input: Bytes +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json; + + #[test] + fn test_transaction_serialize() { + let t = Transaction::default(); + 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"}"#); + } +} + diff --git a/util/src/uint.rs b/util/src/uint.rs index 4c0b533ef..b3427f6bc 100644 --- a/util/src/uint.rs +++ b/util/src/uint.rs @@ -458,7 +458,8 @@ macro_rules! construct_uint { let mut hex = "0x".to_owned(); let mut bytes = [0u8; 8 * $n_words]; self.to_bytes(&mut bytes); - hex.push_str(bytes.to_hex().as_ref()); + let len = cmp::max((self.bits() + 7) / 8, 1); + hex.push_str(bytes[bytes.len() - len..].to_hex().as_ref()); serializer.visit_str(hex.as_ref()) } }