zebra-consensus 17.0.0

Implementation of Zcash consensus checks
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! Consensus-based block verification.
//!
//! In contrast to checkpoint verification, which only checks hardcoded
//! hashes, block verification checks all Zcash consensus rules.
//!
//! The block verifier performs all of the semantic validation checks.
//! If accepted, the block is sent to the state service for contextual
//! verification, where it may be accepted or rejected.

use std::{
    future::Future,
    pin::Pin,
    sync::Arc,
    task::{Context, Poll},
};

use chrono::Utc;
use futures::stream::FuturesUnordered;
use futures_util::FutureExt;
use thiserror::Error;
use tower::{Service, ServiceExt};
use tracing::Instrument;

use zebra_chain::{
    amount::Amount,
    block,
    parameters::{subsidy::SubsidyError, Network},
    transparent,
    work::equihash,
};
use zebra_state as zs;

use crate::{error::*, transaction as tx, BoxError};

pub mod check;
pub mod request;

pub use request::Request;

#[cfg(test)]
mod tests;

/// Asynchronous semantic block verification.
#[derive(Debug)]
pub struct SemanticBlockVerifier<S, V> {
    /// The network to be verified.
    network: Network,
    state_service: S,
    transaction_verifier: V,
}

/// Block verification errors.
// TODO: dedupe with crate::error::BlockError
#[non_exhaustive]
#[allow(missing_docs)]
#[derive(Debug, Error)]
pub enum VerifyBlockError {
    #[error("unable to verify depth for block {hash} from chain state during block verification")]
    Depth { source: BoxError, hash: block::Hash },

    #[error(transparent)]
    Block {
        #[from]
        source: BlockError,
    },

    #[error(transparent)]
    Equihash {
        #[from]
        source: equihash::Error,
    },

    #[error(transparent)]
    Time(zebra_chain::block::BlockTimeError),

