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

libbitcoin / libbitcoin-system / 4637370737

pending completion
4637370737

push

github

GitHub
Merge pull request #1353 from evoskuil/master

102 of 102 new or added lines in 5 files covered. (100.0%)

9438 of 11391 relevant lines covered (82.85%)

6703526.24 hits per line

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

39.87
/src/chain/block.cpp
1
/**
2
 * Copyright (c) 2011-2022 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 <cfenv>
23
#include <iterator>
24
#include <memory>
25
#include <numeric>
26
#include <type_traits>
27
#include <unordered_map>
28
#include <unordered_set>
29
#include <utility>
30
#include <unordered_map>
31
#include <bitcoin/system/chain/context.hpp>
32
#include <bitcoin/system/chain/enums/forks.hpp>
33
#include <bitcoin/system/chain/enums/magic_numbers.hpp>
34
#include <bitcoin/system/chain/enums/opcode.hpp>
35
#include <bitcoin/system/chain/point.hpp>
36
#include <bitcoin/system/chain/script.hpp>
37
#include <bitcoin/system/data/data.hpp>
38
#include <bitcoin/system/define.hpp>
39
#include <bitcoin/system/error/error.hpp>
40
#include <bitcoin/system/math/math.hpp>
41
#include <bitcoin/system/settings.hpp>
42
#include <bitcoin/system/stream/stream.hpp>
43

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

48
// Constructors.
49
// ----------------------------------------------------------------------------
50

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

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

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

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

75
block::block(const data_slice& data, bool witness) NOEXCEPT
66✔
76
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
77
  : block(stream::in::copy(data), witness)
66✔
78
    BC_POP_WARNING()
79
{
80
}
66✔
81

82
block::block(std::istream&& stream, bool witness) NOEXCEPT
66✔
83
  : block(read::bytes::istream(stream), witness)
66✔
84
{
85
}
66✔
86

87
block::block(std::istream& stream, bool witness) NOEXCEPT
3✔
88
  : block(read::bytes::istream(stream), witness)
3✔
89
{
90
}
3✔
91

92
block::block(reader&& source, bool witness) NOEXCEPT
69✔
93
  : block(from_data(source, witness))
69✔
94
{
95
}
69✔
96

97
block::block(reader& source, bool witness) NOEXCEPT
1✔
98
  : block(from_data(source, witness))
1✔
99
{
100
}
1✔
101

102
// protected
103
block::block(const chain::header::cptr& header,
142✔
104
    const chain::transactions_cptr& txs, bool valid) NOEXCEPT
142✔
105
  : header_(header), txs_(txs), valid_(valid)
142✔
106
{
107
}
142✔
108

109
// Operators.
110
// ----------------------------------------------------------------------------
111

112
bool block::operator==(const block& other) const NOEXCEPT
28✔
113
{
114
    return (header_ == other.header_ || *header_ == *other.header_)
28✔
115
        && deep_equal(*txs_, *other.txs_);
35✔
116
}
117

118
bool block::operator!=(const block& other) const NOEXCEPT
5✔
119
{
120
    return !(*this == other);
5✔
121
}
122

123
// Deserialization.
124
// ----------------------------------------------------------------------------
125

126
// static/private
127
block block::from_data(reader& source, bool witness) NOEXCEPT
70✔
128
{
129
    const auto read_transactions = [witness](reader& source) NOEXCEPT
255✔
130
    {
131
        auto txs = to_shared<transaction_cptrs>();
70✔
132
        txs->reserve(source.read_size(max_block_size));
70✔
133

134
        for (size_t tx = 0; tx < txs->capacity(); ++tx)
185✔
135
        {
136
            BC_PUSH_WARNING(NO_NEW_OR_DELETE)
137
            BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
138
            txs->emplace_back(new transaction{ source, witness });
115✔
139
            BC_POP_WARNING()
140
            BC_POP_WARNING()
141
        }
142

143
        // This is a pointer copy (non-const to const).
144
        return txs;
70✔
145
    };
70✔
146

147
    return
70✔
148
    {
149
        to_shared<chain::header>(source),
70✔
150
        read_transactions(source),
140✔
151
        source
152
    };
140✔
153
}
154

155
// Serialization.
156
// ----------------------------------------------------------------------------
157

158
data_chunk block::to_data(bool witness) const NOEXCEPT
17✔
159
{
160
    data_chunk data(serialized_size(witness));
17✔
161

162
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
163
    stream::out::copy ostream(data);
17✔
164
    BC_POP_WARNING()
165

166
    to_data(ostream, witness);
17✔
167
    return data;
34✔
168
}
17✔
169

170
void block::to_data(std::ostream& stream, bool witness) const NOEXCEPT
18✔
171
{
172
    write::bytes::ostream out(stream);
18✔
173
    to_data(out, witness);
18✔
174
}
18✔
175

176
void block::to_data(writer& sink, bool witness) const NOEXCEPT
19✔
177
{
178
    header_->to_data(sink);
19✔
179
    sink.write_variable(txs_->size());
19✔
180

181
    for (const auto& tx: *txs_)
44✔
182
        tx->to_data(sink, witness);
25✔
183
}
19✔
184

185
// Properties.
186
// ----------------------------------------------------------------------------
187

188
bool block::is_valid() const NOEXCEPT
31✔
189
{
190
    return valid_;
31✔
191
}
192

193
const chain::header& block::header() const NOEXCEPT
12✔
194
{
195
    return *header_;
12✔
196
}
197

198
const chain::header::cptr block::header_ptr() const NOEXCEPT
×
199
{
200
    return header_;
×
201
}
202

203
// Roll up inputs for concurrent prevout processing.
204
const inputs_cptr block::inputs_ptr() const NOEXCEPT
×
205
{
206
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
207
    const auto inputs = std::make_shared<input_cptrs>();
×
208
    BC_POP_WARNING()
209

210
    const auto append_inputs = [&inputs](const transaction::cptr& tx)
×
211
    {
212
        const auto& tx_ins = *tx->inputs_ptr();
×
213
        inputs->insert(inputs->end(), tx_ins.begin(), tx_ins.end());
×
214
    };
×
215

216
    std::for_each(txs_->begin(), txs_->end(), append_inputs);
×
217
    return inputs;
×
218
}
219

220
// vector<transaction> is not exposed (because we don't have it).
221
// This would require a from_shared(txs_) conversion (expensive).
222
const transactions_cptr& block::transactions_ptr() const NOEXCEPT
87✔
223
{
224
    return txs_;
87✔
225
}
226

227
hashes block::transaction_hashes(bool witness) const NOEXCEPT
10✔
228
{
229
    const auto count = txs_->size();
10✔
230
    const auto size = is_odd(count) && count > one ? add1(count) : count;
10✔
231
    hashes out(size);
10✔
232

233
    // Extra allocation for odd count optimizes for merkle root.
234
    // Vector capacity is never reduced when resizing to smaller size.
235
    out.resize(count);
10✔
236

237
    const auto hash = [witness](const transaction::cptr& tx) NOEXCEPT
23✔
238
    {
239
        return tx->hash(witness);
13✔
240
    };
10✔
241

242
    std::transform(txs_->begin(), txs_->end(), out.begin(), hash);
10✔
243
    return out;
10✔
244
}
245

246
// computed
247
hash_digest block::hash() const NOEXCEPT
34✔
248
{
249
    return header_->hash();
34✔
250
}
251

252
size_t block::serialized_size(bool witness) const NOEXCEPT
18✔
253
{
254
    // Overflow returns max_size_t.
255
    const auto sum = [witness](size_t total, const transaction::cptr& tx) NOEXCEPT
40✔
256
    {
257
        return ceilinged_add(total, tx->serialized_size(witness));
22✔
258
    };
18✔
259

260
    return header::serialized_size()
18✔
261
        + variable_size(txs_->size())
262
        + std::accumulate(txs_->begin(), txs_->end(), zero, sum);
18✔
263
}
264

265
// Connect.
266
// ----------------------------------------------------------------------------
267

268
////// Subset of is_internal_double_spend if sha256 collisions cannot happen.
269
////bool block::is_distinct_transaction_set() const
270
////{
271
////    // A set is used to collapse duplicates.
272
////    std::set<hash_digest> hashes;
273
////
274
////    for (const auto& tx: *txs_)
275
////        hashes.insert(tx->hash(false));
276
////
277
////    return hashes.size() == txs_->size();
278
////}
279

280
bool block::is_empty() const NOEXCEPT
2✔
281
{
282
    return txs_->empty();
2✔
283
}
284

285
bool block::is_oversized() const NOEXCEPT
×
286
{
287
    return serialized_size(false) > max_block_size;
×
288
}
289

290
bool block::is_first_non_coinbase() const NOEXCEPT
×
291
{
292
    return !txs_->empty() && !txs_->front()->is_coinbase();
×
293
}
294

295
// True if there is another coinbase other than the first tx.
296
// No txs or coinbases returns false.
297
bool block::is_extra_coinbases() const NOEXCEPT
×
298
{
299
    if (txs_->empty())
×
300
        return false;
301

302
    const auto value = [](const transaction::cptr& tx) NOEXCEPT
×
303
    {
304
        return tx->is_coinbase();
×
305
    };
306

307
    return std::any_of(std::next(txs_->begin()), txs_->end(), value);
×
308
}
309

310
//*****************************************************************************
311
// CONSENSUS: This is only necessary because satoshi stores and queries as it
312
// validates, imposing an otherwise unnecessary partial transaction ordering.
313
//*****************************************************************************
314
bool block::is_forward_reference() const NOEXCEPT
5✔
315
{
316
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
317
    std::unordered_map<hash_digest, bool> hashes(txs_->size());
5✔
318
    BC_POP_WARNING()
319

320
    const auto is_forward = [&hashes](const input::cptr& input) NOEXCEPT
7✔
321
    {
322
        return !is_zero(hashes.count(input->point().hash()));
2✔
323
    };
5✔
324

325
    for (const auto& tx: views_reverse(*txs_))
12✔
326
    {
327
        BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
328
        hashes.emplace(tx->hash(false), false);
8✔
329
        BC_POP_WARNING()
330

331
        const auto& inputs = *tx->inputs_ptr();
8✔
332
        if (std::any_of(inputs.begin(), inputs.end(), is_forward))
8✔
333
            return true;
1✔
334
    }
335

336
    return false;
4✔
337
}
338

339
// private
340
size_t block::non_coinbase_inputs() const NOEXCEPT
2✔
341
{
342
    // Overflow returns max_size_t.
343
    const auto inputs = [](size_t total, const transaction::cptr& tx) NOEXCEPT
11✔
344
    {
345
        return ceilinged_add(total, tx->inputs_ptr()->size());
9✔
346
    };
347

348
    return std::accumulate(std::next(txs_->begin()), txs_->end(), zero, inputs);
2✔
349
}
350

351
// This also precludes the block merkle calculation DoS exploit.
352
// bitcointalk.org/?topic=102395
353
bool block::is_internal_double_spend() const NOEXCEPT
3✔
354
{
355
    if (txs_->empty())
3✔
356
        return false;
357

358
    // A set is used to collapse duplicate points.
359
    std::unordered_set<point> outs{};
2✔
360

361
    // Move the points of all non-coinbase transactions into one set.
362
    for (auto tx = std::next(txs_->begin()); tx != txs_->end(); ++tx)
11✔
363
    {
364
        auto out = (*tx)->points();
9✔
365
        std::move(out.begin(), out.end(), std::inserter(outs, outs.end()));
9✔
366
    }
9✔
367

368
    return outs.size() != non_coinbase_inputs();
2✔
369
}
370

371
// private
372
hash_digest block::generate_merkle_root(bool witness) const NOEXCEPT
10✔
373
{
374
    return sha256::merkle_root(transaction_hashes(witness));
10✔
375
}
376

377
bool block::is_invalid_merkle_root() const NOEXCEPT
10✔
378
{
379
    return generate_merkle_root(false) != header_->merkle_root();
10✔
380
}
381

382
// Accept (contextual).
383
// ----------------------------------------------------------------------------
384

385
size_t block::weight() const NOEXCEPT
×
386
{
387
    // Block weight is 3 * Base size * + 1 * Total size (bip141).
388
    return base_size_contribution * serialized_size(false) +
×
389
        total_size_contribution * serialized_size(true);
×
390
}
391

392
bool block::is_overweight() const NOEXCEPT
×
393
{
394
    return weight() > max_block_weight;
×
395
}
396

397
bool block::is_invalid_coinbase_script(size_t height) const NOEXCEPT
×
398
{
399
    if (txs_->empty() || txs_->front()->inputs_ptr()->empty())
×
400
        return false;
×
401

402
    const auto& script = txs_->front()->inputs_ptr()->front()->script();
×
403
    return !script::is_coinbase_pattern(script.ops(), height);
×
404
}
405

406
// TODO: add bip50 to chain_state with timestamp range activation.
407
// "Special short-term limits to avoid 10,000 BDB lock limit.
408
// Count of unique txids <= 4500 to prevent 10000 BDB lock exhaustion.
409
// header.timestamp > 1363039171 && header.timestamp < 1368576000."
410
bool block::is_hash_limit_exceeded() const NOEXCEPT
×
411
{
412
    if (txs_->empty())
×
413
        return false;
414

415
    // A set is used to collapse duplicates.
416
    std::unordered_set<hash_digest> hashes;
×
417

418
    // Just the coinbase tx hash, skip its null input hashes.
419
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
420
    hashes.insert(txs_->front()->hash(false));
×
421
    BC_POP_WARNING()
422

423
    for (auto tx = std::next(txs_->begin()); tx != txs_->end(); ++tx)
×
424
    {
425
        // Insert the transaction hash.
426
        BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
427
        hashes.insert((*tx)->hash(false));
×
428
        BC_POP_WARNING()
429

430
        const auto& inputs = *(*tx)->inputs_ptr();
×
431

432
        // Insert all input point hashes.
433
        for (const auto& input: inputs)
×
434
        {
435
            BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
436
            hashes.insert(input->point().hash());
×
437
            BC_POP_WARNING()
438
        }
439
    }
440

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

444
bool block::is_segregated() const NOEXCEPT
×
445
{
446
    const auto segregated = [](const transaction::cptr& tx) NOEXCEPT
×
447
    {
448
        return tx->is_segregated();
×
449
    };
450

451
    return std::any_of(txs_->begin(), txs_->end(), segregated);
×
452
}
453

454
bool block::is_invalid_witness_commitment() const NOEXCEPT
×
455
{
456
    if (txs_->empty())
×
457
        return false;
458

459
    const auto& coinbase = *txs_->front();
×
460
    if (coinbase.inputs_ptr()->empty())
×
461
        return false;
462

463
    // If there is a valid commitment, return false (valid).
464
    // Last output of commitment pattern holds committed value (bip141).
465
    hash_digest reserved{}, committed{};
×
466
    if (coinbase.inputs_ptr()->front()->reserved_hash(reserved))
×
467
        for (const auto& output: views_reverse(*coinbase.outputs_ptr()))
×
468
            if (output->committed_hash(committed))
×
469
                if (committed == sha256::double_hash(
×
470
                    generate_merkle_root(true), reserved))
×
471
                    return false;
×
472
    
473
    // If no valid commitment, return true (invalid) if segregated.
474
    // If no block tx has witness data the commitment is optional (bip141).
475
    return is_segregated();
×
476
}
477

478
//*****************************************************************************
479
// CONSENSUS:
480
// bip42 compensates for C++ undefined behavior of a right shift of a number of
481
// bits greater or equal to the shifted integer width. Yet being undefined, the
482
// result of this operation may vary by compiler. The shift_right call below
483
// explicitly implements presumed pre-bip42 behavior (shift overflow modulo) by
484
// default, and specified bip42 behavior (shift overflow to zero) with bip42.
485
//*****************************************************************************
486
static uint64_t block_subsidy(size_t height, uint64_t subsidy_interval,
×
487
    uint64_t initial_block_subsidy_satoshi, bool bip42) NOEXCEPT
488
{
489
    // Guard: quotient domain cannot increase with positive integer divisor.
490
    const auto halves = possible_narrow_cast<size_t>(height / subsidy_interval);
×
491
    return shift_right(initial_block_subsidy_satoshi, halves, bip42);
×
492
}
493

494
// Prevouts required.
495

496
uint64_t block::fees() const NOEXCEPT
×
497
{
498
    // Overflow returns max_uint64.
499
    const auto value = [](uint64_t total, const transaction::cptr& tx) NOEXCEPT
×
500
    {
501
        return ceilinged_add(total, tx->fee());
×
502
    };
503

504
    return std::accumulate(txs_->begin(), txs_->end(), uint64_t{0}, value);
×
505
}
506

507
uint64_t block::claim() const NOEXCEPT
×
508
{
509
    return txs_->empty() ? zero : txs_->front()->value();
×
510
}
511

512
uint64_t block::reward(size_t height, uint64_t subsidy_interval,
×
513
    uint64_t initial_block_subsidy_satoshi, bool bip42) const NOEXCEPT
514
{
515
    // Overflow returns max_uint64.
516
    return ceilinged_add(fees(), block_subsidy(height, subsidy_interval,
×
517
        initial_block_subsidy_satoshi, bip42));
×
518
}
519

520
bool block::is_overspent(size_t height, uint64_t subsidy_interval,
×
521
    uint64_t initial_block_subsidy_satoshi, bool bip42) const NOEXCEPT
522
{
523
    return claim() > reward(height, subsidy_interval,
×
524
        initial_block_subsidy_satoshi, bip42);
×
525
}
526

527
size_t block::signature_operations(bool bip16, bool bip141) const NOEXCEPT
×
528
{
529
    // Overflow returns max_size_t.
530
    const auto value = [=](size_t total, const transaction::cptr& tx) NOEXCEPT
×
531
    {
532
        return ceilinged_add(total, tx->signature_operations(bip16, bip141));
×
533
    };
×
534

535
    return std::accumulate(txs_->begin(), txs_->end(), zero, value);
×
536
}
537

538
bool block::is_signature_operations_limited(bool bip16,
×
539
    bool bip141) const NOEXCEPT
540
{
541
    const auto limit = bip141 ? max_fast_sigops : max_block_sigops;
×
542
    return signature_operations(bip16, bip141) > limit;
×
543
}
544

545
//*****************************************************************************
546
// CONSENSUS:
547
// This check is excluded under two bip30 exception blocks. This also cannot
548
// occur in any branch above bip34, due to height in coinbase and the
549
// presumption of sha256 non-collision. So this check is bypassed for both
550
// exception blocks and if bip34 is active (including under bip90 activation).
551
//*****************************************************************************
552
bool block::is_unspent_coinbase_collision() const NOEXCEPT
×
553
{
554
    if (txs_->empty() || txs_->front()->inputs_ptr()->empty())
×
555
        return false;
×
556

557
    // May only commit a coinbase that has already been confirmed spent.
558
    return !txs_->front()->inputs_ptr()->front()->metadata.spent;
×
559
}
560

561
void block::populate() const NOEXCEPT
×
562
{
563
    // Coinbase, outputs only, inputs only.
564
    if (txs_->size() < 3u)
×
565
        return;
×
566

567
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
568
    std::unordered_map<point, output::cptr> map{};
×
569

570
    // Skip coinbase tx.
571
    auto tx = std::next(txs_->begin());
×
572
    uint32_t index{};
×
573

574
    // Outputs only (first tx).
575
    for (const auto& out: *(*tx)->outputs_ptr())
×
576
        map.emplace(point{ (*tx)->hash(false), index++ }, out);
×
577

578
    // Search is ordered, no forward references or coinbase spend (consensus).
579
    for (++tx; tx != std::prev(txs_->end()); ++tx)
×
580
    {
581
        for (const auto& in: *(*tx)->inputs_ptr())
×
582
        {
583
            const auto element = map.find(in->point());
×
584
            if (element != map.end())
×
585
                in->prevout = element->second;
×
586
        }
587

588
        index = 0;
×
589
        for (const auto& out: *(*tx)->outputs_ptr())
×
590
            map.emplace(point{ (*tx)->hash(false), index++ }, out);
×
591
    }
592

593
    // Inputs only (last tx).
594
    for (const auto& in: *(*tx)->inputs_ptr())
×
595
    {
596
        const auto element = map.find(in->point());
×
597
        if (element != map.end())
×
598
            in->prevout = element->second;
×
599
    }
600

601
    BC_POP_WARNING()
602
}
603

604
// Delegated.
605
// ----------------------------------------------------------------------------
606

607
// DO invoke on coinbase.
608
code block::check_transactions() const NOEXCEPT
×
609
{
610
    code ec;
×
611
    
612
    for (const auto& tx: *txs_)
×
613
        if ((ec = tx->check()))
×
614
            return ec;
×
615

616
    return error::block_success;
×
617
}
618

619
// DO invoke on coinbase.
620
code block::check_transactions(const context& ctx) const NOEXCEPT
×
621
{
622
    code ec;
×
623
    
624
    for (const auto& tx: *txs_)
×
625
        if ((ec = tx->check(ctx)))
×
626
            return ec;
×
627

628
    return error::block_success;
×
629
}
630

631
// Do NOT invoke on coinbase.
632
code block::accept_transactions(const context& ctx) const NOEXCEPT
×
633
{
634
    code ec;
×
635

636
    if (!is_empty())
×
637
        for (auto tx = std::next(txs_->begin()); tx != txs_->end(); ++tx)
×
638
            if ((ec = (*tx)->accept(ctx)))
×
639
                return ec;
×
640

641
    return error::block_success;
×
642
}
643

644
// Do NOT invoke on coinbase.
645
code block::connect_transactions(const context& ctx) const NOEXCEPT
×
646
{
647
    code ec;
×
648

649
    if (!is_empty())
×
650
        for (auto tx = std::next(txs_->begin()); tx != txs_->end(); ++tx)
×
651
            if ((ec = (*tx)->connect(ctx)))
×
652
                return ec;
×
653

654
    return error::block_success;
×
655
}
656

657
// Do NOT invoke on coinbase.
658
code block::confirm_transactions(const context& ctx) const NOEXCEPT
×
659
{
660
    code ec;
×
661
    
662
    for (auto tx = std::next(txs_->begin()); tx != txs_->end(); ++tx)
×
663
        if ((ec = (*tx)->confirm(ctx)))
×
664
            return ec;
×
665

666
    return error::block_success;
×
667
}
668

669
// Validation.
670
// ----------------------------------------------------------------------------
671
// The block header is checked/accepted independently.
672

673
code block::check() const NOEXCEPT
×
674
{
675
    // context free.
676
    if (is_empty())
×
677
        return error::empty_block;
×
678
    if (is_oversized())
×
679
        return error::block_size_limit;
×
680
    if (is_first_non_coinbase())
×
681
        return error::first_not_coinbase;
×
682
    if (is_extra_coinbases())
×
683
        return error::extra_coinbases;
×
684
    if (is_forward_reference())
×
685
        return error::forward_reference;
×
686
    if (is_internal_double_spend())
×
687
        return error::block_internal_double_spend;
×
688
    if (is_invalid_merkle_root())
×
689
        return error::merkle_mismatch;
×
690

691
    return check_transactions();
×
692
}
693

694
code block::check(const context& ctx) const NOEXCEPT
×
695
{
696
    const auto bip34 = ctx.is_enabled(bip34_rule);
×
697
    const auto bip50 = ctx.is_enabled(bip50_rule);
×
698
    const auto bip141 = ctx.is_enabled(bip141_rule);
×
699

700
    // context required.
701
    if (bip141 && is_overweight())
×
702
        return error::block_weight_limit;
×
703
    if (bip34 && is_invalid_coinbase_script(ctx.height))
×
704
        return error::coinbase_height_mismatch;
×
705
    if (bip50 && is_hash_limit_exceeded())
×
706
        return error::temporary_hash_limit;
×
707
    if (bip141 && is_invalid_witness_commitment())
×
708
        return error::invalid_witness_commitment;
×
709

710
    return check_transactions(ctx);
×
711
}
712

713
// These assume that prevout caching is completed on all inputs.
714
code block::accept(const context& ctx, size_t subsidy_interval,
×
715
    uint64_t initial_subsidy) const NOEXCEPT
716
{
717
    const auto bip16 = ctx.is_enabled(bip16_rule);
×
718
    const auto bip42 = ctx.is_enabled(bip42_rule);
×
719
    const auto bip141 = ctx.is_enabled(bip141_rule);
×
720

721
    // prevouts required.
722
    if (is_overspent(ctx.height, subsidy_interval, initial_subsidy, bip42))
×
723
        return error::coinbase_value_limit;
×
724
    if (is_signature_operations_limited(bip16, bip141))
×
725
        return error::block_sigop_limit;
×
726

727
    return accept_transactions(ctx);
×
728
}
729

730
// This assume that prevout and metadata caching are completed on all inputs.
731
code block::confirm(const context& ctx) const NOEXCEPT
×
732
{
733
    const auto bip30 = ctx.is_enabled(bip30_rule);
×
734
    const auto bip34 = ctx.is_enabled(bip34_rule);
×
735

736
    // confirmations required.
737
    if (bip30 && !bip34 && is_unspent_coinbase_collision())
×
738
        return error::unspent_coinbase_collision;
×
739

740
    return confirm_transactions(ctx);
×
741
}
742

743
code block::connect(const context& ctx) const NOEXCEPT
×
744
{
745
    return connect_transactions(ctx);
×
746
}
747

748
// JSON value convertors.
749
// ----------------------------------------------------------------------------
750

751
namespace json = boost::json;
752

753
// boost/json will soon have NOEXCEPT: github.com/boostorg/json/pull/636
754
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
755

756
block tag_invoke(json::value_to_tag<block>,
1✔
757
    const json::value& value) NOEXCEPT
758
{
759
    return
1✔
760
    {
761
        json::value_to<header>(value.at("header")),
1✔
762
        json::value_to<chain::transactions>(value.at("transactions"))
2✔
763
    };
1✔
764
}
765

766
void tag_invoke(json::value_from_tag, json::value& value,
2✔
767
    const block& block) NOEXCEPT
768
{
769
    value =
2✔
770
    {
771
        { "header", block.header() },
772
        { "transactions", *block.transactions_ptr() },
2✔
773
    };
2✔
774
}
2✔
775

776
BC_POP_WARNING()
777

778
block::cptr tag_invoke(json::value_to_tag<block::cptr>,
×
779
    const json::value& value) NOEXCEPT
780
{
781
    return to_shared(tag_invoke(json::value_to_tag<block>{}, value));
×
782
}
783

784
// Shared pointer overload is required for navigation.
785
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
786
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
787

788
void tag_invoke(json::value_from_tag tag, json::value& value,
×
789
    const block::cptr& block) NOEXCEPT
790
{
791
    tag_invoke(tag, value, *block);
×
792
}
×
793

794
BC_POP_WARNING()
795
BC_POP_WARNING()
796

797
} // namespace chain
798
} // namespace system
799
} // 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