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

libbitcoin / libbitcoin-system / 14434326165

13 Apr 2025 11:21PM UTC coverage: 82.798% (-0.1%) from 82.939%
14434326165

push

github

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

Move golomb_coding from /crypto to new /filter folder.

62 of 64 new or added lines in 13 files covered. (96.88%)

32 existing lines in 9 files now uncovered.

10190 of 12307 relevant lines covered (82.8%)

3818174.07 hits per line

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

81.25
/src/chain/output.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/output.hpp>
20

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

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

33
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
34

35
// This is a consensus critical value that must be set on reset.
36
const uint64_t output::not_found = sighash_null_value;
37

38
// Constructors.
39
// ----------------------------------------------------------------------------
40

41
// Invalid default used in signature hashing (validity ignored).
42
// Invalidity is also used to determine that a prevout is not found.
43
output::output() NOEXCEPT
10✔
44
  : output(output::not_found, to_shared<chain::script>(), false)
10✔
45
{
46
}
10✔
47

48
output::output(uint64_t value, chain::script&& script) NOEXCEPT
196✔
49
  : output(value, to_shared(std::move(script)), true)
196✔
50
{
51
}
196✔
52

53
output::output(uint64_t value, const chain::script& script) NOEXCEPT
734✔
54
  : output(value, to_shared(script), true)
734✔
55
{
56
}
734✔
57

58
output::output(uint64_t value, const chain::script::cptr& script) NOEXCEPT
×
59
  : output(value, script ? script : to_shared<chain::script>(), true)
×
60
{
61
}
×
62

63
output::output(stream::in::fast&& stream) NOEXCEPT
4✔
64
  : output(read::bytes::fast(stream))
4✔
65
{
66
}
4✔
67

UNCOV
68
output::output(stream::in::fast& stream) NOEXCEPT
×
UNCOV
69
  : output(read::bytes::fast(stream))
×
70
{
71
}
×
72

UNCOV
73
output::output(std::istream&& stream) NOEXCEPT
×
UNCOV
74
  : output(read::bytes::istream(stream))
×
75
{
UNCOV
76
}
×
77

78
output::output(std::istream& stream) NOEXCEPT
2✔
79
  : output(read::bytes::istream(stream))
2✔
80
{
81
}
2✔
82

83
output::output(reader&& source) NOEXCEPT
6✔
84
  : output(source)
6✔
85
{
86
}
×
87

88
output::output(reader& source) NOEXCEPT
280✔
89
  : value_(source.read_8_bytes_little_endian()),
560✔
90
    script_(CREATE(chain::script, source.get_allocator(), source, true)),
280✔
91
    valid_(source),
280✔
92
    size_(serialized_size(*script_, value_))
560✔
93
{
94
}
280✔
95

96
// protected
97
output::output(uint64_t value, const chain::script::cptr& script,
940✔
98
    bool valid) NOEXCEPT
940✔
99
  : value_(value),
940✔
100
    script_(script),
101
    valid_(valid),
940✔
102
    size_(serialized_size(*script, value))
940✔
103
{
104
}
940✔
105

106
// Operators.
107
// ----------------------------------------------------------------------------
108

109
bool output::operator==(const output& other) const NOEXCEPT
45✔
110
{
111
    return (value_ == other.value_)
45✔
112
        && (script_ == other.script_ || *script_ == *other.script_);
45✔
113
}
114

115
bool output::operator!=(const output& other) const NOEXCEPT
2✔
116
{
117
    return !(*this == other);
2✔
118
}
119

120
// Deserialization.
121
// ----------------------------------------------------------------------------
122

123
// Serialization.
124
// ----------------------------------------------------------------------------
125

126
data_chunk output::to_data() const NOEXCEPT
2✔
127
{
128
    data_chunk data(serialized_size());
2✔
129
    stream::out::fast ostream(data);
2✔
130
    write::bytes::fast out(ostream);
2✔
131
    to_data(out);
2✔
132
    return data;
2✔
133
}
2✔
134

135
void output::to_data(std::ostream& stream) const NOEXCEPT
1✔
136
{
137
    write::bytes::ostream out(stream);
1✔
138
    to_data(out);
1✔
139
}
1✔
140

141
void output::to_data(writer& sink) const NOEXCEPT
1,717✔
142
{
143
    sink.write_8_bytes_little_endian(value_);
1,717✔
144
    script_->to_data(sink, true);
1,717✔
145
}
1,717✔
146

