Skip to content

Commit 4176b54

Browse files
committed
Change Address type to a newtype for H160
1 parent fc8e630 commit 4176b54

File tree

11 files changed

+158
-22
lines changed

11 files changed

+158
-22
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

json/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.1.0"
44
authors = ["CodeChain Team <[email protected]>"]
55

66
[dependencies]
7+
codechain-key = { path = "../key", features = ["ecdsa"] }
78
primitives = { path = "../util/primitives" }
89
rustc-hex = "1.0"
910
serde = "1.0"

json/src/hash.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
use std::fmt;
2020
use std::str::FromStr;
2121

22-
use primitives::{H160 as Hash160, H256 as Hash256, H520 as Hash520, H64 as Hash64};
22+
use ckey::Address as CoreAddress;
23+
use primitives::{H256 as Hash256, H520 as Hash520, H64 as Hash64};
2324
use rustc_hex::ToHex;
2425
use serde::de::{Error, Visitor};
2526
use serde::{Deserialize, Deserializer, Serialize, Serializer};
@@ -94,7 +95,7 @@ macro_rules! impl_hash {
9495
}
9596

9697
impl_hash!(H64, Hash64);
97-
impl_hash!(Address, Hash160);
98+
impl_hash!(Address, CoreAddress);
9899
impl_hash!(H256, Hash256);
99100
impl_hash!(H520, Hash520);
100101

json/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

17+
extern crate codechain_key as ckey;
1718
extern crate primitives;
1819
extern crate rustc_hex;
1920
extern crate serde;

json/src/spec/genesis.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ pub struct Genesis {
4747
mod tests {
4848
use std::str::FromStr;
4949

50-
use primitives::{H160, H256 as Eth256, H520 as Eth520, U256};
50+
use ckey::Address as CoreAddress;
51+
use primitives::{H256 as Eth256, H520 as Eth520, U256};
5152
use serde_json;
5253

5354
use super::super::super::bytes::Bytes;
@@ -85,7 +86,7 @@ mod tests {
8586
]
8687
}),
8788
score: Uint(U256::from(0x400000000u64)),
88-
author: Some(Address(H160::from("0x1000000000000000000000000000000000000001"))),
89+
author: Some(Address(CoreAddress::from("0x1000000000000000000000000000000000000001"))),
8990
timestamp: Some(Uint(U256::from(0x07))),
9091
parent_hash: Some(H256(Eth256::from("0x9000000000000000000000000000000000000000000000000000000000000000"))),
9192
parcels_root: None,

json/src/spec/shard.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ pub struct Shard {
2727

2828
#[cfg(test)]
2929
mod tests {
30-
use primitives::{H160, U256};
30+
use ckey::Address as CoreAddress;
31+
use primitives::U256;
3132
use serde_json;
3233

3334
use super::*;
@@ -46,10 +47,10 @@ mod tests {
4647
assert_eq!(
4748
Shard {
4849
nonce: Some(Uint(U256::from(0))),
49-
owner: Address(H160::from("01234567890abcdef0123456789abcdef0123456")),
50+
owner: Address(CoreAddress::from("01234567890abcdef0123456789abcdef0123456")),
5051
worlds: Some(vec![World {
5152
nonce: Some(Uint(U256::from(3))),
52-
owners: Some(vec![Address(H160::from("01234567890abcdef0123456789abcdef0123457"))]),
53+
owners: Some(vec![Address(CoreAddress::from("01234567890abcdef0123456789abcdef0123457"))]),
5354
}]),
5455
},
5556
shard
@@ -66,7 +67,7 @@ mod tests {
6667
assert_eq!(
6768
Shard {
6869
nonce: Some(Uint(U256::from(100))),
69-
owner: Address(H160::from("01234567890abcdef0123456789abcdef0123456")),
70+
owner: Address(CoreAddress::from("01234567890abcdef0123456789abcdef0123456")),
7071
worlds: None,
7172
},
7273
shard
@@ -91,7 +92,7 @@ mod tests {
9192
assert_eq!(
9293
Shard {
9394
nonce: None,
94-
owner: Address(H160::from("01234567890abcdef0123456789abcdef0123456")),
95+
owner: Address(CoreAddress::from("01234567890abcdef0123456789abcdef0123456")),
9596
worlds: None,
9697
},
9798
shard

json/src/spec/solo_authority.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ pub struct SoloAuthority {
3535

3636
#[cfg(test)]
3737
mod tests {
38-
use primitives::{H160, U256};
38+
use ckey::Address as CoreAddress;
39+
use primitives::U256;
3940
use serde_json;
4041

4142
use super::super::super::hash::Address;
@@ -53,7 +54,7 @@ mod tests {
5354

5455
let deserialized: SoloAuthority = serde_json::from_str(s).unwrap();
5556

56-
let vs = vec![Address(H160::from("0xc6d9d2cd449a754c494264e1809c50e34d64562b"))];
57+
let vs = vec![Address(CoreAddress::from("0xc6d9d2cd449a754c494264e1809c50e34d64562b"))];
5758
assert_eq!(deserialized.params.validators, vs);
5859
assert_eq!(deserialized.params.block_reward, Some(Uint(U256::from(0x0d))));
5960
}

json/src/spec/tendermint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub struct Tendermint {
4343

4444
#[cfg(test)]
4545
mod tests {
46-
use primitives::H160;
46+
use ckey::Address as CoreAddress;
4747
use serde_json;
4848

4949
use super::super::super::hash::Address;
@@ -58,7 +58,7 @@ mod tests {
5858
}"#;
5959

6060
let deserialized: Tendermint = serde_json::from_str(s).unwrap();
61-
let vs = vec![Address(H160::from("0xc6d9d2cd449a754c494264e1809c50e34d64562b"))];
61+
let vs = vec![Address(CoreAddress::from("0xc6d9d2cd449a754c494264e1809c50e34d64562b"))];
6262
assert_eq!(deserialized.params.validators, vs);
6363
}
6464
}

json/src/spec/world.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct World {
2525

2626
#[cfg(test)]
2727
mod tests {
28-
use primitives::H160;
28+
use ckey::Address as CoreAddress;
2929
use serde_json;
3030

3131
use super::*;
@@ -40,7 +40,7 @@ mod tests {
4040
assert_eq!(
4141
World {
4242
nonce: Some(Uint(0.into())),
43-
owners: Some(vec![Address(H160::from("01234567890abcdef0123456789abcdef0123456"))]),
43+
owners: Some(vec![Address(CoreAddress::from("01234567890abcdef0123456789abcdef0123456"))]),
4444
},
4545
world
4646
);
@@ -56,7 +56,7 @@ mod tests {
5656
assert_eq!(
5757
World {
5858
nonce: Some(Uint(100.into())),
59-
owners: Some(vec![Address(H160::from("01234567890abcdef0123456789abcdef0123456"))]),
59+
owners: Some(vec![Address(CoreAddress::from("01234567890abcdef0123456789abcdef0123456"))]),
6060
},
6161
world
6262
);
@@ -86,7 +86,7 @@ mod tests {
8686
assert_eq!(
8787
World {
8888
nonce: None,
89-
owners: Some(vec![Address(H160::from("01234567890abcdef0123456789abcdef0123456"))]),
89+
owners: Some(vec![Address(CoreAddress::from("01234567890abcdef0123456789abcdef0123456"))]),
9090
},
9191
world
9292
);

key/src/address.rs

Lines changed: 132 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,144 @@
1616

1717
use std::cmp;
1818
use std::fmt;
19+
use std::hash::{Hash, Hasher};
20+
use std::ops::{Deref, DerefMut};
1921
use std::str::FromStr;
2022

2123
use bech32::Bech32;
24+
use heapsize::HeapSizeOf;
2225
use primitives::H160;
26+
use rlp::{Decodable, DecoderError, Encodable, RlpStream, UntrustedRlp};
27+
use rustc_hex::FromHexError;
2328
use serde::de::{Error as SerdeError, Visitor};
2429
use serde::{Deserialize, Deserializer, Serialize, Serializer};
2530

26-
use super::{Address, Error, Network};
31+
use super::{Error, Network};
32+
33+
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
34+
pub struct Address(H160);
35+
36+
impl Address {
37+
pub fn zero() -> Self {
38+
Address(H160::zero())
39+
}
40+
41+
pub fn new() -> Self {
42+
Address(H160::new())
43+
}
44+
45+
pub fn random() -> Self {
46+
Address(H160::random())
47+
}
48+
}
49+
50+
impl Deref for Address {
51+
type Target = H160;
52+
53+
fn deref(&self) -> &Self::Target {
54+
&self.0
55+
}
56+
}
57+
58+
impl DerefMut for Address {
59+
fn deref_mut(&mut self) -> &mut Self::Target {
60+
&mut self.0
61+
}
62+
}
63+
64+
impl fmt::Display for Address {
65+
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
66+
write!(f, "{}", self.0)
67+
}
68+
}
69+
70+
impl PartialOrd for Address {
71+
fn partial_cmp(&self, m: &Address) -> Option<cmp::Ordering> {
72+
self.0.partial_cmp(&m.0)
73+
}
74+
}
75+
76+
impl Ord for Address {
77+
fn cmp(&self, m: &Address) -> cmp::Ordering {
78+
self.0.cmp(&m.0)
79+
}
80+
}
81+
82+
impl Hash for Address {
83+
fn hash<H: Hasher>(&self, state: &mut H) {
84+
self.0.hash(state);
85+
}
86+
}
87+
88+
impl Default for Address {
89+
fn default() -> Self {
90+
Address(Default::default())
91+
}
92+
}
93+
94+
impl Encodable for Address {
95+
fn rlp_append(&self, s: &mut RlpStream) {
96+
let data: H160 = self.0.into();
97+
data.rlp_append(s);
98+
}
99+
}
100+
101+
impl Decodable for Address {
102+
fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
103+
let data = H160::decode(rlp)?;
104+
Ok(Address(data))
105+
}
106+
}
107+
108+
impl FromStr for Address {
109+
type Err = FromHexError;
110+
111+
fn from_str(s: &str) -> Result<Self, Self::Err> {
112+
Ok(Address(H160::from_str(s)?))
113+
}
114+
}
115+
116+
impl From<H160> for Address {
117+
fn from(s: H160) -> Self {
118+
Address(s)
119+
}
120+
}
121+
122+
impl From<u64> for Address {
123+
fn from(s: u64) -> Self {
124+
Address(H160::from(s))
125+
}
126+
}
127+
128+
impl From<[u8; 20]> for Address {
129+
fn from(s: [u8; 20]) -> Self {
130+
Address(H160::from(s))
131+
}
132+
}
133+
134+
impl From<&'static str> for Address {
135+
fn from(s: &'static str) -> Self {
136+
Address(H160::from(s))
137+
}
138+
}
139+
140+
impl Into<[u8; 20]> for Address {
141+
fn into(self) -> [u8; 20] {
142+
self.0.into()
143+
}
144+
}
145+
146+
impl AsRef<[u8]> for Address {
147+
fn as_ref(&self) -> &[u8] {
148+
&self.0.as_ref()
149+
}
150+
}
151+
152+
impl HeapSizeOf for Address {
153+
fn heap_size_of_children(&self) -> usize {
154+
self.0.heap_size_of_children()
155+
}
156+
}
27157

28158
#[derive(Debug, PartialEq, Eq, Clone)]
29159
pub struct FullAddress {
@@ -135,7 +265,7 @@ impl FromStr for FullAddress {
135265
for i in 0..20 {
136266
arr[i] = data[1 + i];
137267
}
138-
H160(arr)
268+
Address(H160(arr))
139269
},
140270
})
141271
}

0 commit comments

Comments
 (0)