veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
pop_state.hpp
1// Copyright (c) 2019-2022 Xenios SEZC
2// https://www.veriblock.org
3// Distributed under the MIT software license, see the accompanying
4// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef VERIBLOCK_POP_CPP_POP_STATE_HPP
7#define VERIBLOCK_POP_CPP_POP_STATE_HPP
8
9#include <map>
10#include <memory>
11#include <set>
12#include <vector>
13#include <veriblock/pop/algorithm.hpp>
14#include <veriblock/pop/serde.hpp>
15#include <veriblock/pop/uint.hpp>
16
17namespace altintegration {
18
20template <typename EndorsementT>
21struct PopState {
22 using endorsement_t = EndorsementT;
23 using eid_t = typename endorsement_t::id_t;
24 using containing_endorsement_store_t =
25 std::multimap<eid_t, std::shared_ptr<endorsement_t>>;
26
27 const containing_endorsement_store_t& getContainingEndorsements() const {
28 return _containingEndorsements;
29 }
30
31 const std::vector<const endorsement_t*>& getEndorsedBy() const {
32 return _endorsedBy;
33 }
34
35 void insertEndorsedBy(const endorsement_t* e) {
36 VBK_ASSERT_MSG(e != nullptr, "Inserted endorsement should not be nullptr");
37 _endorsedBy.push_back(e);
38 setDirty();
39 }
40
41 bool eraseLastFromEndorsedBy(const endorsement_t* endorsement) {
42 auto rm = [&endorsement](const endorsement_t* e) -> bool {
43 return e == endorsement;
44 };
45 auto res = erase_last_item_if<const endorsement_t*>(_endorsedBy, rm);
46 if (res) {
47 setDirty();
48 }
49 return res;
50 }
51
52 void insertContainingEndorsement(std::shared_ptr<endorsement_t> e) {
53 VBK_ASSERT_MSG(e != nullptr, "Inserted endorsement should not be nullptr");
54 _containingEndorsements.emplace(e->id, std::move(e));
55 setDirty();
56 }
57
58 const typename containing_endorsement_store_t::const_iterator
59 findContainingEndorsement(const eid_t& id) const {
60 return _containingEndorsements.lower_bound(id);
61 }
62
63 void removeContainingEndorsement(
64 const typename containing_endorsement_store_t::const_iterator it) {
65 _containingEndorsements.erase(it);
66 setDirty();
67 }
68
69 void toVbkEncoding(WriteStream& stream) const {
70 // write containingEndorsements as vector
71 using value_t = typename decltype(_containingEndorsements)::value_type;
72 writeContainer<decltype(_containingEndorsements)>(
73 stream,
74 _containingEndorsements,
75 [&](WriteStream&, const value_t& endorsement) {
76 endorsement.second->toVbkEncoding(stream);
77 });
78 }
79
80 // hide setters from public usage
81 protected:
83 containing_endorsement_store_t _containingEndorsements{};
84
86 // must be a vector, because we can have duplicates here
87 std::vector<const endorsement_t*> _endorsedBy;
88
89 void setDirty();
90
91 void setNull() {
92 _containingEndorsements.clear();
93 _endorsedBy.clear();
94 }
95
96 template <typename T>
97 friend bool DeserializeFromVbkEncoding(ReadStream& stream,
98 PopState<T>& out,
99 ValidationState& state);
100};
101
103template <typename T>
105 PopState<T>& out,
106 ValidationState& state) {
107 std::vector<T> endorsements;
108 auto max = std::max(MAX_POPDATA_ATV, MAX_POPDATA_VTB);
109 if (!readArrayOf<T>(
110 stream,
111 endorsements,
112 state,
113 0,
114 max,
115 [](ReadStream& stream, T& t, ValidationState& state) -> bool {
116 return DeserializeFromVbkEncoding(stream, t, state);
117 })) {
118 return state.Invalid("popstate-bad-endorsement");
119 }
120
121 for (const auto& endorsement : endorsements) {
122 auto it = out._containingEndorsements.emplace(
123 endorsement.getId(), std::make_shared<T>(endorsement));
124 // newly created endorsement must not be null
125 VBK_ASSERT(it->second);
126 }
127 // do not restore 'endorsedBy' here, it will be done later during tree
128 // loading
129 return true;
130}
131
132} // namespace altintegration
133
134#endif // VERIBLOCK_POP_CPP_POP_STATE_HPP
Class that is used for storing validation state.
Defines logging helpers.
Definition: block.hpp:14
constexpr const auto MAX_POPDATA_ATV
absolute maximum number of ATV blocks per ALT block
Definition: consts.hpp:82
bool DeserializeFromVbkEncoding(ReadStream &stream, AltBlockAddon &out, ValidationState &state)
This is an overloaded member function, provided for convenience. It differs from the above function o...
constexpr const auto MAX_POPDATA_VTB
absolute maximum number of VTB blocks per ALT block
Definition: consts.hpp:80
Binary reading stream, that is useful during binary deserialization.
Definition: read_stream.hpp:22