Skip to content
This repository was archived by the owner on Sep 19, 2019. It is now read-only.

Commit cb6e423

Browse files
authored
Merge pull request #47 from paritytech/chore/remove-fixed-hash
Move fixed-hash and uint to parity-common; remove tests crate
2 parents 020b5d8 + c623a42 commit cb6e423

File tree

20 files changed

+231
-3674
lines changed

20 files changed

+231
-3674
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ matrix:
88
- rust: nightly
99
script:
1010
- cargo test -p ethereum-types --no-default-features --release
11-
- cargo test -p tests --release
1211
env:
1312
- RUST_LOG=quickcheck

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[workspace]
2-
members = ["uint", "fixed-hash", "ethereum-types", "tests", "ethbloom", "no-std-tests"]
2+
members = ["ethereum-types", "ethbloom"]

ethbloom/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ repository = "https://github.com/paritytech/primitives"
1111
[dependencies]
1212
tiny-keccak = "1.4"
1313
crunchy = { version = "0.1.6", features = ["limit_256"] }
14-
fixed-hash = { version = "0.2.1", path = "../fixed-hash", default-features = false }
14+
# TODO: remove `branch` when https://github.com/paritytech/parity-common/pull/12 lands
15+
fixed-hash = { git = "https://github.com/paritytech/parity-common", branch = "add-fixed-hash", default_features = false }
1516
ethereum-types-serialize = { version = "0.2.1", path = "../serialize", optional = true }
1617
serde = { version = "1.0", optional = true }
1718

ethereum-types/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ rustc_version = "0.2"
1414
crunchy = "0.1.5"
1515
ethbloom = { path = "../ethbloom", version = "0.5.0", default-features = false }
1616
ethereum-types-serialize = { version = "0.2.1", path = "../serialize", optional = true }
17-
fixed-hash = { path = "../fixed-hash", version = "0.2" }
17+
fixed-hash = { git = "https://github.com/paritytech/parity-common", default_features = false }
1818
serde = { version = "1.0", optional = true }
19-
uint = { path = "../uint", version = "0.2.1", default-features = false }
19+
uint = { git = "https://github.com/paritytech/parity-common", default_features = false }
20+
21+
[dev-dependencies]
22+
serde_json = "1.0"
2023

2124
[features]
2225
default = ["std", "heapsizeof", "serialize"]

ethereum-types/src/hash.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,55 @@ impl<'a> From<&'a H160> for H256 {
118118
ret
119119
}
120120
}
121+
122+
#[cfg(test)]
123+
mod tests {
124+
use super::{H160, H256};
125+
use serde_json as ser;
126+
127+
#[test]
128+
fn test_serialize_h160() {
129+
let tests = vec![
130+
(H160::from(0), "0x0000000000000000000000000000000000000000"),
131+
(H160::from(2), "0x0000000000000000000000000000000000000002"),
132+
(H160::from(15), "0x000000000000000000000000000000000000000f"),
133+
(H160::from(16), "0x0000000000000000000000000000000000000010"),
134+
(H160::from(1_000), "0x00000000000000000000000000000000000003e8"),
135+
(H160::from(100_000), "0x00000000000000000000000000000000000186a0"),
136+
(H160::from(u64::max_value()), "0x000000000000000000000000ffffffffffffffff"),
137+
];
138+
139+
for (number, expected) in tests {
140+
assert_eq!(format!("{:?}", expected), ser::to_string_pretty(&number).unwrap());
141+
assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap());
142+
}
143+
}
144+
145+
#[test]
146+
fn test_serialize_h256() {
147+
let tests = vec![
148+
(H256::from(0), "0x0000000000000000000000000000000000000000000000000000000000000000"),
149+
(H256::from(2), "0x0000000000000000000000000000000000000000000000000000000000000002"),
150+
(H256::from(15), "0x000000000000000000000000000000000000000000000000000000000000000f"),
151+
(H256::from(16), "0x0000000000000000000000000000000000000000000000000000000000000010"),
152+
(H256::from(1_000), "0x00000000000000000000000000000000000000000000000000000000000003e8"),
153+
(H256::from(100_000), "0x00000000000000000000000000000000000000000000000000000000000186a0"),
154+
(H256::from(u64::max_value()), "0x000000000000000000000000000000000000000000000000ffffffffffffffff"),
155+
];
156+
157+
for (number, expected) in tests {
158+
assert_eq!(format!("{:?}", expected), ser::to_string_pretty(&number).unwrap());
159+
assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap());
160+
}
161+
}
162+
163+
#[test]
164+
fn test_serialize_invalid() {
165+
assert!(ser::from_str::<H256>("\"0x000000000000000000000000000000000000000000000000000000000000000\"").unwrap_err().is_data());
166+
assert!(ser::from_str::<H256>("\"0x000000000000000000000000000000000000000000000000000000000000000g\"").unwrap_err().is_data());
167+
assert!(ser::from_str::<H256>("\"0x00000000000000000000000000000000000000000000000000000000000000000\"").unwrap_err().is_data());
168+
assert!(ser::from_str::<H256>("\"\"").unwrap_err().is_data());
169+
assert!(ser::from_str::<H256>("\"0\"").unwrap_err().is_data());
170+
assert!(ser::from_str::<H256>("\"10\"").unwrap_err().is_data());
171+
}
172+
}

