cuprate_consensus/block/
alt_block.rs

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
//! Alt Blocks
//!
//! Alt blocks are sanity checked by [`sanity_check_alt_block`], that function will also compute the cumulative
//! difficulty of the alt chain so callers will know if they should re-org to the alt chain.
use std::{collections::HashMap, sync::Arc};

use monero_serai::{block::Block, transaction::Input};
use tower::{Service, ServiceExt};

use cuprate_consensus_context::{
    difficulty::DifficultyCache,
    rx_vms::RandomXVm,
    weight::{self, BlockWeightsCache},
    AltChainContextCache, AltChainRequestToken, BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW,
};
use cuprate_consensus_rules::{
    blocks::{
        check_block_pow, check_block_weight, check_timestamp, randomx_seed_height, BlockError,
    },
    miner_tx::MinerTxError,
    ConsensusError,
};
use cuprate_helper::{asynch::rayon_spawn_async, cast::u64_to_usize};
use cuprate_types::{
    AltBlockInformation, Chain, ChainId, TransactionVerificationData,
    VerifiedTransactionInformation,
};

use crate::{
    block::{free::pull_ordered_transactions, PreparedBlock},
    BlockChainContextRequest, BlockChainContextResponse, ExtendedConsensusError,
};

/// This function sanity checks an alt-block.
///
/// Returns [`AltBlockInformation`], which contains the cumulative difficulty of the alt chain.
///
/// This function only checks the block's proof-of-work and its weight.
pub async fn sanity_check_alt_block<C>(
    block: Block,
    txs: HashMap<[u8; 32], TransactionVerificationData>,
    mut context_svc: C,
) -> Result<AltBlockInformation, ExtendedConsensusError>
where
    C: Service<
            BlockChainContextRequest,
            Response = BlockChainContextResponse,
            Error = tower::BoxError,
        > + Send
        + 'static,
    C::Future: Send + 'static,
{
    // Fetch the alt-chains context cache.
    let BlockChainContextResponse::AltChainContextCache(mut alt_context_cache) = context_svc
        .ready()
        .await?
        .call(BlockChainContextRequest::AltChainContextCache {
            prev_id: block.header.previous,
            _token: AltChainRequestToken,
        })
        .await?
    else {
        panic!("Context service returned wrong response!");
    };

    // Check if the block's miner input is formed correctly.
    let [Input::Gen(height)] = &block.miner_transaction.prefix().inputs[..] else {
        return Err(ConsensusError::Block(BlockError::MinerTxError(
            MinerTxError::InputNotOfTypeGen,
        ))
        .into());
    };

    if *height != alt_context_cache.chain_height {
        return Err(ConsensusError::Block(BlockError::MinerTxError(
            MinerTxError::InputsHeightIncorrect,
        ))
        .into());
    }

    // prep the alt block.
    let prepped_block = {
        let rx_vm = alt_rx_vm(
            alt_context_cache.chain_height,
            block.header.hardfork_version,
            alt_context_cache.parent_chain,
            &mut alt_context_cache,
            &mut context_svc,
        )
        .await?;

        rayon_spawn_async(move || PreparedBlock::new(block, rx_vm.as_deref())).await?
    };

    // get the difficulty cache for this alt chain.
    let difficulty_cache = alt_difficulty_cache(
        prepped_block.block.header.previous,
        &mut alt_context_cache,
        &mut context_svc,
    )
    .await?;

    // Check the alt block timestamp is in the correct range.
    if let Some(median_timestamp) =
        difficulty_cache.median_timestamp(u64_to_usize(BLOCKCHAIN_TIMESTAMP_CHECK_WINDOW))
    {
        check_timestamp(&prepped_block.block, median_timestamp).map_err(ConsensusError::Block)?;
    };

    let next_difficulty = difficulty_cache.next_difficulty(prepped_block.hf_version);
    // make sure the block's PoW is valid for this difficulty.
    check_block_pow(&prepped_block.pow_hash, next_difficulty).map_err(ConsensusError::Block)?;

    let cumulative_difficulty = difficulty_cache.cumulative_difficulty() + next_difficulty;

    let ordered_txs = pull_ordered_transactions(&prepped_block.block, txs)?;

    let block_weight =
        prepped_block.miner_tx_weight + ordered_txs.iter().map(|tx| tx.tx_weight).sum::<usize>();

    let alt_weight_cache = alt_weight_cache(
        prepped_block.block.header.previous,
        &mut alt_context_cache,
        &mut context_svc,
    )
    .await?;

    // Check the block weight is below the limit.
    check_block_weight(
        block_weight,
        alt_weight_cache.median_for_block_reward(prepped_block.hf_version),
    )
    .map_err(ConsensusError::Block)?;

    let long_term_weight = weight::calculate_block_long_term_weight(
        prepped_block.hf_version,
        block_weight,
        alt_weight_cache.median_long_term_weight(),
    );

    // Get the chainID or generate a new one if this is the first alt block in this alt chain.
    let chain_id = *alt_context_cache
        .chain_id
        .get_or_insert_with(|| ChainId(rand::random()));

    // Create the alt block info.
    let block_info = AltBlockInformation {
        block_hash: prepped_block.block_hash,
        block: prepped_block.block,
        block_blob: prepped_block.block_blob,
        txs: ordered_txs
            .into_iter()
            .map(|tx| VerifiedTransactionInformation {
                tx_blob: tx.tx_blob,
                tx_weight: tx.tx_weight,
                fee: tx.fee,
                tx_hash: tx.tx_hash,
                tx: tx.tx,
            })
            .collect(),
        pow_hash: prepped_block.pow_hash,
        weight: block_weight,
        height: alt_context_cache.chain_height,
        long_term_weight,
        cumulative_difficulty,
        chain_id,
    };

    // Add this block to the cache.
    alt_context_cache.add_new_block(
        block_info.height,
        block_info.block_hash,
        block_info.weight,
        block_info.long_term_weight,
        block_info.block.header.timestamp,
    );

    // Add this alt cache back to the context service.
    context_svc
        .oneshot(BlockChainContextRequest::AddAltChainContextCache {
            prev_id: block_info.block.header.previous,
            cache: alt_context_cache,
            _token: AltChainRequestToken,
        })
        .await?;

    Ok(block_info)
}

