veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
popdata.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 ALT_INTEGRATION_INCLUDE_VERIBLOCK_ENTITIES_ALT_POP_TRANSACTION_HPP_
7#define ALT_INTEGRATION_INCLUDE_VERIBLOCK_ENTITIES_ALT_POP_TRANSACTION_HPP_
8
9#include <cstdint>
10
11#include <vector>
12#include <veriblock/pop/serde.hpp>
13#include <veriblock/pop/slice.hpp>
14
15#include "atv.hpp"
16#include "vtb.hpp"
17
18namespace altintegration {
19
27struct PopData {
28 using id_t = uint256;
29
30 uint32_t version = 1;
31 std::vector<VbkBlock> context;
32 std::vector<VTB> vtbs{};
33 std::vector<ATV> atvs{};
34
35 uint256 getMerkleRoot() const;
36
37 static uint256 getMerkleRoot(uint32_t version,
38 const std::vector<ATV::id_t>& atvs,
39 const std::vector<VTB::id_t>& vtbs,
40 const std::vector<VbkBlock::id_t>& vbks);
41
42 void mergeFrom(const PopData& p) {
43 context.insert(context.end(), p.context.begin(), p.context.end());
44 vtbs.insert(vtbs.end(), p.vtbs.begin(), p.vtbs.end());
45 atvs.insert(atvs.end(), p.atvs.begin(), p.atvs.end());
46 }
47
48 void clear() {
49 context.clear();
50 vtbs.clear();
51 atvs.clear();
52 }
53
58 void toVbkEncoding(WriteStream& stream) const;
59
64 std::vector<uint8_t> toVbkEncoding() const;
65
66 size_t estimateSize() const;
67
68 std::string toPrettyString() const;
69
70 bool empty() const { return context.empty() && atvs.empty() && vtbs.empty(); }
71
72 friend bool operator==(const PopData& a, const PopData& b) {
73 // clang-format off
74 return a.toVbkEncoding() == b.toVbkEncoding();
75 // clang-format on
76 }
77
78 // if true, then this PopData and all its contents have been statelessly
79 // checked already
80 mutable bool checked = false;
81};
82
83namespace detail {
84
85template <typename JsonValue, typename T>
86inline void putArrayOfIds(JsonValue& obj,
87 std::string key,
88 const std::vector<T>& t) {
89 JsonValue arr = json::makeEmptyArray<JsonValue>();
90 for (const auto& b : t) {
91 json::arrayPushBack(arr, ToJSON<JsonValue>(b.getId()));
92 }
93
94 json::putKV(obj, key, arr);
95}
96
97template <typename Value, typename Item>
98void putArrayKV(Value& object,
99 const std::string& key,
100 const std::vector<Item>& val) {
101 auto arr = json::makeEmptyArray<Value>();
102 for (const auto& it : val) {
103 json::arrayPushBack(arr, ToJSON<Value>(it));
104 }
105 json::putKV(object, key, arr);
106}
107
108} // namespace detail
109
111template <typename JsonValue>
112JsonValue ToJSON(const PopData& p, bool verbose = false) {
113 JsonValue obj = json::makeEmptyObject<JsonValue>();
114 json::putIntKV(obj, "version", p.version);
115 if (verbose) {
116 detail::putArrayKV(obj, "vbkblocks", p.context);
117 detail::putArrayKV(obj, "vtbs", p.vtbs);
118 detail::putArrayKV(obj, "atvs", p.atvs);
119 } else {
120 detail::putArrayOfIds(obj, "vbkblocks", p.context);
121 detail::putArrayOfIds(obj, "vtbs", p.vtbs);
122 detail::putArrayOfIds(obj, "atvs", p.atvs);
123 }
124
125 return obj;
126}
127
130 PopData& out,
131 ValidationState& state);
132
133} // namespace altintegration
134
135#endif
Class that is used for storing validation state.
Binary writer that is useful for binary serialization.
Defines logging helpers.
Definition: block.hpp:14
bool DeserializeFromVbkEncoding(ReadStream &stream, AltBlockAddon &out, ValidationState &state)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Blob< SHA256_HASH_SIZE > uint256
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: uint.hpp:24
Represents ALT block body of POP-related info.
Definition: popdata.hpp:27
void toVbkEncoding(WriteStream &stream) const
Convert PopData to data stream using Vbk byte format.
std::vector< uint8_t > toVbkEncoding() const
Convert PopData to raw bytes data using Vbk byte format.
Binary reading stream, that is useful during binary deserialization.
Definition: read_stream.hpp:22