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

libbitcoin / libbitcoin-system / 4586521533

pending completion
4586521533

Pull #1351

github

GitHub
Merge 43395b060 into af2a5a44b
Pull Request #1351: Fix sign inversion (regression) in operation::opcode_to_positive.

15 of 15 new or added lines in 6 files covered. (100.0%)

9441 of 11344 relevant lines covered (83.22%)

6731300.01 hits per line

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

80.36
/src/chain/input.cpp
1
/**
2
 * Copyright (c) 2011-2022 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/input.hpp>
20

21
#include <algorithm>
22
#include <memory>
23
#include <utility>
24
#include <bitcoin/system/chain/context.hpp>
25
#include <bitcoin/system/chain/enums/magic_numbers.hpp>
26
#include <bitcoin/system/chain/point.hpp>
27
#include <bitcoin/system/chain/prevout.hpp>
28
#include <bitcoin/system/chain/script.hpp>
29
#include <bitcoin/system/chain/witness.hpp>
30
#include <bitcoin/system/define.hpp>
31
#include <bitcoin/system/math/math.hpp>
32
#include <bitcoin/system/stream/stream.hpp>
33

34
namespace libbitcoin {
35
namespace system {
36
namespace chain {
37

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

41
// Default point is null_hash and point::null_index. 
42
// Default metadata is spent, invalid, max_size_t value. 
43
input::input() NOEXCEPT
22✔
44
  : input(
45
      to_shared<chain::point>(),
22✔
46
      to_shared<chain::script>(),
22✔
47
      to_shared<chain::witness>(),
22✔
48
      0, false)
88✔
49
{
50
}
22✔
51

52
input::input(chain::point&& point, chain::script&& script,
66✔
53
    uint32_t sequence) NOEXCEPT
66✔
54
  : input(
55
      to_shared(std::move(point)),
66✔
56
      to_shared(std::move(script)),
66✔
57
      to_shared<chain::witness>(),
66✔
58
      sequence, true)
132✔
59
{
60
}
66✔
61

62
input::input(const chain::point& point, const chain::script& script,
734✔
63
    uint32_t sequence) NOEXCEPT
734✔
64
  : input(
65
      to_shared(point),
734✔
66
      to_shared(script),
734✔
67
      to_shared<chain::witness>(),
734✔
68
      sequence, true)
1,468✔
69
{
70
}
734✔
71

72
input::input(const chain::point::cptr& point,
×
73
    const chain::script::cptr& script, uint32_t sequence) NOEXCEPT
×
74
  : input(
75
      point ? point : to_shared<chain::point>(),
×
76
      script ? script : to_shared<chain::script>(),
×
77
      to_shared<chain::witness>(),
×
78
      sequence, true)
×
79
{
80
}
×
81

82
input::input(chain::point&& point, chain::script&& script,
11✔
83
    chain::witness&& witness, uint32_t sequence) NOEXCEPT
11✔
84
  : input(
85
      to_shared(std::move(point)),
11✔
86
      to_shared(std::move(script)),
11✔
87
      to_shared(std::move(witness)),
11✔
88
      sequence, true)
11✔
89
{
90
}
11✔
91

92
input::input(const chain::point& point, const chain::script& script,
1✔
93
    const chain::witness& witness, uint32_t sequence) NOEXCEPT
1✔
94
  : input(
95
      to_shared(point),
1✔
96
      to_shared(script),
1✔
97
      to_shared(witness),
1✔
98
      sequence, true)
1✔
99
{
100
}
1✔
101

102
input::input(const chain::point::cptr& point, const chain::script::cptr& script,
×
103
    const chain::witness::cptr& witness, uint32_t sequence) NOEXCEPT
×
104
  : input(point, script, witness, sequence, true)
×
105
{
106
}
×
107

108
input::input(const data_slice& data) NOEXCEPT
5✔
109
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
110
  : input(stream::in::copy(data))
5✔
111
    BC_POP_WARNING()
112
{
113
}
5✔
114

115
input::input(std::istream&& stream) NOEXCEPT
5✔
116
  : input(read::bytes::istream(stream))
5✔
117
{
118
}
5✔
119

120
input::input(std::istream& stream) NOEXCEPT
3✔
121
  : input(read::bytes::istream(stream))
3✔
122
{
123
}
3✔
124

125
input::input(reader&& source) NOEXCEPT
8✔
126
  : input(from_data(source))
8✔
127
{
128
}
8✔
129

130
input::input(reader& source) NOEXCEPT
225✔
131
  : input(from_data(source))
225✔
132
{
133
}
225✔
134

135
// protected
136
input::input(const chain::point::cptr& point, const chain::script::cptr& script,
1,067✔
137
    const chain::witness::cptr& witness, uint32_t sequence, bool valid) NOEXCEPT
1,067✔
138
  : point_(point),
139
    script_(script),
140
    witness_(witness),
141
    sequence_(sequence),
1,067✔
142
    valid_(valid)
1,067✔
143
{
144
}
1,067✔
145

146
// Operators.
147
// ----------------------------------------------------------------------------
148

149
bool input::operator==(const input& other) const NOEXCEPT
40✔
150
{
151
    return (sequence_ == other.sequence_)
40✔
152
        && (point_ == other.point_ || *point_ == *other.point_)
38✔
153
        && (script_ == other.script_ || *script_ == *other.script_)
38✔
154
        && (witness_ == other.witness_ || *witness_ == *other.witness_);
78✔
155
}
156

157
bool input::operator!=(const input& other) const NOEXCEPT
2✔
158
{
159
    return !(*this == other);
2✔
160
}
161

162
// Deserialization.
163
// ----------------------------------------------------------------------------
164

165
// static/private
166
input input::from_data(reader& source) NOEXCEPT
233✔
167
{
168
    // Witness is deserialized by transaction.
169
    return
233✔
170
    {
171
        to_shared<chain::point>(source),
233✔
172
        to_shared<chain::script>(source, true),
466✔
173
        to_shared<chain::witness>(),
233✔
174
        source.read_4_bytes_little_endian(),
233✔
175
        source
176
    };
466✔
177
}
178

179
// Serialization.
180
// ----------------------------------------------------------------------------
181

182
data_chunk input::to_data() const NOEXCEPT
1✔
183
{
184
    data_chunk data(serialized_size(false));
1✔
185

186
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
187
    stream::out::copy ostream(data);
1✔
188
    BC_POP_WARNING()
189

190
    to_data(ostream);
1✔
191
    return data;
2✔
192
}
1✔
193

194
void input::to_data(std::ostream& stream) const NOEXCEPT
2✔
195
{
196
    write::bytes::ostream out(stream);
2✔
197
    to_data(out);
2✔
198
}
2✔
199

200
// Witness is serialized by transaction.
201
void input::to_data(writer& sink) const NOEXCEPT
1,357✔
202
{
203
    point_->to_data(sink);
1,357✔
204
    script_->to_data(sink, true);
1,357✔
205
    sink.write_4_bytes_little_endian(sequence_);
1,357✔
206
}
1,357✔
207

208
size_t input::serialized_size(bool witness) const NOEXCEPT
51✔
209
{
210
    // input.serialized_size(witness) provides sizing for witness, however
211
    // witnesses are serialized by the transaction. This is an ugly hack as a
212
    // consequence of bip144 not serializing witnesses as part of inputs, which
213
    // is logically the proper association.
214
    return point_->serialized_size()
51✔
215
        + script_->serialized_size(true)
51✔
216
        + (witness ? witness_->serialized_size(true) : zero)
51✔
217
        + sizeof(sequence_);
51✔
218
}
219

220
// Properties.
221
// ----------------------------------------------------------------------------
222

223
bool input::is_valid() const NOEXCEPT
15✔
224
{
225
    return valid_;
15✔
226
}
227

228
const point& input::point() const NOEXCEPT
155✔
229
{
230
    return *point_;
155✔
231
}
232

233
const chain::script& input::script() const NOEXCEPT
76✔
234
{
235
    return *script_;
76✔
236
}
237

238
const chain::witness& input::witness() const NOEXCEPT
1,793✔
239
{
240
    return *witness_;
1,793✔
241
}
242

243
const point::cptr& input::point_ptr() const NOEXCEPT
×
244
{
245
    return point_;
×
246
}
247

248
const chain::script::cptr& input::script_ptr() const NOEXCEPT
1,555✔
249
{
250
    return script_;
1,555✔
251
}
252

253
const chain::witness::cptr& input::witness_ptr() const NOEXCEPT
×
254
{
255
    return witness_;
×
256
}
257

258
uint32_t input::sequence() const NOEXCEPT
73✔
259
{
260
    return sequence_;
73✔
261
}
262

263
// Methods.
264
// ----------------------------------------------------------------------------
265

266
bool input::is_final() const NOEXCEPT
22✔
267
{
268
    return sequence_ == max_input_sequence;
22✔
269
}
270

271
bool input::is_locked(size_t height, uint32_t median_time_past) const NOEXCEPT
10✔
272
{
273
    // BIP68: if bit 31 is set then no consensus meaning is applied.
274
    if (get_right(sequence_, relative_locktime_disabled_bit))
10✔
275
        return false;
276

277
    // BIP68: the low 16 bits of the sequence apply to relative lock-time.
278
    const auto blocks = mask_left(sequence_, relative_locktime_mask_left);
8✔
279

280
    // BIP68: bit 22 determines if relative lock is time or block based.
281
    if (get_right(sequence_, relative_locktime_time_locked_bit))
8✔
282
    {
283
        // BIP68: change sequence to seconds by shifting up by 9 bits (x 512).
284
        auto time = shift_left(blocks, relative_locktime_seconds_shift_left);
3✔
285
        auto age = floored_subtract(median_time_past, metadata.median_time_past);
3✔
286
        return age < time;
3✔
287
    }
288

289
    auto age = floored_subtract(height, metadata.height);
5✔
290
    return age < blocks;
5✔
291
}
292

293
bool input::reserved_hash(hash_digest& out) const NOEXCEPT
×
294
{
295
    if (!witness::is_reserved_pattern(witness_->stack()))
×
296
        return false;
297

298
    std::copy_n(witness_->stack().front()->begin(), hash_size, out.begin());
×
299
    return true;
×
300
}
301

302
// private
303
bool input::embedded_script(chain::script& out) const NOEXCEPT
1✔
304
{
305
    if (!prevout)
1✔
306
        return false;
307

308
    const auto& ops = script_->ops();
×
309
    const auto& script = prevout->script();
×
310

311
    // There are no embedded sigops when the prevout script is not p2sh.
312
    if (!script::is_pay_script_hash_pattern(script.ops()))
×
313
        return false;
314

315
    // There are no embedded sigops when the input script is not push only.
316
    // The first operations access must be method-based to guarantee the cache.
317
    if (ops.empty() || !script::is_relaxed_push(ops))
×
318
        return false;
×
319

320
    // Parse the embedded script from the last input script item (data).
321
    // This cannot fail because there is no prefix to invalidate the length.
322
    out = { ops.back().data(), false };
×
323
    return true;
×
324
}
325

326
// Product overflows guarded by script size limit.
327
static_assert(max_script_size < 
328
    max_size_t / multisig_default_sigops / heavy_sigops_factor,
329
    "input sigop overflow guard");
330

331
// TODO: Prior to block 79400 sigops were limited only by policy.
332
// TODO: Create legacy sigops fork flag and pass here, return 0 if false.
333
// TODO: this was an unbipped flag day soft fork, prior to BIP16/141.
334
// TODO: if (nHeight > 79400 && GetSigOpCount() > MAX_BLOCK_SIGOPS).
335
size_t input::signature_operations(bool bip16, bool bip141) const NOEXCEPT
5✔
336
{
337
    if (bip141 && !prevout)
5✔
338
        return max_size_t;
339

340
    chain::script witness, embedded;
3✔
341

342
    // Penalize quadratic signature operations (bip141).
343
    const auto factor = bip141 ? heavy_sigops_factor : one;
3✔
344

345
    // Count heavy sigops in the input script.
346
    const auto sigops = script_->signature_operations(false) * factor;
3✔
347

348
    if (bip141 && witness_->extract_sigop_script(witness, prevout->script()))
3✔
349
    {
350
        // Add sigops in the witness script (bip141).
351
        return ceilinged_add(sigops, witness.signature_operations(true));
×
352
    }
353

354
    if (bip16 && embedded_script(embedded))
3✔
355
    {
356
        if (bip141 && witness_->extract_sigop_script(witness, embedded))
×
357
        {
358
            // Add sigops in the embedded witness script (bip141).
359
            return ceilinged_add(sigops, witness.signature_operations(true));
×
360
        }
361
        else
362
        {
363
            // Add heavy sigops in the embedded script (bip16).
364
            return ceilinged_add(sigops,
×
365
                embedded.signature_operations(true) * factor);
×
366
        }
367
    }
368

369
    return sigops;
370
}
3✔
371

372
// JSON value convertors.
373
// ----------------------------------------------------------------------------
374

375
namespace json = boost::json;
376

377
// boost/json will soon have NOEXCEPT: github.com/boostorg/json/pull/636
378
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
379

380
input tag_invoke(json::value_to_tag<input>, const json::value& value) NOEXCEPT
5✔
381
{
382
    return
5✔
383
    {
384
        json::value_to<chain::point>(value.at("point")),
5✔
385
        json::value_to<chain::script>(value.at("script")),
5✔
386
        json::value_to<chain::witness>(value.at("witness")),
10✔
387
        value.at("sequence").to_number<uint32_t>()
10✔
388
    };
5✔
389
}
390

391
void tag_invoke(json::value_from_tag, json::value& value,
10✔
392
    const input& input) NOEXCEPT
393
{
394
    value =
10✔
395
    {
396
        { "point", input.point() },
397
        { "script", input.script() },
398
        { "witness", input.witness() },
399
        { "sequence", input.sequence() }
400
    };
10✔
401
}
10✔
402

403
BC_POP_WARNING()
404

405
input::cptr tag_invoke(json::value_to_tag<input::cptr>,
×
406
    const json::value& value) NOEXCEPT
407
{
408
    return to_shared(tag_invoke(json::value_to_tag<input>{}, value));
×
409
}
410

411
// Shared pointer overload is required for navigation.
412
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
413
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
414

415
void tag_invoke(json::value_from_tag tag, json::value& value,
8✔
416
    const input::cptr& input) NOEXCEPT
417
{
418
    tag_invoke(tag, value, *input);
8✔
419
}
8✔
420

421
BC_POP_WARNING()
422
BC_POP_WARNING()
423

424
} // namespace chain
425
} // namespace system
426
} // 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