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

libbitcoin / libbitcoin-system / 14509835486

17 Apr 2025 06:50AM UTC coverage: 82.762% (-0.06%) from 82.822%
14509835486

push

github

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

Refactor is_invalid_witness_commitment, use cref to avoid copies.

0 of 24 new or added lines in 4 files covered. (0.0%)

4 existing lines in 3 files now uncovered.

10183 of 12304 relevant lines covered (82.76%)

3819104.78 hits per line

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

71.36
/src/chain/input.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/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
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
39

40
// Product overflows guarded by script size limit.
41
static_assert(max_script_size < 
42
    max_size_t / multisig_default_sigops / heavy_sigops_factor,
43
    "input sigop overflow guard");
44

45
// Null witness helpers.
46
// ----------------------------------------------------------------------------
47

48
// static/private
49
const witness& input::no_witness() NOEXCEPT
×
50
{
51
    static const chain::witness empty_witness{};
×
52
    return empty_witness;
×
53
}
54

55
// static/private
56
const witness::cptr& input::no_witness_cptr() NOEXCEPT
×
57
{
58
    BC_PUSH_WARNING(NO_NEW_OR_DELETE)
59
    static const std::shared_ptr<const chain::witness> empty
×
60
    {
61
        new const chain::witness{}
×
62
    };
×
63
    BC_POP_WARNING()
64
    return empty;
×
65
}
66

67
const chain::witness& input::get_witness() const NOEXCEPT
1,985✔
68
{
69
    return witness_ ? *witness_ : no_witness();
1,985✔
70
}
71

72
const chain::witness::cptr& input::get_witness_cptr() const NOEXCEPT
×
73
{
74
    return witness_ ? witness_ : no_witness_cptr();
×
75
}
76

77
// Constructors.
78
// ----------------------------------------------------------------------------
79

80
// Default point is null_hash and point::null_index. 
81
// Default metadata is spent, invalid, max_size_t value. 
82
input::input() NOEXCEPT
22✔
83
  : input(
84
      to_shared<chain::point>(),
22✔
85
      to_shared<chain::script>(),
22✔
86
      to_shared<chain::witness>(),
22✔
87
      0, false)
66✔
88
{
89
}
22✔
90

91
input::input(chain::point&& point, chain::script&& script,
160✔
92
    uint32_t sequence) NOEXCEPT
160✔
93
  : input(
94
      to_shared(std::move(point)),
160✔
95
      to_shared(std::move(script)),
160✔
96
      to_shared<chain::witness>(),
160✔
97
      sequence, true)
160✔
98
{
99
}
160✔
100

101
input::input(const chain::point& point, const chain::script& script,
736✔
102
    uint32_t sequence) NOEXCEPT
736✔
103
  : input(
104
      to_shared(point),
736✔
105
      to_shared(script),
736✔
106
      to_shared<chain::witness>(),
736✔
107
      sequence, true)
736✔
108
{
109
}
736✔
110

111
input::input(const chain::point::cptr& point,
×
112
    const chain::script::cptr& script, uint32_t sequence) NOEXCEPT
×
113
  : input(
114
      point ? point : to_shared<chain::point>(),
×
115
      script ? script : to_shared<chain::script>(),
×
116
      to_shared<chain::witness>(),
×
117
      sequence, true)
×
118
{
119
}
×
120

121
input::input(chain::point&& point, chain::script&& script,
14✔
122
    chain::witness&& witness, uint32_t sequence) NOEXCEPT
14✔
123
  : input(
124
      to_shared(std::move(point)),
14✔
125
      to_shared(std::move(script)),
14✔
126
      to_shared(std::move(witness)),
14✔
127
      sequence, true)
14✔
128
{
129
}
14✔
130

131
input::input(const chain::point& point, const chain::script& script,
1✔
132
    const chain::witness& witness, uint32_t sequence) NOEXCEPT
1✔
133
  : input(
134
      to_shared(point),
1✔
135
      to_shared(script),
1✔
136
      to_shared(witness),
1✔
137
      sequence, true)
