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
// Copyright 2019 Conflux Foundation. All rights reserved.
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/

use crate::{
    sync::{
        message::{
            handleable::{Context, Handleable},
            DynamicCapability,
        },
        Error, SynchronizationPeerState,
    },
    NodeType,
};
use cfx_internal_common::ChainIdParamsDeprecated;
use cfx_types::H256;
use network::{NODE_TAG_ARCHIVE, NODE_TAG_FULL, NODE_TAG_NODE_TYPE};
use rlp_derive::{RlpDecodable, RlpEncodable};
use std::{collections::HashSet, time::Instant};
use throttling::token_bucket::TokenBucketManager;

#[derive(Debug, PartialEq, RlpDecodable, RlpEncodable)]
pub struct StatusV2 {
    pub chain_id: ChainIdParamsDeprecated,
    pub genesis_hash: H256,
    pub best_epoch: u64,
    pub terminal_block_hashes: Vec<H256>,
}

impl Handleable for StatusV2 {
    fn handle(self, ctx: &Context) -> Result<(), Error> {
        debug!("on_status, msg=:{:?}", self);

        let chain_id = ctx
            .manager
            .graph
            .consensus
            .best_chain_id()
            .in_native_space();
        if chain_id != self.chain_id.chain_id {
            debug!(
                "Peer {:?} chain_id mismatches (ours: {:?}, theirs: {:?})",
                ctx.node_id, chain_id, self.chain_id,
            );
            bail!(Error::InvalidStatus("chain_id mismatches".into()));
        }
        let genesis_hash = ctx.manager.graph.data_man.true_genesis.hash();
        if ctx.manager.protocol_config.check_status_genesis {
            if genesis_hash != self.genesis_hash {
                debug!(
                "Peer {:?} genesis hash mismatches (ours: {:?}, theirs: {:?})",
                ctx.node_id, genesis_hash, self.genesis_hash
            );
                bail!(Error::InvalidStatus("genesis hash mismatches".into()));
            }
        }

        let latest: HashSet<H256> =
            self.terminal_block_hashes.iter().cloned().collect();

        if let Ok(peer_info) = ctx.manager.syn.get_peer_info(&ctx.node_id) {
            let latest_updated = {
                let mut peer_info = peer_info.write();
                peer_info.update(
                    Some(NodeType::Unknown),
                    latest,
                    self.best_epoch,
                )
            };

            if latest_updated {
                ctx.manager.start_sync(ctx.io);
            }
        } else {
            let peer_protocol_version =
                match ctx.manager.syn.on_status_in_handshaking(&ctx.node_id) {
                    None => {
                        warn!(
                            "Unexpected Status message from peer={}",
                            ctx.node_id
                        );
                        return Err(Error::UnknownPeer.into());
                    }
                    Some(protocol_version) => protocol_version,
                };

            let throttling =
                match ctx.manager.protocol_config.throttling_config_file {
                    Some(ref file) => {
                        TokenBucketManager::load(file, Some("sync_protocol"))
                            .expect("invalid throttling configuration file")
                    }
                    None => TokenBucketManager::default(),
                };

            let mut peer_state = SynchronizationPeerState {
                node_id: ctx.node_id(),
                node_type: NodeType::Unknown,
                is_validator: false,
                protocol_version: peer_protocol_version,
                genesis_hash,
                best_epoch: self.best_epoch,
                latest_block_hashes: latest,
                received_transaction_count: 0,
                heartbeat: Instant::now(),
                capabilities: Default::default(),
                notified_capabilities: Default::default(),
                throttling,
                throttled_msgs: Default::default(),
            };

            peer_state
                .capabilities
                .insert(DynamicCapability::NormalPhase(true));

            debug!(
                "New peer (pv={:?}, gh={:?})",
                peer_protocol_version, self.genesis_hash
            );

            debug!("Peer {:?} connected", ctx.node_id);
            ctx.manager
                .syn
                .peer_connected(ctx.node_id.clone(), peer_state);
            ctx.manager.request_manager.on_peer_connected(&ctx.node_id);

            ctx.manager.start_sync(ctx.io);
        }

        Ok(())
    }
}

