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
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of OpenEthereum.

// OpenEthereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// OpenEthereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with OpenEthereum.  If not, see <http://www.gnu.org/licenses/>.

//! Stores recently seen bad blocks.

use bytes::{Bytes, ToPretty};
use ethereum_types::H256;
use itertools::Itertools;
use memory_cache::MemoryLruCache;
use parking_lot::RwLock;
use types::BlockNumber;
use verification::queue::kind::blocks::Unverified;

/// Recently seen bad blocks.
pub struct BadBlocks {
    last_blocks: RwLock<MemoryLruCache<H256, (Unverified, String)>>,
}

impl Default for BadBlocks {
    fn default() -> Self {
        BadBlocks {
            last_blocks: RwLock::new(MemoryLruCache::new(8 * 1024 * 1024)),
        }
    }
}

impl BadBlocks {
    /// Reports given RLP as invalid block.
    pub fn report(&self, raw: Bytes, message: String, eip1559_transition: BlockNumber) {
        match Unverified::from_rlp(raw, eip1559_transition) {
            Ok(unverified) => {
                error!(
                    target: "client",
                    "\nBad block detected: {}\nRLP: {}\nHeader: {:?}\nUncles: {}\nTransactions:{}\n",
                    message,
                    unverified.bytes.to_hex(),
                    unverified.header,
                    unverified.uncles
                        .iter()
                        .enumerate()
                        .map(|(index, uncle)| format!("[Uncle {}] {:?}", index, uncle))
                        .join("\n"),
                    unverified.transactions
                        .iter()
                        .enumerate()
                        .map(|(index, tx)| format!("[Tx {}] {:?}", index, tx))
                        .join("\n"),
                );
                self.last_blocks
                    .write()
                    .insert(unverified.header.hash(), (unverified, message));
            }
            Err(err) => {
                error!(target: "client", "Bad undecodable block detected: {}\n{:?}", message, err);
            }
        }
    }

    /// Returns a list of recently detected bad blocks with error descriptions.
    pub fn bad_blocks(&self, eip1559_transition: BlockNumber) -> Vec<(Unverified, String)> {
        self.last_blocks
            .read()
            .backstore()
            .iter()
            .map(|(_k, (unverified, message))| {
                (
                    Unverified::from_rlp(unverified.bytes.clone(), eip1559_transition)
                        .expect("Bytes coming from UnverifiedBlock so decodable; qed"),
                    message.clone(),
                )
            })
            .collect()
    }
}