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

libbitcoin / libbitcoin-system / 14945433772

10 May 2025 12:40PM UTC coverage: 82.466% (-0.01%) from 82.479%
14945433772

push

github

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

Refactoring to support tapscript signature hash generation.

48 of 63 new or added lines in 6 files covered. (76.19%)

10 existing lines in 6 files now uncovered.

10286 of 12473 relevant lines covered (82.47%)

3862936.9 hits per line

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

74.36
/src/chain/transaction.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/transaction.hpp>
20

21
#include <algorithm>
22
#include <iterator>
23
#include <numeric>
24
#include <type_traits>
25
#include <utility>
26
#include <vector>
27
#include <bitcoin/system/chain/context.hpp>
28
#include <bitcoin/system/chain/enums/coverage.hpp>
29
#include <bitcoin/system/chain/enums/magic_numbers.hpp>
30
#include <bitcoin/system/chain/header.hpp>
31
#include <bitcoin/system/chain/input.hpp>
32
#include <bitcoin/system/chain/output.hpp>
33
#include <bitcoin/system/chain/script.hpp>
34
#include <bitcoin/system/data/data.hpp>
35
#include <bitcoin/system/define.hpp>
36
#include <bitcoin/system/error/error.hpp>
37
#include <bitcoin/system/hash/hash.hpp>
38
#include <bitcoin/system/machine/machine.hpp>
39
#include <bitcoin/system/math/math.hpp>
40
#include <bitcoin/system/stream/stream.hpp>
41

