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

libbitcoin / libbitcoin-system / 14507858421

17 Apr 2025 04:11AM UTC coverage: 82.822% (+0.007%) from 82.815%
14507858421

push

github

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

Fix inverted NOEXCEPT define (regression), style, comments, whitespace.

7 of 8 new or added lines in 1 file covered. (87.5%)

15 existing lines in 6 files now uncovered.

10183 of 12295 relevant lines covered (82.82%)

3821900.38 hits per line

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

76.74
/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/magic_numbers.hpp>
29
#include <bitcoin/system/chain/header.hpp>
30
#include <bitcoin/system/chain/input.hpp>
31
#include <bitcoin/system/chain/output.hpp>
32
#include <bitcoin/system/chain/script.hpp>
33
#include <bitcoin/system/data/data.hpp>
34
#include <bitcoin/system/define.hpp>
35
#include <bitcoin/system/error/error.hpp>
36
#include <bitcoin/system/hash/hash.hpp>
37
#include <bitcoin/system/machine/machine.hpp>
38
#include <bitcoin/system/math/math.hpp>
39
#include <bitcoin/system/stream/stream.hpp>
40

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

45
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
46

47
// Precompute fixed elements of signature hashing.
48
// ----------------------------------------------------------------------------
49

50
constexpr auto prefixed = true;
51

52
static const auto& null_output() NOEXCEPT
1✔
53
{
54
    static const auto null = output{}.to_data();
1✔
55
    return null;
1✔
56
}
57

58
static const auto& empty_script() NOEXCEPT
8✔
59
{
60
    static const auto empty = script{}.to_data(prefixed);
8✔
61
    return empty;
8✔
62
}
63

64
static const auto& zero_sequence() NOEXCEPT
1✔
65
{
66
    static const auto sequence = to_little_endian<uint32_t>(0);
1✔
67
    return sequence;
1✔
68
}
69

70
// Constructors.
71
// ----------------------------------------------------------------------------
72

73
transaction::transaction() NOEXCEPT
21✔
74
  : transaction(0,
75
      to_shared<input_cptrs>(),
21✔
76
      to_shared<output_cptrs>(),
21✔
77
      0, false, false)
42✔
78
{
79
}
21✔
80

81
transaction::transaction(uint32_t version, chain::inputs&& inputs,
905✔
82
    chain::outputs&& outputs, uint32_t locktime) NOEXCEPT
905✔
83
  : transaction(version, to_shareds(std::move(inputs)),
905✔
84
      to_shareds(std::move(outputs)), locktime)
2,715✔
85
{
86
}
905✔
87

88
transaction::transaction(uint32_t version, const chain::inputs& inputs,
1✔
89
    const chain::outputs& outputs, uint32_t locktime) NOEXCEPT
1✔
90
  : transaction(version, to_shareds(inputs), to_shareds(outputs), locktime,
1✔
91
      segregated(inputs), true)
3✔
92
{
93
}
1✔
94

95
transaction::transaction(uint32_t version, const inputs_cptr& inputs,
905✔
96
    const outputs_cptr& outputs, uint32_t locktime) NOEXCEPT
905✔
97
  : transaction(version, inputs, outputs, locktime, segregated(*inputs), true)
905✔
98
{
99
}
905✔
100

101
transaction::transaction(stream::in::fast&& stream, bool witness) NOEXCEPT
41✔
102
  : transaction(read::bytes::fast(stream), witness)
41✔
103
{
104
}
41✔
105

106
transaction::transaction(stream::in::fast& stream, bool witness) NOEXCEPT
2✔
107
  : transaction(read::bytes::fast(stream), witness)
2✔
108
{
109
}
2✔
110

111
transaction::transaction(std::istream&& stream, bool witness) NOEXCEPT
×
112
  : transaction(read::bytes::istream(stream), witness)
×
113
{
114
}
×
115

116
transaction::transaction(std::istream& stream, bool witness) NOEXCEPT
4✔
117
  : transaction(read::bytes::istream(stream), witness)
4✔
118
{
119
}
4✔
120

121
transaction::transaction(reader&& source, bool witness) NOEXCEPT
47✔
122
  : transaction(source, witness)
47✔
123
{
124
}
×
125

126
transaction::transaction(reader& source, bool witness) NOEXCEPT
204✔
127
  : version_(source.read_4_bytes_little_endian()),
408✔
128
    inputs_(CREATE(input_cptrs, source.get_allocator())),
204✔
129
    outputs_(CREATE(output_cptrs, source.get_allocator()))
612✔
130
{
131
    assign_data(source, witness);
204✔
132
}
204✔
133

134
// protected
135
transaction::transaction(uint32_t version,
927✔
136
    const chain::inputs_cptr& inputs, const chain::outputs_cptr& outputs,
137
    uint32_t locktime, bool segregated, bool valid) NOEXCEPT
927✔
138
  : version_(version),
927✔
139
    inputs_(inputs ? inputs : to_shared<input_cptrs>()),
1,854✔
140
    outputs_(outputs ? outputs : to_shared<output_cptrs>()),
927✔
141
    locktime_(locktime),
927✔
142
    segregated_(segregated),
927✔
143
    valid_(valid),
927✔
144
    size_(serialized_size(*inputs, *outputs, segregated))
1,854✔
145
{
146
}
927✔
147

148
// Operators.
149
// ----------------------------------------------------------------------------
150

151
bool transaction::operator==(const transaction& other) const NOEXCEPT
60✔
152
{
153
    // Compares input/output elements, not pointers, cache not compared.
154
    return (version_ == other.version_)
60✔
155
        && (locktime_ == other.locktime_)
58✔
156
        && ((inputs_ == other.inputs_) || 
84✔
157
            deep_equal(*inputs_, *other.inputs_))
26✔
158
        && ((outputs_ == other.outputs_) ||
144✔
159
            deep_equal(*outputs_, *other.outputs_));
26✔
160
}
161

162
bool transaction::operator!=(const transaction& other) const NOEXCEPT
2✔
163
{
164
    return !(*this == other);
2✔
165
}
166

167
// Deserialization.
168
// ----------------------------------------------------------------------------
169

