ckb_verification/
header_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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use crate::{
    BlockVersionError, EpochError, NumberError, PowError, TimestampError, UnknownParentError,
    ALLOWED_FUTURE_BLOCKTIME,
};
use ckb_chain_spec::consensus::Consensus;
use ckb_error::Error;
use ckb_pow::PowEngine;
use ckb_systemtime::unix_time_as_millis;
use ckb_traits::HeaderFieldsProvider;
use ckb_types::core::{BlockNumber, EpochNumberWithFraction, HeaderView};
use ckb_verification_traits::Verifier;

/// Context-dependent verification checks for block header
///
/// By "context", only mean the previous block headers here.
pub struct HeaderVerifier<'a, DL> {
    data_loader: &'a DL,
    consensus: &'a Consensus,
}

impl<'a, DL: HeaderFieldsProvider> HeaderVerifier<'a, DL> {
    /// Crate new HeaderVerifier
    pub fn new(data_loader: &'a DL, consensus: &'a Consensus) -> Self {
        HeaderVerifier {
            consensus,
            data_loader,
        }
    }
}

impl<'a, DL: HeaderFieldsProvider> Verifier for HeaderVerifier<'a, DL> {
    type Target = HeaderView;
    fn verify(&self, header: &Self::Target) -> Result<(), Error> {
        VersionVerifier::new(header, self.consensus).verify()?;
        // POW check first
        PowVerifier::new(header, self.consensus.pow_engine().as_ref()).verify()?;
        let parent_fields = self
            .data_loader
            .get_header_fields(&header.parent_hash())
            .ok_or_else(|| UnknownParentError {
                parent_hash: header.parent_hash(),
            })?;
        NumberVerifier::new(parent_fields.number, header).verify()?;
        EpochVerifier::new(parent_fields.epoch, header).verify()?;
        TimestampVerifier::new(
            self.data_loader,
            header,
            self.consensus.median_time_block_count(),
        )
        .verify()?;
        Ok(())
    }
}

pub struct VersionVerifier<'a> {
    header: &'a HeaderView,
    consensus: &'a Consensus,
}

impl<'a> VersionVerifier<'a> {
    pub fn new(header: &'a HeaderView, consensus: &'a Consensus) -> Self {
        VersionVerifier { header, consensus }
    }

    pub fn verify(&self) -> Result<(), Error> {
        if !self
            .consensus
            .hardfork_switch
            .ckb2023
            .is_remove_header_version_reservation_rule_enabled(self.header.epoch().number())
            && self.header.version() != self.consensus.block_version()
        {
            return Err(BlockVersionError {
                expected: self.consensus.block_version(),
                actual: self.header.version(),
            }
            .into());
        }
        Ok(())
    }
}

pub struct TimestampVerifier<'a, DL> {
    header: &'a HeaderView,
    data_loader: &'a DL,
    median_block_count: usize,
    now: u64,
}

impl<'a, DL: HeaderFieldsProvider> TimestampVerifier<'a, DL> {
    pub fn new(data_loader: &'a DL, header: &'a HeaderView, median_block_count: usize) -> Self {
        TimestampVerifier {
            data_loader,
            header,
            median_block_count,
            now: unix_time_as_millis(),
        }
    }

    pub fn verify(&self) -> Result<(), Error> {
        // skip genesis block
        if self.header.is_genesis() {
            return Ok(());
        }

        let min = self.data_loader.block_median_time(
            &self.header.data().raw().parent_hash(),
            self.median_block_count,
        );
        if self.header.timestamp() <= min {
            return Err(TimestampError::BlockTimeTooOld {
                min,
                actual: self.header.timestamp(),
            }
            .into());
        }
        let max = self.now + ALLOWED_FUTURE_BLOCKTIME;
        if self.header.timestamp() > max {
            return Err(TimestampError::BlockTimeTooNew {
                max,
                actual: self.header.timestamp(),
            }
            .into());
        }
        Ok(())
    }
}

/// Checks if the block number of the given header matches the expected number,
/// which is the parent block's number + 1.
pub struct NumberVerifier<'a> {
    parent: BlockNumber,
    header: &'a HeaderView,
}

impl<'a> NumberVerifier<'a> {
    pub fn new(parent: BlockNumber, header: &'a HeaderView) -> Self {
        NumberVerifier { parent, header }
    }

    pub fn verify(&self) -> Result<(), Error> {
        if self.header.number() != self.parent + 1 {
            return Err(NumberError {
                expected: self.parent + 1,
                actual: self.header.number(),
            }
            .into());
        }
        Ok(())
    }
}

pub struct EpochVerifier<'a> {
    parent: EpochNumberWithFraction,
    header: &'a HeaderView,
}

impl<'a> EpochVerifier<'a> {
    pub fn new(parent: EpochNumberWithFraction, header: &'a HeaderView) -> Self {
        EpochVerifier { parent, header }
    }

    pub fn verify(&self) -> Result<(), Error> {
        if !self.header.epoch().is_well_formed() {
            return Err(EpochError::Malformed {
                value: self.header.epoch(),
            }
            .into());
        }
        if !self.parent.is_genesis() && !self.header.epoch().is_successor_of(self.parent) {
            return Err(EpochError::NonContinuous {
                current: self.header.epoch(),
                parent: self.parent,
            }
            .into());
        }
        Ok(())
    }
}

pub struct PowVerifier<'a> {
    header: &'a HeaderView,
    pow: &'a dyn PowEngine,
}

impl<'a> PowVerifier<'a> {
    pub fn new(header: &'a HeaderView, pow: &'a dyn PowEngine) -> Self {
        PowVerifier { header, pow }
    }

    pub fn verify(&self) -> Result<(), Error> {
        if self.pow.verify(&self.header.data()) {
            Ok(())
        } else {
            Err(PowError::InvalidNonce.into())
        }
    }
}