Changeset View
Changeset View
Standalone View
Standalone View
src/chain.h
- This file was copied to src/blockindex.h.
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_CHAIN_H | #ifndef BITCOIN_CHAIN_H | ||||
#define BITCOIN_CHAIN_H | #define BITCOIN_CHAIN_H | ||||
#include "arith_uint256.h" | #include "arith_uint256.h" | ||||
#include "blockstatus.h" | #include "blockindex.h" | ||||
#include "blockvalidity.h" | |||||
#include "consensus/params.h" | #include "consensus/params.h" | ||||
#include "diskblockpos.h" | #include "diskblockpos.h" | ||||
#include "pow.h" | #include "pow.h" | ||||
#include "primitives/block.h" | #include "primitives/block.h" | ||||
#include "tinyformat.h" | #include "tinyformat.h" | ||||
#include "uint256.h" | #include "uint256.h" | ||||
#include <unordered_map> | #include <unordered_map> | ||||
Show All 9 Lines | |||||
* Timestamp window used as a grace period by code that compares external | * Timestamp window used as a grace period by code that compares external | ||||
* timestamps (such as timestamps passed to RPCs, or wallet key creation times) | * timestamps (such as timestamps passed to RPCs, or wallet key creation times) | ||||
* to block timestamps. This should be set at least as high as | * to block timestamps. This should be set at least as high as | ||||
* MAX_FUTURE_BLOCK_TIME. | * MAX_FUTURE_BLOCK_TIME. | ||||
*/ | */ | ||||
static const int64_t TIMESTAMP_WINDOW = MAX_FUTURE_BLOCK_TIME; | static const int64_t TIMESTAMP_WINDOW = MAX_FUTURE_BLOCK_TIME; | ||||
/** | /** | ||||
* 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: | |||||
//! pointer to the hash of the block, if any. Memory is owned by this | |||||
//! CBlockIndex | |||||
const uint256 *phashBlock; | |||||
//! pointer to the index of the predecessor of this block | |||||
CBlockIndex *pprev; | |||||
//! pointer to the index of some further predecessor of this block | |||||
CBlockIndex *pskip; | |||||
//! height of the entry in the chain. The genesis block has height 0 | |||||
int nHeight; | |||||
//! Which # file this block is stored in (blk?????.dat) | |||||
int nFile; | |||||
//! Byte offset within blk?????.dat where this block's data is stored | |||||
unsigned int nDataPos; | |||||
//! Byte offset within rev?????.dat where this block's undo data is stored | |||||
unsigned int nUndoPos; | |||||
//! (memory only) Total amount of work (expected number of hashes) in the | |||||
//! chain up to and including this block | |||||
arith_uint256 nChainWork; | |||||
//! Number of transactions in this block. | |||||
//! Note: in a potential headers-first mode, this number cannot be relied | |||||
//! upon | |||||
unsigned int nTx; | |||||
//! (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; | |||||
//! Verification status of this block. See enum BlockStatus | |||||
BlockStatus nStatus; | |||||
//! block header | |||||
int32_t nVersion; | |||||
uint256 hashMerkleRoot; | |||||
uint32_t nTime; | |||||
uint32_t nBits; | |||||
uint32_t nNonce; | |||||
//! (memory only) Sequential id assigned to distinguish order in which | |||||
//! blocks are received. | |||||
int32_t nSequenceId; | |||||
//! (memory only) block header metadata | |||||
uint64_t nTimeReceived; | |||||
//! (memory only) Maximum nTime in the chain upto and including this block. | |||||
unsigned int nTimeMax; | |||||
void SetNull() { | |||||
phashBlock = nullptr; | |||||
pprev = nullptr; | |||||
pskip = nullptr; | |||||
nHeight = 0; | |||||
nFile = 0; | |||||
nDataPos = 0; | |||||
nUndoPos = 0; | |||||
nChainWork = arith_uint256(); | |||||
nTx = 0; | |||||
nChainTx = 0; | |||||
nStatus = BlockStatus(); | |||||
nSequenceId = 0; | |||||
nTimeMax = 0; | |||||
nVersion = 0; | |||||
hashMerkleRoot = uint256(); | |||||
nTime = 0; | |||||
nTimeReceived = 0; | |||||
nBits = 0; | |||||
nNonce = 0; | |||||
} | |||||
CBlockIndex() { SetNull(); } | |||||
CBlockIndex(const CBlockHeader &block) { | |||||
SetNull(); | |||||
nVersion = block.nVersion; | |||||
hashMerkleRoot = block.hashMerkleRoot; | |||||
nTime = block.nTime; | |||||
// Default to block time if nTimeReceived is never set, which | |||||
// in effect assumes that this block is honestly mined. | |||||
// Note that nTimeReceived isn't written to disk, so blocks read from | |||||
// disk will be assumed to be honestly mined. | |||||
nTimeReceived = block.nTime; | |||||
nBits = block.nBits; | |||||
nNonce = block.nNonce; | |||||
} | |||||
CDiskBlockPos GetBlockPos() const { | |||||
CDiskBlockPos ret; | |||||
if (nStatus.hasData()) { | |||||
ret.nFile = nFile; | |||||
ret.nPos = nDataPos; | |||||
} | |||||
return ret; | |||||
} | |||||
CDiskBlockPos GetUndoPos() const { | |||||
CDiskBlockPos ret; | |||||
if (nStatus.hasUndo()) { | |||||
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; | |||||
block.nTime = nTime; | |||||
block.nBits = nBits; | |||||
block.nNonce = nNonce; | |||||
return block; | |||||
} | |||||
uint256 GetBlockHash() const { return *phashBlock; } | |||||
int64_t GetBlockTime() const { return int64_t(nTime); } | |||||
int64_t GetBlockTimeMax() const { return int64_t(nTimeMax); } | |||||
int64_t GetHeaderReceivedTime() const { return nTimeReceived; } | |||||
int64_t GetReceivedTimeDiff() const { | |||||
return GetHeaderReceivedTime() - GetBlockTime(); | |||||
} | |||||
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 { | |||||
return strprintf( | |||||
"CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)", pprev, | |||||
nHeight, hashMerkleRoot.ToString(), GetBlockHash().ToString()); | |||||
} | |||||
//! Check whether this block index entry is valid up to the passed validity | |||||
//! level. | |||||
bool IsValid(enum BlockValidity nUpTo = BlockValidity::TRANSACTIONS) const { | |||||
return nStatus.isValid(nUpTo); | |||||
} | |||||
//! Raise the validity level of this block index entry. | |||||
//! Returns true if the validity was changed. | |||||
bool RaiseValidity(enum BlockValidity nUpTo) { | |||||
// Only validity flags allowed. | |||||
if (nStatus.isInvalid()) { | |||||
return false; | |||||
} | |||||
if (nStatus.getValidity() >= nUpTo) { | |||||
return false; | |||||
} | |||||
nStatus = nStatus.withValidity(nUpTo); | |||||
return true; | |||||
} | |||||
//! Build the skiplist pointer for this entry. | |||||
void BuildSkip(); | |||||
//! Efficiently find an ancestor of this block. | |||||
CBlockIndex *GetAncestor(int height); | |||||
const CBlockIndex *GetAncestor(int height) const; | |||||
}; | |||||
/** | |||||
* Maintain a map of CBlockIndex for all known headers. | * Maintain a map of CBlockIndex for all known headers. | ||||
*/ | */ | ||||
struct BlockHasher { | struct BlockHasher { | ||||
size_t operator()(const uint256 &hash) const { return hash.GetCheapHash(); } | size_t operator()(const uint256 &hash) const { return hash.GetCheapHash(); } | ||||
}; | }; | ||||
typedef std::unordered_map<uint256, CBlockIndex *, BlockHasher> BlockMap; | typedef std::unordered_map<uint256, CBlockIndex *, BlockHasher> BlockMap; | ||||
extern BlockMap mapBlockIndex; | extern BlockMap mapBlockIndex; | ||||
Show All 10 Lines | int64_t GetBlockProofEquivalentTime(const CBlockIndex &to, | ||||
const CBlockIndex &tip, | const CBlockIndex &tip, | ||||
const Consensus::Params &); | const Consensus::Params &); | ||||
/** | /** | ||||
* Find the forking point between two chain tips. | * Find the forking point between two chain tips. | ||||
*/ | */ | ||||
const CBlockIndex *LastCommonAncestor(const CBlockIndex *pa, | const CBlockIndex *LastCommonAncestor(const CBlockIndex *pa, | ||||
const CBlockIndex *pb); | const CBlockIndex *pb); | ||||
/** Used to marshal pointers into hashes for db storage. */ | /** | ||||
* Used to marshal pointers into hashes for db storage. | |||||
* TODO: Once CDiskBlockIndex is removed chain.h should | |||||
* use a forward declaration for CBlockIndex. | |||||
*/ | |||||
class CDiskBlockIndex : public CBlockIndex { | class CDiskBlockIndex : public CBlockIndex { | ||||
public: | public: | ||||
uint256 hashPrev; | uint256 hashPrev; | ||||
CDiskBlockIndex() { hashPrev = uint256(); } | CDiskBlockIndex() { hashPrev = uint256(); } | ||||
explicit CDiskBlockIndex(const CBlockIndex *pindex) : CBlockIndex(*pindex) { | explicit CDiskBlockIndex(const CBlockIndex *pindex) : CBlockIndex(*pindex) { | ||||
hashPrev = (pprev ? pprev->GetBlockHash() : uint256()); | hashPrev = (pprev ? pprev->GetBlockHash() : uint256()); | ||||
▲ Show 20 Lines • Show All 137 Lines • Show Last 20 Lines |