1✔
138
{
139
}
1✔
140

141
input::input(const chain::point::cptr& point, const chain::script::cptr& script,
×
142
    const chain::witness::cptr& witness, uint32_t sequence) NOEXCEPT
×
143
  : input(point, script, witness, sequence, true)
×
144
{
145
}
×
146

147
input::input(stream::in::fast&& stream) NOEXCEPT
5✔
148
  : input(read::bytes::fast(stream))
5✔
149
{
150
}
5✔
151

152
input::input(stream::in::fast& stream) NOEXCEPT
1✔
153
  : input(read::bytes::fast(stream))
1✔
154
{
155
}
1✔
156

157
input::input(std::istream&& stream) NOEXCEPT
×
158
  : input(read::bytes::istream(stream))
×
159
{
160
}
×
161

162
input::input(std::istream& stream) NOEXCEPT
3✔
163
  : input(read::bytes::istream(stream))
3✔
164
{
165
}
3✔
166

167
input::input(reader&& source) NOEXCEPT
9✔
168
  : input(source)
9✔
169
{
170
}
×
171

172
// Witness is deserialized and assigned by transaction.
173
input::input(reader& source) NOEXCEPT
280✔
174
  : point_(CREATE(chain::point, source.get_allocator(), source)),
280✔
175
    script_(CREATE(chain::script, source.get_allocator(), source, true)),
280✔
176
    witness_(CREATE(chain::witness, source.get_allocator())),
280✔
177
    sequence_(source.read_4_bytes_little_endian()),
280✔
178
    valid_(source),
280✔
179
    size_(serialized_size(*script_))
560✔
180
{
181
}
280✔
182

183
// protected
184
input::input(const chain::point::cptr& point, const chain::script::cptr& script,
933✔
185
    const chain::witness::cptr& witness, uint32_t sequence, bool valid) NOEXCEPT
933✔
186
  : point_(point),
187
    script_(script),
188
    witness_(witness),
189
    sequence_(sequence),
933✔
190
    valid_(valid),
933✔
191
    size_(serialized_size(*script, *witness))
1,866✔
192
{
193
}
933✔
194

195
// Operators.
196
// ----------------------------------------------------------------------------
197

198
bool input::operator==(const input& other) const NOEXCEPT
53✔
199
{
200
    return (sequence_ == other.sequence_)
53✔
201
        && (point_ == other.point_ || *point_ == *other.point_)
51✔
202
        && (script_ == other.script_ || *script_ == *other.script_)
51✔
203
        && (witness_ == other.witness_ || get_witness() == other.get_witness());
104✔
204
}
205

206
bool input::operator!=(const input& other) const NOEXCEPT
2✔
207
{
208
    return !(*this == other);
2✔
209
}
210

211
// Constant reference optimizers.
212

213
bool operator<(const cref_point& left, const cref_point& right) NOEXCEPT
×
214
{
215
    // Arbitrary compare, for uniqueness sorting.
216
    return left.index == right.index ?
×
217
        left.hash.get() < right.hash.get() :
×
218
        left.index < right.index;
×
219
}
220

221
bool operator==(const cref_point& left, const cref_point& right) NOEXCEPT
×
222
{
223
    return (left.hash.get() == right.hash.get())
×
224
        && (left.index == right.index);
×
225
}
226

227
bool operator!=(const cref_point& left, const cref_point& right) NOEXCEPT
×
228
{
229
    return !(left == right);
×
230
}
231

232
// Deserialization.
233
// ----------------------------------------------------------------------------
234

235
// Serialization.
236
// ----------------------------------------------------------------------------
237

238
data_chunk input::to_data() const NOEXCEPT
1✔
239
{
240
    data_chunk data(serialized_size(false));
1✔
241
    stream::out::fast ostream(data);
1✔
242
    write::bytes::fast out(ostream);
1✔
243
    to_data(out);
1✔
244
    return data;
2✔
245
}
1✔
246

