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
405
406
407
408
409
// Copyright 2019 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/
mod error;
pub mod message;
pub mod request_manager;
mod state;

#[cfg(test)]
pub mod tests;

mod synchronization_graph;
mod synchronization_phases;
mod synchronization_protocol_handler;
mod synchronization_service;
mod synchronization_state;
pub mod utils;

pub use self::{
    error::Error,
    state::StateSyncConfiguration,
    synchronization_graph::{
        SharedSynchronizationGraph, SyncGraphConfig, SyncGraphStatistics,
        SynchronizationGraph, SynchronizationGraphInner,
        SynchronizationGraphNode,
    },
    synchronization_phases::{
        CatchUpCheckpointPhase, CatchUpFillBlockBodyPhase,
        CatchUpRecoverBlockHeaderFromDbPhase, CatchUpSyncBlockHeaderPhase,
        CatchUpSyncBlockPhase, NormalSyncPhase, SyncPhaseType,
        SynchronizationPhaseManager, SynchronizationPhaseTrait,
    },
    synchronization_protocol_handler::{
        LocalMessageTask, ProtocolConfiguration, SyncHandlerWorkType,
        SynchronizationProtocolHandler, CHECK_RPC_REQUEST_TIMER,
    },
    synchronization_service::{
        SharedSynchronizationService, SynchronizationService,
    },
    synchronization_state::{SynchronizationPeerState, SynchronizationState},
};
use network::service::ProtocolVersion;

/// The current version of the synchronization protocol.
///
/// Bump the version for incompatible changes. Before the version is too
/// high, transit to a new protocol name for the next generation of the
/// protocol.
///
/// To update messages within the protocol, we would like to be
/// backward-compatible as much as possible. DO NOT UPDATE directly on the
/// message. Instead, create a new message, mark the old one for
/// deprecation.
///
/// Do NOT make this const pub.
const SYNCHRONIZATION_PROTOCOL_VERSION: ProtocolVersion = ProtocolVersion(3);
/// Support at most this number of old versions.
const SYNCHRONIZATION_PROTOCOL_OLD_VERSIONS_TO_SUPPORT: u8 = 2;
/// The version to pass to Message for their lifetime declaration.
pub const SYNC_PROTO_V1: ProtocolVersion = ProtocolVersion(1);
pub const SYNC_PROTO_V2: ProtocolVersion = ProtocolVersion(2);
pub const SYNC_PROTO_V3: ProtocolVersion = ProtocolVersion(3);

pub mod random {
    use rand;
    pub fn new() -> rand::prelude::ThreadRng { rand::thread_rng() }
}

pub mod msg_sender {
    use super::message::msgid;
    use crate::message::MsgId;
    use metrics::{register_meter_with_group, Meter};
    use std::sync::Arc;

    pub const NULL: usize = !0;

    lazy_static! {
        static ref GET_BLOCK_TXN_RESPOPNSE_METER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data",
                "get_block_txn_response"
            );
        static ref GET_BLOCK_TXN_RESPOPNSE_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "get_block_txn_response_counter"
            );
        static ref DYNAMIC_CAPABILITY_CHANGE_METER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data",
                "dynamic_capability_change"
            );
        static ref DYNAMIC_CAPABILITY_CHANGE_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "dynamic_capability_change_counter"
            );
        static ref TRANSACTION_DIGESTS_METER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data",
                "transaction_digests"
            );
        static ref TRANSACTION_DIGESTS_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "transaction_digests_counter"
            );
        static ref GET_TRANSACTIONS_METER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data",
                "get_transactions"
            );
        static ref GET_TRANSACTIONS_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "get_transactions_counter"
            );
        static ref GET_TRANSACTIONS_RESPONSE_METER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data",
                "get_transactions_response"
            );
        static ref GET_TRANSACTIONS_RESPONSE_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "get_transactions_response_counter"
            );
        static ref GET_BLOCK_HASHES_BY_EPOCH_METER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data",
                "get_block_hashes_by_epoch"
            );
        static ref GET_BLOCK_HASHES_BY_EPOCH_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "get_block_hashes_by_epoch_counter"
            );
        static ref GET_BLOCK_HASHES_RESPONSE_METER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data",
                "get_block_hashes_response"
            );
        static ref GET_BLOCK_HASHES_RESPONSE_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "get_block_hashes_response_counter"
            );
        static ref OTHER_HIGH_METER: Arc<dyn Meter> = register_meter_with_group(
            "network_connection_data",
            "other_high_meter"
        );
        static ref OTHER_HIGH_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "other_high_meter_counter"
            );
    }
    lazy_static! {
        static ref ON_STATUS_METER: Arc<dyn Meter> =
            register_meter_with_group("network_connection_data", "on_status");
        static ref ON_STATUS_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "on_status_counter"
            );
        static ref GET_BLOCK_HEADER_RESPONSE_METER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data",
                "get_block_header_response"
            );
        static ref GET_BLOCK_HEADER_RESPONSE_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "get_block_header_response_counter"
            );
        static ref GET_BLOCK_HEADERS_METER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data",
                "get_block_headers"
            );
        static ref GET_BLOCK_HEADERS_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "get_block_headers_counter"
            );
        static ref GET_BLOCK_HEADER_CHAIN_METER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data",
                "get_block_header_chain"
            );
        static ref GET_BLOCK_HEADER_CHAIN_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "get_block_header_chain_counter"
            );
        static ref NEW_BLOCK_METER: Arc<dyn Meter> =
            register_meter_with_group("network_connection_data", "new_block");
        static ref NEW_BLOCK_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "new_block_counter"
            );
        static ref NEW_BLOCK_HASHES_METER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data",
                "new_block_hashes"
            );
        static ref NEW_BLOCK_HASHES_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "new_block_hashes_counter"
            );
        static ref GET_BLOCKS_RESPONSE_METER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data",
                "get_blocks_response"
            );
        static ref GET_BLOCKS_RESPONSE_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "get_blocks_response_counter"
            );
        static ref GET_BLOCKS_WITH_PUBLIC_RESPONSE_METER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data",
                "get_blocks_with_public_response"
            );
        static ref GET_BLOCKS_WITH_PUBLIC_RESPONSE_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "get_blocks_with_public_response_counter"
            );
        static ref GET_BLOCKS_METER: Arc<dyn Meter> =
            register_meter_with_group("network_connection_data", "get_blocks");
        static ref GET_BLOCKS_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "get_blocks_counter"
            );
        static ref GET_TERMINAL_BLOCK_HASHES_RESPONSE_METER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data",
                "get_terminal_block_hashes_response"
            );
        static ref GET_TERMINAL_BLOCK_HASHES_RESPONSE_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "get_terminal_block_hashes_response_counter"
            );
        static ref GET_TERMINAL_BLOCK_HASHES_METER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data",
                "get_terminal_block_hashes"
            );
        static ref GET_TERMINAL_BLOCK_HASHES_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "get_terminal_block_hashes_counter"
            );
        static ref TRANSACTIONS_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "transactions_counter"
            );
        static ref GET_CMPCT_BLOCKS_METER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data",
                "get_cmpct_blocks"
            );
        static ref GET_CMPCT_BLOCKS_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "get_cmpct_blocks_counter"
            );
        static ref GET_CMPCT_BLOCKS_RESPONSE_METER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data",
                "get_cmpct_blocks_response"
            );
        static ref GET_CMPCT_BLOCKS_RESPONSE_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "get_cmpct_blocks_response_counter"
            );
        static ref GET_BLOCK_TXN_METER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data",
                "get_block_txn"
            );
        static ref GET_BLOCK_TXN_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "get_block_txn_counter"
            );
    }
    lazy_static! {
        static ref GET_TRANSACTIONS_FROM_TX_HASHES_METER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data",
                "get_transactions_from_tx_hashes"
            );
        static ref GET_TRANSACTIONS_FROM_TX_HASHES_COUNTRER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "get_transactions_from_tx_hashes_counter"
            );
        static ref GET_TRANSACTIONS_FROM_TX_HASHES_RESPONSE_METER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data",
                "get_transactions_from_tx_hashes_response"
            );
        static ref GET_TRANSACTIONS_FROM_TX_HASHES_RESPONSE_COUNTER: Arc<dyn Meter> =
            register_meter_with_group(
                "network_connection_data_counter",
                "get_transactions_from_tx_hashes_response_counter"
            );
    }

    pub fn metric_message(msg_id: MsgId, size: usize) {
        match msg_id {
            msgid::STATUS_V2 => ON_STATUS_METER.mark(size),
            msgid::STATUS_V3 => ON_STATUS_METER.mark(size),
            msgid::GET_BLOCK_HEADERS_RESPONSE => {
                GET_BLOCK_HEADER_RESPONSE_METER.mark(size);
                GET_BLOCK_HEADER_RESPONSE_COUNTER.mark(1);
            }
            msgid::GET_BLOCK_HEADERS => {
                GET_BLOCK_HEADERS_METER.mark(size);
                GET_BLOCK_HEADERS_COUNTER.mark(1);
            }
            msgid::GET_BLOCK_HEADER_CHAIN => {
                GET_BLOCK_HEADER_CHAIN_METER.mark(size);
                GET_BLOCK_HEADER_CHAIN_COUNTER.mark(1);
            }
            msgid::NEW_BLOCK => {
                NEW_BLOCK_METER.mark(size);
                NEW_BLOCK_COUNTER.mark(1);
            }
            msgid::NEW_BLOCK_HASHES => {
                NEW_BLOCK_HASHES_METER.mark(size);
                NEW_BLOCK_HASHES_COUNTER.mark(1);
            }
            msgid::GET_BLOCKS_RESPONSE => {
                GET_BLOCKS_RESPONSE_METER.mark(size);
                GET_BLOCKS_RESPONSE_COUNTER.mark(1);
            }
            msgid::GET_BLOCKS_WITH_PUBLIC_RESPONSE => {
                GET_BLOCKS_WITH_PUBLIC_RESPONSE_METER.mark(size);
                GET_BLOCKS_WITH_PUBLIC_RESPONSE_COUNTER.mark(1);
            }
            msgid::GET_BLOCKS => {
                GET_BLOCKS_METER.mark(size);
                GET_BLOCKS_COUNTER.mark(1);
            }
            msgid::GET_TERMINAL_BLOCK_HASHES_RESPONSE => {
                GET_TERMINAL_BLOCK_HASHES_RESPONSE_METER.mark(size);
                GET_TERMINAL_BLOCK_HASHES_RESPONSE_COUNTER.mark(1);
            }
            msgid::GET_TERMINAL_BLOCK_HASHES => {
                GET_TERMINAL_BLOCK_HASHES_METER.mark(size);
                GET_TERMINAL_BLOCK_HASHES_COUNTER.mark(1);
            }
            msgid::GET_CMPCT_BLOCKS => {
                GET_CMPCT_BLOCKS_METER.mark(size);
                GET_CMPCT_BLOCKS_COUNTER.mark(1);
            }
            msgid::GET_CMPCT_BLOCKS_RESPONSE => {
                GET_CMPCT_BLOCKS_RESPONSE_METER.mark(size);
                GET_CMPCT_BLOCKS_RESPONSE_COUNTER.mark(1);
            }
            msgid::GET_BLOCK_TXN => {
                GET_BLOCK_TXN_METER.mark(size);
                GET_BLOCK_TXN_COUNTER.mark(1);
            }
            msgid::GET_BLOCK_TXN_RESPONSE => {
                GET_BLOCK_TXN_RESPOPNSE_METER.mark(size);
                GET_BLOCK_TXN_RESPOPNSE_COUNTER.mark(1);
            }
            msgid::DYNAMIC_CAPABILITY_CHANGE => {
                DYNAMIC_CAPABILITY_CHANGE_METER.mark(size);
                DYNAMIC_CAPABILITY_CHANGE_COUNTER.mark(1);
            }
            msgid::TRANSACTION_DIGESTS => {
                TRANSACTION_DIGESTS_METER.mark(size);
                TRANSACTION_DIGESTS_COUNTER.mark(1);
            }
            msgid::GET_TRANSACTIONS => {
                GET_TRANSACTIONS_METER.mark(size);
                GET_TRANSACTIONS_COUNTER.mark(1);
            }
            msgid::GET_TRANSACTIONS_RESPONSE => {
                GET_TRANSACTIONS_RESPONSE_METER.mark(size);
                GET_TRANSACTIONS_RESPONSE_COUNTER.mark(1);
            }
            msgid::GET_BLOCK_HASHES_BY_EPOCH => {
                GET_BLOCK_HASHES_BY_EPOCH_METER.mark(size);
                GET_BLOCK_HASHES_BY_EPOCH_COUNTER.mark(1);
            }
            msgid::GET_BLOCK_HASHES_RESPONSE => {
                GET_BLOCK_HASHES_RESPONSE_METER.mark(size);
                GET_BLOCK_HASHES_RESPONSE_COUNTER.mark(1);
            }
            _ => {
                OTHER_HIGH_METER.mark(size);
                OTHER_HIGH_COUNTER.mark(1);
            }
        }
    }
}