blob: f1baa46ed00a38bf2fb363f141df380574cbbc6d [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
peizhen guo410e0e12014-08-12 13:24:14 -070030namespace nsl {
31
32class MerkleTree
33{
34public:
Yingdi Yu0c3e5912015-03-17 14:22:38 -070035 class Error : public std::runtime_error
peizhen guo410e0e12014-08-12 13:24:14 -070036 {
Yingdi Yu0c3e5912015-03-17 14:22:38 -070037 public:
38 explicit
39 Error(const std::string& what)
40 : std::runtime_error(what)
41 {
42 }
43 };
44
45public:
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 guo410e0e12014-08-12 13:24:14 -070062 }
63
Yingdi Yu0c3e5912015-03-17 14:22:38 -070064 const ndn::ConstBufferPtr&
65 getRootHash() const
66 {
67 return m_hash;
68 }
peizhen guo410e0e12014-08-12 13:24:14 -070069
Yingdi Yu0c3e5912015-03-17 14:22:38 -070070 bool
71 addLeaf(const NonNegativeInteger& seqNo, ndn::ConstBufferPtr hash);
peizhen guo410e0e12014-08-12 13:24:14 -070072
Yingdi Yu0c3e5912015-03-17 14:22:38 -070073 void
74 loadPendingSubTrees();
peizhen guo410e0e12014-08-12 13:24:14 -070075
Yingdi Yu0c3e5912015-03-17 14:22:38 -070076 void
77 savePendingTree();
peizhen guo410e0e12014-08-12 13:24:14 -070078
Yingdi Yu0c3e5912015-03-17 14:22:38 -070079 shared_ptr<Data>
80 getPendingSubTreeData(size_t level);
peizhen guo410e0e12014-08-12 13:24:14 -070081
Yingdi Yu0c3e5912015-03-17 14:22:38 -070082 std::vector<ConstSubTreeBinaryPtr>
83 getExistenceProof(const NonNegativeInteger& seqNo);
peizhen guo410e0e12014-08-12 13:24:14 -070084
Yingdi Yu0c3e5912015-03-17 14:22:38 -070085 std::vector<ConstSubTreeBinaryPtr>
86 getConsistencyProof(const NonNegativeInteger& seqNo);
peizhen guo410e0e12014-08-12 13:24:14 -070087
88private:
Yingdi Yu0c3e5912015-03-17 14:22:38 -070089 void
90 getNewRoot(const Node::Index& idx);
peizhen guo410e0e12014-08-12 13:24:14 -070091
Yingdi Yu0c3e5912015-03-17 14:22:38 -070092 void
93 getNewSibling(const Node::Index& idx);
94
95private:
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 guo410e0e12014-08-12 13:24:14 -0700104};
105
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700106}// namespace nsl
peizhen guo410e0e12014-08-12 13:24:14 -0700107
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -0700108#endif // NDN_DELOREAN_CORE_MERKLE_TREE_HPP