    /// Error when attempting to commit a block after semantic verification.
    #[error("unable to commit block after semantic verification: {0}")]
    Commit(#[from] zs::CommitBlockError),

    #[error("unable to validate block proposal: failed semantic verification (proof of work is not checked for proposals): {0}")]
    // TODO: make this into a concrete type (see #5732)
    ValidateProposal(#[source] BoxError),

    #[error("invalid transaction: {0}")]
    Transaction(#[from] TransactionError),

    #[error("invalid block subsidy: {0}")]
    Subsidy(#[from] SubsidyError),

    /// Errors originating from the state service, which may arise from general failures in interacting with the state.
    /// This is for errors that are not specifically related to block depth or commit failures.
    #[error("state service error for block {hash}: {source}")]
    StateService { source: BoxError, hash: block::Hash },
}

impl VerifyBlockError {
    /// Returns `true` if this is definitely a duplicate request.
    /// Some duplicate requests might not be detected, and therefore return `false`.
    pub fn is_duplicate_request(&self) -> bool {
        match self {
            VerifyBlockError::Block { source, .. } => source.is_duplicate_request(),
            VerifyBlockError::Commit(commit_err) => commit_err.is_duplicate_request(),
            _ => false,
        }
    }

    /// Returns `true` if the block's authorizing data doesn't match the commitment in
    /// its header.
    ///
    /// See [`zs::ValidateContextError::is_auth_commitment_mismatch()`].
    pub fn is_auth_commitment_mismatch(&self) -> bool {
        match self {
            VerifyBlockError::Commit(commit_err) => commit_err.is_auth_commitment_mismatch(),
            _ => false,
        }
    }

    /// Returns `true` if the block was rejected only because an ancestor's authorizing
    /// data didn't match the commitment in its header.
    ///
    /// See [`zs::ValidateContextError::is_descendant_of_auth_commitment_mismatch()`].
    pub fn is_descendant_of_auth_commitment_mismatch(&self) -> bool {
        match self {
            VerifyBlockError::Commit(commit_err) => {
                commit_err.is_descendant_of_auth_commitment_mismatch()
            }
            _ => false,
        }
    }

    /// Returns a suggested misbehaviour score increment for a certain error.
    pub fn misbehavior_score(&self) -> u32 {
        use VerifyBlockError::*;
        match self {
            Block { source } => source.misbehavior_score(),
            Equihash { .. } | Subsidy(_) => 100,
            Transaction(err) => err.mempool_misbehavior_score(),
            Commit(err) => err.misbehavior_score(),
            _other => 0,
        }
    }
}

/// Converts an error from a `CommitSemanticallyVerifiedBlock` state request
/// into a [`VerifyBlockError`].
///
/// The state boxes commit errors as [`zs::CommitSemanticallyVerifiedError`], a
/// newtype around [`zs::CommitBlockError`], so the wrapper must be unwrapped
/// here for `is_duplicate_request()` and `misbehavior_score()` to classify
/// duplicate blocks as benign.
fn map_commit_error(source: BoxError, hash: block::Hash) -> VerifyBlockError {
    if let Some(commit_err) = source
        .downcast_ref::<zs::CommitSemanticallyVerifiedError>()
        .map(zs::CommitSemanticallyVerifiedError::inner)
        .or_else(|| source.downcast_ref::<zs::CommitBlockError>())
    {
        return VerifyBlockError::Commit(commit_err.clone());
    }

    VerifyBlockError::StateService { source, hash }
}

/// The maximum number of transparent signature operations allowed in a block.
///
/// # Consensus
///
/// For every block, the sum of legacy and P2SH transparent signature operations across all
/// transactions must not exceed [20_000].
///
/// ## Notes
///
/// This rule is inherited from pre-SegWit Bitcoin, and is not explicitly stated in the Zcash
/// protocol spec. It is covered implicitly in [§7.6], which closes with "Other rules inherited from
/// Bitcoin". The inclusion of this rule is tracked in [`zcash/zips#568`].
///
/// Zebra mirrors `zcashd`'s `ConnectBlock`, which sums `GetLegacySigOpCount()` and
/// `GetP2SHSigOpCount()` per transaction before comparing against this constant.
///
/// [20_000]: <https://github.com/zcash/zcash/blob/bad7f7eadbbb3466bebe3354266c7f69f607fcfd/src/consensus/consensus.h#L30>
/// [`zcash/zips#568`]: <https://github.com/zcash/zips/issues/568>
/// [§7.6]: <https://zips.z.cash/protocol/protocol.pdf#blockheader>
pub const MAX_BLOCK_SIGOPS: u32 = 20_000;

/// The [ZIP 218] per-pool and global shielded action limits, which apply from NU7 activation.
///
/// `GlobalShieldedBudget`, `OrchardProtocolBlockActionLimit`, `SaplingBlockIOLimit` and
/// `SproutBlockJoinSplitLimit` in the ZIP. Orchard and Ironwood share the global budget and
/// have the same per-pool action limit. Sprout JoinSplits are prohibited.
///
/// [ZIP 218]: https://zips.z.cash/zip-0218
pub const GLOBAL_SHIELDED_BUDGET: usize = 330;
/// The per-pool action limit for both Orchard and Ironwood. See [`GLOBAL_SHIELDED_BUDGET`].
pub const ORCHARD_BLOCK_ACTION_LIMIT: usize = 330;
/// See [`GLOBAL_SHIELDED_BUDGET`].
pub const SAPLING_BLOCK_IO_LIMIT: usize = 300;
/// See [`GLOBAL_SHIELDED_BUDGET`].
pub const SPROUT_BLOCK_JOIN_SPLIT_LIMIT: usize = 0;

/// The shielded components that the [ZIP 218] action limits count.
///
/// Shared by the block verifier's limit check and by `getblocktemplate` transaction selection, so
/// that a template can not be built from transactions the verifier would then reject.
///
/// [ZIP 218]: https://zips.z.cash/zip-0218
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ShieldedActionCounts {
    /// The number of Orchard actions.
    pub orchard_actions: usize,
    /// The number of Ironwood actions.
    pub ironwood_actions: usize,
    /// The number of Sapling inputs plus outputs.
    pub sapling_io: usize,
    /// The number of Sprout JoinSplits.
    pub joinsplits: usize,
}

impl ShieldedActionCounts {
    /// Returns the counts for `transaction`.
    pub fn from_transaction(transaction: &zebra_chain::transaction::Transaction) -> Self {
        Self {
            orchard_actions: transaction.orchard_actions().count(),
            ironwood_actions: transaction.ironwood_actions().count(),
            sapling_io: transaction
                .sapling_spends_count()
                .saturating_add(transaction.sapling_outputs().count()),
            joinsplits: transaction.joinsplit_count(),
        }
    }

    /// Returns these counts plus `other`.
    ///
    /// Saturating, so that a malformed count can not wrap back below a limit.
    pub fn saturating_add(self, other: Self) -> Self {
        Self {
            orchard_actions: self.orchard_actions.saturating_add(other.orchard_actions),
            ironwood_actions: self.ironwood_actions.saturating_add(other.ironwood_actions),
            sapling_io: self.sapling_io.saturating_add(other.sapling_io),
            joinsplits: self.joinsplits.saturating_add(other.joinsplits),
        }
    }

    /// Returns the total shielded cost, which each JoinSplit contributes twice to, because it
    /// produces two shielded outputs.
    pub fn shielded_cost(&self) -> usize {
        self.orchard_actions
            .saturating_add(self.ironwood_actions)
            .saturating_add(self.sapling_io)
            .saturating_add(self.joinsplits.saturating_mul(2))
    }

    /// Returns the first limit these counts exceed, as `(pool, count, limit)`.
    pub fn exceeded_limit(&self) -> Option<(&'static str, usize, usize)> {
        [
            (
                "Orchard actions",
                self.orchard_actions,
                ORCHARD_BLOCK_ACTION_LIMIT,
            ),
            (
                "Ironwood actions",
                self.ironwood_actions,
                ORCHARD_BLOCK_ACTION_LIMIT,
            ),
            (
                "Sapling inputs and outputs",
                self.sapling_io,
                SAPLING_BLOCK_IO_LIMIT,
            ),
            (
                "Sprout JoinSplits",
                self.joinsplits,
                SPROUT_BLOCK_JOIN_SPLIT_LIMIT,
            ),
            (
                "shielded actions across all pools",
                self.shielded_cost(),
                GLOBAL_SHIELDED_BUDGET,
            ),
        ]
        .into_iter()
        .find(|&(_, count, limit)| count > limit)
    }
}

impl<S, V> SemanticBlockVerifier<S, V>
where
    S: Service<zs::Request, Response = zs::Response, Error = BoxError> + Send + Clone + 'static,
    S::Future: Send + 'static,
    V: Service<tx::BlockRequest, Response = tx::BlockResponse, Error = BoxError>
        + Send
        + Clone
        + 'static,
    V::Future: Send + 'static,
{
    /// Creates a new SemanticBlockVerifier
    pub fn new(network: &Network, state_service: S, transaction_verifier: V) -> Self {
        Self {
            network: network.clone(),
            state_service,
            transaction_verifier,
        }
    }
}

impl<S, V> Service<Request> for SemanticBlockVerifier<S, V>
where
    S: Service<zs::Request, Response = zs::Response, Error = BoxError> + Send + Clone + 'static,
    S::Future: Send + 'static,
    V: Service<tx::BlockRequest, Response = tx::BlockResponse, Error = BoxError>
        + Send
        + Clone
        + 'static,
    V::Future: Send + 'static,
{
    type Response = block::Hash;
    type Error = VerifyBlockError;
    type Future =
        Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;

    fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        // We use the state for contextual verification, and we expect those
        // queries to be fast. So we don't need to call
        // `state_service.poll_ready()` here.
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, request: Request) -> Self::Future {
        // Stamp the time the verifier received this block: the non-finalized state's
        // `Chain::cmp` uses it to prefer the first-received chain on equal-work ties.
        let received_time = std::time::Instant::now();

        let mut state_service = self.state_service.clone();
        let mut transaction_verifier = self.transaction_verifier.clone();
        let network = self.network.clone();

        let block = request.block();

        // We don't include the block hash, because it's likely already in a parent span
        let span = tracing::debug_span!("block", height = ?block.coinbase_height());

        async move {
            let hash = block.hash();
            // Check that this block is actually a new block.
            tracing::trace!("checking that block is not already in state");
            match state_service
                .ready()
                .await
                .map_err(|source| VerifyBlockError::Depth { source, hash })?
                .call(zs::Request::KnownBlock(hash))
                .await
                .map_err(|source| VerifyBlockError::Depth { source, hash })?
            {
                zs::Response::KnownBlock(Some(location)) => {
                    return Err(BlockError::AlreadyInChain(hash, location).into())
                }
                zs::Response::KnownBlock(None) => {}
                _ => unreachable!("wrong response to Request::KnownBlock"),
            }

            tracing::trace!("performing block checks");
            let height = block
                .coinbase_height()
                .ok_or(BlockError::MissingHeight(hash))?;

            // Zebra does not support heights greater than
            // [`block::Height::MAX`].
            if height > block::Height::MAX {
                Err(BlockError::MaxHeight(height, hash, block::Height::MAX))?;
            }

            // > The block data MUST be validated and checked against the server's usual
            // > acceptance rules (excluding the check for a valid proof-of-work).
            // <https://en.bitcoin.it/wiki/BIP_0023#Block_Proposal>
            if request.is_proposal() || network.disable_pow() {
                check::difficulty_threshold_is_valid(&block.header, &network, &height, &hash)?;
            } else {
                // Do the difficulty checks first, to raise the threshold for
                // attacks that use any other fields.
                check::difficulty_is_valid(&block.header, &network, &height, &hash)?;
                check::equihash_solution_is_valid(&block.header)?;
            }

            // Next, check the Merkle root validity, to ensure that
            // the header binds to the transactions in the blocks.

            // Precomputing this avoids duplicating transaction hash computations.
            let transaction_hashes: Arc<[_]> =
                block.transactions.iter().map(|t| t.hash()).collect();

            check::merkle_root_validity(&network, &block, &transaction_hashes)?;

            // Since errors cause an early exit, try to do the
            // quick checks first.

            // Quick field validity and structure checks
            let now = Utc::now();
            check::time_is_valid_at(&block.header, now, &height, &hash)
                .map_err(VerifyBlockError::Time)?;
            let coinbase_tx = check::coinbase_is_first(&block)?;

            // From the NSM reissuance height, the block subsidy depends on the
            // NSM value balance after the parent block, which is not committed yet while
            // blocks are verified concurrently. The state checks the subsidy, funding streams,
            // and miner fees during contextual validation instead, where the parent's chain value
            // pools are known.
            let nsm_reissuance_is_active =
                zebra_chain::parameters::subsidy::nsm_reissuance_is_active(height, &network);

            let expected_subsidy_and_deferred_pool_balance_change = if nsm_reissuance_is_active {
                None
            } else {
                let expected_block_subsidy =
                    zebra_chain::parameters::subsidy::block_subsidy(height, &network)?;

                // See [ZIP-1015](https://zips.z.cash/zip-1015).
                let deferred_pool_balance_change =
                    check::subsidy_is_valid(&block, &network, expected_block_subsidy)?;

                Some((expected_block_subsidy, deferred_pool_balance_change))
            };

            // Bound shielded work before output recovery or proof verification.
            check::shielded_action_limits_are_valid(&block, &network, height, hash)?;

            // Now do the slower checks

            // Check compatibility with ZIP-212 shielded Sapling and Orchard coinbase output decryption
            tx::check::coinbase_outputs_are_decryptable(&coinbase_tx, &network, height)?;

            // Send transactions to the transaction verifier to be checked
            let mut async_checks = FuturesUnordered::new();

            let known_utxos = Arc::new(transparent::new_ordered_outputs(
                &block,
                &transaction_hashes,
            ));

            for (&transaction_hash, transaction) in
                transaction_hashes.iter().zip(block.transactions.iter())
            {
                let rsp = transaction_verifier
                    .ready()
                    .await
                    .expect("transaction verifier is always ready")
                    .call(tx::BlockRequest {
                        transaction_hash,
                        transaction: transaction.clone(),
                        known_utxos: known_utxos.clone(),
                        height,
                        time: block.header.time,
                    });
                async_checks.push(rsp);
            }
            tracing::trace!(len = async_checks.len(), "built async tx checks");

            // Get the transaction results back from the transaction verifier.

            // Sum up some block totals from the transaction responses.
            let mut sigops = 0;
            let mut block_miner_fees = Ok(Amount::zero());

            use futures::StreamExt;
            while let Some(result) = async_checks.next().await {
                tracing::trace!(?result, remaining = async_checks.len());
                let response = result
                    .map_err(Into::into)
                    .map_err(VerifyBlockError::Transaction)?;

                sigops += response.sigops;

                // Coinbase transactions consume the miner fee,
                // so they don't add any value to the block's total miner fee.
                if let Some(miner_fee) = response.miner_fee {
                    block_miner_fees += miner_fee;
                }
            }

            // Check the summed block totals

            if sigops > MAX_BLOCK_SIGOPS {
                Err(BlockError::TooManyTransparentSignatureOperations {
                    height,
                    hash,
                    sigops,
                })?;
            }

            let block_miner_fees =
                block_miner_fees.map_err(|amount_error| BlockError::SummingMinerFees {
                    height,
                    hash,
                    source: amount_error,
                })?;

            if let Some((expected_block_subsidy, deferred_pool_balance_change)) =
                expected_subsidy_and_deferred_pool_balance_change
            {
                check::miner_fees_are_valid(
                    &coinbase_tx,
                    height,
                    block_miner_fees,
                    expected_block_subsidy,
                    deferred_pool_balance_change,
                    &network,
                )?;
            }

            // Finally, submit the block for contextual verification.
            let new_outputs = Arc::into_inner(known_utxos)
                .expect("all verification tasks using known_utxos are complete");

            let prepared_block = zs::SemanticallyVerifiedBlock {
                block,
                hash,
                height,
                new_outputs,
                transaction_hashes,
                received_time: Some(received_time),
            };

            // Return early for proposal requests.
            if request.is_proposal() {
                return match state_service
                    .ready()
                    .await
                    .map_err(VerifyBlockError::ValidateProposal)?
                    .call(zs::Request::CheckBlockProposalValidity(prepared_block))
                    .await
                    .map_err(VerifyBlockError::ValidateProposal)?
                {
                    zs::Response::ValidBlockProposal => Ok(hash),
                    _ => unreachable!("wrong response for CheckBlockProposalValidity"),
                };
            }

            match state_service
                .ready()
                .await
                .map_err(|source| VerifyBlockError::StateService { source, hash })?
                .call(zs::Request::CommitSemanticallyVerifiedBlock(prepared_block))
                .await
            {
                Ok(zs::Response::Committed(committed_hash)) => {
                    assert_eq!(committed_hash, hash, "state must commit correct hash");
                    Ok(hash)
                }

                Err(source) => Err(map_commit_error(source, hash)),

                _ => unreachable!("wrong response for CommitSemanticallyVerifiedBlock"),
            }
        }
        .instrument(span)
        .boxed()
    }
}