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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
use crate::chain_params::{ChainParams, MEDIAN_TIME_SPAN};
use bitcoin::blockdata::block::{Header as BitcoinHeader, ValidationError};
use bitcoin::consensus::Params;
use bitcoin::hashes::Hash;
use bitcoin::pow::U256;
use bitcoin::{BlockHash, Target};
use sc_client_api::AuxStore;
use sp_blockchain::HeaderBackend;
use sp_runtime::traits::Block as BlockT;
use std::marker::PhantomData;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use subcoin_primitives::BackendExt;

// 2 hours
const MAX_FUTURE_BLOCK_TIME: u32 = 2 * 60 * 60;

/// Block header error.
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// Block's difficulty is invalid.
    #[error("Incorrect proof-of-work: {{ got: {got:?}, expected: {expected:?} }}")]
    BadDifficultyBits { got: Target, expected: Target },
    /// Block's proof-of-work is invalid.
    #[error("proof-of-work validation failed: {0:?}")]
    InvalidProofOfWork(ValidationError),
    /// Block's timestamp is too far in the future.
    #[error("Block time is too far in the future")]
    TooFarInFuture,
    /// Block's timestamp is too old.
    #[error("Time is the median time of last 11 blocks or before")]
    TimeTooOld,
    #[error("Outdated version")]
    BadVersion,
    /// An error occurred in the client.
    #[error(transparent)]
    Client(#[from] sp_blockchain::Error),
}

/// A struct responsible for verifying block header.
pub struct HeaderVerifier<Block, Client> {
    client: Arc<Client>,
    chain_params: ChainParams,
    _phantom: PhantomData<Block>,
}

impl<Block, Client> Clone for HeaderVerifier<Block, Client> {
    fn clone(&self) -> Self {
        Self {
            client: self.client.clone(),
            chain_params: self.chain_params.clone(),
            _phantom: self._phantom,
        }
    }
}

impl<Block, Client> HeaderVerifier<Block, Client> {
    /// Constructs a new instance of [`HeaderVerifier`].
    pub fn new(client: Arc<Client>, chain_params: ChainParams) -> Self {
        Self {
            client,
            chain_params,
            _phantom: Default::default(),
        }
    }
}

impl<Block, Client> HeaderVerifier<Block, Client>
where
    Block: BlockT,
    Client: HeaderBackend<Block> + AuxStore,
{
    /// Validates the header and returns the block time, which is used for verifying the finality of
    /// transactions.
    ///
    /// The validation process includes:
    /// - Checking the proof of work.
    /// - Validating the block's timestamp:
    ///     - The time must not be more than 2 hours in the future.
    ///     - The time must be greater than the median time of the last 11 blocks.
    ///
    /// <https://github.com/bitcoin/bitcoin/blob/6f9db1ebcab4064065ccd787161bf2b87e03cc1f/src/validation.cpp#L4146>
    pub fn verify(&self, header: &BitcoinHeader) -> Result<u32, Error> {
        let prev_block_hash = header.prev_blockhash;

        let prev_block_header = self.client.block_header(prev_block_hash).ok_or(
            sp_blockchain::Error::MissingHeader(prev_block_hash.to_string()),
        )?;

        let prev_block_height = self
            .client
            .block_number(prev_block_hash)
            .expect("Prev block must exist as we checked before; qed");

        let expected_target = get_next_work_required(
            prev_block_height,
            prev_block_header,
            &self.chain_params.params,
            &self.client,
        );
        let expected_bits = expected_target.to_compact_lossy().to_consensus();

        let actual_target = header.target();

        if actual_target.to_compact_lossy().to_consensus() != expected_bits {
            return Err(Error::BadDifficultyBits {
                got: actual_target,
                expected: expected_target,
            });
        }

        header
            .validate_pow(actual_target)
            .map_err(Error::InvalidProofOfWork)?;

        // Get the seconds since the UNIX epoch
        let current_time = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("Time went backwards")
            .as_secs() as u32;

        if header.time > current_time + MAX_FUTURE_BLOCK_TIME {
            return Err(Error::TooFarInFuture);
        }

        let block_number = prev_block_height + 1;

        let version = header.version.to_consensus();

        if version < 2 && block_number >= self.chain_params.params.bip34_height
            || version < 3 && block_number >= self.chain_params.params.bip66_height
            || version < 4 && block_number >= self.chain_params.params.bip65_height
        {
            return Err(Error::BadVersion);
        }

        // BIP 113
        let lock_time_cutoff = if block_number >= self.chain_params.csv_height {
            let mtp = self.calculate_median_time_past(header);
            if header.time <= mtp {
                return Err(Error::TimeTooOld);
            }
            mtp
        } else {
            header.time
        };

        Ok(lock_time_cutoff)
    }

    /// Check if the proof-of-work is valid.
    pub fn has_valid_proof_of_work(&self, header: &BitcoinHeader) -> bool {
        let target = header.target();

        if target == Target::ZERO
            || target > Target::MAX
            || target > self.chain_params.params.max_attainable_target
        {
            return false;
        }

        header.validate_pow(target).is_ok()
    }

    /// Calculates the median time of the previous few blocks prior to the header (inclusive).
    fn calculate_median_time_past(&self, header: &BitcoinHeader) -> u32 {
        let mut timestamps = Vec::with_capacity(MEDIAN_TIME_SPAN);

        timestamps.push(header.time);

        let zero_hash = BlockHash::all_zeros();

        let mut block_hash = header.prev_blockhash;

        for _ in 0..MEDIAN_TIME_SPAN - 1 {
            // Genesis block
            if block_hash == zero_hash {
                break;
            }

            let header = self
                .client
                .block_header(block_hash)
                .expect("Parent header must exist; qed");

            timestamps.push(header.time);

            block_hash = header.prev_blockhash;
        }

        timestamps.sort_unstable();

        timestamps
            .get(timestamps.len() / 2)
            .copied()
            .expect("Timestamps must be non-empty; qed")
    }
}

