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

libbitcoin / libbitcoin-system / 8473791609

28 Mar 2024 09:16PM UTC coverage: 82.724% (+0.06%) from 82.668%
8473791609

push

github

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

Refactor forks, incorporate bip30_deactivate/activate forks.

111 of 215 new or added lines in 4 files covered. (51.63%)

6 existing lines in 1 file now uncovered.

9706 of 11733 relevant lines covered (82.72%)

4861747.91 hits per line

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

11.37
/src/chain/chain_state.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/chain_state.hpp>
20

21
#include <algorithm>
22
#include <chrono>
23
#include <iterator>
24
#include <bitcoin/system/chain/block.hpp>
25
#include <bitcoin/system/chain/chain_state.hpp>
26
#include <bitcoin/system/chain/checkpoint.hpp>
27
#include <bitcoin/system/chain/compact.hpp>
28
#include <bitcoin/system/chain/context.hpp>
29
#include <bitcoin/system/chain/enums/forks.hpp>
30
#include <bitcoin/system/chain/enums/policy.hpp>
31
#include <bitcoin/system/chain/script.hpp>
32
#include <bitcoin/system/data/data.hpp>
33
#include <bitcoin/system/hash/hash.hpp>
34
#include <bitcoin/system/math/math.hpp>
35
#include <bitcoin/system/settings.hpp>
36