247
void input::to_data(std::ostream& stream) const NOEXCEPT
1✔
248
{
249
    write::bytes::ostream out(stream);
1✔
250
    to_data(out);
1✔
251
}
1✔
252

253
// Witness is serialized by transaction.
254
void input::to_data(writer& sink) const NOEXCEPT
1,389✔
255
{
256
    point_->to_data(sink);
1,389✔
257
    script_->to_data(sink, true);
1,389✔
258
    sink.write_4_bytes_little_endian(sequence_);
1,389✔
259
}
1,389✔
260

261
// static/private
262
input::sizes input::serialized_size(const chain::script& script) NOEXCEPT
280✔
263
{
264
    constexpr auto const_size = ceilinged_add(point::serialized_size(),
280✔
265
        sizeof(sequence_));
266

267
    const auto nominal_size = ceilinged_add(const_size,
280✔
268
        script.serialized_size(true));
269

270
    // Non-segregated input serialization requires an empty witness stack size.
271
    // when serializing with witness included (witnessed_size for non-witness).
272
    // This does not affect tx serialiation as does not set witness paramter.
273
    return { nominal_size, add1(nominal_size) };
280✔
274
}
275

276
// static/private
277
input::sizes input::serialized_size(const chain::script& script,
933✔
278
    const chain::witness& witness) NOEXCEPT
279
{
280
    constexpr auto const_size = ceilinged_add(point::serialized_size(),
933✔
281
        sizeof(sequence_));
282

283
    const auto nominal_size = ceilinged_add(const_size,
933✔
284
        script.serialized_size(true));
285

286
    // The input has no way to determine if its tx is segregated, as an empty
287
    // witness applies to segregated tx as a one byte (zero) serialization.
288
    // This determination is therefore left to the transaction, and witness
289
    // retains both serialization options from construction.
290
    const auto witnessed_size = ceilinged_add(nominal_size,
933✔
291
        witness.serialized_size(true));
292

293
    // Values are the same for non-segregated transactions.
294
    return { nominal_size, witnessed_size };
933✔
295
}
296

297
// input.serialized_size(witness) provides sizing for witness, however
298
// witnesses are serialized by the transaction. This is an ugly hack as a
299
// consequence of bip144 not serializing witnesses as part of inputs, which
300
// is logically the proper association.
301
size_t input::serialized_size(bool witness) const NOEXCEPT
2✔
302
{
303
    return witness ? size_.witnessed : size_.nominal;
2✔
304
}
305

306
// Friend accessors (private).
307
// ----------------------------------------------------------------------------
308

309
size_t input::nominal_size() const NOEXCEPT
1,183✔
310
{
311
    return size_.nominal;
1,183✔
312
}
313

314
size_t input::witnessed_size() const NOEXCEPT
32✔
315
{
316
    return size_.witnessed;
32✔
317
}
318

319
////void input::set_witness(reader& source) NOEXCEPT
320
////{
321
////    witness_ = to_shared<chain::witness>(source, true);
322
////    size_.witnessed = ceilinged_add(size_.nominal,
323
////        witness_->serialized_size(true));
324
////}
325

326
void input::set_witness(reader& source) NOEXCEPT
20✔
327
{
328
    auto& allocator = source.get_allocator();
20✔
329
    witness_.reset(CREATE(chain::witness, allocator, source, true));
20✔
330
    size_.witnessed = ceilinged_add(size_.nominal,
20✔
331
        witness_->serialized_size(true));
332
}
20✔
333

334
// Properties.
335
// ----------------------------------------------------------------------------
336

337
bool input::is_valid() const NOEXCEPT
16✔
338
{
339
    return valid_;
16✔
340
}
341

342
const point& input::point() const NOEXCEPT
169✔
343
{
344
    return *point_;
10✔
345
}
346

347
const chain::script& input::script() const NOEXCEPT
75✔
348
{
349
    return *script_;
10✔
350
}
351

