pub fn update(&mut self, key: H256, value: V) -> Result<&H256> {
        // compute and store new leaf
        let node = MergeValue::from_h256(value.to_h256());
        // notice when value is zero the leaf is deleted, so we do not need to store it
        if !node.is_zero() {
            self.store.insert_leaf(key, value)?;
        } else {
            self.store.remove_leaf(&key)?;
        }

        // recompute the tree from bottom to top
        let mut current_key = key;
        let mut current_node = node;
        for height in 0..=core::u8::MAX {
            let parent_key = current_key.parent_path(height);
            let parent_branch_key = BranchKey::new(height, parent_key);
            let (left, right) =
                if let Some(parent_branch) = self.store.get_branch(&parent_branch_key)? {
                    if current_key.is_right(height) {
                        (parent_branch.left, current_node)
                    } else {
                        (current_node, parent_branch.right)
                    }
                } else if current_key.is_right(height) {
                    (MergeValue::zero(), current_node)
                } else {
                    (current_node, MergeValue::zero())
                };

            if !left.is_zero() || !right.is_zero() {
                // insert or update branch
                self.store.insert_branch(
                    parent_branch_key,
                    BranchNode {
                        left: left.clone(),
                        right: right.clone(),
                    },
                )?;
            } else {
                // remove empty branch
                self.store.remove_branch(&parent_branch_key)?;
            }
            // prepare for next round
            current_key = parent_key;
            current_node = merge::<H>(height, &parent_key, &left, &right);
        }

        self.root = current_node.hash::<H>();
        Ok(&self.root)
    }
/// Update a leaf, return new merkle root
/// set to zero value to delete a key
Update a leaf, return new merkle root
set to zero value to delete a key
Update a leaf return new merkle root set to zero value to delete a key
pub fn update(&mut self, key: H256, value: V) -> Result<&H256> {
        // compute and store new leaf
        let node = MergeValue::from_h256(value.to_h256());
        // notice when value is zero the leaf is deleted, so we do not need to store it
        if !node.is_zero() {
            self.store.insert_leaf(key, value)?;
        } else {
            self.store.remove_leaf(&key)?;
        }
        // recompute the tree from bottom to top
        let mut current_key = key;
        let mut current_node = node;
        for height in 0..=core::u8::MAX {
            let parent_key = current_key.parent_path(height);
            let parent_branch_key = BranchKey::new(height, parent_key);
            let (left, right) =
                if let Some(parent_branch) = self.store.get_branch(&parent_branch_key)? {
                    if current_key.is_right(height) {
                        (parent_branch.left, current_node)
                    } else {
                        (current_node, parent_branch.right)
                    }
                } else if current_key.is_right(height) {
                    (MergeValue::zero(), current_node)
                } else {
                    (current_node, MergeValue::zero())
                };
            if !left.is_zero() || !right.is_zero() {
                // insert or update branch
                self.store.insert_branch(
                    parent_branch_key,
                    BranchNode {
                        left: left.clone(),
                        right: right.clone(),
                    },
                )?;
            } else {
                // remove empty branch
                self.store.remove_branch(&parent_branch_key)?;
            }
            // prepare for next round
            current_key = parent_key;
            current_node = merge::<H>(height, &parent_key, &left, &right);
        }
        self.root = current_node.hash::<H>();
        Ok(&self.root)
    }
pub fn update ( & mut self , key : H256 , value : V ) -> Result < & H256 > { // compute and store new leaf let node = MergeValue :: from_h256 ( value . to_h256 ( ) ) ; // notice when value is zero the leaf is deleted, so we do not need to store it if ! node . is_zero ( ) { self . store . insert_leaf ( key , value ) ? ; } else { self . store . remove_leaf ( & key ) ? ; } // recompute the tree from bottom to top let mut current_key = key ; let mut current_node = node ; for height in 0 ..= core :: u8 :: MAX { let parent_key = current_key . parent_path ( height ) ; let parent_branch_key = BranchKey :: new ( height , parent_key ) ; let ( left , right ) = if let Some ( parent_branch ) = self . store . get_branch ( & parent_branch_key ) ? { if current_key . is_right ( height ) { ( parent_branch . left , current_node ) } else { ( current_node , parent_branch . right ) } } else if current_key . is_right ( height ) { ( MergeValue :: zero ( ) , current_node ) } else { ( current_node , MergeValue :: zero ( ) ) } ; if ! left . is_zero ( ) || ! right . is_zero ( ) { // insert or update branch self . store . insert_branch ( parent_branch_key , BranchNode { left : left . clone ( ) , right : right . clone ( ) , } , ) ? ; } else { // remove empty branch self . store . remove_branch ( & parent_branch_key ) ? ; } // prepare for next round current_key = parent_key ; current_node = merge :: < H > ( height , & parent_key , & left , & right ) ; } self . root = current_node . hash :: < H > ( ) ; Ok ( & self . root ) }
Update a leaf, return new merkle root
set to zero value to delete a key
Update a leaf return new merkle root set to zero value to delete a key
&mut self
key
H256
value
V