veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
btcblock.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_BTCBLOCK_HPP_
7#define ALT_INTEGRATION_INCLUDE_VERIBLOCK_ENTITIES_BTCBLOCK_HPP_
8
9#include <algorithm>
10#include <cstddef>
11#include <cstdint>
12#include <sstream>
13#include <string>
14#include <system_error>
15#include <utility>
16#include <vector>
17
18#include "veriblock/pop/arith_uint256.hpp"
19#include "veriblock/pop/blob.hpp"
20#include "veriblock/pop/blockchain/btc_block_addon.hpp"
21#include "veriblock/pop/fmt.hpp"
22#include "veriblock/pop/hashutil.hpp"
23#include "veriblock/pop/json.hpp"
24#include "veriblock/pop/serde.hpp"
25#include "veriblock/pop/storage/stored_btc_block_addon.hpp"
26#include "veriblock/pop/strutil.hpp"
27#include "veriblock/pop/uint.hpp"
28#include "veriblock/pop/validation_state.hpp"
29
30namespace altintegration {
31class ValidationState;
32class WriteStream;
33struct ReadStream;
34
41struct BtcBlock {
42 using hash_t = uint256;
43 using prev_hash_t = uint256;
44 using height_t = int32_t;
45 using nonce_t = uint32_t;
46 using merkle_t = uint256;
47 using addon_t = StoredBtcBlockAddon::addon_t;
48 using stored_addon_t = StoredBtcBlockAddon;
49
50 BtcBlock() = default;
51 BtcBlock(int32_t version,
52 uint256 previousBlock,
53 uint256 merkleRoot,
54 uint32_t timestamp,
55 uint32_t bits,
56 uint32_t nonce);
57
62 void toRaw(WriteStream& stream) const;
63
68 void toVbkEncoding(WriteStream& stream) const;
69
70 size_t estimateSize() const;
71
72 friend bool operator==(const BtcBlock& a, const BtcBlock& b) {
73 // clang-format off
74 return a.bits == b.bits &&
75 a.version == b.version &&
76 a.timestamp == b.timestamp &&
77 a.nonce == b.nonce &&
78 a.merkleRoot == b.merkleRoot &&
79 a.previousBlock == b.previousBlock;
80 // clang-format on
81 }
82
83 friend bool operator!=(const BtcBlock& a, const BtcBlock& b) {
84 return !(a == b);
85 }
86
87 hash_t calculateHash() const;
88
93 const hash_t& getHash() const;
94
95 static const std::string& name() { return _name; }
96
97 std::string toPrettyString() const;
98
99 int32_t getVersion() const { return version; }
100 uint256 getPreviousBlock() const { return previousBlock; }
101 uint256 getMerkleRoot() const { return merkleRoot; }
102 uint32_t getNonce() const { return nonce; }
103 uint32_t getTimestamp() const { return timestamp; }
104 uint32_t getDifficulty() const { return bits; }
105
106 void setVersion(int32_t v);
107 void setPreviousBlock(const uint256& prev);
108 void setMerkleRoot(const uint256& mr);
109 void setDifficulty(uint32_t bits);
110 void setNonce(uint32_t nnc);
111 void setTimestamp(uint32_t ts);
112
113 protected:
114 static const std::string _name;
115
116 void invalidateHash() const { hash_.fill(0); }
117
118 int32_t version = 0;
119 uint256 previousBlock{};
120 uint256 merkleRoot{};
121 uint32_t timestamp = 0;
122 uint32_t bits = 0;
123 uint32_t nonce = 0;
124
125 mutable hash_t hash_;
126
127 friend bool DeserializeFromRaw(ReadStream& stream,
128 BtcBlock& block,
129 ValidationState& state,
130 const BtcBlock::hash_t& precalculatedHash);
131
133 ReadStream& stream,
134 BtcBlock& block,
135 ValidationState& state,
136 const BtcBlock::hash_t& precalculatedHash);
137};
138
140template <typename JsonValue>
141JsonValue ToJSON(const BtcBlock& b) {
142 JsonValue object = json::makeEmptyObject<JsonValue>();
143 json::putStringKV(object, "hash", HexStr(b.getHash()));
144 json::putIntKV(object, "version", b.getVersion());
145 json::putStringKV(object, "previousBlock", HexStr(b.getPreviousBlock()));
146 json::putStringKV(object, "merkleRoot", HexStr(b.getMerkleRoot()));
147 json::putIntKV(object, "timestamp", b.getTimestamp());
148 json::putIntKV(object, "bits", b.getDifficulty());
149 json::putIntKV(object, "nonce", b.getNonce());
150 return object;
151}
152
155 ReadStream& stream,
156 BtcBlock& out,
157 ValidationState& state,
158 const BtcBlock::hash_t& precalculatedHash = BtcBlock::hash_t{});
159
162 ReadStream& stream,
163 BtcBlock& out,
164 ValidationState& state,
165 const BtcBlock::hash_t& precalculatedHash = BtcBlock::hash_t{});
166
167} // namespace altintegration
168
170template <>
171struct std::hash<altintegration::BtcBlock> {
172 size_t operator()(const altintegration::BtcBlock& block) {
173 return altintegration::ArithUint256(block.getHash()).getLow64();
174 }
175};
176
177#endif // ALT_INTEGRATION_INCLUDE_VERIBLOCK_ENTITIES_BTCBLOCK_HPP_
256-bit unsigned big integer.
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
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
Bitcoin block.
Definition: btcblock.hpp:41
friend bool DeserializeFromRaw(ReadStream &stream, BtcBlock &block, ValidationState &state, const BtcBlock::hash_t &precalculatedHash)
This is an overloaded member function, provided for convenience. It differs from the above function o...
void toRaw(WriteStream &stream) const
Convert BtcBlock to data stream using BtcBlock basic byte format.
friend bool DeserializeFromVbkEncoding(ReadStream &stream, BtcBlock &block, ValidationState &state, const BtcBlock::hash_t &precalculatedHash)
This is an overloaded member function, provided for convenience. It differs from the above function o...
const hash_t & getHash() const
Calculate the hash of the btc block.
void toVbkEncoding(WriteStream &stream) const
Convert BtcBlock to data stream using BtcBlock VBK byte format.
Binary reading stream, that is useful during binary deserialization.
Definition: read_stream.hpp:22