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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use bytemuck::TransparentWrapper;
use monero_serai::block::{Block, BlockHeader};

use cuprate_database::{DatabaseRo, DatabaseRw, RuntimeError, StorableVec};
use cuprate_helper::map::{combine_low_high_bits_to_u128, split_u128_into_low_high_bits};
use cuprate_types::{AltBlockInformation, Chain, ChainId, ExtendedBlockHeader, HardFork};

use crate::{
    ops::{
        alt_block::{add_alt_transaction_blob, get_alt_transaction, update_alt_chain_info},
        block::get_block_info,
        macros::doc_error,
    },
    tables::{Tables, TablesMut},
    types::{AltBlockHeight, BlockHash, BlockHeight, CompactAltBlockInfo},
};

/// Flush all alt-block data from all the alt-block tables.
///
/// This function completely empties the alt block tables.
pub fn flush_alt_blocks<'a, E: cuprate_database::EnvInner<'a>>(
    env_inner: &E,
    tx_rw: &mut E::Rw<'_>,
) -> Result<(), RuntimeError> {
    use crate::tables::{
        AltBlockBlobs, AltBlockHeights, AltBlocksInfo, AltChainInfos, AltTransactionBlobs,
        AltTransactionInfos,
    };

    env_inner.clear_db::<AltChainInfos>(tx_rw)?;
    env_inner.clear_db::<AltBlockHeights>(tx_rw)?;
    env_inner.clear_db::<AltBlocksInfo>(tx_rw)?;
    env_inner.clear_db::<AltBlockBlobs>(tx_rw)?;
    env_inner.clear_db::<AltTransactionBlobs>(tx_rw)?;
    env_inner.clear_db::<AltTransactionInfos>(tx_rw)
}

/// Add a [`AltBlockInformation`] to the database.
///
/// This extracts all the data from the input block and
/// maps/adds them to the appropriate database tables.
///
#[doc = doc_error!()]
///
/// # Panics
/// This function will panic if:
/// - `alt_block.height` is == `0`
/// - `alt_block.txs.len()` != `alt_block.block.transactions.len()`
///
pub fn add_alt_block(
    alt_block: &AltBlockInformation,
    tables: &mut impl TablesMut,
) -> Result<(), RuntimeError> {
    let alt_block_height = AltBlockHeight {
        chain_id: alt_block.chain_id.into(),
        height: alt_block.height,
    };

    tables
        .alt_block_heights_mut()
        .put(&alt_block.block_hash, &alt_block_height)?;

    update_alt_chain_info(&alt_block_height, &alt_block.block.header.previous, tables)?;

    let (cumulative_difficulty_low, cumulative_difficulty_high) =
        split_u128_into_low_high_bits(alt_block.cumulative_difficulty);

    let alt_block_info = CompactAltBlockInfo {
        block_hash: alt_block.block_hash,
        pow_hash: alt_block.pow_hash,
        height: alt_block.height,
        weight: alt_block.weight,
        long_term_weight: alt_block.long_term_weight,
        cumulative_difficulty_low,
        cumulative_difficulty_high,
    };

    tables
        .alt_blocks_info_mut()
        .put(&alt_block_height, &alt_block_info)?;

    tables.alt_block_blobs_mut().put(
        &alt_block_height,
        StorableVec::wrap_ref(&alt_block.block_blob),
    )?;

    assert_eq!(alt_block.txs.len(), alt_block.block.transactions.len());
    for tx in &alt_block.txs {
        add_alt_transaction_blob(tx, tables)?;
    }

    Ok(())
}

