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

libbitcoin / libbitcoin-system / 10442817098

18 Aug 2024 06:22PM UTC coverage: 83.245% (-0.03%) from 83.273%
10442817098

push

github

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

Pass allocator to block shared_ptr constructions.

28 of 31 new or added lines in 9 files covered. (90.32%)

4 existing lines in 2 files now uncovered.

10056 of 12080 relevant lines covered (83.25%)

4780448.39 hits per line

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

76.0
/src/chain/input.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/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
UNCOV
49
const witness& input::no_witness() NOEXCEPT
×
50
{
UNCOV
51
    static const chain::witness empty_witness{};
×
UNCOV
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,959✔
68
{
69
    return witness_ ? *witness_ : no_witness();
1,959✔
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)
88✔
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)
320✔
98
{
99
}
160✔
100

101
input::input(const chain::point& point, const chain::script& script,
734✔
102
    uint32_t sequence) NOEXCEPT
734✔
103
  : input(
104
      to_shared(point),
734✔
105
      to_shared(script),
734✔
106
      to_shared<chain::witness>(),
734✔
107
      sequence, true)
1,468✔
108
{
109
}
734✔
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(const data_slice& data) NOEXCEPT
5✔
148
  : input(stream::in::copy(data))
5✔
149
{
150
}
5✔
151

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

157
input::input(stream::in::fast& stream) NOEXCEPT
1✔
158
  : input(read::bytes::fast(stream))
1✔
159
{
160
}
1✔
161

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

167
input::input(std::istream& stream) NOEXCEPT
3✔
168
  : input(read::bytes::istream(stream))
3✔
169
{
170
}
3✔
171

172
input::input(reader&& source) NOEXCEPT
9✔
173
  : input(source)
9✔
174
{
175
}
×
176

177
// Witness is deserialized and assigned by transaction.
178
input::input(reader& source) NOEXCEPT
255✔
179
  : point_(CREATE(chain::point, source.get_allocator(), source)),
255✔
180
    script_(CREATE(chain::script, source.get_allocator(), source, true)),
255✔
181
    witness_(CREATE(chain::witness, source.get_allocator())),
255✔
182
    sequence_(source.read_4_bytes_little_endian()),
255✔
183
    valid_(source),
255✔
184
    size_(serialized_size(*script_))
510✔
185
{
186
}
255✔
187

188
// protected
189
input::input(const chain::point::cptr& point, const chain::script::cptr& script,
931✔
190
    const chain::witness::cptr& witness, uint32_t sequence, bool valid) NOEXCEPT
931✔
191
  : point_(point),
192
    script_(script),
193
    witness_(witness),
194
    sequence_(sequence),
931✔
195
    valid_(valid),
931✔
196
    size_(serialized_size(*script, *witness))
1,862✔
197
{
198
}
931✔
199

200
// Operators.
201
// ----------------------------------------------------------------------------
202

203
bool input::operator==(const input& other) const NOEXCEPT
43✔
204
{
205
    return (sequence_ == other.sequence_)
43✔
206
        && (point_ == other.point_ || *point_ == *other.point_)
41✔
207
        && (script_ == other.script_ || *script_ == *other.script_)
41✔
208
        && (witness_ == other.witness_ || get_witness() == other.get_witness());
84✔
209
}
210

211
bool input::operator!=(const input& other) const NOEXCEPT
2✔
212
{
213
    return !(*this == other);
2✔
214
}
215

216
// Deserialization.
217
// ----------------------------------------------------------------------------
218

219
// Serialization.
220
// ----------------------------------------------------------------------------
221

222
data_chunk input::to_data() const NOEXCEPT
1✔
223
{
224
    data_chunk data(serialized_size(false));
1✔
225
    stream::out::copy ostream(data);
1✔
226
    to_data(ostream);
1✔
227
    return data;
2✔
228
}
1✔
229

230
void input::to_data(std::ostream& stream) const NOEXCEPT
2✔
231
{
232
    write::bytes::ostream out(stream);
2✔
233
    to_data(out);
2✔
234
}
2✔
235

236
// Witness is serialized by transaction.
237
void input::to_data(writer& sink) const NOEXCEPT
1,392✔
238
{
239
    point_->to_data(sink);
1,392✔
240
    script_->to_data(sink, true);
1,392✔
241
    sink.write_4_bytes_little_endian(sequence_);
1,392✔
242
}
1,392✔
243

244
// static/private
245
input::sizes input::serialized_size(const chain::script& script) NOEXCEPT
255✔
246
{
247
    constexpr auto const_size = ceilinged_add(point::serialized_size(),
255✔
248
        sizeof(sequence_));
249

250
    const auto nominal_size = ceilinged_add(const_size,
255✔
251
        script.serialized_size(true));
252

253
    return { nominal_size, zero };
255✔
254
}
255

256
// static/private
257
input::sizes input::serialized_size(const chain::script& script,
931✔
258
    const chain::witness& witness) NOEXCEPT
259
{
260
    constexpr auto const_size = ceilinged_add(point::serialized_size(),
931✔
261
        sizeof(sequence_));
262

263
    const auto nominal_size = ceilinged_add(const_size,
931✔
264
        script.serialized_size(true));
265

266
    const auto witnessed_size = ceilinged_add(nominal_size,
931✔
267
        witness.serialized_size(true));
268

269
    return { nominal_size, witnessed_size };
931✔
270
}
271

272
// input.serialized_size(witness) provides sizing for witness, however
273
// witnesses are serialized by the transaction. This is an ugly hack as a
274
// consequence of bip144 not serializing witnesses as part of inputs, which
275
// is logically the proper association.
276
size_t input::serialized_size(bool witness) const NOEXCEPT
2✔
277
{
278
    return witness ? size_.witnessed : size_.nominal;
2✔
279
}
280

281
// Friend accessors (private).
282
// ----------------------------------------------------------------------------
283

284
size_t input::nominal_size() const NOEXCEPT
1,156✔
285
{
286
    return size_.nominal;
1,156✔
287
}
288

289
size_t input::witnessed_size() const NOEXCEPT
32✔
290
{
291
    return size_.witnessed;
32✔
292
}
293

294
////void input::set_witness(reader& source) NOEXCEPT
295
////{
296
////    witness_ = to_shared<chain::witness>(source, true);
297
////    size_.witnessed = ceilinged_add(size_.nominal,
298
////        witness_->serialized_size(true));
299
////}
300

301
void input::set_witness(reader& source) NOEXCEPT
20✔
302
{
303
    auto& allocator = source.get_allocator();
20✔
304
    witness_.reset(CREATE(chain::witness, allocator, source, true));
20✔
305
    size_.witnessed = ceilinged_add(size_.nominal,
20✔
306
        witness_->serialized_size(true));
307
}
20✔
308

309
// Properties.
310
// ----------------------------------------------------------------------------
311

312
bool input::is_valid() const NOEXCEPT
16✔
313
{
314
    return valid_;
16✔
315
}
316

317
const point& input::point() const NOEXCEPT
169✔
318
{
319
    return *point_;
10✔
320
}
321

322
const chain::script& input::script() const NOEXCEPT
75✔
323
{
324
    return *script_;
10✔
325
}
326

327
const chain::witness& input::witness() const NOEXCEPT
1,893✔
328
{
329
    return get_witness();
1,893✔
330
}
331

332
const point::cptr& input::point_ptr() const NOEXCEPT
×
333
{
334
    return point_;
×
335
}
336

337
const chain::script::cptr& input::script_ptr() const NOEXCEPT
1,555✔
338
{
339
    return script_;
1,555✔
340
}
341

342
const chain::witness::cptr& input::witness_ptr() const NOEXCEPT
×
343
{
344
    return get_witness_cptr();
×
345
}
346

347
uint32_t input::sequence() const NOEXCEPT
73✔
348
{
349
    return sequence_;
10✔
350
}
351

352
// Methods.
353
// ----------------------------------------------------------------------------
354

355
bool input::is_final() const NOEXCEPT
22✔
356
{
357
    return sequence_ == max_input_sequence;
22✔
358
}
359

360
bool input::is_roller() const NOEXCEPT
4✔
361
{
362
    return script_->is_roller() || (prevout && prevout->script().is_roller());
4✔
363
}
364

365
// static
366
bool input::is_locked(uint32_t sequence, size_t height,
10✔
367
    uint32_t median_time_past, size_t prevout_height,
368
    uint32_t prevout_median_time_past) NOEXCEPT
369
{
370
    // BIP68: if bit 31 is set then no consensus meaning is applied.
371
    if (get_right(sequence, relative_locktime_disabled_bit))
10✔
372
        return false;
373

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

377
    // BIP68: bit 22 determines if relative lock is time or block based.
378
    if (get_right(sequence, relative_locktime_time_locked_bit))
8✔
379
    {
380
        // BIP68: change sequence to seconds by shift up by 9 bits (x 512).
381
        auto time = shift_left(blocks, relative_locktime_seconds_shift_left);
3✔
382
        auto age = floored_subtract(median_time_past, prevout_median_time_past);
3✔
383
        return age < time;
3✔
384
    }
385

386
    const auto age = floored_subtract(height, prevout_height);
5✔
387
    return age < blocks;
5✔
388
}
389

390
bool input::is_locked(size_t height, uint32_t median_time_past) const NOEXCEPT
10✔
391
{
392
    // Prevout must be found and height/median_time_past metadata populated.
393
    ////BC_ASSERT(!is_zero(metadata.height));
394
    return is_locked(sequence_, height, median_time_past, metadata.height,
10✔
395
        metadata.median_time_past);
10✔
396
}
397

