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

libbitcoin / libbitcoin-system / 9339255156

02 Jun 2024 03:11PM UTC coverage: 82.741% (-0.03%) from 82.77%
9339255156

Pull #1473

github

web-flow
Merge 5f216f442 into e47b9bf17
Pull Request #1473: Rem get_hash() where result copied, check malleated32 in bypass.

1 of 13 new or added lines in 1 file covered. (7.69%)

3 existing lines in 1 file now uncovered.

9847 of 11901 relevant lines covered (82.74%)

4792049.58 hits per line

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

46.67
/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
59✔
53
  : block(to_shared<chain::header>(), to_shared<chain::transaction_cptrs>(),
59✔
54
      false)
177✔
55
{
56
}
59✔
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
67✔
77
  : block(stream::in::copy(data), witness)
67✔
78
{
79
}
67✔
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
67✔
92
  : block(read::bytes::istream(stream), witness)
67✔
93
{
94
}
67✔
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
71✔
102
  : block(from_data(source, witness))
71✔
103
{
104
}
71✔
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,
172✔
113
    const chain::transactions_cptr& txs, bool valid) NOEXCEPT
172✔
114
  : header_(header),
115
    txs_(txs),
116
    valid_(valid),
172✔
117
    size_(serialized_size(*txs))
344✔
118
{
119
}
172✔
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
72✔
140
{
141
    const auto read_transactions = [witness](reader& source) NOEXCEPT
261✔
142
    {
143
        auto txs = to_shared<transaction_cptrs>();
72✔
144
        const auto capacity = source.read_size(max_block_size);
72✔
145
        txs->reserve(capacity);
72✔
146

147
        for (size_t tx = 0; tx < capacity; ++tx)
189✔
148
        {
149
            BC_PUSH_WARNING(NO_NEW_OR_DELETE)
150
            txs->emplace_back(new transaction{ source, witness });
117✔
151
            BC_POP_WARNING()
152
        }
153

154
        // This is a pointer copy (non-const to const).
155
        return txs;
72✔
156
    };
72✔
157

158
    return
72✔
159
    {
160
        to_shared<chain::header>(source),
72✔
161
        read_transactions(source),
144✔
162
        source
163
    };
144✔
164
}
165

166
// Serialization.
167
// ----------------------------------------------------------------------------
168

169
data_chunk block::to_data(bool witness) const NOEXCEPT
18✔
170
{
171
    data_chunk data(serialized_size(witness));
36✔
172
    stream::out::copy ostream(data);
18✔
173
    to_data(ostream, witness);
18✔
174
    return data;
36✔
175
}
18✔
176

177
void block::to_data(std::ostream& stream, bool witness) const NOEXCEPT
19✔
178
{
179
    write::bytes::ostream out(stream);
19✔
180
    to_data(out, witness);
19✔
181
}
19✔
182

183
void block::to_data(writer& sink, bool witness) const NOEXCEPT
20✔
184
{
185
    header_->to_data(sink);
20✔
186
    sink.write_variable(txs_->size());
20✔
187

188
    for (const auto& tx: *txs_)
46✔
189
        tx->to_data(sink, witness);
26✔
190
}
20✔
191

192
// Properties.
193
// ----------------------------------------------------------------------------
194

195
bool block::is_valid() const NOEXCEPT
32✔
196
{
197
    return valid_;
32✔
198
}
199

200
size_t block::transactions() const NOEXCEPT
×
201
{
202
    return txs_->size();
×
203
}
204

205
const chain::header& block::header() const NOEXCEPT
12✔
206
{
207
    return *header_;
2✔
208
}
209

210
const chain::header::cptr block::header_ptr() const NOEXCEPT
×
211
{
212
    return header_;
×
213
}
214

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

225
    std::for_each(txs_->begin(), txs_->end(), append_ins);
×
226
    return inputs;
×
227
}
228

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

