This repository was archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 289
Split out VerifiableMap from SparseMerkleTree. #1061
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| #include <array> | ||
| #include <string> | ||
|
|
||
| #include "merkletree/verifiable_map.h" | ||
|
|
||
|
|
||
| using std::string; | ||
| using std::unique_ptr; | ||
| using std::vector; | ||
| using util::Status; | ||
| using util::StatusOr; | ||
|
|
||
|
|
||
| namespace cert_trans { | ||
|
|
||
|
|
||
| VerifiableMap::VerifiableMap(SerialHasher* hasher) | ||
| : hasher_model_(CHECK_NOTNULL(hasher)->Create()), merkle_tree_(hasher) { | ||
| } | ||
|
|
||
|
|
||
| void VerifiableMap::Set(const string& key, const string& value) { | ||
| const SparseMerkleTree::Path path(PathFromKey(key)); | ||
| merkle_tree_.SetLeaf(path, value); | ||
| values_[path] = value; | ||
| } | ||
|
|
||
|
|
||
| StatusOr<string> VerifiableMap::Get(const string& key) const { | ||
| const SparseMerkleTree::Path path(PathFromKey(key)); | ||
| const auto it(values_.find(path)); | ||
| if (it == values_.end()) { | ||
| return Status(util::error::NOT_FOUND, "No such entry."); | ||
| } | ||
| return it->second; | ||
| } | ||
|
|
||
|
|
||
| vector<string> VerifiableMap::InclusionProof(const string& key) { | ||
| return merkle_tree_.InclusionProof(PathFromKey(key)); | ||
| } | ||
|
|
||
|
|
||
| SparseMerkleTree::Path VerifiableMap::PathFromKey(const string& key) const { | ||
| unique_ptr<SerialHasher> h(hasher_model_->Create()); | ||
| h->Update(key); | ||
| return PathFromBytes(h->Final()); | ||
| } | ||
|
|
||
| } // namespace cert_trans |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| #ifndef CERT_TRANS_MERKLETREE_VERIFIABLE_MAP_H_ | ||
| #define CERT_TRANS_MERKLETREE_VERIFIABLE_MAP_H_ | ||
|
|
||
| #include <string> | ||
| #include <unordered_map> | ||
| #include <vector> | ||
|
|
||
| #include "base/macros.h" | ||
| #include "merkletree/sparse_merkle_tree.h" | ||
| #include "util/statusor.h" | ||
|
|
||
| namespace cert_trans { | ||
|
|
||
|
|
||
| // Implements a Verifiable Map using a SparseMerkleTree and hashmap. | ||
| class VerifiableMap { | ||
| public: | ||
| VerifiableMap(SerialHasher* hasher); | ||
|
|
||
| std::string CurrentRoot() { | ||
| return merkle_tree_.CurrentRoot(); | ||
| } | ||
|
|
||
| void Set(const std::string& key, const std::string& value); | ||
|
|
||
| util::StatusOr<std::string> Get(const std::string& key) const; | ||
|
|
||
| std::vector<std::string> InclusionProof(const std::string& key); | ||
|
|
||
| private: | ||
| SparseMerkleTree::Path PathFromKey(const std::string& key) const; | ||
|
|
||
| std::unique_ptr<SerialHasher> hasher_model_; | ||
| SparseMerkleTree merkle_tree_; | ||
|
|
||
| // TODO(alcutter): allow arbitrary stores here. | ||
| std::unordered_map<SparseMerkleTree::Path, std::string, PathHasher> values_; | ||
|
|
||
| DISALLOW_COPY_AND_ASSIGN(VerifiableMap); | ||
| }; | ||
|
|
||
|
|
||
| } // namespace cert_trans | ||
|
|
||
| #endif // CERT_TRANS_MERKLETREE_VERIFIABLE_MAP_H_ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could ding the
const.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is because you get back a
const T*from thestd::array::data(), so I'd need to activelyconst_castit away.