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
// 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::{
    message::RequestId,
    sync::{
        message::{
            metrics::BLOCK_HANDLE_TIMER, Context, GetBlocks, GetCompactBlocks,
            Handleable,
        },
        synchronization_protocol_handler::RecoverPublicTask,
        Error,
    },
};
use cfx_types::H256;
use metrics::MeterTimer;
use primitives::Block;
use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};
use rlp_derive::{RlpDecodable, RlpEncodable};
use std::collections::HashSet;

#[derive(Debug, PartialEq, Default, RlpDecodable, RlpEncodable)]
pub struct GetBlocksResponse {
    pub request_id: RequestId,
    pub blocks: Vec<Block>,
}

impl Handleable for GetBlocksResponse {
    fn handle(self, ctx: &Context) -> Result<(), Error> {
        let _timer = MeterTimer::time_func(BLOCK_HANDLE_TIMER.as_ref());

        debug!(
            "on_blocks_response, get block hashes {:?}",
            self.blocks
                .iter()
                .map(|b| b.block_header.hash())
                .collect::<Vec<H256>>()
        );

        // TODO Check block size in advance to avoid attacks causing OOM.
        if ctx.manager.is_block_queue_full() {
            debug!("recover_public_queue is full, discard GetBlocksResponse");
            return Ok(());
        }

        for block in &self.blocks {
            debug!("transaction received by block: ratio=1");
            debug!(
                "new block received: block_header={:?}, tx_count={}, block_size={}",
                block.block_header,
                block.transactions.len(),
                block.size(),
            );
        }

        let req = ctx.match_request(self.request_id)?;
        let delay = req.delay;
        let requested_blocks: HashSet<H256> = req
            .downcast_ref::<GetBlocks>(ctx.io, &ctx.manager.request_manager)?
            .hashes
            .iter()
            .cloned()
            .collect();

        ctx.manager.recover_public_queue.dispatch(
            ctx.io,
            RecoverPublicTask::new(
                self.blocks,
                requested_blocks,
                ctx.node_id.clone(),
                false,
                delay,
            ),
        );

        Ok(())
    }
}

//////////////////////////////////////////////////////////////

#[derive(Debug, PartialEq, Default)]
pub struct GetBlocksWithPublicResponse {
    pub request_id: RequestId,
    pub blocks: Vec<Block>,
}

impl Handleable for GetBlocksWithPublicResponse {
    fn handle(self, ctx: &Context) -> Result<(), Error> {
        debug!(
            "on_blocks_with_public_response, get block hashes {:?}",
            self.blocks
                .iter()
                .map(|b| b.block_header.hash())
                .collect::<Vec<H256>>()
        );
        let req = ctx.match_request(self.request_id)?;
        let delay = req.delay;
        let req_hashes: HashSet<H256> = if let Ok(req) = req
            .downcast_ref::<GetCompactBlocks>(
                ctx.io,
                &ctx.manager.request_manager,
            ) {
            req.hashes.iter().cloned().collect()
        } else {
            let req = req.downcast_ref::<GetBlocks>(
                ctx.io,
                &ctx.manager.request_manager,
            )?;
            req.hashes.iter().cloned().collect()
        };

        ctx.manager.recover_public_queue.dispatch(
            ctx.io,
            RecoverPublicTask::new(
                self.blocks,
                req_hashes,
                ctx.node_id.clone(),
                false, /* compact */
                delay,
            ),
        );

        Ok(())
    }
}

impl Encodable for GetBlocksWithPublicResponse {
    fn rlp_append(&self, stream: &mut RlpStream) {
        stream
            .begin_list(2)
            .append(&self.request_id)
            .begin_list(self.blocks.len());

        for block in self.blocks.iter() {
            stream.begin_list(2).append(&block.block_header);
            stream.begin_list(block.transactions.len());
            for tx in &block.transactions {
                stream.append(tx.as_ref());
            }
        }
    }
}

impl Decodable for GetBlocksWithPublicResponse {
    fn decode(rlp: &Rlp) -> Result<Self, DecoderError> {
        let request_id = rlp.val_at(0)?;
        let rlp_blocks = rlp.at(1)?;
        let mut blocks = Vec::new();

        for i in 0..rlp_blocks.item_count()? {
            let rlp_block = rlp_blocks.at(i)?;
            let block = Block::decode_with_tx_public(&rlp_block)
                .expect("Wrong block rlp format!");
            blocks.push(block);
        }

        Ok(GetBlocksWithPublicResponse { request_id, blocks })
    }
}