veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
stored_block_index.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_STORAGE_STORED_BLOCK_INDEX_HPP_
7#define ALT_INTEGRATION_INCLUDE_VERIBLOCK_STORAGE_STORED_BLOCK_INDEX_HPP_
8
9#include <veriblock/pop/blockchain/block_status.hpp>
10#include <veriblock/pop/read_stream.hpp>
11#include <veriblock/pop/write_stream.hpp>
12
13namespace altintegration {
14
15struct AltChainParams;
16
17template <typename Block>
19 using block_t = Block;
20 using height_t = typename block_t::height_t;
21 using stored_addon_t = typename block_t::stored_addon_t;
22
23 ~StoredBlockIndex() = default;
24
25 bool isDeleted() const { return this->status & BLOCK_DELETED; };
26
27 void toVbkEncoding(WriteStream& stream) const {
28 using height_t = typename Block::height_t;
29 stream.writeBE<height_t>(height);
30 header->toRaw(stream);
31 stream.writeBE<uint32_t>(status);
32 addon.toVbkEncoding(stream);
33 }
34
35 std::vector<uint8_t> toVbkEncoding() const {
36 WriteStream stream;
37 toVbkEncoding(stream);
38 return stream.data();
39 }
40
41 std::string toPrettyString(size_t level = 0) const {
42 return format(
43 "{}{}StoredBlockIndex(height={}, hash={}, status={}, header={}, {})",
44 std::string(level, ' '),
45 Block::name(),
46 height,
47 HexStr(header->getHash()),
48 status,
49 header->toPrettyString(),
50 addon.toPrettyString());
51 }
52
54 height_t height = 0;
55
57 std::shared_ptr<block_t> header = std::make_shared<block_t>();
58
61
62 stored_addon_t addon;
63};
64
65template <typename Block>
67 ReadStream& stream,
69 ValidationState& state,
70 typename Block::hash_t precalculatedHash = typename Block::hash_t()) {
71 const auto& name = Block::name();
72 using height_t = typename Block::height_t;
73 if (!stream.readBE<height_t>(out.height, state)) {
74 return state.Invalid(name + "-stored-block-index-height");
75 }
76 Block block{};
77 if (!DeserializeFromRaw(stream, block, state, precalculatedHash)) {
78 return state.Invalid(name + "-stored-block-index-header");
79 }
80 out.header = std::make_shared<Block>(block);
81 if (!stream.readBE<uint32_t>(out.status, state)) {
82 return state.Invalid(name + "-stored-block-index-status");
83 }
84
85 if (!DeserializeFromVbkEncoding(stream, out.addon, state)) {
86 return state.Invalid(name + "-stored-block-index-addon");
87 }
88 return true;
89}
90
91} // namespace altintegration
92#endif // ALT_INTEGRATION_INCLUDE_VERIBLOCK_STORAGE_STORED_BLOCK_INDEX_HPP_
Class that is used for storing validation state.
Binary writer that is useful for binary serialization.
Defines logging helpers.
Definition: block.hpp:14
bool DeserializeFromRaw(ReadStream &stream, AltBlock &out, ValidationState &state, const AltBlock::hash_t &=AltBlock::hash_t{})
This is an overloaded member function, provided for convenience. It differs from the above function o...
@ BLOCK_VALID_UNKNOWN
default state for validity - validity state is unknown
@ BLOCK_DELETED
the block is temporarily deleted
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::string HexStr(const T itbegin, const T itend)
Convert bytes to hex.
Definition: strutil.hpp:44
Binary reading stream, that is useful during binary deserialization.
Definition: read_stream.hpp:22
uint32_t status
contains status flags
std::shared_ptr< block_t > header
block header
height_t height
height of the entry in the chain