Changeset View
Changeset View
Standalone View
Standalone View
src/chain.h
Show First 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | |||||
* the root, with each block potentially having multiple candidates to be the | * 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 | * 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. | * one of them can be part of the currently active branch. | ||||
*/ | */ | ||||
class CBlockIndex { | class CBlockIndex { | ||||
public: | public: | ||||
//! pointer to the hash of the block, if any. Memory is owned by this | //! pointer to the hash of the block, if any. Memory is owned by this | ||||
//! CBlockIndex | //! CBlockIndex | ||||
const uint256 *phashBlock; | const BlockHash *phashBlock; | ||||
//! pointer to the index of the predecessor of this block | //! pointer to the index of the predecessor of this block | ||||
CBlockIndex *pprev; | CBlockIndex *pprev; | ||||
//! pointer to the index of some further predecessor of this block | //! pointer to the index of some further predecessor of this block | ||||
CBlockIndex *pskip; | CBlockIndex *pskip; | ||||
//! height of the entry in the chain. The genesis block has height 0 | //! height of the entry in the chain. The genesis block has height 0 | ||||
▲ Show 20 Lines • Show All 106 Lines • ▼ Show 20 Lines | CBlockHeader GetBlockHeader() const { | ||||
} | } | ||||
block.hashMerkleRoot = hashMerkleRoot; | block.hashMerkleRoot = hashMerkleRoot; | ||||
block.nTime = nTime; | block.nTime = nTime; | ||||
block.nBits = nBits; | block.nBits = nBits; | ||||
block.nNonce = nNonce; | block.nNonce = nNonce; | ||||
return block; | return block; | ||||
} | } | ||||
uint256 GetBlockHash() const { return *phashBlock; } | BlockHash GetBlockHash() const { return *phashBlock; } | ||||
int64_t GetBlockTime() const { return int64_t(nTime); } | int64_t GetBlockTime() const { return int64_t(nTime); } | ||||
int64_t GetBlockTimeMax() const { return int64_t(nTimeMax); } | int64_t GetBlockTimeMax() const { return int64_t(nTimeMax); } | ||||
int64_t GetHeaderReceivedTime() const { return nTimeReceived; } | int64_t GetHeaderReceivedTime() const { return nTimeReceived; } | ||||
int64_t GetReceivedTimeDiff() const { | int64_t GetReceivedTimeDiff() const { | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | |||||
/** | /** | ||||
* Maintain a map of CBlockIndex for all known headers. | * Maintain a map of CBlockIndex for all known headers. | ||||
*/ | */ | ||||
struct BlockHasher { | struct BlockHasher { | ||||
// this used to call `GetCheapHash()` in uint256, which was later moved; the | // this used to call `GetCheapHash()` in uint256, which was later moved; the | ||||
// cheap hash function simply calls ReadLE64() however, so the end result is | // cheap hash function simply calls ReadLE64() however, so the end result is | ||||
// identical | // identical | ||||
size_t operator()(const uint256 &hash) const { | size_t operator()(const BlockHash &hash) const { | ||||
return ReadLE64(hash.begin()); | return ReadLE64(hash.begin()); | ||||
} | } | ||||
}; | }; | ||||
typedef std::unordered_map<uint256, CBlockIndex *, BlockHasher> BlockMap; | typedef std::unordered_map<BlockHash, CBlockIndex *, BlockHasher> BlockMap; | ||||
extern BlockMap &mapBlockIndex; | extern BlockMap &mapBlockIndex; | ||||
extern CCriticalSection cs_main; | extern CCriticalSection cs_main; | ||||
inline CBlockIndex *LookupBlockIndex(const uint256 &hash) { | inline CBlockIndex *LookupBlockIndex(const BlockHash &hash) { | ||||
AssertLockHeld(cs_main); | AssertLockHeld(cs_main); | ||||
BlockMap::const_iterator it = mapBlockIndex.find(hash); | BlockMap::const_iterator it = mapBlockIndex.find(hash); | ||||
return it == mapBlockIndex.end() ? nullptr : it->second; | return it == mapBlockIndex.end() ? nullptr : it->second; | ||||
} | } | ||||
arith_uint256 GetBlockProof(const CBlockIndex &block); | arith_uint256 GetBlockProof(const CBlockIndex &block); | ||||
/** | /** | ||||
Show All 14 Lines | |||||
/** | /** | ||||
* Check if two block index are on the same fork. | * Check if two block index are on the same fork. | ||||
*/ | */ | ||||
bool AreOnTheSameFork(const CBlockIndex *pa, const CBlockIndex *pb); | bool AreOnTheSameFork(const CBlockIndex *pa, const CBlockIndex *pb); | ||||
/** Used to marshal pointers into hashes for db storage. */ | /** Used to marshal pointers into hashes for db storage. */ | ||||
class CDiskBlockIndex : public CBlockIndex { | class CDiskBlockIndex : public CBlockIndex { | ||||
public: | public: | ||||
uint256 hashPrev; | BlockHash hashPrev; | ||||
CDiskBlockIndex() { hashPrev = uint256(); } | CDiskBlockIndex() { hashPrev = BlockHash(); } | ||||
explicit CDiskBlockIndex(const CBlockIndex *pindex) : CBlockIndex(*pindex) { | explicit CDiskBlockIndex(const CBlockIndex *pindex) : CBlockIndex(*pindex) { | ||||
hashPrev = (pprev ? pprev->GetBlockHash() : uint256()); | hashPrev = (pprev ? pprev->GetBlockHash() : BlockHash()); | ||||
} | } | ||||
ADD_SERIALIZE_METHODS; | ADD_SERIALIZE_METHODS; | ||||
template <typename Stream, typename Operation> | template <typename Stream, typename Operation> | ||||
inline void SerializationOp(Stream &s, Operation ser_action) { | inline void SerializationOp(Stream &s, Operation ser_action) { | ||||
int _nVersion = s.GetVersion(); | int _nVersion = s.GetVersion(); | ||||
if (!(s.GetType() & SER_GETHASH)) { | if (!(s.GetType() & SER_GETHASH)) { | ||||
Show All 17 Lines | inline void SerializationOp(Stream &s, Operation ser_action) { | ||||
READWRITE(this->nVersion); | READWRITE(this->nVersion); | ||||
READWRITE(hashPrev); | READWRITE(hashPrev); | ||||
READWRITE(hashMerkleRoot); | READWRITE(hashMerkleRoot); | ||||
READWRITE(nTime); | READWRITE(nTime); | ||||
READWRITE(nBits); | READWRITE(nBits); | ||||
READWRITE(nNonce); | READWRITE(nNonce); | ||||
} | } | ||||
uint256 GetBlockHash() const { | BlockHash GetBlockHash() const { | ||||
CBlockHeader block; | CBlockHeader block; | ||||
block.nVersion = nVersion; | block.nVersion = nVersion; | ||||
block.hashPrevBlock = hashPrev; | block.hashPrevBlock = hashPrev; | ||||
block.hashMerkleRoot = hashMerkleRoot; | block.hashMerkleRoot = hashMerkleRoot; | ||||
block.nTime = nTime; | block.nTime = nTime; | ||||
block.nBits = nBits; | block.nBits = nBits; | ||||
block.nNonce = nNonce; | block.nNonce = nNonce; | ||||
return block.GetHash(); | return block.GetHash(); | ||||
▲ Show 20 Lines • Show All 95 Lines • Show Last 20 Lines |