aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2012-09-03 15:26:57 +0200
committerPieter Wuille <pieter.wuille@gmail.com>2012-10-20 23:08:57 +0200
commitd979e6e36ac6be0d40b2a6bb70c668f9e6989ff9 (patch)
tree6ed14b062fb97de3c1b9646161297649825b62e4 /src/main.cpp
parent857c61df0b71c8a0482b1bf8fc55849f8ad831b8 (diff)
downloadbitcoin-d979e6e36ac6be0d40b2a6bb70c668f9e6989ff9.tar.xz
Use singleton block tree database instance
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp61
1 files changed, 24 insertions, 37 deletions
diff --git a/src/main.cpp b/src/main.cpp
index b077eb9d6e..6395a25415 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -270,6 +270,7 @@ bool CCoinsViewMemPool::HaveCoins(uint256 txid) {
}
CCoinsViewCache *pcoinsTip = NULL;
+CBlockTreeDB *pblocktree = NULL;
//////////////////////////////////////////////////////////////////////////////
//
@@ -1143,7 +1144,7 @@ void static InvalidChainFound(CBlockIndex* pindexNew)
if (pindexNew->bnChainWork > bnBestInvalidWork)
{
bnBestInvalidWork = pindexNew->bnChainWork;
- CChainDB().WriteBestInvalidWork(bnBestInvalidWork);
+ pblocktree->WriteBestInvalidWork(bnBestInvalidWork);
uiInterface.NotifyBlocksChanged();
}
printf("InvalidChainFound: invalid block=%s height=%d work=%s date=%s\n",
@@ -1159,7 +1160,7 @@ void static InvalidChainFound(CBlockIndex* pindexNew)
void static InvalidBlockFound(CBlockIndex *pindex) {
pindex->nStatus |= BLOCK_FAILED_VALID;
- CChainDB().WriteBlockIndex(CDiskBlockIndex(pindex));
+ pblocktree->WriteBlockIndex(CDiskBlockIndex(pindex));
setBlockIndexValid.erase(pindex);
InvalidChainFound(pindex);
if (pindex->pnext)
@@ -1186,12 +1187,11 @@ bool ConnectBestBlock() {
do {
if (pindexTest->nStatus & BLOCK_FAILED_MASK) {
// mark descendants failed
- CChainDB chaindb;
CBlockIndex *pindexFailed = pindexNewBest;
while (pindexTest != pindexFailed) {
pindexFailed->nStatus |= BLOCK_FAILED_CHILD;
setBlockIndexValid.erase(pindexFailed);
- chaindb.WriteBlockIndex(CDiskBlockIndex(pindexFailed));
+ pblocktree->WriteBlockIndex(CDiskBlockIndex(pindexFailed));
pindexFailed = pindexFailed->pprev;
}
InvalidChainFound(pindexNewBest);
@@ -1498,7 +1498,7 @@ bool CBlock::DisconnectBlock(CBlockIndex *pindex, CCoinsViewCache &view)
return true;
}
-bool FindUndoPos(CChainDB &chaindb, int nFile, CDiskBlockPos &pos, unsigned int nAddSize);
+bool FindUndoPos(int nFile, CDiskBlockPos &pos, unsigned int nAddSize);
bool CBlock::ConnectBlock(CBlockIndex* pindex, CCoinsViewCache &view, bool fJustCheck)
{
@@ -1581,11 +1581,9 @@ bool CBlock::ConnectBlock(CBlockIndex* pindex, CCoinsViewCache &view, bool fJust
// Write undo information to disk
if (pindex->GetUndoPos().IsNull() || (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_SCRIPTS)
{
- CChainDB chaindb;
-
if (pindex->GetUndoPos().IsNull()) {
CDiskBlockPos pos;
- if (!FindUndoPos(chaindb, pindex->nFile, pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 8))
+ if (!FindUndoPos(pindex->nFile, pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 8))
return error("ConnectBlock() : FindUndoPos failed");
if (!blockundo.WriteToDisk(pos))
return error("ConnectBlock() : CBlockUndo::WriteToDisk failed");
@@ -1598,7 +1596,7 @@ bool CBlock::ConnectBlock(CBlockIndex* pindex, CCoinsViewCache &view, bool fJust
pindex->nStatus = (pindex->nStatus & ~BLOCK_VALID_MASK) | BLOCK_VALID_SCRIPTS;
CDiskBlockIndex blockindex(pindex);
- if (!chaindb.WriteBlockIndex(blockindex))
+ if (!pblocktree->WriteBlockIndex(blockindex))
return error("ConnectBlock() : WriteBlockIndex failed");
}
@@ -1804,12 +1802,7 @@ bool CBlock::AddToBlockIndex(const CDiskBlockPos &pos)
pindexNew->nStatus = BLOCK_VALID_TRANSACTIONS | BLOCK_HAVE_DATA;
setBlockIndexValid.insert(pindexNew);
- CChainDB chaindb;
- if (!chaindb.TxnBegin())
- return false;
- chaindb.WriteBlockIndex(CDiskBlockIndex(pindexNew));
- if (!chaindb.TxnCommit())
- return false;
+ pblocktree->WriteBlockIndex(CDiskBlockIndex(pindexNew));
// New best?
if (!ConnectBestBlock())
@@ -1823,13 +1816,15 @@ bool CBlock::AddToBlockIndex(const CDiskBlockPos &pos)
hashPrevBestCoinBase = GetTxHash(0);
}
+ pblocktree->Flush();
+
uiInterface.NotifyBlocksChanged();
return true;
}
-bool FindBlockPos(CChainDB &chaindb, CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64 nTime)
+bool FindBlockPos(CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64 nTime)
{
bool fUpdatedLast = false;
@@ -1845,7 +1840,7 @@ bool FindBlockPos(CChainDB &chaindb, CDiskBlockPos &pos, unsigned int nAddSize,
fclose(file);
nLastBlockFile++;
infoLastBlockFile.SetNull();
- chaindb.ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile); // check whether data for the new file somehow already exist; can fail just fine
+ pblocktree->ReadBlockFileInfo(nLastBlockFile, infoLastBlockFile); // check whether data for the new file somehow already exist; can fail just fine
fUpdatedLast = true;
}
@@ -1865,15 +1860,15 @@ bool FindBlockPos(CChainDB &chaindb, CDiskBlockPos &pos, unsigned int nAddSize,
fclose(file);
}
- if (!chaindb.WriteBlockFileInfo(nLastBlockFile, infoLastBlockFile))
+ if (!pblocktree->WriteBlockFileInfo(nLastBlockFile, infoLastBlockFile))
return error("FindBlockPos() : cannot write updated block info");
if (fUpdatedLast)
- chaindb.WriteLastBlockFile(nLastBlockFile);
+ pblocktree->WriteLastBlockFile(nLastBlockFile);
return true;
}
-bool FindUndoPos(CChainDB &chaindb, int nFile, CDiskBlockPos &pos, unsigned int nAddSize)
+bool FindUndoPos(int nFile, CDiskBlockPos &pos, unsigned int nAddSize)
{
pos.nFile = nFile;
@@ -1883,15 +1878,15 @@ bool FindUndoPos(CChainDB &chaindb, int nFile, CDiskBlockPos &pos, unsigned int
if (nFile == nLastBlockFile) {
pos.nPos = infoLastBlockFile.nUndoSize;
nNewSize = (infoLastBlockFile.nUndoSize += nAddSize);
- if (!chaindb.WriteBlockFileInfo(nLastBlockFile, infoLastBlockFile))
+ if (!pblocktree->WriteBlockFileInfo(nLastBlockFile, infoLastBlockFile))
return error("FindUndoPos() : cannot write updated block info");
} else {
CBlockFileInfo info;
- if (!chaindb.ReadBlockFileInfo(nFile, info))
+ if (!pblocktree->ReadBlockFileInfo(nFile, info))
return error("FindUndoPos() : cannot read block info");
pos.nPos = info.nUndoSize;
nNewSize = (info.nUndoSize += nAddSize);
- if (!chaindb.WriteBlockFileInfo(nFile, info))
+ if (!pblocktree->WriteBlockFileInfo(nFile, info))
return error("FindUndoPos() : cannot write updated block info");
}
@@ -2022,11 +2017,8 @@ bool CBlock::AcceptBlock()
if (!CheckDiskSpace(::GetSerializeSize(*this, SER_DISK, CLIENT_VERSION)))
return error("AcceptBlock() : out of disk space");
CDiskBlockPos blockPos;
- {
- CChainDB chaindb;
- if (!FindBlockPos(chaindb, blockPos, nBlockSize+8, nHeight, nTime))
- return error("AcceptBlock() : FindBlockPos failed");
- }
+ if (!FindBlockPos(blockPos, nBlockSize+8, nHeight, nTime))
+ return error("AcceptBlock() : FindBlockPos failed");
if (!WriteToDisk(blockPos))
return error("AcceptBlock() : WriteToDisk failed");
if (!AddToBlockIndex(blockPos))
@@ -2210,12 +2202,10 @@ bool LoadBlockIndex(bool fAllowNew)
}
//
- // Load block index
+ // Load block index from databases
//
- CChainDB chaindb("cr");
- if (!LoadBlockIndex(chaindb))
+ if (!LoadBlockIndexDB())
return false;
- chaindb.Close();
//
// Init with genesis block
@@ -2267,11 +2257,8 @@ bool LoadBlockIndex(bool fAllowNew)
// Start new block file
unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
CDiskBlockPos blockPos;
- {
- CChainDB chaindb;
- if (!FindBlockPos(chaindb, blockPos, nBlockSize+8, 0, block.nTime))
- return error("AcceptBlock() : FindBlockPos failed");
- }
+ if (!FindBlockPos(blockPos, nBlockSize+8, 0, block.nTime))
+ return error("AcceptBlock() : FindBlockPos failed");
if (!block.WriteToDisk(blockPos))
return error("LoadBlockIndex() : writing genesis block to disk failed");
if (!block.AddToBlockIndex(blockPos))