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

libbitcoin / libbitcoin-system / 9372336698

04 Jun 2024 06:30PM UTC coverage: 82.786% (+0.04%) from 82.751%
9372336698

push

github

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

Rename error::merkle_mismatch to invalid_transaction_commitment.

0 of 9 new or added lines in 1 file covered. (0.0%)

3 existing lines in 1 file now uncovered.

9854 of 11903 relevant lines covered (82.79%)

4791270.77 hits per line

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

47.32
/src/chain/block.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/block.hpp>
20

21
#include <algorithm>
22
#include <functional>
23
#include <iterator>
24
#include <memory>
25
#include <numeric>
26
#include <set>
27
#include <type_traits>
28
#include <unordered_map>
29
#include <utility>
30
#include <bitcoin/system/chain/context.hpp>
31
#include <bitcoin/system/chain/enums/flags.hpp>
32
#include <bitcoin/system/chain/enums/magic_numbers.hpp>
33
#include <bitcoin/system/chain/enums/opcode.hpp>
34
#include <bitcoin/system/chain/point.hpp>
35
#include <bitcoin/system/chain/script.hpp>
36
#include <bitcoin/system/data/data.hpp>
37
#include <bitcoin/system/define.hpp>
38
#include <bitcoin/system/error/error.hpp>
39
#include <bitcoin/system/hash/hash.hpp>
40
#include <bitcoin/system/math/math.hpp>
41
#include <bitcoin/system/stream/stream.hpp>
42

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

47
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
48

49
// Constructors.
50
// ----------------------------------------------------------------------------
51

52
block::block() NOEXCEPT
63✔
53
  : block(to_shared<chain::header>(), to_shared<chain::transaction_cptrs>(),
63✔
54
      false)
189✔
55
{
56
}
63✔
57

58
block::block(chain::header&& header, chain::transactions&& txs) NOEXCEPT
11✔
59
  : block(to_shared(std::move(header)), to_shareds(std::move(txs)), true)
33✔
60
{
61
}
11✔
62

63
block::block(const chain::header& header,
30✔
64
    const chain::transactions& txs) NOEXCEPT
30✔
65
  : block(to_shared<chain::header>(header), to_shareds(txs), true)
90✔
66
{
67
}
30✔
68

69
block::block(const chain::header::cptr& header,
×
70
    const chain::transactions_cptr& txs) NOEXCEPT
×
71
  : block(header ? header : to_shared<chain::header>(),
×
72
      txs ? txs : to_shared<transaction_cptrs>(), true)
×
73
{
74
}
×
75

76
block::block(const data_slice& data, bool witness) NOEXCEPT
71✔
77
  : block(stream::in::copy(data), witness)
71✔
78
{
79
}
71✔
80

81
////block::block(stream::in::fast&& stream, bool witness) NOEXCEPT
82
////  : block(read::bytes::fast(stream), witness)
83
////{
84
////}
85

86
block::block(stream::in::fast& stream, bool witness) NOEXCEPT
1✔
87
  : block(read::bytes::fast(stream), witness)
1✔
88
{
89
}
1✔
90

91
block::block(std::istream&& stream, bool witness) NOEXCEPT
71✔
92
  : block(read::bytes::istream(stream), witness)
71✔
93
{
94
}
71✔
95

96
block::block(std::istream& stream, bool witness) NOEXCEPT
3✔
97
  : block(read::bytes::istream(stream), witness)
3✔
98
{
99
}
3✔
100

101
block::block(reader&& source, bool witness) NOEXCEPT
75✔
102
  : block(from_data(source, witness))
75✔
103
{
104
}
75✔
105

106
block::block(reader& source, bool witness) NOEXCEPT
1✔
107
  : block(from_data(source, witness))
1✔
108
{
109
}
1✔
110

111
// protected
112
block::block(const chain::header::cptr& header,
180✔
113
    const chain::transactions_cptr& txs, bool valid) NOEXCEPT
180✔
114
  : header_(header),
115
    txs_(txs),
116
    valid_(valid),
180✔
117
    size_(serialized_size(*txs))
