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

libbitcoin / libbitcoin-system / 22113584445

17 Feb 2026 07:59PM UTC coverage: 81.187% (-0.04%) from 81.224%
22113584445

push

github

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

Add block/tx virtual_size as (ceiling_divide(weight(),4)).

0 of 6 new or added lines in 2 files covered. (0.0%)

2 existing lines in 2 files now uncovered.

10858 of 13374 relevant lines covered (81.19%)

3540905.83 hits per line

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

76.34
/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 <utility>
25
#include <bitcoin/system/chain/context.hpp>
26
#include <bitcoin/system/chain/enums/coverage.hpp>
27
#include <bitcoin/system/chain/enums/magic_numbers.hpp>
28
#include <bitcoin/system/chain/header.hpp>
29
#include <bitcoin/system/chain/input.hpp>
30
#include <bitcoin/system/chain/output.hpp>
31
#include <bitcoin/system/chain/script.hpp>
32
#include <bitcoin/system/data/data.hpp>
33
#include <bitcoin/system/define.hpp>
34
#include <bitcoin/system/error/error.hpp>
35
#include <bitcoin/system/hash/hash.hpp>
36
#include <bitcoin/system/machine/machine.hpp>
37
#include <bitcoin/system/math/math.hpp>
38
#include <bitcoin/system/stream/stream.hpp>
39

