veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
altblock.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_ALTBLOCK_HPP_
7#define ALT_INTEGRATION_INCLUDE_VERIBLOCK_ENTITIES_ALTBLOCK_HPP_
8
9#include <cstdint>
10#include <string>
11#include <vector>
12#include <veriblock/pop/blockchain/alt_block_addon.hpp>
13#include <veriblock/pop/blockchain/block_index.hpp>
14#include <veriblock/pop/hashers.hpp>
15#include <veriblock/pop/json.hpp>
16#include <veriblock/pop/serde.hpp>
17#include <veriblock/pop/storage/stored_alt_block_addon.hpp>
18
19namespace altintegration {
20
21struct VbkBlock;
22struct AltChainParams;
23
29struct AltBlock {
30 using height_t = int32_t;
31 using hash_t = std::vector<uint8_t>;
32 using prev_hash_t = std::vector<uint8_t>;
33 using addon_t = StoredAltBlockAddon::addon_t;
34 using stored_addon_t = StoredAltBlockAddon;
35
40 void toRaw(WriteStream& stream) const;
41
43 void toVbkEncoding(WriteStream& stream) const;
44
49 std::vector<uint8_t> toRaw() const;
50
51 size_t estimateSize() const;
52
53 uint32_t getBlockTime() const noexcept;
54
58 const hash_t& getHash() const;
59
60 // dummy
61 uint32_t getDifficulty() const { return 0; }
62
63 friend bool operator==(const AltBlock& a, const AltBlock& b);
64 friend bool operator!=(const AltBlock& a, const AltBlock& b);
65
67 static const std::string& name() { return _name; }
68
73 std::string toPrettyString() const;
74
75 hash_t getPreviousBlock() const { return previousBlock; }
76 uint32_t getTimestamp() const { return timestamp; }
77 height_t getHeight() const { return height; }
78
79 hash_t hash{};
80 hash_t previousBlock{};
81 uint32_t timestamp{};
82 height_t height{};
83
84 private:
85 static const std::string _name;
86};
87
89template <typename JsonValue>
90JsonValue ToJSON(const AltBlock& alt, bool reverseHashes = true) {
91 JsonValue object = json::makeEmptyObject<JsonValue>();
92 json::putStringKV(object, "hash", HexStr(alt.getHash(), reverseHashes));
93 json::putStringKV(
94 object, "previousBlock", HexStr(alt.previousBlock, reverseHashes));
95 json::putIntKV(object, "timestamp", alt.getBlockTime());
96 json::putIntKV(object, "height", alt.height);
97 return object;
98}
99
101inline void PrintTo(const AltBlock& block, ::std::ostream* os) {
102 *os << block.toPrettyString();
103}
104
107 ReadStream& stream,
108 AltBlock& out,
109 ValidationState& state,
110 const AltBlock::hash_t& /* ignore */ = AltBlock::hash_t{});
111
114 ReadStream& stream,
115 AltBlock& out,
116 ValidationState& state,
117 const AltBlock::hash_t& /* ignore */ = AltBlock::hash_t{});
118
119} // namespace altintegration
120
122template <>
123struct std::hash<altintegration::AltBlock> {
124 size_t operator()(const altintegration::AltBlock& block) {
125 std::hash<std::vector<uint8_t>> hasher;
126 return hasher(block.getHash());
127 }
128};
129
130#endif // ALT_INTEGRATION_INCLUDE_VERIBLOCK_ENTITIES_ALTBLOCK_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...
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
void PrintTo(const ArithUint256 &uint, ::std::ostream *os)
custom gtest printer, which prints Blob of any size as hexstring @ private
Represents a view on Altchain block - i.e.
Definition: altblock.hpp:29
std::vector< uint8_t > toRaw() const
Convert AltBlock to bytes data using AltBlock basic byte format.
void toRaw(WriteStream &stream) const
Convert AltBlock to data stream using AltBlock basic byte format.
std::string toPrettyString() const
Print this entity.
const hash_t & getHash() const
void toVbkEncoding(WriteStream &stream) const
for AltBlock Raw format == VbkEncoding
Binary reading stream, that is useful during binary deserialization.
Definition: read_stream.hpp:22