360✔
118
{
119
}
180✔
120

121
// Operators.
122
// ----------------------------------------------------------------------------
123

124
bool block::operator==(const block& other) const NOEXCEPT
28✔
125
{
126
    return (header_ == other.header_ || *header_ == *other.header_)
28✔
127
        && deep_equal(*txs_, *other.txs_);
35✔
128
}
129

130
bool block::operator!=(const block& other) const NOEXCEPT
5✔
131
{
132
    return !(*this == other);
5✔
133
}
134

135
// Deserialization.
136
// ----------------------------------------------------------------------------
137

138
// static/private
139
block block::from_data(reader& source, bool witness) NOEXCEPT
76✔
140
{
141
    const auto read_transactions = [witness](reader& source) NOEXCEPT
152✔
142
    {
143
        auto txs = to_shared<transaction_cptrs>();
76✔
144
        const auto capacity = source.read_size(max_block_size);
76✔
145
        txs->reserve(capacity);
76✔
146

147
        for (size_t tx = 0; tx < capacity; ++tx)
197✔
148
            txs->push_back(to_shared<transaction>(source, witness));
242✔
149

150
        // This is a pointer copy (non-const to const).
151
        return txs;
76✔
152
    };
76✔
153

154
    return
76✔
155
    {
156
        to_shared<chain::header>(source),
76✔
157
        read_transactions(source),
152✔
158
        source
159
    };
152✔
160
}
161

162
// Serialization.
163
// ----------------------------------------------------------------------------
164

165
data_chunk block::to_data(bool witness) const NOEXCEPT
18✔
166
{
167
    data_chunk data(serialized_size(witness));
36✔
168
    stream::out::copy ostream(data);
18✔
169
    to_data(ostream, witness);
18✔
170
    return data;
36✔
171
}
18✔
172

173
void block::to_data(std::ostream& stream, bool witness) const NOEXCEPT
19✔
174
{
175
    write::bytes::ostream out(stream);
19✔
176
    to_data(out, witness);
19✔
177
}
19✔
178

179
void block::to_data(writer& sink, bool witness) const NOEXCEPT
20✔
180
{
181
    header_->to_data(sink);
20✔
182
    sink.write_variable(txs_->size());
20✔
183

184
    for (const auto& tx: *txs_)
46✔
185
        tx->to_data(sink, witness);
26✔
186
}
20✔
187

188
// Properties.
189
// ----------------------------------------------------------------------------
190

191
bool block::is_valid() const NOEXCEPT
32✔
192
{
193
    return valid_;
32✔
194
}
195

196
size_t block::transactions() const NOEXCEPT
×
197
{
198
    return txs_->size();
×
199
}
200

201
const chain::header& block::header() const NOEXCEPT
12✔
202
{
203
    return *header_;
2✔
204
}
205

206
const chain::header::cptr block::header_ptr() const NOEXCEPT
×
207
{
208
    return header_;
×
209
}
210

211
// Roll up inputs for concurrent prevout processing.
212
const inputs_cptr block::inputs_ptr() const NOEXCEPT
×
213
{
214
    const auto inputs = std::make_shared<input_cptrs>();
×
215
    const auto append_ins = [&inputs](const auto& tx) NOEXCEPT
×
216
    {
217
        const auto& tx_ins = *tx->inputs_ptr();
×
218
        inputs->insert(inputs->end(), tx_ins.begin(), tx_ins.end());
×
219
    };
×
220

221
    std::for_each(txs_->begin(), txs_->end(), append_ins);
×
222
    return inputs;
×
223
}
224

225
// vector<transaction> is not exposed (because we don't have it).
226
// This would require a from_shared(txs_) conversion (expensive).
227
const transactions_cptr& block::transactions_ptr() const NOEXCEPT
85✔
228
{
229
    return txs_;
85✔
230
}
231

232
hashes block::transaction_hashes(bool witness) const NOEXCEPT
11✔
233
{
234
    const auto count = txs_->size();
11✔
235
    const auto size = is_odd(count) && count > one ? add1(count) : count;
11✔
236
    hashes out(size);
11✔
237

238
    // Extra allocation for odd count optimizes for merkle root.
239
    // Vector capacity is never reduced when resizing to smaller size.
240
    out.resize(count);
11✔
241

242
    const auto hash = [witness](const auto& tx) NOEXCEPT
14✔
243
    {
244
        return tx->hash(witness);
14✔
245
    };
11✔
246

247
    std::transform(txs_->begin(), txs_->end(), out.begin(), hash);
11✔
248
    return out;
11✔
249
}
250

251
// computed
252
hash_digest block::hash() const NOEXCEPT
36✔
253
{
254
    return header_->hash();
36✔
255
}
256

257
// static/private
258
block::sizes block::serialized_size(
180✔
259
    const chain::transaction_cptrs& txs) NOEXCEPT
260
{
261
    sizes size{};
180✔
262
    std::for_each(txs.begin(), txs.end(), [&](const auto& tx) NOEXCEPT
429✔
263
    {
264
        size.nominal = ceilinged_add(size.nominal, tx->serialized_size(false));
498✔
265
        size.witnessed = ceilinged_add(size.witnessed, tx->serialized_size(true));
249✔
266
    });
249✔
267

268
    const auto common_size = ceilinged_add(
180✔
269
        header::serialized_size(),
270
        variable_size(txs.size()));
271

272
    const auto nominal_size = ceilinged_add(
180✔
273
        common_size,
274
        size.nominal);
275

276
    const auto witnessed_size = ceilinged_add(
180✔
277
        common_size,
278
        size.witnessed);
279

280
    return { nominal_size, witnessed_size };
180✔
281
}
282

283
size_t block::serialized_size(bool witness) const NOEXCEPT
19✔
284
{
285
    return witness ? size_.witnessed : size_.nominal;
19✔
286
}
287

288
// Connect.
289
// ----------------------------------------------------------------------------
290

291
bool block::is_empty() const NOEXCEPT
2✔
292
{
293
    return txs_->empty();
2✔
294
}
295

296
bool block::is_oversized() const NOEXCEPT
×
297
{
298
    return serialized_size(false) > max_block_size;
×
299
}
300

301
bool block::is_first_non_coinbase() const NOEXCEPT
×
302
{
303
    return !txs_->empty() && !txs_->front()->is_coinbase();
×
304
}
305

306
// True if there is another coinbase other than the first tx.
307
// No txs or coinbases returns false.
308
bool block::is_extra_coinbases() const NOEXCEPT
×
309
{
310
    if (txs_->empty())
×
311
        return false;
312

313
    const auto value = [](const auto& tx) NOEXCEPT
×
314
    {
315
        return tx->is_coinbase();
×
316
    };
317

318
    return std::any_of(std::next(txs_->begin()), txs_->end(), value);
×
319
}
320

321
//*****************************************************************************
322
// CONSENSUS: This is only necessary because satoshi stores and queries as it
323
// validates, imposing an otherwise unnecessary partial transaction ordering.
324
//*****************************************************************************
325
bool block::is_forward_reference() const NOEXCEPT
5✔
326
{
327
    if (txs_->empty())
5✔
328
        return false;
329

330
    const auto sum_txs = sub1(txs_->size());
4✔
331
    unordered_set_of_constant_referenced_hashes hashes{ sum_txs };
4✔
332
    const auto spent = [&hashes](const input::cptr& input) NOEXCEPT
6✔
333
    {
334
        return hashes.find(std::ref(input->point().hash())) != hashes.end();
4✔
335
    };
4✔
336

337
    const auto spend = [&spent, &hashes](const auto& tx) NOEXCEPT
5✔
338
    {
339
        const auto& ins = *tx->inputs_ptr();
5✔
340
        const auto forward = std::any_of(ins.begin(), ins.end(), spent);
5✔
341
        hashes.emplace(tx->get_hash(false));
5✔
342
        return forward;
5✔
343
    };
4✔
344

345
    return std::any_of(txs_->rbegin(), std::prev(txs_->rend()), spend);
4✔
346
}
347