ethereum-types/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ extern crate ethereum_types_serialize;
1717
#[cfg(feature="serialize")]
1818
extern crate serde;
1919

20+
#[cfg(test)]
21+
extern crate serde_json;
22+
2023
mod hash;
2124
mod uint;
2225

ethereum-types/src/uint.rs

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,171 @@ impl From<U512> for [u8; 64] {
359359
arr
360360
}
361361
}
362+
363+
#[cfg(test)]
364+
mod tests {
365+
use super::{U256, U512};
366+
use std::u64::MAX;
367+
use serde_json as ser;
368+
369+
macro_rules! test_serialize {
370+
($name: ident, $test_name: ident) => {
371+
#[test]
372+
fn $test_name() {
373+
let tests = vec![
374+
($name::from(0), "0x0"),
375+
($name::from(1), "0x1"),
376+
($name::from(2), "0x2"),
377+
($name::from(10), "0xa"),
378+
($name::from(15), "0xf"),
379+
($name::from(15), "0xf"),
380+
($name::from(16), "0x10"),
381+
($name::from(1_000), "0x3e8"),
382+
($name::from(100_000), "0x186a0"),
383+
($name::from(u64::max_value()), "0xffffffffffffffff"),
384+
($name::from(u64::max_value()) + 1, "0x10000000000000000"),
385+
];
386+
387+
for (number, expected) in tests {
388+
assert_eq!(format!("{:?}", expected), ser::to_string_pretty(&number).unwrap());
389+
assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap());
390+
}
391+
392+
// Invalid examples
393+
assert!(ser::from_str::<$name>("\"0x\"").unwrap_err().is_data());
394+
assert!(ser::from_str::<$name>("\"0xg\"").unwrap_err().is_data());
395+
assert!(ser::from_str::<$name>("\"\"").unwrap_err().is_data());
396+
assert!(ser::from_str::<$name>("\"10\"").unwrap_err().is_data());
397+
assert!(ser::from_str::<$name>("\"0\"").unwrap_err().is_data());
398+
}
399+
}
400+
}
401+
402+
test_serialize!(U256, test_u256);
403+
test_serialize!(U512, test_u512);
404+
405+
#[test]
406+
fn test_serialize_large_values() {
407+
assert_eq!(
408+
ser::to_string_pretty(&!U256::zero()).unwrap(),
409+
"\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\""
410+
);
411+
assert!(
412+
ser::from_str::<U256>("\"0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"").unwrap_err().is_data()
413+
);
414+
}
415+
416+
#[test]
417+
fn fixed_arrays_roundtrip() {
418+
let raw: U256 = "7094875209347850239487502394881".into();
419+
let array: [u8; 32] = raw.into();
420+
let new_raw = array.into();
421+
422+
assert_eq!(raw, new_raw);
423+
}
424+
425+
#[test]
426+
fn u256_multi_full_mul() {
427+
let result = U256([0, 0, 0, 0]).full_mul(U256([0, 0, 0, 0]));
428+
assert_eq!(U512([0, 0, 0, 0, 0, 0, 0, 0]), result);
429+
430+
let result = U256([1, 0, 0, 0]).full_mul(U256([1, 0, 0, 0]));
431+
assert_eq!(U512([1, 0, 0, 0, 0, 0, 0, 0]), result);
432+
433+
let result = U256([5, 0, 0, 0]).full_mul(U256([5, 0, 0, 0]));
434+
assert_eq!(U512([25, 0, 0, 0, 0, 0, 0, 0]), result);
435+
436+
let result = U256([0, 5, 0, 0]).full_mul(U256([0, 5, 0, 0]));
437+
assert_eq!(U512([0, 0, 25, 0, 0, 0, 0, 0]), result);
438+
439+
let result = U256([0, 0, 0, 4]).full_mul(U256([4, 0, 0, 0]));
440+
assert_eq!(U512([0, 0, 0, 16, 0, 0, 0, 0]), result);
441+
442+
let result = U256([0, 0, 0, 5]).full_mul(U256([2, 0, 0, 0]));
443+
assert_eq!(U512([0, 0, 0, 10, 0, 0, 0, 0]), result);
444+
445+
let result = U256([0, 0, 2, 0]).full_mul(U256([0, 5, 0, 0]));
446+
assert_eq!(U512([0, 0, 0, 10, 0, 0, 0, 0]), result);
447+
448+
let result = U256([0, 3, 0, 0]).full_mul(U256([0, 0, 3, 0]));
449+
assert_eq!(U512([0, 0, 0, 9, 0, 0, 0, 0]), result);
450+
451+
let result = U256([0, 0, 8, 0]).full_mul(U256([0, 0, 6, 0]));
452+
assert_eq!(U512([0, 0, 0, 0, 48, 0, 0, 0]), result);
453+
454+
let result = U256([9, 0, 0, 0]).full_mul(U256([0, 3, 0, 0]));
455+
assert_eq!(U512([0, 27, 0, 0, 0, 0, 0, 0]), result);
456+
457+
let result = U256([MAX, 0, 0, 0]).full_mul(U256([MAX, 0, 0, 0]));
458+
assert_eq!(U512([1, MAX-1, 0, 0, 0, 0, 0, 0]), result);
459+
460+
let result = U256([0, MAX, 0, 0]).full_mul(U256([MAX, 0, 0, 0]));
461+
assert_eq!(U512([0, 1, MAX-1, 0, 0, 0, 0, 0]), result);
462+
463+
let result = U256([MAX, MAX, 0, 0]).full_mul(U256([MAX, 0, 0, 0]));
464+
assert_eq!(U512([1, MAX, MAX-1, 0, 0, 0, 0, 0]), result);
465+
466+
let result = U256([MAX, 0, 0, 0]).full_mul(U256([MAX, MAX, 0, 0]));
467+
assert_eq!(U512([1, MAX, MAX-1, 0, 0, 0, 0, 0]), result);
468+
469+
let result = U256([MAX, MAX, 0, 0]).full_mul(U256([MAX, MAX, 0, 0]));
470+
assert_eq!(U512([1, 0, MAX-1, MAX, 0, 0, 0, 0]), result);
471+
472+
let result = U256([MAX, 0, 0, 0]).full_mul(U256([MAX, MAX, MAX, 0]));
473+
assert_eq!(U512([1, MAX, MAX, MAX-1, 0, 0, 0, 0]), result);
474+
475+
let result = U256([MAX, MAX, MAX, 0]).full_mul(U256([MAX, 0, 0, 0]));
476+
assert_eq!(U512([1, MAX, MAX, MAX-1, 0, 0, 0, 0]), result);
477+
478+
let result = U256([MAX, 0, 0, 0]).full_mul(U256([MAX, MAX, MAX, MAX]));
479+
assert_eq!(U512([1, MAX, MAX, MAX, MAX-1, 0, 0, 0]), result);
480+
481+
let result = U256([MAX, MAX, MAX, MAX]).full_mul(U256([MAX, 0, 0, 0]));
482+
assert_eq!(U512([1, MAX, MAX, MAX, MAX-1, 0, 0, 0]), result);
483+
484+
let result = U256([MAX, MAX, MAX, 0]).full_mul(U256([MAX, MAX, 0, 0]));
485+
assert_eq!(U512([1, 0, MAX, MAX-1, MAX, 0, 0, 0]), result);
486+
487+
let result = U256([MAX, MAX, 0, 0]).full_mul(U256([MAX, MAX, MAX, 0]));
488+
assert_eq!(U512([1, 0, MAX, MAX-1, MAX, 0, 0, 0]), result);
489+
490+
let result = U256([MAX, MAX, MAX, MAX]).full_mul(U256([MAX, MAX, 0, 0]));
491+
assert_eq!(U512([1, 0, MAX, MAX, MAX-1, MAX, 0, 0]), result);
492+
493+
let result = U256([MAX, MAX, 0, 0]).full_mul(U256([MAX, MAX, MAX, MAX]));
494+
assert_eq!(U512([1, 0, MAX, MAX, MAX-1, MAX, 0, 0]), result);
495+
496+
let result = U256([MAX, MAX, MAX, 0]).full_mul(U256([MAX, MAX, MAX, 0]));
497+
assert_eq!(U512([1, 0, 0, MAX-1, MAX, MAX, 0, 0]), result);
498+
499+
let result = U256([MAX, MAX, MAX, 0]).full_mul(U256([MAX, MAX, MAX, MAX]));
500+
assert_eq!(U512([1, 0, 0, MAX, MAX-1, MAX, MAX, 0]), result);
501+
502+
let result = U256([MAX, MAX, MAX, MAX]).full_mul(U256([MAX, MAX, MAX, 0]));
503+
assert_eq!(U512([1, 0, 0, MAX, MAX-1, MAX, MAX, 0]), result);
504+
505+
let result = U256([MAX, MAX, MAX, MAX]).full_mul(U256([MAX, MAX, MAX, MAX]));
506+
assert_eq!(U512([1, 0, 0, 0, MAX-1, MAX, MAX, MAX]), result);
507+
508+
let result = U256([0, 0, 0, MAX]).full_mul(U256([0, 0, 0, MAX]));
509+
assert_eq!(U512([0, 0, 0, 0, 0, 0, 1, MAX-1]), result);
510+
511+
let result = U256([1, 0, 0, 0]).full_mul(U256([0, 0, 0, MAX]));
512+
assert_eq!(U512([0, 0, 0, MAX, 0, 0, 0, 0]), result);
513+
514+
let result = U256([1, 2, 3, 4]).full_mul(U256([5, 0, 0, 0]));
515+
assert_eq!(U512([5, 10, 15, 20, 0, 0, 0, 0]), result);
516+
517+
let result = U256([1, 2, 3, 4]).full_mul(U256([0, 6, 0, 0]));
518+
assert_eq!(U512([0, 6, 12, 18, 24, 0, 0, 0]), result);
519+
520+
let result = U256([1, 2, 3, 4]).full_mul(U256([0, 0, 7, 0]));
521+
assert_eq!(U512([0, 0, 7, 14, 21, 28, 0, 0]), result);
522+
523+
let result = U256([1, 2, 3, 4]).full_mul(U256([0, 0, 0, 8]));
524+
assert_eq!(U512([0, 0, 0, 8, 16, 24, 32, 0]), result);
525+
526+
let result = U256([1, 2, 3, 4]).full_mul(U256([5, 6, 7, 8]));
527+
assert_eq!(U512([5, 16, 34, 60, 61, 52, 32, 0]), result);
528+
}
529+
}

fixed-hash/Cargo.toml

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)