37
namespace libbitcoin {
38
namespace system {
39
namespace chain {
40

41
// github.com/bitcoin/bips/blob/master/bip-0030.mediawiki#specification
42
// As bip30 exceptions apply only to bitcoin mainnet these can be embedded.
43
static const checkpoint mainnet_bip30_exception_checkpoint1
44
{
45
    "00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec", 91842
46
};
47
static const checkpoint mainnet_bip30_exception_checkpoint2
48
{
49
    "00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721", 91880
50
};
51

52
// Inlines.
53
// ----------------------------------------------------------------------------
54

55
constexpr bool is_active(size_t count, size_t bip34_activation_threshold) NOEXCEPT
56
{
57
    return count >= bip34_activation_threshold;
58
}
59

60
constexpr bool is_enforced(size_t count, size_t bip34_enforcement_threshold) NOEXCEPT
61
{
62
    return count >= bip34_enforcement_threshold;
63
}
64

65
// Determine the number of blocks back to the closest retarget height.
66
constexpr size_t retarget_distance(size_t height,
2✔
67
    size_t retargeting_interval) NOEXCEPT
68
{
69
    return height % retargeting_interval;
2✔
70
}
71

72
// Determine if height is a multiple of retargeting_interval.
73
constexpr bool is_retarget_height(size_t height,
2✔
74
    size_t retargeting_interval) NOEXCEPT
75
{
76
    return is_zero(retarget_distance(height, retargeting_interval));
2✔
77
}
78

79
// bip30 is active for all but two mainnet blocks that violate the rule.
80
// These two blocks each have a coinbase transaction that exactly duplicates
81
// another that is not spent by the arrival of the corresponding duplicate.
82
// No need to check the configuration for mainnet (hashes presumed unique).
83
inline bool is_bip30_exception(const checkpoint& check) NOEXCEPT
×
84
{
85
    return (check == mainnet_bip30_exception_checkpoint1) ||
×
86
         (check == mainnet_bip30_exception_checkpoint2);
×
87
}
88

89
inline uint32_t timestamp_high(const chain_state::data& values) NOEXCEPT
2✔
90
{
91
    return values.timestamp.ordered.back();
2✔
92
}
93

94
inline uint32_t bits_high(const chain_state::data& values) NOEXCEPT
4✔
95
{
96
    return values.bits.ordered.back();
4✔
97
}
98

99
// activation
100
// ----------------------------------------------------------------------------
101

102
chain_state::activations chain_state::activation(const data& values,
×
103
    const forks_t& forks, const system::settings& settings) NOEXCEPT
104
{
105
    // Initialize activation results with genesis values.
NEW
106
    activations result{ forks::no_rules, settings.first_version };
×
107

108
    // regtest is only activated via configuration.
NEW
109
    if (forks.retarget)
×
110
    {
NEW
111
        result.flags |= forks::retarget;
×
112
    }
113

114
    // testnet is activated based on configuration alone.
NEW
115
    if (forks.difficult)
×
116
    {
NEW
117
        result.flags |= forks::difficult;
×
118
    }
119

120
    // time_warp_patch is activated based on configuration alone.
NEW
121
    if (forks.time_warp_patch)
×
122
    {
NEW
123
        result.flags |= forks::time_warp_patch;
×
124
    }
125

126
    // retarget_overflow_patch is activated based on configuration alone.
NEW
127
    if (forks.retarget_overflow_patch)
×
128
    {
NEW
129
        result.flags |= forks::retarget_overflow_patch;
×
130
    }
131

132
    // scrypt_proof_of_work is activated based on configuration alone.
NEW
133
    if (forks.scrypt_proof_of_work)
×
134
    {
NEW
135
        result.flags |= forks::scrypt_proof_of_work;
×
136
    }
137

138
    // bip42 is activated based on configuration alone (soft fork).
NEW
139
    if (forks.bip42)
×
140
    {
NEW
141
        result.flags |= forks::bip42_rule;
×
142
    }
143

144
    // bip90 is activated based on configuration alone (hard fork).
NEW
145
    if (forks.bip90)
×
146
    {
NEW
147
        result.flags |= forks::bip90_rule;
×
148
    }
149

150
    // bip16 was activated by manual inspection of signal history (soft fork).
NEW
151
    if (forks.bip16 &&
×
NEW
152
        (values.timestamp.self >= settings.bip16_activation_time))
×
153
    {
NEW
154
        result.flags |= forks::bip16_rule;
×
155
    }
156

157
    const auto height = values.height;
×
158
    const auto version = values.version.self;
×
159
    const auto& history = values.version.ordered;
×
160

161
    //*************************************************************************
162
    // CONSENSUS: Though unspecified in bip34, the satoshi implementation
163
    // performed this comparison using the signed integer version value.
164
    //*************************************************************************
165
    constexpr auto ge = [](uint32_t value, uint32_t version) NOEXCEPT
×
166
    {
167
        return sign_cast<int32_t>(value) >= sign_cast<int32_t>(version);
×
168
    };
169

170
    // Declare bip34-based version predicates.
NEW
171
    const auto ge_2 = [&](uint32_t value) NOEXCEPT
×
NEW
172
        { return ge(value, settings.bip34_version); };
×
NEW
173
    const auto ge_3 = [&](uint32_t value) NOEXCEPT
×
NEW
174
        { return ge(value, settings.bip66_version); };
×
NEW
175
    const auto ge_4 = [&](uint32_t value) NOEXCEPT
×
NEW
176
        { return ge(value, settings.bip65_version); };
×
177

178
    // Compute bip34-based activation version summaries (empty if disabled).
179
    const auto count_2 = std::count_if(history.begin(), history.end(), ge_2);
×
180
    const auto count_3 = std::count_if(history.begin(), history.end(), ge_3);
×
181
    const auto count_4 = std::count_if(history.begin(), history.end(), ge_4);
×
182

183
    // Frozen activations (require version and enforce above freeze height).
NEW
184
    const auto bip90_bip34 = forks.bip90 && height >= settings.bip90_bip34_height;
×
NEW
185
    const auto bip90_bip65 = forks.bip90 && height >= settings.bip90_bip65_height;
×
NEW
186
    const auto bip90_bip66 = forks.bip90 && height >= settings.bip90_bip66_height;
×
187

188
    // bip34 activations oscillate until enforced by minimum_block_version.
189
    // bip90 does not require that the corresponding rules be defined.
190
    // bip34 is active based on 75% of preceding 1000 mainnet blocks.
NEW
191
    if (bip90_bip34 ||
×
NEW
192
        (is_active(count_2, settings.bip34_activation_threshold) &&
×
UNCOV
193
        version >= settings.bip34_version))
×
194
    {
NEW
195
        result.flags |= forks::bip34_rule;
×
196
    }
197

198
    // bip66 is active based on 75% of preceding 1000 mainnet blocks.
NEW
199
    if (bip90_bip66 ||
×
NEW
200
        (is_active(count_3, settings.bip34_activation_threshold) &&
×
UNCOV
201
        version >= settings.bip66_version))
×
202
    {
NEW
203
        result.flags |= forks::bip66_rule;
×
204
    }
205

206
    // bip65 is active based on 75% of preceding 1000 mainnet blocks.
NEW
207
    if (bip90_bip65 ||
×
NEW
208
        (is_active(count_4, settings.bip34_activation_threshold) &&
×
UNCOV
209
        version >= settings.bip65_version))
×
210
    {
NEW
211
        result.flags |= forks::bip65_rule;
×
212
    }
213

214
    // version 4/3/2 enforced based on 95% of preceding 1000 mainnet blocks.
NEW
215
    if (bip90_bip65 ||
×
NEW
216
        is_enforced(count_4, settings.bip34_enforcement_threshold))
×
217
    {
218
        result.minimum_block_version = settings.bip65_version;
×
219
    }
NEW
220
    else if (bip90_bip66 ||
×
NEW
221
        is_enforced(count_3, settings.bip34_enforcement_threshold))
×
222
    {
223
        result.minimum_block_version = settings.bip66_version;
×
224
    }
NEW
225
    else if (bip90_bip34 ||
×
NEW
226
        is_enforced(count_2, settings.bip34_enforcement_threshold))
×
227
    {
228
        result.minimum_block_version = settings.bip34_version;
×
229
    }
230
    else
231
    {
232
        result.minimum_block_version = settings.first_version;
233
    }
234

235
    // TODO: bip9 historical activation.
236

237
    // bip9_bit0 forks are enforced above the bip9_bit0 checkpoint.
238
    if (values.bip9_bit0_hash == settings.bip9_bit0_active_checkpoint.hash())
×
239
    {
NEW
240
        result.flags |= forks::bip68_rule;
×
NEW
241
        result.flags |= forks::bip112_rule;
×
NEW
242
        result.flags |= forks::bip113_rule;
×
243
    }
244

245
    // bip9_bit1 forks are enforced above the bip9_bit1 checkpoint.
246
    if (values.bip9_bit1_hash == settings.bip9_bit1_active_checkpoint.hash())
×
247
    {
NEW
248
        result.flags |= forks::bip141_rule;
×
NEW
249
        result.flags |= forks::bip143_rule;
×
NEW
250
        result.flags |= forks::bip147_rule;
×
251
    }
252

253
    // bip30_deactivate fork enforced above bip30_deactivate (bip34) checkpoint.
NEW
254
    const auto bip30_deactivate = forks.bip30 && forks.bip30_deactivate &&
×
NEW
255
        (values.bip30_deactivate_hash ==
×
NEW
256
            settings.bip30_deactivate_checkpoint.hash());
×
257

258
    // bip30_reactivate fork is enforced above the bip30_reactivate height.
NEW
259
    const auto bip30_reactivate = bip30_deactivate && forks.bip30_reactivate &&
×
NEW
260
        (height >= settings.bip30_reactivate_height);
×
261

262
    // bip30 is disabled by bip30_deactivate and reenabled by bip30_reactivate.
263
    // Otherwise if not exception, existing duplicate coinbase must be spent.
NEW
264
    if (forks.bip30 && (!bip30_deactivate || bip30_reactivate) &&
×
NEW
265
        !is_bip30_exception({ values.hash, height }))
×
266
    {
NEW
267
        result.flags |= forks::bip30_rule;
×
268
    }
269

270
    return result;
×
271
}
272

NEW
273
size_t chain_state::bits_count(size_t height, const forks_t& forks,
×
274
    size_t retargeting_interval) NOEXCEPT
275
{
276
    // Mainnet doesn't use bits in retargeting.
NEW
277
    if (forks.difficult)
×
278
        return one;
279

280
    // Regtest bypasses all retargeting.
NEW
281
    if (!forks.retarget)
×
282
        return one;
283

284
    // Testnet uses mainnet retargeting on interval.
285
    if (is_retarget_height(height, retargeting_interval))
×
286
        return one;
287

288
    // Testnet requires all bits for inter-interval retargeting.
289
    return std::min(height, retargeting_interval);
×
290
}
291

NEW
292
size_t chain_state::version_count(size_t height, const forks_t& forks,
×
293
    size_t bip34_activation_sample) NOEXCEPT
294
{
NEW
295
    if (forks.bip90 || (!forks.bip34 && !forks.bip65 && !forks.bip66))
×
296
        return zero;
297

UNCOV
298
    return std::min(height, bip34_activation_sample);
×
299
}
300

NEW
301
size_t chain_state::timestamp_count(size_t height, const forks_t&) NOEXCEPT
×
302
{
303
    return std::min(height, median_time_past_interval);
×
304
}
305

NEW
306
size_t chain_state::retarget_height(size_t height, const forks_t& forks,
×
307
    size_t retargeting_interval) NOEXCEPT
308
{
NEW
309
    if (!forks.retarget)
×
310
        return map::unrequested;
311

312
    // Height must be a positive multiple of interval, so underflow safe.
313
    // If not retarget height get most recent so that it may be promoted.
314
    return height - (is_retarget_height(height, retargeting_interval) ?
×
315
        retargeting_interval : retarget_distance(height, retargeting_interval));
×
316
}
317

318
// median_time_past
319
// ----------------------------------------------------------------------------
320

321
//*****************************************************************************
322
// CONSENSUS: satoshi associates the median time past for block N with block
323
// N-1, as opposed to block N. Given that the value is actually obtained from
324
// yet another preceding block in all cases except block 1 and 2, this is a
325
// curious and confusing convention. We associate the median time past for
326
// block N with block N. This is simple but requires care when comparing code.
327
//*****************************************************************************
NEW
328
uint32_t chain_state::median_time_past(const data& values,
×
329
    const forks_t&) NOEXCEPT
330
{
331
    // Sort the times by value to obtain the median.
332
    auto times = sort_copy(values.timestamp.ordered);
×
333

334
    // Consensus defines median time using modulo 2 element selection.
335
    // This differs from arithmetic median which averages two middle values.
336
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
337
    return times.empty() ? 0 : times.at(to_half(times.size()));
×
338
    BC_POP_WARNING()
339
}
×
340

341
// work_required
342
// ----------------------------------------------------------------------------
343

344
uint32_t chain_state::work_required(const data& values, const forks_t& forks,
2✔
345
    const system::settings& settings) NOEXCEPT
346
{
347
    // Genesis has no preceding block data.
348
    if (is_zero(values.height))
2✔
349
        return 0;
350

351
    // Previous block has an invalid bits value.
352
    if (is_zero(compact::expand(bits_high(values))))
2✔
353
        return 0;
354

355
    // Regtest bypasses all retargeting.
356
    if (!forks.retarget)
2✔
357
        return bits_high(values);
×
358

359
    // Mainnet and testnet retarget on interval.
360
    if (is_retarget_height(values.height, settings.retargeting_interval()))
2✔
361
        return work_required_retarget(values, forks,
6✔
362
            settings.proof_of_work_limit,
2✔
363
            settings.minimum_timespan(),
2✔
364
            settings.maximum_timespan(),
2✔
365
            settings.retargeting_interval_seconds);
4✔
366

367
    // Testnet retargets easy on inter-interval.
NEW
368
    if (!forks.difficult)
×
369
        return easy_work_required(values,
×
370
            settings.retargeting_interval(),
×
371
            settings.proof_of_work_limit,
×
372
            settings.block_spacing_seconds);
×
373

374
    // Mainnet not retargeting, must exact match the previous block bits value.
375
    return bits_high(values);
×
376
}
377

378
// Get the bounded total time spanning the highest 2016 blocks.
379
uint32_t chain_state::retarget_timespan(const data& values,
2✔
380
    uint32_t minimum_timespan, uint32_t maximum_timespan) NOEXCEPT
381
{
382
    //*************************************************************************
383
    // CONSENSUS: "Subtract unsigned 32 bit numbers in signed 64 bit space".
384
    // This is done order to prevent underflow before applying the range
385
    // constraint. This is properly just a floored subtraction in 32 bit space.
386
    //*************************************************************************
387
    const auto timespan = floored_subtract(timestamp_high(values),
2✔
388
        values.timestamp.retarget);
2✔
389

390
    //*************************************************************************
391
    // CONSENSUS: Constrain the timespan to the configured consensus limits.
392
    //*************************************************************************
393
    return limit(timespan, minimum_timespan, maximum_timespan);
2✔
394
}
395

396
constexpr bool patch_timewarp(const chain_state::forks_t& forks,
2✔
397
    const uint256_t& limit, const uint256_t& target) NOEXCEPT
398
{
399
    return forks.retarget_overflow_patch &&
3✔
400
        floored_log2(target) >= floored_log2(limit);
1✔
401
}
402

403
uint32_t chain_state::work_required_retarget(const data& values,
2✔
404
    const forks_t& forks, uint32_t proof_of_work_limit,
405
    uint32_t minimum_timespan, uint32_t maximum_timespan,
406
    uint32_t retargeting_interval_seconds) NOEXCEPT
407
{
408
    static const auto limit = compact::expand(proof_of_work_limit);
2✔
409
    auto target = compact::expand(bits_high(values));
2✔
410

411
    // Conditionally implement retarget overflow patch (e.g. Litecoin).
412
    const auto timewarp = to_int(patch_timewarp(forks, limit, target));
2✔
413

414
    target >>= timewarp;
2✔
415
    target *= retarget_timespan(values, minimum_timespan, maximum_timespan);
2✔
416
    target /= retargeting_interval_seconds;
2✔
417
    target <<= timewarp;
2✔
418

419
    // Disallow target from falling below minimum configured.
420
    // All targets are a bits value normalized by compress here.
421
    return target > limit ? proof_of_work_limit : compact::compress(target);
2✔
422
}
423

424
// A retarget height, or a block that does not have proof_of_work_limit bits.
425
constexpr bool is_retarget_or_non_limit(size_t height, uint32_t bits,
×
426
    size_t retargeting_interval, uint32_t proof_of_work_limit) NOEXCEPT
427
{
428
    // Zero is a retarget height, termination required before height underflow.
429
    // This is guaranteed, just a comment here because it may not be obvious.
430
    return bits != proof_of_work_limit ||
×
431
        is_retarget_height(height, retargeting_interval);
432
}
433

434
uint32_t chain_state::easy_work_required(const data& values,
×
435
    size_t retargeting_interval, uint32_t proof_of_work_limit,
436
    uint32_t block_spacing_seconds) NOEXCEPT
437
{
438
    BC_ASSERT(!is_zero(values.height));
×
439

440
    // Overflow allowed here since supported coins would not do so.
441
    const auto easy_spacing_seconds = shift_left(block_spacing_seconds);
×
442

443
    // If the time limit has passed allow a minimum difficulty block.
444
    if (values.timestamp.self > ceilinged_add(timestamp_high(values),
×
445
        easy_spacing_seconds))
446
        return proof_of_work_limit;
447

448
    auto height = values.height;
×
449
    const auto& bits = values.bits.ordered;
×
450

451
    // Reverse iterate the ordered-by-height list of header bits.
452
    for (auto bit: views_reverse(bits))
×
453
    {
454
        if (is_retarget_or_non_limit(--height, bit, retargeting_interval,
×
455
            proof_of_work_limit))
456
            return bit;
×
457
    }
458

459
    // Since the set of heights is either a full retarget range or ends at
460
    // zero this is not reachable unless the data set is invalid.
461
    BC_ASSERT(false);
×
462
    return proof_of_work_limit;
×
463
}
464

NEW
465
size_t chain_state::bip30_deactivate_height(size_t height,
×
466
    const checkpoint& bip30_deactivate_checkpoint) NOEXCEPT
467
{
NEW
468
    const auto activation_height = bip30_deactivate_checkpoint.height();
×
469

470
    // Require bip30_deactivate hash at heights at/above bip30_deactivate active.
NEW
471
    return height < activation_height ? map::unrequested : activation_height;
×
472
}
473

UNCOV
474
size_t chain_state::bip9_bit0_height(size_t height,
×
475
    const checkpoint& bip9_bit0_active_checkpoint) NOEXCEPT
476
{
477
    const auto activation_height = bip9_bit0_active_checkpoint.height();
×
478

479
    // Require bip9_bit0 hash at heights at/above bip9_bit0 active.
480
    return height < activation_height ? map::unrequested : activation_height;
×
481
}
482

483
size_t chain_state::bip9_bit1_height(size_t height,
×
484
    const checkpoint& bip9_bit1_active_checkpoint) NOEXCEPT
485
{
486
    const auto activation_height = bip9_bit1_active_checkpoint.height();
×
487

488
    // Require bip9_bit1 hash at heights at/above bip9_bit1 active.
489
    return height < activation_height ? map::unrequested : activation_height;
×
490
}
491

492
// Public static
493
// ----------------------------------------------------------------------------
494

495
chain_state::map chain_state::get_map(size_t height,
×
496
    const system::settings& settings) NOEXCEPT
497
{
498
    if (is_zero(height))
×
499
        return {};
×
500

NEW
501
    const auto& forks = settings.forks;
×
502
    const auto interval = settings.retargeting_interval();
×
503
    map map{};
×
504

505
    // The height bound of the reverse (high to low) retarget search.
506
    map.bits_self = height;
×
507
    map.bits.high = sub1(height);
×
508
    map.bits.count = bits_count(height, forks, interval);
×
509

510
    // The height bound of the median time past function.
511
    map.timestamp_self = height;
×
512
    map.timestamp.high = sub1(height);
×
513
    map.timestamp.count = timestamp_count(height, forks);
×
514

515
    // The height bound of the version sample for activations.
516
    map.version_self = height;
×
517
    map.version.high = sub1(height);
×
518
    map.version.count = version_count(height, forks,
×
519
        settings.bip34_activation_sample);
×
520

521
    // The most recent past retarget height.
522
    map.timestamp_retarget = retarget_height(height, forks, interval);
×
523

524
    // The checkpoint at/above which bip30_deactivate rule is enforced.
NEW
525
    if (forks.bip30 && forks.bip30_deactivate)
×
NEW
526
        map.bip30_deactivate_height = bip30_deactivate_height(height,
×
NEW
527
            settings.bip30_deactivate_checkpoint);
×
528

529
    // The checkpoint at/above which bip9_bit0 rules are enforced.
NEW
530
    if (forks.bip68 || forks.bip112 || forks.bip113)
×
NEW
531
        map.bip9_bit0_height = bip9_bit0_height(height,
×
NEW
532
            settings.bip9_bit0_active_checkpoint);
×
533

534
    // The checkpoint at/above which bip9_bit1 rules are enforced.
NEW
535
    if (forks.bip141 || forks.bip143 || forks.bip147)
×
NEW
536
        map.bip9_bit1_height = bip9_bit1_height(height,
×
NEW
537
            settings.bip9_bit1_active_checkpoint);
×
538

539
    return map;
×
540
}
541

NEW
542
uint32_t chain_state::signal_version(const system::settings& settings) NOEXCEPT
×
543
{
NEW
544
    const auto& forks = settings.forks;
×
545

546
    // TODO: these can be retired.
547
    // Signal bip9 bit1 if any of the group is configured.
NEW
548
    if (forks.bip141 || forks.bip143 || forks.bip147)
×
NEW
549
        return settings.bip9_version_base | settings.bip9_version_bit1;
×
550

551
    // TODO: these can be retired.
552
    // Signal bip9 bit0 if any of the group is configured.
NEW
553
    if (forks.bip68 || forks.bip112 || forks.bip113)
×
554
        return settings.bip9_version_base | settings.bip9_version_bit0;
×
555

NEW
556
    if (forks.bip65)
×
NEW
557
        return settings.bip65_version;
×
558

NEW
559
    if (forks.bip66)
×
NEW
560
        return settings.bip66_version;
×
561

NEW
562
    if (forks.bip34)
×
NEW
563
        return settings.bip34_version;
×
564

565
    return settings.first_version;
×
566
}
567

568
// Constructors.
569
// ----------------------------------------------------------------------------
570

571
// This is promotion from a preceding height to the next.
572
chain_state::data chain_state::to_pool(const chain_state& top,
×
573
    const system::settings& settings) NOEXCEPT
574
{
575
    // Alias configured forks.
NEW
576
    const auto& forks = top.forks_;
×
577

578
    // Copy data from presumed previous-height block state.
579
    chain_state::data data{ top.data_ };
×
580

581
    // If this overflows height is zero and result is handled as invalid.
582
    const auto height = add1(data.height);
×
583
    
584
    // Enqueue previous block values to collections.
585
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
586
    data.bits.ordered.push_back(data.bits.self);
×
587
    data.version.ordered.push_back(data.version.self);
×
588
    data.timestamp.ordered.push_back(data.timestamp.self);
×
589
    BC_POP_WARNING()
590

591
    // If bits collection overflows, dequeue oldest member.
592
    if (data.bits.ordered.size() >
×
593
        bits_count(height, forks, settings.retargeting_interval()))
×
594
        data.bits.ordered.pop_front();
×
595

596
    // If version collection overflows, dequeue oldest member.
597
    if (data.version.ordered.size() > version_count(height, forks,
×
598
        settings.bip34_activation_sample))
×
599
        data.version.ordered.pop_front();
×
600

601
    // If timestamp collection overflows, dequeue oldest member.
602
    if (data.timestamp.ordered.size() > timestamp_count(height, forks))
×
603
        data.timestamp.ordered.pop_front();
×
604

605
    // Regtest does not perform retargeting.
606
    // If promoting from retarget height, move that timestamp into retarget.
NEW
607
    if (forks.retarget && is_retarget_height(sub1(height),
×
608
        settings.retargeting_interval()))
×
609
    {
610
        // Conditionally patch time warp bug (e.g. Litecoin).
NEW
611
        data.timestamp.retarget = (forks.time_warp_patch && height != one) ?
×
UNCOV
612
            *std::next(data.timestamp.ordered.crbegin()) : data.timestamp.self;
×
613
    }
614

615
    // Replace previous block state with tx pool chain state for next height
616
    // Preserve top block timestamp for use in computation of staleness.
617
    // Preserve data.bip30_deactivate_hash promotion.
618
    // Preserve data.bip9_bit0_hash promotion.
619
    // Preserve data.bip9_bit1_hash promotion.
620
    // bits.self is unused.
621
    data.height = height;
×
622
    data.hash = {};
×
623
    data.bits.self = 0;
×
NEW
624
    data.version.self = signal_version(settings);
×
625
    return data;
×
626
}
627

628
// Top to pool.
629
// This generates a state for the pool above the presumed top block state.
630
chain_state::chain_state(const chain_state& top,
×
631
    const system::settings& settings) NOEXCEPT
×
632
  : data_(to_pool(top, settings)),
×
633
    forks_(top.forks_),
×
NEW
634
    activations_(activation(data_, forks_, settings)),
×
635
    work_required_(work_required(data_, forks_, settings)),
×
636
    median_time_past_(median_time_past(data_, forks_))
×
637
{
638
}
×
639

640
chain_state::data chain_state::to_block(const chain_state& pool,
×
641
    const block& block, const system::settings& settings) NOEXCEPT
642
{
643
    // Copy data from presumed same-height pool state.
644
    chain_state::data data{ pool.data_ };
×
645

646
    // Replace pool chain state with block state at same (next) height.
647
    // Preserve data.timestamp.retarget promotion.
648
    const auto& header = block.header();
×
649
    data.hash = {};
×
650
    data.bits.self = header.bits();
×
651
    data.version.self = header.version();
×
652
    data.timestamp.self = header.timestamp();
×
653
    data.cumulative_work += header.proof();
×
654

655
    // Cache hash of bip9 bit0 height block, otherwise use preceding state.
656
    if (data.height == settings.bip9_bit0_active_checkpoint.height())
×
657
        data.bip9_bit0_hash = data.hash;
×
658

659
    // Cache hash of bip9 bit1 height block, otherwise use preceding state.
660
    if (data.height == settings.bip9_bit1_active_checkpoint.height())
×
661
        data.bip9_bit1_hash = data.hash;
×
662

663
    return data;
×
664
}
665

666
// Pool to block.
667
// This assumes that the pool state is the same height as the block.
668
chain_state::chain_state(const chain_state& pool, const block& block,
×
669
    const system::settings& settings) NOEXCEPT
×
670
  : data_(to_block(pool, block, settings)),
×
671
    forks_(pool.forks_),
×
NEW
672
    activations_(activation(data_, forks_, settings)),
×
673
    work_required_(work_required(data_, forks_, settings)),
×
674
    median_time_past_(median_time_past(data_, forks_))
×
675
{
676
}
×
677

678
chain_state::data chain_state::to_header(const chain_state& parent,
×
679
    const header& header, const system::settings& settings) NOEXCEPT
680
{
681
    BC_ASSERT(header.previous_block_hash() == parent.hash());
×
682

683
    // Copy and promote data from presumed parent-height header/block state.
684
    auto data = to_pool(parent, settings);
×
685

686
    // Replace the parent (pool or previous) block state with given state.
687
    // Preserve data.timestamp.retarget promotion.
688
    data.hash = header.hash();
×
689
    data.bits.self = header.bits();
×
690
    data.version.self = header.version();
×
691
    data.timestamp.self = header.timestamp();
×
692
    data.cumulative_work += header.proof();
×
693

694
    // Cache hash of bip9 bit0 height block, otherwise use preceding state.
695
    if (data.height == settings.bip9_bit0_active_checkpoint.height())
×
696
        data.bip9_bit0_hash = data.hash;
×
697

698
    // Cache hash of bip9 bit1 height block, otherwise use preceding state.
699
    if (data.height == settings.bip9_bit1_active_checkpoint.height())
×
700
        data.bip9_bit1_hash = data.hash;
×
701

702
    return data;
×
703
}
704

705
// Parent to header.
706
// This assumes that parent is the state of the header's previous block.
707
chain_state::chain_state(const chain_state& parent, const header& header,
×
708
    const system::settings& settings) NOEXCEPT
×
709
  : data_(to_header(parent, header, settings)),
×
710
    forks_(parent.forks_),
×
NEW
711
    activations_(activation(data_, forks_, settings)),
×
712
    work_required_(work_required(data_, forks_, settings)),
×
713
    median_time_past_(median_time_past(data_, forks_))
×
714
{
715
}
×
716

717
// From scratch (e.g. raw data obtained from store).
718
chain_state::chain_state(data&& values,
×
719
    const system::settings& settings) NOEXCEPT
×
720
  : data_(std::move(values)),
×
NEW
721
    forks_(settings.forks),
×
NEW
722
    activations_(activation(data_, forks_, settings)),
×
723
    work_required_(work_required(data_, forks_, settings)),
×
724
    median_time_past_(median_time_past(data_, forks_))
×
725
{
726
}
×
727

728
// Properties.
729
// ----------------------------------------------------------------------------
730

731
chain::context chain_state::context() const NOEXCEPT
×
732
{
733
    return
×
734
    {
735
        flags(),
736
        timestamp(),
737
        median_time_past(),
738
        possible_narrow_cast<uint32_t>(height()),
739
        minimum_block_version(),
740
        work_required()
741
    };
×
742
}
743

744
const hash_digest& chain_state::hash() const NOEXCEPT
×
745
{
746
    return data_.hash;
×
747
}
748

749
const uint256_t& chain_state::cumulative_work() const NOEXCEPT
×
750
{
751
    return data_.cumulative_work;
×
752
}
753

754
uint32_t chain_state::minimum_block_version() const NOEXCEPT
×
755
{
NEW
756
    return activations_.minimum_block_version;
×
757
}
758

759
uint32_t chain_state::work_required() const NOEXCEPT
×
760
{
761
    return work_required_;
×
762
}
763

764
// context
765

766
uint32_t chain_state::timestamp() const NOEXCEPT
×
767
{
768
    return data_.timestamp.self;
×
769
}
770

771
uint32_t chain_state::median_time_past() const NOEXCEPT
×
772
{
773
    return median_time_past_;
×
774
}
775

NEW
776
uint32_t chain_state::flags() const NOEXCEPT
×
777
{
NEW
778
    return activations_.flags;
×
779
}
780

781
size_t chain_state::height() const NOEXCEPT
×
782
{
783
    return data_.height;
×
784
}
785

786
} // namespace chain
787
} // namespace system
788
} // 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

© 2026 Coveralls, Inc