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
use super::Error;
use crate::delay::RandomDelay;
use async_trait::async_trait;
use bitcoin::{sha256, Hash};
use runtime::{BtcRelayPallet, H256Le, InterBtcParachain, RawBlockHeader};
use std::sync::Arc;

#[async_trait]
pub trait Issuing {
    /// Returns true if the light client is initialized
    async fn is_initialized(&self) -> Result<bool, Error>;

    /// Initialize the light client
    ///
    /// # Arguments
    ///
    /// * `header` - Raw block header
    /// * `height` - Starting height
    async fn initialize(&self, header: Vec<u8>, height: u32) -> Result<(), Error>;

    /// Submit a block header and wait for inclusion
    ///
    /// # Arguments
    ///
    /// * `header` - Raw block header
    async fn submit_block_header(
        &self,
        header: Vec<u8>,
        random_delay: Arc<Box<dyn RandomDelay + Send + Sync>>,
    ) -> Result<(), Error>;

    /// Submit a batch of block headers and wait for inclusion
    ///
    /// # Arguments
    ///
    /// * `headers` - Raw block headers (multiple of 80 bytes)
    async fn submit_block_header_batch(&self, headers: Vec<Vec<u8>>) -> Result<(), Error>;

    /// Returns the light client's chain tip
    async fn get_best_height(&self) -> Result<u32, Error>;

    /// Returns the block hash stored at a given height,
    /// this is assumed to be in little-endian format
    ///
    /// # Arguments
    ///
    /// * `height` - Height of the block to fetch
    async fn get_block_hash(&self, height: u32) -> Result<Vec<u8>, Error>;

    /// Returns true if the block described by the hash
    /// has been stored in the light client
    ///
    /// # Arguments
    ///
    /// * `hash_le` - Hash (little-endian) of the block
    async fn is_block_stored(&self, hash_le: Vec<u8>) -> Result<bool, Error>;
}

#[async_trait]
impl Issuing for InterBtcParachain {
    async fn is_initialized(&self) -> Result<bool, Error> {
        let hash = BtcRelayPallet::get_best_block(self).await?;
        Ok(!hash.is_zero())
    }

    async fn initialize(&self, header: Vec<u8>, height: u32) -> Result<(), Error> {
        BtcRelayPallet::initialize_btc_relay(self, RawBlockHeader(header), height)
            .await
            .map_err(Into::into)
    }

    #[tracing::instrument(name = "submit_block_header", skip(self, header))]
    async fn submit_block_header(
        &self,
        header: Vec<u8>,
        random_delay: Arc<Box<dyn RandomDelay + Send + Sync>>,
    ) -> Result<(), Error> {
        let raw_block_header = RawBlockHeader(header.clone());

        // wait a random amount of blocks, to avoid all vaults flooding the parachain with
        // this transaction
        (*random_delay)
            .delay(sha256::Hash::hash(header.as_slice()).as_byte_array())
            .await?;
        if self
            .is_block_stored(raw_block_header.hash().to_bytes_le().to_vec())
            .await?
        {
            return Ok(());
        }
        BtcRelayPallet::store_block_header(self, raw_block_header)
            .await
            .map_err(Into::into)
    }

    #[tracing::instrument(name = "submit_block_header_batch", skip(self, headers))]
    async fn submit_block_header_batch(&self, headers: Vec<Vec<u8>>) -> Result<(), Error> {
        BtcRelayPallet::store_block_headers(
            self,
            headers
                .iter()
                .map(|header| RawBlockHeader(header.to_vec()))
                .collect::<Vec<_>>(),
        )
        .await
        .map_err(Into::into)
    }

    async fn get_best_height(&self) -> Result<u32, Error> {
        BtcRelayPallet::get_best_block_height(self).await.map_err(Into::into)
    }

    async fn get_block_hash(&self, height: u32) -> Result<Vec<u8>, Error> {
        let hash = BtcRelayPallet::get_block_hash(self, height).await?;
        hex::decode(hash.to_hex_le()).map_err(|_| Error::DecodeHash)
    }

    async fn is_block_stored(&self, hash_le: Vec<u8>) -> Result<bool, Error> {
        let head = BtcRelayPallet::get_block_header(self, H256Le::from_bytes_le(&hash_le)).await?;
        Ok(head.block_height > 0)
    }
}