Skip to main content

zebra_state/
arbitrary.rs

1//! Randomised data generation for state data.
2
3use std::sync::Arc;
4
5use zebra_chain::{
6    amount::{Amount, DeferredPoolBalanceChange},
7    block::{self, Block},
8    transaction::Transaction,
9    transparent,
10    value_balance::ValueBalance,
11};
12
13use crate::{
14    request::ContextuallyVerifiedBlock, service::chain_tip::ChainTipBlock,
15    SemanticallyVerifiedBlock,
16};
17
18/// Mocks computation done during semantic validation
19pub trait Prepare {
20    /// Runs block semantic validation computation, and returns the result.
21    /// Test-only method.
22    fn prepare(self) -> SemanticallyVerifiedBlock;
23}
24
25impl Prepare for Arc<Block> {
26    fn prepare(self) -> SemanticallyVerifiedBlock {
27        self.into()
28    }
29}
30
31impl<T> From<T> for ChainTipBlock
32where
33    T: Prepare,
34{
35    fn from(block: T) -> Self {
36        block.prepare().into()
37    }
38}
39
40impl SemanticallyVerifiedBlock {
41    /// Returns a [`ContextuallyVerifiedBlock`] created from this block,
42    /// with fake zero-valued UTXOs for spends from earlier blocks.
43    ///
44    /// Only for use in tests.
45    #[cfg(test)]
46    pub fn test_with_zero_spent_utxos(&self) -> ContextuallyVerifiedBlock {
47        ContextuallyVerifiedBlock::test_with_zero_spent_utxos(self)
48    }
49
50    /// Returns a [`ContextuallyVerifiedBlock`] created from this block,
51    /// with no chain value pool change.
52    ///
53    /// Only for use in tests.
54    #[cfg(test)]
55    pub fn test_with_zero_chain_pool_change(&self) -> ContextuallyVerifiedBlock {
56        ContextuallyVerifiedBlock::test_with_zero_chain_pool_change(self)
57    }
58}
59
60impl ContextuallyVerifiedBlock {
61    /// Create a block ready for non-finalized `Chain` contextual validation, using actual
62    /// intra-block outputs and fake zero-valued UTXOs for spends from earlier blocks.
63    ///
64    /// Only for use in tests.
65    pub fn test_with_zero_spent_utxos(block: impl Into<SemanticallyVerifiedBlock>) -> Self {
66        let block = block.into();
67
68        let zero_output = transparent::Output {
69            value: Amount::zero(),
70            lock_script: transparent::Script::new(&[]),
71        };
72
73        let zero_utxo = transparent::OrderedUtxo::new(zero_output, block::Height(1), 1);
74
75        let zero_spent_utxos = block
76            .block
77            .transactions
78            .iter()
79            .map(AsRef::as_ref)
80            .flat_map(Transaction::inputs)
81            .flat_map(|input| input.outpoint())
82            .map(|outpoint| {
83                let utxo = block.new_outputs.get(&outpoint).unwrap_or(&zero_utxo);
84                (outpoint, utxo.clone())
85            })
86            .collect();
87
88        ContextuallyVerifiedBlock::with_block_and_spent_utxos(
89            block,
90            zero_spent_utxos,
91            DeferredPoolBalanceChange::zero(),
92            &zebra_chain::parameters::Network::Mainnet,
93            ValueBalance::zero(),
94        )
95        .expect("all UTXOs are provided with zero values")
96    }
97
98    /// Create a [`ContextuallyVerifiedBlock`] from a [`Block`] or [`SemanticallyVerifiedBlock`],
99    /// with no chain value pool change.
100    ///
101    /// Only for use in tests.
102    pub fn test_with_zero_chain_pool_change(block: impl Into<SemanticallyVerifiedBlock>) -> Self {
103        let SemanticallyVerifiedBlock {
104            block,
105            hash,
106            height,
107            new_outputs,
108            transaction_hashes,
109            received_time,
110        } = block.into();
111        let new_outputs = Arc::new(new_outputs);
112
113        Self {
114            block,
115            hash,
116            height,
117            new_outputs: Arc::new(
118                new_outputs
119                    .iter()
120                    .map(|(outpoint, utxo)| (*outpoint, Arc::new(utxo.clone())))
121                    .collect(),
122            ),
123            // Just re-use the outputs we created in this block, even though that's incorrect.
124            //
125            // TODO: fix the tests, and stop adding unrelated inputs and outputs.
126            spent_outputs: new_outputs,
127            transaction_hashes,
128            chain_value_pool_change: ValueBalance::zero(),
129            received_time,
130        }
131    }
132}