170
// private
171
BC_PUSH_WARNING(NO_UNGUARDED_POINTERS)
172
void transaction::assign_data(reader& source, bool witness) NOEXCEPT
204✔
173
{
174
    auto& allocator = source.get_allocator();
204✔
175
    auto ins = to_non_const_raw_ptr(inputs_);
204✔
176
    auto count = source.read_size(max_block_size);
204✔
177
    ins->reserve(count);
204✔
178
    for (size_t in = 0; in < count; ++in)
453✔
179
        ins->emplace_back(CREATE(input, allocator, source));
249✔
180

181
    // Expensive repeated recomputation, so cache segregated state.
182
    // Detect witness as no inputs (marker) and expected flag (bip144).
183
    segregated_ = 
204✔
184
        inputs_->size() == witness_marker &&
220✔
185
        source.peek_byte() == witness_enabled;
16✔
186

187
    if (segregated_)
204✔
188
    {
189
        // Skip over the peeked witness flag.
190
        source.skip_byte();
16✔
191

192
        count = source.read_size(max_block_size);
16✔
193
        ins->reserve(count);
16✔
194
        for (size_t in = 0; in < count; ++in)
37✔
195
            ins->emplace_back(CREATE(input, allocator, source));
21✔
196

197
        auto outs = to_non_const_raw_ptr(outputs_);
16✔
198
        count = source.read_size(max_block_size);
16✔
199
        outs->reserve(count);
16✔
200
        for (size_t out = 0; out < count; ++out)
41✔
201
            outs->emplace_back(CREATE(output, allocator, source));
25✔
202

203
        // Read or skip witnesses as specified.
204
        if (witness)
16✔
205
        {
206
            for (auto& input: *inputs_)
35✔
207
                to_non_const_raw_ptr(input)->set_witness(source);
20✔
208
        }
209
        else
210
        {
211
            // Default witness is populated on input construct.
212
            for (size_t in = 0; in < inputs_->size(); ++in)
2✔
213
                witness::skip(source, true);
1✔
214
        }
215
    }
216
    else
217
    {
218
        auto outs = to_non_const_raw_ptr(outputs_);
188✔
219
        count = source.read_size(max_block_size);
188✔
220
        outs->reserve(count);
188✔
221
        for (size_t out = 0; out < count; ++out)
437✔
222
            outs->emplace_back(CREATE(output, allocator, source));
249✔
223
    }
224

225
    locktime_ = source.read_4_bytes_little_endian();
204✔
226
    size_ = serialized_size(*inputs_, *outputs_, segregated_);
204✔
227
    valid_ = source;
204✔
228
}
204✔
229
BC_POP_WARNING()
230

231
// Serialization.
232
// ----------------------------------------------------------------------------
233

234
// Transactions with empty witnesses always use old serialization (bip144).
235
// If no inputs are witness programs then witness hash is tx hash (bip141).
236
data_chunk transaction::to_data(bool witness) const NOEXCEPT
10✔
237
{
238
    witness &= segregated_;
10✔
239

240
    data_chunk data(serialized_size(witness));
10✔
241
    stream::out::fast ostream(data);
10✔
242
    write::bytes::fast out(ostream);
10✔
243
    to_data(out, witness);
10✔
244
    return data;
20✔
245
}
10✔
246

247
void transaction::to_data(std::ostream& stream, bool witness) const NOEXCEPT
1✔
248
{
249
    witness &= segregated_;
1✔
250

251
    write::bytes::ostream out(stream);
1✔
252
    to_data(out, witness);
1✔
253
}
1✔
254

255
void transaction::to_data(writer& sink, bool witness) const NOEXCEPT
966✔
256
{
257
    witness &= segregated_;
966✔
258

259
    sink.write_4_bytes_little_endian(version_);
966✔
260

261
    if (witness)
966✔
262
    {
263
        sink.write_byte(witness_marker);
2✔
264
        sink.write_byte(witness_enabled);
2✔
265
    }
266

267
    sink.write_variable(inputs_->size());
966✔
268
    for (const auto& input: *inputs_)
2,352✔
269
        input->to_data(sink);
1,386✔
270

271
    sink.write_variable(outputs_->size());
966✔
272
    for (const auto& output: *outputs_)
2,634✔
273
        output->to_data(sink);
1,668✔
274

275
    if (witness)
966✔
276
        for (auto& input: *inputs_)
5✔
277
            input->witness().to_data(sink, true);
3✔
278

279
    sink.write_4_bytes_little_endian(locktime_);
966✔
280
}
966✔
281

282
// static/private
283
transaction::sizes transaction::serialized_size(const input_cptrs& inputs,
1,131✔
284
    const output_cptrs& outputs, bool segregated) NOEXCEPT
285
{
286
    sizes size{ zero, zero };
1,131✔
287

288
    std::for_each(inputs.begin(), inputs.end(), [&](const auto& in) NOEXCEPT
2,314✔
289
    {
290
        size.nominal = ceilinged_add(size.nominal, in->nominal_size());
1,183✔
291
        if (segregated)
1,183✔
292
            size.witnessed = ceilinged_add(size.witnessed, in->witnessed_size());
64✔
293
    });
1,183✔
294

295
    const auto outs = [](size_t total, const auto& output) NOEXCEPT
394✔
296
    {
297
        return ceilinged_add(total, output->serialized_size());
394✔
298
    };
299

300
    constexpr auto base_const_size = ceilinged_add(sizeof(version_),
1,131✔
301
        sizeof(locktime_));
302
    constexpr auto witness_const_size = ceilinged_add(sizeof(witness_marker),
1,131✔
303
        sizeof(witness_enabled));
304

305
    const auto base_size = ceilinged_add(ceilinged_add(ceilinged_add(
1,131✔
306
        base_const_size, variable_size(inputs.size())),
307
        variable_size(outputs.size())),
308
        std::accumulate(outputs.begin(), outputs.end(), zero, outs));
309
    const auto nominal_size = ceilinged_add(base_size, size.nominal);
1,131✔
310

311
    // witnessed_size is nominal_size for non-segregated transactions.
312
    const auto witnessed_size = segregated ? ceilinged_add(ceilinged_add(
1,131✔
313
        base_size, witness_const_size), size.witnessed) : nominal_size;
314

315
    // Values are the same for non-segregated transactions.
316
    return { nominal_size, witnessed_size };
1,131✔
317
}
318

319
size_t transaction::serialized_size(bool witness) const NOEXCEPT
613✔
320
{
321
    witness &= segregated_;
613✔
322

323
    return witness ? size_.witnessed : size_.nominal;
613✔
324
}
325

326
// Properties.
327
// ----------------------------------------------------------------------------
328

329
bool transaction::is_valid() const NOEXCEPT
777✔
330
{
331
    return valid_;
777✔
332
}
333

334
size_t transaction::spends() const NOEXCEPT
×
335
{
336
    return is_coinbase() ? zero : inputs_->size();
×
337
}
338

339
size_t transaction::inputs() const NOEXCEPT
1,569✔
340
{
341
    return inputs_->size();
1,569✔
342
}
343

344
size_t transaction::outputs() const NOEXCEPT
1✔
345
{
346
    return outputs_->size();
1✔
347
}
348

349
uint32_t transaction::version() const NOEXCEPT
6✔
350
{
351
    return version_;
4✔
352
}
353

354
uint32_t transaction::locktime() const NOEXCEPT
13✔
355
{
356
    return locktime_;
4✔
357
}
358

359
const inputs_cptr& transaction::inputs_ptr() const NOEXCEPT
2,446✔
360
{
361
    return inputs_;
2,446✔
362
}
363

