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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use crate::block_info::{BlockInfo, BlockLocation};
use ethereum_types::H256;
#[derive(Debug, PartialEq, Clone)]
pub struct ImportRoute {
pub retracted: Vec<H256>,
pub enacted: Vec<H256>,
pub omitted: Vec<H256>,
}
impl ImportRoute {
pub fn none() -> Self {
ImportRoute {
retracted: vec![],
enacted: vec![],
omitted: vec![],
}
}
}
impl From<BlockInfo> for ImportRoute {
fn from(info: BlockInfo) -> ImportRoute {
match info.location {
BlockLocation::CanonChain => ImportRoute {
retracted: vec![],
enacted: vec![info.hash],
omitted: vec![],
},
BlockLocation::Branch => ImportRoute {
retracted: vec![],
enacted: vec![],
omitted: vec![info.hash],
},
BlockLocation::BranchBecomingCanonChain(mut data) => {
data.enacted.push(info.hash);
ImportRoute {
retracted: data.retracted,
enacted: data.enacted,
omitted: vec![],
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::ImportRoute;
use crate::block_info::{BlockInfo, BlockLocation, BranchBecomingCanonChainData};
use ethereum_types::{BigEndianHash, U256};
#[test]
fn import_route_none() {
assert_eq!(
ImportRoute::none(),
ImportRoute {
enacted: vec![],
retracted: vec![],
omitted: vec![],
}
);
}
#[test]
fn import_route_branch() {
let info = BlockInfo {
hash: BigEndianHash::from_uint(&U256::from(1)),
number: 0,
total_difficulty: U256::from(0),
location: BlockLocation::Branch,
};
assert_eq!(
ImportRoute::from(info),
ImportRoute {
retracted: vec![],
enacted: vec![],
omitted: vec![BigEndianHash::from_uint(&U256::from(1))],
}
);
}
#[test]
fn import_route_canon_chain() {
let info = BlockInfo {
hash: BigEndianHash::from_uint(&U256::from(1)),
number: 0,
total_difficulty: U256::from(0),
location: BlockLocation::CanonChain,
};
assert_eq!(
ImportRoute::from(info),
ImportRoute {
retracted: vec![],
enacted: vec![BigEndianHash::from_uint(&U256::from(1))],
omitted: vec![],
}
);
}
#[test]
fn import_route_branch_becoming_canon_chain() {
let info = BlockInfo {
hash: BigEndianHash::from_uint(&U256::from(2)),
number: 0,
total_difficulty: U256::from(0),
location: BlockLocation::BranchBecomingCanonChain(BranchBecomingCanonChainData {
ancestor: BigEndianHash::from_uint(&U256::from(0)),
enacted: vec![BigEndianHash::from_uint(&U256::from(1))],
retracted: vec![
BigEndianHash::from_uint(&U256::from(3)),
BigEndianHash::from_uint(&U256::from(4)),
],
}),
};
assert_eq!(
ImportRoute::from(info),
ImportRoute {
retracted: vec![
BigEndianHash::from_uint(&U256::from(3)),
BigEndianHash::from_uint(&U256::from(4))
],
enacted: vec![
BigEndianHash::from_uint(&U256::from(1)),
BigEndianHash::from_uint(&U256::from(2))
],
omitted: vec![],
}
);
}
}