348
// This also precludes the block merkle calculation DoS exploit by preventing
349
// duplicate txs, as a duplicate non-empty tx implies a duplicate point.
350
// bitcointalk.org/?topic=102395
351
bool block::is_internal_double_spend() const NOEXCEPT
3✔
352
{
353
    if (txs_->empty())
3✔
354
        return false;
355

356
    // Overflow returns max_size_t.
357
    const auto sum_ins = [](size_t total, const auto& tx) NOEXCEPT
9✔
358
    {
359
        return ceilinged_add(total, tx->inputs());
9✔
360
    };
361

362
    const auto tx1 = std::next(txs_->begin());
2✔
363
    const auto spends_count = std::accumulate(tx1, txs_->end(), zero, sum_ins);
2✔
364
    unordered_set_of_constant_referenced_points points{ spends_count };
2✔
365
    const auto spent = [&points](const input::cptr& in) NOEXCEPT
9✔
366
    {
367
        return !points.emplace(in->point()).second;
7✔
368
    };
2✔
369

370
    const auto double_spent = [&spent](const auto& tx) NOEXCEPT
7✔
371
    {
372
        const auto& ins = *tx->inputs_ptr();
7✔
373
        return std::any_of(ins.begin(), ins.end(), spent);
7✔
374
    };
2✔
375

376
    return std::any_of(tx1, txs_->end(), double_spent);
2✔
377
}
378

379
// private
380
hash_digest block::generate_merkle_root(bool witness) const NOEXCEPT
11✔
381
{
382
    return sha256::merkle_root(transaction_hashes(witness));
11✔
383
}
384

385
bool block::is_invalid_merkle_root() const NOEXCEPT
11✔
386
{
387
    return generate_merkle_root(false) != header_->merkle_root();
11✔
388
}
389

390
// Accept (contextual).
391
// ----------------------------------------------------------------------------
392

393
size_t block::weight() const NOEXCEPT
×
394
{
395
    // Block weight is 3 * nominal size * + 1 * witness size (bip141).
396
    return ceilinged_add(
×
397
        ceilinged_multiply(base_size_contribution, serialized_size(false)),
398
        ceilinged_multiply(total_size_contribution, serialized_size(true)));
×
399
}
400

401
bool block::is_overweight() const NOEXCEPT
×
402
{
403
    return weight() > max_block_weight;
×
404
}
405

406
bool block::is_invalid_coinbase_script(size_t height) const NOEXCEPT
×
407
{
408
    if (txs_->empty() || txs_->front()->inputs_ptr()->empty())
×
409
        return false;
×
410

411
    const auto& script = txs_->front()->inputs_ptr()->front()->script();
×
412
    return !script::is_coinbase_pattern(script.ops(), height);
×
413
}
414

415
// TODO: add bip50 to chain_state with timestamp range activation.
416
// "Special short-term limits to avoid 10,000 BDB lock limit.
417
// Count of unique txids <= 4500 to prevent 10000 BDB lock exhaustion.
418
// header.timestamp > 1363039171 && header.timestamp < 1368576000."
419
bool block::is_hash_limit_exceeded() const NOEXCEPT
×
420
{
421
    if (txs_->empty())
×
422
        return false;
423

424
    // A set is used to collapse duplicates.
425
    unordered_set_of_constant_referenced_hashes hashes{};
×
426

427
    // Just the coinbase tx hash, skip its null input hashes.
428
    hashes.emplace(txs_->front()->get_hash(false));
×
429

430
    for (auto tx = std::next(txs_->begin()); tx != txs_->end(); ++tx)
×
431
    {
432
        // Insert the transaction hash.
433
        hashes.emplace((*tx)->get_hash(false));
×
434
        const auto& inputs = *(*tx)->inputs_ptr();
×
435

436
        // Insert all input point hashes.
437
        for (const auto& input: inputs)
×
438
            hashes.emplace(input->point().hash());
×
439
    }
440

441
    return hashes.size() > hash_limit;
×
442
}
443

444
bool block::is_malleable() const NOEXCEPT
3✔
445
{
446
    return is_malleable64() || is_malleable32();
3✔
447
}
448