364
const outputs_cptr& transaction::outputs_ptr() const NOEXCEPT
62✔
365
{
366
    return outputs_;
62✔
367
}
368

369
uint64_t transaction::fee() const NOEXCEPT
4✔
370
{
371
    // Underflow returns zero (and is_overspent() will be true).
372
    // This is value of prevouts spent by inputs minus that claimed by outputs.
373
    return floored_subtract(value(), claim());
4✔
374
}
375

376
void transaction::set_nominal_hash(const hash_digest& hash) const NOEXCEPT
20✔
377
{
378
    nominal_hash_ = hash;
20✔
379
}
1✔
380

381
void transaction::set_witness_hash(const hash_digest& hash) const NOEXCEPT
2✔
382
{
383
    witness_hash_ = hash;
2✔
384
}
1✔
385

386
const hash_digest& transaction::get_hash(bool witness) const NOEXCEPT
26✔
387
{
388
    if (witness)
26✔
389
    {
390
        if (!witness_hash_) set_witness_hash(hash(witness));
4✔
391
        return *witness_hash_;
3✔
392
    }
393
    else
394
    {
395
        if (!nominal_hash_) set_nominal_hash(hash(witness));
42✔
396
        return *nominal_hash_;
23✔
397
    }
398
}
399

400
hash_digest transaction::hash(bool witness) const NOEXCEPT
934✔
401
{
402
    if (segregated_)
934✔
403
    {
404
        if (witness)
23✔
405
        {
406
            // Witness coinbase tx hash is assumed to be null_hash (bip141).
407
            if (witness_hash_) return *witness_hash_;
×
408
            if (is_coinbase()) return null_hash;
×
409
        }
410
        else
411
        {
412
            if (nominal_hash_) return *nominal_hash_;
23✔
413
        }
414
    }
415
    else
416
    {
417
        if (nominal_hash_) return *nominal_hash_;
911✔
418
    }
419

420
    hash_digest digest{};
927✔
421
    stream::out::fast stream{ digest };
927✔
422
    hash::sha256x2::fast sink{ stream };
927✔
423
    to_data(sink, witness);
927✔
424
    sink.flush();
927✔
425
    return digest;
927✔
426
}
927✔
427

428
// static
429
hash_digest transaction::desegregated_hash(size_t witnessed,
×
430
    size_t unwitnessed, const uint8_t* data) NOEXCEPT
431
{
432
    if (is_null(data))
×
433
        return null_hash;
×
434

435
    constexpr auto preamble = sizeof(uint32_t) + two * sizeof(uint8_t);
×
436
    const auto puts = floored_subtract(unwitnessed, two * sizeof(uint32_t));
×
437
    const auto locktime = floored_subtract(witnessed, sizeof(uint32_t));
×
438

439
    hash_digest digest{};
×
440
    stream::out::fast stream{ digest };
×
441
    hash::sha256x2::fast sink{ stream };
×
442
    sink.write_bytes(data, sizeof(uint32_t));
×
443
    sink.write_bytes(std::next(data, preamble), puts);
×
444
    sink.write_bytes(std::next(data, locktime), sizeof(uint32_t));
×
445
    sink.flush();
×
446
    return digest;
×
447
}
×
448

449
// Methods.
450
// ----------------------------------------------------------------------------
451

452
bool transaction::is_dusty(uint64_t minimum_output_value) const NOEXCEPT
6✔
453
{
454
    const auto dusty = [=](const auto& output) NOEXCEPT
9✔
455
    {
456
        return output->is_dust(minimum_output_value);
9✔
457
    };
6✔
458

459
    return std::any_of(outputs_->begin(), outputs_->end(), dusty);
6✔
460
}
461

462
size_t transaction::signature_operations(bool bip16, bool bip141) const NOEXCEPT
1✔
463
{
464
    // Includes BIP16 p2sh additional sigops, max_size_t if prevout invalid.
465
    const auto in = [=](size_t total, const auto& input) NOEXCEPT
×
466
    {
467
        return ceilinged_add(total, input->signature_operations(bip16, bip141));
×
468
    };
1✔
469

470
    const auto out = [=](size_t total, const auto& output) NOEXCEPT
×
471
    {
472
        return ceilinged_add(total, output->signature_operations(bip141));
×
473
    };
1✔
474

475
    // Overflow returns max_size_t.
476
    return ceilinged_add(
1✔
477
        std::accumulate(inputs_->begin(), inputs_->end(), zero, in),
478
        std::accumulate(outputs_->begin(), outputs_->end(), zero, out));
1✔
479
}
480

481
chain::points transaction::points() const NOEXCEPT
4✔
482
{
483
    chain::points out(inputs_->size());
4✔
484

485
    const auto point = [](const auto& input) NOEXCEPT
8✔
486
    {
487
        return input->point();
8✔
488
    };
489

490
    std::transform(inputs_->begin(), inputs_->end(), out.begin(), point);
4✔
491
    return out;
4✔
492
}
493

494
hash_digest transaction::outputs_hash() const NOEXCEPT
8✔
495
{
496
    if (sighash_cache_)
8✔
497
        return sighash_cache_->outputs;
×
498

499
    hash_digest digest{};
8✔
500
    stream::out::fast stream{ digest };
8✔
501
    hash::sha256x2::fast sink{ stream };
8✔
502

503
    for (const auto& output: *outputs_)
22✔
504
        output->to_data(sink);
14✔
505

506
    sink.flush();
8✔
507
    return digest;
8✔
508
}
8✔
509

510
hash_digest transaction::points_hash() const NOEXCEPT
11✔
511
{
512
    if (sighash_cache_)
11✔
513
        return sighash_cache_->points;
×
514

515
    hash_digest digest{};
11✔
516
    stream::out::fast stream{ digest };
11✔
517
    hash::sha256x2::fast sink{ stream };
11✔
518

519
    for (const auto& input: *inputs_)
27✔
520
        input->point().to_data(sink);
16✔
521

522
    sink.flush();
11✔
523
    return digest;
11✔
524
}
11✔
525

526
hash_digest transaction::sequences_hash() const NOEXCEPT
7✔
527
{
528
    if (sighash_cache_)
7✔
529
        return sighash_cache_->sequences;
×
530

531
    hash_digest digest{};
7✔
532
    stream::out::fast stream{ digest };
7✔
533
    hash::sha256x2::fast sink{ stream };
7✔
534

535
    for (const auto& input: *inputs_)
17✔
536
        sink.write_4_bytes_little_endian(input->sequence());
10✔
537

538
    sink.flush();
7✔
539
    return digest;
7✔
540
}
7✔
541

542
// Signing (unversioned).
543
// ----------------------------------------------------------------------------
544

545
// private
546
transaction::input_iterator transaction::input_at(
4✔
547
    uint32_t index) const NOEXCEPT