236
hashes block::transaction_hashes(bool witness) const NOEXCEPT
11✔
237
{
238
    const auto count = txs_->size();
11✔
239
    const auto size = is_odd(count) && count > one ? add1(count) : count;
11✔
240
    hashes out(size);
11✔
241

242
    // Extra allocation for odd count optimizes for merkle root.
243
    // Vector capacity is never reduced when resizing to smaller size.
244
    out.resize(count);
11✔
245

246
    const auto hash = [witness](const auto& tx) NOEXCEPT
14✔
247
    {
248
        return tx->hash(witness);
14✔
249
    };
11✔
250

251
    std::transform(txs_->begin(), txs_->end(), out.begin(), hash);
11✔
252
    return out;
11✔
253
}
254

255
// computed
256
hash_digest block::hash() const NOEXCEPT
34✔
257
{
258
    return header_->hash();
34✔
259
}
260

261
// static/private
262
block::sizes block::serialized_size(
172✔
263
    const chain::transaction_cptrs& txs) NOEXCEPT
264
{
265
    sizes size{};
172✔
266
    std::for_each(txs.begin(), txs.end(), [&](const auto& tx) NOEXCEPT
417✔
267
    {
268
        size.nominal = ceilinged_add(size.nominal, tx->serialized_size(false));
490✔
269
        size.witnessed = ceilinged_add(size.witnessed, tx->serialized_size(true));
245✔
270
    });
245✔
271

272
    const auto common_size = ceilinged_add(
172✔
273
        header::serialized_size(),
274
        variable_size(txs.size()));
275

276
    const auto nominal_size = ceilinged_add(
172✔
277
        common_size,
278
        size.nominal);
279

280
    const auto witnessed_size = ceilinged_add(
172✔
281
        common_size,
282
        size.witnessed);
283

284
    return { nominal_size, witnessed_size };
172✔
285
}
286

287
size_t block::serialized_size(bool witness) const NOEXCEPT
19✔
288
{
289
    return witness ? size_.witnessed : size_.nominal;
19✔
290
}
291

292
// Connect.
293
// ----------------------------------------------------------------------------
294

295
bool block::is_empty() const NOEXCEPT
2✔
296
{
297
    return txs_->empty();
2✔
298
}
299

300
bool block::is_oversized() const NOEXCEPT
×
301
{
302
    return serialized_size(false) > max_block_size;
×
303
}
304

305
bool block::is_first_non_coinbase() const NOEXCEPT
×
306
{
307
    return !txs_->empty() && !txs_->front()->is_coinbase();
×
308
}
309

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

317
    const auto value = [](const auto& tx) NOEXCEPT
×
318
    {
319
        return tx->is_coinbase();
×
320
    };
321

322
    return std::any_of(std::next(txs_->begin()), txs_->end(), value);
×
323
}
324

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

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

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

349
    return std::any_of(txs_->rbegin(), std::prev(txs_->rend()), spend);
4✔
350
}
351

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

360
    // Overflow returns max_size_t.
361
    const auto sum_ins = [](size_t total, const auto& tx) NOEXCEPT
9✔
362
    {
363
        return ceilinged_add(total, tx->inputs());
9✔
364
    };
365

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

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

380
    return std::any_of(tx1, txs_->end(), double_spent);
2✔
381
}
382

383
// private
384
hash_digest block::generate_merkle_root(bool witness) const NOEXCEPT
11✔
385
{
386
    return sha256::merkle_root(transaction_hashes(witness));
11✔
387
}
388

389
bool block::is_invalid_merkle_root() const NOEXCEPT
11✔
390
{
391
    return generate_merkle_root(false) != header_->merkle_root();
11✔
392
}
393

394
// Accept (contextual).
395
// ----------------------------------------------------------------------------
396

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

405
bool block::is_overweight() const NOEXCEPT
×
406
{
407
    return weight() > max_block_weight;
×
408
}
409

410
bool block::is_invalid_coinbase_script(size_t height) const NOEXCEPT
×
411
{
412
    if (txs_->empty() || txs_->front()->inputs_ptr()->empty())
×
413
        return false;
×
414

415
    const auto& script = txs_->front()->inputs_ptr()->front()->script();
×
416
    return !script::is_coinbase_pattern(script.ops(), height);
×
417
}
418

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

