darkfi/blockchain/
mod.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2025 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use std::sync::{Arc, Mutex};
20
21use darkfi_sdk::{monotree::Monotree, tx::TransactionHash};
22use log::debug;
23use sled_overlay::{sled, sled::Transactional};
24
25use crate::{tx::Transaction, util::time::Timestamp, Error, Result};
26
27/// Block related definitions and storage implementations
28pub mod block_store;
29pub use block_store::{
30    Block, BlockDifficulty, BlockInfo, BlockStore, BlockStoreOverlay, SLED_BLOCK_DIFFICULTY_TREE,
31    SLED_BLOCK_ORDER_TREE, SLED_BLOCK_STATE_INVERSE_DIFF_TREE, SLED_BLOCK_TREE,
32};
33
34/// Header definition and storage implementation
35pub mod header_store;
36pub use header_store::{
37    Header, HeaderHash, HeaderStore, HeaderStoreOverlay, SLED_HEADER_TREE, SLED_SYNC_HEADER_TREE,
38};
39
40/// Transactions related storage implementations
41pub mod tx_store;
42pub use tx_store::{
43    TxStore, TxStoreOverlay, SLED_PENDING_TX_ORDER_TREE, SLED_PENDING_TX_TREE,
44    SLED_TX_LOCATION_TREE, SLED_TX_TREE,
45};
46
47/// Contracts and Wasm storage implementations
48pub mod contract_store;
49pub use contract_store::{
50    ContractStore, ContractStoreOverlay, SLED_BINCODE_TREE, SLED_CONTRACTS_TREE,
51};
52
53/// Monero definitions needed for merge mining
54pub mod monero;
55
56/// Structure holding all sled trees that define the concept of Blockchain.
57#[derive(Clone)]
58pub struct Blockchain {
59    /// Main pointer to the sled db connection
60    pub sled_db: sled::Db,
61    /// Headers sled tree
62    pub headers: HeaderStore,
63    /// Blocks sled tree
64    pub blocks: BlockStore,
65    /// Transactions related sled trees
66    pub transactions: TxStore,
67    /// Contracts related sled trees
68    pub contracts: ContractStore,
69}
70
71impl Blockchain {
72    /// Instantiate a new `Blockchain` with the given `sled` database.
73    pub fn new(db: &sled::Db) -> Result<Self> {
74        let headers = HeaderStore::new(db)?;
75        let blocks = BlockStore::new(db)?;
76        let transactions = TxStore::new(db)?;
77        let contracts = ContractStore::new(db)?;
78
79        Ok(Self { sled_db: db.clone(), headers, blocks, transactions, contracts })
80    }
81
82    /// Insert a given [`BlockInfo`] into the blockchain database.
83    /// This functions wraps all the logic of separating the block into specific
84    /// data that can be fed into the different trees of the database.
85    /// Upon success, the functions returns the block hash that
86    /// were given and appended to the ledger.
87    pub fn add_block(&self, block: &BlockInfo) -> Result<HeaderHash> {
88        let mut trees = vec![];
89        let mut batches = vec![];
90
91        // Store header
92        let (headers_batch, _) = self.headers.insert_batch(&[block.header.clone()]);
93        trees.push(self.headers.main.clone());
94        batches.push(headers_batch);
95
96        // Store block
97        let blk: Block = Block::from_block_info(block);
98        let (bocks_batch, block_hashes) = self.blocks.insert_batch(&[blk]);
99        let block_hash = block_hashes[0];
100        let block_hash_vec = [block_hash];
101        trees.push(self.blocks.main.clone());
102        batches.push(bocks_batch);
103
104        // Store block order
105        let blocks_order_batch =
106            self.blocks.insert_batch_order(&[block.header.height], &block_hash_vec);
107        trees.push(self.blocks.order.clone());
108        batches.push(blocks_order_batch);
109
110        // Store transactions
111        let (txs_batch, txs_hashes) = self.transactions.insert_batch(&block.txs);
112        trees.push(self.transactions.main.clone());
113        batches.push(txs_batch);
114
115        // Store transactions_locations
116        let txs_locations_batch =
117            self.transactions.insert_batch_location(&txs_hashes, block.header.height);
118        trees.push(self.transactions.location.clone());
119        batches.push(txs_locations_batch);
120
121        // Perform an atomic transaction over the trees and apply the batches.
122        self.atomic_write(&trees, &batches)?;
123
124        Ok(block_hash)
125    }
126
127    /// Check if the given [`BlockInfo`] is in the database and all trees.
128    pub fn has_block(&self, block: &BlockInfo) -> Result<bool> {
129        let blockhash = match self.blocks.get_order(&[block.header.height], true) {
130            Ok(v) => v[0].unwrap(),
131            Err(_) => return Ok(false),
132        };
133
134        // Check if we have all transactions
135        let txs: Vec<TransactionHash> = block.txs.iter().map(|tx| tx.hash()).collect();
136        if self.transactions.get(&txs, true).is_err() {
137            return Ok(false)
138        }
139
140        // Check provided info produces the same hash
141        Ok(blockhash == block.hash())
142    }
143
144    /// Retrieve [`BlockInfo`]s by given hashes. Fails if any of them is not found.
145    pub fn get_blocks_by_hash(&self, hashes: &[HeaderHash]) -> Result<Vec<BlockInfo>> {
146        let blocks = self.blocks.get(hashes, true)?;
147        let blocks: Vec<Block> = blocks.iter().map(|x| x.clone().unwrap()).collect();
148        let ret = self.get_blocks_infos(&blocks)?;
149
150        Ok(ret)
151    }
152
153    /// Retrieve all [`BlockInfo`] for given slice of [`Block`].
154    /// Fails if any of them is not found
155    fn get_blocks_infos(&self, blocks: &[Block]) -> Result<Vec<BlockInfo>> {
156        let mut ret = Vec::with_capacity(blocks.len());
157        for block in blocks {
158            let headers = self.headers.get(&[block.header], true)?;
159            // Since we used strict get, its safe to unwrap here
160            let header = headers[0].clone().unwrap();
161
162            let txs = self.transactions.get(&block.txs, true)?;
163            let txs = txs.iter().map(|x| x.clone().unwrap()).collect();
164
165            let info = BlockInfo::new(header, txs, block.signature);
166            ret.push(info);
167        }
168
169        Ok(ret)
170    }
171
172    /// Retrieve [`BlockInfo`]s by given heights. Does not fail if any of them are not found.
173    pub fn get_blocks_by_heights(&self, heights: &[u32]) -> Result<Vec<BlockInfo>> {
174        debug!(target: "blockchain", "get_blocks_by_heights(): {heights:?}");
175        let blockhashes = self.blocks.get_order(heights, false)?;
176
177        let mut hashes = vec![];
178        for i in blockhashes.into_iter().flatten() {
179            hashes.push(i);
180        }
181
182        self.get_blocks_by_hash(&hashes)
183    }
184
185    /// Retrieve n headers before given block height.
186    pub fn get_headers_before(&self, height: u32, n: usize) -> Result<Vec<Header>> {
187        debug!(target: "blockchain", "get_headers_before(): {height} -> {n}");
188        let hashes = self.blocks.get_before(height, n)?;
189        let headers = self.headers.get(&hashes, true)?;
190        Ok(headers.iter().map(|h| h.clone().unwrap()).collect())
191    }
192
193    /// Retrieve stored blocks count
194    pub fn len(&self) -> usize {
195        self.blocks.len()
196    }
197
198    /// Retrieve stored txs count
199    pub fn txs_len(&self) -> usize {
200        self.transactions.len()
201    }
202
203    /// Check if blockchain contains any blocks
204    pub fn is_empty(&self) -> bool {
205        self.blocks.is_empty()
206    }
207
208    /// Retrieve genesis (first) block height and hash.
209    pub fn genesis(&self) -> Result<(u32, HeaderHash)> {
210        self.blocks.get_first()
211    }
212
213    /// Retrieve genesis (first) block info.
214    pub fn genesis_block(&self) -> Result<BlockInfo> {
215        let (_, hash) = self.genesis()?;
216        Ok(self.get_blocks_by_hash(&[hash])?[0].clone())
217    }
218
219    /// Retrieve the last block height and hash.
220    pub fn last(&self) -> Result<(u32, HeaderHash)> {
221        self.blocks.get_last()
222    }
223
224    /// Retrieve the last block header.
225    pub fn last_header(&self) -> Result<Header> {
226        let (_, hash) = self.last()?;
227        Ok(self.headers.get(&[hash], true)?[0].clone().unwrap())
228    }
229
230    /// Retrieve the last block info.
231    pub fn last_block(&self) -> Result<BlockInfo> {
232        let (_, hash) = self.last()?;
233        Ok(self.get_blocks_by_hash(&[hash])?[0].clone())
234    }
235
236    /// Retrieve the last block difficulty. If the tree is empty,
237    /// returns `BlockDifficulty::genesis` difficulty.
238    pub fn last_block_difficulty(&self) -> Result<BlockDifficulty> {
239        if let Some(found) = self.blocks.get_last_difficulty()? {
240            return Ok(found)
241        }
242
243        let genesis_block = self.genesis_block()?;
244        Ok(BlockDifficulty::genesis(genesis_block.header.timestamp))
245    }
246
247    /// Check if block order for the given height is in the database.
248    pub fn has_height(&self, height: u32) -> Result<bool> {
249        let vec = match self.blocks.get_order(&[height], true) {
250            Ok(v) => v,
251            Err(_) => return Ok(false),
252        };
253        Ok(!vec.is_empty())
254    }
255
256    /// Insert a given slice of pending transactions into the blockchain database.
257    /// On success, the function returns the transaction hashes in the same order
258    /// as the input transactions.
259    pub fn add_pending_txs(&self, txs: &[Transaction]) -> Result<Vec<TransactionHash>> {
260        let (txs_batch, txs_hashes) = self.transactions.insert_batch_pending(txs);
261        let txs_order_batch = self.transactions.insert_batch_pending_order(&txs_hashes)?;
262
263        // Perform an atomic transaction over the trees and apply the batches.
264        let trees = [self.transactions.pending.clone(), self.transactions.pending_order.clone()];
265        let batches = [txs_batch, txs_order_batch];
266        self.atomic_write(&trees, &batches)?;
267
268        Ok(txs_hashes)
269    }
270
271    /// Retrieve all transactions from the pending tx store.
272    /// Be careful as this will try to load everything in memory.
273    pub fn get_pending_txs(&self) -> Result<Vec<Transaction>> {
274        let txs = self.transactions.get_all_pending()?;
275        let indexes = self.transactions.get_all_pending_order()?;
276        if txs.len() != indexes.len() {
277            return Err(Error::InvalidInputLengths)
278        }
279
280        let mut ret = Vec::with_capacity(txs.len());
281        for index in indexes {
282            ret.push(txs.get(&index.1).unwrap().clone());
283        }
284
285        Ok(ret)
286    }
287
288    /// Remove a given slice of pending transactions from the blockchain database.
289    pub fn remove_pending_txs(&self, txs: &[Transaction]) -> Result<()> {
290        let txs_hashes: Vec<TransactionHash> = txs.iter().map(|tx| tx.hash()).collect();
291        self.remove_pending_txs_hashes(&txs_hashes)
292    }
293
294    /// Remove a given slice of pending transactions hashes from the blockchain database.
295    pub fn remove_pending_txs_hashes(&self, txs: &[TransactionHash]) -> Result<()> {
296        let indexes = self.transactions.get_all_pending_order()?;
297        // We could do indexes.iter().map(|x| txs.contains(x.1)).collect.map(|x| x.0).collect
298        // but this is faster since we don't do the second iteration
299        let mut removed_indexes = vec![];
300        for index in indexes {
301            if txs.contains(&index.1) {
302                removed_indexes.push(index.0);
303            }
304        }
305
306        let txs_batch = self.transactions.remove_batch_pending(txs);
307        let txs_order_batch = self.transactions.remove_batch_pending_order(&removed_indexes);
308
309        // Perform an atomic transaction over the trees and apply the batches.
310        let trees = [self.transactions.pending.clone(), self.transactions.pending_order.clone()];
311        let batches = [txs_batch, txs_order_batch];
312        self.atomic_write(&trees, &batches)?;
313
314        Ok(())
315    }
316
317    /// Auxiliary function to write to multiple trees completely atomic.
318    fn atomic_write(&self, trees: &[sled::Tree], batches: &[sled::Batch]) -> Result<()> {
319        if trees.len() != batches.len() {
320            return Err(Error::InvalidInputLengths)
321        }
322
323        trees.transaction(|trees| {
324            for (index, tree) in trees.iter().enumerate() {
325                tree.apply_batch(&batches[index])?;
326            }
327
328            Ok::<(), sled::transaction::ConflictableTransactionError<sled::Error>>(())
329        })?;
330
331        Ok(())
332    }
333
334    /// Retrieve all blocks contained in the blockchain in order.
335    /// Be careful as this will try to load everything in memory.
336    pub fn get_all(&self) -> Result<Vec<BlockInfo>> {
337        let order = self.blocks.get_all_order()?;
338        let order: Vec<HeaderHash> = order.iter().map(|x| x.1).collect();
339        let blocks = self.get_blocks_by_hash(&order)?;
340
341        Ok(blocks)
342    }
343
344    /// Retrieve [`BlockInfo`]s by given heights range.
345    pub fn get_by_range(&self, start: u32, end: u32) -> Result<Vec<BlockInfo>> {
346        let blockhashes = self.blocks.get_order_by_range(start, end)?;
347        let hashes: Vec<HeaderHash> = blockhashes.into_iter().map(|(_, hash)| hash).collect();
348        self.get_blocks_by_hash(&hashes)
349    }
350
351    /// Retrieve last 'N' [`BlockInfo`]s from the blockchain.
352    pub fn get_last_n(&self, n: usize) -> Result<Vec<BlockInfo>> {
353        let records = self.blocks.get_last_n_orders(n)?;
354
355        let mut last_n = vec![];
356        for record in records {
357            let header_hash = record.1;
358            let blocks = self.get_blocks_by_hash(&[header_hash])?;
359            for block in blocks {
360                last_n.push(block.clone());
361            }
362        }
363
364        Ok(last_n)
365    }
366
367    /// Auxiliary function to reset the blockchain and consensus state
368    /// to the provided block height.
369    pub fn reset_to_height(&self, height: u32) -> Result<()> {
370        // First we grab the last block height
371        let (last, _) = self.last()?;
372
373        // Check if request height is after our last height
374        if height >= last {
375            return Ok(())
376        }
377
378        // Grab all state inverse diffs until requested height,
379        // going backwards.
380        let heights: Vec<u32> = (height + 1..=last).rev().collect();
381        let inverse_diffs = self.blocks.get_state_inverse_diff(&heights, true)?;
382
383        // Create an overlay to apply the reverse diffs
384        let overlay = BlockchainOverlay::new(self)?;
385
386        // Apply the inverse diffs sequence
387        let overlay_lock = overlay.lock().unwrap();
388        let mut lock = overlay_lock.overlay.lock().unwrap();
389        for inverse_diff in inverse_diffs {
390            // Since we used strict retrieval it's safe to unwrap here
391            let inverse_diff = inverse_diff.unwrap();
392            lock.add_diff(&inverse_diff)?;
393            lock.apply_diff(&inverse_diff)?;
394            self.sled_db.flush()?;
395        }
396        drop(lock);
397        drop(overlay_lock);
398
399        Ok(())
400    }
401
402    /// Generate a Monotree(SMT) containing all contracts states
403    /// checksums, along with the wasm bincodes checksum.
404    ///
405    /// Note: native contracts zkas tree and wasm bincodes are excluded.
406    pub fn get_state_monotree(&self) -> Result<Monotree> {
407        self.contracts.get_state_monotree(&self.sled_db)
408    }
409}
410
411/// Atomic pointer to sled db overlay.
412pub type SledDbOverlayPtr = Arc<Mutex<sled_overlay::SledDbOverlay>>;
413
414/// Atomic pointer to blockchain overlay.
415pub type BlockchainOverlayPtr = Arc<Mutex<BlockchainOverlay>>;
416
417/// Overlay structure over a [`Blockchain`] instance.
418pub struct BlockchainOverlay {
419    /// Main [`sled_overlay::SledDbOverlay`] to the sled db connection
420    pub overlay: SledDbOverlayPtr,
421    /// Headers overlay
422    pub headers: HeaderStoreOverlay,
423    /// Blocks overlay
424    pub blocks: BlockStoreOverlay,
425    /// Transactions overlay
426    pub transactions: TxStoreOverlay,
427    /// Contract overlay
428    pub contracts: ContractStoreOverlay,
429}
430
431impl BlockchainOverlay {
432    /// Instantiate a new `BlockchainOverlay` over the given [`Blockchain`] instance.
433    pub fn new(blockchain: &Blockchain) -> Result<BlockchainOverlayPtr> {
434        // Here we configure all our blockchain sled trees to be protected in the overlay
435        let protected_trees = vec![
436            SLED_BLOCK_TREE,
437            SLED_BLOCK_ORDER_TREE,
438            SLED_BLOCK_DIFFICULTY_TREE,
439            SLED_BLOCK_STATE_INVERSE_DIFF_TREE,
440            SLED_HEADER_TREE,
441            SLED_SYNC_HEADER_TREE,
442            SLED_TX_TREE,
443            SLED_TX_LOCATION_TREE,
444            SLED_PENDING_TX_TREE,
445            SLED_PENDING_TX_ORDER_TREE,
446            SLED_CONTRACTS_TREE,
447            SLED_BINCODE_TREE,
448        ];
449        let overlay = Arc::new(Mutex::new(sled_overlay::SledDbOverlay::new(
450            &blockchain.sled_db,
451            protected_trees,
452        )));
453        let headers = HeaderStoreOverlay::new(&overlay)?;
454        let blocks = BlockStoreOverlay::new(&overlay)?;
455        let transactions = TxStoreOverlay::new(&overlay)?;
456        let contracts = ContractStoreOverlay::new(&overlay)?;
457
458        Ok(Arc::new(Mutex::new(Self { overlay, headers, blocks, transactions, contracts })))
459    }
460
461    /// Check if blockchain contains any blocks
462    pub fn is_empty(&self) -> Result<bool> {
463        self.blocks.is_empty()
464    }
465
466    /// Retrieve the last block height and hash.
467    pub fn last(&self) -> Result<(u32, HeaderHash)> {
468        self.blocks.get_last()
469    }
470
471    /// Retrieve the last block info.
472    pub fn last_block(&self) -> Result<BlockInfo> {
473        let (_, hash) = self.last()?;
474        Ok(self.get_blocks_by_hash(&[hash])?[0].clone())
475    }
476
477    /// Retrieve the last block height.
478    pub fn last_block_height(&self) -> Result<u32> {
479        Ok(self.last()?.0)
480    }
481
482    /// Retrieve the last block timestamp.
483    pub fn last_block_timestamp(&self) -> Result<Timestamp> {
484        let (_, hash) = self.last()?;
485        Ok(self.get_blocks_by_hash(&[hash])?[0].header.timestamp)
486    }
487
488    /// Insert a given [`BlockInfo`] into the overlay.
489    /// This functions wraps all the logic of separating the block into specific
490    /// data that can be fed into the different trees of the overlay.
491    /// Upon success, the functions returns the block hash that
492    /// were given and appended to the overlay.
493    /// Since we are adding to the overlay, we don't need to exeucte
494    /// the writes atomically.
495    pub fn add_block(&self, block: &BlockInfo) -> Result<HeaderHash> {
496        // Store header
497        self.headers.insert(&[block.header.clone()])?;
498
499        // Store block
500        let blk: Block = Block::from_block_info(block);
501        let txs_hashes = blk.txs.clone();
502        let block_hash = self.blocks.insert(&[blk])?[0];
503        let block_hash_vec = [block_hash];
504
505        // Store block order
506        self.blocks.insert_order(&[block.header.height], &block_hash_vec)?;
507
508        // Store transactions
509        self.transactions.insert(&block.txs)?;
510
511        // Store transactions locations
512        self.transactions.insert_location(&txs_hashes, block.header.height)?;
513
514        Ok(block_hash)
515    }
516
517    /// Check if the given [`BlockInfo`] is in the database and all trees.
518    pub fn has_block(&self, block: &BlockInfo) -> Result<bool> {
519        let blockhash = match self.blocks.get_order(&[block.header.height], true) {
520            Ok(v) => v[0].unwrap(),
521            Err(_) => return Ok(false),
522        };
523
524        // Check if we have all transactions
525        let txs: Vec<TransactionHash> = block.txs.iter().map(|tx| tx.hash()).collect();
526        if self.transactions.get(&txs, true).is_err() {
527            return Ok(false)
528        }
529
530        // Check provided info produces the same hash
531        Ok(blockhash == block.hash())
532    }
533
534    /// Retrieve [`Header`]s by given hashes. Fails if any of them is not found.
535    pub fn get_headers_by_hash(&self, hashes: &[HeaderHash]) -> Result<Vec<Header>> {
536        let headers = self.headers.get(hashes, true)?;
537        let ret: Vec<Header> = headers.iter().map(|x| x.clone().unwrap()).collect();
538
539        Ok(ret)
540    }
541
542    /// Retrieve [`BlockInfo`]s by given hashes. Fails if any of them is not found.
543    pub fn get_blocks_by_hash(&self, hashes: &[HeaderHash]) -> Result<Vec<BlockInfo>> {
544        let blocks = self.blocks.get(hashes, true)?;
545        let blocks: Vec<Block> = blocks.iter().map(|x| x.clone().unwrap()).collect();
546        let ret = self.get_blocks_infos(&blocks)?;
547
548        Ok(ret)
549    }
550
551    /// Retrieve all [`BlockInfo`] for given slice of [`Block`].
552    /// Fails if any of them is not found
553    fn get_blocks_infos(&self, blocks: &[Block]) -> Result<Vec<BlockInfo>> {
554        let mut ret = Vec::with_capacity(blocks.len());
555        for block in blocks {
556            let headers = self.headers.get(&[block.header], true)?;
557            // Since we used strict get, its safe to unwrap here
558            let header = headers[0].clone().unwrap();
559
560            let txs = self.transactions.get(&block.txs, true)?;
561            let txs = txs.iter().map(|x| x.clone().unwrap()).collect();
562
563            let info = BlockInfo::new(header, txs, block.signature);
564            ret.push(info);
565        }
566
567        Ok(ret)
568    }
569
570    /// Retrieve [`Block`]s by given hashes and return their transactions hashes.
571    pub fn get_blocks_txs_hashes(&self, hashes: &[HeaderHash]) -> Result<Vec<TransactionHash>> {
572        let blocks = self.blocks.get(hashes, true)?;
573        let mut ret = vec![];
574        for block in blocks {
575            ret.extend_from_slice(&block.unwrap().txs);
576        }
577
578        Ok(ret)
579    }
580
581    /// Checkpoint overlay so we can revert to it, if needed.
582    pub fn checkpoint(&self) {
583        self.overlay.lock().unwrap().checkpoint();
584    }
585
586    /// Revert to current overlay checkpoint.
587    pub fn revert_to_checkpoint(&self) -> Result<()> {
588        self.overlay.lock().unwrap().revert_to_checkpoint()?;
589
590        Ok(())
591    }
592
593    /// Auxiliary function to create a full clone using SledDbOverlay::clone,
594    /// generating new pointers for the underlying overlays.
595    pub fn full_clone(&self) -> Result<BlockchainOverlayPtr> {
596        let overlay = Arc::new(Mutex::new(self.overlay.lock().unwrap().clone()));
597        let headers = HeaderStoreOverlay::new(&overlay)?;
598        let blocks = BlockStoreOverlay::new(&overlay)?;
599        let transactions = TxStoreOverlay::new(&overlay)?;
600        let contracts = ContractStoreOverlay::new(&overlay)?;
601
602        Ok(Arc::new(Mutex::new(Self { overlay, headers, blocks, transactions, contracts })))
603    }
604
605    /// Generate a Monotree(SMT) containing all contracts states
606    /// checksums, along with the wasm bincodes checksum.
607    /// A clone is used so we are not affected by the opened trees
608    /// during checksum computing.
609    ///
610    /// Note: native contracts zkas tree and wasm bincodes are excluded.
611    pub fn get_state_monotree(&self) -> Result<Monotree> {
612        self.full_clone()?.lock().unwrap().contracts.get_state_monotree()
613    }
614}