Skip to main content

zebra_chain/tests/
vectors.rs

1//! Network methods for fetching blockchain vectors.
2//!
3
4use std::{collections::BTreeMap, ops::RangeBounds};
5
6use crate::{
7    amount::Amount,
8    block::Block,
9    parameters::Network,
10    serialization::ZcashDeserializeInto,
11    transaction::{UnminedTx, VerifiedUnminedTx},
12};
13
14use zebra_test::vectors::{
15    BLOCK_MAINNET_1046400_BYTES, BLOCK_MAINNET_653599_BYTES, BLOCK_MAINNET_982681_BYTES,
16    BLOCK_TESTNET_1116000_BYTES, BLOCK_TESTNET_583999_BYTES, BLOCK_TESTNET_925483_BYTES,
17    CONTINUOUS_MAINNET_BLOCKS, CONTINUOUS_TESTNET_BLOCKS, MAINNET_BLOCKS,
18    MAINNET_FINAL_ORCHARD_ROOTS, MAINNET_FINAL_SAPLING_ROOTS, MAINNET_FINAL_SPROUT_ROOTS,
19    SAPLING_FINAL_ROOT_MAINNET_1046400_BYTES, SAPLING_FINAL_ROOT_TESTNET_1116000_BYTES,
20    TESTNET_BLOCKS, TESTNET_FINAL_ORCHARD_ROOTS, TESTNET_FINAL_SAPLING_ROOTS,
21    TESTNET_FINAL_SPROUT_ROOTS,
22};
23
24/// Network methods for fetching blockchain vectors.
25impl Network {
26    /// Returns true if network is of type Mainnet.
27    pub fn is_mainnet(&self) -> bool {
28        matches!(self, Network::Mainnet)
29    }
30
31    /// Returns iterator over blocks.
32    pub fn block_iter(&self) -> std::collections::btree_map::Iter<'static, u32, &'static [u8]> {
33        if self.is_mainnet() {
34            MAINNET_BLOCKS.iter()
35        } else {
36            TESTNET_BLOCKS.iter()
37        }
38    }
39
40    /// Returns iterator over deserialized blocks.
41    pub fn block_parsed_iter(&self) -> impl Iterator<Item = Block> {
42        self.block_iter().map(|(_, block_bytes)| {
43            block_bytes
44                .zcash_deserialize_into::<Block>()
45                .expect("block is structurally valid")
46        })
47    }
48
49    /// Returns iterator over verified unmined transactions in the provided block height range.
50    pub fn unmined_transactions_in_blocks(
51        &self,
52        block_height_range: impl RangeBounds<u32>,
53    ) -> impl DoubleEndedIterator<Item = VerifiedUnminedTx> {
54        let blocks = self.block_iter();
55
56        // Deserialize the blocks that are selected based on the specified `block_height_range`.
57        let selected_blocks = blocks
58            .filter(move |(&height, _)| block_height_range.contains(&height))
59            .map(|(_, block)| {
60                block
61                    .zcash_deserialize_into::<Block>()
62                    .expect("block test vector is structurally valid")
63            });
64
65        // Extract the transactions from the blocks and wrap each one as an unmined transaction.
66        // Use a fake zero miner fee and sigops, because we don't have the UTXOs to calculate
67        // the correct fee.
68        selected_blocks
69            .flat_map(|block| block.transactions)
70            .map(UnminedTx::from)
71            // Skip transactions that fail ZIP-317 mempool checks
72            .filter_map(|transaction| {
73                VerifiedUnminedTx::new(
74                    transaction,
75                    Amount::try_from(1_000_000).expect("valid amount"),
76                    0,
77                    std::sync::Arc::new(vec![]),
78                )
79                .ok()
80            })
81    }
82
83    /// Returns blocks indexed by height in a [`BTreeMap`].
84    ///
85    /// Returns Mainnet blocks if `self` is set to Mainnet, and Testnet blocks otherwise.
86    pub fn block_map(&self) -> BTreeMap<u32, &'static [u8]> {
87        if self.is_mainnet() {
88            zebra_test::vectors::MAINNET_BLOCKS.clone()
89        } else {
90            zebra_test::vectors::TESTNET_BLOCKS.clone()
91        }
92    }
93
94    /// Returns genesis block for chain.
95    pub fn gen_block(&self) -> std::option::Option<&&[u8]> {
96        if self.is_mainnet() {
97            MAINNET_BLOCKS.get(&0)
98        } else {
99            TESTNET_BLOCKS.get(&0)
100        }
101    }
102
103    /// Returns block bytes
104    pub fn test_block(&self, main_height: u32, test_height: u32) -> Option<Block> {
105        match (self.is_mainnet(), main_height, test_height) {
106            (true, 653_599, _) => BLOCK_MAINNET_653599_BYTES.zcash_deserialize_into().ok(),
107            (true, 982_681, _) => BLOCK_MAINNET_982681_BYTES.zcash_deserialize_into().ok(),
108            (false, _, 583_999) => BLOCK_TESTNET_583999_BYTES.zcash_deserialize_into().ok(),
109            (false, _, 925_483) => BLOCK_TESTNET_925483_BYTES.zcash_deserialize_into().ok(),
110            _ => None,
111        }
112    }
113
114    /// Returns iterator over blockchain.
115    pub fn blockchain_iter(&self) -> std::collections::btree_map::Iter<'_, u32, &[u8]> {
116        if self.is_mainnet() {
117            CONTINUOUS_MAINNET_BLOCKS.iter()
118        } else {
119            CONTINUOUS_TESTNET_BLOCKS.iter()
120        }
121    }
122
123    /// Returns BTreemap of blockchain.
124    pub fn blockchain_map(&self) -> &BTreeMap<u32, &'static [u8]> {
125        if self.is_mainnet() {
126            &CONTINUOUS_MAINNET_BLOCKS
127        } else {
128            &CONTINUOUS_TESTNET_BLOCKS
129        }
130    }
131
132    /// Returns a [`BTreeMap`] of heights and Sapling anchors for this network.
133    pub fn sapling_anchors(&self) -> std::collections::BTreeMap<u32, &[u8; 32]> {
134        if self.is_mainnet() {
135            MAINNET_FINAL_SAPLING_ROOTS.clone()
136        } else {
137            TESTNET_FINAL_SAPLING_ROOTS.clone()
138        }
139    }
140
141    /// Returns a [`BTreeMap`] of heights and Orchard anchors for this network.
142    pub fn orchard_anchors(&self) -> std::collections::BTreeMap<u32, &[u8; 32]> {
143        if self.is_mainnet() {
144            MAINNET_FINAL_ORCHARD_ROOTS.clone()
145        } else {
146            TESTNET_FINAL_ORCHARD_ROOTS.clone()
147        }
148    }
149
150    /// Returns BTreemap of blocks and sapling roots.
151    pub fn block_sapling_roots_map(
152        &self,
153    ) -> (
154        &std::collections::BTreeMap<u32, &'static [u8]>,
155        &std::collections::BTreeMap<u32, &'static [u8; 32]>,
156    ) {
157        if self.is_mainnet() {
158            (&*MAINNET_BLOCKS, &*MAINNET_FINAL_SAPLING_ROOTS)
159        } else {
160            (&*TESTNET_BLOCKS, &*TESTNET_FINAL_SAPLING_ROOTS)
161        }
162    }
163
164    /// Returns block and sapling root bytes
165    pub fn test_block_sapling_roots(
166        &self,
167        main_height: u32,
168        test_height: u32,
169    ) -> Option<(&[u8], [u8; 32])> {
170        match (self.is_mainnet(), main_height, test_height) {
171            (true, 1_046_400, _) => Some((
172                &BLOCK_MAINNET_1046400_BYTES[..],
173                *SAPLING_FINAL_ROOT_MAINNET_1046400_BYTES,
174            )),
175            (false, _, 1_116_000) => Some((
176                &BLOCK_TESTNET_1116000_BYTES[..],
177                *SAPLING_FINAL_ROOT_TESTNET_1116000_BYTES,
178            )),
179            _ => None,
180        }
181    }
182
183    /// Returns BTreemap of blocks and sprout roots, and last split height.
184    pub fn block_sprout_roots_height(
185        &self,
186    ) -> (
187        &std::collections::BTreeMap<u32, &'static [u8]>,
188        &std::collections::BTreeMap<u32, &'static [u8; 32]>,
189        u32,
190    ) {
191        // The mainnet block height at which the first JoinSplit occurred.
192        const MAINNET_FIRST_JOINSPLIT_HEIGHT: u32 = 396;
193
194        // The testnet block height at which the first JoinSplit occurred.
195        const TESTNET_FIRST_JOINSPLIT_HEIGHT: u32 = 2259;
196        if self.is_mainnet() {
197            (
198                &*MAINNET_BLOCKS,
199                &*MAINNET_FINAL_SPROUT_ROOTS,
200                MAINNET_FIRST_JOINSPLIT_HEIGHT,
201            )
202        } else {
203            (
204                &*TESTNET_BLOCKS,
205                &*TESTNET_FINAL_SPROUT_ROOTS,
206                TESTNET_FIRST_JOINSPLIT_HEIGHT,
207            )
208        }
209    }
210}