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

libbitcoin / libbitcoin-system / 9950156475

16 Jul 2024 03:16AM UTC coverage: 83.203% (+0.3%) from 82.874%
9950156475

push

github

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

Optimizing deserializations.

205 of 222 new or added lines in 12 files covered. (92.34%)

14 existing lines in 8 files now uncovered.

10090 of 12127 relevant lines covered (83.2%)

4761709.16 hits per line

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

85.37
/src/chain/point.cpp
1
/**
2
 * Copyright (c) 2011-2023 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
884✔
40
  : point(null_hash, point::null_index, false)
41
{
42
}
884✔
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/*from_data(source)*/)
3✔
83
{
UNCOV
84
}
×
85

86
point::point(reader& source) NOEXCEPT
258✔
87
////: point(from_data(source))
88
{
89
    assign_data(source);
258✔
90
}
258✔
91

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

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

104
// Operators.
105
// ----------------------------------------------------------------------------
106

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

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

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

125
// Deserialization.
126
// ----------------------------------------------------------------------------
127

128
// static/private
129
////point point::from_data(reader& source) NOEXCEPT
130
////{
131
////    return
132
////    {
133
////        source.read_hash(),
134
////        source.read_4_bytes_little_endian(),
135
////        source
136
////    };
137
////}
138

139
// private
140
// This denormalization eliminates allocation and copy of 32 bytes per block.
141
void point::assign_data(reader& source) NOEXCEPT
258✔
142
{
143
    source.read_bytes(hash_.data(), hash_size);
258✔
144
    index_ = source.read_4_bytes_little_endian();
258✔
145
    valid_ = source;
258✔
146
}
258✔
147

148
// Serialization.
149
// ----------------------------------------------------------------------------
150

151
data_chunk point::to_data() const NOEXCEPT
1✔
152
{
153
    data_chunk data(serialized_size());
1✔
154

155
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
156
    stream::out::copy ostream(data);
1✔
157
    BC_POP_WARNING()
158

159
    to_data(ostream);
1✔
160
    return data;
2✔
161
}
1✔
162

163
void point::to_data(std::ostream& stream) const NOEXCEPT
2✔
164
{
165
    write::bytes::ostream out(stream);
2✔
166
    to_data(out);
2✔
167
}
2✔
168

169
void point::to_data(writer& sink) const NOEXCEPT
1,457✔
170
{
171
    sink.write_bytes(hash_);
1,457✔
172
    sink.write_4_bytes_little_endian(index_);
1,457✔
173
}
1,457✔
174

175
// Properties.
176
// ----------------------------------------------------------------------------
177

178
bool point::is_valid() const NOEXCEPT
7✔
179
{
180
    return valid_;
7✔
181
}
182

183
const hash_digest& point::hash() const NOEXCEPT
41✔
184
{
185
    return hash_;
3✔
186
}
187

188
uint32_t point::index() const NOEXCEPT
42✔
189
{
190
    return index_;
20✔
191
}
192

193
// Validation.
194
// ----------------------------------------------------------------------------
195

196
bool point::is_null() const NOEXCEPT
68✔
197
{
198
    return (index_ == null_index) && (hash_ == null_hash);
68✔
199
}
200

201
// JSON value convertors.
202
// ----------------------------------------------------------------------------
203

204
namespace json = boost::json;
205

206
// boost/json will soon have NOEXCEPT: github.com/boostorg/json/pull/636
207
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
208

209
point tag_invoke(json::value_to_tag<point>, const json::value& value) NOEXCEPT
6✔
210
{
211
    hash_digest hash;
6✔
212
    if (!decode_hash(hash, value.at("hash").get_string().c_str()))
6✔
213
        return {};
×
214

215
    return
6✔
216
    {
217
        hash,
218
        value.at("index").to_number<uint32_t>()
12✔
219
    };
6✔
220
}
221

222
void tag_invoke(json::value_from_tag, json::value& value,
12✔
223
    const point& point) NOEXCEPT
224
{
225
    value =
36✔
226
    {
227
        { "hash", encode_hash(point.hash()) },
12✔
228
        { "index", point.index() }
229
    };
12✔
230
}
12✔
231

232
BC_POP_WARNING()
233

234
point::cptr tag_invoke(json::value_to_tag<point::cptr>,
×
235
    const json::value& value) NOEXCEPT
236
{
237
    return to_shared(tag_invoke(json::value_to_tag<point>{}, value));
×
238
}
239

240
// Shared pointer overload is required for navigation.
241
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
242
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
243

244
void tag_invoke(json::value_from_tag tag, json::value& value,
×
245
    const point::cptr& output) NOEXCEPT
246
{
247
    tag_invoke(tag, value, *output);
×
248
}
×
249

250
BC_POP_WARNING()
251
BC_POP_WARNING()
252

253
} // namespace chain
254
} // namespace system
255
} // 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

© 2025 Coveralls, Inc