| 1588 | } |
| 1589 | |
| 1590 | bool AddToBlockIndex(CBlock &block, CValidationState &state, const CDiskBlockPos &pos) { |
| 1591 | // Check for duplicate |
| 1592 | uint256 hash = block.GetHash(); |
| 1593 | if (mapBlockIndex.count(hash)) |
| 1594 | return state.Invalid(ERRORMSG("AddToBlockIndex() : %s already exists", block.GetIdStr()), 0, "duplicate"); |
| 1595 | |
| 1596 | // Construct new block index object |
| 1597 | CBlockIndex *pIndexNew = new CBlockIndex(block); |
| 1598 | |
| 1599 | assert(pIndexNew); |
| 1600 | { |
| 1601 | LOCK(cs_nBlockSequenceId); |
| 1602 | pIndexNew->nSequenceId = nBlockSequenceId++; |
| 1603 | } |
| 1604 | map<uint256, CBlockIndex *>::iterator mi = mapBlockIndex.insert(make_pair(hash, pIndexNew)).first; |
| 1605 | // LogPrint(BCLog::INFO, "in map hash:%s map size:%d\n", hash.GetHex(), mapBlockIndex.size()); |
| 1606 | pIndexNew->pBlockHash = &((*mi).first); |
| 1607 | map<uint256, CBlockIndex *>::iterator miPrev = mapBlockIndex.find(block.GetPrevBlockHash()); |
| 1608 | if (miPrev != mapBlockIndex.end()) { |
| 1609 | pIndexNew->pprev = (*miPrev).second; |
| 1610 | pIndexNew->height = pIndexNew->pprev->height + 1; |
| 1611 | pIndexNew->BuildSkip(); |
| 1612 | } |
| 1613 | |
| 1614 | pIndexNew->nFile = pos.nFile; |
| 1615 | pIndexNew->nDataPos = pos.nPos; |
| 1616 | pIndexNew->nUndoPos = 0; |
| 1617 | pIndexNew->nStatus = BLOCK_VALID_TRANSACTIONS | BLOCK_HAVE_DATA; |
| 1618 | setBlockIndexValid.insert(pIndexNew); |
| 1619 | |
| 1620 | const auto &bpRegid = GetBlockBpRegid(block); |
| 1621 | CDiskBlockIndex diskBlockIndex(pIndexNew, block, bpRegid); |
| 1622 | // pIndexNew->nChainTx = (pIndexNew->pprev ? pIndexNew->pprev->nChainTx : 0) + pIndexNew->nTx; |
| 1623 | if (!pCdMan->pBlockIndexDb->WriteBlockIndex(diskBlockIndex)) |
| 1624 | return state.Abort(_("Failed to write block index")); |
| 1625 | int64_t beginTime = GetTimeMillis(); |
| 1626 | // New best? |
| 1627 | if (!ActivateBestChain(state, pIndexNew)) { |
| 1628 | LogPrint(BCLog::INFO, "ActivateBestChain() elapse time:%lld ms\n", GetTimeMillis() - beginTime); |
| 1629 | return false; |
| 1630 | } |
| 1631 | // LogPrint(BCLog::INFO, "ActivateBestChain() elapse time:%lld ms\n", GetTimeMillis() - beginTime); |
| 1632 | LOCK(cs_main); |
| 1633 | if (pIndexNew == chainActive.Tip()) { |
| 1634 | // Clear fork warning if its no longer applicable |
| 1635 | CheckForkWarningConditions(); |
| 1636 | } else |
| 1637 | CheckForkWarningConditionsOnNewFork(pIndexNew); |
| 1638 | |
| 1639 | if (!pCdMan->pBlockCache->Flush()) |
| 1640 | return state.Abort(_("Failed to sync block index")); |
| 1641 | |
| 1642 | if (chainActive.Height() > nSyncTipHeight) |
| 1643 | nSyncTipHeight = chainActive.Height(); |
| 1644 | |
| 1645 | return true; |
| 1646 | } |
| 1647 |
no test coverage detected