Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: lovecodelc on October 10, 2016, 03:14:21 AM



Title: what is the role of nChainWork in CBlockIndex
Post by: lovecodelc on October 10, 2016, 03:14:21 AM
Hello, i have question about nChainWork:
The block index database gets loaded into memory when the node starts, in src/main.cpp LoadBlockIndexDB function,
the code "pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex);", why do this?

nChainWork noted:"Total amount of work (expected number of hashes) in the chain up to and including this block".
here:https://en.bitcoin.it/wiki/Bitcoin_Core_0.11_(ch_6):_The_Blockchain

Thank you.


Title: Re: what is the role of nChainWork in CBlockIndex
Post by: achow101 on October 10, 2016, 03:27:46 AM
Can you stop spamming with all of these threads? Just make a thread for questions about the code breakdown. Also, I highly suggest that you read the Documentation at https://dev.visucore.com/bitcoin/doxygen/ and you logically think through what you are asking before asking the question.

I don't need to even read the code to understand this line, just logically thinking.

This line adds the chainwork of the previous block to the chainwork of the current block. It uses the ternary operator to start at 0 if the previous block doesn't exist, i.e. the genesis block. Otherwise it will get the chainwork of the previous block.