428
    // A set is used to collapse duplicates.
NEW
429
    unordered_set_of_constant_referenced_hashes hashes{};
×
430

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

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

440
        // Insert all input point hashes.
441
        for (const auto& input: inputs)
×
442
            hashes.emplace(input->point().hash());
×
443
    }
444

445
    return hashes.size() > hash_limit;
×
446
}
447

448
bool block::is_malleable() const NOEXCEPT
3✔
449
{
450
    return is_malleable64() || is_malleable32();
3✔
451
}
452

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

460
    return false;
461
}
462

463
bool block::is_malleated32() const NOEXCEPT
11✔
464
{
465
    return !is_zero(malleated32_size());
11✔
466
}
467

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

477
    return zero;
478
}
479

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

488
    auto mally = txs_->rbegin();
6✔
489
    auto legit = std::next(mally, width);
6✔
490
    while (!is_zero(width--))
9✔
491
        if ((*mally++)->hash(false) != (*legit++)->hash(false))
7✔
492
            return false;
493

494
    return true;
495
}
496

497
bool block::is_malleable64() const NOEXCEPT
12✔
498
{
499
    return is_malleable64(*txs_);
12✔
500
}
501

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

512
    return !txs.empty() && std::all_of(txs.begin(), txs.end(), two_leaves);
12✔
513
}
514

515
bool block::is_segregated() const NOEXCEPT
×
516
{
517
    const auto segregated = [](const auto& tx) NOEXCEPT
×
518
    {
519
        return tx->is_segregated();
×
520
    };
521

522
    return std::any_of(txs_->begin(), txs_->end(), segregated);
×
523
}
524

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

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

565
// Prevouts required.
566

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

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

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

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

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

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

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

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

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

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

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

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

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

655
// Delegated.
656
// ----------------------------------------------------------------------------
657

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

667
    return error::block_success;
×
668
}
669

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

679
    return error::block_success;
×
680
}
681

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

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

692
    return error::block_success;
×
693
}
694

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

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

705
    return error::block_success;
×
706
}
707

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

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

718
    return error::block_success;
×
719
}
720

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

725
// context free.
726
// TODO: use of get_hash() in is_forward_reference makes this thread unsafe.
UNCOV
727
code block::check(bool bypass) const NOEXCEPT
×
728
{
729
    if (bypass)
×
730
    {
731
        // type32_malleated is subset of is_internal_double_spend, assuming
732
        // otherwise valid txs, as that will catch any duplicated transaction.
NEW
733
        if (is_invalid_merkle_root())
×
NEW
734
            return error::merkle_mismatch;
×
NEW
735
        if (is_malleated32())
×
NEW
736
            return error::type32_malleated;
×
737

NEW
738
        return error::block_success;
×
739
    }
740

741
    // empty_block is redundant with first_not_coinbase.
742
    //if (is_empty())
743
    //    return error::empty_block;
744
    if (is_oversized())
×
745
        return error::block_size_limit;
×
746
    if (is_first_non_coinbase())
×
747
        return error::first_not_coinbase;
×
748
    if (is_extra_coinbases())
×
749
        return error::extra_coinbases;
×
750
    if (is_forward_reference())
×
751
        return error::forward_reference;
×
752
    if (is_internal_double_spend())
×
753
        return error::block_internal_double_spend;
×
754
    if (is_invalid_merkle_root())
×
755
        return error::merkle_mismatch;
×
756

757
    return check_transactions();
×
758
}
759

760
// forks
761
// height
762
// timestamp
763
// median_time_past
764

