Guest User

Untitled

a guest
Dec 18th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. // validation.cpp
  2.  
  3. /** Store block on disk. If dbp is non-nullptr, the file is known to already reside on disk */
  4. static bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool fRequested, const CDiskBlockPos* dbp, bool* fNewBlock)
  5. {
  6. // ...
  7. // Header is valid/has work, merkle tree and segwit merkle tree are good...RELAY NOW
  8. // (but if it does not build on our best tip, let the SendMessages loop relay it)
  9. if (!IsInitialBlockDownload() && chainActive.Tip() == pindex->pprev)
  10. GetMainSignals().NewPoWValidBlock(pindex, pblock);
  11.  
  12. int nHeight = pindex->nHeight;
  13.  
  14. // Write block to history file
  15. try {
  16. unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
  17. CDiskBlockPos blockPos;
  18. if (dbp != nullptr)
  19. blockPos = *dbp;
  20. if (!FindBlockPos(state, blockPos, nBlockSize+8, nHeight, block.GetBlockTime(), dbp != nullptr))
  21. return error("AcceptBlock(): FindBlockPos failed");
  22. if (dbp == nullptr)
  23. if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
  24. AbortNode(state, "Failed to write block");
  25. if (!ReceivedBlockTransactions(block, state, pindex, blockPos, chainparams.GetConsensus()))
  26. return error("AcceptBlock(): ReceivedBlockTransactions failed");
  27. } catch (const std::runtime_error& e) {
  28. return AbortNode(state, std::string("System error: ") + e.what());
  29. }
  30.  
  31. if (fCheckForPruning)
  32. FlushStateToDisk(chainparams, state, FLUSH_STATE_NONE); // we just allocated more disk space for block files
  33.  
  34. return true;
  35. }
  36.  
  37. static bool FindBlockPos(CValidationState &state, CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown = false)
  38. {
  39. // ...
  40. unsigned int nFile = fKnown ? pos.nFile : nLastBlockFile;
  41. if (vinfoBlockFile.size() <= nFile) {
  42. vinfoBlockFile.resize(nFile + 1);
  43. }
  44.  
  45. if (!fKnown) {
  46. while (vinfoBlockFile[nFile].nSize + nAddSize >= MAX_BLOCKFILE_SIZE) {
  47. nFile++;
  48. if (vinfoBlockFile.size() <= nFile) {
  49. vinfoBlockFile.resize(nFile + 1);
  50. }
  51. }
  52. pos.nFile = nFile;
  53. pos.nPos = vinfoBlockFile[nFile].nSize;
  54. }
  55.  
  56. if ((int)nFile != nLastBlockFile) {
  57. if (!fKnown) {
  58. LogPrintf("Leaving block file %i: %s\n", nLastBlockFile, vinfoBlockFile[nLastBlockFile].ToString());
  59. }
  60. FlushBlockFile(!fKnown);
  61. nLastBlockFile = nFile;
  62. }
  63.  
  64. vinfoBlockFile[nFile].AddBlock(nHeight, nTime);
  65. if (fKnown)
  66. vinfoBlockFile[nFile].nSize = std::max(pos.nPos + nAddSize, vinfoBlockFile[nFile].nSize);
  67. else
  68. vinfoBlockFile[nFile].nSize += nAddSize;
  69.  
  70. if (!fKnown) {
  71. unsigned int nOldChunks = (pos.nPos + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
  72. unsigned int nNewChunks = (vinfoBlockFile[nFile].nSize + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
  73. if (nNewChunks > nOldChunks) {
  74. if (fPruneMode)
  75. fCheckForPruning = true;
  76. if (CheckDiskSpace(nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos)) {
  77. FILE *file = OpenBlockFile(pos);
  78. if (file) {
  79. LogPrintf("Pre-allocating up to position 0x%x in blk%05u.dat\n", nNewChunks * BLOCKFILE_CHUNK_SIZE, pos.nFile);
  80. AllocateFileRange(file, pos.nPos, nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos);
  81. fclose(file);
  82. }
  83. }
  84. else
  85. return state.Error("out of disk space");
  86. }
  87. }
  88.  
  89. setDirtyFileInfo.insert(nFile);
  90. return true;
  91. }
  92.  
  93. static bool WriteBlockToDisk(const CBlock& block, CDiskBlockPos& pos, const CMessageHeader::MessageStartChars& messageStart)
  94. {
  95. // Open history file to append
  96. CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
  97. if (fileout.IsNull())
  98. return error("WriteBlockToDisk: OpenBlockFile failed");
  99.  
  100. // Write index header
  101. unsigned int nSize = GetSerializeSize(fileout, block);
  102. fileout << FLATDATA(messageStart) << nSize;
  103.  
  104. // Write block
  105. long fileOutPos = ftell(fileout.Get());
  106. if (fileOutPos < 0)
  107. return error("WriteBlockToDisk: ftell failed");
  108. pos.nPos = (unsigned int)fileOutPos;
  109. fileout << block;
  110.  
  111. return true;
  112. }
Add Comment
Please, Sign In to add comment