veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
endorsement.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_ENDORSEMENT_HPP_
7#define ALT_INTEGRATION_INCLUDE_VERIBLOCK_ENTITIES_ENDORSEMENT_HPP_
8
9#include <memory>
10#include <veriblock/pop/serde.hpp>
11#include <veriblock/pop/uint.hpp>
12
13namespace altintegration {
14
36template <class EndorsedHash, class ContainingHash, class Container>
39 using id_t = uint256;
40 using endorsed_hash_t = EndorsedHash;
41 using containing_hash_t = ContainingHash;
42 using container_t = Container;
43
44 // The unique key that identifies this endorsement
45 id_t id;
46 EndorsedHash endorsedHash;
47 EndorsedHash containingHash;
48 ContainingHash blockOfProof;
49
50 void toVbkEncoding(WriteStream& stream) const {
51 writeSingleByteLenValue(stream, id);
52 writeSingleByteLenValue(stream, endorsedHash);
53 writeSingleByteLenValue(stream, containingHash);
54 writeSingleByteLenValue(stream, blockOfProof);
55 }
56
57 std::vector<uint8_t> toVbkEncoding() const {
58 WriteStream stream;
59 toVbkEncoding(stream);
60 return stream.data();
61 }
62
63 static type fromContainer(const Container& c);
64
65 static type fromContainer(const Container& c,
66 const EndorsedHash& containingHash,
67 const EndorsedHash& endorsedHash);
68
69 static std::shared_ptr<type> fromContainerPtr(
70 const Container& c,
71 const EndorsedHash& containingHash,
72 const EndorsedHash& endorsedHash) {
73 return std::make_shared<type>(
74 fromContainer(c, containingHash, endorsedHash));
75 }
76
77 static std::shared_ptr<type> fromContainerPtr(const Container& c) {
78 return std::make_shared<type>(fromContainer(c));
79 }
80
81 static type::id_t getId(const Container& c);
82
83 type::id_t getId() const { return id; }
84
85 bool operator==(const type& other) const { return id == other.id; }
86
87 friend bool operator<(const type& a, const type& b) {
88 if (a.id < b.id) return true;
89 if (a.id > b.id) return false;
90 if (a.endorsedHash < b.endorsedHash) return true;
91 if (a.endorsedHash > b.endorsedHash) return false;
92 if (a.containingHash < b.containingHash) return true;
93 if (a.containingHash > b.containingHash) return false;
94 return a.blockOfProof < b.blockOfProof;
95 }
96
97 bool operator!=(const type& other) const { return !operator==(other); }
98
99 std::string toPrettyString(size_t level = 0) const {
100 return format(
101 "{}{}Endorsement{{id={}, containing={}, endorsed={}, blockOfProof={}}}",
102 std::string(level, ' '),
103 type::name(),
104 HexStr(id),
105 HexStr(containingHash),
106 HexStr(endorsedHash),
107 HexStr(blockOfProof));
108 }
109
110 static const std::string name();
111};
112
114template <typename Value, class A, class B, class C>
115Value ToJSON(const Endorsement<A, B, C>& e) {
116 auto obj = json::makeEmptyObject<Value>();
117 json::putStringKV(obj, "id", HexStr(e.id));
118 json::putStringKV(obj, "endorsedHash", HexStr(e.endorsedHash));
119 json::putStringKV(obj, "containingHash", e.containingHash);
120 json::putStringKV(obj, "blockOfProof", e.blockOfProof);
121 return obj;
122}
123
125template <typename A, typename B, typename C>
126void PrintTo(const Endorsement<A, B, C>& e, std::ostream* os) {
127 *os << e.toPrettyString();
128}
129
130} // namespace altintegration
131
132#endif // ALT_INTEGRATION_INCLUDE_VERIBLOCK_ENTITIES_ENDORSEMENT_HPP_
Binary writer that is useful for binary serialization.
Defines logging helpers.
Definition: block.hpp:14
void writeSingleByteLenValue(WriteStream &stream, Slice< const uint8_t > value)
Write single byte length value, which consists of N bytes vector Appends 1 byte data length to the st...
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
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