548
{
549
    // Guarded by check_signature and create_endorsement.
550
    BC_ASSERT_MSG(index < inputs_->size(), "invalid input index");
4✔
551

552
    return std::next(inputs_->begin(), index);
4✔
553
}
554

555
// private
556
uint32_t transaction::input_index(const input_iterator& input) const NOEXCEPT
25✔
557
{
558
    // Guarded by unversioned_signature_hash and output_hash.
559
    BC_ASSERT_MSG(inputs_->begin() != inputs_->end(), "invalid input iterator");
25✔
560

561
    return possible_narrow_and_sign_cast<uint32_t>(
25✔
562
        std::distance(inputs_->begin(), input));
×
563
}
564

565
//*****************************************************************************
566
// CONSENSUS: Due to masking of bits 6/7 (8 is the anyone_can_pay flag),
567
// there are 4 possible 7 bit values that can set "single" and 4 others that
568
// can set none, and yet all other values set "all".
569
//*****************************************************************************
570
inline coverage mask_sighash(uint8_t sighash_flags) NOEXCEPT
41✔
571
{
572
    switch (sighash_flags & coverage::mask)
41✔
573
    {
574
        case coverage::hash_single:
575
            return coverage::hash_single;
576
        case coverage::hash_none:
2✔
577
            return coverage::hash_none;
2✔
578
        default:
18✔
579
            return coverage::hash_all;
18✔
580
    }
581
}
582

583
void transaction::signature_hash_single(writer& sink,
4✔
584
    const input_iterator& input, const script& sub,
585
    uint8_t sighash_flags) const NOEXCEPT
586
{
587
    const auto write_inputs = [this, &input, &sub, sighash_flags](
8✔
588
        writer& sink) NOEXCEPT
589
    {
590
        const auto anyone = to_bool(sighash_flags & coverage::anyone_can_pay);
4✔
591
        input_cptrs::const_iterator in;
4✔
592

593
        sink.write_variable(anyone ? one : inputs_->size());
4✔
594

595
        for (in = inputs_->begin(); !anyone && in != input; ++in)
4✔
596
        {
597
            (*in)->point().to_data(sink);
×
598
            sink.write_bytes(empty_script());
×
599
            sink.write_bytes(zero_sequence());
×
600
        }
601

602
        (*input)->point().to_data(sink);
4✔
603
        sub.to_data(sink, prefixed);
4✔
604
        sink.write_4_bytes_little_endian((*input)->sequence());
4✔
605

606
        for (++in; !anyone && in != inputs_->end(); ++in)
5✔
607
        {
608
            (*in)->point().to_data(sink);
1✔
609
            sink.write_bytes(empty_script());
1✔
610
            sink.write_bytes(zero_sequence());
1✔
611
        }
612
    };
4✔
613

614
    const auto write_outputs = [this, &input](writer& sink) NOEXCEPT
8✔
615
    {
616
        const auto index = input_index(input);
4✔
617

618
        sink.write_variable(add1(index));
4✔
619

620
        for (size_t output = 0; output < index; ++output)
5✔
621
            sink.write_bytes(null_output());
1✔
622

623
        // Guarded by unversioned_signature_hash.
624
        outputs_->at(index)->to_data(sink);
4✔
625
    };
8✔
626

627
    sink.write_4_bytes_little_endian(version_);
4✔
628
    write_inputs(sink);
4✔
629
    write_outputs(sink);
4✔
630
    sink.write_4_bytes_little_endian(locktime_);
4✔
631
    sink.write_4_bytes_little_endian(sighash_flags);
4✔
632
}
4✔
633

634
void transaction::signature_hash_none(writer& sink,
×
635
    const input_iterator& input, const script& sub,
636
    uint8_t sighash_flags) const NOEXCEPT
637
{
638
    const auto write_inputs = [this, &input, &sub, sighash_flags](
×
639
        writer& sink) NOEXCEPT
640
    {
641
        const auto anyone = to_bool(sighash_flags & coverage::anyone_can_pay);
×
642
        input_cptrs::const_iterator in;
×
643

644
        sink.write_variable(anyone ? one : inputs_->size());
×
645

646
        for (in = inputs_->begin(); !anyone && in != input; ++in)
×
647
        {
648
            (*in)->point().to_data(sink);
×
649
            sink.write_bytes(empty_script());
×
650
            sink.write_bytes(zero_sequence());
×
651
        }
652

653
        (*input)->point().to_data(sink);
×
654
        sub.to_data(sink, prefixed);
×
655
        sink.write_4_bytes_little_endian((*input)->sequence());
×
656

657
        for (++in; !anyone && in != inputs_->end(); ++in)
×
658
        {
659
            (*in)->point().to_data(sink);
×
660
            sink.write_bytes(empty_script());
×
661
            sink.write_bytes(zero_sequence());
×
662
        }
663
    };
×
664

665
    sink.write_4_bytes_little_endian(version_);
×
666
    write_inputs(sink);
×
667
    sink.write_variable(zero);
×
668
    sink.write_4_bytes_little_endian(locktime_);
×
669
    sink.write_4_bytes_little_endian(sighash_flags);
×
670
}
×
671

672
void transaction::signature_hash_all(writer& sink,
12✔
673
    const input_iterator& input, const script& sub,
674
    uint8_t flags) const NOEXCEPT
675
{
676
    const auto write_inputs = [this, &input, &sub, flags](
24✔
677
        writer& sink) NOEXCEPT
678
    {
679
        const auto anyone = to_bool(flags & coverage::anyone_can_pay);
12✔
680
        input_cptrs::const_iterator in;
12✔
681

682
        sink.write_variable(anyone ? one : inputs_->size());
12✔
683

684
        for (in = inputs_->begin(); !anyone && in != input; ++in)
15✔
685
        {
686
            (*in)->point().to_data(sink);
3✔
687
            sink.write_bytes(empty_script());
3✔
688
            sink.write_4_bytes_little_endian((*in)->sequence());
3✔
689
        }
690

691
        (*input)->point().to_data(sink);
12✔
692
        sub.to_data(sink, prefixed);
12✔
693
        sink.write_4_bytes_little_endian((*input)->sequence());
12✔
694

695
        for (++in; !anyone && in != inputs_->end(); ++in)
16✔
696
        {
697
            (*in)->point().to_data(sink);
4✔
698
            sink.write_bytes(empty_script());
4✔
699
            sink.write_4_bytes_little_endian((*in)->sequence());
4✔
700
        }
701
    };
12✔
702

703
    const auto write_outputs = [this](writer& sink) NOEXCEPT
24✔
704
    {
705
        sink.write_variable(outputs_->size());
12✔
706
        for (const auto& output: *outputs_)
27✔
707
            output->to_data(sink);
15✔
708
    };
24✔
709

710
    sink.write_4_bytes_little_endian(version_);
12✔
711
    write_inputs(sink);
12✔
712
    write_outputs(sink);
12✔
713
    sink.write_4_bytes_little_endian(locktime_);
12✔
714
    sink.write_4_bytes_little_endian(flags);
12✔
715
}
12✔
716