352
const chain::witness& input::witness() const NOEXCEPT
1,899✔
353
{
354
    return get_witness();
1,899✔
355
}
356

357
const point::cptr& input::point_ptr() const NOEXCEPT
×
358
{
359
    return point_;
×
360
}
361

362
const chain::script::cptr& input::script_ptr() const NOEXCEPT
1,559✔
363
{
364
    return script_;
1,559✔
365
}
366

367
const chain::witness::cptr& input::witness_ptr() const NOEXCEPT
×
368
{
369
    return get_witness_cptr();
×
370
}
371

372
uint32_t input::sequence() const NOEXCEPT
73✔
373
{
374
    return sequence_;
10✔
375
}
376

377
// Methods.
378
// ----------------------------------------------------------------------------
379

380
bool input::is_final() const NOEXCEPT
22✔
381
{
382
    return sequence_ == max_input_sequence;
22✔
383
}
384

385
bool input::is_roller() const NOEXCEPT
4✔
386
{
387
    return script_->is_roller() || (prevout && prevout->script().is_roller());
4✔
388
}
389

390
// static
391
bool input::is_relative_locktime_applied(uint32_t sequence) NOEXCEPT
10✔
392
{
393
    // BIP68: if bit 31 is set then no consensus meaning is applied.
394
    return !get_right(sequence, relative_locktime_disabled_bit);
10✔
395
}
396

397
// static
398
bool input::is_relative_locked(uint32_t sequence, size_t height,
10✔
399
    uint32_t median_time_past, size_t prevout_height,
400
    uint32_t prevout_median_time_past) NOEXCEPT
401
{
402
    if (!is_relative_locktime_applied(sequence))
10✔
403
        return false;
404

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

408
    // BIP68: bit 22 determines if relative lock is time or block based.
409
    if (get_right(sequence, relative_locktime_time_locked_bit))
8✔
410
    {
411
        // BIP68: references to median time past are as defined by bip113.
412
        // BIP68: change sequence to seconds by shift up by 9 bits (x 512).
413
        auto time = shift_left(blocks, relative_locktime_seconds_shift_left);
3✔
414
        auto age = floored_subtract(median_time_past, prevout_median_time_past);
3✔
415
        return age < time;
3✔
416
    }
417

418
    // BIP68: when the relative lock time is block based, it is interpreted as
419
    // a minimum block height constraint over the age of the input.
420
    const auto age = floored_subtract(height, prevout_height);
5✔
421
    return age < blocks;
5✔
422
}
423

424
bool input::is_relative_locked(size_t height,
10✔
425
    uint32_t median_time_past) const NOEXCEPT
426
{
427
    // Prevout must be found and height/median_time_past metadata populated.
428
    ////BC_ASSERT(!is_zero(metadata.height));
429
    return is_relative_locked(sequence_, height, median_time_past,
10✔
430
        metadata.height, metadata.median_time_past);
10✔
431
}
432

NEW
433
bool input::reserved_hash(hash_cref& out) const NOEXCEPT
×
434
{
NEW
435
    const auto& stack = get_witness().stack();
×
NEW
436
    if (!witness::is_reserved_pattern(stack))
×
437
        return false;
438

439
    // Guarded by is_reserved_pattern.
NEW
440
    out = unsafe_array_cast<uint8_t, hash_size>(stack.front()->data());
×
UNCOV
441
    return true;
×
442
}
443

444
// private
445
// prevout_script is only used to determine is_pay_script_hash_pattern.
446
bool input::extract_sigop_script(chain::script& out,
×
447
    const chain::script& prevout_script) const NOEXCEPT
448
{
449
    // There are no embedded sigops when the prevout script is not p2sh.
450
    if (!script::is_pay_script_hash_pattern(prevout_script.ops()))
×
451
        return false;
452

453
    // There are no embedded sigops when the input script is not push only.
454
    const auto& ops = script_->ops();
×
455
    if (ops.empty() || !script::is_relaxed_push_pattern(ops))
×
456
        return false;
×
457

458
    // Parse the embedded script from the last input script item (data).
459
    // This cannot fail because there is no prefix to invalidate the length.
460
    out = { ops.back().data(), false };
×
461
    return true;
×
462
}
463

