cuprate_consensus/block/
alt_block.rs

1//! Alt Blocks
2//!
3//! Alt blocks are sanity checked by [`sanity_check_alt_block`], that function will also compute the cumulative
4//! difficulty of the alt chain so callers will know if they should re-org to the alt chain.
5use std::{collections::HashMap, sync::Arc};
6
7use monero_serai::{block::Block, transaction::Input};
8use tower::{Service, ServiceExt};
9
10use cuprate_consensus_context::{
11    difficulty::DifficultyCache,
12    rx_vms::RandomXVm,
13    weight::{self, BlockWeightsCache},
14    AltChainContextCache, AltChainRequestToken, BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW,
15};
16use cuprate_consensus_rules::{
17    blocks::{
18        check_block_pow, check_block_weight, check_timestamp, randomx_seed_height, BlockError,
19    },
20    miner_tx::MinerTxError,
21    ConsensusError,
22};
23use cuprate_helper::{asynch::rayon_spawn_async, cast::u64_to_usize};
24use cuprate_types::{
25    AltBlockInformation, Chain, ChainId, TransactionVerificationData,
26    VerifiedTransactionInformation,
27};
28
29use crate::{
30    block::{free::pull_ordered_transactions, PreparedBlock},
31    BlockChainContextRequest, BlockChainContextResponse, ExtendedConsensusError,
32};
33
34/// This function sanity checks an alt-block.
35///
36/// Returns [`AltBlockInformation`], which contains the cumulative difficulty of the alt chain.
37///
38/// This function only checks the block's proof-of-work and its weight.
39pub async fn sanity_check_alt_block<C>(
40    block: Block,
41    txs: HashMap<[u8; 32], TransactionVerificationData>,
42    mut context_svc: C,
43) -> Result<AltBlockInformation, ExtendedConsensusError>
44where
45    C: Service<
46            BlockChainContextRequest,
47            Response = BlockChainContextResponse,
48            Error = tower::BoxError,
49        > + Send
50        + 'static,
51    C::Future: Send + 'static,
52{
53    // Fetch the alt-chains context cache.
54    let BlockChainContextResponse::AltChainContextCache(mut alt_context_cache) = context_svc
55        .ready()
56        .await?
57        .call(BlockChainContextRequest::AltChainContextCache {
58            prev_id: block.header.previous,
59            _token: AltChainRequestToken,
60        })
61        .await?
62    else {
63        panic!("Context service returned wrong response!");
64    };
65
66    // Check if the block's miner input is formed correctly.
67    let [Input::Gen(height)] = &block.miner_transaction.prefix().inputs[..] else {
68        return Err(ConsensusError::Block(BlockError::MinerTxError(
69            MinerTxError::InputNotOfTypeGen,
70        ))
71        .into());
72    };
73
74    if *height != alt_context_cache.chain_height {
75        return Err(ConsensusError::Block(BlockError::MinerTxError(
76            MinerTxError::InputsHeightIncorrect,
77        ))
78        .into());
79    }
80
81    // prep the alt block.
82    let prepped_block = {
83        let rx_vm = alt_rx_vm(
84            alt_context_cache.chain_height,
85            block.header.hardfork_version,
86            alt_context_cache.parent_chain,
87            &mut alt_context_cache,
88            &mut context_svc,
89        )
90        .await?;
91
92        rayon_spawn_async(move || PreparedBlock::new(block, rx_vm.as_deref())).await?
93    };
94
95    // get the difficulty cache for this alt chain.
96    let difficulty_cache = alt_difficulty_cache(
97        prepped_block.block.header.previous,
98        &mut alt_context_cache,
99        &mut context_svc,
100    )
101    .await?;
102
103    // Check the alt block timestamp is in the correct range.
104    if let Some(median_timestamp) =
105        difficulty_cache.median_timestamp(u64_to_usize(BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW))
106    {
107        check_timestamp(&prepped_block.block, median_timestamp).map_err(ConsensusError::Block)?;
108    };
109
110    let next_difficulty = difficulty_cache.next_difficulty(prepped_block.hf_version);
111    // make sure the block's PoW is valid for this difficulty.
112    check_block_pow(&prepped_block.pow_hash, next_difficulty).map_err(ConsensusError::Block)?;
113
114    let cumulative_difficulty = difficulty_cache.cumulative_difficulty() + next_difficulty;
115
116    let ordered_txs = pull_ordered_transactions(&prepped_block.block, txs)?;
117
118    let block_weight =
119        prepped_block.miner_tx_weight + ordered_txs.iter().map(|tx| tx.tx_weight).sum::<usize>();
120
121    let alt_weight_cache = alt_weight_cache(
122        prepped_block.block.header.previous,
123        &mut alt_context_cache,
124        &mut context_svc,
125    )
126    .await?;
127
128    // Check the block weight is below the limit.
129    check_block_weight(
130        block_weight,
131        alt_weight_cache.median_for_block_reward(prepped_block.hf_version),
132    )
133    .map_err(ConsensusError::Block)?;
134
135    let long_term_weight = weight::calculate_block_long_term_weight(
136        prepped_block.hf_version,
137        block_weight,
138        alt_weight_cache.median_long_term_weight(),
139    );
140
141    // Get the chainID or generate a new one if this is the first alt block in this alt chain.
142    let chain_id = *alt_context_cache
143        .chain_id
144        .get_or_insert_with(|| ChainId(rand::random()));
145
146    // Create the alt block info.
147    let block_info = AltBlockInformation {
148        block_hash: prepped_block.block_hash,
149        block: prepped_block.block,
150        block_blob: prepped_block.block_blob,
151        txs: ordered_txs
152            .into_iter()
153            .map(|tx| VerifiedTransactionInformation {
154                tx_blob: tx.tx_blob,
155                tx_weight: tx.tx_weight,
156                fee: tx.fee,
157                tx_hash: tx.tx_hash,
158                tx: tx.tx,
159            })
160            .collect(),
161        pow_hash: prepped_block.pow_hash,
162        weight: block_weight,
163        height: alt_context_cache.chain_height,
164        long_term_weight,
165        cumulative_difficulty,
166        chain_id,
167    };
168
169    // Add this block to the cache.
170    alt_context_cache.add_new_block(
171        block_info.height,
172        block_info.block_hash,
173        block_info.weight,
174        block_info.long_term_weight,
175        block_info.block.header.timestamp,
176        cumulative_difficulty,
177    );
178
179    // Add this alt cache back to the context service.
180    context_svc
181        .oneshot(BlockChainContextRequest::AddAltChainContextCache {
182            cache: alt_context_cache,
183            _token: AltChainRequestToken,
184        })
185        .await?;
186
187    Ok(block_info)
188}
189
190/// Retrieves the alt RX VM for the chosen block height.
191///
192/// If the `hf` is less than 12 (the height RX activates), then [`None`] is returned.
193async fn alt_rx_vm<C>(
194    block_height: usize,
195    hf: u8,
196    parent_chain: Chain,
197    alt_chain_context: &mut AltChainContextCache,
198    context_svc: C,
199) -> Result<Option<Arc<RandomXVm>>, ExtendedConsensusError>
200where
201    C: Service<
202            BlockChainContextRequest,
203            Response = BlockChainContextResponse,
204            Error = tower::BoxError,
205        > + Send,
206    C::Future: Send + 'static,
207{
208    if hf < 12 {
209        return Ok(None);
210    }
211
212    let seed_height = randomx_seed_height(block_height);
213
214    let cached_vm = match alt_chain_context.cached_rx_vm.take() {
215        // If the VM is cached and the height is the height we need, we can use this VM.
216        Some((cached_seed_height, vm)) if seed_height == cached_seed_height => {
217            (cached_seed_height, vm)
218        }
219        // Otherwise we need to make a new VM.
220        _ => {
221            let BlockChainContextResponse::AltChainRxVM(vm) = context_svc
222                .oneshot(BlockChainContextRequest::AltChainRxVM {
223                    height: block_height,
224                    chain: parent_chain,
225                    _token: AltChainRequestToken,
226                })
227                .await?
228            else {
229                panic!("Context service returned wrong response!");
230            };
231
232            (seed_height, vm)
233        }
234    };
235
236    Ok(Some(Arc::clone(
237        &alt_chain_context.cached_rx_vm.insert(cached_vm).1,
238    )))
239}
240
241/// Returns the [`DifficultyCache`] for the alt chain.
242async fn alt_difficulty_cache<C>(
243    prev_id: [u8; 32],
244    alt_chain_context: &mut AltChainContextCache,
245    context_svc: C,
246) -> Result<&mut DifficultyCache, ExtendedConsensusError>
247where
248    C: Service<
249            BlockChainContextRequest,
250            Response = BlockChainContextResponse,
251            Error = tower::BoxError,
252        > + Send,
253    C::Future: Send + 'static,
254{
255    // First look to see if the difficulty cache for this alt chain is already cached.
256    match &mut alt_chain_context.difficulty_cache {
257        Some(cache) => Ok(cache),
258        // Otherwise make a new one.
259        difficulty_cache => {
260            let BlockChainContextResponse::AltChainDifficultyCache(cache) = context_svc
261                .oneshot(BlockChainContextRequest::AltChainDifficultyCache {
262                    prev_id,
263                    _token: AltChainRequestToken,
264                })
265                .await?
266            else {
267                panic!("Context service returned wrong response!");
268            };
269
270            Ok(difficulty_cache.insert(cache))
271        }
272    }
273}
274
275/// Returns the [`BlockWeightsCache`] for the alt chain.
276async fn alt_weight_cache<C>(
277    prev_id: [u8; 32],
278    alt_chain_context: &mut AltChainContextCache,
279    context_svc: C,
280) -> Result<&mut BlockWeightsCache, ExtendedConsensusError>
281where
282    C: Service<
283            BlockChainContextRequest,
284            Response = BlockChainContextResponse,
285            Error = tower::BoxError,
286        > + Send,
287    C::Future: Send + 'static,
288{
289    // First look to see if the weight cache for this alt chain is already cached.
290    match &mut alt_chain_context.weight_cache {
291        Some(cache) => Ok(cache),
292        // Otherwise make a new one.
293        weight_cache => {
294            let BlockChainContextResponse::AltChainWeightCache(cache) = context_svc
295                .oneshot(BlockChainContextRequest::AltChainWeightCache {
296                    prev_id,
297                    _token: AltChainRequestToken,
298                })
299                .await?
300            else {
301                panic!("Context service returned wrong response!");
302            };
303
304            Ok(weight_cache.insert(cache))
305        }
306    }
307}