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

libbitcoin / libbitcoin-system / 11616218925

31 Oct 2024 04:47PM UTC coverage: 83.009% (-0.001%) from 83.01%
11616218925

push

github

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

Avoid assigning shared ptr dereference to reference.

13 of 23 new or added lines in 2 files covered. (56.52%)

1 existing line in 1 file now uncovered.

10059 of 12118 relevant lines covered (83.01%)

4765458.48 hits per line

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

78.47
/src/chain/transaction.cpp
1
/**
2
 * Copyright (c) 2011-2023 libbitcoin developers (see AUTHORS)
3
 *
4
 * This file is part of libbitcoin.
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
#include <bitcoin/system/chain/transaction.hpp>
20

21
#include <algorithm>
22
#include <iterator>
23
#include <memory>
24
#include <numeric>
25
#include <type_traits>
26
#include <utility>
27
#include <vector>
28
#include <bitcoin/system/chain/context.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
// Precompute fixed elements of signature hashing.
49
// ----------------------------------------------------------------------------
50

51
constexpr auto prefixed = true;
52

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

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

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

71
// Constructors.
72
// ----------------------------------------------------------------------------
73

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

82
transaction::transaction(uint32_t version, chain::inputs&& inputs,
905✔
83
    chain::outputs&& outputs, uint32_t locktime) NOEXCEPT
905✔
84
  : transaction(version, to_shareds(std::move(inputs)),
1,810✔
85
      to_shareds(std::move(outputs)), locktime)
3,620✔
86
{
87
}
905✔
88

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

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

102
transaction::transaction(const data_slice& data, bool witness) NOEXCEPT
41✔
103
  : transaction(stream::in::copy(data), witness)
41✔
104
{
105
}
41✔
106

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

112
transaction::transaction(stream::in::fast& stream, bool witness) NOEXCEPT
2✔
113
  : transaction(read::bytes::fast(stream), witness)
2✔
114
{
115
}
2✔
116

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

122
transaction::transaction(std::istream& stream, bool witness) NOEXCEPT
4✔
123
  : transaction(read::bytes::istream(stream), witness)
4✔
124
{
125
}
4✔
126

127
transaction::transaction(reader&& source, bool witness) NOEXCEPT
47✔
128
  : transaction(source, witness)
47✔
129
{
130
}
×
131

132
transaction::transaction(reader& source, bool witness) NOEXCEPT
179✔
133
  : version_(source.read_4_bytes_little_endian()),
358✔
134
    inputs_(CREATE(input_cptrs, source.get_allocator())),
179✔
135
    outputs_(CREATE(output_cptrs, source.get_allocator()))
537✔
136
{
137
    assign_data(source, witness);
179✔
138
}
179✔
139

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

154
// Operators.
155
// ----------------------------------------------------------------------------
156

157
bool transaction::operator==(const transaction& other) const NOEXCEPT
60✔
158
{
159
    // Compares input/output elements, not pointers, cache not compared.
160
    return (version_ == other.version_)
60✔
161
        && (locktime_ == other.locktime_)
58✔
162
        && ((inputs_ == other.inputs_) || 
74✔
163
            deep_equal(*inputs_, *other.inputs_))
16✔
164
        && ((outputs_ == other.outputs_) ||
134✔
165
            deep_equal(*outputs_, *other.outputs_));
16✔
166
}
167

168
bool transaction::operator!=(const transaction& other) const NOEXCEPT
2✔
169
{
170
    return !(*this == other);
2✔
171
}
172

173
// Deserialization.
174
// ----------------------------------------------------------------------------
175

176
// private
177
BC_PUSH_WARNING(NO_UNGUARDED_POINTERS)
178
void transaction::assign_data(reader& source, bool witness) NOEXCEPT
179✔
179
{
180
    auto& allocator = source.get_allocator();
179✔
181
    auto ins = to_non_const_raw_ptr(inputs_);
179✔
182
    auto count = source.read_size(max_block_size);
179✔
183
    ins->reserve(count);
179✔
184
    for (size_t in = 0; in < count; ++in)
403✔
185
        ins->emplace_back(CREATE(input, allocator, source));
224✔
186

187
    // Expensive repeated recomputation, so cache segregated state.
188
    // Detect witness as no inputs (marker) and expected flag (bip144).
189
    segregated_ = 
179✔
190
        inputs_->size() == witness_marker &&
195✔
191
        source.peek_byte() == witness_enabled;
16✔
192

193
    if (segregated_)
179✔
194
    {
195
        // Skip over the peeked witness flag.
196
        source.skip_byte();
16✔
197

198
        count = source.read_size(max_block_size);
16✔
199
        ins->reserve(count);
16✔
200
        for (size_t in = 0; in < count; ++in)
37✔
201
            ins->emplace_back(CREATE(input, allocator, source));
21✔
202

203
        auto outs = to_non_const_raw_ptr(outputs_);
16✔
204
        count = source.read_size(max_block_size);
16✔
205
        outs->reserve(count);
16✔
206
        for (size_t out = 0; out < count; ++out)
41✔
207
            outs->emplace_back(CREATE(output, allocator, source));
25✔
208

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

231
    locktime_ = source.read_4_bytes_little_endian();
179✔
232
    size_ = serialized_size(*inputs_, *outputs_, segregated_);
179✔
233
    valid_ = source;
179✔
234
}
179✔
235
BC_POP_WARNING()
236

237
// Serialization.
238
// ----------------------------------------------------------------------------
239

240
// Transactions with empty witnesses always use old serialization (bip144).
241
// If no inputs are witness programs then witness hash is tx hash (bip141).
242
data_chunk transaction::to_data(bool witness) const NOEXCEPT
10✔
243
{
244
    witness &= segregated_;
10✔
245

246
    data_chunk data(serialized_size(witness));
10✔
247
    stream::out::copy ostream(data);
10✔
248
    to_data(ostream, witness);
10✔
249
    return data;
20✔
250
}
10✔
251

252
void transaction::to_data(std::ostream& stream, bool witness) const NOEXCEPT
11✔
253
{
254
    witness &= segregated_;
11✔
255

256
    write::bytes::ostream out(stream);
11✔
257
    to_data(out, witness);
11✔
258
}
11✔
259

260
void transaction::to_data(writer& sink, bool witness) const NOEXCEPT
969✔
261
{
262
    witness &= segregated_;
969✔
263

264
    sink.write_4_bytes_little_endian(version_);
969✔
265

266
    if (witness)
969✔
267
    {
268
        sink.write_byte(witness_marker);
2✔
269
        sink.write_byte(witness_enabled);
2✔
270
    }
271

272
    sink.write_variable(inputs_->size());
969✔
273
    for (const auto& input: *inputs_)
2,358✔
274
        input->to_data(sink);
1,389✔
275

276
    sink.write_variable(outputs_->size());
969✔
277
    for (const auto& output: *outputs_)
2,639✔
278
        output->to_data(sink);
1,670✔
279

280
    if (witness)
969✔
281
        for (auto& input: *inputs_)
5✔
282
            input->witness().to_data(sink, true);
3✔
283

284
    sink.write_4_bytes_little_endian(locktime_);
969✔
285
}
969✔
286

287
// static/private
288
transaction::sizes transaction::serialized_size(
1,106✔
289
    const chain::input_cptrs& inputs,
290
    const chain::output_cptrs& outputs, bool segregated) NOEXCEPT
291
{
292
    sizes size{ zero, zero };
1,106✔
293

294
    // Keep the condition outside of the loop.
295
    if (segregated)
1,106✔
296
    {
297
        std::for_each(inputs.begin(), inputs.end(), [&](const auto& in) NOEXCEPT
54✔
298
        {
299
            size.nominal = ceilinged_add(size.nominal, in->nominal_size());
64✔
300
            size.witnessed = ceilinged_add(size.witnessed, in->witnessed_size());
32✔
301
        });
32✔
302
    }
303
    else
304
    {
305
        // Witness must be zeroed because witnesses have nonzero size when they
306
        // are zero-valued, so they can be archived easily. Also it would be
307
        // wasteful to to count mutiple zero sizes, so exclude them here.
308
        std::for_each(inputs.begin(), inputs.end(), [&](const auto& in) NOEXCEPT
2,210✔
309
        {
310
            size.nominal = ceilinged_add(size.nominal, in->nominal_size());
1,126✔
311
        });
1,126✔
312
    }
313

314
    const auto outs = [](size_t total, const auto& output) NOEXCEPT
369✔
315
    {
316
        return ceilinged_add(total, output->serialized_size());
369✔
317
    };
318

319
    constexpr auto base_const_size = ceilinged_add(sizeof(version_),
1,106✔
320
        sizeof(locktime_));
321

322
    constexpr auto witness_const_size = ceilinged_add(sizeof(witness_marker),
1,106✔
323
        sizeof(witness_enabled));
324

325
    const auto base_size = ceilinged_add(ceilinged_add(ceilinged_add(
1,106✔
326
        base_const_size, variable_size(inputs.size())),
327
        variable_size(outputs.size())),
328
        std::accumulate(outputs.begin(), outputs.end(), zero, outs));
329

330
    const auto nominal_size = ceilinged_add(base_size, size.nominal);
1,106✔
331
    const auto witnessed_size = ceilinged_add(ceilinged_add(base_size,
1,106✔
332
        witness_const_size),
333
        size.witnessed);
334

335
    return { nominal_size, witnessed_size };
1,106✔
336
}
337

338
size_t transaction::serialized_size(bool witness) const NOEXCEPT
563✔
339
{
340
    witness &= segregated_;
563✔
341

342
    return witness ? size_.witnessed : size_.nominal;
563✔
343
}
344

345
// Properties.
346
// ----------------------------------------------------------------------------
347

348
bool transaction::is_valid() const NOEXCEPT
777✔
349
{
350
    return valid_;
777✔
351
}
352

353
size_t transaction::inputs() const NOEXCEPT
1,569✔
354
{
355
    return inputs_->size();
1,569✔
356
}
357

358
size_t transaction::outputs() const NOEXCEPT
1✔
359
{
360
    return outputs_->size();
1✔
361
}
362

363
uint32_t transaction::version() const NOEXCEPT
6✔
364
{
365
    return version_;
4✔
366
}
367

368
uint32_t transaction::locktime() const NOEXCEPT
15✔
369
{
370
    return locktime_;
4✔
371
}
372

373
const inputs_cptr& transaction::inputs_ptr() const NOEXCEPT
2,446✔
374
{
375
    return inputs_;
2,446✔
376
}
377

378
const outputs_cptr& transaction::outputs_ptr() const NOEXCEPT
62✔
379
{
380
    return outputs_;
62✔
381
}
382

383
uint64_t transaction::fee() const NOEXCEPT
4✔
384
{
385
    // Underflow returns zero (and is_overspent() will be true).
386
    // This is value of prevouts spent by inputs minus that claimed by outputs.
387
    return floored_subtract(value(), claim());
4✔
388
}
389

390
void transaction::set_nominal_hash(const hash_digest& hash) const NOEXCEPT
9✔
391
{
392
    nominal_hash_ = hash;
9✔
393
}
1✔
394

395
void transaction::set_witness_hash(const hash_digest& hash) const NOEXCEPT
2✔
396
{
397
    witness_hash_ = hash;
2✔
398
}
1✔
399

400
const hash_digest& transaction::get_hash(bool witness) const NOEXCEPT
13✔
401
{
402
    if (witness)
13✔
403
    {
404
        if (!witness_hash_) set_witness_hash(hash(witness));
4✔
405
        return *witness_hash_;
3✔
406
    }
407
    else
408
    {
409
        if (!nominal_hash_) set_nominal_hash(hash(witness));
18✔
410
        return *nominal_hash_;
10✔
411
    }
412
}
413

414
hash_digest transaction::hash(bool witness) const NOEXCEPT
937✔
415
{
416
    if (segregated_)
937✔
417
    {
418
        if (witness)
23✔
419
        {
420
            // Witness coinbase tx hash is assumed to be null_hash (bip141).
421
            if (witness_hash_) return *witness_hash_;
×
422
            if (is_coinbase()) return null_hash;
×
423
        }
424
        else
425
        {
426
            if (nominal_hash_) return *nominal_hash_;
23✔
427
        }
428
    }
429
    else
430
    {
431
        if (nominal_hash_) return *nominal_hash_;
914✔
432
    }
433

434
    BC_PUSH_WARNING(LOCAL_VARIABLE_NOT_INITIALIZED)
435
    hash_digest digest;
930✔
436
    BC_POP_WARNING()
437

438
    stream::out::fast stream{ digest };
930✔
439
    hash::sha256x2::fast sink{ stream };
930✔
440
    to_data(sink, witness);
930✔
441
    sink.flush();
930✔
442
    return digest;
930✔
443
}
930✔
444

445
// static
446
hash_digest transaction::desegregated_hash(size_t witnessed,
×
447
    size_t unwitnessed, const uint8_t* data) NOEXCEPT
448
{
449
    if (is_null(data))
×
450
        return null_hash;
×
451

452
    constexpr auto preamble = sizeof(uint32_t) + two * sizeof(uint8_t);
×
453
    const auto puts = floored_subtract(unwitnessed, two * sizeof(uint32_t));
×
454
    const auto locktime = floored_subtract(witnessed, sizeof(uint32_t));
×
455

456
    hash_digest digest{};
×
457
    stream::out::fast stream{ digest };
×
458
    hash::sha256x2::fast sink{ stream };
×
459
    sink.write_bytes(data, sizeof(uint32_t));
×
460
    sink.write_bytes(std::next(data, preamble), puts);
×
461
    sink.write_bytes(std::next(data, locktime), sizeof(uint32_t));
×
462
    sink.flush();
×
463
    return digest;
×
464
}
×
465

466
// Methods.
467
// ----------------------------------------------------------------------------
468

469
bool transaction::is_dusty(uint64_t minimum_output_value) const NOEXCEPT
6✔
470
{
471
    const auto dusty = [=](const auto& output) NOEXCEPT
9✔
472
    {
473
        return output->is_dust(minimum_output_value);
9✔
474
    };
6✔
475

476
    return std::any_of(outputs_->begin(), outputs_->end(), dusty);
6✔
477
}
478

479
size_t transaction::signature_operations(bool bip16, bool bip141) const NOEXCEPT
1✔
480
{
481
    // Includes BIP16 p2sh additional sigops, max_size_t if prevout invalid.
482
    const auto in = [=](size_t total, const auto& input) NOEXCEPT
×
483
    {
484
        return ceilinged_add(total, input->signature_operations(bip16, bip141));
×
485
    };
1✔
486

487
    const auto out = [=](size_t total, const auto& output) NOEXCEPT
×
488
    {
489
        return ceilinged_add(total, output->signature_operations(bip141));
×
490
    };
1✔
491

492
    // Overflow returns max_size_t.
493
    return ceilinged_add(
1✔
494
        std::accumulate(inputs_->begin(), inputs_->end(), zero, in),
495
        std::accumulate(outputs_->begin(), outputs_->end(), zero, out));
1✔
496
}
497

498
chain::points transaction::points() const NOEXCEPT
4✔
499
{
500
    chain::points out(inputs_->size());
4✔
501

502
    const auto point = [](const auto& input) NOEXCEPT
8✔
503
    {
504
        return input->point();
8✔
505
    };
506

507
    std::transform(inputs_->begin(), inputs_->end(), out.begin(), point);
4✔
508
    return out;
4✔
509
}
510

511
hash_digest transaction::outputs_hash() const NOEXCEPT
8✔
512
{
513
    if (sighash_cache_)
8✔
514
        return sighash_cache_->outputs;
×
515

516
    BC_PUSH_WARNING(LOCAL_VARIABLE_NOT_INITIALIZED)
517
    hash_digest digest;
8✔
518
    BC_POP_WARNING()
519
        
520
    stream::out::fast stream{ digest };
8✔
521
    hash::sha256x2::fast sink{ stream };
8✔
522

523
    for (const auto& output: *outputs_)
22✔
524
        output->to_data(sink);
14✔
525

526
    sink.flush();
8✔
527
    return digest;
8✔
528
}
8✔
529

530
hash_digest transaction::points_hash() const NOEXCEPT
11✔
531
{
532
    if (sighash_cache_)
11✔
533
        return sighash_cache_->points;
×
534

535
    BC_PUSH_WARNING(LOCAL_VARIABLE_NOT_INITIALIZED)
536
    hash_digest digest;
11✔
537
    BC_POP_WARNING()
538

539
    stream::out::fast stream{ digest };
11✔
540
    hash::sha256x2::fast sink{ stream };
11✔
541

542
    for (const auto& input: *inputs_)
27✔
543
        input->point().to_data(sink);
16✔
544

545
    sink.flush();
11✔
546
    return digest;
11✔
547
}
11✔
548

549
hash_digest transaction::sequences_hash() const NOEXCEPT
7✔
550
{
551
    if (sighash_cache_)
7✔
552
        return sighash_cache_->sequences;
×
553

554
    BC_PUSH_WARNING(LOCAL_VARIABLE_NOT_INITIALIZED)
555
    hash_digest digest;
7✔
556
    BC_POP_WARNING()
557

558
    stream::out::fast stream{ digest };
7✔
559
    hash::sha256x2::fast sink{ stream };
7✔
560

561
    for (const auto& input: *inputs_)
17✔
562
        sink.write_4_bytes_little_endian(input->sequence());
10✔
563

564
    sink.flush();
7✔
565
    return digest;
7✔
566
}
7✔
567

568
// Signing (unversioned).
569
// ----------------------------------------------------------------------------
570

571
// private
572
transaction::input_iterator transaction::input_at(
4✔
573
    uint32_t index) const NOEXCEPT
574
{
575
    // Guarded by check_signature and create_endorsement.
576
    BC_ASSERT_MSG(index < inputs_->size(), "invalid input index");
4✔
577

578
    return std::next(inputs_->begin(), index);
4✔
579
}
580

581
// private
582
uint32_t transaction::input_index(const input_iterator& input) const NOEXCEPT
25✔
583
{
584
    // Guarded by unversioned_signature_hash and output_hash.
585
    BC_ASSERT_MSG(inputs_->begin() != inputs_->end(), "invalid input iterator");
25✔
586

587
    return possible_narrow_and_sign_cast<uint32_t>(
25✔
588
        std::distance(inputs_->begin(), input));
×
589
}
590

591
//*****************************************************************************
592
// CONSENSUS: Due to masking of bits 6/7 (8 is the anyone_can_pay flag),
593
// there are 4 possible 7 bit values that can set "single" and 4 others that
594
// can set none, and yet all other values set "all".
595
//*****************************************************************************
596
inline coverage mask_sighash(uint8_t sighash_flags) NOEXCEPT
41✔
597
{
598
    switch (sighash_flags & coverage::mask)
41✔
599
    {
600
        case coverage::hash_single:
601
            return coverage::hash_single;
602
        case coverage::hash_none:
2✔
603
            return coverage::hash_none;
2✔
604
        default:
18✔
605
            return coverage::hash_all;
18✔
606
    }
607
}
608

609
void transaction::signature_hash_single(writer& sink,
4✔
610
    const input_iterator& input, const script& sub,
611
    uint8_t sighash_flags) const NOEXCEPT
612
{
613
    const auto write_inputs = [this, &input, &sub, sighash_flags](
8✔
614
        writer& sink) NOEXCEPT
4✔
615
    {
616
        const auto anyone = to_bool(sighash_flags & coverage::anyone_can_pay);
4✔
617
        input_cptrs::const_iterator in;
4✔
618

619
        sink.write_variable(anyone ? one : inputs_->size());
5✔
620

621
        for (in = inputs_->begin(); !anyone && in != input; ++in)
4✔
622
        {
623
            (*in)->point().to_data(sink);
×
624
            sink.write_bytes(empty_script());
×
625
            sink.write_bytes(zero_sequence());
×
626
        }
627

628
        (*input)->point().to_data(sink);
4✔
629
        sub.to_data(sink, prefixed);
4✔
630
        sink.write_4_bytes_little_endian((*input)->sequence());
4✔
631

632
        for (++in; !anyone && in != inputs_->end(); ++in)
5✔
633
        {
634
            (*in)->point().to_data(sink);
1✔
635
            sink.write_bytes(empty_script());
1✔
636
            sink.write_bytes(zero_sequence());
1✔
637
        }
638
    };
4✔
639

640
    const auto write_outputs = [this, &input](writer& sink) NOEXCEPT
12✔
641
    {
642
        const auto index = input_index(input);
4✔
643

644
        sink.write_variable(add1(index));
4✔
645

646
        for (size_t output = 0; output < index; ++output)
5✔
647
            sink.write_bytes(null_output());
1✔
648

649
        // Guarded by unversioned_signature_hash.
650
        outputs_->at(index)->to_data(sink);
4✔
651
    };
8✔
652

653
    sink.write_4_bytes_little_endian(version_);
4✔
654
    write_inputs(sink);
4✔
655
    write_outputs(sink);
4✔
656
    sink.write_4_bytes_little_endian(locktime_);
4✔
657
    sink.write_4_bytes_little_endian(sighash_flags);
4✔
658
}
4✔
659

660
void transaction::signature_hash_none(writer& sink,
×
661
    const input_iterator& input, const script& sub,
662
    uint8_t sighash_flags) const NOEXCEPT
663
{
664
    const auto write_inputs = [this, &input, &sub, sighash_flags](
×
665
        writer& sink) NOEXCEPT
×
666
    {
667
        const auto anyone = to_bool(sighash_flags & coverage::anyone_can_pay);
×
668
        input_cptrs::const_iterator in;
×
669

670
        sink.write_variable(anyone ? one : inputs_->size());
×
671

672
        for (in = inputs_->begin(); !anyone && in != input; ++in)
×
673
        {
674
            (*in)->point().to_data(sink);
×
675
            sink.write_bytes(empty_script());
×
676
            sink.write_bytes(zero_sequence());
×
677
        }
678

NEW
679
        (*input)->point().to_data(sink);
×
680
        sub.to_data(sink, prefixed);
×
NEW
681
        sink.write_4_bytes_little_endian((*input)->sequence());
×
682

683
        for (++in; !anyone && in != inputs_->end(); ++in)
×
684
        {
685
            (*in)->point().to_data(sink);
×
686
            sink.write_bytes(empty_script());
×
687
            sink.write_bytes(zero_sequence());
×
688
        }
689
    };
×
690

691
    sink.write_4_bytes_little_endian(version_);
×
692
    write_inputs(sink);
×
693
    sink.write_variable(zero);
×
694
    sink.write_4_bytes_little_endian(locktime_);
×
695
    sink.write_4_bytes_little_endian(sighash_flags);
×
696
}
×
697

698
void transaction::signature_hash_all(writer& sink,
12✔
699
    const input_iterator& input, const script& sub,
700
    uint8_t flags) const NOEXCEPT
701
{
702
    const auto write_inputs = [this, &input, &sub, flags](
24✔
703
        writer& sink) NOEXCEPT
43✔
704
    {
705
        const auto anyone = to_bool(flags & coverage::anyone_can_pay);
12✔
706
        input_cptrs::const_iterator in;
12✔
707

708
        sink.write_variable(anyone ? one : inputs_->size());
24✔
709

710
        for (in = inputs_->begin(); !anyone && in != input; ++in)
15✔
711
        {
712
            (*in)->point().to_data(sink);
3✔
713
            sink.write_bytes(empty_script());
3✔
714
            sink.write_4_bytes_little_endian((*in)->sequence());
3✔
715
        }
716

717
        (*input)->point().to_data(sink);
12✔
718
        sub.to_data(sink, prefixed);
12✔
719
        sink.write_4_bytes_little_endian((*input)->sequence());
12✔
720

721
        for (++in; !anyone && in != inputs_->end(); ++in)
16✔
722
        {
723
            (*in)->point().to_data(sink);
4✔
724
            sink.write_bytes(empty_script());
4✔
725
            sink.write_4_bytes_little_endian((*in)->sequence());
4✔
726
        }
727
    };
12✔
728

729
    const auto write_outputs = [this](writer& sink) NOEXCEPT
36✔
730
    {
731
        sink.write_variable(outputs_->size());
12✔
732
        for (const auto& output: *outputs_)
27✔
733
            output->to_data(sink);
15✔
734
    };
24✔
735

736
    sink.write_4_bytes_little_endian(version_);
12✔
737
    write_inputs(sink);
12✔
738
    write_outputs(sink);
12✔
739
    sink.write_4_bytes_little_endian(locktime_);
12✔
740
    sink.write_4_bytes_little_endian(flags);
12✔
741
}
12✔
742

743
// private
744
hash_digest transaction::unversioned_signature_hash(
19✔
745
    const input_iterator& input, const script& sub,
746
    uint8_t sighash_flags) const NOEXCEPT
747
{
748
    // Set options.
749
    const auto flag = mask_sighash(sighash_flags);
19✔
750

751
    // Create hash writer.
752
    BC_PUSH_WARNING(LOCAL_VARIABLE_NOT_INITIALIZED)
753
    hash_digest digest;
19✔
754
    BC_POP_WARNING()
755

756
    stream::out::fast stream{ digest };
19✔
757
    hash::sha256x2::fast sink{ stream };
19✔
758

759
    switch (flag)
19✔
760
    {
761
        case coverage::hash_single:
7✔
762
        {
7✔
763
            //*****************************************************************
764
            // CONSENSUS: return one_hash if index exceeds outputs in sighash.
765
            // Related Bug: bitcointalk.org/index.php?topic=260595
766
            // Exploit: joncave.co.uk/2014/08/bitcoin-sighash-single/
767
            //*****************************************************************
768
            if (input_index(input) >= outputs_->size())
7✔
769
                return one_hash;
3✔
770

771
            signature_hash_single(sink, input, sub, sighash_flags);
4✔
772
            break;
4✔
773
        }
774
        case coverage::hash_none:
×
775
        {
×
776
            signature_hash_none(sink, input, sub, sighash_flags);
×
777
            break;
×
778
        }
779
        default:
12✔
780
        case coverage::hash_all:
12✔
781
        {
12✔
782
            signature_hash_all(sink, input, sub, sighash_flags);
12✔
783
        }
784
    }
785

786
    sink.flush();
16✔
787
    return digest;
16✔
788
}
19✔
789

790
// Signing (version 0).
791
// ----------------------------------------------------------------------------
792

793
// private
794
// TODO: taproot requires both single and double hash of each.
795
void transaction::initialize_sighash_cache() const NOEXCEPT
2✔
796
{
797
    // C++23: std::optional<T>::or_else.
798
    if (!segregated_)
2✔
799
        return;
800

801
    // This overconstructs the cache (anyone or !all), however it is simple.
802
    sighash_cache_ =
2✔
803
    {
804
        outputs_hash(),
2✔
805
        points_hash(),
2✔
806
        sequences_hash()
2✔
807
    };
2✔
808
}
809

810
// private
811
hash_digest transaction::output_hash(const input_iterator& input) const NOEXCEPT
14✔
812
{
813
    const auto index = input_index(input);
14✔
814

815
    //*************************************************************************
816
    // CONSENSUS: if index exceeds outputs in signature hash, return null_hash.
817
    //*************************************************************************
818
    if (index >= outputs_->size())
14✔
819
        return null_hash;
2✔
820

821
    BC_PUSH_WARNING(LOCAL_VARIABLE_NOT_INITIALIZED)
822
    hash_digest digest;
12✔
823
    BC_POP_WARNING()
824

825
    stream::out::fast stream{ digest };
12✔
826
    hash::sha256x2::fast sink{ stream };
12✔
827
    outputs_->at(index)->to_data(sink);
12✔
828
    sink.flush();
12✔
829
    return digest;
12✔
830
}
12✔
831

832
// private
833
hash_digest transaction::version_0_signature_hash(const input_iterator& input,
29✔
834
    const script& sub, uint64_t value, uint8_t sighash_flags,
835
    bool bip143) const NOEXCEPT
836
{
837
    // bip143/v0: the way of serialization is changed.
838
    if (!bip143)
29✔
839
        return unversioned_signature_hash(input, sub, sighash_flags);
7✔
840

841
    // Set options.
842
    const auto anyone = to_bool(sighash_flags & coverage::anyone_can_pay);
22✔
843
    const auto flag = mask_sighash(sighash_flags);
22✔
844
    const auto all = (flag == coverage::hash_all);
22✔
845
    const auto single = (flag == coverage::hash_single);
22✔
846

847
    // Create hash writer.
848
    BC_PUSH_WARNING(LOCAL_VARIABLE_NOT_INITIALIZED)
849
    hash_digest digest;
22✔
850
    BC_POP_WARNING()
851

852
    stream::out::fast stream{ digest };
22✔
853
    hash::sha256x2::fast sink{ stream };
22✔
854

855
    // Create signature hash.
856
    sink.write_little_endian(version_);
22✔
857

858
    // Conditioning points, sequences, and outputs writes on cache_ instead of
859
    // conditionally passing them from methods avoids copying the cached hash.
860

861
    // points
862
    sink.write_bytes(!anyone ? points_hash() : null_hash);
22✔
863

864
    // sequences
865
    sink.write_bytes(!anyone && all ? sequences_hash() : null_hash);
22✔
866

867
    (*input)->point().to_data(sink);
22✔
868
    sub.to_data(sink, prefixed);
22✔
869
    sink.write_little_endian(value);
22✔
870
    sink.write_little_endian((*input)->sequence());
22✔
871

872
    // outputs
873
    if (single)
22✔
874
        sink.write_bytes(output_hash(input));
14✔
875
    else
876
        sink.write_bytes(all ? outputs_hash() : null_hash);
8✔
877

878
    sink.write_little_endian(locktime_);
22✔
879
    sink.write_4_bytes_little_endian(sighash_flags);
22✔
880

881
    sink.flush();
22✔
882
    return digest;
22✔
883
}
22✔
884

885
// Signing (unversioned and version 0).
886
// ----------------------------------------------------------------------------
887

888
// ****************************************************************************
889
// CONSENSUS: sighash flags are carried in a single byte but are encoded as 4
890
// bytes in the signature hash preimage serialization.
891
// ****************************************************************************
892

893
hash_digest transaction::signature_hash(const input_iterator& input,
41✔
894
    const script& sub, uint64_t value, uint8_t sighash_flags,
895
    script_version version, bool bip143) const NOEXCEPT
896
{
897
    // There is no rational interpretation of a signature hash for a coinbase.
898
    BC_ASSERT(!is_coinbase());
41✔
899

900
    switch (version)
41✔
901
    {
902
        case script_version::unversioned:
12✔
903
            return unversioned_signature_hash(input, sub, sighash_flags);
12✔
904
        case script_version::zero:
29✔
905
            return version_0_signature_hash(input, sub, value, sighash_flags,
29✔
906
                bip143);
29✔
907
        case script_version::reserved:
×
908
        default:
×
909
            return {};
×
910
    }
911
}
912

913
// This is not used internal to the library.
914
bool transaction::check_signature(const ec_signature& signature,
2✔
915
    const data_slice& public_key, const script& sub, uint32_t index,
916
    uint64_t value, uint8_t sighash_flags, script_version version,
917
    bool bip143) const NOEXCEPT
918
{
919
    if ((index >= inputs_->size()) || signature.empty() || public_key.empty())
2✔
920
        return false;
921

922
    const auto sighash = signature_hash(input_at(index), sub, value,
2✔
923
        sighash_flags, version, bip143);
924

925
    // Validate the EC signature.
926
    return verify_signature(public_key, sighash, signature);
2✔
927
}
928

929
// This is not used internal to the library.
930
bool transaction::create_endorsement(endorsement& out, const ec_secret& secret,
2✔
931
    const script& sub, uint32_t index, uint64_t value, uint8_t sighash_flags,
932
    script_version version, bool bip143) const NOEXCEPT
933
{
934
    if (index >= inputs_->size())
2✔
935
        return false;
936

937
    out.reserve(max_endorsement_size);
2✔
938
    const auto sighash = signature_hash(input_at(index), sub, value,
2✔
939
        sighash_flags, version, bip143);
940

941
    // Create the EC signature and encode as DER.
942
    ec_signature signature;
2✔
943
    if (!sign(signature, secret, sighash) || !encode_signature(out, signature))
2✔
944
        return false;
×
945

946
    // Add the sighash type to the end of the DER signature -> endorsement.
947
    out.push_back(sighash_flags);
2✔
948
    ////out.shrink_to_fit();
949
    return true;
2✔
950
}
951

952
// Guard (context free).
953
// ----------------------------------------------------------------------------
954

955
bool transaction::is_coinbase() const NOEXCEPT
80✔
956
{
957
    return is_one(inputs_->size()) && inputs_->front()->point().is_null();
80✔
958
}
959

960
bool transaction::is_internal_double_spend() const NOEXCEPT
4✔
961
{
962
    // TODO: optimize (see block.is_internal_double_spend).
963
    return !is_distinct(points());
4✔
964
}
965

966
// TODO: a pool (non-coinbase) tx must fit into a block (with a coinbase).
967
bool transaction::is_oversized() const NOEXCEPT
×
968
{
969
    return serialized_size(false) > max_block_size;
×
970
}
971

972
// Guard (contextual).
973
// ----------------------------------------------------------------------------
974

975
// static/private
976
bool transaction::segregated(const chain::inputs& inputs) NOEXCEPT
1✔
977
{
978
    const auto witnessed = [](const auto& input) NOEXCEPT
2✔
979
    {
980
        return !input.witness().stack().empty();
1✔
981
    };
982

983
    return std::any_of(inputs.begin(), inputs.end(), witnessed);
1✔
984
}
985

986
// static/private
987
bool transaction::segregated(const chain::input_cptrs& inputs) NOEXCEPT
905✔
988
{
989
    const auto witnessed = [](const auto& input) NOEXCEPT
908✔
990
    {
991
        return !input->witness().stack().empty();
908✔
992
    };
993

994
    return std::any_of(inputs.begin(), inputs.end(), witnessed);
905✔
995
}
996

997
bool transaction::is_segregated() const NOEXCEPT
4✔
998
{
999
    return segregated_;
4✔
1000
}
1001

1002
size_t transaction::weight() const NOEXCEPT
×
1003
{
1004
    // Block weight is 3 * base size * + 1 * total size (bip141).
1005
    return ceilinged_add(
×
1006
        ceilinged_multiply(base_size_contribution, serialized_size(false)),
1007
        ceilinged_multiply(total_size_contribution, serialized_size(true)));
×
1008
}
1009

1010
bool transaction::is_overweight() const NOEXCEPT
×
1011
{
1012
    return weight() > max_block_weight;
×
1013
}
1014

1015
//*****************************************************************************
1016
// CONSENSUS: Legacy sigops are counted in coinbase scripts despite the fact
1017
// that coinbase input scripts are never executed. There is no need to exclude
1018
// p2sh coinbase sigops since there is never a script to count.
1019
//*****************************************************************************
1020
bool transaction::is_signature_operations_limit(bool bip16,
×
1021
    bool bip141) const NOEXCEPT
1022
{
1023
    const auto limit = bip141 ? max_fast_sigops : max_block_sigops;
×
1024
    return signature_operations(bip16, bip141) > limit;
×
1025
}
1026

1027
// Check (context free).
1028
// ----------------------------------------------------------------------------
1029

1030
bool transaction::is_empty() const NOEXCEPT
9✔
1031
{
1032
    return inputs_->empty() || outputs_->empty();
9✔
1033
}
1034

1035
bool transaction::is_null_non_coinbase() const NOEXCEPT
7✔
1036
{
1037
    BC_ASSERT(!is_coinbase());
7✔
1038

1039
    const auto invalid = [](const auto& input) NOEXCEPT
9✔
1040
    {
1041
        return input->point().is_null();
9✔
1042
    };
1043

1044
    // True if not coinbase but has null previous_output(s).
1045
    return std::any_of(inputs_->begin(), inputs_->end(), invalid);
7✔
1046
}
1047

1048
bool transaction::is_invalid_coinbase_size() const NOEXCEPT
9✔
1049
{
1050
    BC_ASSERT(is_coinbase());
9✔
1051

1052
    // True if coinbase and has invalid input[0] script size.
1053
    const auto script_size = inputs_->front()->script().serialized_size(false);
9✔
1054
    return script_size < min_coinbase_size || script_size > max_coinbase_size;
9✔
1055
}
1056

1057
// Accept (contextual).
1058
// ----------------------------------------------------------------------------
1059

1060
bool transaction::is_non_final(size_t height, uint32_t timestamp,
5✔
1061
    uint32_t median_time_past, bool bip113) const NOEXCEPT
1062
{
1063
    // BIP113: comparing the locktime against the median of the past 11 block
1064
    // timestamps, rather than the timestamp of the block including the tx.
1065
    const auto time = bip113 ? median_time_past : timestamp;
5✔
1066

1067
    const auto finalized = [](const auto& input) NOEXCEPT
2✔
1068
    {
1069
        return input->is_final();
2✔
1070
    };
1071

1072
    const auto height_time = locktime_ < locktime_threshold ? height : time;
5✔
1073

1074
    return !(is_zero(locktime_) || locktime_ < height_time ||
8✔
1075
        std::all_of(inputs_->begin(), inputs_->end(), finalized));
3✔
1076
}
1077

1078
bool transaction::is_missing_prevouts() const NOEXCEPT
3✔
1079
{
1080
    BC_ASSERT(!is_coinbase());
3✔
1081

1082
    // Null or invalid prevout indicates not found.
1083
    const auto missing = [](const auto& input) NOEXCEPT
2✔
1084
    {
1085
        return !input->prevout;
1086
    };
1087

1088
    return std::any_of(inputs_->begin(), inputs_->end(), missing);
3✔
1089
}
1090

1091
uint64_t transaction::claim() const NOEXCEPT
8✔
1092
{
1093
    // Overflow returns max_uint64.
1094
    const auto sum = [](uint64_t total, const auto& output) NOEXCEPT
8✔
1095
    {
1096
        return ceilinged_add(total, output->value());
8✔
1097
    };
1098

1099
    // The amount claimed by outputs.
1100
    return std::accumulate(outputs_->begin(), outputs_->end(), 0_u64, sum);
8✔
1101
}
1102

1103
uint64_t transaction::value() const NOEXCEPT
9✔
1104
{
1105
    // Overflow, not populated, and coinbase (default) return max_uint64.
1106
    const auto sum = [](uint64_t total, const auto& input) NOEXCEPT
7✔
1107
    {
1108
        const auto value = input->prevout ? input->prevout->value() : max_uint64;
7✔
1109
        return ceilinged_add(total, value);
7✔
1110
    };
1111

1112
    // The amount of prevouts (referenced by inputs).
1113
    return std::accumulate(inputs_->begin(), inputs_->end(), 0_u64, sum);
9✔
1114
}
1115

1116
bool transaction::is_overspent() const NOEXCEPT
2✔
1117
{
1118
    BC_ASSERT(!is_coinbase());
2✔
1119

1120
    return claim() > value();
2✔
1121
}
1122

1123
constexpr bool is_non_coinbase_mature(size_t tx_height, size_t height) NOEXCEPT
2✔
1124
{
1125
    return tx_height <= height;
2✔
1126
}
1127

1128
//*****************************************************************************
1129
// CONSENSUS: Coinbase output matures at 100 blocks depth.
1130
// CONSENSUS: Genesis coinbase is forever immature (exception).
1131
//*****************************************************************************
1132
bool transaction::is_coinbase_mature(size_t coinbase_height,
3✔
1133
    size_t height) NOEXCEPT
1134
{
1135
    return !is_zero(coinbase_height) &&
5✔
1136
        ceilinged_add(coinbase_height, coinbase_maturity) <= height;
3✔
1137
}
1138

1139
bool transaction::is_immature(size_t height) const NOEXCEPT
6✔
1140
{
1141
    BC_ASSERT(!is_coinbase());
6✔
1142

1143
    // Spends internal to a block are handled by block validation.
1144
    const auto mature = [=](const auto& input) NOEXCEPT
5✔
1145
    {
1146
        return input->metadata.coinbase ?
5✔
1147
            is_coinbase_mature(input->metadata.height, height) :
3✔
1148
            is_non_coinbase_mature(input->metadata.height, height);
2✔
1149
    };
6✔
1150

1151
    return !std::all_of(inputs_->begin(), inputs_->end(), mature);
6✔
1152
}
1153

1154
bool transaction::is_locked(size_t height,
4✔
1155
    uint32_t median_time_past) const NOEXCEPT
1156
{
1157
    // BIP68: not applied to the sequence of the input of a coinbase.
1158
    BC_ASSERT(!is_coinbase());
4✔
1159

1160
    // BIP68: applied to txs with a version greater than or equal to two.
1161
    if (version_ < relative_locktime_min_version)
4✔
1162
        return false;
1163

1164
    // BIP68: references to median time past are as defined by bip113.
1165
    const auto locked = [=](const auto& input) NOEXCEPT
2✔
1166
    {
1167
        return input->is_locked(height, median_time_past);
2✔
1168
    };
2✔
1169

1170
    // BIP68: when the relative lock time is block based, it is interpreted as
1171
    // a minimum block height constraint over the age of the input.
1172
    return std::any_of(inputs_->begin(), inputs_->end(), locked);
2✔
1173
}
1174

1175
// Spends internal to a block are handled by block validation.
1176
bool transaction::is_unconfirmed_spend(size_t height) const NOEXCEPT
×
1177
{
1178
    BC_ASSERT(!is_coinbase());
×
1179

1180
    // Zero is either genesis or not found.
1181
    // Test maturity first to obtain proper error code.
1182
    // Spends internal to a block are handled by block validation.
1183
    const auto unconfirmed = [=](const auto& input) NOEXCEPT
×
1184
    {
1185
        const auto prevout_height = input->metadata.height;
×
1186
        return is_zero(prevout_height) && !(height > prevout_height);
×
1187
    };
×
1188

1189
    return std::any_of(inputs_->begin(), inputs_->end(), unconfirmed);
×
1190
}
1191

1192
bool transaction::is_confirmed_double_spend(size_t height) const NOEXCEPT
4✔
1193
{
1194
    BC_ASSERT(!is_coinbase());
4✔
1195

1196
    // Spends internal to a block are handled by block validation.
1197
    const auto spent = [=](const auto& input) NOEXCEPT
4✔
1198
    {
1199
        return input->metadata.spent && height > input->metadata.height;
4✔
1200
    };
4✔
1201

1202
    return std::any_of(inputs_->begin(), inputs_->end(), spent);
4✔
1203
}
1204

1205
// Guards (for tx pool without compact blocks).
1206
// ----------------------------------------------------------------------------
1207

1208
// Pools do not have coinbases.
1209
// Redundant with block is_internal_double_spend check.
1210
// Redundant with block max_block_size check.
1211
code transaction::guard_check() const NOEXCEPT
×
1212
{
1213
    if (is_coinbase())
×
1214
        return error::coinbase_transaction;
×
1215
    if (is_internal_double_spend())
×
1216
        return error::transaction_internal_double_spend;
×
1217
    if (is_oversized())
×
1218
        return error::transaction_size_limit;
×
1219

1220
    return error::transaction_success;
×
1221
}
1222

1223
// Redundant with block max_block_weight accept.
1224
code transaction::guard_check(const context& ctx) const NOEXCEPT
×
1225
{
1226
    const auto bip141 = ctx.is_enabled(flags::bip141_rule);
×
1227

1228
     if (!bip141 && is_segregated())
×
1229
        return error::unexpected_witness_transaction;
×
1230
    if (bip141 && is_overweight())
×
1231
        return error::transaction_weight_limit;
×
1232

1233
    return error::transaction_success;
×
1234
}
1235

1236
// Redundant with block max_block_sigops accept.
1237
code transaction::guard_accept(const context& ctx) const NOEXCEPT
×
1238
{
1239
    const auto bip16 = ctx.is_enabled(flags::bip16_rule);
×
1240
    const auto bip141 = ctx.is_enabled(flags::bip141_rule);
×
1241

1242
    if (is_missing_prevouts())
×
1243
        return error::missing_previous_output;
×
1244
    if (is_signature_operations_limit(bip16, bip141))
×
1245
        return error::transaction_sigop_limit;
×
1246

1247
    return error::transaction_success;
×
1248
}
1249

1250
// Validation.
1251
// ----------------------------------------------------------------------------
1252

1253
// DO invoke on coinbase.
1254
code transaction::check() const NOEXCEPT
5✔
1255
{
1256
    const auto coinbase = is_coinbase();
5✔
1257

1258
    if (is_empty())
5✔
1259
        return error::empty_transaction;
×
1260
    if (coinbase && is_invalid_coinbase_size())
5✔
1261
        return error::invalid_coinbase_script_size;
×
1262
    if (!coinbase && is_null_non_coinbase())
5✔
1263
        return error::previous_output_null;
×
1264

1265
    return error::transaction_success;
5✔
1266
}
1267

1268
// forks
1269
// height
1270
// timestamp
1271
// median_time_past
1272

1273
// DO invoke on coinbase.
1274
code transaction::check(const context& ctx) const NOEXCEPT
×
1275
{
1276
    const auto bip113 = ctx.is_enabled(bip113_rule);
×
1277

1278
    if (is_non_final(ctx.height, ctx.timestamp, ctx.median_time_past, bip113))
×
1279
        return error::transaction_non_final;
×
1280

1281
    return error::transaction_success;
×
1282
}
1283

1284
// Do not need to invoke on coinbase.
1285
// This assumes that prevout caching is completed on all inputs.
1286
code transaction::accept(const context&) const NOEXCEPT
×
1287
{
1288
    ////BC_ASSERT(!is_coinbase());
1289

1290
    if (is_coinbase())
×
1291
        return error::transaction_success;
×
1292
    if (is_missing_prevouts())
×
1293
        return error::missing_previous_output;
×
1294
    if (is_overspent())
×
1295
        return error::spend_exceeds_value;
×
1296

1297
    return error::transaction_success;
×
1298
}
1299

1300
// forks
1301
// height
1302
// median_time_past
1303

1304
// Do not need to invoke on coinbase.
1305
// Node performs these checks through database query.
1306
// This assumes that prevout and metadata caching are completed on all inputs.
1307
code transaction::confirm(const context& ctx) const NOEXCEPT
×
1308
{
1309
    ////BC_ASSERT(!is_coinbase());
1310
    const auto bip68 = ctx.is_enabled(bip68_rule);
×
1311

1312
    if (is_coinbase())
×
1313
        return error::transaction_success;
×
1314
    if (bip68 && is_locked(ctx.height, ctx.median_time_past))
×
1315
        return error::relative_time_locked;
×
1316
    if (is_immature(ctx.height))
×
1317
        return error::coinbase_maturity;
×
1318
    if (is_unconfirmed_spend(ctx.height))
×
1319
        return error::unconfirmed_spend;
×
1320
    if (is_confirmed_double_spend(ctx.height))
×
1321
        return error::confirmed_double_spend;
×
1322

1323
    return error::transaction_success;
×
1324
}
1325

1326
// Connect (contextual).
1327
// ----------------------------------------------------------------------------
1328

1329
// forks
1330

1331
// Do not need to invoke on coinbase.
1332
code transaction::connect(const context& ctx) const NOEXCEPT
2✔
1333
{
1334
    ////BC_ASSERT(!is_coinbase());
1335

1336
    if (is_coinbase())
2✔
1337
        return error::transaction_success;
×
1338

1339
    code ec{};
2✔
1340
    using namespace machine;
2✔
1341
    initialize_sighash_cache();
2✔
1342

1343
    // Validate scripts.
1344
    for (auto input = inputs_->begin(); input != inputs_->end(); ++input)
6✔
1345
    {
1346
        // Evaluate rolling scripts with linear search but constant erase.
1347
        // Evaluate non-rolling scripts with constant search but linear erase.
1348
        if ((ec = (*input)->is_roller() ?
8✔
1349
            interpreter<linked_stack>::connect(ctx, *this, input) :
×
1350
            interpreter<contiguous_stack>::connect(ctx, *this, input)))
4✔
1351
            return ec;
×
1352
    }
1353

1354
    // TODO: accumulate sigops from each connect result and add coinbase.
1355
    // TODO: return in override with out parameter. more impactful with segwit.
1356
    return error::transaction_success;
2✔
1357
}
1358

1359
BC_POP_WARNING()
1360

1361
// JSON value convertors.
1362
// ----------------------------------------------------------------------------
1363

1364
namespace json = boost::json;
1365

1366
// boost/json will soon have NOEXCEPT: github.com/boostorg/json/pull/636
1367
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
1368

1369
transaction tag_invoke(json::value_to_tag<transaction>,
2✔
1370
    const json::value& value) NOEXCEPT
1371
{
1372
    return
2✔
1373
    {
1374
        value.at("version").to_number<uint32_t>(),
2✔
1375
        json::value_to<chain::inputs>(value.at("inputs")),
2✔
1376
        json::value_to<chain::outputs>(value.at("outputs")),
4✔
1377
        value.at("locktime").to_number<uint32_t>()
4✔
1378
    };
4✔
1379
}
1380

1381
void tag_invoke(json::value_from_tag, json::value& value,
4✔
1382
    const transaction& tx) NOEXCEPT
1383
{
1384
    value =
4✔
1385
    {
1386
        { "version", tx.version() },
1387
        { "inputs", *tx.inputs_ptr() },
1388
        { "outputs", *tx.outputs_ptr() },
1389
        { "locktime", tx.locktime() }
1390
    };
4✔
1391
}
4✔
1392

1393
BC_POP_WARNING()
1394

1395
transaction::cptr tag_invoke(json::value_to_tag<transaction::cptr>,
×
1396
    const json::value& value) NOEXCEPT
1397
{
1398
    return to_shared(tag_invoke(json::value_to_tag<transaction>{}, value));
×
1399
}
1400

1401
// Shared pointer overload is required for navigation.
1402
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
1403
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
1404

1405
void tag_invoke(json::value_from_tag tag, json::value& value,
2✔
1406
    const transaction::cptr& tx) NOEXCEPT
1407
{
1408
    tag_invoke(tag, value, *tx);
2✔
1409
}
2✔
1410

1411
BC_POP_WARNING()
1412
BC_POP_WARNING()
1413

1414
} // namespace chain
1415
} // namespace system
1416
} // namespace libbitcoin
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc