versionbits: Split out internal details into impl header

This commit is contained in:
Anthony Towns 2023-12-09 10:14:07 +10:00
parent 37b9b67a39
commit d00d1ed52c
6 changed files with 55 additions and 36 deletions

View file

@ -9,6 +9,7 @@
#include <primitives/block.h>
#include <util/chaintype.h>
#include <versionbits.h>
#include <versionbits_impl.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>

View file

@ -9,6 +9,7 @@
#include <test/util/setup_common.h>
#include <util/chaintype.h>
#include <versionbits.h>
#include <versionbits_impl.h>
#include <boost/test/unit_test.hpp>

View file

@ -7,6 +7,7 @@
#include <kernel/chainparams.h>
#include <util/check.h>
#include <versionbits.h>
#include <versionbits_impl.h>
using enum ThresholdState;

View file

@ -24,18 +24,8 @@ static const int32_t VERSIONBITS_TOP_MASK = 0xE0000000UL;
/** Total bits available for versionbits */
static const int32_t VERSIONBITS_NUM_BITS = 29;
/** BIP 9 defines a finite-state-machine to deploy a softfork in multiple stages.
* State transitions happen during retarget period if conditions are met
* In case of reorg, transitions can go backward. Without transition, state is
* inherited between periods. All blocks of a period share the same state.
*/
enum class ThresholdState {
DEFINED, // First state that each softfork starts out as. The genesis block is by definition in this state for each deployment.
STARTED, // For blocks past the starttime.
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.
ACTIVE, // For all blocks after the LOCKED_IN retarget period (final state)
FAILED, // For all blocks once the first retarget period after the timeout time is hit, if LOCKED_IN wasn't already reached (final state)
};
/** Opaque type for BIP9 state. See versionbits_impl.h for details. */
enum class ThresholdState : uint8_t;
// A map that gives the state for blocks whose height is a multiple of Period().
// The map is indexed by the block's parent, however, so all keys in the map
@ -75,30 +65,6 @@ struct BIP9GBTStatus {
std::map<std::string, const Info, std::less<>> signalling, locked_in, active;
};
/**
* Abstract class that implements BIP9-style threshold logic, and caches results.
*/
class AbstractThresholdConditionChecker {
protected:
virtual bool Condition(const CBlockIndex* pindex) const =0;
virtual int64_t BeginTime() const =0;
virtual int64_t EndTime() const =0;
virtual int MinActivationHeight() const { return 0; }
virtual int Period() const =0;
virtual int Threshold() const =0;
public:
/** Returns the numerical statistics of an in-progress BIP9 softfork in the period including pindex
* If provided, signalling_blocks is set to true/false based on whether each block in the period signalled
*/
BIP9Stats GetStateStatisticsFor(const CBlockIndex* pindex, std::vector<bool>* signalling_blocks = nullptr) const;
/** Returns the state for pindex A based on parent pindexPrev B. Applies any state transition if conditions are present.
* Caches state from first block of period. */
ThresholdState GetStateFor(const CBlockIndex* pindexPrev, ThresholdConditionCache& cache) const;
/** 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 */
int GetStateSinceHeightFor(const CBlockIndex* pindexPrev, ThresholdConditionCache& cache) const;
};
/** BIP 9 allows multiple softforks to be deployed in parallel. We cache
* per-period state for every one of them. */
class VersionBitsCache

49
src/versionbits_impl.h Normal file
View file

@ -0,0 +1,49 @@
// Copyright (c) 2016-2022 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_VERSIONBITS_IMPL_H
#define BITCOIN_VERSIONBITS_IMPL_H
#include <chain.h>
#include <sync.h>
#include <versionbits.h>
/** BIP 9 defines a finite-state-machine to deploy a softfork in multiple stages.
* State transitions happen during retarget period if conditions are met
* In case of reorg, transitions can go backward. Without transition, state is
* inherited between periods. All blocks of a period share the same state.
*/
enum class ThresholdState : uint8_t {
DEFINED, // First state that each softfork starts out as. The genesis block is by definition in this state for each deployment.
STARTED, // For blocks past the starttime.
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.
ACTIVE, // For all blocks after the LOCKED_IN retarget period (final state)
FAILED, // For all blocks once the first retarget period after the timeout time is hit, if LOCKED_IN wasn't already reached (final state)
};
/**
* Abstract class that implements BIP9-style threshold logic, and caches results.
*/
class AbstractThresholdConditionChecker {
protected:
virtual bool Condition(const CBlockIndex* pindex) const =0;
virtual int64_t BeginTime() const =0;
virtual int64_t EndTime() const =0;
virtual int MinActivationHeight() const { return 0; }
virtual int Period() const =0;
virtual int Threshold() const =0;
public:
/** Returns the numerical statistics of an in-progress BIP9 softfork in the period including pindex
* If provided, signalling_blocks is set to true/false based on whether each block in the period signalled
*/
BIP9Stats GetStateStatisticsFor(const CBlockIndex* pindex, std::vector<bool>* signalling_blocks = nullptr) const;
/** Returns the state for pindex A based on parent pindexPrev B. Applies any state transition if conditions are present.
* Caches state from first block of period. */
ThresholdState GetStateFor(const CBlockIndex* pindexPrev, ThresholdConditionCache& cache) const;
/** 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 */
int GetStateSinceHeightFor(const CBlockIndex* pindexPrev, ThresholdConditionCache& cache) const;
};
#endif // BITCOIN_VERSIONBITS_IMPL_H

View file

@ -21,6 +21,7 @@ EXPECTED_CIRCULAR_DEPENDENCIES = (
"qt/transactiontablemodel -> qt/walletmodel -> qt/transactiontablemodel",
"wallet/wallet -> wallet/walletdb -> wallet/wallet",
"kernel/coinstats -> validation -> kernel/coinstats",
"versionbits -> versionbits_impl -> versionbits",
# Temporary, removed in followup https://github.com/bitcoin/bitcoin/pull/24230
"index/base -> node/context -> net_processing -> index/blockfilterindex -> index/base",