765
// context required.
766
// TODO: use of get_hash() in is_hash_limit_exceeded makes this thread unsafe.
UNCOV
767
code block::check(const context& ctx, bool bypass) const NOEXCEPT
×
768
{
769
    if (bypass)
×
770
    {
771
        const auto bip141 = ctx.is_enabled(bip141_rule);
×
772

NEW
773
        if (bip141 && is_invalid_witness_commitment())
×
NEW
774
            return error::invalid_witness_commitment;
×
775
        
NEW
776
        return error::block_success;
×
777
    }
778

779
    const auto bip34 = ctx.is_enabled(bip34_rule);
×
780
    const auto bip50 = ctx.is_enabled(bip50_rule);
×
781
    const auto bip141 = ctx.is_enabled(bip141_rule);
×
782

UNCOV
783
    if (bip141 && is_overweight())
×
784
        return error::block_weight_limit;
×
785
    if (bip34 && is_invalid_coinbase_script(ctx.height))
×
786
        return error::coinbase_height_mismatch;
×
787
    if (bip50 && is_hash_limit_exceeded())
×
788
        return error::temporary_hash_limit;
×
789
    if (bip141 && is_invalid_witness_commitment())
×
790
        return error::invalid_witness_commitment;
×
791

792
    return check_transactions(ctx);
×
793
}
794

795
// forks
796
// height
797

798
// This assumes that prevout caching is completed on all inputs.
799
code block::accept(const context& ctx, size_t subsidy_interval,
×
800
    uint64_t initial_subsidy) const NOEXCEPT
801
{
802
    const auto bip16 = ctx.is_enabled(bip16_rule);
×
803
    const auto bip42 = ctx.is_enabled(bip42_rule);
×
804
    const auto bip141 = ctx.is_enabled(bip141_rule);
×
805

806
    // prevouts required.
807
    if (is_overspent(ctx.height, subsidy_interval, initial_subsidy, bip42))
×
808
        return error::coinbase_value_limit;
×
809
    if (is_signature_operations_limited(bip16, bip141))
×
810
        return error::block_sigop_limit;
×
811

812
    return accept_transactions(ctx);
×
813
}
814

815
// forks
816

817
// Node performs these checks through database query.
818
// This assumes that prevout and metadata caching are completed on all inputs.
819
code block::confirm(const context& ctx) const NOEXCEPT
×
820
{
821
    const auto bip30 = ctx.is_enabled(bip30_rule);
×
822

823
    if (bip30 && is_unspent_coinbase_collision())
×
824
        return error::unspent_coinbase_collision;
×
825

826
    return confirm_transactions(ctx);
×
827
}
828

829
// forks
830

831
code block::connect(const context& ctx) const NOEXCEPT
×
832
{
833
    return connect_transactions(ctx);
×
834
}
835

836
BC_POP_WARNING()
837

838
// JSON value convertors.
839
// ----------------------------------------------------------------------------
840

841
namespace json = boost::json;
842

843
// boost/json will soon have NOEXCEPT: github.com/boostorg/json/pull/636
844
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
845

846
block tag_invoke(json::value_to_tag<block>,
1✔
847
    const json::value& value) NOEXCEPT
848
{
849
    return
1✔
850
    {
851
        json::value_to<header>(value.at("header")),
1✔
852
        json::value_to<chain::transactions>(value.at("transactions"))
2✔
853
    };
1✔
854
}
855

856
void tag_invoke(json::value_from_tag, json::value& value,
2✔
857
    const block& block) NOEXCEPT
858
{
859
    value =
2✔
860
    {
861
        { "header", block.header() },
862
        { "transactions", *block.transactions_ptr() },
863
    };
2✔
864
}
2✔
865

866
BC_POP_WARNING()
867

868
block::cptr tag_invoke(json::value_to_tag<block::cptr>,
×
869
    const json::value& value) NOEXCEPT
870
{
871
    return to_shared(tag_invoke(json::value_to_tag<block>{}, value));
×
872
}
873

874
// Shared pointer overload is required for navigation.
875
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
876
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
877

878
void tag_invoke(json::value_from_tag tag, json::value& value,
×
879
    const block::cptr& block) NOEXCEPT
880
{
881
    tag_invoke(tag, value, *block);
×
882
}
×
883

884
BC_POP_WARNING()
885
BC_POP_WARNING()
886

887
} // namespace chain
888
} // namespace system
889
} // 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