717
// private
718
hash_digest transaction::unversioned_signature_hash(
19✔
719
    const input_iterator& input, const script& sub,
720
    uint8_t sighash_flags) const NOEXCEPT
721
{
722
    // Set options.
723
    const auto flag = mask_sighash(sighash_flags);
19✔
724

725
    // Create hash writer.
726
    hash_digest digest{};
19✔
727
    stream::out::fast stream{ digest };
19✔
728
    hash::sha256x2::fast sink{ stream };
19✔
729

730
    switch (flag)
19✔
731
    {
732
        case coverage::hash_single:
7✔
733
        {
7✔
734
            //*****************************************************************
735
            // CONSENSUS: return one_hash if index exceeds outputs in sighash.
736
            // Related Bug: bitcointalk.org/index.php?topic=260595
737
            // Exploit: joncave.co.uk/2014/08/bitcoin-sighash-single/
738
            //*****************************************************************
739
            if (input_index(input) >= outputs_->size())
7✔
740
                return one_hash;
3✔
741

742
            signature_hash_single(sink, input, sub, sighash_flags);
4✔
743
            break;
4✔
744
        }
745
        case coverage::hash_none:
×
746
        {
×
747
            signature_hash_none(sink, input, sub, sighash_flags);
×
UNCOV
748
            break;
×
749
        }
750
        default:
12✔
751
        case coverage::hash_all:
12✔
752
        {
12✔
753
            signature_hash_all(sink, input, sub, sighash_flags);
12✔
754
        }
755
    }
756

757
    sink.flush();
16✔
758
    return digest;
16✔
759
}
19✔
760

761
// Signing (version 0).
762
// ----------------------------------------------------------------------------
763

764
// private
765
// TODO: taproot requires both single and double hash of each.
766
void transaction::initialize_sighash_cache() const NOEXCEPT
2✔
767
{
768
    // C++23: std::optional<T>::or_else.
769
    if (!segregated_)
2✔
770
        return;
771

772
    // This overconstructs the cache (anyone or !all), however it is simple.
773
    sighash_cache_ =
2✔
774
    {
775
        outputs_hash(),
2✔
776
        points_hash(),
2✔
777
        sequences_hash()
2✔
778
    };
2✔
779
}
780

781
// private
782
hash_digest transaction::output_hash(const input_iterator& input) const NOEXCEPT
14✔
783
{
784
    const auto index = input_index(input);
14✔
785

786
    //*************************************************************************
787
    // CONSENSUS: if index exceeds outputs in signature hash, return null_hash.
788
    //*************************************************************************
789
    if (index >= outputs_->size())
14✔
790
        return null_hash;
2✔
791

792
    hash_digest digest{};
12✔
793
    stream::out::fast stream{ digest };
12✔
794
    hash::sha256x2::fast sink{ stream };
12✔
795
    outputs_->at(index)->to_data(sink);
12✔
796
    sink.flush();
12✔
797
    return digest;
12✔
798
}
12✔
799

800
// private
801
hash_digest transaction::version_0_signature_hash(const input_iterator& input,
29✔
802
    const script& sub, uint64_t value, uint8_t sighash_flags,
803
    bool bip143) const NOEXCEPT
804
{
805
    // bip143/v0: the way of serialization is changed.
806
    if (!bip143)
29✔
807
        return unversioned_signature_hash(input, sub, sighash_flags);
7✔
808

809
    // Set options.
810
    const auto anyone = to_bool(sighash_flags & coverage::anyone_can_pay);
22✔
811
    const auto flag = mask_sighash(sighash_flags);
22✔
812
    const auto all = (flag == coverage::hash_all);
22✔
813
    const auto single = (flag == coverage::hash_single);
22✔
814

815
    // Create hash writer.
816
    hash_digest digest{};
22✔
817
    stream::out::fast stream{ digest };
22✔
818
    hash::sha256x2::fast sink{ stream };
22✔
819

820
    // Create signature hash.
821
    sink.write_little_endian(version_);
22✔
822

823
    // Conditioning points, sequences, and outputs writes on cache_ instead of
824
    // conditionally passing them from methods avoids copying the cached hash.
825

826
    // points
827
    sink.write_bytes(!anyone ? points_hash() : null_hash);
22✔
828

829
    // sequences
830
    sink.write_bytes(!anyone && all ? sequences_hash() : null_hash);
22✔
831

832
    (*input)->point().to_data(sink);
22✔
833
    sub.to_data(sink, prefixed);
22✔
834
    sink.write_little_endian(value);
22✔
835
    sink.write_little_endian((*input)->sequence());
22✔
836

837
    // outputs
838
    if (single)
22✔
839
        sink.write_bytes(output_hash(input));
14✔
840
    else
841
        sink.write_bytes(all ? outputs_hash() : null_hash);
8✔
842

843
    sink.write_little_endian(locktime_);
22✔
844
    sink.write_4_bytes_little_endian(sighash_flags);
22✔
845

846
    sink.flush();
22✔
847
    return digest;
22✔
848
}
22✔
849

850
// Signing (unversioned and version 0).
851
// ----------------------------------------------------------------------------
852

853
// ****************************************************************************
854
// CONSENSUS: sighash flags are carried in a single byte but are encoded as 4
855
// bytes in the signature hash preimage serialization.
856
// ****************************************************************************
857

858
hash_digest transaction::signature_hash(const input_iterator& input,
41✔
859
    const script& sub, uint64_t value, uint8_t sighash_flags,
860
    script_version version, bool bip143) const NOEXCEPT
861
{
862
    // There is no rational interpretation of a signature hash for a coinbase.
863
    BC_ASSERT(!is_coinbase());
41✔
864

865
    switch (version)
41✔
866
    {
867
        case script_version::unversioned:
12✔
868
            return unversioned_signature_hash(input, sub, sighash_flags);
12✔
869
        case script_version::zero:
29✔
870
            return version_0_signature_hash(input, sub, value, sighash_flags,
29✔
871
                bip143);
29✔
872
        case script_version::reserved:
×
873
        default:
×
874
            return {};
×
875
    }
876
}
877

878
// This is not used internal to the library.
879
bool transaction::check_signature(const ec_signature& signature,
2✔
880
    const data_slice& public_key, const script& sub, uint32_t index,
881
    uint64_t value, uint8_t sighash_flags, script_version version,
882
    bool bip143) const NOEXCEPT
883
{
884
    if ((index >= inputs_->size()) || signature.empty() || public_key.empty())
2✔
885
        return false;
886

887
    const auto sighash = signature_hash(input_at(index), sub, value,
2✔
888
        sighash_flags, version, bip143);
889

890
    // Validate the EC signature.
891
    return verify_signature(public_key, sighash, signature);
2✔
892
}
893

