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
// 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_TXN_HANDLE_TIMER, Context, GetBlockTxn, Handleable,
        },
        Error,
    },
};
use cfx_types::H256;
use metrics::MeterTimer;
use primitives::{Block, TransactionWithSignature};
use rlp_derive::{RlpDecodable, RlpEncodable};
use std::collections::HashSet;

#[derive(Debug, PartialEq, Default, RlpEncodable, RlpDecodable)]
pub struct GetBlockTxnResponse {
    pub request_id: RequestId,
    pub block_hash: H256,
    pub block_txn: Vec<TransactionWithSignature>,
}

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

        debug!("on_get_blocktxn_response, hash={:?}", self.block_hash);

        let resp_hash = self.block_hash;
        let req = ctx.match_request(self.request_id)?;
        let delay = req.delay;
        let req = req.downcast_ref::<GetBlockTxn>(
            ctx.io,
            &ctx.manager.request_manager,
        )?;

        let mut request_from_same_peer = false;
        // There can be at most one success block in this set.
        let mut received_blocks = HashSet::new();
        if resp_hash != req.block_hash {
            warn!("Response blocktxn is not the requested block, req={:?}, resp={:?}", req.block_hash, resp_hash);
        } else if ctx.manager.graph.contains_block(&resp_hash) {
            debug!(
                "Get blocktxn, but full block already received, hash={}",
                resp_hash
            );
            received_blocks.insert(resp_hash);
        } else if let Some(header) =
            ctx.manager.graph.block_header_by_hash(&resp_hash)
        {
            debug!("Process blocktxn hash={:?}", resp_hash);
            let signed_txns = ctx
                .manager
                .graph
                .data_man
                .recover_unsigned_tx_with_order(&self.block_txn)?;
            match ctx.manager.graph.data_man.compact_block_by_hash(&resp_hash) {
                Some(cmpct) => {
                    let mut trans =
                        Vec::with_capacity(cmpct.reconstructed_txns.len());
                    let mut index = 0;
                    for tx in cmpct.reconstructed_txns {
                        match tx {
                            Some(tx) => trans.push(tx),
                            None => {
                                trans.push(signed_txns[index].clone());
                                index += 1;
                            }
                        }
                    }
                    // FIXME Should check if hash matches
                    let block = Block::new(header, trans);
                    debug!(
                        "transaction received by block: ratio={:?}",
                        self.block_txn.len() as f64
                            / block.transactions.len() as f64
                    );
                    debug!(
                        "new block received: block_header={:?}, tx_count={}, block_size={}",
                        block.block_header,
                        block.transactions.len(),
                        block.size(),
                    );
                    let insert_result = ctx.manager.graph.insert_block(
                        block, true,  // need_to_verify
                        true,  // persistent
                        false, // recover_from_db
                    );

                    if !insert_result.request_again() {
                        received_blocks.insert(resp_hash);
                    }
                    if insert_result.is_valid() {
                        //insert signed transaction to tx pool
                        let (signed_txns, _) = ctx
                            .manager
                            .graph
                            .consensus
                            .get_tx_pool()
                            .insert_new_signed_transactions(signed_txns);
                        // a transaction from compact block should be
                        // added to received pool
                        ctx.manager
                            .request_manager
                            .append_received_transactions(signed_txns);
                    }
                    if insert_result.should_relay()
                        && !ctx.manager.catch_up_mode()
                    {
                        ctx.manager.relay_blocks(ctx.io, vec![resp_hash]).ok();
                    }
                    if insert_result.request_again() {
                        request_from_same_peer = true;
                    }
                }
                None => {
                    warn!(
                        "Get blocktxn, but misses compact block, hash={}",
                        resp_hash
                    );
                }
            }
        } else {
            warn!("Get blocktxn, but header not received, hash={}", resp_hash);
        }

        let peer = if request_from_same_peer {
            Some(ctx.node_id.clone())
        } else {
            None
        };
        ctx.manager.blocks_received(
            ctx.io,
            vec![req.block_hash].into_iter().collect(),
            received_blocks,
            true,
            peer,
            delay,
            None, /* preferred_node_type_for_block_request */
        );
        Ok(())
    }
}