cuprate_consensus/
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//! Block Verification.
//!
//! This module contains functions for verifying blocks:
//! - [`verify_main_chain_block`]
//! - [`batch_prepare_main_chain_blocks`]
//! - [`verify_prepped_main_chain_block`]
//! - [`sanity_check_alt_block`]
//!
use std::{collections::HashMap, mem};

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

use cuprate_consensus_context::{
    BlockChainContextRequest, BlockChainContextResponse, BlockchainContextService,
};
use cuprate_helper::asynch::rayon_spawn_async;
use cuprate_types::{
    AltBlockInformation, TransactionVerificationData, VerifiedBlockInformation,
    VerifiedTransactionInformation,
};

use cuprate_consensus_rules::{
    blocks::{
        calculate_pow_hash, check_block, check_block_pow, randomx_seed_height, BlockError, RandomX,
    },
    hard_forks::HardForkError,
    miner_tx::MinerTxError,
    ConsensusError, HardFork,
};

use crate::{transactions::start_tx_verification, Database, ExtendedConsensusError};

mod alt_block;
mod batch_prepare;
mod free;

pub use alt_block::sanity_check_alt_block;
pub use batch_prepare::batch_prepare_main_chain_blocks;
use free::pull_ordered_transactions;

/// A pre-prepared block with all data needed to verify it, except the block's proof of work.
#[derive(Debug)]
pub struct PreparedBlockExPow {
    /// The block.
    pub block: Block,
    /// The serialised block's bytes.
    pub block_blob: Vec<u8>,

    /// The block's hard-fork vote.
    pub hf_vote: HardFork,
    /// The block's hard-fork version.
    pub hf_version: HardFork,

    /// The block's hash.
    pub block_hash: [u8; 32],
    /// The height of the block.
    pub height: usize,

    /// The weight of the block's miner transaction.
    pub miner_tx_weight: usize,
}

impl PreparedBlockExPow {
    /// Prepare a new block.
    ///
    /// # Errors
    /// This errors if either the `block`'s:
    /// - Hard-fork values are invalid
    /// - Miner transaction is missing a miner input
    pub fn new(block: Block) -> Result<Self, ConsensusError> {
        let (hf_version, hf_vote) = HardFork::from_block_header(&block.header)
            .map_err(|_| BlockError::HardForkError(HardForkError::HardForkUnknown))?;

        let Some(Input::Gen(height)) = block.miner_transaction.prefix().inputs.first() else {
            return Err(ConsensusError::Block(BlockError::MinerTxError(
                MinerTxError::InputNotOfTypeGen,
            )));
        };

        Ok(Self {
            block_blob: block.serialize(),
            hf_vote,
            hf_version,

            block_hash: block.hash(),
            height: *height,

            miner_tx_weight: block.miner_transaction.weight(),
            block,
        })
    }
}

/// A pre-prepared block with all data needed to verify it.
#[derive(Debug)]
pub struct PreparedBlock {
    /// The block
    pub block: Block,
    /// The serialised blocks bytes
    pub block_blob: Vec<u8>,

    /// The blocks hf vote
    pub hf_vote: HardFork,
    /// The blocks hf version
    pub hf_version: HardFork,

    /// The blocks hash
    pub block_hash: [u8; 32],
    /// The blocks POW hash.
    pub pow_hash: [u8; 32],

    /// The weight of the blocks miner transaction.
    pub miner_tx_weight: usize,
}

impl PreparedBlock {
    /// Creates a new [`PreparedBlock`].
    ///
    /// The randomX VM must be Some if RX is needed or this will panic.
    /// The randomX VM must also be initialised with the correct seed.
    pub fn new<R: RandomX>(block: Block, randomx_vm: Option<&R>) -> Result<Self, ConsensusError> {
        let (hf_version, hf_vote) = HardFork::from_block_header(&block.header)
            .map_err(|_| BlockError::HardForkError(HardForkError::HardForkUnknown))?;

        let [Input::Gen(height)] = &block.miner_transaction.prefix().inputs[..] else {
            return Err(ConsensusError::Block(BlockError::MinerTxError(
                MinerTxError::InputNotOfTypeGen,
            )));
        };

        Ok(Self {
            block_blob: block.serialize(),
            hf_vote,
            hf_version,

            block_hash: block.hash(),
            pow_hash: calculate_pow_hash(
                randomx_vm,
                &block.serialize_pow_hash(),
                *height,
                &hf_version,
            )?,

            miner_tx_weight: block.miner_transaction.weight(),
            block,
        })
    }

    /// Creates a new [`PreparedBlock`] from a [`PreparedBlockExPow`].
    ///
    /// This function will give an invalid proof-of-work hash if `randomx_vm` is not initialised
    /// with the correct seed.
    ///
    /// # Panics
    /// This function will panic if `randomx_vm` is
    /// [`None`] even though `RandomX` is needed.
    fn new_prepped<R: RandomX>(
        block: PreparedBlockExPow,
        randomx_vm: Option<&R>,
    ) -> Result<Self, ConsensusError> {
        Ok(Self {
            block_blob: block.block_blob,
            hf_vote: block.hf_vote,
            hf_version: block.hf_version,

            block_hash: block.block_hash,
            pow_hash: calculate_pow_hash(
                randomx_vm,
                &block.block.serialize_pow_hash(),
                block.height,
                &block.hf_version,
            )?,

            miner_tx_weight: block.block.miner_transaction.weight(),
            block: block.block,
        })
    }