894
// This is not used internal to the library.
895
bool transaction::create_endorsement(endorsement& out, const ec_secret& secret,
2✔
896
    const script& sub, uint32_t index, uint64_t value, uint8_t sighash_flags,
897
    script_version version, bool bip143) const NOEXCEPT
898
{
899
    if (index >= inputs_->size())
2✔
900
        return false;
901

902
    out.reserve(max_endorsement_size);
2✔
903
    const auto sighash = signature_hash(input_at(index), sub, value,
2✔
904
        sighash_flags, version, bip143);
905

906
    // Create the EC signature and encode as DER.
907
    ec_signature signature;
2✔
908
    if (!sign(signature, secret, sighash) || !encode_signature(out, signature))
2✔
909
        return false;
×
910

911
    // Add the sighash type to the end of the DER signature -> endorsement.
912
    out.push_back(sighash_flags);
2✔
913
    ////out.shrink_to_fit();
914
    return true;
2✔
915
}
916

917
// Guard (context free).
918
// ----------------------------------------------------------------------------
919

920
bool transaction::is_coinbase() const NOEXCEPT
80✔
921
{
922
    return is_one(inputs_->size()) && inputs_->front()->point().is_null();
80✔
923
}
924

925
bool transaction::is_internal_double_spend() const NOEXCEPT
4✔
926
{
927
    // TODO: optimize (see block.is_internal_double_spend).
928
    return !is_distinct(points());
4✔
929
}
930

931
// TODO: a pool (non-coinbase) tx must fit into a block (with a coinbase).
932
bool transaction::is_oversized() const NOEXCEPT
×
933
{
934
    return serialized_size(false) > max_block_size;
×
935
}
936

937
// Guard (contextual).
938
// ----------------------------------------------------------------------------
939

940
// static/private
941
bool transaction::segregated(const chain::inputs& inputs) NOEXCEPT
1✔
942
{
943
    const auto witnessed = [](const auto& input) NOEXCEPT
2✔
944
    {
945
        return !input.witness().stack().empty();
1✔
946
    };
947

948
    return std::any_of(inputs.begin(), inputs.end(), witnessed);
1✔
949
}
950

951
// static/private
952
bool transaction::segregated(const input_cptrs& inputs) NOEXCEPT
905✔
953
{
954
    const auto witnessed = [](const auto& input) NOEXCEPT
908✔
955
    {
956
        return !input->witness().stack().empty();
908✔
957
    };
958

959
    return std::any_of(inputs.begin(), inputs.end(), witnessed);
905✔
960
}
961

962
bool transaction::is_segregated() const NOEXCEPT
4✔
963
{
964
    return segregated_;
4✔
965
}
966

967
size_t transaction::weight() const NOEXCEPT
×
968
{
969
    // Block weight is 3 * base size * + 1 * total size (bip141).
970
    return ceilinged_add(
×
971
        ceilinged_multiply(base_size_contribution, serialized_size(false)),
972
        ceilinged_multiply(total_size_contribution, serialized_size(true)));
×
973
}
974

975
bool transaction::is_overweight() const NOEXCEPT
×
976
{
977
    return weight() > max_block_weight;
×
978
}
979

980
//*****************************************************************************
981
// CONSENSUS: Legacy sigops are counted in coinbase scripts despite the fact
982
// that coinbase input scripts are never executed. There is no need to exclude
983
// p2sh coinbase sigops since there is never a script to count.
984
//*****************************************************************************
985
bool transaction::is_signature_operations_limit(bool bip16,
×
986
    bool bip141) const NOEXCEPT
987
{
988
    const auto limit = bip141 ? max_fast_sigops : max_block_sigops;
×
989
    return signature_operations(bip16, bip141) > limit;
×
990
}
991

992
// Check (context free).
993
// ----------------------------------------------------------------------------
994

995
bool transaction::is_empty() const NOEXCEPT
9✔
996
{
997
    return inputs_->empty() || outputs_->empty();
9✔
998
}
999

1000
bool transaction::is_null_non_coinbase() const NOEXCEPT
7✔
1001
{
1002
    BC_ASSERT(!is_coinbase());
7✔
1003

1004
    const auto invalid = [](const auto& input) NOEXCEPT
9✔
1005
    {
1006
        return input->point().is_null();
9✔
1007
    };
1008

1009
    // True if not coinbase but has null previous_output(s).
1010
    return std::any_of(inputs_->begin(), inputs_->end(), invalid);
7✔
1011
}
1012

1013
bool transaction::is_invalid_coinbase_size() const NOEXCEPT
9✔
1014
{
1015
    BC_ASSERT(is_coinbase());
9✔
1016

1017
    // True if coinbase and has invalid input[0] script size.
1018
    const auto script_size = inputs_->front()->script().serialized_size(false);
9✔
1019
    return script_size < min_coinbase_size || script_size > max_coinbase_size;
9✔
1020
}
1021

1022
// Accept (contextual).
1023
// ----------------------------------------------------------------------------
1024

1025
bool transaction::is_absolute_locked(size_t height, uint32_t timestamp,
5✔
1026
    uint32_t median_time_past, bool bip113) const NOEXCEPT
1027
{
1028
    // BIP113: comparing the locktime against the median of the past 11 block
1029
    // timestamps, rather than the timestamp of the block including the tx.
1030
    const auto time = bip113 ? median_time_past : timestamp;
5✔
1031

1032
    const auto finalized = [](const auto& input) NOEXCEPT
2✔
1033
    {
1034
        return input->is_final();
2✔
1035
    };
1036

1037
    const auto height_time = locktime_ < locktime_threshold ? height : time;
5✔
1038

1039
    return !(is_zero(locktime_) || locktime_ < height_time ||
8✔
1040
        std::all_of(inputs_->begin(), inputs_->end(), finalized));
3✔
1041
}
1042

1043
bool transaction::is_missing_prevouts() const NOEXCEPT
3✔
1044
{
1045
    BC_ASSERT(!is_coinbase());
3✔
1046

1047
    // Null or invalid prevout indicates not found.
1048
    const auto missing = [](const auto& input) NOEXCEPT
2✔
1049
    {
1050
        return !input->prevout;
1051
    };
1052

1053
    return std::any_of(inputs_->begin(), inputs_->end(), missing);
3✔
1054
}
1055

1056
uint64_t transaction::claim() const NOEXCEPT
8✔
1057
{
1058
    // Overflow returns max_uint64.
1059
    const auto sum = [](uint64_t total, const auto& output) NOEXCEPT
8✔
1060
    {
1061
        return ceilinged_add(total, output->value());
8✔
1062
    };
1063

1064
    // The amount claimed by outputs.
1065
    return std::accumulate(outputs_->begin(), outputs_->end(), 0_u64, sum);
8✔
1066
}
1067

