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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use ckb_constant::sync::{BAD_MESSAGE_BAN_TIME, SYNC_USELESS_BAN_TIME};
use std::fmt::{self, Display, Formatter};
use std::time::Duration;
#[macro_export]
macro_rules! attempt {
($code:expr) => {{
let ret = $code;
if !ret.is_ok() {
return ret;
}
ret
}};
}
#[repr(u16)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StatusCode {
OK = 100,
Ignored = 101,
CompactBlockIsAlreadyPending = 102,
CompactBlockIsAlreadyInFlight = 103,
CompactBlockAlreadyStored = 104,
CompactBlockIsStaled = 105,
CompactBlockRequiresParent = 106,
CompactBlockRequiresFreshTransactions = 107,
CompactBlockMeetsShortIdsCollision = 108,
BlocksInFlightReachLimit = 109,
TooManyRequests = 110,
ProtocolMessageIsMalformed = 400,
BlockIsInvalid = 401,
CompactBlockHasInvalidHeader = 402,
CompactBlockHasDuplicatedShortIds = 403,
CompactBlockHasNotPrefilledCellbase = 404,
CompactBlockHasDuplicatedPrefilledTransactions = 405,
CompactBlockHasOutOfOrderPrefilledTransactions = 406,
CompactBlockHasOutOfIndexPrefilledTransactions = 407,
CompactBlockHasInvalidUncle = 408,
CompactBlockHasUnmatchedTransactionRootWithReconstructedBlock = 409,
BlockTransactionsLengthIsUnmatchedWithPendingCompactBlock = 410,
BlockTransactionsShortIdsAreUnmatchedWithPendingCompactBlock = 411,
BlockUnclesLengthIsUnmatchedWithPendingCompactBlock = 412,
BlockUnclesAreUnmatchedWithPendingCompactBlock = 413,
GetHeadersMissCommonAncestors = 414,
HeadersIsInvalid = 415,
TxPool = 501,
Network = 502,
}
impl StatusCode {
pub fn with_context<S: ToString>(self, context: S) -> Status {
Status::new(self, Some(context))
}
}
#[derive(Clone, Debug, Eq)]
pub struct Status {
code: StatusCode,
context: Option<String>,
}
impl Status {
pub fn new<S: ToString>(code: StatusCode, context: Option<S>) -> Self {
Self {
code,
context: context.map(|s| s.to_string()),
}
}
pub fn ok() -> Self {
Self::new::<&str>(StatusCode::OK, None)
}
pub fn ignored() -> Self {
Self::new::<&str>(StatusCode::Ignored, None)
}
pub fn is_ok(&self) -> bool {
self.code == StatusCode::OK
}
pub fn should_ban(&self) -> Option<Duration> {
if !(400..500).contains(&(self.code as u16)) {
return None;
}
match self.code {
StatusCode::GetHeadersMissCommonAncestors => Some(SYNC_USELESS_BAN_TIME),
_ => Some(BAD_MESSAGE_BAN_TIME),
}
}
pub fn should_warn(&self) -> bool {
self.code as u16 >= 500
}
pub fn code(&self) -> StatusCode {
self.code
}
}
impl PartialEq for Status {
fn eq(&self, other: &Self) -> bool {
self.code == other.code
}
}
impl Display for Status {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self.context {
Some(ref context) => write!(f, "{:?}({}): {}", self.code, self.code as u16, context),
None => write!(f, "{:?}({})", self.code, self.code as u16),
}
}
}
impl From<StatusCode> for Status {
fn from(code: StatusCode) -> Self {
Self::new::<&str>(code, None)
}
}