veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
payloads_index.hpp
1#ifndef DE6BAACA_91D4_4C99_B0C5_F1AB318E58B0
2#define DE6BAACA_91D4_4C99_B0C5_F1AB318E58B0
3
4#include <cstdint>
5#include <unordered_map>
6#include <vector>
7#include <veriblock/pop/assert.hpp>
8
9#include "payloads_index_detail.hpp"
10
11namespace altintegration {
12
16template <typename ContainerIndex>
18 using index_t = ContainerIndex;
19 using hash_t = typename ContainerIndex::hash_t;
20 using payload_id = std::vector<uint8_t>;
21
22 const hash_t* find(const payload_id& pid) const {
23 auto it = map_.find(pid);
24 if (it == map_.end()) {
25 return nullptr;
26 }
27
28 return &it->second;
29 }
30
31 bool empty() const { return map_.empty(); }
32
33 size_t size() const { return map_.size(); }
34
35 void add(const payload_id& pid, const hash_t& hash) {
36 auto it = map_.find(pid);
37 VBK_ASSERT(it == map_.end());
38 map_.insert(std::make_pair(pid, hash));
39 }
40
41 void remove(const payload_id& pid) {
42 auto it = map_.find(pid);
43 VBK_ASSERT(it != map_.end());
44 map_.erase(pid);
45 }
46
47 void addBlock(const index_t& block) {
48 // in FinalizedPayloadsIndex we can add only finalized payloads!
49 VBK_ASSERT_MSG(block.finalized, block.toPrettyString());
50 detail::PLIAddBlock(*this, block);
51 }
52
53 const std::unordered_map<payload_id, hash_t>& getAll() const { return map_; }
54
55 private:
56 std::unordered_map<payload_id, hash_t> map_;
57};
58
62template <typename IndexT>
64 using index_t = IndexT;
65 using hash_t = typename index_t::hash_t;
66 using payload_id = std::vector<uint8_t>;
67
68 void addBlock(const index_t& block) {
69 // in PayloadsIndex we can add only NON-FINALIZED blocks!
70 VBK_ASSERT_MSG(!block.finalized, block.toPrettyString());
71 detail::PLIAddBlock(*this, block);
72 }
73
74 void removeBlock(const index_t& block) {
75 // when blocks are removed from PayloadsIndex they are mostly non finalized.
76 // but there's an edge case: when we deallocate entire Tree (in destructor)
77 // we can remove finalized blocks.
78 detail::PLIRemoveBlock(*this, block);
79 }
80
81 void add(const payload_id& id, const hash_t& block) {
82 map_[id].insert(block);
83 }
84
85 void remove(const payload_id& id, const hash_t& block) {
86 auto it = map_.find(id);
87 if (it == map_.end()) {
88 // not found
89 return;
90 }
91
92 auto& set = it->second;
93 size_t erased = set.erase(block);
94
95 if (set.empty()) {
96 // "value" set is empty, we can cleanup key
97 erased = map_.erase(id);
98 VBK_ASSERT(erased == 1);
99 }
100 }
101
102 const std::set<hash_t>& find(const payload_id& id) const {
103 static std::set<hash_t> empty;
104 auto it = map_.find(id);
105 if (it == map_.end()) {
106 return empty;
107 }
108
109 return it->second;
110 }
111
112 const std::unordered_map<payload_id, std::set<hash_t>>& getAll() const {
113 return map_;
114 }
115
116 private:
117 std::unordered_map<payload_id, std::set<hash_t>> map_;
118};
119
120} // namespace altintegration
121
122#endif /* DE6BAACA_91D4_4C99_B0C5_F1AB318E58B0 */
Defines logging helpers.
Definition: block.hpp:14
Stores a mapping "payload id -> containing block" hash of payloads that are stored in finalized block...
Payloads index that stores mapping "payload id -> set of containing blocks" from all NON-FINALIZED bl...