/// Retrieves the alt RX VM for the chosen block height.
///
/// If the `hf` is less than 12 (the height RX activates), then [`None`] is returned.
async fn alt_rx_vm<C>(
    block_height: usize,
    hf: u8,
    parent_chain: Chain,
    alt_chain_context: &mut AltChainContextCache,
    context_svc: C,
) -> Result<Option<Arc<RandomXVm>>, ExtendedConsensusError>
where
    C: Service<
            BlockChainContextRequest,
            Response = BlockChainContextResponse,
            Error = tower::BoxError,
        > + Send,
    C::Future: Send + 'static,
{
    if hf < 12 {
        return Ok(None);
    }

    let seed_height = randomx_seed_height(block_height);

    let cached_vm = match alt_chain_context.cached_rx_vm.take() {
        // If the VM is cached and the height is the height we need, we can use this VM.
        Some((cached_seed_height, vm)) if seed_height == cached_seed_height => {
            (cached_seed_height, vm)
        }
        // Otherwise we need to make a new VM.
        _ => {
            let BlockChainContextResponse::AltChainRxVM(vm) = context_svc
                .oneshot(BlockChainContextRequest::AltChainRxVM {
                    height: block_height,
                    chain: parent_chain,
                    _token: AltChainRequestToken,
                })
                .await?
            else {
                panic!("Context service returned wrong response!");
            };

            (seed_height, vm)
        }
    };

    Ok(Some(Arc::clone(
        &alt_chain_context.cached_rx_vm.insert(cached_vm).1,
    )))
}

/// Returns the [`DifficultyCache`] for the alt chain.
async fn alt_difficulty_cache<C>(
    prev_id: [u8; 32],
    alt_chain_context: &mut AltChainContextCache,
    context_svc: C,
) -> Result<&mut DifficultyCache, ExtendedConsensusError>
where
    C: Service<
            BlockChainContextRequest,
            Response = BlockChainContextResponse,
            Error = tower::BoxError,
        > + Send,
    C::Future: Send + 'static,
{
    // First look to see if the difficulty cache for this alt chain is already cached.
    match &mut alt_chain_context.difficulty_cache {
        Some(cache) => Ok(cache),
        // Otherwise make a new one.
        difficulty_cache => {
            let BlockChainContextResponse::AltChainDifficultyCache(cache) = context_svc
                .oneshot(BlockChainContextRequest::AltChainDifficultyCache {
                    prev_id,
                    _token: AltChainRequestToken,
                })
                .await?
            else {
                panic!("Context service returned wrong response!");
            };

            Ok(difficulty_cache.insert(cache))
        }
    }
}

/// Returns the [`BlockWeightsCache`] for the alt chain.
async fn alt_weight_cache<C>(
    prev_id: [u8; 32],
    alt_chain_context: &mut AltChainContextCache,
    context_svc: C,
) -> Result<&mut BlockWeightsCache, ExtendedConsensusError>
where
    C: Service<
            BlockChainContextRequest,
            Response = BlockChainContextResponse,
            Error = tower::BoxError,
        > + Send,
    C::Future: Send + 'static,
{
    // First look to see if the weight cache for this alt chain is already cached.
    match &mut alt_chain_context.weight_cache {
        Some(cache) => Ok(cache),
        // Otherwise make a new one.
        weight_cache => {
            let BlockChainContextResponse::AltChainWeightCache(cache) = context_svc
                .oneshot(BlockChainContextRequest::AltChainWeightCache {
                    prev_id,
                    _token: AltChainRequestToken,
                })
                .await?
            else {
                panic!("Context service returned wrong response!");
            };

            Ok(weight_cache.insert(cache))
        }
    }
}