42
namespace libbitcoin {
43
namespace system {
44
namespace chain {
45

46
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
47

48
// Constructors.
49
// ----------------------------------------------------------------------------
50

51
transaction::transaction() NOEXCEPT
21✔
52
  : transaction(0,
53
      to_shared<input_cptrs>(),
21✔
54
      to_shared<output_cptrs>(),
21✔
55
      0, false, false)
42✔
56
{
57
}
21✔
58

59
transaction::transaction(uint32_t version, chain::inputs&& inputs,
905✔
60
    chain::outputs&& outputs, uint32_t locktime) NOEXCEPT
905✔
61
  : transaction(version, to_shareds(std::move(inputs)),
905✔
62
      to_shareds(std::move(outputs)), locktime)
2,715✔
63
{
64
}
905✔
65

66
transaction::transaction(uint32_t version, const chain::inputs& inputs,
1✔
67
    const chain::outputs& outputs, uint32_t locktime) NOEXCEPT
1✔
68
  : transaction(version, to_shareds(inputs), to_shareds(outputs), locktime,
1✔
69
      segregated(inputs), true)
3✔
70
{
71
}
1✔
72

73
transaction::transaction(uint32_t version, const inputs_cptr& inputs,
905✔
74
    const outputs_cptr& outputs, uint32_t locktime) NOEXCEPT
905✔
75
  : transaction(version, inputs, outputs, locktime, segregated(*inputs), true)
905✔
76
{
77
}
905✔
78

79
transaction::transaction(stream::in::fast&& stream, bool witness) NOEXCEPT
41✔
80
  : transaction(read::bytes::fast(stream), witness)
41✔
81
{
82
}
41✔
83

84
transaction::transaction(stream::in::fast& stream, bool witness) NOEXCEPT
2✔
85
  : transaction(read::bytes::fast(stream), witness)
2✔
86
{
87
}
2✔
88

89
transaction::transaction(std::istream&& stream, bool witness) NOEXCEPT
×
90
  : transaction(read::bytes::istream(stream), witness)
×
91
{
92
}
×
93

94
transaction::transaction(std::istream& stream, bool witness) NOEXCEPT
4✔
95
  : transaction(read::bytes::istream(stream), witness)
4✔
96
{
97
}
4✔
98

99
transaction::transaction(reader&& source, bool witness) NOEXCEPT
47✔
100
  : transaction(source, witness)
47✔
101
{
102
}
×
103

104
transaction::transaction(reader& source, bool witness) NOEXCEPT
204✔
105
  : version_(source.read_4_bytes_little_endian()),
408✔
106
    inputs_(CREATE(input_cptrs, source.get_allocator())),
204✔
107
    outputs_(CREATE(output_cptrs, source.get_allocator()))
612✔
108
{
109
    assign_data(source, witness);
204✔
110
}
204✔
111

112
// protected
113
transaction::transaction(uint32_t version,
927✔
114
    const chain::inputs_cptr& inputs, const chain::outputs_cptr& outputs,
115
    uint32_t locktime, bool segregated, bool valid) NOEXCEPT
927✔
116
  : version_(version),
927✔
117
    inputs_(inputs ? inputs : to_shared<input_cptrs>()),
1,854✔
118
    outputs_(outputs ? outputs : to_shared<output_cptrs>()),
927✔
119
    locktime_(locktime),
927✔
120
    segregated_(segregated),
927✔
121
    valid_(valid),
927✔
122
    size_(serialized_size(*inputs, *outputs, segregated))
1,854✔
123
{
124
}
927✔
125

126
// Operators.
127
// ----------------------------------------------------------------------------
128

129
bool transaction::operator==(const transaction& other) const NOEXCEPT
60✔
130
{
131
    // Compares input/output elements, not pointers, cache not compared.
132
    return (version_ == other.version_)
60✔
133
        && (locktime_ == other.locktime_)
58✔
134
        && ((inputs_ == other.inputs_) || 
84✔
135
            deep_equal(*inputs_, *other.inputs_))
26✔
136
        && ((outputs_ == other.outputs_) ||
144✔
137
            deep_equal(*outputs_, *other.outputs_));
26✔
138
}
139

140
bool transaction::operator!=(const transaction& other) const NOEXCEPT
2✔
141
{
142
    return !(*this == other);
2✔
143
}
144

145
// Deserialization.
146
// ----------------------------------------------------------------------------
147

148
// private
149
BC_PUSH_WARNING(NO_UNGUARDED_POINTERS)
150
void transaction::assign_data(reader& source, bool witness) NOEXCEPT
204✔
151
{
152
    auto& allocator = source.get_allocator();
204✔
153
    auto ins = to_non_const_raw_ptr(inputs_);
204✔
154
    auto count = source.read_size(max_block_size);
204✔
155
    ins->reserve(count);
204✔
156
    for (size_t in = 0; in < count; ++in)
453✔
157
        ins->emplace_back(CREATE(input, allocator, source));
249✔
158

159
    // Expensive repeated recomputation, so cache segregated state.
160
    // Detect witness as no inputs (marker) and expected flag (bip144).
161
    segregated_ = 
204✔
162
        inputs_->size() == witness_marker &&
220✔
163
        source.peek_byte() == witness_enabled;
16✔
164

165
    if (segregated_)
204✔
166
    {
167
        // Skip over the peeked witness flag.
168
        source.skip_byte();
16✔
169

170
        count = source.read_size(max_block_size);
16✔
171
        ins->reserve(count);
16✔
172
        for (size_t in = 0; in < count; ++in)
37✔
173
            ins->emplace_back(CREATE(input, allocator, source));
21✔
174

175
        auto outs = to_non_const_raw_ptr(outputs_);
16✔
176
        count = source.read_size(max_block_size);
16✔
177
        outs->reserve(count);
16✔
178
        for (size_t out = 0; out < count; ++out)
41✔
179
            outs->emplace_back(CREATE(output, allocator, source));
25✔
180

181
        // Read or skip witnesses as specified.
182
        if (witness)
16✔
183
        {
184
            for (auto& input: *inputs_)
35✔
185
                to_non_const_raw_ptr(input)->set_witness(source);
20✔
186
        }
187
        else
188
        {
189
            // Default witness is populated on input construct.
190
            for (size_t in = 0; in < inputs_->size(); ++in)
2✔
191
                witness::skip(source, true);
1✔
192
        }
193
    }
194
    else
195
    {
196
        auto outs = to_non_const_raw_ptr(outputs_);
188✔
197
        count = source.read_size(max_block_size);
188✔
198
        outs->reserve(count);
188✔
199
        for (size_t out = 0; out < count; ++out)
437✔
200
            outs->emplace_back(CREATE(output, allocator, source));
249✔
201
    }
202

203
    locktime_ = source.read_4_bytes_little_endian();
204✔
204
    size_ = serialized_size(*inputs_, *outputs_, segregated_);
204✔
205
    valid_ = source;
204✔
206
}
204✔
207
BC_POP_WARNING()
208

209
// Serialization.
210
// ----------------------------------------------------------------------------
211

212
// Transactions with empty witnesses always use old serialization (bip144).
213
// If no inputs are witness programs then witness hash is tx hash (bip141).
214
data_chunk transaction::to_data(bool witness) const NOEXCEPT
10✔
215
{
216
    witness &= segregated_;
10✔
217

218
    data_chunk data(serialized_size(witness));
10✔
219
    stream::out::fast ostream(data);
10✔
220
    write::bytes::fast out(ostream);
10✔
221
    to_data(out, witness);
10✔
222
    return data;
20✔
223
}
10✔
224

225
void transaction::to_data(std::ostream& stream, bool witness) const NOEXCEPT
1✔
226
{
227
    witness &= segregated_;
1✔
228

229
    write::bytes::ostream out(stream);
1✔
230
    to_data(out, witness);
1✔
231
}
1✔
232

233
void transaction::to_data(writer& sink, bool witness) const NOEXCEPT
966✔
234
{
235
    witness &= segregated_;
966✔
236

237
    sink.write_4_bytes_little_endian(version_);
966✔
238

239
    if (witness)
966✔
240
    {
241
        sink.write_byte(witness_marker);
2✔
242
        sink.write_byte(witness_enabled);
2✔
243
    }
244

245
    sink.write_variable(inputs_->size());
966✔
246
    for (const auto& input: *inputs_)
2,352✔
247
        input->to_data(sink);
1,386✔
248

249
    sink.write_variable(outputs_->size());
966✔
250
    for (const auto& output: *outputs_)
2,634✔
251
        output->to_data(sink);
1,668✔
252

253
    if (witness)
966✔
254
        for (auto& input: *inputs_)
5✔
255
            input->witness().to_data(sink, true);
3✔
256

257
    sink.write_4_bytes_little_endian(locktime_);
966✔
258
}
966✔
259

260
// static/private
261
transaction::sizes transaction::serialized_size(const input_cptrs& inputs,
1,131✔
262
    const output_cptrs& outputs, bool segregated) NOEXCEPT
263
{
264
    sizes size{ zero, zero };
1,131✔
265

266
    std::for_each(inputs.begin(), inputs.end(), [&](const auto& in) NOEXCEPT
2,314✔
267
    {
268
        size.nominal = ceilinged_add(size.nominal, in->nominal_size());
1,183✔
269
        if (segregated)
1,183✔
270
            size.witnessed = ceilinged_add(size.witnessed, in->witnessed_size());
64✔
271
    });
1,183✔
272

273
    const auto outs = [](size_t total, const auto& output) NOEXCEPT
394✔
274
    {
275
        return ceilinged_add(total, output->serialized_size());
394✔
276
    };
277

278
    constexpr auto base_const_size = sizeof(version_) + sizeof(locktime_);
1,131✔
279
    constexpr auto witness_const_size = sizeof(witness_marker) +
1,131✔
280
        sizeof(witness_enabled);
281

282
    const auto base_size =
1,131✔
283
        ceilinged_add(ceilinged_add(ceilinged_add(base_const_size,
1,131✔
284
            variable_size(inputs.size())), variable_size(outputs.size())),
285
            std::accumulate(outputs.begin(), outputs.end(), zero, outs));
286

287
    const auto nominal_size = ceilinged_add(base_size, size.nominal);
1,131✔
288

289
    // For non-segregated transactions, witnessed_size is nominal_size.
290
    const auto witnessed_size = segregated ? ceilinged_add(ceilinged_add(
1,131✔
291
        base_size, witness_const_size), size.witnessed) : nominal_size;
292

293
    // For non-segregated transactions, values are the same.
294
    return { nominal_size, witnessed_size };
1,131✔
295
}
296

297
size_t transaction::serialized_size(bool witness) const NOEXCEPT
613✔
298
{
299
    witness &= segregated_;
613✔
300

301
    return witness ? size_.witnessed : size_.nominal;
613✔
302
}
303

304
// Properties.
305
// ----------------------------------------------------------------------------
306

307
bool transaction::is_valid() const NOEXCEPT
777✔
308
{
309
    return valid_;
777✔
310
}
311

312
size_t transaction::spends() const NOEXCEPT
×
313
{
314
    return is_coinbase() ? zero : inputs_->size();
×
315
}
316

317
size_t transaction::inputs() const NOEXCEPT
1,569✔
318
{
319
    return inputs_->size();
1,569✔
320
}
321

322
size_t transaction::outputs() const NOEXCEPT
1✔
323
{
324
    return outputs_->size();
1✔
325
}
326

327
uint32_t transaction::version() const NOEXCEPT
6✔
328
{
329
    return version_;
4✔
330
}
331

332
uint32_t transaction::locktime() const NOEXCEPT
13✔
333
{
334
    return locktime_;
4✔
335
}
336

337
const inputs_cptr& transaction::inputs_ptr() const NOEXCEPT
2,446✔
338
{
339
    return inputs_;
2,446✔
340
}
341

342
const outputs_cptr& transaction::outputs_ptr() const NOEXCEPT
62✔
343
{
344
    return outputs_;
62✔
345
}
346

347
uint64_t transaction::fee() const NOEXCEPT
4✔
348
{
349
    // Underflow returns zero (and is_overspent() will be true).
350
    // This is value of prevouts spent by inputs minus that claimed by outputs.
351
    return floored_subtract(value(), claim());
4✔
352
}
353

354
void transaction::set_nominal_hash(const hash_digest& hash) const NOEXCEPT
20✔
355
{
356
    nominal_hash_ = hash;
20✔
357
}
1✔
358

359
void transaction::set_witness_hash(const hash_digest& hash) const NOEXCEPT
2✔
360
{
361
    witness_hash_ = hash;
2✔
362
}
1✔
363

364
const hash_digest& transaction::get_hash(bool witness) const NOEXCEPT
26✔
365
{
366
    if (witness)
26✔
367
    {
368
        if (!witness_hash_) set_witness_hash(hash(witness));
4✔
369
        return *witness_hash_;
3✔
370
    }
371
    else
372
    {
373
        if (!nominal_hash_) set_nominal_hash(hash(witness));
42✔
374
        return *nominal_hash_;
23✔
375
    }
376
}
377

378
hash_digest transaction::hash(bool witness) const NOEXCEPT
934✔
379
{
380
    if (segregated_)
934✔
381
    {
382
        if (witness)
23✔
383
        {
384
            // Witness coinbase tx hash is assumed to be null_hash (bip141).
385
            if (witness_hash_) return *witness_hash_;
×
386
            if (is_coinbase()) return null_hash;
×
387
        }
388
        else
389
        {
390
            if (nominal_hash_) return *nominal_hash_;
23✔
391
        }
392
    }
393
    else
394
    {
395
        if (nominal_hash_) return *nominal_hash_;
911✔
396
    }
397

398
    hash_digest digest{};
927✔
399
    stream::out::fast stream{ digest };
927✔
400
    hash::sha256x2::fast sink{ stream };
927✔
401
    to_data(sink, witness);
927✔
402
    sink.flush();
927✔
403
    return digest;
927✔
404
}
927✔
405

406
// static
407
hash_digest transaction::desegregated_hash(size_t witnessed,
×
408
    size_t unwitnessed, const uint8_t* data) NOEXCEPT
409
{
410
    if (is_null(data))
×
411
        return null_hash;
×
412

413
    constexpr auto preamble = sizeof(uint32_t) + two * sizeof(uint8_t);
×
414
    const auto puts = floored_subtract(unwitnessed, two * sizeof(uint32_t));
×
415
    const auto locktime = floored_subtract(witnessed, sizeof(uint32_t));
×
416

417
    hash_digest digest{};
×
418
    stream::out::fast stream{ digest };
×
419
    hash::sha256x2::fast sink{ stream };
×
420
    sink.write_bytes(data, sizeof(uint32_t));
×
421
    sink.write_bytes(std::next(data, preamble), puts);
×
422
    sink.write_bytes(std::next(data, locktime), sizeof(uint32_t));
×
423
    sink.flush();
×
424
    return digest;
×
425
}
×
426

427
// Methods.
428
// ----------------------------------------------------------------------------
429

430
bool transaction::is_dusty(uint64_t minimum_output_value) const NOEXCEPT
6✔
431
{
432
    const auto dusty = [=](const auto& output) NOEXCEPT
9✔
433
    {
434
        return output->is_dust(minimum_output_value);
9✔
435
    };
6✔
436

437
    return std::any_of(outputs_->begin(), outputs_->end(), dusty);
6✔
438
}
439

440
size_t transaction::signature_operations(bool bip16, bool bip141) const NOEXCEPT
1✔
441
{
442
    // Includes BIP16 p2sh additional sigops, max_size_t if prevout invalid.
443
    const auto in = [=](size_t total, const auto& input) NOEXCEPT
×
444
    {
445
        return ceilinged_add(total, input->signature_operations(bip16, bip141));
×
446
    };
1✔
447

448
    const auto out = [=](size_t total, const auto& output) NOEXCEPT
×
449
    {
450
        return ceilinged_add(total, output->signature_operations(bip141));
×
451
    };
1✔
452

453
    // Overflow returns max_size_t.
454
    return ceilinged_add(
1✔
455
        std::accumulate(inputs_->begin(), inputs_->end(), zero, in),
456
        std::accumulate(outputs_->begin(), outputs_->end(), zero, out));
1✔
457
}
458

459
// private
460
chain::points transaction::points() const NOEXCEPT
4✔
461
{
462
    chain::points out(inputs_->size());
4✔
463

464
    const auto point = [](const auto& input) NOEXCEPT
8✔
465
    {
466
        return input->point();
8✔
467
    };
468

469
    std::transform(inputs_->begin(), inputs_->end(), out.begin(), point);
4✔
470
    return out;
4✔
471
}
472

473
// Signatures (public)
474
// ----------------------------------------------------------------------------
475

476
hash_digest transaction::signature_hash(const input_iterator& input,
51✔
477
    const script& subscript, uint64_t value, uint8_t sighash_flags,
478
    script_version version, bool bip143, bool bip342) const NOEXCEPT
479
{
480
    // There is no rational interpretation of a signature hash for a coinbase.
481
    BC_ASSERT(!is_coinbase());
51✔
482

483
    // This is where the connection between bip141 and bip143 is made. If a
484
    // versioned 1 program (segwit) extracted by bip141 but bip143 (segwit
485
    // hashing) is not active, then drop down to unversioned signature hashing.
486
    if (bip143 && version == script_version::segwit)
51✔
487
        return version_0_sighash(input, subscript, value, sighash_flags);
22✔
488

489
    // This is where the connection between bip341 and bip342 is made. If a
490
    // version 2 program (taproot) extracted by bip341 but bip342 (tapscript)
491
    // is not active then drop down to unversioned signature hashing. 
492
    if (bip342 && version == script_version::taproot)
29✔
NEW
493
        return version_1_sighash(input, subscript, value, sighash_flags);
×
494

495
    // Given above forks are documented to activate together, this distinction
496
    // is moot, however these are distinct BIPs and therefore must be either be
497
    // differentiated as such in code, or the BIP distiction would be ignored.
498
    return unversioned_sighash(input, subscript, sighash_flags);
29✔
499
}
500

501
// This is not used internal to the library.
502
bool transaction::check_signature(const ec_signature& signature,
2✔
503
    const data_slice& public_key, const script& sub, uint32_t index,
504
    uint64_t value, uint8_t sighash_flags, script_version version,
505
    uint32_t flags) const NOEXCEPT
506
{
507
    if ((index >= inputs_->size()) || signature.empty() || public_key.empty())
2✔
508
        return false;
509

510
    const auto bip143 = script::is_enabled(flags, flags::bip143_rule);
2✔
511
    const auto bip341 = script::is_enabled(flags, flags::bip341_rule);
2✔
512

513
    const auto sighash = signature_hash(input_at(index), sub, value,
2✔
514
        sighash_flags, version, bip143, bip341);
515

516
    // Validate the EC signature.
517
    return ecdsa::verify_signature(public_key, sighash, signature);
2✔
518
}
519

520
// This is not used internal to the library.
521
bool transaction::create_endorsement(endorsement& out, const ec_secret& secret,
2✔
522
    const script& sub, uint32_t index, uint64_t value, uint8_t sighash_flags,
523
    script_version version, uint32_t flags) const NOEXCEPT
524
{
525
    if (index >= inputs_->size())
2✔
526
        return false;
527

528
    const auto bip143 = script::is_enabled(flags, flags::bip143_rule);
2✔
529
    const auto bip341 = script::is_enabled(flags, flags::bip341_rule);
2✔
530

531
    out.reserve(max_endorsement_size);
2✔
532
    const auto sighash = signature_hash(input_at(index), sub, value,
2✔
533
        sighash_flags, version, bip143, bip341);
534

535
    // Create the EC signature and encode as DER.
536
    ec_signature signature;
2✔
537
    if (!ecdsa::sign(signature, secret, sighash) ||
4✔
538
        !ecdsa::encode_signature(out, signature))
2✔
UNCOV
539
        return false;
×
540

541
    // Add the sighash type to the end of the DER signature -> endorsement.
542
    out.push_back(sighash_flags);
2✔
543
    ////out.shrink_to_fit();
544
    return true;
2✔
545
}
546

547
// Guard (context free).
548
// ----------------------------------------------------------------------------
549

550
bool transaction::is_coinbase() const NOEXCEPT
80✔
551
{
552
    return is_one(inputs_->size()) && inputs_->front()->point().is_null();
80✔
553
}
554

555
bool transaction::is_internal_double_spend() const NOEXCEPT
4✔
556
{
557
    // TODO: optimize (see block.is_internal_double_spend).
558
    return !is_distinct(points());
4✔
559
}
560

561
// TODO: a pool (non-coinbase) tx must fit into a block (with a coinbase).
562
bool transaction::is_oversized() const NOEXCEPT
×
563
{
564
    return serialized_size(false) > max_block_size;
×
565
}
566

567
// Guard (contextual).
568
// ----------------------------------------------------------------------------
569

570
// static/private
571
bool transaction::segregated(const chain::inputs& inputs) NOEXCEPT
1✔
572
{
573
    const auto witnessed = [](const auto& input) NOEXCEPT
2✔
574
    {
575
        return !input.witness().stack().empty();
1✔
576
    };
577

578
    return std::any_of(inputs.begin(), inputs.end(), witnessed);
1✔
579
}
580

581
// static/private
582
bool transaction::segregated(const input_cptrs& inputs) NOEXCEPT
905✔
583
{
584
    const auto witnessed = [](const auto& input) NOEXCEPT
908✔
585
    {
586
        return !input->witness().stack().empty();
908✔
587
    };
588

589
    return std::any_of(inputs.begin(), inputs.end(), witnessed);
905✔
590
}
591

592
bool transaction::is_segregated() const NOEXCEPT
4✔
593
{
594
    return segregated_;
4✔
595
}
596

597
size_t transaction::weight() const NOEXCEPT
×
598
{
599
    // Block weight is 3 * base size * + 1 * total size (bip141).
600
    return ceilinged_add(
×
601
        ceilinged_multiply(base_size_contribution, serialized_size(false)),
602
        ceilinged_multiply(total_size_contribution, serialized_size(true)));
×
603
}
604

605
bool transaction::is_overweight() const NOEXCEPT
×
606
{
607
    return weight() > max_block_weight;
×
608
}
609

610
//*****************************************************************************
611
// CONSENSUS: Legacy sigops are counted in coinbase scripts despite the fact
612
// that coinbase input scripts are never executed. There is no need to exclude
613
// p2sh coinbase sigops since there is never a script to count.
614
//*****************************************************************************
615
bool transaction::is_signature_operations_limit(bool bip16,
×
616
    bool bip141) const NOEXCEPT
617
{
618
    const auto limit = bip141 ? max_fast_sigops : max_block_sigops;
×
619
    return signature_operations(bip16, bip141) > limit;
×
620
}
621

622
// Check (context free).
623
// ----------------------------------------------------------------------------
624

625
bool transaction::is_empty() const NOEXCEPT
9✔
626
{
627
    return inputs_->empty() || outputs_->empty();
9✔
628
}
629

630
bool transaction::is_null_non_coinbase() const NOEXCEPT
7✔
631
{
632
    BC_ASSERT(!is_coinbase());
7✔
633

634
    const auto invalid = [](const auto& input) NOEXCEPT
9✔
635
    {
636
        return input->point().is_null();
9✔
637
    };
638

639
    // True if not coinbase but has null previous_output(s).
640
    return std::any_of(inputs_->begin(), inputs_->end(), invalid);
7✔
641
}
642

643
bool transaction::is_invalid_coinbase_size() const NOEXCEPT
9✔
644
{
645
    BC_ASSERT(is_coinbase());
9✔
646

647
    // True if coinbase and has invalid input[0] script size.
648
    const auto script_size = inputs_->front()->script().serialized_size(false);
9✔
649
    return script_size < min_coinbase_size || script_size > max_coinbase_size;
9✔
650
}
651

652
// Accept (contextual).
653
// ----------------------------------------------------------------------------
654

655
bool transaction::is_absolute_locked(size_t height, uint32_t timestamp,
5✔
656
    uint32_t median_time_past, bool bip113) const NOEXCEPT
657
{
658
    // BIP113: comparing the locktime against the median of the past 11 block
659
    // timestamps, rather than the timestamp of the block including the tx.
660
    const auto time = bip113 ? median_time_past : timestamp;
5✔
661

662
    const auto finalized = [](const auto& input) NOEXCEPT
2✔
663
    {
664
        return input->is_final();
2✔
665
    };
666

667
    const auto height_time = locktime_ < locktime_threshold ? height : time;
5✔
668

669
    return !(is_zero(locktime_) || locktime_ < height_time ||
8✔
670
        std::all_of(inputs_->begin(), inputs_->end(), finalized));
3✔
671
}
672

673
bool transaction::is_missing_prevouts() const NOEXCEPT
3✔
674
{
675
    BC_ASSERT(!is_coinbase());
3✔
676

677
    // Null or invalid prevout indicates not found.
678
    const auto missing = [](const auto& input) NOEXCEPT
2✔
679
    {
680
        return !input->prevout;
681
    };
682

683
    return std::any_of(inputs_->begin(), inputs_->end(), missing);
3✔
684
}
685

686
uint64_t transaction::claim() const NOEXCEPT
8✔
687
{
688
    // Overflow returns max_uint64.
689
    const auto sum = [](uint64_t total, const auto& output) NOEXCEPT
8✔
690
    {
691
        return ceilinged_add(total, output->value());
8✔
692
    };
693

694
    // The amount claimed by outputs.
695
    return std::accumulate(outputs_->begin(), outputs_->end(), 0_u64, sum);
8✔
696
}
697

698
uint64_t transaction::value() const NOEXCEPT
9✔
699
{
700
    // Overflow, not populated, and coinbase (default) return max_uint64.
701
    const auto sum = [](uint64_t total, const auto& input) NOEXCEPT
7✔
702
    {
703
        const auto value = input->prevout ? input->prevout->value() : max_uint64;
7✔
704
        return ceilinged_add(total, value);
7✔
705
    };
706

707
    // The amount of prevouts (referenced by inputs).
708
    return std::accumulate(inputs_->begin(), inputs_->end(), 0_u64, sum);
9✔
709
}
710

711
bool transaction::is_overspent() const NOEXCEPT
2✔
712
{
713
    BC_ASSERT(!is_coinbase());
2✔
714

715
    return claim() > value();
2✔
716
}
717

718
constexpr bool is_non_coinbase_mature(size_t tx_height, size_t height) NOEXCEPT
2✔
719
{
720
    return tx_height <= height;
2✔
721
}
722

723
// static
724
//*****************************************************************************
725
// CONSENSUS: Coinbase output matures at 100 blocks depth.
726
// CONSENSUS: Genesis coinbase is forever immature (exception).
727
//*****************************************************************************
728
bool transaction::is_coinbase_mature(size_t coinbase_height,
3✔
729
    size_t height) NOEXCEPT
730
{
731
    return !is_zero(coinbase_height) &&
5✔
732
        ceilinged_add(coinbase_height, coinbase_maturity) <= height;
3✔
733
}
734

735
bool transaction::is_immature(size_t height) const NOEXCEPT
6✔
736
{
737
    BC_ASSERT(!is_coinbase());
6✔
738

739
    // Spends internal to a block are handled by block validation.
740
    const auto mature = [=](const auto& input) NOEXCEPT
5✔
741
    {
742
        return input->metadata.coinbase ?
5✔
743
            is_coinbase_mature(input->metadata.height, height) :
3✔
744
            is_non_coinbase_mature(input->metadata.height, height);
2✔
745
    };
6✔
746

747
    return !std::all_of(inputs_->begin(), inputs_->end(), mature);
6✔
748
}
749

750
// static
751
bool transaction::is_relative_locktime_applied(bool coinbase, uint32_t version,
×
752
    uint32_t sequence) NOEXCEPT
753
{
754
    // BIP68: not applied to the sequence of the input of a coinbase.
755
    // BIP68: if bit 31 is set then no consensus meaning is applied.
756
    // BIP68: applied to txs with a version greater than or equal to two.
757
    return !coinbase && input::is_relative_locktime_applied(sequence) &&
×
758
        (version >= relative_locktime_min_version);
×
759
}
760

761
bool transaction::is_internally_locked(const input& in) const NOEXCEPT
×
762
{
763
    // BIP68: not applied to the sequence of the input of a coinbase.
764
    BC_ASSERT(!is_coinbase());
×
765

766
    // BIP68: applied to txs with a version greater than or equal to two.
767
    if (version_ < relative_locktime_min_version)
×
768
        return false;
769

770
    // Internal spends have no relative height/mtp (own metadata vs. itself).
771
    return in.is_relative_locked(in.metadata.height,
×
772
        in.metadata.median_time_past);
×
773
}
774

775
bool transaction::is_relative_locked(size_t height,
4✔
776
    uint32_t median_time_past) const NOEXCEPT
777
{
778
    // BIP68: not applied to the sequence of the input of a coinbase.
779
    BC_ASSERT(!is_coinbase());
4✔
780

781
    // BIP68: applied to txs with a version greater than or equal to two.
782
    if (version_ < relative_locktime_min_version)
4✔
783
        return false;
784

785
    // BIP68: references to median time past are as defined by bip113.
786
    const auto locked = [=](const auto& input) NOEXCEPT
2✔
787
    {
788
        return input->is_relative_locked(height, median_time_past);
2✔
789
    };
2✔
790

791
    return std::any_of(inputs_->begin(), inputs_->end(), locked);
2✔
792
}
793

794
// Spends internal to a block are handled by block validation.
795
bool transaction::is_unconfirmed_spend(size_t height) const NOEXCEPT
×
796
{
797
    BC_ASSERT(!is_coinbase());
×
798

799
    // Zero is either genesis or not found.
800
    // Test maturity first to obtain proper error code.
801
    // Spends internal to a block are handled by block validation.
802
    const auto unconfirmed = [=](const auto& input) NOEXCEPT
×
803
    {
804
        const auto prevout_height = input->metadata.height;
×
805
        return is_zero(prevout_height) && !(height > prevout_height);
×
806
    };
×
807

808
    return std::any_of(inputs_->begin(), inputs_->end(), unconfirmed);
×
809
}
810

811
bool transaction::is_confirmed_double_spend(size_t height) const NOEXCEPT
4✔
812
{
813
    BC_ASSERT(!is_coinbase());
4✔
814

815
    // Spends internal to a block are handled by block validation.
816
    const auto spent = [=](const auto& input) NOEXCEPT
3✔
817
    {
818
        return input->metadata.spent && height > input->metadata.height;
3✔
819
    };
4✔
820

821
    return std::any_of(inputs_->begin(), inputs_->end(), spent);
4✔
822
}
823

824
// Guards (for tx pool without compact blocks).
825
// ----------------------------------------------------------------------------
826

827
// Pools do not have coinbases.
828
// Redundant with block is_internal_double_spend check.
829
// Redundant with block max_block_size check.
830
code transaction::guard_check() const NOEXCEPT
×
831
{
832
    if (is_coinbase())
×
833
        return error::coinbase_transaction;
×
834
    if (is_internal_double_spend())
×
835
        return error::transaction_internal_double_spend;
×
836
    if (is_oversized())
×
837
        return error::transaction_size_limit;
×
838

839
    return error::transaction_success;
×
840
}
841

842
// Redundant with block max_block_weight accept.
843
code transaction::guard_check(const context& ctx) const NOEXCEPT
×
844
{
845
    const auto bip141 = ctx.is_enabled(flags::bip141_rule);
×
846

847
     if (!bip141 && is_segregated())
×
848
        return error::unexpected_witness_transaction;
×
849
     if (bip141 && is_overweight())
×
850
        return error::transaction_weight_limit;
×
851

852
    return error::transaction_success;
×
853
}
854

855
// Redundant with block max_block_sigops accept.
856
code transaction::guard_accept(const context& ctx) const NOEXCEPT
×
857
{
858
    const auto bip16 = ctx.is_enabled(flags::bip16_rule);
×
859
    const auto bip141 = ctx.is_enabled(flags::bip141_rule);
×
860

861
    if (is_missing_prevouts())
×
862
        return error::missing_previous_output;
×
863
    if (is_signature_operations_limit(bip16, bip141))
×
864
        return error::transaction_sigop_limit;
×
865

866
    return error::transaction_success;
×
867
}
868

869
// Validation.
870
// ----------------------------------------------------------------------------
871

872
// DO invoke on coinbase.
873
code transaction::check() const NOEXCEPT
5✔
874
{
875
    const auto coinbase = is_coinbase();
5✔
876

877
    if (is_empty())
5✔
878
        return error::empty_transaction;
×
879
    if (coinbase && is_invalid_coinbase_size())
5✔
880
        return error::invalid_coinbase_script_size;
×
881
    if (!coinbase && is_null_non_coinbase())
5✔
882
        return error::previous_output_null;
×
883

884
    return error::transaction_success;
5✔
885
}
886

887
// forks
888
// height
889
// timestamp
890
// median_time_past
891

892
// DO invoke on coinbase.
893
code transaction::check(const context& ctx) const NOEXCEPT
×
894
{
895
    const auto bip113 = ctx.is_enabled(bip113_rule);
×
896

897
    if (is_absolute_locked(ctx.height, ctx.timestamp, ctx.median_time_past, bip113))
×
898
        return error::absolute_time_locked;
×
899

900
    return error::transaction_success;
×
901
}
902

903
// Do not need to invoke on coinbase.
904
// This assumes that prevout caching is completed on all inputs.
905
code transaction::accept(const context&) const NOEXCEPT
×
906
{
907
    ////BC_ASSERT(!is_coinbase());
908

909
    if (is_coinbase())
×
910
        return error::transaction_success;
×
911
    if (is_missing_prevouts())
×
912
        return error::missing_previous_output;
×
913
    if (is_overspent())
×
914
        return error::spend_exceeds_value;
×
915

916
    return error::transaction_success;
×
917
}
918

919
// forks
920
// height
921
// median_time_past
922

923
// Do not need to invoke on coinbase.
924
// Node performs these checks through database query.
925
// This assumes that prevout and metadata caching are completed on all inputs.
926
code transaction::confirm(const context& ctx) const NOEXCEPT
×
927
{
928
    ////BC_ASSERT(!is_coinbase());
929
    const auto bip68 = ctx.is_enabled(bip68_rule);
×
930

931
    if (is_coinbase())
×
932
        return error::transaction_success;
×
933
    if (bip68 && is_relative_locked(ctx.height, ctx.median_time_past))
×
934
        return error::relative_time_locked;
×
935
    if (is_immature(ctx.height))
×
936
        return error::coinbase_maturity;
×
937
    if (is_unconfirmed_spend(ctx.height))
×
938
        return error::unconfirmed_spend;
×
939
    if (is_confirmed_double_spend(ctx.height))
×
940
        return error::confirmed_double_spend;
×
941

942
    return error::transaction_success;
×
943
}
944

945
// Delegated.
946
// ----------------------------------------------------------------------------
947

948
code transaction::connect_input(const context& ctx,
4✔
949
    const input_iterator& it) const NOEXCEPT
950
{
951
    using namespace machine;
4✔
952

953
    // TODO: evaluate performance tradeoff.
954
    if ((*it)->is_roller())
4✔
955
    {
956
        // Evaluate rolling scripts with linear search but constant erase.
957
        return interpreter<linked_stack>::connect(ctx, *this, it);
×
958
    }
959

960
    // Evaluate non-rolling scripts with constant search but linear erase.
961
    return interpreter<contiguous_stack>::connect(ctx, *this, it);
4✔
962
}
963

964
// Connect (contextual).
965
// ----------------------------------------------------------------------------
966
// TODO: accumulate sigops from each connect result and add coinbase.
967
// TODO: return in override with out parameter. more impactful with segwit.
968

969
// forks
970

971
// Do not need to invoke on coinbase.
972
// This assumes that prevout caching is completed on all inputs.
973
code transaction::connect(const context& ctx) const NOEXCEPT
2✔
974
{
975
    ////BC_ASSERT(!is_coinbase());
976

977
    if (is_coinbase())
2✔
978
        return error::transaction_success;
×
979

980
    initialize_sighash_cache();
2✔
981

982
    for (auto in = inputs_->begin(); in != inputs_->end(); ++in)
6✔
983
        if (const auto ec = connect_input(ctx, in))
4✔
984
            return ec;
×
985

986
    return error::transaction_success;
2✔
987
}
988

989
BC_POP_WARNING()
990

991
// JSON value convertors.
992
// ----------------------------------------------------------------------------
993

994
namespace json = boost::json;
995

996
// boost/json will soon have NOEXCEPT: github.com/boostorg/json/pull/636
997
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
998

999
transaction tag_invoke(json::value_to_tag<transaction>,
2✔
1000
    const json::value& value) NOEXCEPT
1001
{
1002
    return
2✔
1003
    {
1004
        value.at("version").to_number<uint32_t>(),
2✔
1005
        json::value_to<chain::inputs>(value.at("inputs")),
2✔
1006
        json::value_to<chain::outputs>(value.at("outputs")),
4✔
1007
        value.at("locktime").to_number<uint32_t>()
4✔
1008
    };
4✔
1009
}
1010

1011
void tag_invoke(json::value_from_tag, json::value& value,
4✔
1012
    const transaction& tx) NOEXCEPT
1013
{
1014
    value =
4✔
1015
    {
1016
        { "version", tx.version() },
1017
        { "inputs", *tx.inputs_ptr() },
1018
        { "outputs", *tx.outputs_ptr() },
1019
        { "locktime", tx.locktime() }
1020
    };
4✔
1021
}
4✔
1022

1023
BC_POP_WARNING()
1024

1025
transaction::cptr tag_invoke(json::value_to_tag<transaction::cptr>,
×
1026
    const json::value& value) NOEXCEPT
1027
{
1028
    return to_shared(tag_invoke(json::value_to_tag<transaction>{}, value));
×
1029
}
1030

1031
// Shared pointer overload is required for navigation.
1032
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
1033
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
1034

1035
void tag_invoke(json::value_from_tag tag, json::value& value,
2✔
1036
    const transaction::cptr& tx) NOEXCEPT
1037
{
1038
    tag_invoke(tag, value, *tx);
2✔
1039
}
2✔
1040

1041
BC_POP_WARNING()
1042
BC_POP_WARNING()
1043

1044
} // namespace chain
1045
} // namespace system
1046
} // 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