fixed U256 serialization, tests for transaction serialization

This commit is contained in:
debris 2016-02-08 11:58:47 +01:00
parent 3adfebdc20
commit b2c083ce56
7 changed files with 52 additions and 2 deletions

View File

@ -118,7 +118,7 @@ impl Eth for EthClient {
// TODO: do not ignore block number param
fn code_at(&self, params: Params) -> Result<Value, Error> {
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)
}
}

View File

@ -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::*;

1
rpc/src/v1/tests/mod.rs Normal file
View File

@ -0,0 +1 @@
//TODO: load custom blockchain state and test

View File

@ -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<u8>);
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<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer {

View File

@ -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;

View File

@ -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"}"#);
}
}

View File

@ -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())
}
}