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

libbitcoin / libbitcoin-system / 8575613188

05 Apr 2024 08:56PM UTC coverage: 82.661% (-0.02%) from 82.682%
8575613188

push

github

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

Factor and fix malleability checks.

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

2 existing lines in 1 file now uncovered.

9706 of 11742 relevant lines covered (82.66%)

4858021.49 hits per line

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

40.44
/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 <iterator>
23
#include <memory>
24
#include <numeric>
25
#include <set>
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/flags.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/stream/stream.hpp>
42

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

47
// Constructors.
48
// ----------------------------------------------------------------------------
49

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

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

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

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

74
block::block(const data_slice& data, bool witness) NOEXCEPT
67✔
75
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
76
  : block(stream::in::copy(data), witness)
67✔
77
    BC_POP_WARNING()
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,
145✔
113
    const chain::transactions_cptr& txs, bool valid) NOEXCEPT
145✔
114
  : header_(header), txs_(txs), valid_(valid)
145✔
115
{
116
}
145✔
117

118
// Operators.
119
// ----------------------------------------------------------------------------
120

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

127
bool block::operator!=(const block& other) const NOEXCEPT
5✔
128
{
129
    return !(*this == other);
5✔
130
}
131

132
// Deserialization.
133
// ----------------------------------------------------------------------------
134

135
// static/private
136
block block::from_data(reader& source, bool witness) NOEXCEPT
72✔
137
{
138
    const auto read_transactions = [witness](reader& source) NOEXCEPT
261✔
139
    {
140
        auto txs = to_shared<transaction_cptrs>();
72✔
141
        txs->reserve(source.read_size(max_block_size));
72✔
142

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

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

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

164
// Serialization.
165
// ----------------------------------------------------------------------------
166

167
data_chunk block::to_data(bool witness) const NOEXCEPT
18✔
168
{
169
    data_chunk data(serialized_size(witness));
18✔
170

171
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
172
    stream::out::copy ostream(data);
18✔
173
    BC_POP_WARNING()
174

175
    to_data(ostream, witness);
18✔
176
    return data;
36✔
177
}
18✔
178

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

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

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

194
// Properties.
195
// ----------------------------------------------------------------------------
196

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

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

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

212
// Roll up inputs for concurrent prevout processing.
213
const inputs_cptr block::inputs_ptr() const NOEXCEPT
×
214
{
215
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
216
    const auto inputs = std::make_shared<input_cptrs>();
×
217
    BC_POP_WARNING()
218

219
    const auto append_inputs = [&inputs](const transaction::cptr& tx)
×
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_inputs);
×
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 transaction::cptr& tx) NOEXCEPT
25✔
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
size_t block::serialized_size(bool witness) const NOEXCEPT
19✔
262
{
263
    // Overflow returns max_size_t.
264
    const auto sum = [witness](size_t total, const transaction::cptr& tx) NOEXCEPT
42✔
265
    {
266
        return ceilinged_add(total, tx->serialized_size(witness));
23✔
267
    };
19✔
268

269
    return header::serialized_size()
19✔
270
        + variable_size(txs_->size())
19✔
271
        + std::accumulate(txs_->begin(), txs_->end(), zero, sum);
19✔
272
}
273

274
// Connect.
275
// ----------------------------------------------------------------------------
276

277
bool block::is_empty() const NOEXCEPT
2✔
278
{
279
    return txs_->empty();
2✔
280
}
281

282
bool block::is_oversized() const NOEXCEPT
×
283
{
284
    return serialized_size(false) > max_block_size;
×
285
}
286

287
bool block::is_first_non_coinbase() const NOEXCEPT
×
288
{
289
    return !txs_->empty() && !txs_->front()->is_coinbase();
×
290
}
291

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

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

304
    return std::any_of(std::next(txs_->begin()), txs_->end(), value);
×
305
}
306

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

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

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

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

333
    return false;
4✔
334
}
335

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

345
    return std::accumulate(std::next(txs_->begin()), txs_->end(), zero, inputs);
2✔
346
}
347

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

355
    // A set is used to collapse duplicate points.
356
    std::unordered_set<point> outs{};
2✔
357

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

365
    return outs.size() != non_coinbase_inputs();
2✔
366
}
367

368
// private
369
hash_digest block::generate_merkle_root(bool witness) const NOEXCEPT
11✔
370
{
371
    return sha256::merkle_root(transaction_hashes(witness));
11✔
372
}
373

374
bool block::is_invalid_merkle_root() const NOEXCEPT
11✔
375
{
376
    return generate_merkle_root(false) != header_->merkle_root();
11✔
377
}
378

379
// Accept (contextual).
380
// ----------------------------------------------------------------------------
381

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