1068
uint64_t transaction::value() const NOEXCEPT
9✔
1069
{
1070
    // Overflow, not populated, and coinbase (default) return max_uint64.
1071
    const auto sum = [](uint64_t total, const auto& input) NOEXCEPT
7✔
1072
    {
1073
        const auto value = input->prevout ? input->prevout->value() : max_uint64;
7✔
1074
        return ceilinged_add(total, value);
7✔
1075
    };
1076

1077
    // The amount of prevouts (referenced by inputs).
1078
    return std::accumulate(inputs_->begin(), inputs_->end(), 0_u64, sum);
9✔
1079
}
1080

1081
bool transaction::is_overspent() const NOEXCEPT
2✔
1082
{
1083
    BC_ASSERT(!is_coinbase());
2✔
1084

1085
    return claim() > value();
2✔
1086
}
1087

1088
constexpr bool is_non_coinbase_mature(size_t tx_height, size_t height) NOEXCEPT
2✔
1089
{
1090
    return tx_height <= height;
2✔
1091
}
1092

1093
// static
1094
//*****************************************************************************
1095
// CONSENSUS: Coinbase output matures at 100 blocks depth.
1096
// CONSENSUS: Genesis coinbase is forever immature (exception).
1097
//*****************************************************************************
1098
bool transaction::is_coinbase_mature(size_t coinbase_height,
3✔
1099
    size_t height) NOEXCEPT
1100
{
1101
    return !is_zero(coinbase_height) &&
5✔
1102
        ceilinged_add(coinbase_height, coinbase_maturity) <= height;
3✔
1103
}
1104

1105
bool transaction::is_immature(size_t height) const NOEXCEPT
6✔
1106
{
1107
    BC_ASSERT(!is_coinbase());
6✔
1108

1109
    // Spends internal to a block are handled by block validation.
1110
    const auto mature = [=](const auto& input) NOEXCEPT
5✔
1111
    {
1112
        return input->metadata.coinbase ?
5✔
1113
            is_coinbase_mature(input->metadata.height, height) :
3✔
1114
            is_non_coinbase_mature(input->metadata.height, height);
2✔
1115
    };
6✔
1116

1117
    return !std::all_of(inputs_->begin(), inputs_->end(), mature);
6✔
1118
}
1119

1120
// static
1121
bool transaction::is_relative_locktime_applied(bool coinbase, uint32_t version,
×
1122
    uint32_t sequence) NOEXCEPT
1123
{
1124
    // BIP68: not applied to the sequence of the input of a coinbase.
1125
    // BIP68: if bit 31 is set then no consensus meaning is applied.
1126
    // BIP68: applied to txs with a version greater than or equal to two.
1127
    return !coinbase && input::is_relative_locktime_applied(sequence) &&
×
1128
        (version >= relative_locktime_min_version);
×
1129
}
1130

1131
bool transaction::is_internally_locked(const input& in) const NOEXCEPT
×
1132
{
1133
    // BIP68: not applied to the sequence of the input of a coinbase.
1134
    BC_ASSERT(!is_coinbase());
×
1135

1136
    // BIP68: applied to txs with a version greater than or equal to two.
1137
    if (version_ < relative_locktime_min_version)
×
1138
        return false;
1139

1140
    // Internal spends have no relative height/mtp (own metadata vs. itself).
1141
    return in.is_relative_locked(in.metadata.height,
×
1142
        in.metadata.median_time_past);
×
1143
}
1144

1145
bool transaction::is_relative_locked(size_t height,
4✔
1146
    uint32_t median_time_past) const NOEXCEPT
1147
{
1148
    // BIP68: not applied to the sequence of the input of a coinbase.
1149
    BC_ASSERT(!is_coinbase());
4✔
1150

1151
    // BIP68: applied to txs with a version greater than or equal to two.
1152
    if (version_ < relative_locktime_min_version)
4✔
1153
        return false;
1154

1155
    // BIP68: references to median time past are as defined by bip113.
1156
    const auto locked = [=](const auto& input) NOEXCEPT
2✔
1157
    {
1158
        return input->is_relative_locked(height, median_time_past);
2✔
1159
    };
2✔
1160

1161
    return std::any_of(inputs_->begin(), inputs_->end(), locked);
2✔
1162
}
1163

1164
// Spends internal to a block are handled by block validation.
1165
bool transaction::is_unconfirmed_spend(size_t height) const NOEXCEPT
×
1166
{
1167
    BC_ASSERT(!is_coinbase());
×
1168

1169
    // Zero is either genesis or not found.
1170
    // Test maturity first to obtain proper error code.
1171
    // Spends internal to a block are handled by block validation.
1172
    const auto unconfirmed = [=](const auto& input) NOEXCEPT
×
1173
    {
1174
        const auto prevout_height = input->metadata.height;
×
1175
        return is_zero(prevout_height) && !(height > prevout_height);
×
1176
    };
×
1177

1178
    return std::any_of(inputs_->begin(), inputs_->end(), unconfirmed);
×
1179
}
1180

1181
bool transaction::is_confirmed_double_spend(size_t height) const NOEXCEPT
4✔
1182
{
1183
    BC_ASSERT(!is_coinbase());
4✔
1184

1185
    // Spends internal to a block are handled by block validation.
1186
    const auto spent = [=](const auto& input) NOEXCEPT
3✔
1187
    {
1188
        return input->metadata.spent && height > input->metadata.height;
3✔
1189
    };
4✔
1190

1191
    return std::any_of(inputs_->begin(), inputs_->end(), spent);
4✔
1192
}
1193

1194
// Guards (for tx pool without compact blocks).
1195
// ----------------------------------------------------------------------------
1196

1197
// Pools do not have coinbases.
1198
// Redundant with block is_internal_double_spend check.
1199
// Redundant with block max_block_size check.
1200
code transaction::guard_check() const NOEXCEPT
×
1201
{
1202
    if (is_coinbase())
×
1203
        return error::coinbase_transaction;
×
1204
    if (is_internal_double_spend())
×
1205
        return error::transaction_internal_double_spend;
×
1206
    if (is_oversized())
×
1207
        return error::transaction_size_limit;
×
1208

1209
    return error::transaction_success;
×
1210
}
1211

1212
// Redundant with block max_block_weight accept.
1213
code transaction::guard_check(const context& ctx) const NOEXCEPT
×
1214
{
1215
    const auto bip141 = ctx.is_enabled(flags::bip141_rule);
×
1216

1217
     if (!bip141 && is_segregated())
×
1218
        return error::unexpected_witness_transaction;
×
NEW
1219
     if (bip141 && is_overweight())
×
1220
        return error::transaction_weight_limit;
×
1221

1222
    return error::transaction_success;
×
1223
}
1224