449
bool block::is_malleable32() const NOEXCEPT
12✔
450
{
451
    const auto unmalleated = txs_->size();
12✔
452
    for (auto mally = one; mally <= unmalleated; mally *= two)
23✔
453
        if (is_malleable32(unmalleated, mally))
17✔
454
            return true;
455

456
    return false;
457
}
458

459
bool block::is_malleated32() const NOEXCEPT
11✔
460
{
461
    return !is_zero(malleated32_size());
11✔
462
}
463

464
// protected
465
// The size of an actual malleation of this block, or zero.
466
size_t block::malleated32_size() const NOEXCEPT
11✔
467
{
468
    const auto malleated = txs_->size();
11✔
469
    for (auto mally = one; mally <= to_half(malleated); mally *= two)
23✔
470
        if (is_malleable32(malleated - mally, mally) && is_malleated32(mally))
14✔
471
            return mally;
2✔
472

473
    return zero;
474
}
475

476
// protected
477
// True if the last width set of tx hashes repeats.
478
bool block::is_malleated32(size_t width) const NOEXCEPT
7✔
479
{
480
    const auto malleated = txs_->size();
7✔
481
    if (is_zero(width) || width > to_half(malleated))
7✔
482
        return false;
483

484
    auto mally = txs_->rbegin();
6✔
485
    auto legit = std::next(mally, width);
6✔
486
    while (!is_zero(width--))
9✔
487
        if ((*mally++)->hash(false) != (*legit++)->hash(false))
7✔
488
            return false;
489

490
    return true;
491
}
492

493
bool block::is_malleable64() const NOEXCEPT
12✔
494
{
495
    return is_malleable64(*txs_);
12✔
496
}
497

498
// static
499
// If all non-witness tx serializations are 64 bytes the id is malleable.
500
// This form of malleability does not imply current block instance is invalid.
501
bool block::is_malleable64(const transaction_cptrs& txs) NOEXCEPT
12✔
502
{
503
    const auto two_leaves = [](const auto& tx) NOEXCEPT
15✔
504
    {
505
        return tx->serialized_size(false) == two * hash_size;
15✔
506
    };
507

508
    return !txs.empty() && std::all_of(txs.begin(), txs.end(), two_leaves);
12✔
509
}
510

511
bool block::is_segregated() const NOEXCEPT
×
512
{
513
    const auto segregated = [](const auto& tx) NOEXCEPT
×
514
    {
515
        return tx->is_segregated();
×
516
    };
517

518
    return std::any_of(txs_->begin(), txs_->end(), segregated);
×
519
}
520

521
// The witness merkle root is obtained from wtxids, subject to malleation just
522
// as the txs commitment. However, since tx duplicates are precluded by the
523
// malleable32 (or complete) block check, there is no opportunity for this.
524
// Similarly the witness commitment cannot be malleable64.
525
bool block::is_invalid_witness_commitment() const NOEXCEPT
×
526
{
527
    if (txs_->empty())
×
528
        return false;
529

530
    const auto& coinbase = *txs_->front();
×
531
    if (coinbase.inputs_ptr()->empty())
×
532
        return false;
533

534
    // If there is a valid commitment, return false (valid).
535
    // Coinbase input witness must be 32 byte witness reserved value (bip141).
536
    // Last output of commitment pattern holds the committed value (bip141).
537
    hash_digest reserved{}, committed{};
×
538
    if (coinbase.inputs_ptr()->front()->reserved_hash(reserved))
×
539
        for (const auto& output: views_reverse(*coinbase.outputs_ptr()))
×
540
            if (output->committed_hash(committed))
×
541
                if (committed == sha256::double_hash(
×
542
                    generate_merkle_root(true), reserved))
×
543
                    return false;
544
    
545
    // If no valid commitment, return true (invalid) if segregated.
546
    // If no block tx has witness data the commitment is optional (bip141).
547
    return is_segregated();
×
548
}
549

