@@ -45,17 +45,11 @@ void SparseMerkleTree::EnsureHaveLevel(size_t level) {
4545}
4646
4747
48- void SparseMerkleTree::SetLeafByHash (const string& key, const string& data) {
49- serial_hasher_->Reset ();
50- serial_hasher_->Update (key);
51- return SetLeaf (PathFromBytes (serial_hasher_->Final ()), data);
52- }
53-
54-
5548void SparseMerkleTree::SetLeaf (const Path& path, const string& data) {
5649 CHECK_EQ (treehasher_.DigestSize (), path.size ());
5750 // Mark the tree dirty:
5851 root_hash_.clear ();
52+ string leaf_hash (treehasher_.HashLeaf (data));
5953
6054 IndexType node_index (0 );
6155 for (int depth (0 ); depth <= kDigestSizeBits ; ++depth) {
@@ -64,29 +58,29 @@ void SparseMerkleTree::SetLeaf(const Path& path, const string& data) {
6458 auto it (tree_[depth].find (node_index));
6559 if (it == tree_[depth].end ()) {
6660 CHECK (tree_[depth]
67- .emplace (make_pair (node_index, TreeNode (path, data )))
61+ .emplace (make_pair (node_index, TreeNode (path, leaf_hash )))
6862 .second );
6963 return ;
7064 } else if (it->second .type_ == TreeNode::INTERNAL) {
7165 // Mark the internal node hash dirty
72- it->second .hash_ .reset ();
73- } else if (it->second .leaf_ -> path_ == path) {
66+ it->second .hash_ .clear ();
67+ } else if (* it->second .path_ == path) {
7468 // replacement
7569 CHECK_EQ (TreeNode::LEAF, it->second .type_ );
76- it->second .leaf_ -> value_ = data ;
70+ it->second .hash_ = std::move (leaf_hash) ;
7771 return ;
7872 } else {
7973 // restructure: push the existing node down a level and replace this one
8074 // with an INTERNAL node
8175 CHECK_LT (depth, kDigestSizeBits );
8276 EnsureHaveLevel (depth + 1 );
8377 IndexType child_index ((node_index << 1 ) +
84- PathBit (it->second .leaf_ -> path_ , depth + 1 ));
78+ PathBit (* it->second .path_ , depth + 1 ));
8579 CHECK (tree_[depth + 1 ]
8680 .emplace (make_pair (child_index, std::move (it->second )))
8781 .second );
8882 it->second .type_ = TreeNode::INTERNAL;
89- it->second .leaf_ . reset ();
83+ it->second .hash_ . clear ();
9084 }
9185 node_index <<= 1 ;
9286 }
@@ -129,22 +123,21 @@ string SparseMerkleTree::CalculateSubtreeHash(size_t depth, IndexType index) {
129123 if (it != tree_[depth].end ()) {
130124 switch (it->second .type_ ) {
131125 case TreeNode::INTERNAL: {
132- if (it->second .hash_ ) {
133- return *( it->second .hash_ ) ;
126+ if (! it->second .hash_ . empty () ) {
127+ return it->second .hash_ ;
134128 }
135129 IndexType left_child_index (index << 1 );
136130 const string left (CalculateSubtreeHash (depth + 1 , left_child_index));
137131 const string right (
138132 CalculateSubtreeHash (depth + 1 , left_child_index + 1 ));
139- it->second .hash_ .reset (
140- new string (treehasher_.HashChildren (left, right)));
141- return *(it->second .hash_ );
133+ it->second .hash_ .assign (treehasher_.HashChildren (left, right));
134+ return it->second .hash_ ;
142135 }
143136
144137 case TreeNode::LEAF: {
145- string ret (treehasher_. HashLeaf ( it->second .leaf_ -> value_ ) );
138+ string ret (it->second .hash_ );
146139 for (int i (kDigestSizeBits - 1 ); i > depth; --i) {
147- if (PathBit (it->second .leaf_ -> path_ , i) == 0 ) {
140+ if (PathBit (*( it->second .path_ ) , i) == 0 ) {
148141 ret = treehasher_.HashChildren (ret, null_hashes_->at (i));
149142 } else {
150143 ret = treehasher_.HashChildren (null_hashes_->at (i), ret);
@@ -178,29 +171,28 @@ std::vector<string> SparseMerkleTree::InclusionProof(const Path& path) {
178171
179172string SparseMerkleTree::TreeNode::DebugString () const {
180173 ostringstream os;
181- os << " [TreeNode " ;
174+ os << " [TreeNode type: " ;
182175 switch (type_) {
183176 case INTERNAL:
184- os << " INTL hash: " ;
185- if (hash_) {
186- os << util::ToBase64 (*hash_);
187- } else {
188- os << " (unset)" ;
189- }
177+ os << " I" ;
190178 break ;
191179 case LEAF:
192- os << " LEAF leaf: " ;
193- os << leaf_->DebugString ();
180+ os << " L" ;
194181 break ;
195182 }
196- os << " ]" ;
197- return os.str ();
198- }
199183
184+ os << " hash: " ;
185+ if (!hash_.empty ()) {
186+ os << util::ToBase64 (hash_);
187+ } else {
188+ os << " (unset)" ;
189+ }
200190
201- string SparseMerkleTree::Leaf::DebugString () const {
202- ostringstream os;
203- os << " [Leaf path: " << path_ << " value: " << value_ << " ]" ;
191+ if (path_) {
192+ os << " path: " ;
193+ os << *path_;
194+ }
195+ os << " ]" ;
204196 return os.str ();
205197}
206198
0 commit comments