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

libbitcoin / libbitcoin-system / 4586521533

pending completion
4586521533

Pull #1351

github

GitHub
Merge 43395b060 into af2a5a44b
Pull Request #1351: Fix sign inversion (regression) in operation::opcode_to_positive.

15 of 15 new or added lines in 6 files covered. (100.0%)

9441 of 11344 relevant lines covered (83.22%)

6731300.01 hits per line

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

45.32
/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_set>
28
#include <utility>
29
#include <unordered_map>
30
#include <bitcoin/system/chain/context.hpp>
31
#include <bitcoin/system/chain/enums/forks.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/math/math.hpp>
40
#include <bitcoin/system/settings.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
56✔
51
  : block(to_shared<chain::header>(), to_shared<chain::transaction_cptrs>(),
56✔
52
      false)
168✔
53
{
54
}
56✔
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)
44✔
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)
20✔
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
66✔
75
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
76
  : block(stream::in::copy(data), witness)
66✔
77
    BC_POP_WARNING()
78
{
79
}
66✔
80

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

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

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

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

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

108
// Operators.
109
// ----------------------------------------------------------------------------
110

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

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

122
// Deserialization.
123
// ----------------------------------------------------------------------------
124

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

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

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

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

154
// Serialization.
155
// ----------------------------------------------------------------------------
156

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

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

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

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

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

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

184
// Properties.
185
// ----------------------------------------------------------------------------
186

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

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

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

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

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

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

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

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

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

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

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

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

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

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

264
// Connect.
265
// ----------------------------------------------------------------------------
266

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

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

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

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

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

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

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

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

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

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

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

335
    return false;
4✔
336
}
337

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

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

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
10✔
370
{
371
    return sha256::merkle_root(transaction_hashes(witness));
10✔
372
}
373