/// Retrieves an [`AltBlockInformation`] from the database.
///
/// This function will look at only the blocks with the given [`AltBlockHeight::chain_id`], no others
/// even if they are technically part of this chain.
#[doc = doc_error!()]
pub fn get_alt_block(
    alt_block_height: &AltBlockHeight,
    tables: &impl Tables,
) -> Result<AltBlockInformation, RuntimeError> {
    let block_info = tables.alt_blocks_info().get(alt_block_height)?;

    let block_blob = tables.alt_block_blobs().get(alt_block_height)?.0;

    let block = Block::read(&mut block_blob.as_slice())?;

    let txs = block
        .transactions
        .iter()
        .map(|tx_hash| get_alt_transaction(tx_hash, tables))
        .collect::<Result<_, RuntimeError>>()?;

    Ok(AltBlockInformation {
        block,
        block_blob,
        txs,
        block_hash: block_info.block_hash,
        pow_hash: block_info.pow_hash,
        height: block_info.height,
        weight: block_info.weight,
        long_term_weight: block_info.long_term_weight,
        cumulative_difficulty: combine_low_high_bits_to_u128(
            block_info.cumulative_difficulty_low,
            block_info.cumulative_difficulty_high,
        ),
        chain_id: alt_block_height.chain_id.into(),
    })
}

/// Retrieves the hash of the block at the given `block_height` on the alt chain with
/// the given [`ChainId`].
///
/// This function will get blocks from the whole chain, for example if you were to ask for height
/// `0` with any [`ChainId`] (as long that chain actually exists) you will get the main chain genesis.
///
#[doc = doc_error!()]
pub fn get_alt_block_hash(
    block_height: &BlockHeight,
    alt_chain: ChainId,
    tables: &impl Tables,
) -> Result<BlockHash, RuntimeError> {
    let alt_chains = tables.alt_chain_infos();

    // First find what [`ChainId`] this block would be stored under.
    let original_chain = {
        let mut chain = alt_chain.into();
        loop {
            let chain_info = alt_chains.get(&chain)?;

            if chain_info.common_ancestor_height < *block_height {
                break Chain::Alt(chain.into());
            }

            match chain_info.parent_chain.into() {
                Chain::Main => break Chain::Main,
                Chain::Alt(alt_chain_id) => {
                    chain = alt_chain_id.into();
                    continue;
                }
            }
        }
    };

    // Get the block hash.
    match original_chain {
        Chain::Main => {
            get_block_info(block_height, tables.block_infos()).map(|info| info.block_hash)
        }
        Chain::Alt(chain_id) => tables
            .alt_blocks_info()
            .get(&AltBlockHeight {
                chain_id: chain_id.into(),
                height: *block_height,
            })
            .map(|info| info.block_hash),
    }
}

/// Retrieves the [`ExtendedBlockHeader`] of the alt-block with an exact [`AltBlockHeight`].
///
/// This function will look at only the blocks with the given [`AltBlockHeight::chain_id`], no others
/// even if they are technically part of this chain.
///
#[doc = doc_error!()]
pub fn get_alt_block_extended_header_from_height(
    height: &AltBlockHeight,
    table: &impl Tables,
) -> Result<ExtendedBlockHeader, RuntimeError> {
    let block_info = table.alt_blocks_info().get(height)?;

    let block_blob = table.alt_block_blobs().get(height)?.0;

    let block_header = BlockHeader::read(&mut block_blob.as_slice())?;

    Ok(ExtendedBlockHeader {
        version: HardFork::from_version(block_header.hardfork_version)
            .expect("Block in DB must have correct version"),
        vote: block_header.hardfork_version,
        timestamp: block_header.timestamp,
        cumulative_difficulty: combine_low_high_bits_to_u128(
            block_info.cumulative_difficulty_low,
            block_info.cumulative_difficulty_high,
        ),
        block_weight: block_info.weight,
        long_term_weight: block_info.long_term_weight,
    })
}

#[cfg(test)]
mod tests {
    use std::num::NonZero;

    use cuprate_database::{Env, EnvInner, TxRw};
    use cuprate_test_utils::data::{BLOCK_V16_TX0, BLOCK_V1_TX2, BLOCK_V9_TX3};
    use cuprate_types::{Chain, ChainId};

    use crate::{
        ops::{
            alt_block::{
                add_alt_block, flush_alt_blocks, get_alt_block,
                get_alt_block_extended_header_from_height, get_alt_block_hash,
                get_alt_chain_history_ranges,
            },
            block::{add_block, pop_block},
        },
        tables::{OpenTables, Tables},
        tests::{assert_all_tables_are_empty, map_verified_block_to_alt, tmp_concrete_env},
        types::AltBlockHeight,
    };

    #[expect(clippy::range_plus_one)]
    #[test]
    fn all_alt_blocks() {
        let (env, _tmp) = tmp_concrete_env();
        let env_inner = env.env_inner();
        assert_all_tables_are_empty(&env);

        let chain_id = ChainId(NonZero::new(1).unwrap());

        // Add initial block.
        {
            let tx_rw = env_inner.tx_rw().unwrap();
            let mut tables = env_inner.open_tables_mut(&tx_rw).unwrap();

            let mut initial_block = BLOCK_V1_TX2.clone();
            initial_block.height = 0;

            add_block(&initial_block, &mut tables).unwrap();

            drop(tables);
            TxRw::commit(tx_rw).unwrap();
        }

        let alt_blocks = [
            map_verified_block_to_alt(BLOCK_V9_TX3.clone(), chain_id),
            map_verified_block_to_alt(BLOCK_V16_TX0.clone(), chain_id),
        ];

        // Add alt-blocks
        {
            let tx_rw = env_inner.tx_rw().unwrap();
            let mut tables = env_inner.open_tables_mut(&tx_rw).unwrap();

            let mut prev_hash = BLOCK_V1_TX2.block_hash;
            for (i, mut alt_block) in alt_blocks.into_iter().enumerate() {
                let height = i + 1;

                alt_block.height = height;
                alt_block.block.header.previous = prev_hash;
                alt_block.block_blob = alt_block.block.serialize();

                add_alt_block(&alt_block, &mut tables).unwrap();

                let alt_height = AltBlockHeight {
                    chain_id: chain_id.into(),
                    height,
                };

                let alt_block_2 = get_alt_block(&alt_height, &tables).unwrap();
                assert_eq!(alt_block.block, alt_block_2.block);

                let headers = get_alt_chain_history_ranges(
                    0..(height + 1),
                    chain_id,
                    tables.alt_chain_infos(),
                )
                .unwrap();

                assert_eq!(headers.len(), 2);
                assert_eq!(headers[1], (Chain::Main, 0..1));
                assert_eq!(headers[0], (Chain::Alt(chain_id), 1..(height + 1)));

                prev_hash = alt_block.block_hash;

                let header =
                    get_alt_block_extended_header_from_height(&alt_height, &tables).unwrap();

                assert_eq!(header.timestamp, alt_block.block.header.timestamp);
                assert_eq!(header.block_weight, alt_block.weight);
                assert_eq!(header.long_term_weight, alt_block.long_term_weight);
                assert_eq!(
                    header.cumulative_difficulty,
                    alt_block.cumulative_difficulty
                );
                assert_eq!(
                    header.version.as_u8(),
                    alt_block.block.header.hardfork_version
                );
                assert_eq!(header.vote, alt_block.block.header.hardfork_signal);

                let block_hash = get_alt_block_hash(&height, chain_id, &tables).unwrap();

                assert_eq!(block_hash, alt_block.block_hash);
            }

            drop(tables);
            TxRw::commit(tx_rw).unwrap();
        }

        {
            let mut tx_rw = env_inner.tx_rw().unwrap();

            flush_alt_blocks(&env_inner, &mut tx_rw).unwrap();

            let mut tables = env_inner.open_tables_mut(&tx_rw).unwrap();
            pop_block(None, &mut tables).unwrap();

            drop(tables);
            TxRw::commit(tx_rw).unwrap();
        }

        assert_all_tables_are_empty(&env);
    }
}