147
// static/private
148
size_t output::serialized_size(const chain::script& script,
1,220✔
149
    uint64_t value) NOEXCEPT
150
{
151
    return ceilinged_add(sizeof(value), script.serialized_size(true));
1,220✔
152
}
153

154
size_t output::serialized_size() const NOEXCEPT
397✔
155
{
156
    return size_;
2✔
157
}
158

159
// Properties.
160
// ----------------------------------------------------------------------------
161

162
bool output::is_valid() const NOEXCEPT
9✔
163
{
164
    return valid_;
9✔
165
}
166

167
uint64_t output::value() const NOEXCEPT
55✔
168
{
169
    return value_;
10✔
170
}
171

172
const chain::script& output::script() const NOEXCEPT
174✔
173
{
174
    return *script_;
10✔
175
}
176

177
const chain::script::cptr& output::script_ptr() const NOEXCEPT
1,496✔
178
{
179
    return script_;
1,496✔
180
}
181

182
// Methods.
183
// ----------------------------------------------------------------------------
184

185
bool output::committed_hash(hash_digest& out) const NOEXCEPT
×
186
{
187
    const auto& ops = script_->ops();
×
188
    if (!script::is_commitment_pattern(ops))
×
189
        return false;
190

191
    // The four byte offset for the witness commitment hash (bip141).
192

193
    // More efficient [] dereference is guarded above.
194
    BC_PUSH_WARNING(NO_ARRAY_INDEXING)
195
    const auto start = std::next(ops[1].data().begin(), sizeof(witness_head));
×
196
    BC_POP_WARNING()
197

198
    std::copy_n(start, hash_size, out.begin());
×
199
    return true;
×
200
}
201

202
// Product overflows guarded by script size limit.
203
static_assert(max_script_size < max_size_t / multisig_default_sigops / 
204
    heavy_sigops_factor, "output sigop overflow guard");
205

206
size_t output::signature_operations(bool bip141) const NOEXCEPT
1✔
207
{
208
    // Penalize quadratic signature operations (bip141).
209
    const auto factor = bip141 ? heavy_sigops_factor : one;
1✔
210

211
    // Count heavy sigops in the output script.
212
    return script_->signature_operations(false) * factor;
1✔
213
}
214

215
bool output::is_dust(uint64_t minimum_value) const NOEXCEPT
9✔
216
{
217
    // If provably unspendable it does not expand the unspent output set. Dust
218
    // is all about prunability. Miners can be expected take the largest fee
219
    // independent of dust, so this is an attempt to prevent miners from seeing
220
    // transactions with unprunable outputs.
221
    return value_ < minimum_value && !script_->is_unspendable();
9✔
222
}
223

224
BC_POP_WARNING()
225

226
// JSON value convertors.
227
// ----------------------------------------------------------------------------
228

229
namespace json = boost::json;
230

231
// boost/json will soon have NOEXCEPT: github.com/boostorg/json/pull/636
232
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
233

234
output tag_invoke(json::value_to_tag<output>,
5✔
235
    const json::value& value) NOEXCEPT
236
{
237
    return
5✔
238
    {
239
        value.at("value").to_number<uint64_t>(),
5✔
240
        json::value_to<chain::script>(value.at("script"))
10✔
241
    };
15✔
242
}
243

244
void tag_invoke(json::value_from_tag, json::value& value,
10✔
245
    const output& output) NOEXCEPT
246
{
247
    value =
10✔
248
    {
249
        { "value", output.value() },
250
        { "script", output.script() },
251
    };
10✔
252
}
10✔
253

254
BC_POP_WARNING()
255

256
output::cptr tag_invoke(json::value_to_tag<output::cptr>,
×
257
    const json::value& value) NOEXCEPT
258
{
259
    return to_shared(tag_invoke(json::value_to_tag<output>{}, value));
×
260
}
261

262
// Shared pointer overload is required for navigation.
263
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
264
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
265

266
void tag_invoke(json::value_from_tag tag, json::value& value,
8✔
267
    const output::cptr& output) NOEXCEPT
268
{
269
    tag_invoke(tag, value, *output);
8✔
270
}
8✔
271

272
BC_POP_WARNING()
273
BC_POP_WARNING()
274

275
} // namespace chain
276
} // namespace system
277
} // 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