ckb_verification/
genesis_verifier.rs

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
use crate::{
    error::CellbaseError, BlockErrorKind, BlockVerifier, EpochError, NumberError, UnclesError,
    UnknownParentError,
};
use ckb_chain_spec::{calculate_block_reward, consensus::Consensus};
use ckb_dao_utils::genesis_dao_data_with_satoshi_gift;
use ckb_error::Error;
use ckb_types::{core::BlockView, packed::CellInput};
use ckb_verification_traits::Verifier;

/// The genesis verification
///
/// BlockVerifier is not applicable to genesis, genesis have particular rules,
/// It's not limited by block size. Its previous hash is zero hash, and it has no uncles.
#[derive(Clone)]
pub struct GenesisVerifier {}

impl GenesisVerifier {
    /// Create new GenesisVerifier
    pub fn new() -> Self {
        GenesisVerifier {}
    }
}

impl Default for GenesisVerifier {
    fn default() -> Self {
        Self::new()
    }
}

impl Verifier for GenesisVerifier {
    type Target = Consensus;

    fn verify(&self, consensus: &Self::Target) -> Result<(), Error> {
        NumberVerifier::verify(consensus.genesis_block())?;
        EpochVerifier::verify(consensus.genesis_block())?;
        ParentHashVerifier::verify(consensus.genesis_block())?;
        CellbaseVerifier::verify(consensus.genesis_block())?;
        UnclesVerifier::verify(consensus.genesis_block())?;
        DAOVerifier::new(consensus).verify(consensus.genesis_block())?;
        BlockVerifier::new(consensus).verify(consensus.genesis_block())
    }
}

#[derive(Clone)]
pub struct NumberVerifier {}

impl NumberVerifier {
    pub fn verify(block: &BlockView) -> Result<(), Error> {
        if block.header().number() != 0 {
            return Err((NumberError {
                expected: 0,
                actual: block.header().number(),
            })
            .into());
        }
        Ok(())
    }
}

#[derive(Clone)]
pub struct EpochVerifier {}

impl EpochVerifier {
    pub fn verify(block: &BlockView) -> Result<(), Error> {
        if block.header().epoch().number() != 0 {
            return Err((EpochError::NumberMismatch {
                expected: 0,
                actual: block.header().epoch().number(),
            })
            .into());
        }
        Ok(())
    }
}

#[derive(Clone)]
pub struct ParentHashVerifier {}

impl ParentHashVerifier {
    pub fn verify(block: &BlockView) -> Result<(), Error> {
        if block.parent_hash().raw_data()[..] != [0u8; 32][..] {
            return Err((UnknownParentError {
                parent_hash: block.parent_hash(),
            })
            .into());
        }
        Ok(())
    }
}

#[derive(Clone)]
pub struct UnclesVerifier {}

impl UnclesVerifier {
    pub fn verify(block: &BlockView) -> Result<(), Error> {
        if !block.uncles().hashes().is_empty() {
            return Err((UnclesError::OverCount {
                max: 0,
                actual: block.uncles().hashes().len() as u32,
            })
            .into());
        }
        Ok(())
    }
}

#[derive(Clone)]
pub struct DAOVerifier<'a> {
    consensus: &'a Consensus,
}

impl<'a> DAOVerifier<'a> {
    pub fn new(consensus: &'a Consensus) -> Self {
        DAOVerifier { consensus }
    }

    pub fn verify(&self, block: &BlockView) -> Result<(), Error> {
        let txs = block.transactions();
        let epoch_length = self.consensus.genesis_epoch_ext.length();
        let primary_issuance =
            calculate_block_reward(self.consensus.initial_primary_epoch_reward, epoch_length);
        let secondary_issuance =
            calculate_block_reward(self.consensus.secondary_epoch_reward, epoch_length);
        let dao = genesis_dao_data_with_satoshi_gift(
            txs.iter().collect::<Vec<_>>(),
            &self.consensus.satoshi_pubkey_hash,
            self.consensus.satoshi_cell_occupied_ratio,
            primary_issuance,
            secondary_issuance,
        )?;
        if dao != block.header().dao() {
            return Err((BlockErrorKind::InvalidDAO).into());
        }
        Ok(())
    }
}

#[derive(Clone)]
pub struct CellbaseVerifier {}

impl CellbaseVerifier {
    pub fn verify(block: &BlockView) -> Result<(), Error> {
        let cellbase_len = block
            .transactions()
            .iter()
            .filter(|tx| tx.is_cellbase())
            .count();

        // empty checked, block must contain cellbase
        if cellbase_len != 1 {
            return Err((CellbaseError::InvalidQuantity).into());
        }

        let cellbase_transaction = &block.transactions()[0];

        if !cellbase_transaction.is_cellbase() {
            return Err((CellbaseError::InvalidPosition).into());
        }

        // cellbase outputs/outputs_data len must be equalized
        if cellbase_transaction.outputs().len() != cellbase_transaction.outputs_data().len() {
            return Err((CellbaseError::InvalidOutputQuantity).into());
        }

        let cellbase_input = &cellbase_transaction
            .inputs()
            .get(0)
            .expect("cellbase should have input");
        if cellbase_input != &CellInput::new_cellbase_input(block.header().number()) {
            return Err((CellbaseError::InvalidInput).into());
        }

        Ok(())
    }
}