Fixing output of eth_call and Bytes deserialization (#2230)

* Fixing eth_call to builtins

* Fixing bytes deserialization

* Removing comment
This commit is contained in:
Tomasz Drwięga
2016-09-22 14:50:00 +02:00
committed by Gav Wood
parent 07b5e9a5c7
commit 15488b3e40
5 changed files with 147 additions and 68 deletions

View File

@@ -70,7 +70,7 @@ impl Visitor for BytesVisitor {
type Value = Bytes;
fn visit_str<E>(&mut self, value: &str) -> Result<Self::Value, E> where E: Error {
if value.len() >= 2 && &value[0..2] == "0x" {
if value.len() >= 2 && &value[0..2] == "0x" && value.len() & 1 == 0 {
Ok(Bytes::new(FromHex::from_hex(&value[2..]).unwrap_or_else(|_| vec![])))
} else {
Err(Error::custom("invalid hex"))
@@ -95,5 +95,21 @@ mod tests {
let serialized = serde_json::to_string(&bytes).unwrap();
assert_eq!(serialized, r#""0x0123456789abcdef""#);
}
#[test]
fn test_bytes_deserialize() {
let bytes1: Result<Bytes, serde_json::Error> = serde_json::from_str(r#""""#);
let bytes2: Result<Bytes, serde_json::Error> = serde_json::from_str(r#""0x123""#);
let bytes3: Bytes = serde_json::from_str(r#""0x""#).unwrap();
let bytes4: Bytes = serde_json::from_str(r#""0x12""#).unwrap();
let bytes5: Bytes = serde_json::from_str(r#""0x0123""#).unwrap();
assert!(bytes1.is_err());
assert!(bytes2.is_err());
assert_eq!(bytes3, Bytes(vec![]));
assert_eq!(bytes4, Bytes(vec![0x12]));
assert_eq!(bytes5, Bytes(vec![0x1, 0x23]));
}
}