forked from libbitcoin/libbitcoin-system
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpoint.hpp
154 lines (124 loc) · 4.73 KB
/
point.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_SYSTEM_CHAIN_POINT_HPP
#define LIBBITCOIN_SYSTEM_CHAIN_POINT_HPP
#include <istream>
#include <memory>
#include <unordered_set>
#include <vector>
#include <bitcoin/system/data/data.hpp>
#include <bitcoin/system/define.hpp>
#include <bitcoin/system/hash/hash.hpp>
#include <bitcoin/system/stream/stream.hpp>
namespace libbitcoin {
namespace system {
namespace chain {
class BC_API point
{
public:
DEFAULT_COPY_MOVE_DESTRUCT(point);
typedef std::shared_ptr<const point> cptr;
/// This is a sentinel used in .index to indicate no output, e.g. coinbase.
/// This value is serialized and defined by consensus, not implementation.
static const uint32_t null_index;
static constexpr size_t serialized_size() NOEXCEPT
{
return hash_size + sizeof(uint32_t);
}
/// Constructors.
/// -----------------------------------------------------------------------
/// Default point is an invalid null point (null_hash/null_index) object.
point() NOEXCEPT;
point(hash_digest&& hash, uint32_t index) NOEXCEPT;
point(const hash_digest& hash, uint32_t index) NOEXCEPT;
point(const data_slice& data) NOEXCEPT;
////point(stream::in::fast&& stream) NOEXCEPT;
point(stream::in::fast& stream) NOEXCEPT;
point(std::istream&& stream) NOEXCEPT;
point(std::istream& stream) NOEXCEPT;
point(reader&& source) NOEXCEPT;
point(reader& source) NOEXCEPT;
/// Operators.
/// -----------------------------------------------------------------------
bool operator==(const point& other) const NOEXCEPT;
bool operator!=(const point& other) const NOEXCEPT;
/// Serialization.
/// -----------------------------------------------------------------------
data_chunk to_data() const NOEXCEPT;
void to_data(std::ostream& stream) const NOEXCEPT;
void to_data(writer& sink) const NOEXCEPT;
/// Properties.
/// -----------------------------------------------------------------------
/// Native properties.
bool is_valid() const NOEXCEPT;
const hash_digest& hash() const NOEXCEPT;
uint32_t index() const NOEXCEPT;
/// Computed properties.
bool is_null() const NOEXCEPT;
protected:
point(hash_digest&& hash, uint32_t index, bool valid) NOEXCEPT;
point(const hash_digest& hash, uint32_t index, bool valid) NOEXCEPT;
private:
void assign_data(reader& source) NOEXCEPT;
// The index is consensus-serialized as a fixed 4 bytes, however it is
// effectively bound to 2^17 by the block byte size limit.
// Point should be stored as shared (adds 16 bytes).
// copy: 256 + 32 + 1 = 37 bytes (vs. 16 when shared).
hash_digest hash_;
uint32_t index_;
// Cache.
bool valid_;
};
/// Arbitrary compare, for uniqueness sorting.
bool operator<(const point& left, const point& right) NOEXCEPT;
typedef std_vector<point> points;
/// Constant reference optimizers.
using point_cref = std::reference_wrapper<const point>;
using unordered_set_of_point_cref = std::unordered_set<point_cref>;
BC_API bool operator<(const point_cref& left, const point_cref& right) NOEXCEPT;
BC_API bool operator==(const point_cref& left, const point_cref& right) NOEXCEPT;
BC_API bool operator!=(const point_cref& left, const point_cref& right) NOEXCEPT;
DECLARE_JSON_VALUE_CONVERTORS(point);
DECLARE_JSON_VALUE_CONVERTORS(point::cptr);
} // namespace chain
} // namespace system
} // namespace libbitcoin
namespace std
{
template<>
struct hash<bc::system::chain::point>
{
size_t operator()(const bc::system::chain::point& value) const NOEXCEPT
{
// Right should always be the lower entropy value (i.e. point index).
return bc::system::hash_combine(bc::system::unique_hash(value.hash()),
value.index());
}
};
template<>
struct hash<bc::system::chain::point_cref>
{
std::size_t operator()(
const bc::system::chain::point_cref& value) const NOEXCEPT
{
return std::hash<bc::system::chain::point>{}(value.get());
}
};
} // namespace std
#endif