kyoto/chain/
checkpoints.rs

1use std::str::FromStr;
2
3use bitcoin::BlockHash;
4
5type Height = u32;
6
7/// A known block hash in the chain of most work.
8#[derive(Debug, Clone, Copy)]
9pub struct HeaderCheckpoint {
10    /// The index of the block hash.
11    pub height: Height,
12    /// The Bitcoin block hash expected at this height
13    pub hash: BlockHash,
14}
15
16impl HeaderCheckpoint {
17    /// Create a new checkpoint from a known checkpoint of significant work.
18    pub fn new(height: Height, hash: BlockHash) -> Self {
19        HeaderCheckpoint { height, hash }
20    }
21
22    /// One block before the activation of the taproot softfork.
23    pub fn taproot_activation() -> Self {
24        let hash = "000000000000000000013712fc242ee6dd28476d0e9c931c75f83e6974c6bccc"
25            .parse::<BlockHash>()
26            .unwrap();
27        let height = 709_631;
28        HeaderCheckpoint { height, hash }
29    }
30
31    /// One block before the activation of the segwit softfork.
32    pub fn segwit_activation() -> Self {
33        let hash = "000000000000000000cbeff0b533f8e1189cf09dfbebf57a8ebe349362811b80"
34            .parse::<BlockHash>()
35            .unwrap();
36        let height = 481_823;
37        HeaderCheckpoint { height, hash }
38    }
39}
40
41impl From<(u32, BlockHash)> for HeaderCheckpoint {
42    fn from(value: (u32, BlockHash)) -> Self {
43        HeaderCheckpoint::new(value.0, value.1)
44    }
45}
46
47impl TryFrom<(u32, String)> for HeaderCheckpoint {
48    type Error = <BlockHash as FromStr>::Err;
49
50    fn try_from(value: (u32, String)) -> Result<Self, Self::Error> {
51        let hash = BlockHash::from_str(&value.1)?;
52        Ok(HeaderCheckpoint::new(value.0, hash))
53    }
54}
55
56impl TryFrom<(u32, &str)> for HeaderCheckpoint {
57    type Error = <BlockHash as FromStr>::Err;
58
59    fn try_from(value: (u32, &str)) -> Result<Self, Self::Error> {
60        let hash = BlockHash::from_str(value.1)?;
61        Ok(HeaderCheckpoint::new(value.0, hash))
62    }
63}