Skip to content

Commit

Permalink
Add a method that loads all the sprout trees from the database
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Jun 23, 2023
1 parent 75ab951 commit 80407d8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
21 changes: 21 additions & 0 deletions zebra-state/src/service/finalized_state/disk_format/shielded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ impl IntoDisk for sprout::tree::Root {
}
}

impl FromDisk for sprout::tree::Root {
fn from_bytes(bytes: impl AsRef<[u8]>) -> Self {
let array: [u8; 32] = bytes.as_ref().try_into().unwrap();
array.into()
}
}

impl IntoDisk for sapling::tree::Root {
type Bytes = [u8; 32];

Expand All @@ -52,6 +59,13 @@ impl IntoDisk for sapling::tree::Root {
}
}

impl FromDisk for sapling::tree::Root {
fn from_bytes(bytes: impl AsRef<[u8]>) -> Self {
let array: [u8; 32] = bytes.as_ref().try_into().unwrap();
array.try_into().expect("finalized data must be valid")
}
}

impl IntoDisk for orchard::tree::Root {
type Bytes = [u8; 32];

Expand All @@ -60,6 +74,13 @@ impl IntoDisk for orchard::tree::Root {
}
}

impl FromDisk for orchard::tree::Root {
fn from_bytes(bytes: impl AsRef<[u8]>) -> Self {
let array: [u8; 32] = bytes.as_ref().try_into().unwrap();
array.try_into().expect("finalized data must be valid")
}
}

// The following implementations for the note commitment trees use `serde` and
// `bincode` because currently the inner Merkle tree frontier (from
// `incrementalmerkletree`) only supports `serde` for serialization. `bincode`
Expand Down
15 changes: 14 additions & 1 deletion zebra-state/src/service/finalized_state/zebra_db/shielded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//! The [`crate::constants::DATABASE_FORMAT_VERSION`] constant must
//! be incremented each time the database format (column, serialization, etc) changes.

use std::sync::Arc;
use std::{collections::HashMap, sync::Arc};

use zebra_chain::{
block::Height, history_tree::HistoryTree, orchard, parallel::tree::NoteCommitmentTrees,
Expand Down Expand Up @@ -98,6 +98,19 @@ impl ZebraDb {
.map(Arc::new)
}

/// Returns all the Sprout note commitment trees in the database.
///
/// Calling this method can load a lot of data into RAM, and delay block commit transactions.
#[allow(dead_code, clippy::unwrap_in_result)]
pub fn sprout_note_commitments_full_map(
&self,
) -> HashMap<sprout::tree::Root, Arc<sprout::tree::NoteCommitmentTree>> {
let sprout_anchors_handle = self.db.cf_handle("sprout_anchors").unwrap();

self.db
.zs_items_in_range_unordered(&sprout_anchors_handle, ..)
}

/// Returns the Sapling note commitment tree of the finalized tip
/// or the empty tree if the state is empty.
pub fn sapling_note_commitment_tree(&self) -> Arc<sapling::tree::NoteCommitmentTree> {
Expand Down

0 comments on commit 80407d8

Please sign in to comment.