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