389
bool block::is_overweight() const NOEXCEPT
×
390
{
391
    return weight() > max_block_weight;
×
392
}
393

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

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

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

412
    // A set is used to collapse duplicates.
413
    std::unordered_set<hash_digest> hashes;
×
414

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

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

427
        const auto& inputs = *(*tx)->inputs_ptr();
×
428

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

438
    return hashes.size() > hash_limit;
×
439
}
440

441
// This is not part of validation. Should be called after *invalidation* to
442
// determine if the invalidity is universal (otherwise do not cache invalid),
443
// and as an abbreviated validation when under checkpoint/milestone.
444
// lists.linuxfoundation.org/pipermail/bitcoin-dev/2019-February/016697.html
445
bool block::is_malleable() const NOEXCEPT
×
446
{
NEW
447
    return is_malleable_coincident() || is_malleable_duplicate();
×
448
}
449

450
// Repeated tx hashes is a subset of is_internal_double_spend.
451
// This form of malleability also implies current block instance is invalid.
NEW
452
bool block::is_malleable_duplicate() const NOEXCEPT
×
453
{
454
    // A set is used to collapse duplicates.
NEW
455
    std::set<hash_digest> hashes;
×
456

457
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
NEW
458
    for (const auto& tx: *txs_)
×
NEW
459
        hashes.insert(tx->hash(false));
×
460
    BC_POP_WARNING()
461
    
NEW
462
    return hashes.size() == txs_->size();
×
463
}
464

465
// If all non-witness tx serializations are 64 bytes the id is malleable.
466
// This form of malleability does not imply current block instance is invalid.
NEW
467
bool block::is_malleable_coincident() const NOEXCEPT
×
468
{
UNCOV
469
    const auto two_leaf_size = [](const transaction::cptr& tx) NOEXCEPT
×
470
    {
471
        return tx->serialized_size(false) == two * hash_size;
×
472
    };
473

UNCOV
474
    return std::all_of(txs_->begin(), txs_->end(), two_leaf_size);
×
475
}
476

477
bool block::is_segregated() const NOEXCEPT
×
478
{
479
    const auto segregated = [](const transaction::cptr& tx) NOEXCEPT
×
480
    {
481
        return tx->is_segregated();
×
482
    };
483

484
    return std::any_of(txs_->begin(), txs_->end(), segregated);
×
485
}
486

487
bool block::is_invalid_witness_commitment() const NOEXCEPT
×
488
{
489
    if (txs_->empty())
×
490
        return false;
491

492
    const auto& coinbase = *txs_->front();
×
493
    if (coinbase.inputs_ptr()->empty())
×
494
        return false;
495

496
    // If there is a valid commitment, return false (valid).
497
    // Last output of commitment pattern holds committed value (bip141).
498
    hash_digest reserved{}, committed{};
×
499
    if (coinbase.inputs_ptr()->front()->reserved_hash(reserved))
×
500
        for (const auto& output: views_reverse(*coinbase.outputs_ptr()))
×
501
            if (output->committed_hash(committed))
×
502
                if (committed == sha256::double_hash(
×
503
                    generate_merkle_root(true), reserved))
×
504
                    return false;
505
    
506
    // If no valid commitment, return true (invalid) if segregated.
507
    // If no block tx has witness data the commitment is optional (bip141).
508
    return is_segregated();
×
509
}
510

511
//*****************************************************************************
512
// CONSENSUS:
513
// bip42 compensates for C++ undefined behavior of a right shift of a number of
514
// bits greater or equal to the shifted integer width. Yet being undefined, the
515
// result of this operation may vary by compiler. The shift_right call below
516
// explicitly implements presumed pre-bip42 behavior (shift overflow modulo) by
517
// default, and specified bip42 behavior (shift overflow to zero) with bip42.
518
//*****************************************************************************
519
static uint64_t block_subsidy(size_t height, uint64_t subsidy_interval,
×
520
    uint64_t initial_block_subsidy_satoshi, bool bip42) NOEXCEPT
521
{
522
    // Guard: quotient domain cannot increase with positive integer divisor.
523
    const auto halves = possible_narrow_cast<size_t>(height / subsidy_interval);
×
524
    return shift_right(initial_block_subsidy_satoshi, halves, bip42);
×
525
}
526

527
// Prevouts required.
528

529
uint64_t block::fees() const NOEXCEPT
×
530
{
531
    // Overflow returns max_uint64.
532
    const auto value = [](uint64_t total, const transaction::cptr& tx) NOEXCEPT
×
533
    {
534
        return ceilinged_add(total, tx->fee());
×
535
    };
536

537
    return std::accumulate(txs_->begin(), txs_->end(), uint64_t{0}, value);
×
538
}
539

540
uint64_t block::claim() const NOEXCEPT
×
541
{
542
    return txs_->empty() ? zero : txs_->front()->value();
×
543
}
544

