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
use std::{marker::PhantomData, sync::Arc};

use log::error;
use lru::LruCache;
use sc_client_api::HeaderBackend;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};

use crate::{
    phron_primitives::{BlockHash, BlockNumber},
    data_io::ChainInfoCacheConfig,
    BlockId,
};

pub trait ChainInfoProvider: Send + Sync + 'static {
    fn is_block_imported(&mut self, block: &BlockId) -> bool;

    fn get_finalized_at(&mut self, number: BlockNumber) -> Result<BlockId, ()>;

    fn get_parent_hash(&mut self, block: &BlockId) -> Result<BlockHash, ()>;

    fn get_highest_finalized(&mut self) -> BlockId;
}

pub struct SubstrateChainInfoProvider<B, C>
where
    B: BlockT<Hash = BlockHash>,
    B::Header: HeaderT<Number = BlockNumber>,
    C: HeaderBackend<B> + 'static,
{
    client: Arc<C>,
    _phantom: PhantomData<B>,
}

impl<B, C> SubstrateChainInfoProvider<B, C>
where
    B: BlockT<Hash = BlockHash>,
    B::Header: HeaderT<Number = BlockNumber>,
    C: HeaderBackend<B>,
{
    pub fn new(client: Arc<C>) -> Self {
        SubstrateChainInfoProvider {
            client,
            _phantom: PhantomData,
        }
    }
}
impl<B, C> ChainInfoProvider for SubstrateChainInfoProvider<B, C>
where
    B: BlockT<Hash = BlockHash>,
    B::Header: HeaderT<Number = BlockNumber>,
    C: HeaderBackend<B>,
{
    fn is_block_imported(&mut self, block: &BlockId) -> bool {
        let maybe_header = self
            .client
            .header(block.hash)
            .expect("client must answer a query");
        if let Some(header) = maybe_header {
            // If the block number is incorrect, we treat as not imported.
            return *header.number() == block.number;
        }
        false
    }

    fn get_finalized_at(&mut self, num: BlockNumber) -> Result<BlockId, ()> {
        if self.client.info().finalized_number < num {
            return Err(());
        }

        let block_hash = match self.client.hash(num).ok().flatten() {
            None => {
                error!(target: "chain-info", "Could not get hash for block #{:?}", num);
                return Err(());
            }
            Some(h) => h,
        };

        if let Some(header) = self.client.header(block_hash).expect("client must respond") {
            Ok((header.hash(), num).into())
        } else {
            Err(())
        }
    }

    fn get_parent_hash(&mut self, block: &BlockId) -> Result<BlockHash, ()> {
        if let Some(header) = self.client.header(block.hash).expect("client must respond") {
            Ok(*header.parent_hash())
        } else {
            Err(())
        }
    }

    fn get_highest_finalized(&mut self) -> BlockId {
        let status = self.client.info();
        (status.finalized_hash, status.finalized_number).into()
    }
}

pub struct CachedChainInfoProvider<CIP>
where
    CIP: ChainInfoProvider,
{
    available_block_with_parent_cache: LruCache<BlockId, BlockHash>,
    available_blocks_cache: LruCache<BlockId, ()>,
    finalized_cache: LruCache<BlockNumber, BlockHash>,
    chain_info_provider: CIP,
}

impl<CIP> CachedChainInfoProvider<CIP>
where
    CIP: ChainInfoProvider,
{
    pub fn new(chain_info_provider: CIP, config: ChainInfoCacheConfig) -> Self {
        CachedChainInfoProvider {
            available_block_with_parent_cache: LruCache::new(config.block_cache_capacity),
            available_blocks_cache: LruCache::new(config.block_cache_capacity),
            finalized_cache: LruCache::new(config.block_cache_capacity),
            chain_info_provider,
        }
    }

    pub fn inner(&mut self) -> &mut CIP {
        &mut self.chain_info_provider
    }
}