550
//*****************************************************************************
551
// CONSENSUS:
552
// bip42 compensates for C++ undefined behavior of a right shift of a number of
553
// bits greater or equal to the shifted integer width. Yet being undefined, the
554
// result of this operation may vary by compiler. The shift_right call below
555
// explicitly implements presumed pre-bip42 behavior (shift overflow modulo) by
556
// default, and specified bip42 behavior (shift overflow to zero) with bip42.
557
//*****************************************************************************
558
static uint64_t block_subsidy(size_t height, uint64_t subsidy_interval,
×
559
    uint64_t initial_block_subsidy_satoshi, bool bip42) NOEXCEPT
560
{
561
    // Guard: quotient domain cannot increase with positive integer divisor.
562
    const auto halves = possible_narrow_cast<size_t>(height / subsidy_interval);
×
563
    return shift_right(initial_block_subsidy_satoshi, halves, bip42);
×
564
}
565

566
// Prevouts required.
567

568
uint64_t block::fees() const NOEXCEPT
×
569
{
570
    // Overflow returns max_uint64.
571
    const auto value = [](uint64_t total, const auto& tx) NOEXCEPT
×
572
    {
573
        return ceilinged_add(total, tx->fee());
×
574
    };
575

576
    return std::accumulate(txs_->begin(), txs_->end(), uint64_t{0}, value);
×
577
}
578

579
uint64_t block::claim() const NOEXCEPT
×
580
{
581
    return txs_->empty() ? zero : txs_->front()->value();
×
582
}
583

584
uint64_t block::reward(size_t height, uint64_t subsidy_interval,
×
585
    uint64_t initial_block_subsidy_satoshi, bool bip42) const NOEXCEPT
586
{
587
    // Overflow returns max_uint64.
588
    return ceilinged_add(fees(), block_subsidy(height, subsidy_interval,
×
589
        initial_block_subsidy_satoshi, bip42));
×
590
}
591

592
bool block::is_overspent(size_t height, uint64_t subsidy_interval,
×
593
    uint64_t initial_block_subsidy_satoshi, bool bip42) const NOEXCEPT
594
{
595
    return claim() > reward(height, subsidy_interval,
×
596
        initial_block_subsidy_satoshi, bip42);
×
597
}
598

599
size_t block::signature_operations(bool bip16, bool bip141) const NOEXCEPT
×
600
{
601
    // Overflow returns max_size_t.
602
    const auto value = [=](size_t total, const auto& tx) NOEXCEPT
×
603
    {
604
        return ceilinged_add(total, tx->signature_operations(bip16, bip141));
×
605
    };
×
606

607
    return std::accumulate(txs_->begin(), txs_->end(), zero, value);
×
608
}
609

610
bool block::is_signature_operations_limited(bool bip16,
×
611
    bool bip141) const NOEXCEPT
612
{
613
    const auto limit = bip141 ? max_fast_sigops : max_block_sigops;
×
614
    return signature_operations(bip16, bip141) > limit;
×
615
}
616

617
//*****************************************************************************
618
// CONSENSUS:
619
// This check is excluded under two bip30 exception blocks and bip30_deactivate
620
// until bip30_reactivate. These conditions are rolled up into the bip30 flag.
621
//*****************************************************************************
622
bool block::is_unspent_coinbase_collision() const NOEXCEPT
×
623
{
624
    if (txs_->empty() || txs_->front()->inputs_ptr()->empty())
×
625
        return false;
×
626

627
    // May only commit duplicate coinbase that is already confirmed spent.
628
    // Metadata population defaults coinbase to spent (not a collision).
629
    return !txs_->front()->inputs_ptr()->front()->metadata.spent;
×
630
}
631

