2014-09-03 02:20:09 +02:00
// Copyright (c) 2009-2010 Satoshi Nakamoto
2022-12-25 00:49:50 +01:00
// Copyright (c) 2009-2022 The Bitcoin Core developers
2014-09-29 08:22:03 +02:00
// Distributed under the MIT software license, see the accompanying
2014-09-03 02:20:09 +02:00
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2014-11-03 16:16:40 +01:00
# ifndef BITCOIN_CHAIN_H
# define BITCOIN_CHAIN_H
2014-09-03 02:20:09 +02:00
2017-11-10 01:57:53 +01:00
# include <arith_uint256.h>
2018-05-14 08:39:53 +02:00
# include <consensus/params.h>
2019-01-06 20:43:38 +01:00
# include <flatfile.h>
2022-09-15 11:45:07 +02:00
# include <kernel/cs_main.h>
2017-11-10 01:57:53 +01:00
# include <primitives/block.h>
2021-09-09 17:17:39 +02:00
# include <sync.h>
2017-11-10 01:57:53 +01:00
# include <uint256.h>
2022-08-05 14:45:30 +02:00
# include <util/time.h>
2014-09-03 02:20:09 +02:00
# include <vector>
2017-03-02 18:20:34 +01:00
/**
* Maximum amount of time that a block timestamp is allowed to exceed the
* current network - adjusted time before the block will be accepted .
*/
2018-10-23 21:02:20 +02:00
static constexpr int64_t MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60 ;
2017-03-02 18:20:34 +01:00
/**
* Timestamp window used as a grace period by code that compares external
* timestamps ( such as timestamps passed to RPCs , or wallet key creation times )
* to block timestamps . This should be set at least as high as
* MAX_FUTURE_BLOCK_TIME .
*/
2018-10-23 21:02:20 +02:00
static constexpr int64_t TIMESTAMP_WINDOW = MAX_FUTURE_BLOCK_TIME ;
/**
* Maximum gap between node time and block time used
* for the " Catching up... " mode in GUI .
*
* Ref : https : //github.com/bitcoin/bitcoin/pull/1026
*/
static constexpr int64_t MAX_BLOCK_TIME_GAP = 90 * 60 ;
2017-03-02 18:20:34 +01:00
2016-04-05 15:14:48 +02:00
class CBlockFileInfo
{
public :
unsigned int nBlocks ; //!< number of blocks stored in file
unsigned int nSize ; //!< number of used bytes of block file
unsigned int nUndoSize ; //!< number of used bytes in the undo file
unsigned int nHeightFirst ; //!< lowest height of block in file
unsigned int nHeightLast ; //!< highest height of block in file
uint64_t nTimeFirst ; //!< earliest time of block in file
uint64_t nTimeLast ; //!< latest time of block in file
2017-07-08 05:27:39 +02:00
SERIALIZE_METHODS ( CBlockFileInfo , obj )
{
READWRITE ( VARINT ( obj . nBlocks ) ) ;
READWRITE ( VARINT ( obj . nSize ) ) ;
READWRITE ( VARINT ( obj . nUndoSize ) ) ;
READWRITE ( VARINT ( obj . nHeightFirst ) ) ;
READWRITE ( VARINT ( obj . nHeightLast ) ) ;
READWRITE ( VARINT ( obj . nTimeFirst ) ) ;
READWRITE ( VARINT ( obj . nTimeLast ) ) ;
2016-04-05 15:14:48 +02:00
}
2021-12-14 12:48:59 +01:00
void SetNull ( )
{
nBlocks = 0 ;
nSize = 0 ;
nUndoSize = 0 ;
nHeightFirst = 0 ;
nHeightLast = 0 ;
nTimeFirst = 0 ;
nTimeLast = 0 ;
}
CBlockFileInfo ( )
{
SetNull ( ) ;
}
std : : string ToString ( ) const ;
/** update statistics (does not update nSize) */
void AddBlock ( unsigned int nHeightIn , uint64_t nTimeIn )
{
if ( nBlocks = = 0 | | nHeightFirst > nHeightIn )
nHeightFirst = nHeightIn ;
if ( nBlocks = = 0 | | nTimeFirst > nTimeIn )
nTimeFirst = nTimeIn ;
nBlocks + + ;
if ( nHeightIn > nHeightLast )
nHeightLast = nHeightIn ;
if ( nTimeIn > nTimeLast )
nTimeLast = nTimeIn ;
}
2016-04-05 15:14:48 +02:00
} ;
2021-12-14 12:48:59 +01:00
enum BlockStatus : uint32_t {
2014-10-25 10:46:54 +02:00
//! Unused.
2014-09-03 02:20:09 +02:00
BLOCK_VALID_UNKNOWN = 0 ,
2014-07-12 00:02:35 +02:00
2018-12-03 22:50:08 +01:00
//! Reserved (was BLOCK_VALID_HEADER).
BLOCK_VALID_RESERVED = 1 ,
2014-07-12 00:02:35 +02:00
2014-10-25 10:46:54 +02:00
//! All parent headers found, difficulty matches, timestamp >= median previous, checkpoint. Implies all parents
//! are also at least TREE.
2014-07-12 00:02:35 +02:00
BLOCK_VALID_TREE = 2 ,
2014-10-25 10:46:54 +02:00
/**
* Only first tx is coinbase , 2 < = coinbase input script length < = 100 , transactions valid , no duplicate txids ,
* sigops , size , merkle root . Implies all parents are at least TREE but not necessarily TRANSACTIONS . When all
* parent blocks also have TRANSACTIONS , CBlockIndex : : nChainTx will be set .
*/
2014-07-12 00:02:35 +02:00
BLOCK_VALID_TRANSACTIONS = 3 ,
2015-04-28 16:47:17 +02:00
//! Outputs do not overspend inputs, no double spends, coinbase output ok, no immature coinbase spends, BIP30.
2014-10-25 10:46:54 +02:00
//! Implies all parents are also at least CHAIN.
2014-07-12 00:02:35 +02:00
BLOCK_VALID_CHAIN = 4 ,
2014-10-25 10:46:54 +02:00
//! Scripts & signatures ok. Implies all parents are also at least SCRIPTS.
2014-07-12 00:02:35 +02:00
BLOCK_VALID_SCRIPTS = 5 ,
2014-10-25 10:46:54 +02:00
//! All validity bits.
2018-12-03 22:50:08 +01:00
BLOCK_VALID_MASK = BLOCK_VALID_RESERVED | BLOCK_VALID_TREE | BLOCK_VALID_TRANSACTIONS |
2014-09-03 02:20:09 +02:00
BLOCK_VALID_CHAIN | BLOCK_VALID_SCRIPTS ,
2016-06-29 11:48:51 +02:00
BLOCK_HAVE_DATA = 8 , //!< full block available in blk*.dat
BLOCK_HAVE_UNDO = 16 , //!< undo data available in rev*.dat
2014-09-03 02:20:09 +02:00
BLOCK_HAVE_MASK = BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO ,
2016-06-29 11:48:51 +02:00
BLOCK_FAILED_VALID = 32 , //!< stage after last reached validness failed
BLOCK_FAILED_CHILD = 64 , //!< descends from failed block
2014-09-03 02:20:09 +02:00
BLOCK_FAILED_MASK = BLOCK_FAILED_VALID | BLOCK_FAILED_CHILD ,
2016-03-18 17:20:12 +01:00
2021-07-21 19:31:14 +02:00
BLOCK_OPT_WITNESS = 128 , //!< block data in blk*.dat was received with a witness-enforcing client
/**
* If set , this indicates that the block index entry is assumed - valid .
* Certain diagnostics will be skipped in e . g . CheckBlockIndex ( ) .
* It almost certainly means that the block ' s full validation is pending
2022-02-15 15:29:53 +01:00
* on a background chainstate . See ` doc / design / assumeutxo . md ` .
2021-07-21 19:31:14 +02:00
*/
BLOCK_ASSUMED_VALID = 256 ,
2014-09-03 02:20:09 +02:00
} ;
/** The block chain is a tree shaped structure starting with the
* genesis block at the root , with each block potentially having multiple
* candidates to be the next block . A blockindex may have multiple pprev pointing
* to it , but at most one of them can be part of the currently active branch .
*/
class CBlockIndex
{
public :
2014-11-25 18:54:36 +01:00
//! pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
2019-10-16 18:37:25 +02:00
const uint256 * phashBlock { nullptr } ;
2014-09-03 02:20:09 +02:00
2014-10-25 10:46:54 +02:00
//! pointer to the index of the predecessor of this block
2019-10-16 18:37:25 +02:00
CBlockIndex * pprev { nullptr } ;
2014-09-03 02:20:09 +02:00
2014-10-25 10:46:54 +02:00
//! pointer to the index of some further predecessor of this block
2019-10-16 18:37:25 +02:00
CBlockIndex * pskip { nullptr } ;
2014-09-03 02:20:09 +02:00
2014-10-25 10:46:54 +02:00
//! height of the entry in the chain. The genesis block has height 0
2019-10-16 18:37:25 +02:00
int nHeight { 0 } ;
2014-09-03 02:20:09 +02:00
2014-10-25 10:46:54 +02:00
//! Which # file this block is stored in (blk?????.dat)
2022-01-19 13:42:32 +01:00
int nFile GUARDED_BY ( : : cs_main ) { 0 } ;
2014-09-03 02:20:09 +02:00
2014-10-25 10:46:54 +02:00
//! Byte offset within blk?????.dat where this block's data is stored
2022-01-19 13:42:32 +01:00
unsigned int nDataPos GUARDED_BY ( : : cs_main ) { 0 } ;
2014-09-03 02:20:09 +02:00
2014-10-25 10:46:54 +02:00
//! Byte offset within rev?????.dat where this block's undo data is stored
2022-01-19 13:42:32 +01:00
unsigned int nUndoPos GUARDED_BY ( : : cs_main ) { 0 } ;
2014-09-03 02:20:09 +02:00
2014-10-25 10:46:54 +02:00
//! (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block
2019-10-16 18:37:25 +02:00
arith_uint256 nChainWork { } ;
2014-09-03 02:20:09 +02:00
2014-10-25 10:46:54 +02:00
//! Number of transactions in this block.
//! Note: in a potential headers-first mode, this number cannot be relied upon
2020-08-25 19:50:23 +02:00
//! Note: this value is faked during UTXO snapshot load to ensure that
//! LoadBlockIndex() will load index entries for blocks that we lack data for.
//! @sa ActivateSnapshot
2019-10-16 18:37:25 +02:00
unsigned int nTx { 0 } ;
2014-09-03 02:20:09 +02:00
2014-10-25 10:46:54 +02:00
//! (memory only) Number of transactions in the chain up to and including this block.
//! This value will be non-zero only if and only if transactions for this block and all its parents are available.
2019-10-21 18:11:27 +02:00
//! Change to 64-bit type before 2024 (assuming worst case of 60 byte transactions).
2020-08-25 19:50:23 +02:00
//!
//! Note: this value is faked during use of a UTXO snapshot because we don't
//! have the underlying block data available during snapshot load.
//! @sa AssumeutxoData
//! @sa ActivateSnapshot
2019-10-16 18:37:25 +02:00
unsigned int nChainTx { 0 } ;
2014-09-03 02:20:09 +02:00
2014-10-25 10:46:54 +02:00
//! Verification status of this block. See enum BlockStatus
2020-08-25 19:50:23 +02:00
//!
//! Note: this value is modified to show BLOCK_OPT_WITNESS during UTXO snapshot
//! load to avoid the block index being spuriously rewound.
2021-04-28 09:16:48 +02:00
//! @sa NeedsRedownload
2020-08-25 19:50:23 +02:00
//! @sa ActivateSnapshot
2022-01-19 13:42:32 +01:00
uint32_t nStatus GUARDED_BY ( : : cs_main ) { 0 } ;
2014-09-03 02:20:09 +02:00
2014-10-25 10:46:54 +02:00
//! block header
2019-10-16 18:37:25 +02:00
int32_t nVersion { 0 } ;
uint256 hashMerkleRoot { } ;
uint32_t nTime { 0 } ;
uint32_t nBits { 0 } ;
uint32_t nNonce { 0 } ;
2014-09-03 02:20:09 +02:00
2014-10-25 10:46:54 +02:00
//! (memory only) Sequential id assigned to distinguish order in which blocks are received.
2019-10-16 18:37:25 +02:00
int32_t nSequenceId { 0 } ;
2014-09-03 02:20:09 +02:00
2017-07-30 21:42:17 +02:00
//! (memory only) Maximum nTime in the chain up to and including this block.
2019-10-16 18:37:25 +02:00
unsigned int nTimeMax { 0 } ;
2014-09-03 02:20:09 +02:00
2017-08-01 12:22:41 +02:00
explicit CBlockIndex ( const CBlockHeader & block )
2019-10-16 18:37:25 +02:00
: nVersion { block . nVersion } ,
hashMerkleRoot { block . hashMerkleRoot } ,
nTime { block . nTime } ,
nBits { block . nBits } ,
nNonce { block . nNonce }
2014-09-03 02:20:09 +02:00
{
}
2021-09-09 17:17:39 +02:00
FlatFilePos GetBlockPos ( ) const EXCLUSIVE_LOCKS_REQUIRED ( : : cs_main )
2021-12-14 12:48:59 +01:00
{
2021-09-09 17:17:39 +02:00
AssertLockHeld ( : : cs_main ) ;
2019-01-06 20:46:30 +01:00
FlatFilePos ret ;
2014-09-03 02:20:09 +02:00
if ( nStatus & BLOCK_HAVE_DATA ) {
ret . nFile = nFile ;
2021-12-14 12:48:59 +01:00
ret . nPos = nDataPos ;
2014-09-03 02:20:09 +02:00
}
return ret ;
}
2021-09-09 18:48:22 +02:00
FlatFilePos GetUndoPos ( ) const EXCLUSIVE_LOCKS_REQUIRED ( : : cs_main )
2021-12-14 12:48:59 +01:00
{
2021-09-09 18:48:22 +02:00
AssertLockHeld ( : : cs_main ) ;
2019-01-06 20:46:30 +01:00
FlatFilePos ret ;
2014-09-03 02:20:09 +02:00
if ( nStatus & BLOCK_HAVE_UNDO ) {
ret . nFile = nFile ;
2021-12-14 12:48:59 +01:00
ret . nPos = nUndoPos ;
2014-09-03 02:20:09 +02:00
}
return ret ;
}
CBlockHeader GetBlockHeader ( ) const
{
CBlockHeader block ;
2021-12-14 12:48:59 +01:00
block . nVersion = nVersion ;
2014-09-03 02:20:09 +02:00
if ( pprev )
block . hashPrevBlock = pprev - > GetBlockHash ( ) ;
block . hashMerkleRoot = hashMerkleRoot ;
2021-12-14 12:48:59 +01:00
block . nTime = nTime ;
block . nBits = nBits ;
block . nNonce = nNonce ;
2014-09-03 02:20:09 +02:00
return block ;
}
uint256 GetBlockHash ( ) const
{
2022-07-16 13:22:46 +02:00
assert ( phashBlock ! = nullptr ) ;
2014-09-03 02:20:09 +02:00
return * phashBlock ;
}
2018-12-04 00:14:08 +01:00
/**
* Check whether this block ' s and all previous blocks ' transactions have been
* downloaded ( and stored to disk ) at some point .
*
* Does not imply the transactions are consensus - valid ( ConnectTip might fail )
* Does not imply the transactions are still stored on disk . ( IsBlockPruned might return true )
*/
bool HaveTxsDownloaded ( ) const { return nChainTx ! = 0 ; }
2022-08-05 14:45:30 +02:00
NodeSeconds Time ( ) const
{
return NodeSeconds { std : : chrono : : seconds { nTime } } ;
}
2014-09-03 02:20:09 +02:00
int64_t GetBlockTime ( ) const
{
return ( int64_t ) nTime ;
}
2017-01-08 05:05:14 +01:00
int64_t GetBlockTimeMax ( ) const
{
return ( int64_t ) nTimeMax ;
}
2017-07-05 16:49:57 +02:00
static constexpr int nMedianTimeSpan = 11 ;
2014-09-03 02:20:09 +02:00
int64_t GetMedianTimePast ( ) const
{
int64_t pmedian [ nMedianTimeSpan ] ;
int64_t * pbegin = & pmedian [ nMedianTimeSpan ] ;
int64_t * pend = & pmedian [ nMedianTimeSpan ] ;
const CBlockIndex * pindex = this ;
for ( int i = 0 ; i < nMedianTimeSpan & & pindex ; i + + , pindex = pindex - > pprev )
* ( - - pbegin ) = pindex - > GetBlockTime ( ) ;
std : : sort ( pbegin , pend ) ;
2021-12-14 12:48:59 +01:00
return pbegin [ ( pend - pbegin ) / 2 ] ;
2014-09-03 02:20:09 +02:00
}
2022-07-16 15:19:03 +02:00
std : : string ToString ( ) const ;
2014-09-03 02:20:09 +02:00
2014-10-25 10:46:54 +02:00
//! Check whether this block index entry is valid up to the passed validity level.
2014-09-03 02:20:09 +02:00
bool IsValid ( enum BlockStatus nUpTo = BLOCK_VALID_TRANSACTIONS ) const
2022-01-19 14:03:46 +01:00
EXCLUSIVE_LOCKS_REQUIRED ( : : cs_main )
2014-09-03 02:20:09 +02:00
{
2022-01-19 14:03:46 +01:00
AssertLockHeld ( : : cs_main ) ;
2014-09-03 02:20:09 +02:00
assert ( ! ( nUpTo & ~ BLOCK_VALID_MASK ) ) ; // Only validity flags allowed.
if ( nStatus & BLOCK_FAILED_MASK )
return false ;
return ( ( nStatus & BLOCK_VALID_MASK ) > = nUpTo ) ;
}
2021-07-21 19:31:14 +02:00
//! @returns true if the block is assumed-valid; this means it is queued to be
//! validated by a background chainstate.
2022-01-19 14:03:23 +01:00
bool IsAssumedValid ( ) const EXCLUSIVE_LOCKS_REQUIRED ( : : cs_main )
{
AssertLockHeld ( : : cs_main ) ;
return nStatus & BLOCK_ASSUMED_VALID ;
}
2021-07-21 19:31:14 +02:00
2014-10-25 10:46:54 +02:00
//! Raise the validity level of this block index entry.
//! Returns true if the validity was changed.
2022-01-19 14:03:37 +01:00
bool RaiseValidity ( enum BlockStatus nUpTo ) EXCLUSIVE_LOCKS_REQUIRED ( : : cs_main )
2014-09-03 02:20:09 +02:00
{
2022-01-19 14:03:37 +01:00
AssertLockHeld ( : : cs_main ) ;
2014-09-03 02:20:09 +02:00
assert ( ! ( nUpTo & ~ BLOCK_VALID_MASK ) ) ; // Only validity flags allowed.
2021-07-21 19:31:14 +02:00
if ( nStatus & BLOCK_FAILED_MASK ) return false ;
2014-09-03 02:20:09 +02:00
if ( ( nStatus & BLOCK_VALID_MASK ) < nUpTo ) {
2021-07-21 19:31:14 +02:00
// If this block had been marked assumed-valid and we're raising
// its validity to a certain point, there is no longer an assumption.
if ( nStatus & BLOCK_ASSUMED_VALID & & nUpTo > = BLOCK_VALID_SCRIPTS ) {
nStatus & = ~ BLOCK_ASSUMED_VALID ;
}
2014-09-03 02:20:09 +02:00
nStatus = ( nStatus & ~ BLOCK_VALID_MASK ) | nUpTo ;
return true ;
}
return false ;
}
2014-10-25 10:46:54 +02:00
//! Build the skiplist pointer for this entry.
2014-09-03 02:20:09 +02:00
void BuildSkip ( ) ;
2014-10-25 10:46:54 +02:00
//! Efficiently find an ancestor of this block.
2014-09-03 02:20:09 +02:00
CBlockIndex * GetAncestor ( int height ) ;
const CBlockIndex * GetAncestor ( int height ) const ;
2022-06-08 18:06:55 +02:00
CBlockIndex ( ) = default ;
~ CBlockIndex ( ) = default ;
protected :
//! CBlockIndex should not allow public copy construction because equality
//! comparison via pointer is very common throughout the codebase, making
//! use of copy a footgun. Also, use of copies do not have the benefit
//! of simplifying lifetime considerations due to attributes like pprev and
//! pskip, which are at risk of becoming dangling pointers in a copied
//! instance.
//!
//! We declare these protected instead of simply deleting them so that
//! CDiskBlockIndex can reuse copy construction.
CBlockIndex ( const CBlockIndex & ) = default ;
CBlockIndex & operator = ( const CBlockIndex & ) = delete ;
CBlockIndex ( CBlockIndex & & ) = delete ;
CBlockIndex & operator = ( CBlockIndex & & ) = delete ;
2014-09-03 02:20:09 +02:00
} ;
2015-11-30 00:46:49 +01:00
arith_uint256 GetBlockProof ( const CBlockIndex & block ) ;
/** Return the time it would take to redo the work difference between from and to, assuming the current hashrate corresponds to the difficulty at tip, in seconds. */
int64_t GetBlockProofEquivalentTime ( const CBlockIndex & to , const CBlockIndex & from , const CBlockIndex & tip , const Consensus : : Params & ) ;
2017-04-16 15:57:11 +02:00
/** Find the forking point between two chain tips. */
const CBlockIndex * LastCommonAncestor ( const CBlockIndex * pa , const CBlockIndex * pb ) ;
2015-11-30 00:46:49 +01:00
2014-09-03 02:20:09 +02:00
/** Used to marshal pointers into hashes for db storage. */
class CDiskBlockIndex : public CBlockIndex
{
public :
uint256 hashPrev ;
2021-12-14 12:48:59 +01:00
CDiskBlockIndex ( )
{
2014-12-15 09:11:16 +01:00
hashPrev = uint256 ( ) ;
2014-09-03 02:20:09 +02:00
}
2021-12-14 12:48:59 +01:00
explicit CDiskBlockIndex ( const CBlockIndex * pindex ) : CBlockIndex ( * pindex )
{
2014-12-15 09:11:16 +01:00
hashPrev = ( pprev ? pprev - > GetBlockHash ( ) : uint256 ( ) ) ;
2014-09-03 02:20:09 +02:00
}
2017-07-08 05:27:39 +02:00
SERIALIZE_METHODS ( CDiskBlockIndex , obj )
{
2022-01-19 13:42:32 +01:00
LOCK ( : : cs_main ) ;
2016-11-10 08:00:05 +01:00
int _nVersion = s . GetVersion ( ) ;
2020-02-07 04:57:32 +01:00
if ( ! ( s . GetType ( ) & SER_GETHASH ) ) READWRITE ( VARINT_MODE ( _nVersion , VarIntMode : : NONNEGATIVE_SIGNED ) ) ;
2017-07-08 05:27:39 +02:00
2020-02-07 04:57:32 +01:00
READWRITE ( VARINT_MODE ( obj . nHeight , VarIntMode : : NONNEGATIVE_SIGNED ) ) ;
2017-07-08 05:27:39 +02:00
READWRITE ( VARINT ( obj . nStatus ) ) ;
READWRITE ( VARINT ( obj . nTx ) ) ;
2020-02-07 04:57:32 +01:00
if ( obj . nStatus & ( BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO ) ) READWRITE ( VARINT_MODE ( obj . nFile , VarIntMode : : NONNEGATIVE_SIGNED ) ) ;
2017-07-08 05:27:39 +02:00
if ( obj . nStatus & BLOCK_HAVE_DATA ) READWRITE ( VARINT ( obj . nDataPos ) ) ;
if ( obj . nStatus & BLOCK_HAVE_UNDO ) READWRITE ( VARINT ( obj . nUndoPos ) ) ;
2014-09-03 02:20:09 +02:00
// block header
2017-07-08 05:27:39 +02:00
READWRITE ( obj . nVersion ) ;
READWRITE ( obj . hashPrev ) ;
READWRITE ( obj . hashMerkleRoot ) ;
READWRITE ( obj . nTime ) ;
READWRITE ( obj . nBits ) ;
READWRITE ( obj . nNonce ) ;
2014-09-03 02:20:09 +02:00
}
CDiskBlockIndex: rename GetBlockHash() to ConstructBlockHash()
and mark the inherited CBlockIndex#GetBlockHash public interface member
as deleted, to disallow calling it in the derived CDiskBlockIndex class.
Here is a failing test on master demonstrating the inconsistent behavior of the
current design: calling the same inherited public interface functions on the
same CDiskBlockIndex object should yield identical behavior.
```diff
diff --git a/src/test/validation_chainstatemanager_tests.cpp b/src/test/validation_chainstatemanager_tests.cpp
index 6dc522b421..dac3840f32 100644
--- a/src/test/validation_chainstatemanager_tests.cpp
+++ b/src/test/validation_chainstatemanager_tests.cpp
@@ -240,6 +240,15 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, TestChain100Setup)
const CBlockIndex* tip = chainman.ActiveTip();
BOOST_CHECK_EQUAL(tip->nChainTx, au_data.nChainTx);
+ // CDiskBlockIndex "is a" CBlockIndex, as it publicly inherits from it.
+ // Test that calling the same inherited interface functions on the same
+ // object yields identical behavior.
+ CDiskBlockIndex index{tip};
+ CBlockIndex *pB = &index;
+ CDiskBlockIndex *pD = &index;
+ BOOST_CHECK_EQUAL(pB->GetBlockHash(), pD->GetBlockHash());
+ BOOST_CHECK_EQUAL(pB->ToString(), pD->ToString());
+
```
The GetBlockHash() test assertion only passes on master because the different
methods invoked by the current design happen to return the same result. If one
of the two is changed, it fails like the ToString() assertion does.
Redefining inherited non-virtual functions is well-documented as incorrect
design to avoid inconsistent behavior (see Scott Meyers, "Effective C++", Item
36). Class usage is confusing when the behavior depends on the pointer
definition instead of the object definition (static binding happening where
dynamic binding was expected). This can lead to unsuspected or hard-to-track
bugs.
Outside of critical hot spots, correctness usually comes before optimisation,
but the current design dates back to main.cpp and it may possibly have been
chosen to avoid the overhead of dynamic dispatch. This solution does the same:
the class sizes are unchanged and no vptr or vtbl is added.
There are better designs for doing this that use composition instead of
inheritance or that separate the public interface from the private
implementations. One example of the latter would be a non-virtual public
interface that calls private virtual implementation methods, i.e. the Template
pattern via the Non-Virtual Interface (NVI) idiom.
2022-06-12 12:37:41 +02:00
uint256 ConstructBlockHash ( ) const
2014-09-03 02:20:09 +02:00
{
CBlockHeader block ;
2021-12-14 12:48:59 +01:00
block . nVersion = nVersion ;
block . hashPrevBlock = hashPrev ;
block . hashMerkleRoot = hashMerkleRoot ;
block . nTime = nTime ;
block . nBits = nBits ;
block . nNonce = nNonce ;
2014-09-03 02:20:09 +02:00
return block . GetHash ( ) ;
}
CDiskBlockIndex: rename GetBlockHash() to ConstructBlockHash()
and mark the inherited CBlockIndex#GetBlockHash public interface member
as deleted, to disallow calling it in the derived CDiskBlockIndex class.
Here is a failing test on master demonstrating the inconsistent behavior of the
current design: calling the same inherited public interface functions on the
same CDiskBlockIndex object should yield identical behavior.
```diff
diff --git a/src/test/validation_chainstatemanager_tests.cpp b/src/test/validation_chainstatemanager_tests.cpp
index 6dc522b421..dac3840f32 100644
--- a/src/test/validation_chainstatemanager_tests.cpp
+++ b/src/test/validation_chainstatemanager_tests.cpp
@@ -240,6 +240,15 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, TestChain100Setup)
const CBlockIndex* tip = chainman.ActiveTip();
BOOST_CHECK_EQUAL(tip->nChainTx, au_data.nChainTx);
+ // CDiskBlockIndex "is a" CBlockIndex, as it publicly inherits from it.
+ // Test that calling the same inherited interface functions on the same
+ // object yields identical behavior.
+ CDiskBlockIndex index{tip};
+ CBlockIndex *pB = &index;
+ CDiskBlockIndex *pD = &index;
+ BOOST_CHECK_EQUAL(pB->GetBlockHash(), pD->GetBlockHash());
+ BOOST_CHECK_EQUAL(pB->ToString(), pD->ToString());
+
```
The GetBlockHash() test assertion only passes on master because the different
methods invoked by the current design happen to return the same result. If one
of the two is changed, it fails like the ToString() assertion does.
Redefining inherited non-virtual functions is well-documented as incorrect
design to avoid inconsistent behavior (see Scott Meyers, "Effective C++", Item
36). Class usage is confusing when the behavior depends on the pointer
definition instead of the object definition (static binding happening where
dynamic binding was expected). This can lead to unsuspected or hard-to-track
bugs.
Outside of critical hot spots, correctness usually comes before optimisation,
but the current design dates back to main.cpp and it may possibly have been
chosen to avoid the overhead of dynamic dispatch. This solution does the same:
the class sizes are unchanged and no vptr or vtbl is added.
There are better designs for doing this that use composition instead of
inheritance or that separate the public interface from the private
implementations. One example of the latter would be a non-virtual public
interface that calls private virtual implementation methods, i.e. the Template
pattern via the Non-Virtual Interface (NVI) idiom.
2022-06-12 12:37:41 +02:00
uint256 GetBlockHash ( ) = delete ;
2022-06-12 12:38:38 +02:00
std : : string ToString ( ) = delete ;
2014-09-03 02:20:09 +02:00
} ;
/** An in-memory indexed chain of blocks. */
2021-12-14 12:48:59 +01:00
class CChain
{
2014-09-03 02:20:09 +02:00
private :
std : : vector < CBlockIndex * > vChain ;
public :
2021-12-14 12:39:29 +01:00
CChain ( ) = default ;
CChain ( const CChain & ) = delete ;
CChain & operator = ( const CChain & ) = delete ;
2017-08-07 07:36:37 +02:00
/** Returns the index entry for the genesis block of this chain, or nullptr if none. */
2021-12-14 12:48:59 +01:00
CBlockIndex * Genesis ( ) const
{
2017-08-07 07:36:37 +02:00
return vChain . size ( ) > 0 ? vChain [ 0 ] : nullptr ;
2014-09-03 02:20:09 +02:00
}
2017-08-07 07:36:37 +02:00
/** Returns the index entry for the tip of this chain, or nullptr if none. */
2021-12-14 12:48:59 +01:00
CBlockIndex * Tip ( ) const
{
2017-08-07 07:36:37 +02:00
return vChain . size ( ) > 0 ? vChain [ vChain . size ( ) - 1 ] : nullptr ;
2014-09-03 02:20:09 +02:00
}
2017-08-07 07:36:37 +02:00
/** Returns the index entry at a particular height in this chain, or nullptr if no such height exists. */
2021-12-14 12:48:59 +01:00
CBlockIndex * operator [ ] ( int nHeight ) const
{
2014-09-03 02:20:09 +02:00
if ( nHeight < 0 | | nHeight > = ( int ) vChain . size ( ) )
2017-08-07 07:36:37 +02:00
return nullptr ;
2014-09-03 02:20:09 +02:00
return vChain [ nHeight ] ;
}
/** Efficiently check whether a block is present in this chain. */
2021-12-14 12:48:59 +01:00
bool Contains ( const CBlockIndex * pindex ) const
{
2014-09-03 02:20:09 +02:00
return ( * this ) [ pindex - > nHeight ] = = pindex ;
}
2017-08-07 07:36:37 +02:00
/** Find the successor of a block in this chain, or nullptr if the given index is not found or is the tip. */
2021-12-14 12:48:59 +01:00
CBlockIndex * Next ( const CBlockIndex * pindex ) const
{
2014-09-03 02:20:09 +02:00
if ( Contains ( pindex ) )
return ( * this ) [ pindex - > nHeight + 1 ] ;
else
2017-08-07 07:36:37 +02:00
return nullptr ;
2014-09-03 02:20:09 +02:00
}
/** Return the maximal height in the chain. Is equal to chain.Tip() ? chain.Tip()->nHeight : -1. */
2021-12-14 12:48:59 +01:00
int Height ( ) const
{
2022-01-25 11:18:03 +01:00
return int ( vChain . size ( ) ) - 1 ;
2014-09-03 02:20:09 +02:00
}
2014-10-20 03:41:37 +02:00
/** Set/initialize a chain with a given tip. */
2022-04-29 09:41:09 +02:00
void SetTip ( CBlockIndex & block ) ;
2014-09-03 02:20:09 +02:00
2022-08-08 02:56:17 +02:00
/** Return a CBlockLocator that refers to the tip in of this chain. */
CBlockLocator GetLocator ( ) const ;
2014-09-03 02:20:09 +02:00
/** Find the last common block between this chain and a block index entry. */
2021-12-14 12:48:59 +01:00
const CBlockIndex * FindFork ( const CBlockIndex * pindex ) const ;
2016-06-16 16:57:48 +02:00
2019-03-26 17:46:22 +01:00
/** Find the earliest block with timestamp equal or greater than the given time and height equal or greater than the given height. */
CBlockIndex * FindEarliestAtLeast ( int64_t nTime , int height ) const ;
2014-09-03 02:20:09 +02:00
} ;
2022-08-08 02:56:17 +02:00
/** Get a locator for a block index entry. */
CBlockLocator GetLocator ( const CBlockIndex * index ) ;
/** Construct a list of hash entries to put in a locator. */
std : : vector < uint256 > LocatorEntries ( const CBlockIndex * index ) ;
2014-11-03 16:16:40 +01:00
# endif // BITCOIN_CHAIN_H