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

libbitcoin / libbitcoin-system / 15078892484

16 May 2025 11:04PM UTC coverage: 81.885% (-0.3%) from 82.18%
15078892484

push

github

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

Refactor all signature_hash methods to return bool (tapscript).

293 of 491 new or added lines in 17 files covered. (59.67%)

5 existing lines in 3 files now uncovered.

10365 of 12658 relevant lines covered (81.88%)

3781254.62 hits per line

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

72.84
/src/chain/checkpoint.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/checkpoint.hpp>
20

21
#include <algorithm>
22
#include <iostream>
23
#include <utility>
24
#include <bitcoin/system/data/data.hpp>
25
#include <bitcoin/system/hash/hash.hpp>
26
#include <bitcoin/system/radix/radix.hpp>
27
#include <bitcoin/system/serial/serial.hpp>
28
#include <bitcoin/system/stream/stream.hpp>
29

30
namespace libbitcoin {
31
namespace system {
32
namespace chain {
33

34
bool checkpoint::is_at(const list& checkpoints, size_t height) NOEXCEPT
8✔
35
{
36
    return std::any_of(checkpoints.begin(), checkpoints.end(),
8✔
37
        [&](const auto& item) NOEXCEPT
15✔
38
        {
39
            return height == item.height();
15✔
40
        });
8✔
41
}
42

43
bool checkpoint::is_under(const list& checkpoints, size_t height) NOEXCEPT
8✔
44
{
45
    // False if empty, optimal if checkpoints sorted by increasing height.
46
    return std::any_of(checkpoints.rbegin(), checkpoints.rend(),
8✔
47
        [&](const auto& item) NOEXCEPT
8✔
48
        {
49
            return height <= item.height();
8✔
50
        });
8✔
51
}
52

53
bool checkpoint::is_conflict(const list& checkpoints, const hash_digest& hash,
7✔
54
    size_t height) NOEXCEPT
55
{
56
    //  Conflict if height matches and hash does not.
57
    const auto it = std::find_if(checkpoints.begin(), checkpoints.end(),
7✔
58
        [&](const auto& item) NOEXCEPT
12✔
59
        {
60
            return height == item.height();
12✔
61
        });
62

63
    return it != checkpoints.end() && it->hash() != hash;
7✔
64
}
65

66
// Constructors.
67
// ----------------------------------------------------------------------------
68

69
checkpoint::checkpoint() NOEXCEPT
180✔
70
  : checkpoint({}, zero, false)
180✔
71
{
72
}
180✔
73

74
checkpoint::checkpoint(hash_digest&& hash, size_t height) NOEXCEPT
4✔
75
  : checkpoint(std::move(hash), height, true)
76
{
77
}
4✔
78

79
checkpoint::checkpoint(const hash_digest& hash, size_t height) NOEXCEPT
4✔
80
  : checkpoint(hash, height, true)
81
{
82
}
3✔
83

84
checkpoint::checkpoint(const std::string& hash, size_t height) NOEXCEPT
627✔
85
  : checkpoint(from_string(hash, height))
627✔
86
{
87
}
627✔
88

89
// protected
90
checkpoint::checkpoint(hash_digest&& hash, size_t height, bool valid) NOEXCEPT
809✔
91
  : hash_(std::move(hash)), height_(height), valid_(valid)
809✔
92
{
93
}
625✔
94

95
// protected
96
checkpoint::checkpoint(const hash_digest& hash, size_t height,
4✔
97
    bool valid) NOEXCEPT
3✔
98
  : hash_(hash), height_(height), valid_(valid)
4✔
99
{
100
}
×
101

102
// functional equality
103
bool checkpoint::equals(const hash_digest& hash, size_t height) const NOEXCEPT
67✔
104
{
105
    return this->hash() == hash && this->height() == height;
199✔
106
}
107

108
// Operators.
109
// ----------------------------------------------------------------------------
110

111
bool operator<(const checkpoint& left, const checkpoint& right) NOEXCEPT
100✔
112
{
113
    return left.height() < right.height();
100✔
114
}
115

116
bool operator==(const checkpoint& left, const checkpoint& right) NOEXCEPT
67✔
117
{
118
    return left.equals(right.hash(), right.height());
67✔
119
}
120

121
bool operator!=(const checkpoint& left, const checkpoint& right) NOEXCEPT
2✔
122
{
123
    return !(left == right);
2✔
124
}
125

126
// Deserialization.
127
// ----------------------------------------------------------------------------
128

129
// TODO: add get_line/put_line to reader and eliminate stream_result.
UNCOV
130
std::istream& operator>>(std::istream& stream, checkpoint& out) THROWS
×
131
{
132
    std::string value;
×
133
    stream >> value;
×
134

135
    hash_digest hash;
×
136
    size_t height(zero);
×
137

138
    // std::string avoids boolean override.
139
    const auto tokens = split(value, std::string{ ":" });
×
140

141
    if (tokens.size() != two ||
×
142
        !decode_hash(hash, tokens.front()) ||
×
143
        !deserialize(height, tokens.back()))
×
144
        throw istream_exception(value);
×
145

146
    out = { hash, height };
×
147
    return stream;
×
148
}
×
149

150
// private
151
checkpoint checkpoint::from_string(const std::string& hash,
627✔
152
    size_t height) NOEXCEPT
153
{
154
    hash_digest digest;
627✔
155
    if (!decode_hash(digest, hash))
627✔
156
        return {};
2✔
157

158
    return { std::move(digest), height, true };
625✔
159
}
160

UNCOV
161
bool checkpoint::is_valid() const NOEXCEPT
×
162
{
163
    return valid_;
×
164
}
165

166
// Serialization.
167
// ----------------------------------------------------------------------------
168

169
std::ostream& operator<<(std::ostream& stream, const checkpoint& in) NOEXCEPT
×
170
{
171
    stream << in.to_string();
×
172
    return stream;
×
173
}
174

175
std::string checkpoint::to_string() const NOEXCEPT
×
176
{
177
    return encode_hash(hash_) + ":" + serialize(height_);
×
178
}
179

180
// Properties.
181
// ----------------------------------------------------------------------------
182

183
size_t checkpoint::height() const NOEXCEPT
288✔
184
{
185
    return height_;
102✔
186
}
187

188
const hash_digest& checkpoint::hash() const NOEXCEPT
156✔
189
{
190
    return hash_;
70✔
191
}
192

193
// JSON value convertors.
194
// ----------------------------------------------------------------------------
195

196
namespace json = boost::json;
197

198
checkpoint tag_invoke(json::value_to_tag<checkpoint>,
1✔
199
    const json::value& value) NOEXCEPT
200
{
201
    hash_digest hash;
1✔
202
    if (!decode_hash(hash, value.at("hash").get_string().c_str()))
1✔
203
        return {};
×
204

205
    return
1✔
206
    {
207
        hash,
208
        value.at("height").to_number<size_t>()
2✔
209
    };
1✔
210
}
211

212
void tag_invoke(json::value_from_tag, json::value& value,
2✔
213
    const checkpoint& checkpoint) NOEXCEPT
214
{
215
    value =
6✔
216
    {
217
        { "hash", encode_hash(checkpoint.hash()) },
2✔
218
        { "height", checkpoint.height() }
219
    };
2✔
220
}
2✔
221

222
} // namespace chain
223
} // namespace system
224
} // 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