#[derive(Debug, PartialEq, RlpDecodable, RlpEncodable)]
pub struct StatusV3 {
    pub chain_id: ChainIdParamsDeprecated,
    pub genesis_hash: H256,
    pub best_epoch: u64,
    pub node_type: NodeType,
    pub terminal_block_hashes: Vec<H256>,
}

impl Handleable for StatusV3 {
    fn handle(self, ctx: &Context) -> Result<(), Error> {
        debug!("on_status, msg=:{:?}", self);

        let chain_id = ctx
            .manager
            .graph
            .consensus
            .get_config()
            .chain_id
            .read()
            .to_native_space_params();
        if !chain_id.matches(&self.chain_id.clone().into(), self.best_epoch) {
            debug!(
                "Peer {:?} chain_id mismatches (ours: {:?}, theirs: {:?})",
                ctx.node_id, chain_id, self.chain_id,
            );
            bail!(Error::InvalidStatus("chain_id mismatches".into()));
        }
        drop(chain_id);

        let genesis_hash = ctx.manager.graph.data_man.true_genesis.hash();
        if ctx.manager.protocol_config.check_status_genesis {
            if genesis_hash != self.genesis_hash {
                debug!(
                "Peer {:?} genesis hash mismatches (ours: {:?}, theirs: {:?})",
                ctx.node_id, genesis_hash, self.genesis_hash
            );
                bail!(Error::InvalidStatus("genesis hash mismatches".into()));
            }
        }

        let latest: HashSet<H256> =
            self.terminal_block_hashes.iter().cloned().collect();

        match self.node_type {
            NodeType::Archive => {
                let key: String = NODE_TAG_NODE_TYPE.into();
                let value: String = NODE_TAG_ARCHIVE.into();
                ctx.insert_peer_node_tag(ctx.node_id(), &key, &value);
            }
            NodeType::Full => {
                let key: String = NODE_TAG_NODE_TYPE.into();
                let value: String = NODE_TAG_FULL.into();
                ctx.insert_peer_node_tag(ctx.node_id(), &key, &value);
            }
            _ => {}
        };

        if let Ok(peer_info) = ctx.manager.syn.get_peer_info(&ctx.node_id) {
            let latest_updated = {
                let mut peer_info = peer_info.write();
                peer_info.update(Some(self.node_type), latest, self.best_epoch)
            };

            if latest_updated {
                ctx.manager.start_sync(ctx.io);
            }
        } else {
            let peer_protocol_version =
                match ctx.manager.syn.on_status_in_handshaking(&ctx.node_id) {
                    None => {
                        warn!(
                            "Unexpected Status message from peer={}",
                            ctx.node_id
                        );
                        return Err(Error::UnknownPeer.into());
                    }
                    Some(protocol_version) => protocol_version,
                };

            let throttling =
                match ctx.manager.protocol_config.throttling_config_file {
                    Some(ref file) => {
                        TokenBucketManager::load(file, Some("sync_protocol"))
                            .expect("invalid throttling configuration file")
                    }
                    None => TokenBucketManager::default(),
                };

            let mut peer_state = SynchronizationPeerState {
                node_id: ctx.node_id(),
                node_type: self.node_type,
                is_validator: false,
                protocol_version: peer_protocol_version,
                genesis_hash,
                best_epoch: self.best_epoch,
                latest_block_hashes: latest,
                received_transaction_count: 0,
                heartbeat: Instant::now(),
                capabilities: Default::default(),
                notified_capabilities: Default::default(),
                throttling,
                throttled_msgs: Default::default(),
            };

            peer_state
                .capabilities
                .insert(DynamicCapability::NormalPhase(true));

            debug!(
                "New peer (pv={:?}, gh={:?})",
                peer_protocol_version, self.genesis_hash
            );

            debug!("Peer {:?} connected", ctx.node_id);
            ctx.manager
                .syn
                .peer_connected(ctx.node_id.clone(), peer_state);
            ctx.manager.request_manager.on_peer_connected(&ctx.node_id);

            ctx.manager.start_sync(ctx.io);
        }

        Ok(())
    }
}