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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use std::convert::From;
use std::fs::File;
use std::io;
use std::net::{IpAddr, SocketAddr};
use std::sync::mpsc;
use std::sync::Arc;
use util::RwLock;
use chrono::prelude::*;
use core::core::hash::Hash;
use core::pow::Difficulty;
use core::{core, ser};
use grin_store;
pub const MAX_BLOCK_HEADERS: u32 = 512;
#[allow(dead_code)]
pub const MAX_BLOCK_BODIES: u32 = 16;
pub const MAX_PEER_ADDRS: u32 = 256;
pub const MAX_LOCATORS: u32 = 20;
const BAN_WINDOW: i64 = 10800;
const PEER_MAX_COUNT: u32 = 25;
const PEER_MIN_PREFERRED_COUNT: u32 = 8;
#[derive(Debug)]
pub enum Error {
Serialization(ser::Error),
Connection(io::Error),
BadMessage,
Banned,
ConnectionClose,
Timeout,
Store(grin_store::Error),
PeerWithSelf,
NoDandelionRelay,
ProtocolMismatch {
us: u32,
peer: u32,
},
GenesisMismatch {
us: Hash,
peer: Hash,
},
Send(String),
PeerException,
}
impl From<ser::Error> for Error {
fn from(e: ser::Error) -> Error {
Error::Serialization(e)
}
}
impl From<grin_store::Error> for Error {
fn from(e: grin_store::Error) -> Error {
Error::Store(e)
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Error {
Error::Connection(e)
}
}
impl<T> From<mpsc::TrySendError<T>> for Error {
fn from(e: mpsc::TrySendError<T>) -> Error {
Error::Send(e.to_string())
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct P2PConfig {
pub host: IpAddr,
pub port: u16,
#[serde(default)]
pub seeding_type: Seeding,
pub seeds: Option<Vec<String>>,
pub capabilities: Capabilities,
pub peers_allow: Option<Vec<String>>,
pub peers_deny: Option<Vec<String>>,
pub peers_preferred: Option<Vec<String>>,
pub ban_window: Option<i64>,
pub peer_max_count: Option<u32>,
pub peer_min_preferred_count: Option<u32>,
}
impl Default for P2PConfig {
fn default() -> P2PConfig {
let ipaddr = "0.0.0.0".parse().unwrap();
P2PConfig {
host: ipaddr,
port: 13414,
capabilities: Capabilities::FULL_NODE,
seeding_type: Seeding::default(),
seeds: None,
peers_allow: None,
peers_deny: None,
peers_preferred: None,
ban_window: None,
peer_max_count: None,
peer_min_preferred_count: None,
}
}
}
impl P2PConfig {
pub fn ban_window(&self) -> i64 {
match self.ban_window {
Some(n) => n,
None => BAN_WINDOW,
}
}
pub fn peer_max_count(&self) -> u32 {
match self.peer_max_count {
Some(n) => n,
None => PEER_MAX_COUNT,
}
}
pub fn peer_min_preferred_count(&self) -> u32 {
match self.peer_min_preferred_count {
Some(n) => n,
None => PEER_MIN_PREFERRED_COUNT,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum Seeding {
None,
List,
DNSSeed,
Programmatic,
}
impl Default for Seeding {
fn default() -> Seeding {
Seeding::DNSSeed
}
}
bitflags! {
#[derive(Serialize, Deserialize)]
pub struct Capabilities: u32 {
const UNKNOWN = 0b00000000;
const HEADER_HIST = 0b00000001;
const TXHASHSET_HIST = 0b00000010;
const PEER_LIST = 0b00000100;
const FULL_NODE = Capabilities::HEADER_HIST.bits
| Capabilities::TXHASHSET_HIST.bits
| Capabilities::PEER_LIST.bits;
}
}
enum_from_primitive! {
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum Direction {
Inbound = 0,
Outbound = 1,
}
}
enum_from_primitive! {
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum ReasonForBan {
None = 0,
BadBlock = 1,
BadCompactBlock = 2,
BadBlockHeader = 3,
BadTxHashSet = 4,
ManualBan = 5,
}
}
#[derive(Clone, Debug)]
pub struct PeerLiveInfo {
pub total_difficulty: Difficulty,
pub height: u64,
pub last_seen: DateTime<Utc>,
pub stuck_detector: DateTime<Utc>,
}
#[derive(Clone, Debug)]
pub struct PeerInfo {
pub capabilities: Capabilities,
pub user_agent: String,
pub version: u32,
pub addr: SocketAddr,
pub direction: Direction,
pub live_info: Arc<RwLock<PeerLiveInfo>>,
}
impl PeerInfo {
pub fn total_difficulty(&self) -> Difficulty {
self.live_info.read().total_difficulty
}
pub fn height(&self) -> u64 {
self.live_info.read().height
}
pub fn last_seen(&self) -> DateTime<Utc> {
self.live_info.read().last_seen
}
pub fn update(&self, height: u64, total_difficulty: Difficulty) {
let mut live_info = self.live_info.write();
if total_difficulty != live_info.total_difficulty {
live_info.stuck_detector = Utc::now();
}
live_info.height = height;
live_info.total_difficulty = total_difficulty;
live_info.last_seen = Utc::now()
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PeerInfoDisplay {
pub capabilities: Capabilities,
pub user_agent: String,
pub version: u32,
pub addr: SocketAddr,
pub direction: Direction,
pub total_difficulty: Difficulty,
pub height: u64,
}
impl From<PeerInfo> for PeerInfoDisplay {
fn from(info: PeerInfo) -> PeerInfoDisplay {
PeerInfoDisplay {
capabilities: info.capabilities.clone(),
user_agent: info.user_agent.clone(),
version: info.version.clone(),
addr: info.addr.clone(),
direction: info.direction.clone(),
total_difficulty: info.total_difficulty(),
height: info.height(),
}
}
}
pub struct TxHashSetRead {
pub output_index: u64,
pub kernel_index: u64,
pub reader: File,
}
pub trait ChainAdapter: Sync + Send {
fn total_difficulty(&self) -> Difficulty;
fn total_height(&self) -> u64;
fn transaction_received(&self, tx: core::Transaction, stem: bool);
fn block_received(&self, b: core::Block, addr: SocketAddr) -> bool;
fn compact_block_received(&self, cb: core::CompactBlock, addr: SocketAddr) -> bool;
fn header_received(&self, bh: core::BlockHeader, addr: SocketAddr) -> bool;
fn headers_received(&self, bh: Vec<core::BlockHeader>, addr: SocketAddr) -> bool;
fn locate_headers(&self, locator: Vec<Hash>) -> Vec<core::BlockHeader>;
fn get_block(&self, h: Hash) -> Option<core::Block>;
fn txhashset_read(&self, h: Hash) -> Option<TxHashSetRead>;
fn txhashset_receive_ready(&self) -> bool;
fn txhashset_download_update(
&self,
start_time: DateTime<Utc>,
downloaded_size: u64,
total_size: u64,
) -> bool;
fn txhashset_write(&self, h: Hash, txhashset_data: File, peer_addr: SocketAddr) -> bool;
}
pub trait NetAdapter: ChainAdapter {
fn find_peer_addrs(&self, capab: Capabilities) -> Vec<SocketAddr>;
fn peer_addrs_received(&self, Vec<SocketAddr>);
fn peer_difficulty(&self, SocketAddr, Difficulty, u64);
fn is_banned(&self, addr: SocketAddr) -> bool;
}