545
uint64_t block::reward(size_t height, uint64_t subsidy_interval,
×
546
    uint64_t initial_block_subsidy_satoshi, bool bip42) const NOEXCEPT
547
{
548
    // Overflow returns max_uint64.
549
    return ceilinged_add(fees(), block_subsidy(height, subsidy_interval,
×
550
        initial_block_subsidy_satoshi, bip42));
×
551
}
552

553
bool block::is_overspent(size_t height, uint64_t subsidy_interval,
×
554
    uint64_t initial_block_subsidy_satoshi, bool bip42) const NOEXCEPT
555
{
556
    return claim() > reward(height, subsidy_interval,
×
557
        initial_block_subsidy_satoshi, bip42);
×
558
}
559

560
size_t block::signature_operations(bool bip16, bool bip141) const NOEXCEPT
×
561
{
562
    // Overflow returns max_size_t.
563
    const auto value = [=](size_t total, const transaction::cptr& tx) NOEXCEPT
×
564
    {
565
        return ceilinged_add(total, tx->signature_operations(bip16, bip141));
×
566
    };
×
567

568
    return std::accumulate(txs_->begin(), txs_->end(), zero, value);
×
569
}
570

571
bool block::is_signature_operations_limited(bool bip16,
×
572
    bool bip141) const NOEXCEPT
573
{
574
    const auto limit = bip141 ? max_fast_sigops : max_block_sigops;
×
575
    return signature_operations(bip16, bip141) > limit;
×
576
}
577

578
//*****************************************************************************
579
// CONSENSUS:
580
// This check is excluded under two bip30 exception blocks and bip30_deactivate
581
// until bip30_reactivate. These conditions are rolled up into the bip30 flag.
582
//*****************************************************************************
583
bool block::is_unspent_coinbase_collision() const NOEXCEPT
×
584
{
585
    if (txs_->empty() || txs_->front()->inputs_ptr()->empty())
×
586
        return false;
×
587

588
    // May only commit duplicate coinbase that is already confirmed spent.
589
    // Metadata population defaults coinbase to spent (not a collision).
590
    return !txs_->front()->inputs_ptr()->front()->metadata.spent;
×
591
}
592

593
// Search is not ordered, forward references are caught by block.check.
594
void block::populate() const NOEXCEPT
×
595
{
596
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
597
    std::unordered_map<point, output::cptr> points{};
×
598
    uint32_t index{};
×
599

600
    // Populate outputs hash table.
601
    for (auto tx = txs_->begin(); tx != txs_->end(); ++tx, index = 0)
×
602
        for (const auto& out: *(*tx)->outputs_ptr())
×
603
            points.emplace(point{ (*tx)->hash(false), index++ }, out);
×
604

605
    // Populate input prevouts from hash table.
606
    for (auto tx = txs_->begin(); tx != txs_->end(); ++tx)
×
607
    {
608
        for (const auto& in: *(*tx)->inputs_ptr())
×
609
        {
610
            const auto point = points.find(in->point());
×
611
            if (point != points.end())
×
612
                in->prevout = point->second;
×
613
        }
614
    }
615

616
    BC_POP_WARNING()
617
}
×
618

619
// Delegated.
620
// ----------------------------------------------------------------------------
621

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

631
    return error::block_success;
×
632
}
633

634
// DO invoke on coinbase.
635
code block::check_transactions(const context& ctx) const NOEXCEPT
×
636
{
637
    code ec;
×
638
    
639
    for (const auto& tx: *txs_)
×
640
        if ((ec = tx->check(ctx)))
×
641
            return ec;
×
642

643
    return error::block_success;
×
644
}
645

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

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

656
    return error::block_success;
×
657
}
658

659
// Do NOT invoke on coinbase.
660
code block::connect_transactions(const context& ctx) const NOEXCEPT
×
661
{
662
    code ec;
×
663

664
    if (!is_empty())
×
665
        for (auto tx = std::next(txs_->begin()); tx != txs_->end(); ++tx)
×
666
            if ((ec = (*tx)->connect(ctx)))
×
667
                return ec;
×
668

669
    return error::block_success;
×
670
}
671

672
// Do NOT invoke on coinbase.
673
code block::confirm_transactions(const context& ctx) const NOEXCEPT
×
674
{
675
    code ec;
×
676

677
    if (!is_empty())
×
678
        for (auto tx = std::next(txs_->begin()); tx != txs_->end(); ++tx)
×
679
            if ((ec = (*tx)->confirm(ctx)))
×
680
                return ec;
×
681

682
    return error::block_success;
×
683
}
684

685
// Validation.
686
// ----------------------------------------------------------------------------
687
// The block header is checked/accepted independently.
688