40
namespace libbitcoin {
41
namespace system {
42
namespace chain {
43

44
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
45

46
// Constructors.
47
// ----------------------------------------------------------------------------
48

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

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

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

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

77
transaction::transaction(const data_slice& data, bool witness) NOEXCEPT
41✔
78
  : transaction(stream::in::fast(data), witness)
41✔
79
{
80
}
41✔
81

82
//protected
83
transaction::transaction(stream::in::fast&& stream, bool witness) NOEXCEPT
41✔
84
  : transaction(read::bytes::fast(stream), witness)
41✔
85
{
86
}
41✔
87

88
transaction::transaction(stream::in::fast& stream, bool witness) NOEXCEPT
2✔
89
  : transaction(read::bytes::fast(stream), witness)
2✔
90
{
91
}
2✔
92

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

98
// protected
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
210✔
105
  : version_(source.read_4_bytes_little_endian()),
420✔
106
    inputs_(CREATE(input_cptrs, source.get_allocator())),
210✔
107
    outputs_(CREATE(output_cptrs, source.get_allocator()))
630✔
108
{
109
    assign_data(source, witness);
210✔
110
}
210✔
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
63✔
130
{
131
    // Compares input/output elements, not pointers, cache not compared.
132
    return (version_ == other.version_)
63✔
133
        && (locktime_ == other.locktime_)
61✔
134
        && ((inputs_ == other.inputs_) || 
90✔
135
            deep_equal(*inputs_, *other.inputs_))
29✔
136
        && ((outputs_ == other.outputs_) ||
153✔
137
            deep_equal(*outputs_, *other.outputs_));
29✔
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
210✔
151
{
152
    auto& allocator = source.get_allocator();
210✔
153
    auto ins = to_non_const_raw_ptr(inputs_);
210✔
154
    auto count = source.read_size(max_block_size);
210✔
155
    ins->reserve(count);
210✔
156
    for (size_t in = 0; in < count; ++in)
465✔
157
        ins->emplace_back(CREATE(input, allocator, source));
255✔
158

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

165
    if (segregated_)
210✔
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_);
194✔
197
        count = source.read_size(max_block_size);
194✔
198
        outs->reserve(count);
194✔
199
        for (size_t out = 0; out < count; ++out)
449✔
200
            outs->emplace_back(CREATE(output, allocator, source));
255✔
201
    }
202

203
    locktime_ = source.read_4_bytes_little_endian();
210✔
204
    size_ = serialized_size(*inputs_, *outputs_, segregated_);
210✔
205
    valid_ = source;
210✔
206
}
210✔
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
969✔
234
{
235
    witness &= segregated_;
969✔
236

237
    sink.write_4_bytes_little_endian(version_);
969✔
238

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

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

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

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

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

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

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

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

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

282
    const auto base_size =
1,137✔
283
        ceilinged_add(ceilinged_add(ceilinged_add(base_const_size,
1,137✔
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,137✔
288

289
    // For non-segregated transactions, witnessed_size is nominal_size.
290
    const auto witnessed_size = segregated ? ceilinged_add(ceilinged_add(
1,137✔
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,137✔
295
}
296

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

301
    return witness ? size_.witnessed : size_.nominal;
625✔
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
    // The value of prevouts referenced by inputs minus that spent by outputs.
351
    return floored_subtract(value(), spend());
4✔
352
}
353

354
// Methods.
355
// ----------------------------------------------------------------------------
356

357
bool transaction::is_dusty(uint64_t minimum_output_value) const NOEXCEPT
6✔
358
{
359
    const auto dusty = [=](const auto& output) NOEXCEPT
9✔
360
    {
361
        return output->is_dust(minimum_output_value);
9✔
362
    };
6✔
363

364
    return std::any_of(outputs_->begin(), outputs_->end(), dusty);
6✔
365
}
366

367
size_t transaction::signature_operations(bool bip16, bool bip141) const NOEXCEPT
1✔
368
{
369
    // Overflow returns max_size_t.
370
    const auto in = [=](size_t total, const auto& input) NOEXCEPT
×
371
    {
372
        const auto add = input->signature_operations(bip16, bip141);
×
373
        return ceilinged_add(total, add);
×
374
    };
1✔
375

376
    // Overflow returns max_size_t.
377
    const auto out = [=](size_t total, const auto& output) NOEXCEPT
×
378
    {
379
        const auto add = output->signature_operations(bip141);
×
380
        return ceilinged_add(total, add);
×
381
    };
1✔
382

383
    // Overflow returns max_size_t.
384
    return ceilinged_add(
1✔
385
        std::accumulate(inputs_->begin(), inputs_->end(), zero, in),
386
        std::accumulate(outputs_->begin(), outputs_->end(), zero, out));
1✔
387
}
388

389
// private
390
chain::points transaction::points() const NOEXCEPT
4✔
391
{
392
    chain::points out(inputs_->size());
4✔
393

394
    const auto point = [](const auto& input) NOEXCEPT
8✔
395
    {
396
        return input->point();
8✔
397
    };
398

399
    std::transform(inputs_->begin(), inputs_->end(), out.begin(), point);
4✔
400
    return out;
4✔
401
}
402

403
// Signatures (public).
404
// ----------------------------------------------------------------------------
405

406
transaction::input_iterator transaction::input_at(
4✔
407
    uint32_t index) const NOEXCEPT
408
{
409
    // Guarded by check_signature and create_endorsement.
410
    BC_ASSERT_MSG(index < inputs_->size(), "invalid input index");
4✔
411

412
    return std::next(inputs_->begin(), index);
4✔
413
}
414

415
// This is not used internal to the library.
416
bool transaction::check_signature(const ec_signature& signature,
2✔
417
    const data_slice& public_key, const script& subscript, uint32_t index,
418
    uint64_t value, uint8_t sighash_flags, script_version version,
419
    uint32_t flags) const NOEXCEPT
420
{
421
    if ((index >= inputs_->size()) || signature.empty() || public_key.empty())
2✔
422
        return false;
423

424
    hash_digest sighash{};
2✔
425
    const hash_cptr unused{};
2✔
426
    if (!signature_hash(sighash, input_at(index), subscript, value,
2✔
427
        unused, version, sighash_flags, flags))
428
        return false;
429

430
    // Validate the ECDSA signature.
431
    return ecdsa::verify_signature(public_key, sighash, signature);
2✔
432
}
433

434
// This is not used internal to the library.
435
bool transaction::create_endorsement(endorsement& out, const ec_secret& secret,
2✔
436
    const script& subscript, uint32_t index, uint64_t value,
437
    uint8_t sighash_flags, script_version version,
438
    uint32_t flags) const NOEXCEPT
439
{
440
    if (index >= inputs_->size())
2✔
441
        return false;
442

443
    hash_digest sighash{};
2✔
444
    const hash_cptr unused{};
2✔
445
    out.reserve(max_endorsement_size);
2✔
446
    if (!signature_hash(sighash, input_at(index), subscript, value, unused,
2✔
447
        version, sighash_flags, flags))
448
        return false;
449

450
    // Create the ECDSA signature and encode as DER.
451
    ec_signature signature;
2✔
452
    if (!ecdsa::sign(signature, secret, sighash) ||
4✔
453
        !ecdsa::encode_signature(out, signature))
2✔
454
        return false;
×
455

456
    // Add the sighash type to the end of the DER signature -> endorsement.
457
    out.push_back(sighash_flags);
2✔
458
    ////out.shrink_to_fit();
459
    return true;
2✔
460
}
461

462
// Signature hashing (common).
463
// ----------------------------------------------------------------------------
464

465
uint32_t transaction::input_index(const input_iterator& input) const NOEXCEPT
25✔
466
{
467
    return possible_narrow_and_sign_cast<uint32_t>(
25✔
468
        std::distance(inputs_->begin(), input));
25✔
469
}
470

471
//*****************************************************************************
472
// CONSENSUS: if index exceeds outputs in signature hash, return one_hash.
473
// Related Bug: bitcointalk.org/index.php?topic=260595
474
// Exploit: joncave.co.uk/2014/08/bitcoin-sighash-single/
475
//*****************************************************************************
476
bool transaction::output_overflow(size_t input) const NOEXCEPT
21✔
477
{
478
    return input >= outputs_->size();
21✔
479
}
480

481
// There are three versions of signature hashing and verification.
482
// Version: (unversioned) original, (v0) bip143/segwit, (v1) bip341/taproot.
483
bool transaction::signature_hash(hash_digest& out, const input_iterator& input,
41✔
484
    const script& subscript, uint64_t value, const hash_cptr& tapleaf,
485
    script_version version, uint8_t sighash_flags, uint32_t flags) const NOEXCEPT
486
{
487
    // There is no rational interpretation of a signature hash for a coinbase.
488
    BC_ASSERT(!is_coinbase());
41✔
489

490
    // bip143: the method of signature hashing is changed for v0 scripts.
491
    // bip342: the method of signature hashing is changed for v1 scripts.
492
    const auto bip143 = script::is_enabled(flags, flags::bip143_rule);
41✔
493
    const auto bip342 = script::is_enabled(flags, flags::bip342_rule);
41✔
494

495
    // This is where the connection between bip141 and bip143 is made. If a
496
    // versioned 1 program (segwit) extracted by bip141 but bip143 (segwit
497
    // hashing) is not active, then drop down to unversioned signature hashing.
498
    if (bip143 && version == script_version::segwit)
41✔
499
        return version0_sighash(out, input, subscript, value, sighash_flags);
22✔
500

501
    // This is where the connection between bip341 and bip342 is made. If a
502
    // version 2 program (taproot) extracted by bip341 but bip342 (tapscript)
503
    // is not active then drop down to unversioned signature hashing. 
504
    if (bip342 && version == script_version::taproot)
19✔
505
        return version1_sighash(out, input, subscript, value, tapleaf,
×
506
            sighash_flags);
×
507

508
    // Given above forks are documented to activate together, this distinction
509
    // is moot, however these are distinct BIPs and therefore must be either be
510
    // differentiated as such in code, or the BIP distiction would be ignored.
511
    return unversioned_sighash(out, input, subscript, sighash_flags);
19✔
512
}
513

514
// Guard (context free).
515
// ----------------------------------------------------------------------------
516

517
bool transaction::is_coinbase() const NOEXCEPT
80✔
518
{
519
    return is_one(inputs_->size()) && inputs_->front()->point().is_null();
80✔
520
}
521

522
bool transaction::is_internal_double_spend() const NOEXCEPT
4✔
523
{
524
    // TODO: optimize (see block.is_internal_double_spend).
525
    return !is_distinct(points());
4✔
526
}
527

528
// TODO: a pool (non-coinbase) tx must fit into a block (with a coinbase).
529
bool transaction::is_oversized() const NOEXCEPT
×
530
{
531
    return serialized_size(false) > max_block_size;
×
532
}
533

534
// Guard (contextual).
535
// ----------------------------------------------------------------------------
536

537
// static/private
538
bool transaction::segregated(const chain::inputs& inputs) NOEXCEPT
1✔
539
{
540
    const auto witnessed = [](const auto& input) NOEXCEPT
2✔
541
    {
542
        return !input.witness().stack().empty();
1✔
543
    };
544

545
    return std::any_of(inputs.begin(), inputs.end(), witnessed);
1✔
546
}
547

548
// static/private
549
bool transaction::segregated(const input_cptrs& inputs) NOEXCEPT
905✔
550
{
551
    const auto witnessed = [](const auto& input) NOEXCEPT
908✔
552
    {
553
        return !input->witness().stack().empty();
908✔
554
    };
555

556
    return std::any_of(inputs.begin(), inputs.end(), witnessed);
905✔
557
}
558

559
bool transaction::is_segregated() const NOEXCEPT
4✔
560
{
561
    return segregated_;
4✔
562
}
563

564
size_t transaction::weight() const NOEXCEPT
×
565
{
566
    // Transaction weight is 3 * base size * + 1 * total size [bip141].
567
    return ceilinged_add(
×
568
        ceilinged_multiply(base_size_contribution, serialized_size(false)),
569
        ceilinged_multiply(total_size_contribution, serialized_size(true)));
×
570
}
571

NEW
572
size_t transaction::virtual_size() const NOEXCEPT
×
573
{
NEW
574
    constexpr auto scale = base_size_contribution + total_size_contribution;
×
NEW
575
    return ceilinged_divide(weight(), scale);
×
576
}
577

UNCOV
578
bool transaction::is_overweight() const NOEXCEPT
×
579
{
580
    return weight() > max_block_weight;
×
581
}
582

583
//*****************************************************************************
584
// CONSENSUS: Legacy sigops are counted in coinbase scripts despite the fact
585
// that coinbase input scripts are never executed. There is no need to exclude
586
// p2sh coinbase sigops since there is never a script to count.
587
//*****************************************************************************
588
bool transaction::is_signature_operations_limited(bool bip16,
×
589
    bool bip141) const NOEXCEPT
590
{
591
    const auto limit = bip141 ? max_fast_sigops : max_block_sigops;
×
592
    return signature_operations(bip16, bip141) > limit;
×
593
}
594

595
// Check (context free).
596
// ----------------------------------------------------------------------------
597

598
bool transaction::is_empty() const NOEXCEPT
9✔
599
{
600
    return inputs_->empty() || outputs_->empty();
9✔
601
}
602

603
bool transaction::is_null_non_coinbase() const NOEXCEPT
7✔
604
{
605
    BC_ASSERT(!is_coinbase());
7✔
606

607
    const auto invalid = [](const auto& input) NOEXCEPT
9✔
608
    {
609
        return input->point().is_null();
9✔
610
    };
611

612
    // True if not coinbase but has null previous_output(s).
613
    return std::any_of(inputs_->begin(), inputs_->end(), invalid);
7✔
614
}
615

616
bool transaction::is_invalid_coinbase_size() const NOEXCEPT
9✔
617
{
618
    BC_ASSERT(is_coinbase());
9✔
619

620
    // True if coinbase and has invalid input[0] script size.
621
    const auto script_size = inputs_->front()->script().serialized_size(false);
9✔
622
    return script_size < min_coinbase_size || script_size > max_coinbase_size;
9✔
623
}
624

625
// Accept (contextual).
626
// ----------------------------------------------------------------------------
627

628
bool transaction::is_absolute_locked(size_t height, uint32_t timestamp,
5✔
629
    uint32_t median_time_past, bool bip113) const NOEXCEPT
630
{
631
    // BIP113: comparing the locktime against the median of the past 11 block
632
    // timestamps, rather than the timestamp of the block including the tx.
633
    const auto time = bip113 ? median_time_past : timestamp;
5✔
634

635
    const auto finalized = [](const auto& input) NOEXCEPT
2✔
636
    {
637
        return input->is_final();
2✔
638
    };
639

640
    const auto height_time = locktime_ < locktime_threshold ? height : time;
5✔
641

642
    return !(is_zero(locktime_) || locktime_ < height_time ||
8✔
643
        std::all_of(inputs_->begin(), inputs_->end(), finalized));
3✔
644
}
645

646
bool transaction::is_missing_prevouts() const NOEXCEPT
3✔
647
{
648
    BC_ASSERT(!is_coinbase());
3✔
649

650
    // Null or invalid prevout indicates not found.
651
    const auto missing = [](const auto& input) NOEXCEPT
2✔
652
    {
653
        return !input->prevout;
654
    };
655

656
    return std::any_of(inputs_->begin(), inputs_->end(), missing);
3✔
657
}
658

659
// The value() is the sum of own inputs.
660
uint64_t transaction::value() const NOEXCEPT
9✔
661
{
662
    // Overflow returns max_uint64. Not populated/coinbase return zero.
663
    const auto sum = [](uint64_t total, const auto& input) NOEXCEPT
7✔
664
    {
665
        const auto value = input->prevout ? input->prevout->value() : zero;
7✔
666
        return ceilinged_add(total, value);
7✔
667
    };
668

669
    // The amount referenced by inputs.
670
    return std::accumulate(inputs_->begin(), inputs_->end(), 0_u64, sum);
9✔
671
}
672

673
// The spend() is the sum of own outputs.
674
uint64_t transaction::spend() const NOEXCEPT
8✔
675
{
676
    // Overflow returns max_uint64.
677
    const auto sum = [](uint64_t total, const auto& output) NOEXCEPT
8✔
678
    {
679
        return ceilinged_add(total, output->value());
8✔
680
    };
681

682
    // The amount spent by outputs.
683
    return std::accumulate(outputs_->begin(), outputs_->end(), 0_u64, sum);
8✔
684
}
685

686
// Overspent is invalid (spend exceeds value), while underspent is the fee().
687
bool transaction::is_overspent() const NOEXCEPT
2✔
688
{
689
    BC_ASSERT(!is_coinbase());
2✔
690

691
    return spend() > value();
2✔
692
}
693

694
constexpr bool is_non_coinbase_mature(size_t tx_height, size_t height) NOEXCEPT
2✔
695
{
696
    return tx_height <= height;
2✔
697
}
698

699
// static
700
//*****************************************************************************
701
// CONSENSUS: Coinbase output matures at 100 blocks depth.
702
// CONSENSUS: Genesis coinbase is forever immature (exception).
703
//*****************************************************************************
704
bool transaction::is_coinbase_mature(size_t coinbase_height,
3✔
705
    size_t height) NOEXCEPT
706
{
707
    return !is_zero(coinbase_height) &&
5✔
708
        ceilinged_add(coinbase_height, coinbase_maturity) <= height;
3✔
709
}
710

711
bool transaction::is_immature(size_t height) const NOEXCEPT
6✔
712
{
713
    BC_ASSERT(!is_coinbase());
6✔
714

715
    // Spends internal to a block are handled by block validation.
716
    const auto mature = [=](const auto& input) NOEXCEPT
5✔
717
    {
718
        return input->metadata.coinbase ?
5✔
719
            is_coinbase_mature(input->metadata.height, height) :
3✔
720
            is_non_coinbase_mature(input->metadata.height, height);
2✔
721
    };
6✔
722

723
    return !std::all_of(inputs_->begin(), inputs_->end(), mature);
6✔
724
}
725

726
// static
727
bool transaction::is_relative_locktime_applied(bool coinbase, uint32_t version,
×
728
    uint32_t sequence) NOEXCEPT
729
{
730
    // BIP68: not applied to the sequence of the input of a coinbase.
731
    // BIP68: if bit 31 is set then no consensus meaning is applied.
732
    // BIP68: applied to txs with a version greater than or equal to two.
733
    return !coinbase && input::is_relative_locktime_applied(sequence) &&
×
734
        (version >= relative_locktime_min_version);
×
735
}
736

737
bool transaction::is_internally_locked(const input& in) const NOEXCEPT
×
738
{
739
    // BIP68: not applied to the sequence of the input of a coinbase.
740
    BC_ASSERT(!is_coinbase());
×
741

742
    // BIP68: applied to txs with a version greater than or equal to two.
743
    if (version_ < relative_locktime_min_version)
×
744
        return false;
745

746
    // Internal spends have no relative height/mtp (own metadata vs. itself).
747
    return in.is_relative_locked(in.metadata.height,
×
748
        in.metadata.median_time_past);
×
749
}
750

751
bool transaction::is_relative_locked(size_t height,
4✔
752
    uint32_t median_time_past) const NOEXCEPT
753
{
754
    // BIP68: not applied to the sequence of the input of a coinbase.
755
    BC_ASSERT(!is_coinbase());
4✔
756

757
    // BIP68: applied to txs with a version greater than or equal to two.
758
    if (version_ < relative_locktime_min_version)
4✔
759
        return false;
760

761
    // BIP68: references to median time past are as defined by bip113.
762
    const auto locked = [=](const auto& input) NOEXCEPT
2✔
763
    {
764
        return input->is_relative_locked(height, median_time_past);
2✔
765
    };
2✔
766

767
    return std::any_of(inputs_->begin(), inputs_->end(), locked);
2✔
768
}
769

770
// Spends internal to a block are handled by block validation.
771
bool transaction::is_unconfirmed_spend(size_t height) const NOEXCEPT
×
772
{
773
    BC_ASSERT(!is_coinbase());
×
774

775
    // Zero is either genesis or not found.
776
    // Test maturity first to obtain proper error code.
777
    // Spends internal to a block are handled by block validation.
778
    const auto unconfirmed = [=](const auto& input) NOEXCEPT
×
779
    {
780
        const auto prevout_height = input->metadata.height;
×
781
        return is_zero(prevout_height) && !(height > prevout_height);
×
782
    };
×
783

784
    return std::any_of(inputs_->begin(), inputs_->end(), unconfirmed);
×
785
}
786

787
bool transaction::is_confirmed_double_spend(size_t height) const NOEXCEPT
4✔
788
{
789
    BC_ASSERT(!is_coinbase());
4✔
790

791
    // Spends internal to a block are handled by block validation.
792
    const auto spent = [=](const auto& input) NOEXCEPT
3✔
793
    {
794
        return input->metadata.spent && height > input->metadata.height;
3✔
795
    };
4✔
796

797
    return std::any_of(inputs_->begin(), inputs_->end(), spent);
4✔
798
}
799

800
// Guards (for tx pool without compact blocks).
801
// ----------------------------------------------------------------------------
802

803
// Pools do not have coinbases.
804
// Redundant with block is_internal_double_spend check.
805
// Redundant with block max_block_size check.
806
code transaction::guard_check() const NOEXCEPT
×
807
{
808
    if (is_coinbase())
×
809
        return error::coinbase_transaction;
×
810
    if (is_internal_double_spend())
×
811
        return error::transaction_internal_double_spend;
×
812
    if (is_oversized())
×
813
        return error::transaction_size_limit;
×
814

815
    return error::transaction_success;
×
816
}
817

818
// Redundant with block max_block_weight accept.
819
code transaction::guard_check(const context& ctx) const NOEXCEPT
×
820
{
821
    const auto bip141 = ctx.is_enabled(flags::bip141_rule);
×
822

823
     if (!bip141 && is_segregated())
×
824
        return error::unexpected_witness_transaction;
×
825
     if (bip141 && is_overweight())
×
826
        return error::transaction_weight_limit;
×
827

828
    return error::transaction_success;
×
829
}
830

831
// Redundant with block max_block_sigops accept.
832
code transaction::guard_accept(const context& ctx) const NOEXCEPT
×
833
{
834
    const auto bip16 = ctx.is_enabled(flags::bip16_rule);
×
835
    const auto bip141 = ctx.is_enabled(flags::bip141_rule);
×
836

837
    if (is_missing_prevouts())
×
838
        return error::missing_previous_output;
×
839
    if (is_signature_operations_limited(bip16, bip141))
×
840
        return error::transaction_sigop_limit;
×
841

842
    return error::transaction_success;
×
843
}
844

845
// Validation.
846
// ----------------------------------------------------------------------------
847

848
// DO invoke on coinbase.
849
code transaction::check() const NOEXCEPT
5✔
850
{
851
    const auto coinbase = is_coinbase();
5✔
852

853
    if (is_empty())
5✔
854
        return error::empty_transaction;
×
855
    if (coinbase && is_invalid_coinbase_size())
5✔
856
        return error::invalid_coinbase_script_size;
×
857
    if (!coinbase && is_null_non_coinbase())
5✔
858
        return error::previous_output_null;
×
859

860
    return error::transaction_success;
5✔
861
}
862

863
// forks
864
// height
865
// timestamp
866
// median_time_past
867

868
// DO invoke on coinbase.
869
code transaction::check(const context& ctx) const NOEXCEPT
×
870
{
871
    const auto bip113 = ctx.is_enabled(bip113_rule);
×
872

873
    if (is_absolute_locked(ctx.height, ctx.timestamp, ctx.median_time_past, bip113))
×
874
        return error::absolute_time_locked;
×
875

876
    return error::transaction_success;
×
877
}
878

879
// Do not need to invoke on coinbase.
880
// This assumes that prevout caching is completed on all inputs.
881
code transaction::accept(const context&) const NOEXCEPT
×
882
{
883
    ////BC_ASSERT(!is_coinbase());
884

885
    if (is_coinbase())
×
886
        return error::transaction_success;
×
887
    if (is_missing_prevouts())
×
888
        return error::missing_previous_output;
×
889
    if (is_overspent())
×
890
        return error::spend_exceeds_value;
×
891

892
    return error::transaction_success;
×
893
}
894

895
// forks
896
// height
897
// median_time_past
898

899
// Do not need to invoke on coinbase.
900
// Node performs these checks through database query.
901
// This assumes that prevout and metadata caching are completed on all inputs.
902
code transaction::confirm(const context& ctx) const NOEXCEPT
×
903
{
904
    ////BC_ASSERT(!is_coinbase());
905
    const auto bip68 = ctx.is_enabled(bip68_rule);
×
906

907
    if (is_coinbase())
×
908
        return error::transaction_success;
×
909
    if (bip68 && is_relative_locked(ctx.height, ctx.median_time_past))
×
910
        return error::relative_time_locked;
×
911
    if (is_immature(ctx.height))
×
912
        return error::coinbase_maturity;
×
913
    if (is_unconfirmed_spend(ctx.height))
×
914
        return error::unconfirmed_spend;
×
915
    if (is_confirmed_double_spend(ctx.height))
×
916
        return error::confirmed_double_spend;
×
917

918
    return error::transaction_success;
×
919
}
920

921
// Delegated.
922
// ----------------------------------------------------------------------------
923

924
code transaction::connect_input(const context& ctx,
4✔
925
    const input_iterator& it) const NOEXCEPT
926
{
927
    using namespace machine;
4✔
928

929
    // TODO: evaluate performance tradeoff.
930
    if ((*it)->is_roller())
4✔
931
    {
932
        // Evaluate rolling scripts with linear search but constant erase.
933
        return interpreter<linked_stack>::connect(ctx, *this, it);
×
934
    }
935

936
    // Evaluate non-rolling scripts with constant search but linear erase.
937
    return interpreter<contiguous_stack>::connect(ctx, *this, it);
4✔
938
}
939

940
// Connect (contextual).
941
// ----------------------------------------------------------------------------
942
// TODO: accumulate sigops from each connect result and add coinbase.
943
// TODO: return in override with out parameter. more impactful with segwit.
944

945
// forks
946

947
code transaction::connect(const context& ctx) const NOEXCEPT
2✔
948
{
949
    ////BC_ASSERT(!is_coinbase());
950

951
    if (is_coinbase())
2✔
952
        return error::transaction_success;
×
953

954
    for (auto in = inputs_->begin(); in != inputs_->end(); ++in)
6✔
955
        if (const auto ec = connect_input(ctx, in))
4✔
956
            return ec;
×
957

958
    return error::transaction_success;
2✔
959
}
960

961
BC_POP_WARNING()
962

963
// JSON value convertors.
964
// ----------------------------------------------------------------------------
965

966
DEFINE_JSON_TO_TAG(transaction)
2✔
967
{
968
    return
2✔
969
    {
970
        value.at("version").to_number<uint32_t>(),
2✔
971
        value_to<inputs>(value.at("inputs")),
2✔
972
        value_to<outputs>(value.at("outputs")),
4✔
973
        value.at("locktime").to_number<uint32_t>()
4✔
974
    };
6✔
975
}
976

977
DEFINE_JSON_FROM_TAG(transaction)
4✔
978
{
979
    value =
16✔
980
    {
981
        // hash is computed property
982
        { "hash", encode_hash(instance.hash(false)) },
4✔
983
        { "version", instance.version() },
984
        { "inputs", value_from(*instance.inputs_ptr()) },
8✔
985
        { "outputs", value_from(*instance.outputs_ptr()) },
8✔
986
        { "locktime", instance.locktime() }
987
    };
4✔
988
}
4✔
989

990
DEFINE_JSON_TO_TAG(transaction::cptr)
×
991
{
992
    return to_shared(tag_invoke(to_tag<transaction>{}, value));
×
993
}
994

995
DEFINE_JSON_FROM_TAG(transaction::cptr)
2✔
996
{
997
    tag_invoke(from_tag{}, value, *instance);
2✔
998
}
2✔
999

1000
} // namespace chain
1001
} // namespace system
1002
} // 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