632
// Search is not ordered, forward references are caught by block.check.
633
void block::populate() const NOEXCEPT
×
634
{
635
    std::unordered_map<point, output::cptr> points{};
×
636
    uint32_t index{};
×
637

638
    // Populate outputs hash table.
639
    for (auto tx = txs_->begin(); tx != txs_->end(); ++tx, index = 0)
×
640
        for (const auto& out: *(*tx)->outputs_ptr())
×
641
            points.emplace(std::pair{ point{ (*tx)->hash(false), index++ },
×
642
                out });
643

644
    // Populate input prevouts from hash table.
645
    for (auto tx = txs_->begin(); tx != txs_->end(); ++tx)
×
646
    {
647
        for (const auto& in: *(*tx)->inputs_ptr())
×
648
        {
649
            const auto point = points.find(in->point());
×
650
            if (point != points.end())
×
651
                in->prevout = point->second;
×
652
        }
653
    }
654
}
×
655

656
// Delegated.
657
// ----------------------------------------------------------------------------
658

659
// DO invoke on coinbase.
660
code block::check_transactions() const NOEXCEPT
×
661
{
662
    code ec;
×
663
    
664
    for (const auto& tx: *txs_)
×
665
        if ((ec = tx->check()))
×
666
            return ec;
×
667

668
    return error::block_success;
×
669
}
670

671
// DO invoke on coinbase.
672
code block::check_transactions(const context& ctx) const NOEXCEPT
×
673
{
674
    code ec;
×
675
    
676
    for (const auto& tx: *txs_)
×
677
        if ((ec = tx->check(ctx)))
×
678
            return ec;
×
679

680
    return error::block_success;
×
681
}
682

683
// Do NOT invoke on coinbase.
684
code block::accept_transactions(const context& ctx) const NOEXCEPT
×
685
{
686
    code ec;
×
687

688
    if (!is_empty())
×
689
        for (auto tx = std::next(txs_->begin()); tx != txs_->end(); ++tx)
×
690
            if ((ec = (*tx)->accept(ctx)))
×
691
                return ec;
×
692

693
    return error::block_success;
×
694
}
695

696
// Do NOT invoke on coinbase.
697
code block::connect_transactions(const context& ctx) const NOEXCEPT
×
698
{
699
    code ec;
×
700

701
    if (!is_empty())
×
702
        for (auto tx = std::next(txs_->begin()); tx != txs_->end(); ++tx)
×
703
            if ((ec = (*tx)->connect(ctx)))
×
704
                return ec;
×
705

706
    return error::block_success;
×
707
}
708

709
// Do NOT invoke on coinbase.
710
code block::confirm_transactions(const context& ctx) const NOEXCEPT
×
711
{
712
    code ec;
×
713

714
    if (!is_empty())
×
715
        for (auto tx = std::next(txs_->begin()); tx != txs_->end(); ++tx)
×
716
            if ((ec = (*tx)->confirm(ctx)))
×
717
                return ec;
×
718

719
    return error::block_success;
×
720
}
721

722
// Validation.
723
// ----------------------------------------------------------------------------
724
// The block header is checked/accepted independently.
725

726
// context free.
727
// TODO: use of get_hash() in is_forward_reference makes this thread unsafe.
728
code block::check(bool bypass) const NOEXCEPT
×
729
{
NEW
730
    if (is_invalid_merkle_root())
×
NEW
731
        return error::invalid_transaction_commitment;
×
732

733
    // type32_malleated is subset of is_internal_double_spend
NEW
734
    if (bypass && is_malleated32())
×
NEW
735
        return error::type32_malleated;
×
NEW
736
    if (bypass)
×
UNCOV
737
        return error::block_success;
×
738

739
    // empty_block is subset of first_not_coinbase.
740
    //if (is_empty())
741
    //    return error::empty_block;
742
    if (is_oversized())
×
743
        return error::block_size_limit;
×
744
    if (is_first_non_coinbase())
×
745
        return error::first_not_coinbase;
×
746
    if (is_extra_coinbases())
×
747
        return error::extra_coinbases;
×
748
    if (is_forward_reference())
×
749
        return error::forward_reference;
×
750
    if (is_internal_double_spend())
×
751
        return error::block_internal_double_spend;
×
752

753
    return check_transactions();
×
754
}
755

756
// forks
757
// height
758
// timestamp
759
// median_time_past
760

