peizhen guo | 410e0e1 | 2014-08-12 13:24:14 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California |
| 4 | * |
| 5 | * This file is part of NSL (NDN Signature Logger). |
| 6 | * See AUTHORS.md for complete list of NSL authors and contributors. |
| 7 | * |
| 8 | * NSL is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NSL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * NSL, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 19 | * See AUTHORS.md for complete list of nsl authors and contributors. |
peizhen guo | 410e0e1 | 2014-08-12 13:24:14 -0700 | [diff] [blame] | 20 | */ |
peizhen guo | 410e0e1 | 2014-08-12 13:24:14 -0700 | [diff] [blame] | 21 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 22 | #ifndef NSL_CORE_MERKLE_TREE_HPP |
| 23 | #define NSL_CORE_MERKLE_TREE_HPP |
| 24 | |
| 25 | #include "common.hpp" |
| 26 | #include "db.hpp" |
| 27 | #include "sub-tree-binary.hpp" |
peizhen guo | 410e0e1 | 2014-08-12 13:24:14 -0700 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
peizhen guo | 410e0e1 | 2014-08-12 13:24:14 -0700 | [diff] [blame] | 30 | namespace nsl { |
| 31 | |
| 32 | class MerkleTree |
| 33 | { |
| 34 | public: |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 35 | class Error : public std::runtime_error |
peizhen guo | 410e0e1 | 2014-08-12 13:24:14 -0700 | [diff] [blame] | 36 | { |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 37 | public: |
| 38 | explicit |
| 39 | Error(const std::string& what) |
| 40 | : std::runtime_error(what) |
| 41 | { |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | public: |
| 46 | /** |
| 47 | * @brief Constructor |
| 48 | */ |
| 49 | MerkleTree(Db& db); |
| 50 | |
| 51 | MerkleTree(const Name& loggerName, Db& db); |
| 52 | |
| 53 | ~MerkleTree(); |
| 54 | |
| 55 | void |
| 56 | setLoggerName(const Name& loggerName); |
| 57 | |
| 58 | const NonNegativeInteger& |
| 59 | getNextLeafSeqNo() const |
| 60 | { |
| 61 | return m_nextLeafSeqNo; |
peizhen guo | 410e0e1 | 2014-08-12 13:24:14 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 64 | const ndn::ConstBufferPtr& |
| 65 | getRootHash() const |
| 66 | { |
| 67 | return m_hash; |
| 68 | } |
peizhen guo | 410e0e1 | 2014-08-12 13:24:14 -0700 | [diff] [blame] | 69 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 70 | bool |
| 71 | addLeaf(const NonNegativeInteger& seqNo, ndn::ConstBufferPtr hash); |
peizhen guo | 410e0e1 | 2014-08-12 13:24:14 -0700 | [diff] [blame] | 72 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 73 | void |
| 74 | loadPendingSubTrees(); |
peizhen guo | 410e0e1 | 2014-08-12 13:24:14 -0700 | [diff] [blame] | 75 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 76 | void |
| 77 | savePendingTree(); |
peizhen guo | 410e0e1 | 2014-08-12 13:24:14 -0700 | [diff] [blame] | 78 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 79 | shared_ptr<Data> |
| 80 | getPendingSubTreeData(size_t level); |
peizhen guo | 410e0e1 | 2014-08-12 13:24:14 -0700 | [diff] [blame] | 81 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 82 | std::vector<ConstSubTreeBinaryPtr> |
| 83 | getExistenceProof(const NonNegativeInteger& seqNo); |
peizhen guo | 410e0e1 | 2014-08-12 13:24:14 -0700 | [diff] [blame] | 84 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 85 | std::vector<ConstSubTreeBinaryPtr> |
| 86 | getConsistencyProof(const NonNegativeInteger& seqNo); |
peizhen guo | 410e0e1 | 2014-08-12 13:24:14 -0700 | [diff] [blame] | 87 | |
| 88 | private: |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 89 | void |
| 90 | getNewRoot(const Node::Index& idx); |
peizhen guo | 410e0e1 | 2014-08-12 13:24:14 -0700 | [diff] [blame] | 91 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 92 | void |
| 93 | getNewSibling(const Node::Index& idx); |
| 94 | |
| 95 | private: |
| 96 | Name m_loggerName; |
| 97 | Db& m_db; |
| 98 | |
| 99 | shared_ptr<SubTreeBinary> m_rootSubTree; |
| 100 | NonNegativeInteger m_nextLeafSeqNo; |
| 101 | ndn::ConstBufferPtr m_hash; |
| 102 | |
| 103 | std::map<size_t, shared_ptr<SubTreeBinary>> m_pendingTrees; |
peizhen guo | 410e0e1 | 2014-08-12 13:24:14 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 106 | }// namespace nsl |
peizhen guo | 410e0e1 | 2014-08-12 13:24:14 -0700 | [diff] [blame] | 107 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 108 | #endif // NSL_CORE_MERKLE_TREE_HPP |