/// Usually, it's just the target of last block. However, if we are in a retarget period,
/// it will be calculated from the last 2016 blocks (about two weeks for Bitcoin mainnet).
///
/// <https://github.com/bitcoin/bitcoin/blob/89b910711c004c21b7d67baa888073742f7f94f0/src/pow.cpp#L13>
fn get_next_work_required<Block, Client>(
    last_block_height: u32,
    last_block: BitcoinHeader,
    params: &Params,
    client: &Arc<Client>,
) -> Target
where
    Block: BlockT,
    Client: HeaderBackend<Block> + AuxStore,
{
    if params.no_pow_retargeting {
        return last_block.target();
    }

    let height = last_block_height + 1;

    let difficulty_adjustment_interval = params.difficulty_adjustment_interval() as u32;

    // Only change once per difficulty adjustment interval.
    if height >= difficulty_adjustment_interval && height % difficulty_adjustment_interval == 0 {
        let last_retarget_height = height - difficulty_adjustment_interval;

        let retarget_header_hash = client
            .block_hash(last_retarget_height)
            .expect("Retarget block must be available; qed");

        let retarget_header = client
            .block_header(retarget_header_hash)
            .expect("Retarget block must be available; qed");

        let first_block_time = retarget_header.time;

        // timestamp of last block
        let last_block_time = last_block.time;

        calculate_next_work_required(
            last_block.target().0,
            first_block_time.into(),
            last_block_time.into(),
            params,
        )
    } else {
        last_block.target()
    }
}

// <https://github.com/bitcoin/bitcoin/blob/89b910711c004c21b7d67baa888073742f7f94f0/src/pow.cpp#L49-L72>
fn calculate_next_work_required(
    previous_target: U256,
    first_block_time: u64,
    last_block_time: u64,
    params: &Params,
) -> Target {
    let mut actual_timespan = last_block_time.saturating_sub(first_block_time);

    let pow_target_timespan = params.pow_target_timespan;

    // Limit adjustment step.
    if actual_timespan < pow_target_timespan / 4 {
        actual_timespan = pow_target_timespan / 4;
    }

    if actual_timespan > pow_target_timespan * 4 {
        actual_timespan = pow_target_timespan * 4;
    }

    let pow_limit = params.max_attainable_target;

    // Retarget.
    let target = previous_target * actual_timespan.into();
    let target = Target(target / pow_target_timespan.into());

    if target > pow_limit {
        pow_limit
    } else {
        target
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bitcoin::consensus::encode::deserialize_hex;

    #[test]
    fn test_calculate_next_work_required() {
        // block_354816
        let block_354816: BitcoinHeader = deserialize_hex
            ("020000003f99814a36d2a2043b1d4bf61a410f71828eca1decbf56000000000000000000b3762ed278ac44bb953e24262cfeb952d0abe6d3b7f8b74fd24e009b96b6cb965d674655dd1317186436e79d").unwrap();

        let expected_target = block_354816.target();

        // block_352800, first block in this period.
        let first_block: BitcoinHeader = deserialize_hex("0200000074c51c1cc53aaf478c643bb612da6bd17b268cd9bdccc4000000000000000000ccc0a2618a1f973dfac37827435b463abd18cbfd0f280a90432d3d78497a36cc02f33355f0171718b72a1dc7").unwrap();

        // block_354815, last block in this period.
        let last_block: BitcoinHeader = deserialize_hex("030000004c9c1b59250f30b8d360886a5433501120b056a000bdc0160000000000000000caca1bf0c55a5ba2299f9e60d10c01c679bb266c7df815ff776a1b97fd3a199ac1644655f01717182707bd59").unwrap();

        let new_target = calculate_next_work_required(
            last_block.target().0,
            first_block.time as u64,
            last_block.time as u64,
            &Params::new(bitcoin::Network::Bitcoin),
        );

        assert_eq!(
            new_target.to_compact_lossy().to_consensus(),
            expected_target.to_compact_lossy().to_consensus(),
            "Difficulty bits must match"
        );
    }
}