2018-07-27 00:36:45 +02:00
// Copyright (c) 2016-2018 The Bitcoin Core developers
2016-02-15 05:13:27 +01:00
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2017-11-10 01:57:53 +01:00
# include <versionbits.h>
# include <consensus/params.h>
2016-04-24 01:30:20 +02:00
2017-05-26 02:26:52 +02:00
const struct VBDeploymentInfo VersionBitsDeploymentInfo [ Consensus : : MAX_VERSION_BITS_DEPLOYMENTS ] = {
2016-04-24 01:30:20 +02:00
{
/*.name =*/ " testdummy " ,
2016-06-01 18:47:36 +02:00
/*.gbt_force =*/ true ,
2016-04-24 01:30:20 +02:00
} ,
{
/*.name =*/ " csv " ,
2016-06-01 18:47:36 +02:00
/*.gbt_force =*/ true ,
2015-11-06 01:42:38 +01:00
} ,
{
/*.name =*/ " segwit " ,
2017-03-08 21:56:59 +01:00
/*.gbt_force =*/ true ,
2016-04-24 01:30:20 +02:00
}
} ;
2016-02-15 05:13:27 +01:00
ThresholdState AbstractThresholdConditionChecker : : GetStateFor ( const CBlockIndex * pindexPrev , const Consensus : : Params & params , ThresholdConditionCache & cache ) const
{
int nPeriod = Period ( params ) ;
int nThreshold = Threshold ( params ) ;
int64_t nTimeStart = BeginTime ( params ) ;
int64_t nTimeTimeout = EndTime ( params ) ;
2017-10-12 05:25:05 +02:00
// Check if this deployment is always active.
if ( nTimeStart = = Consensus : : BIP9Deployment : : ALWAYS_ACTIVE ) {
2018-03-09 15:03:40 +01:00
return ThresholdState : : ACTIVE ;
2017-10-12 05:25:05 +02:00
}
2016-02-15 05:13:27 +01:00
// A block's state is always the same as that of the first of its period, so it is computed based on a pindexPrev whose height equals a multiple of nPeriod - 1.
2017-08-07 07:36:37 +02:00
if ( pindexPrev ! = nullptr ) {
2016-02-15 05:13:27 +01:00
pindexPrev = pindexPrev - > GetAncestor ( pindexPrev - > nHeight - ( ( pindexPrev - > nHeight + 1 ) % nPeriod ) ) ;
}
// Walk backwards in steps of nPeriod to find a pindexPrev whose information is known
std : : vector < const CBlockIndex * > vToCompute ;
while ( cache . count ( pindexPrev ) = = 0 ) {
2017-08-07 07:36:37 +02:00
if ( pindexPrev = = nullptr ) {
2016-02-15 05:13:27 +01:00
// The genesis block is by definition defined.
2018-03-09 15:03:40 +01:00
cache [ pindexPrev ] = ThresholdState : : DEFINED ;
2016-02-15 05:13:27 +01:00
break ;
}
if ( pindexPrev - > GetMedianTimePast ( ) < nTimeStart ) {
2016-04-01 21:31:48 +02:00
// Optimization: don't recompute down further, as we know every earlier block will be before the start time
2018-03-09 15:03:40 +01:00
cache [ pindexPrev ] = ThresholdState : : DEFINED ;
2016-02-15 05:13:27 +01:00
break ;
}
vToCompute . push_back ( pindexPrev ) ;
pindexPrev = pindexPrev - > GetAncestor ( pindexPrev - > nHeight - nPeriod ) ;
}
// At this point, cache[pindexPrev] is known
assert ( cache . count ( pindexPrev ) ) ;
ThresholdState state = cache [ pindexPrev ] ;
// Now walk forward and compute the state of descendants of pindexPrev
while ( ! vToCompute . empty ( ) ) {
ThresholdState stateNext = state ;
pindexPrev = vToCompute . back ( ) ;
vToCompute . pop_back ( ) ;
switch ( state ) {
2018-03-09 15:03:40 +01:00
case ThresholdState : : DEFINED : {
2016-02-15 05:13:27 +01:00
if ( pindexPrev - > GetMedianTimePast ( ) > = nTimeTimeout ) {
2018-03-09 15:03:40 +01:00
stateNext = ThresholdState : : FAILED ;
2016-02-15 05:13:27 +01:00
} else if ( pindexPrev - > GetMedianTimePast ( ) > = nTimeStart ) {
2018-03-09 15:03:40 +01:00
stateNext = ThresholdState : : STARTED ;
2016-02-15 05:13:27 +01:00
}
break ;
}
2018-03-09 15:03:40 +01:00
case ThresholdState : : STARTED : {
2016-02-15 05:13:27 +01:00
if ( pindexPrev - > GetMedianTimePast ( ) > = nTimeTimeout ) {
2018-03-09 15:03:40 +01:00
stateNext = ThresholdState : : FAILED ;
2016-02-15 05:13:27 +01:00
break ;
}
// We need to count
const CBlockIndex * pindexCount = pindexPrev ;
int count = 0 ;
for ( int i = 0 ; i < nPeriod ; i + + ) {
if ( Condition ( pindexCount , params ) ) {
count + + ;
}
pindexCount = pindexCount - > pprev ;
}
if ( count > = nThreshold ) {
2018-03-09 15:03:40 +01:00
stateNext = ThresholdState : : LOCKED_IN ;
2016-02-15 05:13:27 +01:00
}
break ;
}
2018-03-09 15:03:40 +01:00
case ThresholdState : : LOCKED_IN : {
2016-02-15 05:13:27 +01:00
// Always progresses into ACTIVE.
2018-03-09 15:03:40 +01:00
stateNext = ThresholdState : : ACTIVE ;
2016-02-15 05:13:27 +01:00
break ;
}
2018-03-09 15:03:40 +01:00
case ThresholdState : : FAILED :
case ThresholdState : : ACTIVE : {
2016-02-15 05:13:27 +01:00
// Nothing happens, these are terminal states.
break ;
}
}
cache [ pindexPrev ] = state = stateNext ;
}
return state ;
}
2017-01-14 02:06:50 +01:00
// return the numerical statistics of blocks signalling the specified BIP9 condition in this current period
BIP9Stats AbstractThresholdConditionChecker : : GetStateStatisticsFor ( const CBlockIndex * pindex , const Consensus : : Params & params ) const
{
2017-07-30 23:36:33 +02:00
BIP9Stats stats = { } ;
2017-01-14 02:06:50 +01:00
stats . period = Period ( params ) ;
stats . threshold = Threshold ( params ) ;
2017-08-07 07:36:37 +02:00
if ( pindex = = nullptr )
2017-01-14 02:06:50 +01:00
return stats ;
// Find beginning of period
const CBlockIndex * pindexEndOfPrevPeriod = pindex - > GetAncestor ( pindex - > nHeight - ( ( pindex - > nHeight + 1 ) % stats . period ) ) ;
stats . elapsed = pindex - > nHeight - pindexEndOfPrevPeriod - > nHeight ;
// Count from current block to beginning of period
int count = 0 ;
const CBlockIndex * currentIndex = pindex ;
while ( pindexEndOfPrevPeriod - > nHeight ! = currentIndex - > nHeight ) {
if ( Condition ( currentIndex , params ) )
count + + ;
currentIndex = currentIndex - > pprev ;
}
stats . count = count ;
stats . possible = ( stats . period - stats . threshold ) > = ( stats . elapsed - count ) ;
return stats ;
}
2016-05-07 00:08:39 +02:00
int AbstractThresholdConditionChecker : : GetStateSinceHeightFor ( const CBlockIndex * pindexPrev , const Consensus : : Params & params , ThresholdConditionCache & cache ) const
{
2017-10-12 05:25:05 +02:00
int64_t start_time = BeginTime ( params ) ;
if ( start_time = = Consensus : : BIP9Deployment : : ALWAYS_ACTIVE ) {
return 0 ;
}
2016-05-07 00:08:39 +02:00
const ThresholdState initialState = GetStateFor ( pindexPrev , params , cache ) ;
// BIP 9 about state DEFINED: "The genesis block is by definition in this state for each deployment."
2018-03-09 15:03:40 +01:00
if ( initialState = = ThresholdState : : DEFINED ) {
2016-05-07 00:08:39 +02:00
return 0 ;
}
const int nPeriod = Period ( params ) ;
// A block's state is always the same as that of the first of its period, so it is computed based on a pindexPrev whose height equals a multiple of nPeriod - 1.
// To ease understanding of the following height calculation, it helps to remember that
// right now pindexPrev points to the block prior to the block that we are computing for, thus:
// if we are computing for the last block of a period, then pindexPrev points to the second to last block of the period, and
// if we are computing for the first block of a period, then pindexPrev points to the last block of the previous period.
2017-08-07 07:36:37 +02:00
// The parent of the genesis block is represented by nullptr.
2016-05-07 00:08:39 +02:00
pindexPrev = pindexPrev - > GetAncestor ( pindexPrev - > nHeight - ( ( pindexPrev - > nHeight + 1 ) % nPeriod ) ) ;
const CBlockIndex * previousPeriodParent = pindexPrev - > GetAncestor ( pindexPrev - > nHeight - nPeriod ) ;
2017-08-07 07:36:37 +02:00
while ( previousPeriodParent ! = nullptr & & GetStateFor ( previousPeriodParent , params , cache ) = = initialState ) {
2016-05-07 00:08:39 +02:00
pindexPrev = previousPeriodParent ;
previousPeriodParent = pindexPrev - > GetAncestor ( pindexPrev - > nHeight - nPeriod ) ;
}
// Adjust the result because right now we point to the parent block.
return pindexPrev - > nHeight + 1 ;
}
2016-02-15 05:13:27 +01:00
namespace
{
/**
* Class to implement versionbits logic .
*/
class VersionBitsConditionChecker : public AbstractThresholdConditionChecker {
private :
const Consensus : : DeploymentPos id ;
protected :
2017-06-20 21:58:56 +02:00
int64_t BeginTime ( const Consensus : : Params & params ) const override { return params . vDeployments [ id ] . nStartTime ; }
int64_t EndTime ( const Consensus : : Params & params ) const override { return params . vDeployments [ id ] . nTimeout ; }
int Period ( const Consensus : : Params & params ) const override { return params . nMinerConfirmationWindow ; }
int Threshold ( const Consensus : : Params & params ) const override { return params . nRuleChangeActivationThreshold ; }
2016-02-15 05:13:27 +01:00
2017-06-20 21:58:56 +02:00
bool Condition ( const CBlockIndex * pindex , const Consensus : : Params & params ) const override
2016-02-15 05:13:27 +01:00
{
return ( ( ( pindex - > nVersion & VERSIONBITS_TOP_MASK ) = = VERSIONBITS_TOP_BITS ) & & ( pindex - > nVersion & Mask ( params ) ) ! = 0 ) ;
}
public :
2017-08-01 12:22:41 +02:00
explicit VersionBitsConditionChecker ( Consensus : : DeploymentPos id_ ) : id ( id_ ) { }
2016-02-15 05:13:27 +01:00
uint32_t Mask ( const Consensus : : Params & params ) const { return ( ( uint32_t ) 1 ) < < params . vDeployments [ id ] . bit ; }
} ;
2017-05-31 22:21:25 +02:00
} // namespace
2016-02-15 05:13:27 +01:00
ThresholdState VersionBitsState ( const CBlockIndex * pindexPrev , const Consensus : : Params & params , Consensus : : DeploymentPos pos , VersionBitsCache & cache )
{
return VersionBitsConditionChecker ( pos ) . GetStateFor ( pindexPrev , params , cache . caches [ pos ] ) ;
}
2017-01-14 02:06:50 +01:00
BIP9Stats VersionBitsStatistics ( const CBlockIndex * pindexPrev , const Consensus : : Params & params , Consensus : : DeploymentPos pos )
{
return VersionBitsConditionChecker ( pos ) . GetStateStatisticsFor ( pindexPrev , params ) ;
}
2016-05-07 00:08:39 +02:00
int VersionBitsStateSinceHeight ( const CBlockIndex * pindexPrev , const Consensus : : Params & params , Consensus : : DeploymentPos pos , VersionBitsCache & cache )
{
return VersionBitsConditionChecker ( pos ) . GetStateSinceHeightFor ( pindexPrev , params , cache . caches [ pos ] ) ;
}
2016-02-15 05:13:27 +01:00
uint32_t VersionBitsMask ( const Consensus : : Params & params , Consensus : : DeploymentPos pos )
{
return VersionBitsConditionChecker ( pos ) . Mask ( params ) ;
}
void VersionBitsCache : : Clear ( )
{
for ( unsigned int d = 0 ; d < Consensus : : MAX_VERSION_BITS_DEPLOYMENTS ; d + + ) {
caches [ d ] . clear ( ) ;
}
}