| 3102 | } |
| 3103 | |
| 3104 | CBlockIndex* CChainState::AddToBlockIndex(const CBlockHeader& block) |
| 3105 | { |
| 3106 | AssertLockHeld(cs_main); |
| 3107 | |
| 3108 | // Check for duplicate |
| 3109 | uint256 hash = block.GetHash(); |
| 3110 | BlockMap::iterator it = mapBlockIndex.find(hash); |
| 3111 | if (it != mapBlockIndex.end()) |
| 3112 | return it->second; |
| 3113 | |
| 3114 | // Construct new block index object |
| 3115 | CBlockIndex* pindexNew = new CBlockIndex(block); |
| 3116 | // We assign the sequence id to blocks only when the full data is available, |
| 3117 | // to avoid miners withholding blocks but broadcasting headers, to get a |
| 3118 | // competitive advantage. |
| 3119 | pindexNew->nSequenceId = 0; |
| 3120 | BlockMap::iterator mi = mapBlockIndex.insert(std::make_pair(hash, pindexNew)).first; |
| 3121 | pindexNew->phashBlock = &((*mi).first); |
| 3122 | BlockMap::iterator miPrev = mapBlockIndex.find(block.hashPrevBlock); |
| 3123 | if (miPrev != mapBlockIndex.end()) |
| 3124 | { |
| 3125 | pindexNew->pprev = (*miPrev).second; |
| 3126 | pindexNew->nHeight = pindexNew->pprev->nHeight + 1; |
| 3127 | pindexNew->BuildSkip(); |
| 3128 | } |
| 3129 | pindexNew->nTimeReceived = GetTime(); |
| 3130 | pindexNew->nTimeMax = (pindexNew->pprev ? std::max(pindexNew->pprev->nTimeMax, pindexNew->nTime) : pindexNew->nTime); |
| 3131 | pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + GetBlockProof(*pindexNew); |
| 3132 | pindexNew->RaiseValidity(BLOCK_VALID_TREE); |
| 3133 | if (pindexBestHeader == nullptr || pindexBestHeader->nChainWork < pindexNew->nChainWork) |
| 3134 | pindexBestHeader = pindexNew; |
| 3135 | |
| 3136 | setDirtyBlockIndex.insert(pindexNew); |
| 3137 | |
| 3138 | return pindexNew; |
| 3139 | } |
| 3140 | |
| 3141 | /** Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS). */ |
| 3142 | void CChainState::ReceivedBlockTransactions(const CBlock& block, CBlockIndex* pindexNew, const CDiskBlockPos& pos, const Consensus::Params& consensusParams) |
nothing calls this directly
no test coverage detected