diff --git a/src/chain.h b/src/chain.h --- a/src/chain.h +++ b/src/chain.h @@ -7,28 +7,34 @@ #define BITCOIN_CHAIN_H #include "arith_uint256.h" -#include "primitives/block.h" #include "pow.h" +#include "primitives/block.h" #include "tinyformat.h" #include "uint256.h" #include -class CBlockFileInfo -{ +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 + //!< number of blocks stored in file + unsigned int nBlocks; + //!< number of used bytes of block file + unsigned int nSize; + //!< number of used bytes in the undo file + unsigned int nUndoSize; + //!< lowest height of block in file + unsigned int nHeightFirst; + //!< highest height of block in file + unsigned int nHeightLast; + //!< earliest time of block in file + uint64_t nTimeFirst; + //!< latest time of block in file + uint64_t nTimeLast; ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action) { + inline void SerializationOp(Stream &s, Operation ser_action) { READWRITE(VARINT(nBlocks)); READWRITE(VARINT(nSize)); READWRITE(VARINT(nUndoSize)); @@ -38,52 +44,43 @@ READWRITE(VARINT(nTimeLast)); } - 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; - } + 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; + } }; -struct CDiskBlockPos -{ +struct CDiskBlockPos { int nFile; unsigned int nPos; ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action) { + inline void SerializationOp(Stream &s, Operation ser_action) { READWRITE(VARINT(nFile)); READWRITE(VARINT(nPos)); } - CDiskBlockPos() { - SetNull(); - } + CDiskBlockPos() { SetNull(); } CDiskBlockPos(int nFileIn, unsigned int nPosIn) { nFile = nFileIn; @@ -98,70 +95,81 @@ return !(a == b); } - void SetNull() { nFile = -1; nPos = 0; } + void SetNull() { + nFile = -1; + nPos = 0; + } bool IsNull() const { return (nFile == -1); } - std::string ToString() const - { + std::string ToString() const { return strprintf("CBlockDiskPos(nFile=%i, nPos=%i)", nFile, nPos); } - }; -enum BlockStatus: uint32_t { +enum BlockStatus : uint32_t { //! Unused. - BLOCK_VALID_UNKNOWN = 0, + BLOCK_VALID_UNKNOWN = 0, - //! Parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, timestamp not in future - BLOCK_VALID_HEADER = 1, + //! Parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, + //! timestamp not in future + BLOCK_VALID_HEADER = 1, - //! All parent headers found, difficulty matches, timestamp >= median previous, checkpoint. Implies all parents - //! are also at least TREE. - BLOCK_VALID_TREE = 2, + //! All parent headers found, difficulty matches, timestamp >= median + //! previous, checkpoint. Implies all parents are also at least TREE. + BLOCK_VALID_TREE = 2, /** - * 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. + * 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. */ - BLOCK_VALID_TRANSACTIONS = 3, + BLOCK_VALID_TRANSACTIONS = 3, - //! Outputs do not overspend inputs, no double spends, coinbase output ok, no immature coinbase spends, BIP30. + //! Outputs do not overspend inputs, no double spends, coinbase output ok, + //! no immature coinbase spends, BIP30. //! Implies all parents are also at least CHAIN. - BLOCK_VALID_CHAIN = 4, + BLOCK_VALID_CHAIN = 4, //! Scripts & signatures ok. Implies all parents are also at least SCRIPTS. - BLOCK_VALID_SCRIPTS = 5, + BLOCK_VALID_SCRIPTS = 5, //! All validity bits. - BLOCK_VALID_MASK = BLOCK_VALID_HEADER | BLOCK_VALID_TREE | BLOCK_VALID_TRANSACTIONS | - BLOCK_VALID_CHAIN | BLOCK_VALID_SCRIPTS, - - BLOCK_HAVE_DATA = 8, //!< full block available in blk*.dat - BLOCK_HAVE_UNDO = 16, //!< undo data available in rev*.dat - BLOCK_HAVE_MASK = BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO, - - BLOCK_FAILED_VALID = 32, //!< stage after last reached validness failed - BLOCK_FAILED_CHILD = 64, //!< descends from failed block - BLOCK_FAILED_MASK = BLOCK_FAILED_VALID | BLOCK_FAILED_CHILD, + BLOCK_VALID_MASK = BLOCK_VALID_HEADER | BLOCK_VALID_TREE | + BLOCK_VALID_TRANSACTIONS | BLOCK_VALID_CHAIN | + BLOCK_VALID_SCRIPTS, + + //!< full block available in blk*.dat + BLOCK_HAVE_DATA = 8, + //!< undo data available in rev*.dat + BLOCK_HAVE_UNDO = 16, + BLOCK_HAVE_MASK = BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO, + + //!< stage after last reached validness failed + BLOCK_FAILED_VALID = 32, + //!< descends from failed block + BLOCK_FAILED_CHILD = 64, + 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. +/** + * 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 -{ +class CBlockIndex { public: - //! pointer to the hash of the block, if any. Memory is owned by this CBlockIndex - const uint256* phashBlock; + //! 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; + CBlockIndex *pprev; //! 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 int nHeight; @@ -175,16 +183,20 @@ //! 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 + //! (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 + //! 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 + //! (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 @@ -197,14 +209,14 @@ unsigned int nBits; unsigned int nNonce; - //! (memory only) Sequential id assigned to distinguish order in which blocks are received. + //! (memory only) Sequential id assigned to distinguish order in which + //! blocks are received. int32_t nSequenceId; //! (memory only) Maximum nTime in the chain upto and including this block. unsigned int nTimeMax; - void SetNull() - { + void SetNull() { phashBlock = NULL; pprev = NULL; pskip = NULL; @@ -219,34 +231,30 @@ nSequenceId = 0; nTimeMax = 0; - nVersion = 0; + nVersion = 0; hashMerkleRoot = uint256(); - nTime = 0; - nBits = 0; - nNonce = 0; + nTime = 0; + nBits = 0; + nNonce = 0; } - CBlockIndex() - { - SetNull(); - } + CBlockIndex() { SetNull(); } - CBlockIndex(const CBlockHeader& block) - { + CBlockIndex(const CBlockHeader &block) { SetNull(); - nVersion = block.nVersion; + nVersion = block.nVersion; hashMerkleRoot = block.hashMerkleRoot; - nTime = block.nTime; - nBits = block.nBits; - nNonce = block.nNonce; + 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; + ret.nPos = nDataPos; } return ret; } @@ -255,79 +263,65 @@ CDiskBlockPos ret; if (nStatus & BLOCK_HAVE_UNDO) { ret.nFile = nFile; - ret.nPos = nUndoPos; + ret.nPos = nUndoPos; } return ret; } - CBlockHeader GetBlockHeader() const - { + CBlockHeader GetBlockHeader() const { CBlockHeader block; - block.nVersion = nVersion; - if (pprev) - block.hashPrevBlock = pprev->GetBlockHash(); + block.nVersion = nVersion; + if (pprev) block.hashPrevBlock = pprev->GetBlockHash(); block.hashMerkleRoot = hashMerkleRoot; - block.nTime = nTime; - block.nBits = nBits; - block.nNonce = nNonce; + block.nTime = nTime; + block.nBits = nBits; + block.nNonce = nNonce; return block; } - uint256 GetBlockHash() const - { - return *phashBlock; - } + uint256 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; } - enum { nMedianTimeSpan=11 }; + enum { nMedianTimeSpan = 11 }; - int64_t GetMedianTimePast() const - { + int64_t GetMedianTimePast() const { int64_t pmedian[nMedianTimeSpan]; - int64_t* pbegin = &pmedian[nMedianTimeSpan]; - int64_t* pend = &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) + 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]; + 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()); + 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 BlockStatus nUpTo = BLOCK_VALID_TRANSACTIONS) const - { - assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed. - if (nStatus & BLOCK_FAILED_MASK) - return false; + //! Check whether this block index entry is valid up to the passed validity + //! level. + bool IsValid(enum BlockStatus nUpTo = BLOCK_VALID_TRANSACTIONS) const { + // Only validity flags allowed. + assert(!(nUpTo & ~BLOCK_VALID_MASK)); + if (nStatus & BLOCK_FAILED_MASK) return false; return ((nStatus & BLOCK_VALID_MASK) >= nUpTo); } //! Raise the validity level of this block index entry. //! Returns true if the validity was changed. - bool RaiseValidity(enum BlockStatus nUpTo) - { - assert(!(nUpTo & ~BLOCK_VALID_MASK)); // Only validity flags allowed. - if (nStatus & BLOCK_FAILED_MASK) - return false; + bool RaiseValidity(enum BlockStatus nUpTo) { + // Only validity flags allowed. + assert(!(nUpTo & ~BLOCK_VALID_MASK)); + if (nStatus & BLOCK_FAILED_MASK) return false; if ((nStatus & BLOCK_VALID_MASK) < nUpTo) { nStatus = (nStatus & ~BLOCK_VALID_MASK) | nUpTo; return true; @@ -339,45 +333,44 @@ void BuildSkip(); //! Efficiently find an ancestor of this block. - CBlockIndex* GetAncestor(int height); - const CBlockIndex* GetAncestor(int height) const; + CBlockIndex *GetAncestor(int height); + const CBlockIndex *GetAncestor(int height) const; }; -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&); +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 &); /** Used to marshal pointers into hashes for db storage. */ -class CDiskBlockIndex : public CBlockIndex -{ +class CDiskBlockIndex : public CBlockIndex { public: 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()); } ADD_SERIALIZE_METHODS; template - inline void SerializationOp(Stream& s, Operation ser_action) { + inline void SerializationOp(Stream &s, Operation ser_action) { int nVersion = s.GetVersion(); - if (!(s.GetType() & SER_GETHASH)) - READWRITE(VARINT(nVersion)); + if (!(s.GetType() & 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)); + if (nStatus & BLOCK_HAVE_DATA) READWRITE(VARINT(nDataPos)); + if (nStatus & BLOCK_HAVE_UNDO) READWRITE(VARINT(nUndoPos)); // block header READWRITE(this->nVersion); @@ -388,26 +381,22 @@ READWRITE(nNonce); } - uint256 GetBlockHash() const - { + uint256 GetBlockHash() const { CBlockHeader block; - block.nVersion = nVersion; - block.hashPrevBlock = hashPrev; - block.hashMerkleRoot = hashMerkleRoot; - block.nTime = nTime; - block.nBits = nBits; - block.nNonce = nNonce; + block.nVersion = nVersion; + block.hashPrevBlock = hashPrev; + block.hashMerkleRoot = hashMerkleRoot; + block.nTime = nTime; + block.nBits = nBits; + block.nNonce = nNonce; return block.GetHash(); } - - std::string ToString() const - { + std::string ToString() const { std::string str = "CDiskBlockIndex("; str += CBlockIndex::ToString(); str += strprintf("\n hashBlock=%s, hashPrev=%s)", - GetBlockHash().ToString(), - hashPrev.ToString()); + GetBlockHash().ToString(), hashPrev.ToString()); return str; } }; @@ -415,10 +404,11 @@ /** An in-memory indexed chain of blocks. */ class CChain { private: - std::vector vChain; + std::vector vChain; public: - /** Returns the index entry for the genesis block of this chain, or NULL if none. */ + /** 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; } @@ -428,10 +418,10 @@ 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. */ + /** 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; + if (nHeight < 0 || nHeight >= (int)vChain.size()) return NULL; return vChain[nHeight]; } @@ -446,7 +436,8 @@ 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. */ + /** 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]; @@ -454,22 +445,24 @@ 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; - } + /** Return the maximal height in the chain. Is equal to chain.Tip() ? + * chain.Tip()->nHeight : -1. */ + int Height() const { return vChain.size() - 1; } /** Set/initialize a chain with a given tip. */ void SetTip(CBlockIndex *pindex); - /** Return a CBlockLocator that refers to a block in this chain (by default the tip). */ + /** 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. */ + /** Find the last common block between this chain and a block index entry. + */ const CBlockIndex *FindFork(const CBlockIndex *pindex) const; - /** Find the earliest block with timestamp equal or greater than the given. */ - CBlockIndex* FindEarliestAtLeast(int64_t nTime) const; + /** Find the earliest block with timestamp equal or greater than the given. + */ + CBlockIndex *FindEarliestAtLeast(int64_t nTime) const; }; #endif // BITCOIN_CHAIN_H diff --git a/src/chain.cpp b/src/chain.cpp --- a/src/chain.cpp +++ b/src/chain.cpp @@ -25,13 +25,11 @@ std::vector vHave; vHave.reserve(32); - if (!pindex) - pindex = Tip(); + if (!pindex) pindex = Tip(); while (pindex) { vHave.push_back(pindex->GetBlockHash()); // Stop when we have added the genesis block. - if (pindex->nHeight == 0) - break; + if (pindex->nHeight == 0) break; // Exponentially larger steps back, plus the genesis block. int nHeight = std::max(pindex->nHeight - nStep, 0); if (Contains(pindex)) { @@ -41,8 +39,7 @@ // Otherwise, use O(log n) skiplist. pindex = pindex->GetAncestor(nHeight); } - if (vHave.size() > 10) - nStep *= 2; + if (vHave.size() > 10) nStep *= 2; } return CBlockLocator(vHave); @@ -52,48 +49,50 @@ if (pindex == NULL) { return NULL; } - if (pindex->nHeight > Height()) - pindex = pindex->GetAncestor(Height()); + if (pindex->nHeight > Height()) pindex = pindex->GetAncestor(Height()); while (pindex && !Contains(pindex)) pindex = pindex->pprev; return pindex; } -CBlockIndex* CChain::FindEarliestAtLeast(int64_t nTime) const -{ - std::vector::const_iterator lower = std::lower_bound(vChain.begin(), vChain.end(), nTime, - [](CBlockIndex* pBlock, const int64_t& time) -> bool { return pBlock->GetBlockTimeMax() < time; }); +CBlockIndex *CChain::FindEarliestAtLeast(int64_t nTime) const { + std::vector::const_iterator lower = + std::lower_bound(vChain.begin(), vChain.end(), nTime, + [](CBlockIndex *pBlock, const int64_t &time) -> bool { + return pBlock->GetBlockTimeMax() < time; + }); return (lower == vChain.end() ? NULL : *lower); } -/** Turn the lowest '1' bit in the binary representation of a number into a '0'. */ -int static inline InvertLowestOne(int n) { return n & (n - 1); } +/** Turn the lowest '1' bit in the binary representation of a number into a '0'. + */ +int static inline InvertLowestOne(int n) { + return n & (n - 1); +} /** Compute what height to jump back to with the CBlockIndex::pskip pointer. */ int static inline GetSkipHeight(int height) { - if (height < 2) - return 0; + if (height < 2) return 0; - // Determine which height to jump back to. Any number strictly lower than height is acceptable, - // but the following expression seems to perform well in simulations (max 110 steps to go back - // up to 2**18 blocks). - return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height); + // Determine which height to jump back to. Any number strictly lower than + // height is acceptable, but the following expression seems to perform well + // in simulations (max 110 steps to go back up to 2**18 blocks). + return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 + : InvertLowestOne(height); } -CBlockIndex* CBlockIndex::GetAncestor(int height) -{ - if (height > nHeight || height < 0) - return NULL; +CBlockIndex *CBlockIndex::GetAncestor(int height) { + if (height > nHeight || height < 0) return NULL; - CBlockIndex* pindexWalk = this; + CBlockIndex *pindexWalk = this; int heightWalk = nHeight; while (heightWalk > height) { int heightSkip = GetSkipHeight(heightWalk); int heightSkipPrev = GetSkipHeight(heightWalk - 1); if (pindexWalk->pskip != NULL && - (heightSkip == height || - (heightSkip > height && !(heightSkipPrev < heightSkip - 2 && - heightSkipPrev >= height)))) { + (heightSkip == height || (heightSkip > height && + !(heightSkipPrev < heightSkip - 2 && + heightSkipPrev >= height)))) { // Only follow pskip if pprev->pskip isn't better than pskip->pprev. pindexWalk = pindexWalk->pskip; heightWalk = heightSkip; @@ -106,34 +105,31 @@ return pindexWalk; } -const CBlockIndex* CBlockIndex::GetAncestor(int height) const -{ - return const_cast(this)->GetAncestor(height); +const CBlockIndex *CBlockIndex::GetAncestor(int height) const { + return const_cast(this)->GetAncestor(height); } -void CBlockIndex::BuildSkip() -{ - if (pprev) - pskip = pprev->GetAncestor(GetSkipHeight(nHeight)); +void CBlockIndex::BuildSkip() { + if (pprev) pskip = pprev->GetAncestor(GetSkipHeight(nHeight)); } -arith_uint256 GetBlockProof(const CBlockIndex& block) -{ +arith_uint256 GetBlockProof(const CBlockIndex &block) { arith_uint256 bnTarget; bool fNegative; bool fOverflow; bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow); - if (fNegative || fOverflow || bnTarget == 0) - return 0; + if (fNegative || fOverflow || bnTarget == 0) return 0; // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256 - // as it's too large for a arith_uint256. However, as 2**256 is at least as large - // as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1, - // or ~bnTarget / (nTarget+1) + 1. + // as it's too large for a arith_uint256. However, as 2**256 is at least as + // large as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / + // (bnTarget+1)) + 1, or ~bnTarget / (nTarget+1) + 1. return (~bnTarget / (bnTarget + 1)) + 1; } -int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params) -{ +int64_t GetBlockProofEquivalentTime(const CBlockIndex &to, + const CBlockIndex &from, + const CBlockIndex &tip, + const Consensus::Params ¶ms) { arith_uint256 r; int sign = 1; if (to.nChainWork > from.nChainWork) {