• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

libbitcoin / libbitcoin-system / 12739695152

13 Jan 2025 02:44AM UTC coverage: 82.969% (-0.1%) from 83.098%
12739695152

push

github

web-flow
Merge pull request #1582 from evoskuil/master

Optimize populate by avoiding hash copies, normalize ref optimizations.

14 of 40 new or added lines in 6 files covered. (35.0%)

2 existing lines in 1 file now uncovered.

10065 of 12131 relevant lines covered (82.97%)

4721517.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.02
/src/chain/point.cpp
1
/**
2
 * Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS)
3
 *
4
 * This file is part of libbitcoin.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
#include <bitcoin/system/chain/point.hpp>
20

21
#include <memory>
22
#include <utility>
23
#include <bitcoin/system/chain/enums/magic_numbers.hpp>
24
#include <bitcoin/system/define.hpp>
25
#include <bitcoin/system/math/math.hpp>
26
#include <bitcoin/system/stream/stream.hpp>
27

28
namespace libbitcoin {
29
namespace system {
30
namespace chain {
31

32
// This sentinel is serialized and defined by consensus, not implementation.
33
const uint32_t point::null_index = no_previous_output;
34

35
// Constructors.
36
// ----------------------------------------------------------------------------
37

38
// Invalid default used in signature hashing.
39
point::point() NOEXCEPT
886✔
40
  : point(null_hash, point::null_index, false)
41
{
42
}
886✔
43

44
point::point(hash_digest&& hash, uint32_t index) NOEXCEPT
28✔
45
  : point(std::move(hash), index, true)
46
{
47
}
28✔
48

49
point::point(const hash_digest& hash, uint32_t index) NOEXCEPT
65✔
50
  : point(hash, index, true)
51
{
52
}
59✔
53

54
point::point(const data_slice& data) NOEXCEPT
1✔
55
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
56
  : point(stream::in::copy(data))
1✔
57
    BC_POP_WARNING()
58
{
59
}
1✔
60

61
////point::point(stream::in::fast&& stream) NOEXCEPT
62
////  : point(read::bytes::fast(stream))
63
////{
64
////}
65

66
point::point(stream::in::fast& stream) NOEXCEPT
×
67
  : point(read::bytes::fast(stream))
×
68
{
69
}
×
70

71
point::point(std::istream&& stream) NOEXCEPT
1✔
72
  : point(read::bytes::istream(stream))
1✔
73
{
74
}
1✔
75

76
point::point(std::istream& stream) NOEXCEPT
2✔
77
  : point(read::bytes::istream(stream))
2✔
78
{
79
}
2✔
80

81
point::point(reader&& source) NOEXCEPT
3✔
82
  : point(source)
3✔
83
{
84
}
×
85

86
point::point(reader& source) NOEXCEPT
259✔
87
{
88
    assign_data(source);
259✔
89
}
259✔
90

91
// protected
92
point::point(hash_digest&& hash, uint32_t index, bool valid) NOEXCEPT
28✔
93
  : hash_(std::move(hash)), index_(index), valid_(valid)
28✔
94
{
95
}
×
96

97
// protected
98
point::point(const hash_digest& hash, uint32_t index, bool valid) NOEXCEPT
951✔
99
  : hash_(hash), index_(index), valid_(valid)
951✔
100
{
101
}
×
102

103
// Operators.
104
// ----------------------------------------------------------------------------
105

106
bool point::operator==(const point& other) const NOEXCEPT
61✔
107
{
108
    return (hash_ == other.hash_)
61✔
109
        && (index_ == other.index_);
61✔
110
}
111

112
bool point::operator!=(const point& other) const NOEXCEPT
2✔
113
{
114
    return !(*this == other);
2✔
115
}
116

117
bool operator<(const point& left, const point& right) NOEXCEPT
8✔
118
{
119
    // Arbitrary compare, for uniqueness sorting.
120
    return left.index() == right.index() ?
5✔
121
        left.hash() < right.hash() :
3✔
122
        left.index() < right.index();
8✔
123
}
124

125
// Constant reference optimizers.
126

NEW
127
bool operator<(const point_cref& left, const point_cref& right) NOEXCEPT
×
128
{
NEW
129
    return left.get() < right.get();
×
130
}
131

132
bool operator==(const point_cref& left, const point_cref& right) NOEXCEPT
2✔
133
{
134
    return left.get() == right.get();
2✔
135
}
136

NEW
137
bool operator!=(const point_cref& left, const point_cref& right) NOEXCEPT
×
138
{
NEW
139
    return !(left == right);
×
140
}
141

142

143
// Deserialization.
144
// ----------------------------------------------------------------------------
145

146
// private
147
void point::assign_data(reader& source) NOEXCEPT
259✔
148
{
149
    source.read_bytes(hash_.data(), hash_size);
259✔
150
    index_ = source.read_4_bytes_little_endian();
259✔
151
    valid_ = source;
259✔
152
}
259✔
153

154
// Serialization.
155
// ----------------------------------------------------------------------------
156

157
data_chunk point::to_data() const NOEXCEPT
1✔
158
{
159
    data_chunk data(serialized_size());
1✔
160

161
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
162
    stream::out::copy ostream(data);
1✔
163
    BC_POP_WARNING()
164

165
    to_data(ostream);
1✔
166
    return data;
2✔
167
}
1✔
168

169
void point::to_data(std::ostream& stream) const NOEXCEPT
2✔
170
{
171
    write::bytes::ostream out(stream);
2✔
172
    to_data(out);
2✔
173
}
2✔
174

175
void point::to_data(writer& sink) const NOEXCEPT
1,455✔
176
{
177
    sink.write_bytes(hash_);
1,455✔
178
    sink.write_4_bytes_little_endian(index_);
1,455✔
179
}
1,455✔
180

181
// Properties.
182
// ----------------------------------------------------------------------------
183

184
bool point::is_valid() const NOEXCEPT
7✔
185
{
186
    return valid_;
7✔
187
}
188

189
const hash_digest& point::hash() const NOEXCEPT
41✔
190
{
191
    return hash_;
3✔
192
}
193

194
uint32_t point::index() const NOEXCEPT
42✔
195
{
196
    return index_;
20✔
197
}
198

199
// Validation.
200
// ----------------------------------------------------------------------------
201

202
bool point::is_null() const NOEXCEPT
68✔
203
{
204
    return (index_ == null_index) && (hash_ == null_hash);
68✔
205
}
206

207
// JSON value convertors.
208
// ----------------------------------------------------------------------------
209

210
namespace json = boost::json;
211

212
// boost/json will soon have NOEXCEPT: github.com/boostorg/json/pull/636
213
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
214

215
point tag_invoke(json::value_to_tag<point>, const json::value& value) NOEXCEPT
6✔
216
{
217
    hash_digest hash;
6✔
218
    if (!decode_hash(hash, value.at("hash").get_string().c_str()))
6✔
219
        return {};
×
220

221
    return
6✔
222
    {
223
        hash,
224
        value.at("index").to_number<uint32_t>()
12✔
225
    };
6✔
226
}
227

228
void tag_invoke(json::value_from_tag, json::value& value,
12✔
229
    const point& point) NOEXCEPT
230
{
231
    value =
36✔
232
    {
233
        { "hash", encode_hash(point.hash()) },
12✔
234
        { "index", point.index() }
235
    };
12✔
236
}
12✔
237

238
BC_POP_WARNING()
239

240
point::cptr tag_invoke(json::value_to_tag<point::cptr>,
×
241
    const json::value& value) NOEXCEPT
242
{
243
    return to_shared(tag_invoke(json::value_to_tag<point>{}, value));
×
244
}
245

246
// Shared pointer overload is required for navigation.
247
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
248
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
249

250
void tag_invoke(json::value_from_tag tag, json::value& value,
×
251
    const point::cptr& output) NOEXCEPT
252
{
253
    tag_invoke(tag, value, *output);
×
254
}
×
255

256
BC_POP_WARNING()
257
BC_POP_WARNING()
258

259
} // namespace chain
260
} // namespace system
261
} // namespace libbitcoin
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc