MCPcopy Create free account
hub / github.com/WaykiChain/WaykiChain / DisconnectTip

Function DisconnectTip

src/main.cpp:1344–1389  ·  view source on GitHub ↗

Disconnect chainActive's tip.

Source from the content-addressed store, hash-verified

1342
1343// Disconnect chainActive's tip.
1344bool DisconnectTip(CValidationState &state) {
1345 auto bmTx = MAKE_BENCHMARK("DisconnectTip");
1346 CBlockIndex *pBlockIndexToDelete = chainActive.Tip();
1347 assert(pBlockIndexToDelete);
1348 // Read block from disk.
1349 CBlock block;
1350 if (!ReadBlockFromDisk(pBlockIndexToDelete, block))
1351 return state.Abort(_("Failed to read blocks from disk."));
1352 // Apply the block atomically to the chain state.
1353 auto spCW = std::make_shared<CCacheWrapper>(pCdMan);
1354
1355 if (!DisconnectBlock(block, *spCW, pBlockIndexToDelete, state))
1356 return ERRORMSG("DisconnectBlock %s failed", pBlockIndexToDelete->GetBlockHash().ToString());
1357
1358 // Need to re-sync all to global cache layer.
1359 spCW->Flush();
1360
1361 // Attention: need to reset the lastest block price median
1362 CBlockIndex *pPreBlockIndex = pBlockIndexToDelete->pprev;
1363 CBlock preBlock;
1364 if (pPreBlockIndex) {
1365 if (!ReadBlockFromDisk(pPreBlockIndex, preBlock))
1366 return ERRORMSG("failed to read block [%d]: %s", pPreBlockIndex->height,
1367 pPreBlockIndex->GetBlockHash().ToString());
1368 }
1369
1370 // Write the chain state to disk, if necessary.
1371 if (!WriteChainState(state))
1372 return false;
1373 // Update chainActive and related variables.
1374 UpdateTip(pBlockIndexToDelete->pprev, block);
1375 // Resurrect mempool transactions from the disconnected block.
1376 for (const auto &pTx : block.vptx) {
1377 list<std::shared_ptr<CBaseTx> > removed;
1378 CValidationState stateDummy;
1379 if (!pTx->IsRelayForbidden()) {
1380 if (!AcceptToMemoryPool(mempool, stateDummy, pTx.get(), false)) {
1381 mempool.Remove(pTx.get(), removed, true);
1382 }
1383 } else {
1384 EraseTransactionFromWallet(pTx->GetHash());
1385 }
1386 }
1387
1388 return true;
1389}
1390
1391// Connect a new block to chainActive.
1392bool static ConnectTip(CValidationState &state, CBlockIndex *pIndexNew) {

Callers 3

InvalidateBlockFunction · 0.85
ActivateBestChainFunction · 0.85
disconnectblockFunction · 0.85

Calls 15

ReadBlockFromDiskFunction · 0.85
_Function · 0.85
WriteChainStateFunction · 0.85
UpdateTipFunction · 0.85
AcceptToMemoryPoolFunction · 0.85
TipMethod · 0.80
AbortMethod · 0.80
IsRelayForbiddenMethod · 0.80
DisconnectBlockFunction · 0.70
ToStringMethod · 0.45
GetBlockHashMethod · 0.45

Tested by

no test coverage detected