aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2012-09-06 03:21:18 +0200
committerPieter Wuille <pieter.wuille@gmail.com>2012-10-20 23:08:57 +0200
commit44d40f26dcc73469bfbfda5f981c2c528d592ac7 (patch)
tree94e27888554f534d352d9b90ca955464516411c8
parentd979e6e36ac6be0d40b2a6bb70c668f9e6989ff9 (diff)
Flush and sync block data
-rw-r--r--src/main.cpp29
1 files changed, 21 insertions, 8 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 6395a25415..44e690b959 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1498,6 +1498,23 @@ bool CBlock::DisconnectBlock(CBlockIndex *pindex, CCoinsViewCache &view)
return true;
}
+void static FlushBlockFile()
+{
+ LOCK(cs_LastBlockFile);
+
+ CDiskBlockPos posOld;
+ posOld.nFile = nLastBlockFile;
+ posOld.nPos = 0;
+
+ FILE *fileOld = OpenBlockFile(posOld);
+ FileCommit(fileOld);
+ fclose(fileOld);
+
+ fileOld = OpenUndoFile(posOld);
+ FileCommit(fileOld);
+ fclose(fileOld);
+}
+
bool FindUndoPos(int nFile, CDiskBlockPos &pos, unsigned int nAddSize);
bool CBlock::ConnectBlock(CBlockIndex* pindex, CCoinsViewCache &view, bool fJustCheck)
@@ -1700,9 +1717,11 @@ bool SetBestChain(CBlockIndex* pindexNew)
// Make sure it's successfully written to disk before changing memory structure
bool fIsInitialDownload = IsInitialBlockDownload();
- if (!fIsInitialDownload || view.GetCacheSize()>5000)
+ if (!fIsInitialDownload || view.GetCacheSize()>5000) {
+ FlushBlockFile();
if (!view.Flush())
return false;
+ }
// At this point, all changes have been done to the database.
// Proceed by updating the memory structures.
@@ -1823,7 +1842,6 @@ bool CBlock::AddToBlockIndex(const CDiskBlockPos &pos)
}
-
bool FindBlockPos(CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64 nTime)
{
bool fUpdatedLast = false;
@@ -1832,12 +1850,7 @@ bool FindBlockPos(CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeigh
while (infoLastBlockFile.nSize + nAddSize >= MAX_BLOCKFILE_SIZE) {
printf("Leaving block file %i: %s\n", nLastBlockFile, infoLastBlockFile.ToString().c_str());
- FILE *file = OpenBlockFile(pos);
- FileCommit(file);
- fclose(file);
- file = OpenUndoFile(pos);
- FileCommit(file);
- fclose(file);
+ FlushBlockFile();
nLastBlockFile++;
infoLastBlockFile.SetNull();
pblocktree->ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile); // check whether data for the new file somehow already exist; can fail just fine