761
// context required.
762
// TODO: use of get_hash() in is_hash_limit_exceeded makes this thread unsafe.
763
code block::check(const context& ctx, bool bypass) const NOEXCEPT
×
764
{
NEW
765
    const auto bip141 = ctx.is_enabled(bip141_rule);
×
766

NEW
767
    if (bip141 && is_invalid_witness_commitment())
×
NEW
768
        return error::invalid_witness_commitment;
×
769

NEW
770
    if (bypass)        
×
771
        return error::block_success;
×
772

UNCOV
773
    const auto bip34 = ctx.is_enabled(bip34_rule);
×
774
    const auto bip50 = ctx.is_enabled(bip50_rule);
×
775

UNCOV
776
    if (bip141 && is_overweight())
×
777
        return error::block_weight_limit;
×
778
    if (bip34 && is_invalid_coinbase_script(ctx.height))
×
779
        return error::coinbase_height_mismatch;
×
780
    if (bip50 && is_hash_limit_exceeded())
×
781
        return error::temporary_hash_limit;
×
782

783
    return check_transactions(ctx);
×
784
}
785

786
// forks
787
// height
788

789
// This assumes that prevout caching is completed on all inputs.
790
code block::accept(const context& ctx, size_t subsidy_interval,
×
791
    uint64_t initial_subsidy) const NOEXCEPT
792
{
793
    const auto bip16 = ctx.is_enabled(bip16_rule);
×
794
    const auto bip42 = ctx.is_enabled(bip42_rule);
×
795
    const auto bip141 = ctx.is_enabled(bip141_rule);
×
796

797
    // prevouts required.
798
    if (is_overspent(ctx.height, subsidy_interval, initial_subsidy, bip42))
×
799
        return error::coinbase_value_limit;
×
800
    if (is_signature_operations_limited(bip16, bip141))
×
801
        return error::block_sigop_limit;
×
802

803
    return accept_transactions(ctx);
×
804
}
805

806
// forks
807

808
// Node performs these checks through database query.
809
// This assumes that prevout and metadata caching are completed on all inputs.
810
code block::confirm(const context& ctx) const NOEXCEPT
×
811
{
812
    const auto bip30 = ctx.is_enabled(bip30_rule);
×
813

814
    if (bip30 && is_unspent_coinbase_collision())
×
815
        return error::unspent_coinbase_collision;
×
816

817
    return confirm_transactions(ctx);
×
818
}
819

820
// forks
821

822
code block::connect(const context& ctx) const NOEXCEPT
×
823
{
824
    return connect_transactions(ctx);
×
825
}
826

827
BC_POP_WARNING()
828

829
// JSON value convertors.
830
// ----------------------------------------------------------------------------
831

832
namespace json = boost::json;
833

834
// boost/json will soon have NOEXCEPT: github.com/boostorg/json/pull/636
835
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
836

837
block tag_invoke(json::value_to_tag<block>,
1✔
838
    const json::value& value) NOEXCEPT
839
{
840
    return
1✔
841
    {
842
        json::value_to<header>(value.at("header")),
1✔
843
        json::value_to<chain::transactions>(value.at("transactions"))
2✔
844
    };
1✔
845
}
846

847
void tag_invoke(json::value_from_tag, json::value& value,
2✔
848
    const block& block) NOEXCEPT
849
{
850
    value =
2✔
851
    {
852
        { "header", block.header() },
853
        { "transactions", *block.transactions_ptr() },
854
    };
2✔
855
}
2✔
856

857
BC_POP_WARNING()
858

859
block::cptr tag_invoke(json::value_to_tag<block::cptr>,
×
860
    const json::value& value) NOEXCEPT
861
{
862
    return to_shared(tag_invoke(json::value_to_tag<block>{}, value));
×
863
}
864

865
// Shared pointer overload is required for navigation.
866
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
867
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
868

869
void tag_invoke(json::value_from_tag tag, json::value& value,
×
870
    const block::cptr& block) NOEXCEPT
871
{
872
    tag_invoke(tag, value, *block);
×
873
}
×
874

875
BC_POP_WARNING()
876
BC_POP_WARNING()
877

878
} // namespace chain
879
} // namespace system
880
} // 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