veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
vtb.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_VTB_HPP_
7#define ALT_INTEGRATION_INCLUDE_VERIBLOCK_ENTITIES_VTB_HPP_
8
9#include <cstddef>
10#include <cstdint>
11#include <string>
12#include <vector>
13#include <veriblock/pop/fmt.hpp>
14#include <veriblock/pop/json.hpp>
15#include <veriblock/pop/serde.hpp>
16#include <veriblock/pop/uint.hpp>
17
18#include "vbk_merkle_path.hpp"
19#include "vbkblock.hpp"
20#include "vbkpoptx.hpp"
21#include "veriblock/pop/blob.hpp"
22
23namespace altintegration {
24class ValidationState;
25class WriteStream;
26struct ReadStream;
27template <typename T>
28struct IsPopPayload;
29
37struct VTB {
38 using id_t = uint256;
40
41 uint32_t version = 1;
42 VbkPopTx transaction{};
43 VbkMerklePath merklePath{};
44 containing_block_t containingBlock{};
45
47 mutable bool checked{false};
48
49 std::string toPrettyString() const;
50 std::string toShortPrettyString() const;
51
56 void toVbkEncoding(WriteStream& stream) const;
57
62 std::vector<uint8_t> toVbkEncoding() const;
63
64 size_t estimateSize() const;
65
70 id_t getId() const;
71
72 static const std::string& name() { return _name; }
73
74 friend bool operator==(const VTB& a, const VTB& b) {
75 return a.getId() == b.getId();
76 }
77
78 friend bool operator!=(const VTB& a, const VTB& b) { return !(a == b); }
79
80 friend bool operator<(const VTB& a, const VTB& b) {
81 return a.containingBlock.getHeight() < b.containingBlock.getHeight();
82 }
83
84 private:
85 static const std::string _name;
86};
87
89template <>
90struct IsPopPayload<VTB> {
91 static const bool value = true;
92};
93
95template <typename JsonValue>
96JsonValue ToJSON(const VTB& v) {
97 JsonValue obj = json::makeEmptyObject<JsonValue>();
98 json::putIntKV(obj, "version", v.version);
99 json::putKV(obj, "transaction", ToJSON<JsonValue>(v.transaction));
100 json::putKV(obj, "merklePath", ToJSON<JsonValue>(v.merklePath));
101 json::putKV(obj, "containingBlock", ToJSON<JsonValue>(v.containingBlock));
102
103 // return this entity in VBK-serialized form for easy consumption.
104 // DO NOT REMOVE these fields - otherwise Stratum compat will break.
105 json::putStringKV(obj, "id", v.getId().toHex());
106 json::putStringKV(obj, "serialized", SerializeToHex(v));
107 return obj;
108}
109
112 VTB& out,
113 ValidationState& state);
114
115} // namespace altintegration
116
117#endif // ALT_INTEGRATION_INCLUDE_VERIBLOCK_ENTITIES_VTB_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 DeserializeFromVbkEncoding(ReadStream &stream, AltBlockAddon &out, ValidationState &state)
This is an overloaded member function, provided for convenience. It differs from the above function o...
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
std::string SerializeToHex(const T &obj)
Serialize to HEX VBK encoding.
Definition: serde.hpp:456
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
bool checked
(memory only) indicates whether we already did 'checkPayloads' on this VTB
Definition: vtb.hpp:47
void toVbkEncoding(WriteStream &stream) const
Convert VTB to data stream using Vbk byte format.
std::vector< uint8_t > toVbkEncoding() const
Convert VTB to raw bytes data using Vbk byte format.
id_t getId() const
Calculate a VTB id that is the sha256 hash of the VTB rawBytes.
Veriblock block.
Definition: vbkblock.hpp:32
Path in Merkle tree, which proves that subject exists in the tree.
Veriblock POP transaction, which endorses VBK block in BTC blockchain.
Definition: vbkpoptx.hpp:42