veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
vbkblock.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_VBKBLOCK_HPP_
7#define ALT_INTEGRATION_INCLUDE_VERIBLOCK_ENTITIES_VBKBLOCK_HPP_
8
9#include <cstdint>
10#include <string>
11#include <utility>
12#include <vector>
13#include <veriblock/pop/arith_uint256.hpp>
14#include <veriblock/pop/blockchain/vbk_block_addon.hpp>
15#include <veriblock/pop/fmt.hpp>
16#include <veriblock/pop/serde.hpp>
17#include <veriblock/pop/storage/stored_vbk_block_addon.hpp>
18#include <veriblock/pop/type_traits.hpp>
19#include <veriblock/pop/uint.hpp>
20
21#include "btcblock.hpp"
22
23namespace altintegration {
24
25struct VbkChainParams;
26
32struct VbkBlock {
33 using hash_t = uint192;
34 using short_hash_t = uint96;
35 using id_t = short_hash_t;
37 using height_t = int32_t;
38 using nonce_t = int32_t;
39 using keystone_t = uint72;
40 using merkle_t = uint128;
42 using addon_t = StoredVbkBlockAddon::addon_t;
43 using stored_addon_t = StoredVbkBlockAddon;
44
45 std::string toPrettyString() const;
46 std::string toShortPrettyString() const;
47
52 void toRaw(WriteStream& stream) const;
53
58 std::vector<uint8_t> toVbkEncoding() const;
59
64 std::vector<uint8_t> toRaw() const;
65
70 void toVbkEncoding(WriteStream& stream) const;
71
72 friend bool operator<(const VbkBlock& a, const VbkBlock& b) {
73 return a.getHeight() < b.getHeight();
74 }
75
76 size_t estimateSize() const;
77
78 /*
79 * Getter for timestamp
80 * @return block timestamp
81 */
82 uint32_t getBlockTime() const;
83
84 friend bool operator==(const VbkBlock& a, const VbkBlock& b);
85 friend bool operator!=(const VbkBlock& a, const VbkBlock& b);
86
87 hash_t calculateHash() const;
88
93 const hash_t& getHash() const;
94
100
101 short_hash_t getId() const { return getShortHash(); }
102
103 static const std::string& name() { return _name; }
104
105 VbkBlock() = default;
106 VbkBlock(int32_t h,
107 int16_t v,
108 uint96 prevBlock,
109 keystone_t prev1,
110 keystone_t prev2,
111 uint128 mroot,
112 int32_t ts,
113 int32_t diff,
114 uint64_t nonce);
115
116 int32_t getDifficulty() const { return difficulty; }
117 int32_t getHeight() const { return height; }
118 int16_t getVersion() const { return version; }
119 uint96 getPreviousBlock() const { return previousBlock; }
120 keystone_t getPreviousKeystone() const { return previousKeystone; }
121 keystone_t getSecondPreviousKeystone() const {
122 return secondPreviousKeystone;
123 }
124 uint128 getMerkleRoot() const { return merkleRoot; }
125 uint32_t getTimestamp() const { return timestamp; }
126 uint64_t getNonce() const { return nonce; }
127
128 void setHeight(int32_t h);
129 void setVersion(int16_t v);
130 void setPreviousBlock(const uint96& prev);
131 void setPreviousKeystone(const keystone_t& ks);
132 void setSecondPreviousKeystone(const keystone_t& ks);
133 void setMerkleRoot(const uint128& mroot);
134 void setTimestamp(uint32_t ts);
135 void setDifficulty(int32_t diff);
136 void setNonce(uint64_t nnc);
137
138 private:
139 static const std::string _name;
140
141 int32_t height{};
142 int16_t version{};
143 uint96 previousBlock{};
144 keystone_t previousKeystone{};
145 keystone_t secondPreviousKeystone{};
146 uint128 merkleRoot{};
147 uint32_t timestamp{};
148 int32_t difficulty{};
149 uint64_t nonce{};
150 mutable hash_t hash_{};
151
152 void invalidateHash() { hash_.fill(0); }
153 friend bool DeserializeFromRaw(ReadStream& stream,
154 VbkBlock& out,
155 ValidationState& state,
156 const VbkBlock::hash_t& precalculatedHash);
157
158 friend void setPrecalculatedHash(VbkBlock& block,
159 const VbkBlock::hash_t& precalculatedHash);
160};
161
163template <>
164struct IsPopPayload<VbkBlock> {
165 static const bool value = true;
166};
167
169template <typename JsonValue>
170JsonValue ToJSON(const VbkBlock& b) {
171 JsonValue obj = json::makeEmptyObject<JsonValue>();
172 json::putStringKV(obj, "hash", HexStr(b.getHash()));
173 json::putIntKV(obj, "height", b.getHeight());
174 json::putIntKV(obj, "version", b.getVersion());
175 json::putStringKV(obj, "previousBlock", HexStr(b.getPreviousBlock()));
176 json::putStringKV(obj, "previousKeystone", HexStr(b.getPreviousKeystone()));
177 json::putStringKV(
178 obj, "secondPreviousKeystone", HexStr(b.getSecondPreviousKeystone()));
179 json::putStringKV(obj, "merkleRoot", HexStr(b.getMerkleRoot()));
180 json::putIntKV(obj, "timestamp", b.getTimestamp());
181 json::putIntKV(obj, "difficulty", b.getDifficulty());
182 json::putIntKV(obj, "nonce", b.getNonce());
183
184 // return this entity in VBK-serialized form for easy consumption.
185 // DO NOT REMOVE these fields - otherwise Stratum compat will break.
186 json::putStringKV(obj, "id", HexStr(b.getId()));
187 json::putStringKV(obj, "serialized", SerializeToHex(b));
188 return obj;
189}
190
193 VbkBlock& out,
194 ValidationState& state,
195 const VbkBlock::hash_t& hash = VbkBlock::hash_t{});
196
199 ReadStream& stream,
200 VbkBlock& out,
201 ValidationState& state,
202 const VbkBlock::hash_t& hash = VbkBlock::hash_t{});
203
204} // namespace altintegration
205
206#endif // ALT_INTEGRATION_INCLUDE_VERIBLOCK_ENTITIES_VBKBLOCK_HPP_
Class that is used for storing validation state.
Binary writer that is useful for binary serialization.
Defines logging helpers.
Definition: block.hpp:14
Blob< VBK_PREVIOUS_BLOCK_HASH_SIZE > uint96
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: uint.hpp:28
Blob< VBK_PREVIOUS_KEYSTONE_HASH_SIZE > uint72
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: uint.hpp:26
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...
Blob< VBK_BLOCK_HASH_SIZE > uint192
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: uint.hpp:30
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
std::string SerializeToHex(const T &obj)
Serialize to HEX VBK encoding.
Definition: serde.hpp:456
Blob< VBK_MERKLE_ROOT_HASH_SIZE > uint128
Fixed-size array.
Definition: uint.hpp:22
Contiguous byte array of fixed size.
Definition: blob.hpp:25
Bitcoin block.
Definition: btcblock.hpp:41
type trait which returns true on types that are "POP Payloads"
Definition: type_traits.hpp:15
Binary reading stream, that is useful during binary deserialization.
Definition: read_stream.hpp:22
Veriblock block.
Definition: vbkblock.hpp:32
short_hash_t getShortHash() const
Calculate the hash of the vbk block.
friend bool DeserializeFromRaw(ReadStream &stream, VbkBlock &out, ValidationState &state, const VbkBlock::hash_t &precalculatedHash)
This is an overloaded member function, provided for convenience. It differs from the above function o...
const hash_t & getHash() const
Get current block hash.
std::vector< uint8_t > toRaw() const
Convert VbkBlock to bytes data using VbkBlock basic byte format.
std::vector< uint8_t > toVbkEncoding() const
Convert VbkBlock to raw bytes data using VbkBlock byte format.
void toVbkEncoding(WriteStream &stream) const
Convert VbkBlock to data stream using VbkBlock VBK byte format.
void toRaw(WriteStream &stream) const
Convert VbkBlock to data stream using VbkBlock basic byte format.