Skip to content

Commit d00d1ed

Browse files
committed
versionbits: Split out internal details into impl header
1 parent 37b9b67 commit d00d1ed

File tree

6 files changed

+55
-36
lines changed

6 files changed

+55
-36
lines changed

src/test/fuzz/versionbits.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <primitives/block.h>
1010
#include <util/chaintype.h>
1111
#include <versionbits.h>
12+
#include <versionbits_impl.h>
1213

1314
#include <test/fuzz/FuzzedDataProvider.h>
1415
#include <test/fuzz/fuzz.h>

src/test/versionbits_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <test/util/setup_common.h>
1010
#include <util/chaintype.h>
1111
#include <versionbits.h>
12+
#include <versionbits_impl.h>
1213

1314
#include <boost/test/unit_test.hpp>
1415

src/versionbits.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <kernel/chainparams.h>
88
#include <util/check.h>
99
#include <versionbits.h>
10+
#include <versionbits_impl.h>
1011

1112
using enum ThresholdState;
1213

src/versionbits.h

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,8 @@ static const int32_t VERSIONBITS_TOP_MASK = 0xE0000000UL;
2424
/** Total bits available for versionbits */
2525
static const int32_t VERSIONBITS_NUM_BITS = 29;
2626

27-
/** BIP 9 defines a finite-state-machine to deploy a softfork in multiple stages.
28-
* State transitions happen during retarget period if conditions are met
29-
* In case of reorg, transitions can go backward. Without transition, state is
30-
* inherited between periods. All blocks of a period share the same state.
31-
*/
32-
enum class ThresholdState {
33-
DEFINED, // First state that each softfork starts out as. The genesis block is by definition in this state for each deployment.
34-
STARTED, // For blocks past the starttime.
35-
LOCKED_IN, // For at least one retarget period after the first retarget period with STARTED blocks of which at least threshold have the associated bit set in nVersion, until min_activation_height is reached.
36-
ACTIVE, // For all blocks after the LOCKED_IN retarget period (final state)
37-
FAILED, // For all blocks once the first retarget period after the timeout time is hit, if LOCKED_IN wasn't already reached (final state)
38-
};
27+
/** Opaque type for BIP9 state. See versionbits_impl.h for details. */
28+
enum class ThresholdState : uint8_t;
3929

4030
// A map that gives the state for blocks whose height is a multiple of Period().
4131
// The map is indexed by the block's parent, however, so all keys in the map
@@ -75,30 +65,6 @@ struct BIP9GBTStatus {
7565
std::map<std::string, const Info, std::less<>> signalling, locked_in, active;
7666
};
7767

78-
/**
79-
* Abstract class that implements BIP9-style threshold logic, and caches results.
80-
*/
81-
class AbstractThresholdConditionChecker {
82-
protected:
83-
virtual bool Condition(const CBlockIndex* pindex) const =0;
84-
virtual int64_t BeginTime() const =0;
85-
virtual int64_t EndTime() const =0;
86-
virtual int MinActivationHeight() const { return 0; }
87-
virtual int Period() const =0;
88-
virtual int Threshold() const =0;
89-
90-
public:
91-
/** Returns the numerical statistics of an in-progress BIP9 softfork in the period including pindex
92-
* If provided, signalling_blocks is set to true/false based on whether each block in the period signalled
93-
*/
94-
BIP9Stats GetStateStatisticsFor(const CBlockIndex* pindex, std::vector<bool>* signalling_blocks = nullptr) const;
95-
/** Returns the state for pindex A based on parent pindexPrev B. Applies any state transition if conditions are present.
96-
* Caches state from first block of period. */
97-
ThresholdState GetStateFor(const CBlockIndex* pindexPrev, ThresholdConditionCache& cache) const;
98-
/** Returns the height since when the ThresholdState has started for pindex A based on parent pindexPrev B, all blocks of a period share the same */
99-
int GetStateSinceHeightFor(const CBlockIndex* pindexPrev, ThresholdConditionCache& cache) const;
100-
};
101-
10268
/** BIP 9 allows multiple softforks to be deployed in parallel. We cache
10369
* per-period state for every one of them. */
10470
class VersionBitsCache

src/versionbits_impl.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) 2016-2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_VERSIONBITS_IMPL_H
6+
#define BITCOIN_VERSIONBITS_IMPL_H
7+
8+
#include <chain.h>
9+
#include <sync.h>
10+
#include <versionbits.h>
11+
12+
/** BIP 9 defines a finite-state-machine to deploy a softfork in multiple stages.
13+
* State transitions happen during retarget period if conditions are met
14+
* In case of reorg, transitions can go backward. Without transition, state is
15+
* inherited between periods. All blocks of a period share the same state.
16+
*/
17+
enum class ThresholdState : uint8_t {
18+
DEFINED, // First state that each softfork starts out as. The genesis block is by definition in this state for each deployment.
19+
STARTED, // For blocks past the starttime.
20+
LOCKED_IN, // For at least one retarget period after the first retarget period with STARTED blocks of which at least threshold have the associated bit set in nVersion, until min_activation_height is reached.
21+
ACTIVE, // For all blocks after the LOCKED_IN retarget period (final state)
22+
FAILED, // For all blocks once the first retarget period after the timeout time is hit, if LOCKED_IN wasn't already reached (final state)
23+
};
24+
25+
/**
26+
* Abstract class that implements BIP9-style threshold logic, and caches results.
27+
*/
28+
class AbstractThresholdConditionChecker {
29+
protected:
30+
virtual bool Condition(const CBlockIndex* pindex) const =0;
31+
virtual int64_t BeginTime() const =0;
32+
virtual int64_t EndTime() const =0;
33+
virtual int MinActivationHeight() const { return 0; }
34+
virtual int Period() const =0;
35+
virtual int Threshold() const =0;
36+
37+
public:
38+
/** Returns the numerical statistics of an in-progress BIP9 softfork in the period including pindex
39+
* If provided, signalling_blocks is set to true/false based on whether each block in the period signalled
40+
*/
41+
BIP9Stats GetStateStatisticsFor(const CBlockIndex* pindex, std::vector<bool>* signalling_blocks = nullptr) const;
42+
/** Returns the state for pindex A based on parent pindexPrev B. Applies any state transition if conditions are present.
43+
* Caches state from first block of period. */
44+
ThresholdState GetStateFor(const CBlockIndex* pindexPrev, ThresholdConditionCache& cache) const;
45+
/** Returns the height since when the ThresholdState has started for pindex A based on parent pindexPrev B, all blocks of a period share the same */
46+
int GetStateSinceHeightFor(const CBlockIndex* pindexPrev, ThresholdConditionCache& cache) const;
47+
};
48+
49+
#endif // BITCOIN_VERSIONBITS_IMPL_H

test/lint/lint-circular-dependencies.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"qt/transactiontablemodel -> qt/walletmodel -> qt/transactiontablemodel",
2222
"wallet/wallet -> wallet/walletdb -> wallet/wallet",
2323
"kernel/coinstats -> validation -> kernel/coinstats",
24+
"versionbits -> versionbits_impl -> versionbits",
2425

2526
# Temporary, removed in followup https://github.com/bitcoin/bitcoin/pull/24230
2627
"index/base -> node/context -> net_processing -> index/blockfilterindex -> index/base",

0 commit comments

Comments
 (0)