1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
extern crate ethereum_types;
mod hash;
pub use crate::hash::{H128, H160, H256, H264, H512, H520};
pub use ethereum_types::U256;
pub fn h128_from_u128(u: u128) -> H128 {
H128(u.to_be_bytes())
}
pub fn u256_from_u128(u: u128) -> U256 {
let mut arr: [u64; 4] = [0, 0, 0, 0];
arr[0] = (u & u128::from(std::u64::MAX)) as u64;
arr[1] = (u >> 64) as u64;
U256(arr)
}
pub fn remove_0x_prefix(s: &str) -> &str {
if s.starts_with("0x") {
&s[2..]
} else {
s
}
}
pub type Bytes = Vec<u8>;
#[cfg(test)]
mod tests_h128_from_u128 {
use super::*;
use ethereum_types::U128;
#[test]
fn zero() {
assert_eq!(H128::zero(), h128_from_u128(0));
}
#[test]
fn one() {
assert_eq!(H128::from(1), h128_from_u128(1));
}
#[test]
fn u64_max_plus_1() {
assert_eq!(H128::from(U128::from(std::u64::MAX) + 1), h128_from_u128(u128::from(std::u64::MAX) + 1));
}
#[test]
fn max_minus_1() {
assert_eq!(H128::from(U128::max_value() - 1), h128_from_u128(std::u128::MAX - 1));
}
#[test]
fn u128_max() {
assert_eq!(H128::from(U128::max_value()), h128_from_u128(std::u128::MAX));
}
}
#[cfg(test)]
mod tests {
use ethereum_types::U128;
use super::*;
#[test]
fn u128_zero() {
assert_eq!(u256_from_u128(0), U128::zero().into());
}
#[test]
fn u128_one() {
assert_eq!(u256_from_u128(1), U128::one().into());
}
#[test]
fn u64_max_plus_1() {
let u128: U128 = U128::from(std::u64::MAX) + 1;
assert_eq!(u256_from_u128(u128::from(std::u64::MAX) + 1), u128.into());
}
#[test]
fn u128_max_minus_1() {
let u128: U128 = U128::max_value() - 1;
assert_eq!(u256_from_u128(std::u128::MAX - 1), u128.into());
}
#[test]
fn u128_max() {
assert_eq!(u256_from_u128(std::u128::MAX), U128::max_value().into());
}
#[test]
fn remove_0x_prefix_returns_the_same_if_it_does_not_start_with_0x() {
const S: &str = "abcdef";
assert_eq!("abcdef", remove_0x_prefix(S));
}
#[test]
fn remove_0x_prefix_returns_the_same_if_it_starts_with_0() {
const S: &str = "0abcdef";
assert_eq!("0abcdef", remove_0x_prefix(S));
}
#[test]
fn remove_0x_prefix_works() {
const S: &str = "0xabcdef";
assert_eq!("abcdef", remove_0x_prefix(S));
}
#[test]
fn remove_0x_prefix_returns_empty_string_if_input_is_0x() {
const S: &str = "0x";
assert_eq!("", remove_0x_prefix(S));
}
}