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

Move fixed-hash and uint to parity-common; remove tests crate #47

Merged
merged 11 commits into from
Aug 6, 2018
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ matrix:
- rust: nightly
script:
- cargo test -p ethereum-types --no-default-features --release
- cargo test -p tests --release
env:
- RUST_LOG=quickcheck
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[workspace]
members = ["uint", "fixed-hash", "ethereum-types", "tests", "ethbloom", "no-std-tests"]
members = ["ethereum-types", "ethbloom"]
3 changes: 2 additions & 1 deletion ethbloom/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ repository = "https://github.com/paritytech/primitives"
[dependencies]
tiny-keccak = "1.4"
crunchy = { version = "0.1.6", features = ["limit_256"] }
fixed-hash = { version = "0.2.1", path = "../fixed-hash", default-features = false }
# TODO: remove `branch` when https://github.com/paritytech/parity-common/pull/12 lands
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems it landed already

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think it would be best to actually use crates.io version instead of git repo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems it landed already

Good catch, will fix.

I also think it would be best to actually use crates.io version instead of git repo.

I agree with this (strongly) but I wanted to wait until this stuff lands in polkadot (this poorly named PR) and the dust settles a little bit.

fixed-hash = { git = "https://github.com/paritytech/parity-common", branch = "add-fixed-hash", default_features = false }
ethereum-types-serialize = { version = "0.2.1", path = "../serialize", optional = true }
serde = { version = "1.0", optional = true }

Expand Down
7 changes: 5 additions & 2 deletions ethereum-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ rustc_version = "0.2"
crunchy = "0.1.5"
ethbloom = { path = "../ethbloom", version = "0.5.0", default-features = false }
ethereum-types-serialize = { version = "0.2.1", path = "../serialize", optional = true }
fixed-hash = { path = "../fixed-hash", version = "0.2" }
fixed-hash = { git = "https://github.com/paritytech/parity-common", default_features = false }
serde = { version = "1.0", optional = true }
uint = { path = "../uint", version = "0.2.1", default-features = false }
uint = { git = "https://github.com/paritytech/parity-common", default_features = false }

[dev-dependencies]
serde_json = "1.0"

[features]
default = ["std", "heapsizeof", "serialize"]
Expand Down
52 changes: 52 additions & 0 deletions ethereum-types/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,55 @@ impl<'a> From<&'a H160> for H256 {
ret
}
}

#[cfg(test)]
mod tests {
use super::{H160, H256};
use serde_json as ser;

#[test]
fn test_serialize_h160() {
let tests = vec![
(H160::from(0), "0x0000000000000000000000000000000000000000"),
(H160::from(2), "0x0000000000000000000000000000000000000002"),
(H160::from(15), "0x000000000000000000000000000000000000000f"),
(H160::from(16), "0x0000000000000000000000000000000000000010"),
(H160::from(1_000), "0x00000000000000000000000000000000000003e8"),
(H160::from(100_000), "0x00000000000000000000000000000000000186a0"),
(H160::from(u64::max_value()), "0x000000000000000000000000ffffffffffffffff"),
];

for (number, expected) in tests {
assert_eq!(format!("{:?}", expected), ser::to_string_pretty(&number).unwrap());
assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap());
}
}

#[test]
fn test_serialize_h256() {
let tests = vec![
(H256::from(0), "0x0000000000000000000000000000000000000000000000000000000000000000"),
(H256::from(2), "0x0000000000000000000000000000000000000000000000000000000000000002"),
(H256::from(15), "0x000000000000000000000000000000000000000000000000000000000000000f"),
(H256::from(16), "0x0000000000000000000000000000000000000000000000000000000000000010"),
(H256::from(1_000), "0x00000000000000000000000000000000000000000000000000000000000003e8"),
(H256::from(100_000), "0x00000000000000000000000000000000000000000000000000000000000186a0"),
(H256::from(u64::max_value()), "0x000000000000000000000000000000000000000000000000ffffffffffffffff"),
];

for (number, expected) in tests {
assert_eq!(format!("{:?}", expected), ser::to_string_pretty(&number).unwrap());
assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap());
}
}

#[test]
fn test_serialize_invalid() {
assert!(ser::from_str::<H256>("\"0x000000000000000000000000000000000000000000000000000000000000000\"").unwrap_err().is_data());
assert!(ser::from_str::<H256>("\"0x000000000000000000000000000000000000000000000000000000000000000g\"").unwrap_err().is_data());
assert!(ser::from_str::<H256>("\"0x00000000000000000000000000000000000000000000000000000000000000000\"").unwrap_err().is_data());
assert!(ser::from_str::<H256>("\"\"").unwrap_err().is_data());
assert!(ser::from_str::<H256>("\"0\"").unwrap_err().is_data());
assert!(ser::from_str::<H256>("\"10\"").unwrap_err().is_data());
}
}
3 changes: 3 additions & 0 deletions ethereum-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ extern crate ethereum_types_serialize;
#[cfg(feature="serialize")]
extern crate serde;

#[cfg(test)]
extern crate serde_json;

mod hash;
mod uint;

Expand Down
168 changes: 168 additions & 0 deletions ethereum-types/src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,171 @@ impl From<U512> for [u8; 64] {
arr
}
}

#[cfg(test)]
mod tests {
use super::{U256, U512};
use std::u64::MAX;
use serde_json as ser;

macro_rules! test_serialize {
($name: ident, $test_name: ident) => {
#[test]
fn $test_name() {
let tests = vec![
($name::from(0), "0x0"),
($name::from(1), "0x1"),
($name::from(2), "0x2"),
($name::from(10), "0xa"),
($name::from(15), "0xf"),
($name::from(15), "0xf"),
($name::from(16), "0x10"),
($name::from(1_000), "0x3e8"),
($name::from(100_000), "0x186a0"),
($name::from(u64::max_value()), "0xffffffffffffffff"),
($name::from(u64::max_value()) + 1, "0x10000000000000000"),
];

for (number, expected) in tests {
assert_eq!(format!("{:?}", expected), ser::to_string_pretty(&number).unwrap());
assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap());
}

// Invalid examples
assert!(ser::from_str::<$name>("\"0x\"").unwrap_err().is_data());
assert!(ser::from_str::<$name>("\"0xg\"").unwrap_err().is_data());
assert!(ser::from_str::<$name>("\"\"").unwrap_err().is_data());
assert!(ser::from_str::<$name>("\"10\"").unwrap_err().is_data());
assert!(ser::from_str::<$name>("\"0\"").unwrap_err().is_data());
}
}
}

test_serialize!(U256, test_u256);
test_serialize!(U512, test_u512);

#[test]
fn test_serialize_large_values() {
assert_eq!(
ser::to_string_pretty(&!U256::zero()).unwrap(),
"\"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\""
);
assert!(
ser::from_str::<U256>("\"0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\"").unwrap_err().is_data()
);
}

#[test]
fn fixed_arrays_roundtrip() {
let raw: U256 = "7094875209347850239487502394881".into();
let array: [u8; 32] = raw.into();
let new_raw = array.into();

assert_eq!(raw, new_raw);
}

#[test]
fn u256_multi_full_mul() {
let result = U256([0, 0, 0, 0]).full_mul(U256([0, 0, 0, 0]));
assert_eq!(U512([0, 0, 0, 0, 0, 0, 0, 0]), result);

let result = U256([1, 0, 0, 0]).full_mul(U256([1, 0, 0, 0]));
assert_eq!(U512([1, 0, 0, 0, 0, 0, 0, 0]), result);

let result = U256([5, 0, 0, 0]).full_mul(U256([5, 0, 0, 0]));
assert_eq!(U512([25, 0, 0, 0, 0, 0, 0, 0]), result);

let result = U256([0, 5, 0, 0]).full_mul(U256([0, 5, 0, 0]));
assert_eq!(U512([0, 0, 25, 0, 0, 0, 0, 0]), result);

let result = U256([0, 0, 0, 4]).full_mul(U256([4, 0, 0, 0]));
assert_eq!(U512([0, 0, 0, 16, 0, 0, 0, 0]), result);

let result = U256([0, 0, 0, 5]).full_mul(U256([2, 0, 0, 0]));
assert_eq!(U512([0, 0, 0, 10, 0, 0, 0, 0]), result);

let result = U256([0, 0, 2, 0]).full_mul(U256([0, 5, 0, 0]));
assert_eq!(U512([0, 0, 0, 10, 0, 0, 0, 0]), result);

let result = U256([0, 3, 0, 0]).full_mul(U256([0, 0, 3, 0]));
assert_eq!(U512([0, 0, 0, 9, 0, 0, 0, 0]), result);

let result = U256([0, 0, 8, 0]).full_mul(U256([0, 0, 6, 0]));
assert_eq!(U512([0, 0, 0, 0, 48, 0, 0, 0]), result);

let result = U256([9, 0, 0, 0]).full_mul(U256([0, 3, 0, 0]));
assert_eq!(U512([0, 27, 0, 0, 0, 0, 0, 0]), result);

let result = U256([MAX, 0, 0, 0]).full_mul(U256([MAX, 0, 0, 0]));
assert_eq!(U512([1, MAX-1, 0, 0, 0, 0, 0, 0]), result);

let result = U256([0, MAX, 0, 0]).full_mul(U256([MAX, 0, 0, 0]));
assert_eq!(U512([0, 1, MAX-1, 0, 0, 0, 0, 0]), result);

let result = U256([MAX, MAX, 0, 0]).full_mul(U256([MAX, 0, 0, 0]));
assert_eq!(U512([1, MAX, MAX-1, 0, 0, 0, 0, 0]), result);

let result = U256([MAX, 0, 0, 0]).full_mul(U256([MAX, MAX, 0, 0]));
assert_eq!(U512([1, MAX, MAX-1, 0, 0, 0, 0, 0]), result);

let result = U256([MAX, MAX, 0, 0]).full_mul(U256([MAX, MAX, 0, 0]));
assert_eq!(U512([1, 0, MAX-1, MAX, 0, 0, 0, 0]), result);

let result = U256([MAX, 0, 0, 0]).full_mul(U256([MAX, MAX, MAX, 0]));
assert_eq!(U512([1, MAX, MAX, MAX-1, 0, 0, 0, 0]), result);

let result = U256([MAX, MAX, MAX, 0]).full_mul(U256([MAX, 0, 0, 0]));
assert_eq!(U512([1, MAX, MAX, MAX-1, 0, 0, 0, 0]), result);

let result = U256([MAX, 0, 0, 0]).full_mul(U256([MAX, MAX, MAX, MAX]));
assert_eq!(U512([1, MAX, MAX, MAX, MAX-1, 0, 0, 0]), result);

let result = U256([MAX, MAX, MAX, MAX]).full_mul(U256([MAX, 0, 0, 0]));
assert_eq!(U512([1, MAX, MAX, MAX, MAX-1, 0, 0, 0]), result);

let result = U256([MAX, MAX, MAX, 0]).full_mul(U256([MAX, MAX, 0, 0]));
assert_eq!(U512([1, 0, MAX, MAX-1, MAX, 0, 0, 0]), result);

let result = U256([MAX, MAX, 0, 0]).full_mul(U256([MAX, MAX, MAX, 0]));
assert_eq!(U512([1, 0, MAX, MAX-1, MAX, 0, 0, 0]), result);

let result = U256([MAX, MAX, MAX, MAX]).full_mul(U256([MAX, MAX, 0, 0]));
assert_eq!(U512([1, 0, MAX, MAX, MAX-1, MAX, 0, 0]), result);

let result = U256([MAX, MAX, 0, 0]).full_mul(U256([MAX, MAX, MAX, MAX]));
assert_eq!(U512([1, 0, MAX, MAX, MAX-1, MAX, 0, 0]), result);

let result = U256([MAX, MAX, MAX, 0]).full_mul(U256([MAX, MAX, MAX, 0]));
assert_eq!(U512([1, 0, 0, MAX-1, MAX, MAX, 0, 0]), result);

let result = U256([MAX, MAX, MAX, 0]).full_mul(U256([MAX, MAX, MAX, MAX]));
assert_eq!(U512([1, 0, 0, MAX, MAX-1, MAX, MAX, 0]), result);

let result = U256([MAX, MAX, MAX, MAX]).full_mul(U256([MAX, MAX, MAX, 0]));
assert_eq!(U512([1, 0, 0, MAX, MAX-1, MAX, MAX, 0]), result);

let result = U256([MAX, MAX, MAX, MAX]).full_mul(U256([MAX, MAX, MAX, MAX]));
assert_eq!(U512([1, 0, 0, 0, MAX-1, MAX, MAX, MAX]), result);

let result = U256([0, 0, 0, MAX]).full_mul(U256([0, 0, 0, MAX]));
assert_eq!(U512([0, 0, 0, 0, 0, 0, 1, MAX-1]), result);

let result = U256([1, 0, 0, 0]).full_mul(U256([0, 0, 0, MAX]));
assert_eq!(U512([0, 0, 0, MAX, 0, 0, 0, 0]), result);

let result = U256([1, 2, 3, 4]).full_mul(U256([5, 0, 0, 0]));
assert_eq!(U512([5, 10, 15, 20, 0, 0, 0, 0]), result);

let result = U256([1, 2, 3, 4]).full_mul(U256([0, 6, 0, 0]));
assert_eq!(U512([0, 6, 12, 18, 24, 0, 0, 0]), result);

let result = U256([1, 2, 3, 4]).full_mul(U256([0, 0, 7, 0]));
assert_eq!(U512([0, 0, 7, 14, 21, 28, 0, 0]), result);

let result = U256([1, 2, 3, 4]).full_mul(U256([0, 0, 0, 8]));
assert_eq!(U512([0, 0, 0, 8, 16, 24, 32, 0]), result);

let result = U256([1, 2, 3, 4]).full_mul(U256([5, 6, 7, 8]));
assert_eq!(U512([5, 16, 34, 60, 61, 52, 32, 0]), result);
}
}
22 changes: 0 additions & 22 deletions fixed-hash/Cargo.toml

This file was deleted.

Loading