464
// TODO: Prior to block 79400 sigops were limited only by policy.
465
// TODO: Create legacy sigops fork/flag and pass here, return 0 if false.
466
// TODO: this was an unbipped flag day soft fork, prior to BIP16/141.
467
// TODO: if (nHeight > 79400 && GetSigOpCount() > MAX_BLOCK_SIGOPS).
468
size_t input::signature_operations(bool bip16, bool bip141) const NOEXCEPT
7✔
469
{
470
    // Penalize quadratic signature operations (bip141).
471
    const auto factor = bip141 ? heavy_sigops_factor : one;
7✔
472
    const auto sigops = script_->signature_operations(false) * factor;
7✔
473

474
    // ************************************************************************
475
    // CONSENSUS: coinbase input cannot execute, but sigops are counted anyway.
476
    // ************************************************************************
477
    if (!prevout)
7✔
478
        return sigops;
479

480
    // Null prevout/input (coinbase) cannot have witness or embedded script.
481
    // Embedded/witness scripts are deserialized here and again on scipt eval.
482

483
    chain::script witness;
×
484
    if (bip141 && get_witness().extract_sigop_script(witness, prevout->script()))
×
485
    {
486
        // Add sigops in the witness script (bip141).
487
        return ceilinged_add(sigops, witness.signature_operations(true));
×
488
    }
489

490
    chain::script embedded;
×
491
    if (bip16 && extract_sigop_script(embedded, prevout->script()))
×
492
    {
493
        if (bip141 && get_witness().extract_sigop_script(witness, embedded))
×
494
        {
495
            // Add sigops in the embedded witness script (bip141).
496
            return ceilinged_add(sigops, witness.signature_operations(true));
×
497
        }
498
        else
499
        {
500
            // Add heavy sigops in the embedded script (bip16).
501
            return ceilinged_add(sigops, embedded.signature_operations(true) *
×
502
                factor);
503
        }
504
    }
505

506
    return sigops;
507
}
×
508

509
BC_POP_WARNING()
510

511
// JSON value convertors.
512
// ----------------------------------------------------------------------------
513

514
namespace json = boost::json;
515

516
// boost/json will soon have NOEXCEPT: github.com/boostorg/json/pull/636
517
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
518

519
input tag_invoke(json::value_to_tag<input>, const json::value& value) NOEXCEPT
5✔
520
{
521
    return
5✔
522
    {
523
        json::value_to<chain::point>(value.at("point")),
5✔
524
        json::value_to<chain::script>(value.at("script")),
5✔
525
        json::value_to<chain::witness>(value.at("witness")),
10✔
526
        value.at("sequence").to_number<uint32_t>()
10✔
527
    };
5✔
528
}
529

530
void tag_invoke(json::value_from_tag, json::value& value,
10✔
531
    const input& input) NOEXCEPT
532
{
533
    value =
20✔
534
    {
535
        { "point", input.point() },
536
        { "script", input.script() },
537
        { "witness", input.witness() },
538
        { "sequence", input.sequence() }
539
    };
10✔
540
}
10✔
541

542
BC_POP_WARNING()
543

544
input::cptr tag_invoke(json::value_to_tag<input::cptr>,
×
545
    const json::value& value) NOEXCEPT
546
{
547
    return to_shared(tag_invoke(json::value_to_tag<input>{}, value));
×
548
}
549

550
// Shared pointer overload is required for navigation.
551
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
552
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
553

554
void tag_invoke(json::value_from_tag tag, json::value& value,
8✔
555
    const input::cptr& input) NOEXCEPT
556
{
557
    tag_invoke(tag, value, *input);
8✔
558
}
8✔
559

560
BC_POP_WARNING()
561
BC_POP_WARNING()
562

563
} // namespace chain
564
} // namespace system
565
} // 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