Merge pull request #883 from sipa/loadblock

Add -loadblock to load from an external blk000?.dat file
This commit is contained in:
Gregory Maxwell 2012-05-08 12:37:27 -07:00
commit f1ae31d8af
4 changed files with 72 additions and 3 deletions

View File

@ -613,7 +613,7 @@ bool CTxDB::LoadBlockIndex()
map<pair<unsigned int, unsigned int>, CBlockIndex*> mapBlockPos; map<pair<unsigned int, unsigned int>, CBlockIndex*> mapBlockPos;
for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev) for (CBlockIndex* pindex = pindexBest; pindex && pindex->pprev; pindex = pindex->pprev)
{ {
if (pindex->nHeight < nBestHeight-nCheckDepth) if (fRequestShutdown || pindex->nHeight < nBestHeight-nCheckDepth)
break; break;
CBlock block; CBlock block;
if (!block.ReadFromDisk(pindex)) if (!block.ReadFromDisk(pindex))
@ -715,7 +715,7 @@ bool CTxDB::LoadBlockIndex()
} }
} }
} }
if (pindexFork) if (pindexFork && !fRequestShutdown)
{ {
// Reorg back to the fork // Reorg back to the fork
printf("LoadBlockIndex() : *** moving best chain pointer back to block %d\n", pindexFork->nHeight); printf("LoadBlockIndex() : *** moving best chain pointer back to block %d\n", pindexFork->nHeight);

View File

@ -226,7 +226,8 @@ bool AppInit2(int argc, char* argv[])
" -keypool=<n> \t " + _("Set key pool size to <n> (default: 100)") + "\n" + " -keypool=<n> \t " + _("Set key pool size to <n> (default: 100)") + "\n" +
" -rescan \t " + _("Rescan the block chain for missing wallet transactions") + "\n" + " -rescan \t " + _("Rescan the block chain for missing wallet transactions") + "\n" +
" -checkblocks=<n> \t\t " + _("How many blocks to check at startup (default: 2500, 0 = all)") + "\n" + " -checkblocks=<n> \t\t " + _("How many blocks to check at startup (default: 2500, 0 = all)") + "\n" +
" -checklevel=<n> \t\t " + _("How thorough the block verification is (0-6, default: 1)") + "\n"; " -checklevel=<n> \t\t " + _("How thorough the block verification is (0-6, default: 1)") + "\n" +
" -loadblock=<file>\t " + _("Imports blocks from external blk000?.dat file") + "\n";
strUsage += string() + strUsage += string() +
_("\nSSL options: (see the Bitcoin Wiki for SSL setup instructions)") + "\n" + _("\nSSL options: (see the Bitcoin Wiki for SSL setup instructions)") + "\n" +
@ -367,6 +368,16 @@ bool AppInit2(int argc, char* argv[])
} }
printf(" block index %15"PRI64d"ms\n", GetTimeMillis() - nStart); printf(" block index %15"PRI64d"ms\n", GetTimeMillis() - nStart);
if (mapArgs.count("-loadblock"))
{
BOOST_FOREACH(string strFile, mapMultiArgs["-loadblock"])
{
FILE *file = fopen(strFile.c_str(), "rb");
if (file)
LoadExternalBlockFile(file);
}
}
InitMessage(_("Loading wallet...")); InitMessage(_("Loading wallet..."));
printf("Loading wallet...\n"); printf("Loading wallet...\n");
nStart = GetTimeMillis(); nStart = GetTimeMillis();

View File

@ -2028,6 +2028,62 @@ void PrintBlockTree()
} }
} }
bool LoadExternalBlockFile(FILE* fileIn)
{
int nLoaded = 0;
{
LOCK(cs_main);
try {
CAutoFile blkdat(fileIn, SER_DISK, CLIENT_VERSION);
unsigned int nPos = 0;
while (nPos != -1 && blkdat.good() && !fRequestShutdown)
{
unsigned char pchData[65536];
do {
fseek(blkdat, nPos, SEEK_SET);
int nRead = fread(pchData, 1, sizeof(pchData), blkdat);
if (nRead <= 8)
{
nPos = -1;
break;
}
void* nFind = memchr(pchData, pchMessageStart[0], nRead+1-sizeof(pchMessageStart));
if (nFind)
{
if (memcmp(nFind, pchMessageStart, sizeof(pchMessageStart))==0)
{
nPos += ((unsigned char*)nFind - pchData) + sizeof(pchMessageStart);
break;
}
nPos += ((unsigned char*)nFind - pchData) + 1;
}
else
nPos += sizeof(pchData) - sizeof(pchMessageStart) + 1;
} while(!fRequestShutdown);
if (nPos == -1)
break;
fseek(blkdat, nPos, SEEK_SET);
unsigned int nSize;
blkdat >> nSize;
if (nSize > 0 && nSize <= MAX_BLOCK_SIZE)
{
CBlock block;
blkdat >> block;
if (ProcessBlock(NULL,&block))
{
nLoaded++;
nPos += 4 + nSize;
}
}
}
}
catch (std::exception &e)
{
}
}
printf("Loaded %i blocks from external file\n", nLoaded);
return nLoaded > 0;
}

View File

@ -69,6 +69,7 @@ extern int64 nHPSTimerStart;
extern int64 nTimeBestReceived; extern int64 nTimeBestReceived;
extern CCriticalSection cs_setpwalletRegistered; extern CCriticalSection cs_setpwalletRegistered;
extern std::set<CWallet*> setpwalletRegistered; extern std::set<CWallet*> setpwalletRegistered;
extern unsigned char pchMessageStart[4];
// Settings // Settings
extern int64 nTransactionFee; extern int64 nTransactionFee;
@ -91,6 +92,7 @@ bool LoadBlockIndex(bool fAllowNew=true);
void PrintBlockTree(); void PrintBlockTree();
bool ProcessMessages(CNode* pfrom); bool ProcessMessages(CNode* pfrom);
bool SendMessages(CNode* pto, bool fSendTrickle); bool SendMessages(CNode* pto, bool fSendTrickle);
bool LoadExternalBlockFile(FILE* fileIn);
void GenerateBitcoins(bool fGenerate, CWallet* pwallet); void GenerateBitcoins(bool fGenerate, CWallet* pwallet);
CBlock* CreateNewBlock(CReserveKey& reservekey); CBlock* CreateNewBlock(CReserveKey& reservekey);
void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce); void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce);