mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-26 11:13:23 -03:00
Make sure the genesis block is present after reindex
This commit is contained in:
parent
3d29d5d623
commit
386037615a
4 changed files with 36 additions and 20 deletions
|
@ -365,6 +365,8 @@ void ThreadImport(void *data) {
|
||||||
pblocktree->WriteReindexing(false);
|
pblocktree->WriteReindexing(false);
|
||||||
fReindex = false;
|
fReindex = false;
|
||||||
printf("Reindexing finished\n");
|
printf("Reindexing finished\n");
|
||||||
|
// To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
|
||||||
|
InitBlockIndex();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -802,6 +804,10 @@ bool AppInit2()
|
||||||
if (!LoadBlockIndex())
|
if (!LoadBlockIndex())
|
||||||
return InitError(_("Error loading block database"));
|
return InitError(_("Error loading block database"));
|
||||||
|
|
||||||
|
// Initialize the block index (no-op if non-empty database was already loaded)
|
||||||
|
if (!InitBlockIndex())
|
||||||
|
return InitError(_("Error initializing block database"));
|
||||||
|
|
||||||
uiInterface.InitMessage(_("Verifying block database integrity..."));
|
uiInterface.InitMessage(_("Verifying block database integrity..."));
|
||||||
|
|
||||||
if (!VerifyDB())
|
if (!VerifyDB())
|
||||||
|
|
46
src/main.cpp
46
src/main.cpp
|
@ -2661,18 +2661,22 @@ bool LoadBlockIndex()
|
||||||
if (!fReindex && !LoadBlockIndexDB())
|
if (!fReindex && !LoadBlockIndexDB())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
//
|
return true;
|
||||||
// Init with genesis block
|
}
|
||||||
//
|
|
||||||
if (mapBlockIndex.empty())
|
|
||||||
{
|
|
||||||
fTxIndex = GetBoolArg("-txindex", false);
|
|
||||||
pblocktree->WriteFlag("txindex", fTxIndex);
|
|
||||||
printf("Initializing databases...\n");
|
|
||||||
|
|
||||||
if (fReindex)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
|
bool InitBlockIndex() {
|
||||||
|
// Check whether we're already initialized
|
||||||
|
if (pindexGenesisBlock != NULL)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// Use the provided setting for -txindex in the new database
|
||||||
|
fTxIndex = GetBoolArg("-txindex", false);
|
||||||
|
pblocktree->WriteFlag("txindex", fTxIndex);
|
||||||
|
printf("Initializing databases...\n");
|
||||||
|
|
||||||
|
// Only add the genesis block if not reindexing (in which case we reuse the one already on disk)
|
||||||
|
if (!fReindex) {
|
||||||
// Genesis Block:
|
// Genesis Block:
|
||||||
// CBlock(hash=000000000019d6, ver=1, hashPrevBlock=00000000000000, hashMerkleRoot=4a5e1e, nTime=1231006505, nBits=1d00ffff, nNonce=2083236893, vtx=1)
|
// CBlock(hash=000000000019d6, ver=1, hashPrevBlock=00000000000000, hashMerkleRoot=4a5e1e, nTime=1231006505, nBits=1d00ffff, nNonce=2083236893, vtx=1)
|
||||||
// CTransaction(hash=4a5e1e, ver=1, vin.size=1, vout.size=1, nLockTime=0)
|
// CTransaction(hash=4a5e1e, ver=1, vin.size=1, vout.size=1, nLockTime=0)
|
||||||
|
@ -2713,15 +2717,19 @@ bool LoadBlockIndex()
|
||||||
assert(hash == hashGenesisBlock);
|
assert(hash == hashGenesisBlock);
|
||||||
|
|
||||||
// Start new block file
|
// Start new block file
|
||||||
unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
|
try {
|
||||||
CDiskBlockPos blockPos;
|
unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
|
||||||
CValidationState state;
|
CDiskBlockPos blockPos;
|
||||||
if (!FindBlockPos(state, blockPos, nBlockSize+8, 0, block.nTime))
|
CValidationState state;
|
||||||
return error("AcceptBlock() : FindBlockPos failed");
|
if (!FindBlockPos(state, blockPos, nBlockSize+8, 0, block.nTime))
|
||||||
if (!block.WriteToDisk(blockPos))
|
return error("AcceptBlock() : FindBlockPos failed");
|
||||||
return error("LoadBlockIndex() : writing genesis block to disk failed");
|
if (!block.WriteToDisk(blockPos))
|
||||||
if (!block.AddToBlockIndex(state, blockPos))
|
return error("LoadBlockIndex() : writing genesis block to disk failed");
|
||||||
return error("LoadBlockIndex() : genesis block not accepted");
|
if (!block.AddToBlockIndex(state, blockPos))
|
||||||
|
return error("LoadBlockIndex() : genesis block not accepted");
|
||||||
|
} catch(std::runtime_error &e) {
|
||||||
|
return error("LoadBlockIndex() : failed to initialize block database: %s", e.what());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -133,6 +133,8 @@ FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly = false);
|
||||||
FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly = false);
|
FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly = false);
|
||||||
/** Import blocks from an external file */
|
/** Import blocks from an external file */
|
||||||
bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp = NULL);
|
bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp = NULL);
|
||||||
|
/** Initialize a new block tree database + block data on disk */
|
||||||
|
bool InitBlockIndex();
|
||||||
/** Load the block tree and coins database from disk */
|
/** Load the block tree and coins database from disk */
|
||||||
bool LoadBlockIndex();
|
bool LoadBlockIndex();
|
||||||
/** Verify consistency of the block and coin databases */
|
/** Verify consistency of the block and coin databases */
|
||||||
|
|
|
@ -28,7 +28,7 @@ struct TestingSetup {
|
||||||
pblocktree = new CBlockTreeDB(1 << 20, true);
|
pblocktree = new CBlockTreeDB(1 << 20, true);
|
||||||
pcoinsdbview = new CCoinsViewDB(1 << 23, true);
|
pcoinsdbview = new CCoinsViewDB(1 << 23, true);
|
||||||
pcoinsTip = new CCoinsViewCache(*pcoinsdbview);
|
pcoinsTip = new CCoinsViewCache(*pcoinsdbview);
|
||||||
LoadBlockIndex();
|
InitBlockIndex();
|
||||||
bool fFirstRun;
|
bool fFirstRun;
|
||||||
pwalletMain = new CWallet("wallet.dat");
|
pwalletMain = new CWallet("wallet.dat");
|
||||||
pwalletMain->LoadWallet(fFirstRun);
|
pwalletMain->LoadWallet(fFirstRun);
|
||||||
|
|
Loading…
Add table
Reference in a new issue