    /// Creates a new [`PreparedBlock`] from an [`AltBlockInformation`].
    pub fn new_alt_block(block: AltBlockInformation) -> Result<Self, ConsensusError> {
        Ok(Self {
            block_blob: block.block_blob,
            hf_vote: HardFork::from_version(block.block.header.hardfork_version)
                .map_err(|_| BlockError::HardForkError(HardForkError::HardForkUnknown))?,
            hf_version: HardFork::from_vote(block.block.header.hardfork_signal),
            block_hash: block.block_hash,
            pow_hash: block.pow_hash,
            miner_tx_weight: block.block.miner_transaction.weight(),
            block: block.block,
        })
    }
}

/// Fully verify a block and all its transactions.
pub async fn verify_main_chain_block<D>(
    block: Block,
    txs: HashMap<[u8; 32], TransactionVerificationData>,
    context_svc: &mut BlockchainContextService,
    database: D,
) -> Result<VerifiedBlockInformation, ExtendedConsensusError>
where
    D: Database + Clone + Send + 'static,
{
    let context = context_svc.blockchain_context().clone();
    tracing::debug!("got blockchain context: {:?}", context);

    tracing::debug!(
        "Preparing block for verification, expected height: {}",
        context.chain_height
    );

    // Set up the block and just pass it to [`verify_prepped_main_chain_block`]

    // We just use the raw `hardfork_version` here, no need to turn it into a `HardFork`.
    let rx_vms = if block.header.hardfork_version < 12 {
        HashMap::new()
    } else {
        let BlockChainContextResponse::RxVms(rx_vms) = context_svc
            .ready()
            .await?
            .call(BlockChainContextRequest::CurrentRxVms)
            .await?
        else {
            panic!("Blockchain context service returned wrong response!");
        };

        rx_vms
    };

    let height = context.chain_height;
    let prepped_block = rayon_spawn_async(move || {
        PreparedBlock::new(
            block,
            rx_vms.get(&randomx_seed_height(height)).map(AsRef::as_ref),
        )
    })
    .await?;

    check_block_pow(&prepped_block.pow_hash, context.next_difficulty)
        .map_err(ConsensusError::Block)?;

    // Check that the txs included are what we need and that there are not any extra.
    let ordered_txs = pull_ordered_transactions(&prepped_block.block, txs)?;

    verify_prepped_main_chain_block(prepped_block, ordered_txs, context_svc, database).await
}

/// Fully verify a block that has already been prepared using [`batch_prepare_main_chain_blocks`].
pub async fn verify_prepped_main_chain_block<D>(
    prepped_block: PreparedBlock,
    mut txs: Vec<TransactionVerificationData>,
    context_svc: &mut BlockchainContextService,
    database: D,
) -> Result<VerifiedBlockInformation, ExtendedConsensusError>
where
    D: Database + Clone + Send + 'static,
{
    let context = context_svc.blockchain_context();

    tracing::debug!("verifying block: {}", hex::encode(prepped_block.block_hash));

    check_block_pow(&prepped_block.pow_hash, context.next_difficulty)
        .map_err(ConsensusError::Block)?;

    if prepped_block.block.transactions.len() != txs.len() {
        return Err(ExtendedConsensusError::TxsIncludedWithBlockIncorrect);
    }

    if !prepped_block.block.transactions.is_empty() {
        for (expected_tx_hash, tx) in prepped_block.block.transactions.iter().zip(txs.iter()) {
            if expected_tx_hash != &tx.tx_hash {
                return Err(ExtendedConsensusError::TxsIncludedWithBlockIncorrect);
            }
        }

        let temp = start_tx_verification()
            .append_prepped_txs(mem::take(&mut txs))
            .prepare()?
            .full(
                context.chain_height,
                context.top_hash,
                context.current_adjusted_timestamp_for_time_lock(),
                context.current_hf,
                database,
            )
            .verify()
            .await?;

        txs = temp;
    }

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

    tracing::debug!("Verifying block header.");
    let (_, generated_coins) = check_block(
        &prepped_block.block,
        total_fees,
        block_weight,
        prepped_block.block_blob.len(),
        &context.context_to_verify_block,
    )
    .map_err(ConsensusError::Block)?;

    Ok(VerifiedBlockInformation {
        block_hash: prepped_block.block_hash,
        block: prepped_block.block,
        block_blob: prepped_block.block_blob,
        txs: 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,
        generated_coins,
        weight: block_weight,
        height: context.chain_height,
        long_term_weight: context.next_block_long_term_weight(block_weight),
        cumulative_difficulty: context.cumulative_difficulty + context.next_difficulty,
    })
}