398
bool input::reserved_hash(hash_digest& out) const NOEXCEPT
×
399
{
400
    if (!witness::is_reserved_pattern(get_witness().stack()))
×
401
        return false;
402

403
    std::copy_n(get_witness().stack().front()->begin(), hash_size, out.begin());
×
404
    return true;
×
405
}
406

407
// private
408
// prevout_script is only used to determine is_pay_script_hash_pattern.
409
bool input::extract_sigop_script(chain::script& out,
×
410
    const chain::script& prevout_script) const NOEXCEPT
411
{
412
    // There are no embedded sigops when the prevout script is not p2sh.
413
    if (!script::is_pay_script_hash_pattern(prevout_script.ops()))
×
414
        return false;
415

416
    // There are no embedded sigops when the input script is not push only.
417
    const auto& ops = script_->ops();
×
418
    if (ops.empty() || !script::is_relaxed_push_pattern(ops))
×
419
        return false;
×
420

421
    // Parse the embedded script from the last input script item (data).
422
    // This cannot fail because there is no prefix to invalidate the length.
423
    out = { ops.back().data(), false };
×
424
    return true;
×
425
}
426

427
// TODO: Prior to block 79400 sigops were limited only by policy.
428
// TODO: Create legacy sigops fork/flag and pass here, return 0 if false.
429
// TODO: this was an unbipped flag day soft fork, prior to BIP16/141.
430
// TODO: if (nHeight > 79400 && GetSigOpCount() > MAX_BLOCK_SIGOPS).
431
size_t input::signature_operations(bool bip16, bool bip141) const NOEXCEPT
7✔
432
{
433
    // Penalize quadratic signature operations (bip141).
434
    const auto factor = bip141 ? heavy_sigops_factor : one;
7✔
435
    const auto sigops = script_->signature_operations(false) * factor;
7✔
436

437
    // ************************************************************************
438
    // CONSENSUS: coinbase input cannot execute, but sigops are counted anyway.
439
    // ************************************************************************
440
    if (!prevout)
7✔
441
        return sigops;
442

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

446
    chain::script witness;
×
447
    if (bip141 && get_witness().extract_sigop_script(witness, prevout->script()))
×
448
    {
449
        // Add sigops in the witness script (bip141).
450
        return ceilinged_add(sigops, witness.signature_operations(true));
×
451
    }
452

453
    chain::script embedded;
×
454
    if (bip16 && extract_sigop_script(embedded, prevout->script()))
×
455
    {
456
        if (bip141 && get_witness().extract_sigop_script(witness, embedded))
×
457
        {
458
            // Add sigops in the embedded witness script (bip141).
459
            return ceilinged_add(sigops, witness.signature_operations(true));
×
460
        }
461
        else
462
        {
463
            // Add heavy sigops in the embedded script (bip16).
464
            return ceilinged_add(sigops, embedded.signature_operations(true) *
×
465
                factor);
466
        }
467
    }
468

469
    return sigops;
470
}
×
471

472
BC_POP_WARNING()
473

474
// JSON value convertors.
475
// ----------------------------------------------------------------------------
476

477
namespace json = boost::json;
478

479
// boost/json will soon have NOEXCEPT: github.com/boostorg/json/pull/636
480
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
481

482
input tag_invoke(json::value_to_tag<input>, const json::value& value) NOEXCEPT
5✔
483
{
484
    return
5✔
485
    {
486
        json::value_to<chain::point>(value.at("point")),
5✔
487
        json::value_to<chain::script>(value.at("script")),
5✔
488
        json::value_to<chain::witness>(value.at("witness")),
10✔
489
        value.at("sequence").to_number<uint32_t>()
10✔
490
    };
5✔
491
}
492

493
void tag_invoke(json::value_from_tag, json::value& value,
10✔
494
    const input& input) NOEXCEPT
495
{
496
    value =
20✔
497
    {
498
        { "point", input.point() },
499
        { "script", input.script() },
500
        { "witness", input.witness() },
501
        { "sequence", input.sequence() }
502
    };
10✔
503
}
10✔
504

505
BC_POP_WARNING()
506

507
input::cptr tag_invoke(json::value_to_tag<input::cptr>,
×
508
    const json::value& value) NOEXCEPT
509
{
510
    return to_shared(tag_invoke(json::value_to_tag<input>{}, value));
×
511
}
512

513
// Shared pointer overload is required for navigation.
514
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
515
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
516

517
void tag_invoke(json::value_from_tag tag, json::value& value,
8✔
518
    const input::cptr& input) NOEXCEPT
519
{
520
    tag_invoke(tag, value, *input);
8✔
521
}
8✔
522

523
BC_POP_WARNING()
524
BC_POP_WARNING()
525

526
} // namespace chain
527
} // namespace system
528
} // 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