Fix using temp file for receiving blocks.

This commit is contained in:
blackjok3r
2019-01-29 07:06:00 +08:00
parent 4f212c841d
commit c14e0909c6
2 changed files with 50 additions and 11 deletions

View File

@@ -3291,7 +3291,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
if (!ReceivedBlockTransactions(block, state, pindex, blockPos)) if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
return error("AcceptBlock(): ReceivedBlockTransactions failed"); return error("AcceptBlock(): ReceivedBlockTransactions failed");
setDirtyFileInfo.insert(blockPos.nFile); setDirtyFileInfo.insert(blockPos.nFile);
fprintf(stderr,"added ht.%d copy of tmpfile to %d.%d\n",pindex->GetHeight(),blockPos.nFile,blockPos.nPos); //fprintf(stderr,"added ht.%d copy of tmpfile to %d.%d\n",pindex->GetHeight(),blockPos.nFile,blockPos.nPos);
} }
// verify that the view's current state corresponds to the previous block // verify that the view's current state corresponds to the previous block
uint256 hashPrevBlock = pindex->pprev == NULL ? uint256() : pindex->pprev->GetBlockHash(); uint256 hashPrevBlock = pindex->pprev == NULL ? uint256() : pindex->pprev->GetBlockHash();
@@ -3754,7 +3754,8 @@ bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
std::vector<std::pair<int, const CBlockFileInfo*> > vFiles; std::vector<std::pair<int, const CBlockFileInfo*> > vFiles;
vFiles.reserve(setDirtyFileInfo.size()); vFiles.reserve(setDirtyFileInfo.size());
for (set<int>::iterator it = setDirtyFileInfo.begin(); it != setDirtyFileInfo.end(); ) { for (set<int>::iterator it = setDirtyFileInfo.begin(); it != setDirtyFileInfo.end(); ) {
vFiles.push_back(make_pair(*it, &vinfoBlockFile[*it])); if ( *it < TMPFILE_START )
vFiles.push_back(make_pair(*it, &vinfoBlockFile[*it]));
setDirtyFileInfo.erase(it++); setDirtyFileInfo.erase(it++);
} }
std::vector<const CBlockIndex*> vBlocks; std::vector<const CBlockIndex*> vBlocks;
@@ -4601,11 +4602,15 @@ bool FindBlockPos(int32_t tmpflag,CValidationState &state, CDiskBlockPos &pos, u
LOCK(cs_LastBlockFile); LOCK(cs_LastBlockFile);
unsigned int nFile; unsigned int nFile;
if ( tmpflag != 0 ) if ( tmpflag != 0 )
{ {
ptr = &tmpBlockFiles; ptr = &tmpBlockFiles;
nFile = nLastTmpFile; nFile = nLastTmpFile;
lastfilep = &nLastTmpFile; lastfilep = &nLastTmpFile;
if (tmpBlockFiles.size() <= nFile) {
tmpBlockFiles.resize(nFile + 1);
}
} }
else else
{ {
@@ -4617,7 +4622,7 @@ bool FindBlockPos(int32_t tmpflag,CValidationState &state, CDiskBlockPos &pos, u
} }
} }
if (!fKnown) { if (!fKnown) {
while ((*ptr)[nFile].nSize + nAddSize >= MAX_BLOCKFILE_SIZE) { while ( (*ptr)[nFile].nSize + nAddSize >= ((tmpflag != 0) ? MAX_TEMPFILE_SIZE : MAX_BLOCKFILE_SIZE) ) {
nFile++; nFile++;
if ((*ptr).size() <= nFile) { if ((*ptr).size() <= nFile) {
(*ptr).resize(nFile + 1); (*ptr).resize(nFile + 1);
@@ -4628,13 +4633,39 @@ bool FindBlockPos(int32_t tmpflag,CValidationState &state, CDiskBlockPos &pos, u
if ( 0 && tmpflag != 0 ) if ( 0 && tmpflag != 0 )
fprintf(stderr,"pos.nFile %d nPos %u\n",pos.nFile,pos.nPos); fprintf(stderr,"pos.nFile %d nPos %u\n",pos.nFile,pos.nPos);
} }
if (nFile != *lastfilep) { if (nFile != *lastfilep) {
if (!fKnown) { if (!fKnown) {
LogPrintf("Leaving block file %i: %s\n", nFile, (*ptr)[nFile].ToString()); LogPrintf("Leaving block file %i: %s\n", nFile, (*ptr)[nFile].ToString());
} }
FlushBlockFile(!fKnown); FlushBlockFile(!fKnown);
fprintf(stderr, "nFile = %i size.%li\n",nFile,tmpBlockFiles.size());
if ( tmpflag != 0 && tmpBlockFiles.size() >= 4 )
{
if ( nFile == 1 )
{
PruneOneBlockFile(true,TMPFILE_START+2);
tmpBlockFiles[2].SetNull();
LogPrintf("Reset tempfile 2\n"); sleep(15);
}
else if ( nFile == 2 )
{
PruneOneBlockFile(true,TMPFILE_START+3);
tmpBlockFiles[3].SetNull();
LogPrintf("Reset tempfile 3\n"); sleep(15);
}
}
if ( tmpflag != 0 && nFile == 3 )
{
PruneOneBlockFile(true,TMPFILE_START);
tmpBlockFiles[0].SetNull();
PruneOneBlockFile(true,TMPFILE_START+1);
tmpBlockFiles[1].SetNull();
nFile = 0;
LogPrintf("Reset tempfile 0\n"); sleep(15);
}
*lastfilep = nFile; *lastfilep = nFile;
fprintf(stderr, "*lastfilep = %i\n",*lastfilep);sleep(15);
} }
(*ptr)[nFile].AddBlock(nHeight, nTime); (*ptr)[nFile].AddBlock(nHeight, nTime);
@@ -5232,7 +5263,8 @@ bool AcceptBlock(int32_t *futureblockp,CBlock& block, CValidationState& state, C
} }
int nHeight = pindex->GetHeight(); int nHeight = pindex->GetHeight();
int32_t usetmp = 0; int32_t usetmp = 1;
// Write block to history file // Write block to history file
try { try {
unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION); unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
@@ -5463,8 +5495,9 @@ uint64_t CalculateCurrentUsage()
} }
/* Prune a block file (modify associated database entries)*/ /* Prune a block file (modify associated database entries)*/
void PruneOneBlockFile(const int fileNumber) void PruneOneBlockFile(bool tempfile, const int fileNumber)
{ {
fprintf(stderr, "pruneblockfile.%i\n",fileNumber); sleep(15);
for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); ++it) { for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); ++it) {
CBlockIndex* pindex = it->second; CBlockIndex* pindex = it->second;
if (pindex && pindex->nFile == fileNumber) { if (pindex && pindex->nFile == fileNumber) {
@@ -5474,7 +5507,11 @@ void PruneOneBlockFile(const int fileNumber)
pindex->nDataPos = 0; pindex->nDataPos = 0;
pindex->nUndoPos = 0; pindex->nUndoPos = 0;
setDirtyBlockIndex.insert(pindex); setDirtyBlockIndex.insert(pindex);
if (pindex->nStatus & BLOCK_IN_TMPFILE != 0 )
{
// We should be able to clear these blocks from the index as they are not in the main chains block files.
fprintf(stderr, "Block still in tempfile.%i\n",fileNumber);
}
// Prune from mapBlocksUnlinked -- any block we prune would have // Prune from mapBlocksUnlinked -- any block we prune would have
// to be downloaded again in order to consider its chain, at which // to be downloaded again in order to consider its chain, at which
// point it would be considered as a candidate for // point it would be considered as a candidate for
@@ -5489,8 +5526,8 @@ void PruneOneBlockFile(const int fileNumber)
} }
} }
} }
if (!tempfile)
vinfoBlockFile[fileNumber].SetNull(); vinfoBlockFile[fileNumber].SetNull();
setDirtyFileInfo.insert(fileNumber); setDirtyFileInfo.insert(fileNumber);
} }
@@ -5538,7 +5575,7 @@ void FindFilesToPrune(std::set<int>& setFilesToPrune)
if (vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune) if (vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
continue; continue;
PruneOneBlockFile(fileNumber); PruneOneBlockFile(false, fileNumber);
// Queue up the files for removal // Queue up the files for removal
setFilesToPrune.insert(fileNumber); setFilesToPrune.insert(fileNumber);
nCurrentUsage -= nBytesToPrune; nCurrentUsage -= nBytesToPrune;

View File

@@ -90,7 +90,8 @@ static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100;
/** Default for -txexpirydelta, in number of blocks */ /** Default for -txexpirydelta, in number of blocks */
static const unsigned int DEFAULT_TX_EXPIRY_DELTA = 20; static const unsigned int DEFAULT_TX_EXPIRY_DELTA = 20;
/** The maximum size of a blk?????.dat file (since 0.8) */ /** The maximum size of a blk?????.dat file (since 0.8) */
static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
static const unsigned int MAX_TEMPFILE_SIZE = 0x1000000; // 16 MiB 0x8000000
/** The pre-allocation chunk size for blk?????.dat files (since 0.8) */ /** The pre-allocation chunk size for blk?????.dat files (since 0.8) */
static const unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB static const unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB
/** The pre-allocation chunk size for rev?????.dat files (since 0.8) */ /** The pre-allocation chunk size for rev?????.dat files (since 0.8) */
@@ -807,6 +808,7 @@ bool WriteBlockToDisk(const CBlock& block, CDiskBlockPos& pos, const CMessageHea
bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos,bool checkPOW); bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos,bool checkPOW);
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex,bool checkPOW); bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex,bool checkPOW);
bool RemoveOrphanedBlocks(int32_t notarized_height); bool RemoveOrphanedBlocks(int32_t notarized_height);
void PruneOneBlockFile(bool tempfile, const int fileNumber);
/** Functions for validating blocks and updating the block tree */ /** Functions for validating blocks and updating the block tree */