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

Function FindBlockPos

src/main.cpp:1648–1700  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1646}
1647
1648bool FindBlockPos(CValidationState &state, CDiskBlockPos &pos, uint32_t nAddSize, uint32_t height, uint64_t nTime,
1649 bool fKnown = false) {
1650 bool fUpdatedLast = false;
1651
1652 LOCK(cs_LastBlockFile);
1653
1654 if (fKnown) {
1655 if (nLastBlockFile != pos.nFile) {
1656 nLastBlockFile = pos.nFile;
1657 infoLastBlockFile.SetNull();
1658 pCdMan->pBlockIndexDb->ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile);
1659 fUpdatedLast = true;
1660 }
1661 } else {
1662 while (infoLastBlockFile.nSize + nAddSize >= MAX_BLOCKFILE_SIZE) {
1663 LogPrint(BCLog::INFO, "Leaving block file %d: %s\n", nLastBlockFile, infoLastBlockFile.ToString());
1664 FlushBlockFile(true);
1665 nLastBlockFile++;
1666 infoLastBlockFile.SetNull();
1667 pCdMan->pBlockIndexDb->ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile); // check whether data for the new file somehow already exist; can fail just fine
1668 fUpdatedLast = true;
1669 }
1670 pos.nFile = nLastBlockFile;
1671 pos.nPos = infoLastBlockFile.nSize;
1672 }
1673
1674 infoLastBlockFile.nSize += nAddSize;
1675 infoLastBlockFile.AddBlock(height, nTime);
1676
1677 if (!fKnown) {
1678 uint32_t nOldChunks = (pos.nPos + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
1679 uint32_t nNewChunks = (infoLastBlockFile.nSize + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
1680 if (nNewChunks > nOldChunks) {
1681 if (CheckDiskSpace(nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos)) {
1682 FILE *file = OpenBlockFile(pos);
1683 if (file) {
1684 LogPrint(BCLog::INFO, "Pre-allocating up to position 0x%x in blk%05u.dat\n", nNewChunks * BLOCKFILE_CHUNK_SIZE, pos.nFile);
1685 AllocateFileRange(file, pos.nPos, nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos);
1686 fclose(file);
1687 }
1688 } else
1689 return state.Error("out of disk space");
1690 }
1691 }
1692
1693 if (!pCdMan->pBlockIndexDb->WriteBlockFileInfo(nLastBlockFile, infoLastBlockFile))
1694 return state.Abort(_("Failed to write file info"));
1695
1696 if (fUpdatedLast)
1697 pCdMan->pBlockCache->WriteLastBlockFile(nLastBlockFile);
1698
1699 return true;
1700}
1701
1702bool ProcessForkedChain(const CBlock &block, CBlockIndex *pPreBlockIndex, CValidationState &state) {
1703 bool forkChainTipFound = false;

Callers 2

AcceptBlockFunction · 0.85
InitBlockIndexFunction · 0.85

Calls 13

FlushBlockFileFunction · 0.85
CheckDiskSpaceFunction · 0.85
OpenBlockFileFunction · 0.85
AllocateFileRangeFunction · 0.85
_Function · 0.85
ReadBlockFileInfoMethod · 0.80
AddBlockMethod · 0.80
ErrorMethod · 0.80
WriteBlockFileInfoMethod · 0.80
AbortMethod · 0.80
WriteLastBlockFileMethod · 0.80
SetNullMethod · 0.45

Tested by

no test coverage detected