2014-09-03 02:20:09 +02:00
// Copyright (c) 2009-2010 Satoshi Nakamoto
2015-12-13 17:58:29 +01:00
// Copyright (c) 2009-2015 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
2014-12-16 15:43:03 +01:00
# include "arith_uint256.h"
2014-11-18 22:03:02 +01:00
# include "primitives/block.h"
2014-09-03 02:20:09 +02:00
# include "pow.h"
2014-09-25 05:32:36 +02:00
# include "tinyformat.h"
2014-09-03 02:20:09 +02:00
# include "uint256.h"
# include <vector>
struct CDiskBlockPos
{
int nFile ;
unsigned int nPos ;
ADD_SERIALIZE_METHODS ;
template < typename Stream , typename Operation >
inline void SerializationOp ( Stream & s , Operation ser_action , int nType , int nVersion ) {
READWRITE ( VARINT ( nFile ) ) ;
READWRITE ( VARINT ( nPos ) ) ;
}
CDiskBlockPos ( ) {
SetNull ( ) ;
}
CDiskBlockPos ( int nFileIn , unsigned int nPosIn ) {
nFile = nFileIn ;
nPos = nPosIn ;
}
friend bool operator = = ( const CDiskBlockPos & a , const CDiskBlockPos & b ) {
return ( a . nFile = = b . nFile & & a . nPos = = b . nPos ) ;
}
friend bool operator ! = ( const CDiskBlockPos & a , const CDiskBlockPos & b ) {
return ! ( a = = b ) ;
}
void SetNull ( ) { nFile = - 1 ; nPos = 0 ; }
bool IsNull ( ) const { return ( nFile = = - 1 ) ; }
2015-01-26 09:47:59 +01:00
std : : string ToString ( ) const
{
return strprintf ( " CBlockDiskPos(nFile=%i, nPos=%i) " , nFile, nPos) ;
}
2014-09-03 02:20:09 +02:00
} ;
enum BlockStatus {
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
2014-10-25 10:46:54 +02:00
//! Parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, timestamp not in future
2014-07-12 00:02:35 +02:00
BLOCK_VALID_HEADER = 1 ,
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.
2014-09-03 02:20:09 +02:00
BLOCK_VALID_MASK = BLOCK_VALID_HEADER | BLOCK_VALID_TREE | BLOCK_VALID_TRANSACTIONS |
BLOCK_VALID_CHAIN | BLOCK_VALID_SCRIPTS ,
2014-10-25 10:46:54 +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 ,
2014-10-25 10:46:54 +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 ,
} ;
/** 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
2014-09-03 02:20:09 +02:00
const uint256 * phashBlock ;
2014-10-25 10:46:54 +02:00
//! pointer to the index of the predecessor of this block
2014-09-03 02:20:09 +02:00
CBlockIndex * pprev ;
2014-10-25 10:46:54 +02:00
//! pointer to the index of some further predecessor of this block
2014-09-03 02:20:09 +02:00
CBlockIndex * pskip ;
2014-10-25 10:46:54 +02:00
//! height of the entry in the chain. The genesis block has height 0
2014-09-03 02:20:09 +02:00
int nHeight ;
2014-10-25 10:46:54 +02:00
//! Which # file this block is stored in (blk?????.dat)
2014-09-03 02:20:09 +02:00
int nFile ;
2014-10-25 10:46:54 +02:00
//! Byte offset within blk?????.dat where this block's data is stored
2014-09-03 02:20:09 +02:00
unsigned int nDataPos ;
2014-10-25 10:46:54 +02:00
//! Byte offset within rev?????.dat where this block's undo data is stored
2014-09-03 02:20:09 +02:00
unsigned int nUndoPos ;
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
2014-12-16 15:43:03 +01: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
2014-09-03 02:20:09 +02:00
unsigned int nTx ;
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.
//! Change to 64-bit type when necessary; won't happen before 2030
unsigned int nChainTx ;
2014-09-03 02:20:09 +02:00
2014-10-25 10:46:54 +02:00
//! Verification status of this block. See enum BlockStatus
2014-09-03 02:20:09 +02:00
unsigned int nStatus ;
2014-10-25 10:46:54 +02:00
//! block header
2014-09-03 02:20:09 +02:00
int nVersion ;
uint256 hashMerkleRoot ;
2015-09-24 07:15:28 +02:00
uint256 hashClaimTrie ;
2014-09-03 02:20:09 +02:00
unsigned int nTime ;
unsigned int nBits ;
unsigned int nNonce ;
2014-10-25 10:46:54 +02:00
//! (memory only) Sequential id assigned to distinguish order in which blocks are received.
2014-09-03 02:20:09 +02:00
uint32_t nSequenceId ;
void SetNull ( )
{
phashBlock = NULL ;
pprev = NULL ;
pskip = NULL ;
nHeight = 0 ;
nFile = 0 ;
nDataPos = 0 ;
nUndoPos = 0 ;
2014-12-16 15:43:03 +01:00
nChainWork = arith_uint256 ( ) ;
2014-09-03 02:20:09 +02:00
nTx = 0 ;
nChainTx = 0 ;
nStatus = 0 ;
nSequenceId = 0 ;
nVersion = 0 ;
2014-12-15 09:11:16 +01:00
hashMerkleRoot = uint256 ( ) ;
2015-09-24 07:15:28 +02:00
hashClaimTrie = uint256 ( ) ;
2014-09-03 02:20:09 +02:00
nTime = 0 ;
nBits = 0 ;
nNonce = 0 ;
}
CBlockIndex ( )
{
SetNull ( ) ;
}
2014-07-12 00:02:35 +02:00
CBlockIndex ( const CBlockHeader & block )
2014-09-03 02:20:09 +02:00
{
SetNull ( ) ;
nVersion = block . nVersion ;
hashMerkleRoot = block . hashMerkleRoot ;
2015-09-24 07:15:28 +02:00
hashClaimTrie = block . hashClaimTrie ;
2014-09-03 02:20:09 +02:00
nTime = block . nTime ;
nBits = block . nBits ;
nNonce = block . nNonce ;
}
CDiskBlockPos GetBlockPos ( ) const {
CDiskBlockPos ret ;
if ( nStatus & BLOCK_HAVE_DATA ) {
ret . nFile = nFile ;
ret . nPos = nDataPos ;
}
return ret ;
}
CDiskBlockPos GetUndoPos ( ) const {
CDiskBlockPos ret ;
if ( nStatus & BLOCK_HAVE_UNDO ) {
ret . nFile = nFile ;
ret . nPos = nUndoPos ;
}
return ret ;
}
CBlockHeader GetBlockHeader ( ) const
{
CBlockHeader block ;
block . nVersion = nVersion ;
if ( pprev )
block . hashPrevBlock = pprev - > GetBlockHash ( ) ;
block . hashMerkleRoot = hashMerkleRoot ;
2015-09-24 07:15:28 +02:00
block . hashClaimTrie = hashClaimTrie ;
2014-09-03 02:20:09 +02:00
block . nTime = nTime ;
block . nBits = nBits ;
block . nNonce = nNonce ;
return block ;
}
uint256 GetBlockHash ( ) const
{
return * phashBlock ;
}
2016-04-02 03:37:44 +02:00
uint256 GetBlockPoWHash ( ) const
{
return GetBlockHeader ( ) . GetPoWHash ( ) ;
}
2014-09-03 02:20:09 +02:00
int64_t GetBlockTime ( ) const
{
return ( int64_t ) nTime ;
}
enum { nMedianTimeSpan = 11 } ;
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 ) ;
return pbegin [ ( pend - pbegin ) / 2 ] ;
}
std : : string ToString ( ) const
{
2015-09-24 07:15:28 +02:00
return strprintf ( " CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, claimtrie=%s, hashBlock=%s) " ,
2014-09-03 02:20:09 +02:00
pprev , nHeight ,
hashMerkleRoot . ToString ( ) ,
2015-09-24 07:15:28 +02:00
hashClaimTrie . ToString ( ) ,
2014-09-03 02:20:09 +02:00
GetBlockHash ( ) . ToString ( ) ) ;
}
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
{
assert ( ! ( nUpTo & ~ BLOCK_VALID_MASK ) ) ; // Only validity flags allowed.
if ( nStatus & BLOCK_FAILED_MASK )
return false ;
return ( ( nStatus & BLOCK_VALID_MASK ) > = nUpTo ) ;
}
2014-10-25 10:46:54 +02:00
//! Raise the validity level of this block index entry.
//! Returns true if the validity was changed.
2014-09-03 02:20:09 +02:00
bool RaiseValidity ( enum BlockStatus nUpTo )
{
assert ( ! ( nUpTo & ~ BLOCK_VALID_MASK ) ) ; // Only validity flags allowed.
if ( nStatus & BLOCK_FAILED_MASK )
return false ;
if ( ( nStatus & BLOCK_VALID_MASK ) < nUpTo ) {
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 ;
} ;
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 & ) ;
2014-09-03 02:20:09 +02:00
/** Used to marshal pointers into hashes for db storage. */
class CDiskBlockIndex : public CBlockIndex
{
public :
uint256 hashPrev ;
CDiskBlockIndex ( ) {
2014-12-15 09:11:16 +01:00
hashPrev = uint256 ( ) ;
2014-09-03 02:20:09 +02:00
}
2014-11-25 16:26:20 +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
}
ADD_SERIALIZE_METHODS ;
template < typename Stream , typename Operation >
inline void SerializationOp ( Stream & s , Operation ser_action , int nType , int nVersion ) {
if ( ! ( nType & SER_GETHASH ) )
READWRITE ( VARINT ( nVersion ) ) ;
READWRITE ( VARINT ( nHeight ) ) ;
READWRITE ( VARINT ( nStatus ) ) ;
READWRITE ( VARINT ( nTx ) ) ;
if ( nStatus & ( BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO ) )
READWRITE ( VARINT ( nFile ) ) ;
if ( nStatus & BLOCK_HAVE_DATA )
READWRITE ( VARINT ( nDataPos ) ) ;
if ( nStatus & BLOCK_HAVE_UNDO )
READWRITE ( VARINT ( nUndoPos ) ) ;
// block header
READWRITE ( this - > nVersion ) ;
READWRITE ( hashPrev ) ;
READWRITE ( hashMerkleRoot ) ;
2015-09-24 07:15:28 +02:00
READWRITE ( hashClaimTrie ) ;
2014-09-03 02:20:09 +02:00
READWRITE ( nTime ) ;
READWRITE ( nBits ) ;
READWRITE ( nNonce ) ;
}
uint256 GetBlockHash ( ) const
{
CBlockHeader block ;
block . nVersion = nVersion ;
block . hashPrevBlock = hashPrev ;
block . hashMerkleRoot = hashMerkleRoot ;
2015-09-24 07:15:28 +02:00
block . hashClaimTrie = hashClaimTrie ;
2014-09-03 02:20:09 +02:00
block . nTime = nTime ;
block . nBits = nBits ;
block . nNonce = nNonce ;
return block . GetHash ( ) ;
}
std : : string ToString ( ) const
{
std : : string str = " CDiskBlockIndex( " ;
str + = CBlockIndex : : ToString ( ) ;
str + = strprintf ( " \n hashBlock=%s, hashPrev=%s) " ,
GetBlockHash ( ) . ToString ( ) ,
hashPrev . ToString ( ) ) ;
return str ;
}
} ;
/** An in-memory indexed chain of blocks. */
class CChain {
private :
std : : vector < CBlockIndex * > vChain ;
public :
/** Returns the index entry for the genesis block of this chain, or NULL if none. */
CBlockIndex * Genesis ( ) const {
return vChain . size ( ) > 0 ? vChain [ 0 ] : NULL ;
}
/** Returns the index entry for the tip of this chain, or NULL if none. */
CBlockIndex * Tip ( ) const {
return vChain . size ( ) > 0 ? vChain [ vChain . size ( ) - 1 ] : NULL ;
}
/** Returns the index entry at a particular height in this chain, or NULL if no such height exists. */
CBlockIndex * operator [ ] ( int nHeight ) const {
if ( nHeight < 0 | | nHeight > = ( int ) vChain . size ( ) )
return NULL ;
return vChain [ nHeight ] ;
}
/** Compare two chains efficiently. */
friend bool operator = = ( const CChain & a , const CChain & b ) {
return a . vChain . size ( ) = = b . vChain . size ( ) & &
a . vChain [ a . vChain . size ( ) - 1 ] = = b . vChain [ b . vChain . size ( ) - 1 ] ;
}
/** Efficiently check whether a block is present in this chain. */
bool Contains ( const CBlockIndex * pindex ) const {
return ( * this ) [ pindex - > nHeight ] = = pindex ;
}
/** Find the successor of a block in this chain, or NULL if the given index is not found or is the tip. */
CBlockIndex * Next ( const CBlockIndex * pindex ) const {
if ( Contains ( pindex ) )
return ( * this ) [ pindex - > nHeight + 1 ] ;
else
return NULL ;
}
/** Return the maximal height in the chain. Is equal to chain.Tip() ? chain.Tip()->nHeight : -1. */
int Height ( ) const {
return vChain . size ( ) - 1 ;
}
2014-10-20 03:41:37 +02:00
/** Set/initialize a chain with a given tip. */
void SetTip ( CBlockIndex * pindex ) ;
2014-09-03 02:20:09 +02:00
/** Return a CBlockLocator that refers to a block in this chain (by default the tip). */
CBlockLocator GetLocator ( const CBlockIndex * pindex = NULL ) const ;
/** Find the last common block between this chain and a block index entry. */
const CBlockIndex * FindFork ( const CBlockIndex * pindex ) const ;
} ;
2014-11-03 16:16:40 +01:00
# endif // BITCOIN_CHAIN_H