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                )
78                .ok()
79            })
80    }
81
82    /// Returns blocks indexed by height in a [`BTreeMap`].
83    ///
84    /// Returns Mainnet blocks if `self` is set to Mainnet, and Testnet blocks otherwise.
85    pub fn block_map(&self) -> BTreeMap<u32, &'static [u8]> {
86        if self.is_mainnet() {
87            zebra_test::vectors::MAINNET_BLOCKS.clone()
88        } else {
89            zebra_test::vectors::TESTNET_BLOCKS.clone()
90        }
91    }
92
93    /// Returns genesis block for chain.
94    pub fn gen_block(&self) -> std::option::Option<&&[u8]> {
95        if self.is_mainnet() {
96            MAINNET_BLOCKS.get(&0)
97        } else {
98            TESTNET_BLOCKS.get(&0)
99        }
100    }
101
102    /// Returns block bytes
103    pub fn test_block(&self, main_height: u32, test_height: u32) -> Option<Block> {
104        match (self.is_mainnet(), main_height, test_height) {
105            (true, 653_599, _) => BLOCK_MAINNET_653599_BYTES.zcash_deserialize_into().ok(),
106            (true, 982_681, _) => BLOCK_MAINNET_982681_BYTES.zcash_deserialize_into().ok(),
107            (false, _, 583_999) => BLOCK_TESTNET_583999_BYTES.zcash_deserialize_into().ok(),
108            (false, _, 925_483) => BLOCK_TESTNET_925483_BYTES.zcash_deserialize_into().ok(),
109            _ => None,
110        }
111    }
112
113    /// Returns iterator over blockchain.
114    pub fn blockchain_iter(&self) -> std::collections::btree_map::Iter<'_, u32, &[u8]> {
115        if self.is_mainnet() {
116            CONTINUOUS_MAINNET_BLOCKS.iter()
117        } else {
118            CONTINUOUS_TESTNET_BLOCKS.iter()
119        }
120    }
121
122    /// Returns BTreemap of blockchain.
123    pub fn blockchain_map(&self) -> &BTreeMap<u32, &'static [u8]> {
124        if self.is_mainnet() {
125            &CONTINUOUS_MAINNET_BLOCKS
126        } else {
127            &CONTINUOUS_TESTNET_BLOCKS
128        }
129    }
130
131    /// Returns a [`BTreeMap`] of heights and Sapling anchors for this network.
132    pub fn sapling_anchors(&self) -> std::collections::BTreeMap<u32, &[u8; 32]> {
133        if self.is_mainnet() {
134            MAINNET_FINAL_SAPLING_ROOTS.clone()
135        } else {
136            TESTNET_FINAL_SAPLING_ROOTS.clone()
137        }
138    }
139
140    /// Returns a [`BTreeMap`] of heights and Orchard anchors for this network.
141    pub fn orchard_anchors(&self) -> std::collections::BTreeMap<u32, &[u8; 32]> {
142        if self.is_mainnet() {
143            MAINNET_FINAL_ORCHARD_ROOTS.clone()
144        } else {
145            TESTNET_FINAL_ORCHARD_ROOTS.clone()
146        }
147    }
148
149    /// Returns BTreemap of blocks and sapling roots.
150    pub fn block_sapling_roots_map(
151        &self,
152    ) -> (
153        &std::collections::BTreeMap<u32, &'static [u8]>,
154        &std::collections::BTreeMap<u32, &'static [u8; 32]>,
155    ) {
156        if self.is_mainnet() {
157            (&*MAINNET_BLOCKS, &*MAINNET_FINAL_SAPLING_ROOTS)
158        } else {
159            (&*TESTNET_BLOCKS, &*TESTNET_FINAL_SAPLING_ROOTS)
160        }
161    }
162
163    /// Returns block and sapling root bytes
164    pub fn test_block_sapling_roots(
165        &self,
166        main_height: u32,
167        test_height: u32,
168    ) -> Option<(&[u8], [u8; 32])> {
169        match (self.is_mainnet(), main_height, test_height) {
170            (true, 1_046_400, _) => Some((
171                &BLOCK_MAINNET_1046400_BYTES[..],
172                *SAPLING_FINAL_ROOT_MAINNET_1046400_BYTES,
173            )),
174            (false, _, 1_116_000) => Some((
175                &BLOCK_TESTNET_1116000_BYTES[..],
176                *SAPLING_FINAL_ROOT_TESTNET_1116000_BYTES,
177            )),
178            _ => None,
179        }
180    }
181
182    /// Returns BTreemap of blocks and sprout roots, and last split height.
183    pub fn block_sprout_roots_height(
184        &self,
185    ) -> (
186        &std::collections::BTreeMap<u32, &'static [u8]>,
187        &std::collections::BTreeMap<u32, &'static [u8; 32]>,
188        u32,
189    ) {
190        // The mainnet block height at which the first JoinSplit occurred.
191        const MAINNET_FIRST_JOINSPLIT_HEIGHT: u32 = 396;
192
193        // The testnet block height at which the first JoinSplit occurred.
194        const TESTNET_FIRST_JOINSPLIT_HEIGHT: u32 = 2259;
195        if self.is_mainnet() {
196            (
197                &*MAINNET_BLOCKS,
198                &*MAINNET_FINAL_SPROUT_ROOTS,
199                MAINNET_FIRST_JOINSPLIT_HEIGHT,
200            )
201        } else {
202            (
203                &*TESTNET_BLOCKS,
204                &*TESTNET_FINAL_SPROUT_ROOTS,
205                TESTNET_FIRST_JOINSPLIT_HEIGHT,
206            )
207        }
208    }
209}