blob: 3e877f16c210a4098595e1d7a413e70a390270c1 [file] [log] [blame]
peizhen guo410e0e12014-08-12 13:24:14 -07001/* -*- 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 Yu0c3e5912015-03-17 14:22:38 -070019 * See AUTHORS.md for complete list of nsl authors and contributors.
peizhen guo410e0e12014-08-12 13:24:14 -070020 */
peizhen guo410e0e12014-08-12 13:24:14 -070021
Yingdi Yu0c3e5912015-03-17 14:22:38 -070022#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 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
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700108#endif // NSL_CORE_MERKLE_TREE_HPP