689
code block::check() const NOEXCEPT
×
690
{
691
    // context free.
692
    // empty_block is redundant with first_not_coinbase.
693
    //if (is_empty())
694
    //    return error::empty_block;
695
    if (is_oversized())
×
696
        return error::block_size_limit;
×
697
    if (is_first_non_coinbase())
×
698
        return error::first_not_coinbase;
×
699
    if (is_extra_coinbases())
×
700
        return error::extra_coinbases;
×
701
    if (is_forward_reference())
×
702
        return error::forward_reference;
×
703
    if (is_internal_double_spend())
×
704
        return error::block_internal_double_spend;
×
705
    if (is_invalid_merkle_root())
×
706
        return error::merkle_mismatch;
×
707

708
    return check_transactions();
×
709
}
710

711
// forks
712
// height
713
// timestamp
714
// median_time_past
715

716
code block::check(const context& ctx) const NOEXCEPT
×
717
{
718
    const auto bip34 = ctx.is_enabled(bip34_rule);
×
719
    const auto bip50 = ctx.is_enabled(bip50_rule);
×
720
    const auto bip141 = ctx.is_enabled(bip141_rule);
×
721

722
    // context required.
723
    if (bip141 && is_overweight())
×
724
        return error::block_weight_limit;
×
725
    if (bip34 && is_invalid_coinbase_script(ctx.height))
×
726
        return error::coinbase_height_mismatch;
×
727
    if (bip50 && is_hash_limit_exceeded())
×
728
        return error::temporary_hash_limit;
×
729
    if (bip141 && is_invalid_witness_commitment())
×
730
        return error::invalid_witness_commitment;
×
731

732
    return check_transactions(ctx);
×
733
}
734

735
// forks
736
// height
737

738
// This assumes that prevout caching is completed on all inputs.
739
code block::accept(const context& ctx, size_t subsidy_interval,
×
740
    uint64_t initial_subsidy) const NOEXCEPT
741
{
742
    const auto bip16 = ctx.is_enabled(bip16_rule);
×
743
    const auto bip42 = ctx.is_enabled(bip42_rule);
×
744
    const auto bip141 = ctx.is_enabled(bip141_rule);
×
745

746
    // prevouts required.
747
    if (is_overspent(ctx.height, subsidy_interval, initial_subsidy, bip42))
×
748
        return error::coinbase_value_limit;
×
749
    if (is_signature_operations_limited(bip16, bip141))
×
750
        return error::block_sigop_limit;
×
751

752
    return accept_transactions(ctx);
×
753
}
754

755
// forks
756

757
// Node performs these checks through database query.
758
// This assumes that prevout and metadata caching are completed on all inputs.
759
code block::confirm(const context& ctx) const NOEXCEPT
×
760
{
761
    const auto bip30 = ctx.is_enabled(bip30_rule);
×
762

763
    if (bip30 && is_unspent_coinbase_collision())
×
764
        return error::unspent_coinbase_collision;
×
765

766
    return confirm_transactions(ctx);
×
767
}
768

769
// forks
770

771
code block::connect(const context& ctx) const NOEXCEPT
×
772
{
773
    return connect_transactions(ctx);
×
774
}
775

776
// JSON value convertors.
777
// ----------------------------------------------------------------------------
778

779
namespace json = boost::json;
780

781
// boost/json will soon have NOEXCEPT: github.com/boostorg/json/pull/636
782
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
783

784
block tag_invoke(json::value_to_tag<block>,
1✔
785
    const json::value& value) NOEXCEPT
786
{
787
    return
1✔
788
    {
789
        json::value_to<header>(value.at("header")),
1✔
790
        json::value_to<chain::transactions>(value.at("transactions"))
2✔
791
    };
1✔
792
}
793

794
void tag_invoke(json::value_from_tag, json::value& value,
2✔
795
    const block& block) NOEXCEPT
796
{
797
    value =
2✔
798
    {
799
        { "header", block.header() },
800
        { "transactions", *block.transactions_ptr() },
801
    };
2✔
802
}
2✔
803

804
BC_POP_WARNING()
805

806
block::cptr tag_invoke(json::value_to_tag<block::cptr>,
×
807
    const json::value& value) NOEXCEPT
808
{
809
    return to_shared(tag_invoke(json::value_to_tag<block>{}, value));
×
810
}
811

812
// Shared pointer overload is required for navigation.
813
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
814
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
815

816
void tag_invoke(json::value_from_tag tag, json::value& value,
×
817
    const block::cptr& block) NOEXCEPT
818
{
819
    tag_invoke(tag, value, *block);
×
820
}
×
821

822
BC_POP_WARNING()
823
BC_POP_WARNING()
824

825
} // namespace chain
826
} // namespace system
827
} // 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