forked from CodeChain-io/codechain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracker.rs
108 lines (84 loc) · 2.76 KB
/
tracker.rs
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
// Copyright 2019 Kodebox, Inc.
// This file is part of CodeChain.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use primitives::H256;
use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};
use std::fmt::{self, Display, Formatter};
use std::ops::Deref;
#[derive(Clone, Copy, Default, Eq, Hash, PartialEq, Debug, Deserialize, Serialize)]
pub struct Tracker(H256);
impl From<H256> for Tracker {
fn from(h: H256) -> Self {
Self(h)
}
}
impl Deref for Tracker {
type Target = H256;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Display for Tracker {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
self.0.fmt(f)
}
}
impl Encodable for Tracker {
fn rlp_append(&self, s: &mut RlpStream) {
self.0.rlp_append(s);
}
}
impl Decodable for Tracker {
fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
Ok(H256::decode(rlp)?.into())
}
}
#[cfg(test)]
mod tests {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use rlp::{self, rlp_encode_and_decode_test};
use super::*;
#[test]
fn hash_of_tracker_and_h256_are_the_same() {
let h256 = H256::random();
let tracker = Tracker(h256);
let mut hasher_of_h256 = DefaultHasher::new();
let mut hasher_of_tracker = DefaultHasher::new();
h256.hash(&mut hasher_of_h256);
tracker.hash(&mut hasher_of_tracker);
assert_eq!(hasher_of_h256.finish(), hasher_of_tracker.finish());
}
#[test]
fn rlp_of_tracker_can_be_decoded_to_h256() {
let h256 = H256::random();
let tracker = Tracker(h256);
let encoded = rlp::encode(&tracker);
let decoded = rlp::decode(&*encoded).unwrap();
assert_eq!(h256, decoded);
}
#[test]
fn rlp_of_h256_can_be_decoded_to_tracker() {
let h256 = H256::random();
let encoded = rlp::encode(&h256);
let decoded = rlp::decode(&*encoded).unwrap();
let tracker = Tracker(h256);
assert_eq!(tracker, decoded);
}
#[test]
fn rlp() {
rlp_encode_and_decode_test!(Tracker(H256::random()));
}
}