| 3117 | } |
| 3118 | |
| 3119 | CBlockIndex* AddToBlockIndex(const CBlockHeader& block) |
| 3120 | { |
| 3121 | // Check for duplicate |
| 3122 | uint256 hash = block.GetHash(); |
| 3123 | BlockMap::iterator it = mapBlockIndex.find(hash); |
| 3124 | if (it != mapBlockIndex.end()) |
| 3125 | return it->second; |
| 3126 | |
| 3127 | // Construct new block index object |
| 3128 | CBlockIndex* pindexNew = new CBlockIndex(block); |
| 3129 | assert(pindexNew); |
| 3130 | // We assign the sequence id to blocks only when the full data is available, |
| 3131 | // to avoid miners withholding blocks but broadcasting headers, to get a |
| 3132 | // competitive advantage. |
| 3133 | pindexNew->nSequenceId = 0; |
| 3134 | BlockMap::iterator mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first; |
| 3135 | pindexNew->phashBlock = &((*mi).first); |
| 3136 | BlockMap::iterator miPrev = mapBlockIndex.find(block.hashPrevBlock); |
| 3137 | if (miPrev != mapBlockIndex.end()) |
| 3138 | { |
| 3139 | pindexNew->pprev = (*miPrev).second; |
| 3140 | pindexNew->nHeight = pindexNew->pprev->nHeight + 1; |
| 3141 | pindexNew->BuildSkip(); |
| 3142 | } |
| 3143 | pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + GetBlockProof(*pindexNew); |
| 3144 | pindexNew->RaiseValidity(BLOCK_VALID_TREE); |
| 3145 | if (pindexBestHeader == NULL || pindexBestHeader->nChainWork < pindexNew->nChainWork) |
| 3146 | pindexBestHeader = pindexNew; |
| 3147 | |
| 3148 | setDirtyBlockIndex.insert(pindexNew); |
| 3149 | |
| 3150 | return pindexNew; |
| 3151 | } |
| 3152 | |
| 3153 | /** Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS). */ |
| 3154 | bool ReceivedBlockTransactions(const CBlock &block, CValidationState& state, CBlockIndex *pindexNew, const CDiskBlockPos& pos) |
no test coverage detected