From 3996374a8bb647053af056563a38ad148be5183d Mon Sep 17 00:00:00 2001 From: "Augusto F. Hack" Date: Fri, 14 Apr 2023 20:35:08 +0200 Subject: [PATCH] feat: allow merging of leaves Consider the case of a MMR with one entry, and a new entry is being added. Both of these values are quite unique, they are at the same time the root and only leaf of their corresponding tree. Currently this representation is not supported by the [MerkleStore], so the leaves are not in it. Once the two values are merged, they both become leaves of a new tree under the new parent, and the existing validation didn't permit that promotion from happening. This lifts the validation, and changes the method to clarify that not only `root` are being merged, by arbitrary nodes of a tree (leafs, internal, or roots), with arbitrary mixing of each. --- src/merkle/store/mod.rs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/merkle/store/mod.rs b/src/merkle/store/mod.rs index 7d11f2d..a510826 100644 --- a/src/merkle/store/mod.rs +++ b/src/merkle/store/mod.rs @@ -478,26 +478,23 @@ impl MerkleStore { Ok(RootPath { root, path }) } + /// Merges two elements and adds the resulting node into the store. + /// + /// Merges arbitrary values. They may be leafs, nodes, or a mixture of both. pub fn merge_roots(&mut self, root1: Word, root2: Word) -> Result { let root1: RpoDigest = root1.into(); let root2: RpoDigest = root2.into(); - if !self.nodes.contains_key(&root1) { - Err(MerkleError::NodeNotInStore(root1.into(), NodeIndex::root())) - } else if !self.nodes.contains_key(&root1) { - Err(MerkleError::NodeNotInStore(root2.into(), NodeIndex::root())) - } else { - let parent: Word = Rpo256::merge(&[root1, root2]).into(); - self.nodes.insert( - parent.into(), - Node { - left: root1, - right: root2, - }, - ); + let parent: Word = Rpo256::merge(&[root1, root2]).into(); + self.nodes.insert( + parent.into(), + Node { + left: root1, + right: root2, + }, + ); - Ok(parent) - } + Ok(parent) } }