From b781fe67532629f21ae52168bad8a5e4675d337d Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 14 Jan 2016 23:13:28 +0100 Subject: [PATCH 1/2] from_existing works for an empty DB if passed empty RLP SHA3. --- src/trie/triedbmut.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/trie/triedbmut.rs b/src/trie/triedbmut.rs index 03d2b59de..d205dc8e0 100644 --- a/src/trie/triedbmut.rs +++ b/src/trie/triedbmut.rs @@ -71,6 +71,9 @@ impl<'db> TrieDBMut<'db> { /// Create a new trie with the backing database `db` and `root` /// Panics, if `root` does not exist pub fn from_existing(db: &'db mut HashDB, root: &'db mut H256) -> Self { + if !db.exists(root) && root == &SHA3_NULL_RLP { + *root = db.insert(&NULL_RLP); + } assert!(db.exists(root)); TrieDBMut { db: db, From 6a7c823862d557f8994ad62a1d5cacbd42b66d0a Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 14 Jan 2016 23:39:59 +0100 Subject: [PATCH 2/2] Option types in JSON, more tests. --- src/hash.rs | 1 + src/json_aid.rs | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/hash.rs b/src/hash.rs index 8c1772b2c..c17b706db 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -195,6 +195,7 @@ macro_rules! impl_hash { fn from_json(json: &Json) -> Self { match json { &Json::String(ref s) => { + println!("s: {}", s); match s.len() % 2 { 0 => FromStr::from_str(clean_0x(s)).unwrap(), _ => FromStr::from_str(&("0".to_string() + &(clean_0x(s).to_string()))[..]).unwrap() diff --git a/src/json_aid.rs b/src/json_aid.rs index e25e9406d..79a71cac6 100644 --- a/src/json_aid.rs +++ b/src/json_aid.rs @@ -46,6 +46,16 @@ impl FromJson for Vec where T: FromJson { } } +impl FromJson for Option where T: FromJson { + fn from_json(json: &Json) -> Self { + match json { + &Json::String(ref o) if o.is_empty() => None, + &Json::Null => None, + _ => Some(FromJson::from_json(json)), + } + } +} + impl FromJson for u64 { fn from_json(json: &Json) -> Self { U256::from_json(json).low_u64() @@ -77,7 +87,7 @@ fn u256_from_json() { } #[test] -fn h256_from_json_() { +fn h256_from_json() { let j = Json::from_str("{ \"with\": \"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\", \"without\": \"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\" }").unwrap(); let v: H256 = xjson!(&j["with"]); @@ -95,9 +105,33 @@ fn vec_u256_from_json() { } #[test] -fn vec_h256_from_json_() { +fn vec_h256_from_json() { let j = Json::from_str("{ \"array\": [ \"1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\", \"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef\"] }").unwrap(); let v: Vec = xjson!(&j["array"]); assert_eq!(vec![H256::from_str("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef").unwrap(); 2], v); } + +#[test] +fn simple_types() { + let j = Json::from_str("{ \"null\": null, \"empty\": \"\", \"int\": 42, \"dec\": \"42\", \"hex\": \"0x2a\" }").unwrap(); + let v: u16 = xjson!(&j["int"]); + assert_eq!(42u16, v); + let v: u32 = xjson!(&j["dec"]); + assert_eq!(42u32, v); + let v: u64 = xjson!(&j["hex"]); + assert_eq!(42u64, v); +} + +#[test] +fn option_types() { + let j = Json::from_str("{ \"null\": null, \"empty\": \"\", \"int\": 42, \"dec\": \"42\", \"hex\": \"0x2a\" }").unwrap(); + let v: Option = xjson!(&j["int"]); + assert_eq!(Some(42u16), v); + let v: Option = xjson!(&j["dec"]); + assert_eq!(Some(42u16), v); + let v: Option = xjson!(&j["null"]); + assert_eq!(None, v); + let v: Option = xjson!(&j["empty"]); + assert_eq!(None, v); +} \ No newline at end of file