Bitcoin Stack Exchange is a question and answer site for Bitcoin crypto-currency enthusiasts. It only takes a minute to sign up.
Sign up to join this communityAnybody can ask a question
Anybody can answer
The best answers are voted up and rise to the top
Asked
Viewed 49 times
This function here gives a pointer to a block, and return the succesor block of that pointer.
/** Find the successor of a block in this chain, or nullptr 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 nullptr; }
If the given block is containing in the chain, it attempt to return the successor. So if the Chain.Tip()
is given, it tries to reach the successor while there is no successor of the tip block. Isn't it better to check if nHeight
of the given block is less than the Chain.Tip().nHeight - 1
or not?
That would not guarantee that the currently inspected block has a successor. While eventually the network converges on a single best chain, the network does temporarily fork occasionally, when two miners find a block at the same height. If a node were on the chaintip that is being reorged out there could be a block with a greater height, while it is not the successor of the node's current best block. Insofar, we must handle blocks not having successors anyway, so I assume that the nullptr
is handled benignly somewhere in the surrounding code (I haven't checked, though).
I forgot to check the []
operator of the CChain
class. The implementation is:
/** Returns the index entry at a particular height in this chain, or nullptr if no such height exists. */ CBlockIndex* operator[](int nHeight) const { if (nHeight < 0 || nHeight >= (int)vChain.size()) return nullptr; return vChain[nHeight]; }
So by calling the (*this)[pindex->nHeight + 1]
the operator that is overloaded is called and if the nHeight
is greater that the vChain.size()
it returns the nullptr
.
You can get bonuses upto $100 FREE BONUS when you:
π° Install these recommended apps:
π² SocialGood - 100% Crypto Back on Everyday Shopping
π² xPortal - The DeFi For The Next Billion
π² CryptoTab Browser - Lightweight, fast, and ready to mine!
π° Register on these recommended exchanges:
π‘ Binanceπ‘ Bitfinexπ‘ Bitmartπ‘ Bittrexπ‘ Bitget
π‘ CoinExπ‘ Crypto.comπ‘ Gate.ioπ‘ Huobiπ‘ Kucoin.
Comments