1225
// Redundant with block max_block_sigops accept.
1226
code transaction::guard_accept(const context& ctx) const NOEXCEPT
×
1227
{
1228
    const auto bip16 = ctx.is_enabled(flags::bip16_rule);
×
1229
    const auto bip141 = ctx.is_enabled(flags::bip141_rule);
×
1230

1231
    if (is_missing_prevouts())
×
1232
        return error::missing_previous_output;
×
1233
    if (is_signature_operations_limit(bip16, bip141))
×
1234
        return error::transaction_sigop_limit;
×
1235

1236
    return error::transaction_success;
×
1237
}
1238

1239
// Validation.
1240
// ----------------------------------------------------------------------------
1241

1242
// DO invoke on coinbase.
1243
code transaction::check() const NOEXCEPT
5✔
1244
{
1245
    const auto coinbase = is_coinbase();
5✔
1246

1247
    if (is_empty())
5✔
1248
        return error::empty_transaction;
×
1249
    if (coinbase && is_invalid_coinbase_size())
5✔
1250
        return error::invalid_coinbase_script_size;
×
1251
    if (!coinbase && is_null_non_coinbase())
5✔
1252
        return error::previous_output_null;
×
1253

1254
    return error::transaction_success;
5✔
1255
}
1256

1257
// forks
1258
// height
1259
// timestamp
1260
// median_time_past
1261

1262
// DO invoke on coinbase.
1263
code transaction::check(const context& ctx) const NOEXCEPT
×
1264
{
1265
    const auto bip113 = ctx.is_enabled(bip113_rule);
×
1266

1267
    if (is_absolute_locked(ctx.height, ctx.timestamp, ctx.median_time_past, bip113))
×
1268
        return error::absolute_time_locked;
×
1269

1270
    return error::transaction_success;
×
1271
}
1272

1273
// Do not need to invoke on coinbase.
1274
// This assumes that prevout caching is completed on all inputs.
1275
code transaction::accept(const context&) const NOEXCEPT
×
1276
{
1277
    ////BC_ASSERT(!is_coinbase());
1278

1279
    if (is_coinbase())
×
1280
        return error::transaction_success;
×
1281
    if (is_missing_prevouts())
×
1282
        return error::missing_previous_output;
×
1283
    if (is_overspent())
×
1284
        return error::spend_exceeds_value;
×
1285

1286
    return error::transaction_success;
×
1287
}
1288

1289
// forks
1290
// height
1291
// median_time_past
1292

1293
// Do not need to invoke on coinbase.
1294
// Node performs these checks through database query.
1295
// This assumes that prevout and metadata caching are completed on all inputs.
1296
code transaction::confirm(const context& ctx) const NOEXCEPT
×
1297
{
1298
    ////BC_ASSERT(!is_coinbase());
1299
    const auto bip68 = ctx.is_enabled(bip68_rule);
×
1300

1301
    if (is_coinbase())
×
1302
        return error::transaction_success;
×
1303
    if (bip68 && is_relative_locked(ctx.height, ctx.median_time_past))
×
1304
        return error::relative_time_locked;
×
1305
    if (is_immature(ctx.height))
×
1306
        return error::coinbase_maturity;
×
1307
    if (is_unconfirmed_spend(ctx.height))
×
1308
        return error::unconfirmed_spend;
×
1309
    if (is_confirmed_double_spend(ctx.height))
×
1310
        return error::confirmed_double_spend;
×
1311

1312
    return error::transaction_success;
×
1313
}
1314

1315
// Connect (contextual).
1316
// ----------------------------------------------------------------------------
1317

1318
// forks
1319

1320
// Do not need to invoke on coinbase.
1321
// This assumes that prevout caching is completed on all inputs.
1322
code transaction::connect(const context& ctx) const NOEXCEPT
2✔
1323
{
1324
    ////BC_ASSERT(!is_coinbase());
1325

1326
    if (is_coinbase())
2✔
1327
        return error::transaction_success;
×
1328

1329
    code ec{};
2✔
1330
    using namespace machine;
2✔
1331
    initialize_sighash_cache();
2✔
1332

1333
    // Validate scripts.
1334
    for (auto input = inputs_->begin(); input != inputs_->end(); ++input)
6✔
1335
    {
1336
        // Evaluate rolling scripts with linear search but constant erase.
1337
        // Evaluate non-rolling scripts with constant search but linear erase.
1338
        if ((ec = (*input)->is_roller() ?
8✔
1339
            interpreter<linked_stack>::connect(ctx, *this, input) :
×
1340
            interpreter<contiguous_stack>::connect(ctx, *this, input)))
4✔
1341
            return ec;
×
1342
    }
1343

1344
    // TODO: accumulate sigops from each connect result and add coinbase.
1345
    // TODO: return in override with out parameter. more impactful with segwit.
1346
    return error::transaction_success;
2✔
1347
}
1348

1349
BC_POP_WARNING()
1350

1351
// JSON value convertors.
1352
// ----------------------------------------------------------------------------
1353

1354
namespace json = boost::json;
1355

1356
// boost/json will soon have NOEXCEPT: github.com/boostorg/json/pull/636
1357
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
1358

1359
transaction tag_invoke(json::value_to_tag<transaction>,
2✔
1360
    const json::value& value) NOEXCEPT
1361
{
1362
    return
2✔
1363
    {
1364
        value.at("version").to_number<uint32_t>(),
2✔
1365
        json::value_to<chain::inputs>(value.at("inputs")),
2✔
1366
        json::value_to<chain::outputs>(value.at("outputs")),
4✔
1367
        value.at("locktime").to_number<uint32_t>()
4✔
1368
    };
4✔
1369
}
1370

1371
void tag_invoke(json::value_from_tag, json::value& value,
4✔
1372
    const transaction& tx) NOEXCEPT
1373
{
1374
    value =
4✔
1375
    {
1376
        { "version", tx.version() },
1377
        { "inputs", *tx.inputs_ptr() },
1378
        { "outputs", *tx.outputs_ptr() },
1379
        { "locktime", tx.locktime() }
1380
    };
4✔
1381
}
4✔
1382

1383
BC_POP_WARNING()
1384

1385
transaction::cptr tag_invoke(json::value_to_tag<transaction::cptr>,
×
1386
    const json::value& value) NOEXCEPT
1387
{
1388
    return to_shared(tag_invoke(json::value_to_tag<transaction>{}, value));
×
1389
}
1390

1391
// Shared pointer overload is required for navigation.
1392
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
1393
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
1394

1395
void tag_invoke(json::value_from_tag tag, json::value& value,
2✔
1396
    const transaction::cptr& tx) NOEXCEPT
1397
{
1398
    tag_invoke(tag, value, *tx);
2✔
1399
}
2✔
1400

1401
BC_POP_WARNING()
1402
BC_POP_WARNING()
1403

1404
} // namespace chain
1405
} // namespace system
1406
} // 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