Try to activate to the most-work chain (thereby connecting it).
| 1504 | } |
| 1505 | // Try to activate to the most-work chain (thereby connecting it). |
| 1506 | bool ActivateBestChain(CValidationState &state, CBlockIndex* pNewIndex) { |
| 1507 | LOCK(cs_main); |
| 1508 | CBlockIndex *pIndexOldTip = chainActive.Tip(); |
| 1509 | bool fComplete = false; |
| 1510 | |
| 1511 | while (!fComplete) { |
| 1512 | FindMostWorkChain(); |
| 1513 | fComplete = true; |
| 1514 | |
| 1515 | // Check whether we have something to do. |
| 1516 | if (chainMostWork.Tip() == nullptr) |
| 1517 | break; |
| 1518 | |
| 1519 | auto height = chainActive.Height(); |
| 1520 | while(height >= 0 ){ |
| 1521 | auto chainIndex = chainActive[height]; |
| 1522 | if( chainIndex &&!chainMostWork.Contains(chainIndex)){ |
| 1523 | CBlockIndex* finIndex = pbftMan.GetLocalFinIndex(); |
| 1524 | if(finIndex && chainIndex->GetBlockHash() == finIndex->GetBlockHash()){ |
| 1525 | LogPrint(BCLog::INFO, "finality block can't be reverse\n"); |
| 1526 | if (GetTime() - pbftMan.GetLocalFinLastUpdate() > 60) { |
| 1527 | pbftMan.SetLocalFinTimeout(); |
| 1528 | } else{ |
| 1529 | LogPrint(BCLog::INFO, "connect block on fin chain\n"); |
| 1530 | return ConnectBlockOnFinChain(pNewIndex, state); |
| 1531 | } |
| 1532 | } |
| 1533 | |
| 1534 | uint256 globalFinIndexHash = pbftMan.GetGlobalFinBlockHash(); |
| 1535 | if( chainIndex->GetBlockHash() == globalFinIndexHash){ |
| 1536 | LogPrint(BCLog::INFO, "globalfinality block can't be reverse\n"); |
| 1537 | return ConnectBlockOnFinChain(pNewIndex, state); |
| 1538 | } |
| 1539 | |
| 1540 | height--; |
| 1541 | }else if (chainIndex&& chainMostWork.Contains(chainIndex)){ |
| 1542 | break; |
| 1543 | }else if(chainIndex == nullptr) |
| 1544 | return true; |
| 1545 | } |
| 1546 | |
| 1547 | // Disconnect active blocks which are no longer in the best chain. |
| 1548 | while (chainActive.Tip() && !chainMostWork.Contains(chainActive.Tip())) { |
| 1549 | if (!DisconnectTip(state)) |
| 1550 | return false; |
| 1551 | |
| 1552 | if (chainActive.Tip() && chainMostWork.Contains(chainActive.Tip())) |
| 1553 | mempool.ReScanMemPoolTx(); |
| 1554 | } |
| 1555 | |
| 1556 | // Connect new blocks. |
| 1557 | while (!chainActive.Contains(chainMostWork.Tip())) { |
| 1558 | CBlockIndex *pIndexConnect = chainMostWork[chainActive.Height() + 1]; |
| 1559 | if (!ConnectTip(state, pIndexConnect)) { |
| 1560 | if (state.IsInvalid()) { |
| 1561 | // The block violates a consensus rule. |
| 1562 | if (!state.CorruptionPossible()) |
| 1563 | InvalidChainFound(chainMostWork.Tip()); |
no test coverage detected