veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
payloads_provider_impl.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_ADAPTORS_PAYLOADS_PROVIDER_IMPL_BINDINGS
7#define VERIBLOCK_POP_CPP_ADAPTORS_PAYLOADS_PROVIDER_IMPL_BINDINGS
8
9#include <veriblock/pop/c/extern.h>
10
11#include <veriblock/pop/serde.hpp>
12#include <veriblock/pop/storage/payloads_provider.hpp>
13
14#include "storage_interface.hpp"
15
16namespace altintegration {
17
18namespace adaptors {
19
20const char DB_VBK_PREFIX = '^';
21const char DB_VTB_PREFIX = '<';
22const char DB_ATV_PREFIX = '>';
23
24template <typename pop_t>
25std::vector<uint8_t> payloads_key(const typename pop_t::id_t& id);
26
27template <>
28inline std::vector<uint8_t> payloads_key<ATV>(const ATV::id_t& id) {
29 auto res = id.asVector();
30 res.insert(res.begin(), DB_ATV_PREFIX);
31 return res;
32}
33
34template <>
35inline std::vector<uint8_t> payloads_key<VTB>(const VTB::id_t& id) {
36 auto res = id.asVector();
37 res.insert(res.begin(), DB_VTB_PREFIX);
38 return res;
39}
40
41template <>
42inline std::vector<uint8_t> payloads_key<VbkBlock>(const VbkBlock::id_t& id) {
43 auto res = id.asVector();
44 res.insert(res.begin(), DB_VBK_PREFIX);
45 return res;
46}
47
49 ~PayloadsStorageImpl() override = default;
50
51 PayloadsStorageImpl(Storage& storage) : storage_(storage) {}
52
53 template <typename pop_t>
54 bool getPayloads(const typename pop_t::id_t& id,
55 pop_t& out,
56 ValidationState& state) {
57 std::vector<uint8_t> bytes_out;
58 if (!storage_.read(payloads_key<pop_t>(id), bytes_out)) {
59 return state.Invalid(
60 "bad-" + pop_t::name(),
61 format("can not read {} from storage", pop_t::name()));
62 }
63
64 ReadStream stream(bytes_out);
65 if (!DeserializeFromVbkEncoding(stream, out, state)) {
66 return state.Invalid(
67 "bad-" + pop_t::name(),
68 format("can not deserialize {} from bytes", pop_t::name()));
69 }
70
71 return true;
72 }
73
74 bool getATV(const ATV::id_t& id, ATV& out, ValidationState& state) override {
75 return getPayloads<ATV>(id, out, state);
76 }
77
78 bool getVTB(const VTB::id_t& id, VTB& out, ValidationState& state) override {
79 return getPayloads<VTB>(id, out, state);
80 }
81
82 bool getVBK(const VbkBlock::id_t& id,
83 VbkBlock& out,
84 ValidationState& state) override {
85 return getPayloads<VbkBlock>(id, out, state);
86 }
87
88 void writePayloads(const PopData& payloads) override {
89 auto batch = storage_.generateWriteBatch();
90
91 for (const auto& atv : payloads.atvs) {
92 batch->write(payloads_key<VbkBlock>(atv.blockOfProof.getId()),
93 SerializeToVbkEncoding(atv.blockOfProof));
94
95 batch->write(payloads_key<ATV>(atv.getId()), SerializeToVbkEncoding(atv));
96 }
97
98 for (const auto& vtb : payloads.vtbs) {
99 batch->write(payloads_key<VbkBlock>(vtb.containingBlock.getId()),
100 SerializeToVbkEncoding(vtb.containingBlock));
101
102 batch->write(payloads_key<VTB>(vtb.getId()), SerializeToVbkEncoding(vtb));
103 }
104
105 for (const auto& vbk : payloads.context) {
106 batch->write(payloads_key<VbkBlock>(vbk.getId()),
108 }
109
110 batch->writeBatch();
111 }
112
113 private:
114 Storage& storage_;
115};
116
117} // namespace adaptors
118
119} // namespace altintegration
120
121#endif
Class that is used for storing validation state.
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...
std::vector< uint8_t > SerializeToVbkEncoding(const T &obj)
Serialize to VBK encoding.
Definition: serde.hpp:440
Atlchain endorsement.
Definition: atv.hpp:36
Accessor for ATV/VTB/VbkBlock bodies given hash.
Represents ALT block body of POP-related info.
Definition: popdata.hpp:27
Binary reading stream, that is useful during binary deserialization.
Definition: read_stream.hpp:22
Veriblock to Bitcoin publication, committed to Veriblock blockchain in containingBlock.
Definition: vtb.hpp:37
Veriblock block.
Definition: vbkblock.hpp:32
bool getATV(const ATV::id_t &id, ATV &out, ValidationState &state) override
should write ATV identified by id into out, or return false
bool getVBK(const VbkBlock::id_t &id, VbkBlock &out, ValidationState &state) override
should write VbkBlock identified by id into out, or return false
bool getVTB(const VTB::id_t &id, VTB &out, ValidationState &state) override
should write VTB identified by id into out, or return false