blob: c388fe93534799461e5b70dc73d5c5e15308e03b [file] [log] [blame]
peizhen guo410e0e12014-08-12 13:24:14 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07003 * Copyright (c) 2014-2017, Regents of the University of California
peizhen guo410e0e12014-08-12 13:24:14 -07004 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07005 * 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 guo410e0e12014-08-12 13:24:14 -07008 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07009 * 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 guo410e0e12014-08-12 13:24:14 -070013 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070014 * 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 guo410e0e12014-08-12 13:24:14 -070017 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070018 * 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 guo410e0e12014-08-12 13:24:14 -070020 */
peizhen guo410e0e12014-08-12 13:24:14 -070021
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070022#ifndef NDN_DELOREAN_CORE_MERKLE_TREE_HPP
23#define NDN_DELOREAN_CORE_MERKLE_TREE_HPP
Yingdi Yu0c3e5912015-03-17 14:22:38 -070024
25#include "common.hpp"
26#include "db.hpp"
27#include "sub-tree-binary.hpp"
peizhen guo410e0e12014-08-12 13:24:14 -070028#include <vector>
29
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -070030namespace ndn {
31namespace delorean {
peizhen guo410e0e12014-08-12 13:24:14 -070032
33class MerkleTree
34{
35public:
Yingdi Yu0c3e5912015-03-17 14:22:38 -070036 class Error : public std::runtime_error
peizhen guo410e0e12014-08-12 13:24:14 -070037 {
Yingdi Yu0c3e5912015-03-17 14:22:38 -070038 public:
39 explicit
40 Error(const std::string& what)
41 : std::runtime_error(what)
42 {
43 }
44 };
45
46public:
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 guo410e0e12014-08-12 13:24:14 -070063 }
64
Yingdi Yu0c3e5912015-03-17 14:22:38 -070065 const ndn::ConstBufferPtr&
66 getRootHash() const
67 {
68 return m_hash;
69 }
peizhen guo410e0e12014-08-12 13:24:14 -070070
Yingdi Yu0c3e5912015-03-17 14:22:38 -070071 bool
72 addLeaf(const NonNegativeInteger& seqNo, ndn::ConstBufferPtr hash);
peizhen guo410e0e12014-08-12 13:24:14 -070073
Yingdi Yu0c3e5912015-03-17 14:22:38 -070074 void
75 loadPendingSubTrees();
peizhen guo410e0e12014-08-12 13:24:14 -070076
Yingdi Yu0c3e5912015-03-17 14:22:38 -070077 void
78 savePendingTree();
peizhen guo410e0e12014-08-12 13:24:14 -070079
Yingdi Yu0c3e5912015-03-17 14:22:38 -070080 shared_ptr<Data>
81 getPendingSubTreeData(size_t level);
peizhen guo410e0e12014-08-12 13:24:14 -070082
Yingdi Yu0c3e5912015-03-17 14:22:38 -070083 std::vector<ConstSubTreeBinaryPtr>
84 getExistenceProof(const NonNegativeInteger& seqNo);
peizhen guo410e0e12014-08-12 13:24:14 -070085
Yingdi Yu0c3e5912015-03-17 14:22:38 -070086 std::vector<ConstSubTreeBinaryPtr>
87 getConsistencyProof(const NonNegativeInteger& seqNo);
peizhen guo410e0e12014-08-12 13:24:14 -070088
89private:
Yingdi Yu0c3e5912015-03-17 14:22:38 -070090 void
91 getNewRoot(const Node::Index& idx);
peizhen guo410e0e12014-08-12 13:24:14 -070092
Yingdi Yu0c3e5912015-03-17 14:22:38 -070093 void
94 getNewSibling(const Node::Index& idx);
95
96private:
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 guo410e0e12014-08-12 13:24:14 -0700105};
106
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -0700107} // namespace delorean
108} // namespace ndn
peizhen guo410e0e12014-08-12 13:24:14 -0700109
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -0700110#endif // NDN_DELOREAN_CORE_MERKLE_TREE_HPP