impl<CIP> ChainInfoProvider for CachedChainInfoProvider<CIP>
where
    CIP: ChainInfoProvider,
{
    fn is_block_imported(&mut self, block: &BlockId) -> bool {
        if self.available_blocks_cache.contains(block) {
            return true;
        }

        if self.chain_info_provider.is_block_imported(block) {
            self.available_blocks_cache.put(block.clone(), ());
            return true;
        }
        false
    }

    fn get_finalized_at(&mut self, num: BlockNumber) -> Result<BlockId, ()> {
        if let Some(hash) = self.finalized_cache.get(&num) {
            return Ok((*hash, num).into());
        }

        if self.get_highest_finalized().number < num {
            return Err(());
        }

        if let Ok(block) = self.chain_info_provider.get_finalized_at(num) {
            self.finalized_cache.put(num, block.hash);
            return Ok(block);
        }
        Err(())
    }

    fn get_parent_hash(&mut self, block: &BlockId) -> Result<BlockHash, ()> {
        if let Some(parent) = self.available_block_with_parent_cache.get(block) {
            return Ok(*parent);
        }

        if let Ok(parent) = self.chain_info_provider.get_parent_hash(block) {
            self.available_block_with_parent_cache
                .put(block.clone(), parent);
            return Ok(parent);
        }
        Err(())
    }

    fn get_highest_finalized(&mut self) -> BlockId {
        self.chain_info_provider.get_highest_finalized()
    }
}

// A wrapper around any ChainInfoProvider that uses auxiliary information on finalization `aux_finalized`
// and considers as finalized a block that is either finalized in the sense of the inner ChainInfoProvider
// or is <= the `aux_finalized` block.
// `aux_finalized` is supposed to be updated using `update_aux_finalized`.
pub struct AuxFinalizationChainInfoProvider<CIP>
where
    CIP: ChainInfoProvider,
{
    aux_finalized: BlockId,
    chain_info_provider: CIP,
}

impl<CIP> AuxFinalizationChainInfoProvider<CIP>
where
    CIP: ChainInfoProvider,
{
    pub fn new(chain_info_provider: CIP, aux_finalized: BlockId) -> Self {
        AuxFinalizationChainInfoProvider {
            aux_finalized,
            chain_info_provider,
        }
    }

    pub fn update_aux_finalized(&mut self, aux_finalized: BlockId) {
        self.aux_finalized = aux_finalized;
    }
}

impl<CIP> ChainInfoProvider for AuxFinalizationChainInfoProvider<CIP>
where
    CIP: ChainInfoProvider,
{
    fn is_block_imported(&mut self, block: &BlockId) -> bool {
        self.chain_info_provider.is_block_imported(block)
    }

    fn get_finalized_at(&mut self, num: BlockNumber) -> Result<BlockId, ()> {
        let highest_finalized_inner = self.chain_info_provider.get_highest_finalized();
        if num <= highest_finalized_inner.number {
            return self.chain_info_provider.get_finalized_at(num);
        }
        if num > self.aux_finalized.number {
            return Err(());
        }
        // We are in the situation: internal_highest_finalized < num <= aux_finalized
        let mut curr_block = self.aux_finalized.clone();
        while curr_block.number > num {
            let parent_hash = self.chain_info_provider.get_parent_hash(&curr_block)?;
            curr_block = (parent_hash, curr_block.number - 1).into();
        }
        Ok(curr_block)
    }

    fn get_parent_hash(&mut self, block: &BlockId) -> Result<BlockHash, ()> {
        self.chain_info_provider.get_parent_hash(block)
    }

    fn get_highest_finalized(&mut self) -> BlockId {
        let highest_finalized_inner = self.chain_info_provider.get_highest_finalized();
        if self.aux_finalized.number > highest_finalized_inner.number {
            self.aux_finalized.clone()
        } else {
            highest_finalized_inner
        }
    }
}