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 | |
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 | |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame^] | 108 | #endif // NDN_DELOREAN_CORE_MERKLE_TREE_HPP |