cfxcore/sync/message/
heartbeat.rs

1// Copyright 2019 Conflux Foundation. All rights reserved.
2// Conflux is free software and distributed under GNU General Public License.
3// See http://www.gnu.org/licenses/
4
5use crate::sync::{
6    message::handleable::{Context, Handleable},
7    Error,
8};
9use cfx_types::H256;
10use rlp_derive::{RlpDecodable, RlpEncodable};
11use std::collections::HashSet;
12
13#[derive(Debug, PartialEq, RlpDecodable, RlpEncodable)]
14pub struct Heartbeat {
15    pub best_epoch: u64,
16    pub terminal_block_hashes: Vec<H256>,
17}
18
19impl Handleable for Heartbeat {
20    fn handle(self, ctx: &Context) -> Result<(), Error> {
21        debug!("on_heartbeat, msg=:{:?}", self);
22
23        if let Ok(peer_info) = ctx.manager.syn.get_peer_info(&ctx.node_id) {
24            let latest: HashSet<H256> =
25                self.terminal_block_hashes.iter().cloned().collect();
26
27            let latest_updated = {
28                let mut peer_info = peer_info.write();
29                peer_info.update(None, latest, self.best_epoch)
30            };
31
32            if latest_updated {
33                ctx.manager.start_sync(ctx.io);
34            }
35        }
36
37        Ok(())
38    }
39}