374
bool block::is_invalid_merkle_root() const NOEXCEPT
10✔
375
{
376
    return generate_merkle_root(false) != header_->merkle_root();
10✔
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
bool block::is_segregated() const NOEXCEPT
×
442
{
443
    const auto segregated = [](const transaction::cptr& tx) NOEXCEPT
×
444
    {
445
        return tx->is_segregated();
×
446
    };
447

448
    return std::any_of(txs_->begin(), txs_->end(), segregated);
×
449
}
450

451
bool block::is_invalid_witness_commitment() const NOEXCEPT
×
452
{
453
    if (txs_->empty() || txs_->front()->inputs_ptr()->empty())
×
454
        return false;
×
455

456
    hash_digest reserved{}, committed{};
×
457
    const auto& coinbase = txs_->front();
×
458

459
    // Last output of commitment pattern holds committed value (bip141).
460
    if (coinbase->inputs_ptr()->front()->reserved_hash(reserved))
×
461
    {
462
        const auto& outputs = *coinbase->outputs_ptr();
×
463

464
        for (const auto& output: views_reverse(outputs))
×
465
        {
466
            if (output->committed_hash(committed))
×
467
            {
468
                return committed == sha256::double_hash(
×
469
                    generate_merkle_root(true), reserved);
×
470
            }
471
        }
472
    }
473
    
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(size_t height) const NOEXCEPT
×
553
{
554
    if (txs_->empty() || txs_->front()->inputs_ptr()->empty())
×
555
        return false;
×
556

557
    const auto& prevout = txs_->front()->inputs_ptr()->front()->metadata;
×
558

559
    // This requires that prevout.spent was populated for the height of the
560
    // validating block, otherwise a collision (unspent) must be assumed.
561
    return !(height > prevout.height && prevout.spent);
×
562
}
563

564
// Delegated.
565
// ----------------------------------------------------------------------------
566

567
code block::check_transactions() const NOEXCEPT
×
568
{
569
    code ec;
×
570

571
    for (auto tx = std::next(txs_->begin()); tx != txs_->end(); ++tx)
×
572
        if ((ec = (*tx)->check()))
×
573
            return ec;
×
574

575
    return error::block_success;
×
576
}
577

578
code block::accept_transactions(const context& ctx) const NOEXCEPT
×
579
{
580
    code ec;
×
581

582
    for (const auto& tx: *txs_)
×
583
        if ((ec = tx->accept(ctx)))
×
584
            return ec;
×
585

586
    return error::block_success;
×
587
}
588

589
code block::connect_transactions(const context& ctx) const NOEXCEPT
×
590
{
591
    code ec;
×
592

593
    if (!is_empty())
×
594
        for (auto tx = std::next(txs_->begin()); tx != txs_->end(); ++tx)
×
595
            if ((ec = (*tx)->connect(ctx)))
×
596
                return ec;
×
597

598
    return error::block_success;
×
599
}
600

601
// Validation.
602
// ----------------------------------------------------------------------------
603

604
// The block header is checked independently.
605
// These checks are self-contained; blockchain (and so version) independent.
606
code block::check() const NOEXCEPT
×
607
{
608
    // Inputs and outputs are required.
609
    if (is_empty())
×
610
        return error::empty_block;
×
611

612
    // Relates to total of tx.size (pool cache tx.size(false)).
613
    if (is_oversized())
×
614
        return error::block_size_limit;
×
615

616
    // The first transaction must be coinbase.
617
    if (is_first_non_coinbase())
×
618
        return error::first_not_coinbase;
×
619

620
    // Only the first transaction may be coinbase.
621
    if (is_extra_coinbases())
×
622
        return error::extra_coinbases;
×
623

624
    // Determinable from tx pool graph.
625
    // Satoshi implementation side effect, as tx order is otherwise irrelevant.
626
    if (is_forward_reference())
×
627
        return error::forward_reference;
×
628

629
    // Determinable from tx pool graph.
630
    // This also precludes the block merkle calculation DoS exploit.
631
    // bitcointalk.org/?topic=102395
632
    if (is_internal_double_spend())
×
633
        return error::block_internal_double_spend;
×
634

635
    // TODO: defer to accept (doubles merkle root computation past bip141).
636
    // Relates height to tx.hash (pool cache tx.hash(false)).
637
    if (is_invalid_merkle_root())
×
638
        return error::merkle_mismatch;
×
639

640
    // error::empty_transaction
641
    // error::invalid_coinbase_script_size
642
    // error::previous_output_null
643
    return check_transactions();
×
644
}
645

646
// The block header is accepted independently.
647
// These checks assume that prevout caching is completed on all tx.inputs.
648
code block::accept(const context& ctx, size_t subsidy_interval,
×
649
    uint64_t initial_subsidy) const NOEXCEPT
650
{
651
    const auto bip16 = ctx.is_enabled(bip16_rule);
×
652
    const auto bip30 = ctx.is_enabled(bip30_rule);
×
653
    const auto bip34 = ctx.is_enabled(bip34_rule);
×
654
    const auto bip42 = ctx.is_enabled(bip42_rule);
×
655
    const auto bip50 = ctx.is_enabled(bip50_rule);
×
656
    const auto bip141 = ctx.is_enabled(bip141_rule);
×
657

658
    // Relates block limit to total of tx.weight (pool cache tx.size(t/f)).
659
    if (bip141 && is_overweight())
×
660
        return error::block_weight_limit;
×
661

662
    // Relates block height to coinbase, always under checkpoint.
663
    if (bip34 && is_invalid_coinbase_script(ctx.height))
×
664
        return error::coinbase_height_mismatch;
×
665

666
    // Relates block time to tx and prevout hashes, always under checkpoint.
667
    if (bip50 && is_hash_limit_exceeded())
×
668
        return error::temporary_hash_limit;
×
669

670
    // Static check but requires context.
671
    if (bip141 && is_invalid_witness_commitment())
×
672
        return error::invalid_witness_commitment;
×
673

674
    // prevouts required
675

676
    // Relates block height to total of tx.fee (pool cache tx.fee).
677
    if (is_overspent(ctx.height, subsidy_interval, initial_subsidy, bip42))
×
678
        return error::coinbase_value_limit;
×
679

680
    // Relates block limit to total of tx.sigops (pool cache tx.sigops).
681
    if (is_signature_operations_limited(bip16, bip141))
×
682
        return error::block_sigop_limit;
×
683

684
    // prevout confirmation state required
685

686
    if (bip30 && !bip34 && is_unspent_coinbase_collision(ctx.height))
×
687
        return error::unspent_coinbase_collision;
×
688

689
    // error::transaction_non_final
690
    // error::missing_previous_output
691
    // error::spend_exceeds_value
692
    // error::coinbase_maturity
693
    // error::relative_time_locked
694
    // error::unconfirmed_spend
695
    // error::confirmed_double_spend
696
    return accept_transactions(ctx);
×
697
}
698

699
code block::connect(const context& ctx) const NOEXCEPT
×
700
{
701
    return connect_transactions(ctx);
×
702
}
703

704
// JSON value convertors.
705
// ----------------------------------------------------------------------------
706

707
namespace json = boost::json;
708

709
// boost/json will soon have NOEXCEPT: github.com/boostorg/json/pull/636
710
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
711

712
block tag_invoke(json::value_to_tag<block>,
1✔
713
    const json::value& value) NOEXCEPT
714
{
715
    return
1✔
716
    {
717
        json::value_to<header>(value.at("header")),
1✔
718
        json::value_to<chain::transactions>(value.at("transactions"))
2✔
719
    };
1✔
720
}
721

722
void tag_invoke(json::value_from_tag, json::value& value,
2✔
723
    const block& block) NOEXCEPT
724
{
725
    value =
2✔
726
    {
727
        { "header", block.header() },
728
        { "transactions", *block.transactions_ptr() },
2✔
729
    };
2✔
730
}
2✔
731

732
BC_POP_WARNING()
733

734
block::cptr tag_invoke(json::value_to_tag<block::cptr>,
×
735
    const json::value& value) NOEXCEPT
736
{
737
    return to_shared(tag_invoke(json::value_to_tag<block>{}, value));
×
738
}
739

740
// Shared pointer overload is required for navigation.
741
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
742
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
743

744
void tag_invoke(json::value_from_tag tag, json::value& value,
×
745
    const block::cptr& block) NOEXCEPT
746
{
747
    tag_invoke(tag, value, *block);
×
748
}
×
749

750
BC_POP_WARNING()
751
BC_POP_WARNING()
752

753
} // namespace chain
754
} // namespace system
755
} // 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