make PrecomputedSubtrees more generic

This commit is contained in:
Qyriad 2024-11-04 12:24:41 -07:00
parent d4e504aac8
commit f51694835a

View file

@ -352,8 +352,8 @@ pub(crate) trait SparseMerkleTree<const DEPTH: u8> {
fn sorted_pairs_to_leaves( fn sorted_pairs_to_leaves(
pairs: Vec<(Self::Key, Self::Value)>, pairs: Vec<(Self::Key, Self::Value)>,
) -> PrecomputedSubtrees<Self::Leaf> { ) -> PrecomputedSubtrees<u64, Self::Leaf> {
let mut accumulator: PrecomputedSubtrees<Self::Leaf> = Default::default(); let mut accumulator: PrecomputedSubtrees<u64, Self::Leaf> = Default::default();
// The kv-pairs we've seen so far that correspond to a single leaf. // The kv-pairs we've seen so far that correspond to a single leaf.
let mut current_leaf_buffer: Vec<(Self::Key, Self::Value)> = Default::default(); let mut current_leaf_buffer: Vec<(Self::Key, Self::Value)> = Default::default();
@ -632,14 +632,14 @@ impl SubtreeLeaf {
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct PrecomputedSubtrees<L> { pub struct PrecomputedSubtrees<K, L> {
/// Literal leaves to be added to the sparse Merkle tree's internal mapping. /// Literal leaves to be added to the sparse Merkle tree's internal mapping.
pub nodes: BTreeMap<u64, L>, pub nodes: BTreeMap<K, L>,
/// "Conceptual" leaves that will be used for computations. /// "Conceptual" leaves that will be used for computations.
pub leaves: Vec<Vec<SubtreeLeaf>>, pub leaves: Vec<Vec<SubtreeLeaf>>,
} }
impl<L> PrecomputedSubtrees<L> { impl<K, L> PrecomputedSubtrees<K, L> {
pub fn add_leaf(&mut self, leaf: SubtreeLeaf) { pub fn add_leaf(&mut self, leaf: SubtreeLeaf) {
let last_subtree = match self.leaves.last_mut() { let last_subtree = match self.leaves.last_mut() {
// Base case. // Base case.
@ -670,7 +670,7 @@ impl<L> PrecomputedSubtrees<L> {
} }
// Derive requires `L` to impl Default, even though we don't actually need that. // Derive requires `L` to impl Default, even though we don't actually need that.
impl<L> Default for PrecomputedSubtrees<L> { impl<K, L> Default for PrecomputedSubtrees<K, L> {
fn default() -> Self { fn default() -> Self {
Self { Self {
nodes: Default::default(), nodes: Default::default(),