Skip to content

Commit 0ec16f3

Browse files
committed
Move only: extract WriteChainState and UpdatedTip from SetBestChain.
1 parent ca1913e commit 0ec16f3

File tree

1 file changed

+63
-60
lines changed

1 file changed

+63
-60
lines changed

src/main.cpp

Lines changed: 63 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,6 +1746,66 @@ bool ConnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, C
17461746
return true;
17471747
}
17481748

1749+
bool static WriteChainState(CValidationState &state) {
1750+
if (!IsInitialBlockDownload() || pcoinsTip->GetCacheSize() > nCoinCacheSize) {
1751+
// Typical CCoins structures on disk are around 100 bytes in size.
1752+
// Pushing a new one to the database can cause it to be written
1753+
// twice (once in the log, and once in the tables). This is already
1754+
// an overestimation, as most will delete an existing entry or
1755+
// overwrite one. Still, use a conservative safety factor of 2.
1756+
if (!CheckDiskSpace(100 * 2 * 2 * pcoinsTip->GetCacheSize()))
1757+
return state.Error();
1758+
FlushBlockFile();
1759+
pblocktree->Sync();
1760+
if (!pcoinsTip->Flush())
1761+
return state.Abort(_("Failed to write to coin database"));
1762+
}
1763+
return true;
1764+
}
1765+
1766+
void static UpdateTip(CBlockIndex *pindexNew) {
1767+
chainActive.SetTip(pindexNew);
1768+
1769+
// Update best block in wallet (so we can detect restored wallets)
1770+
bool fIsInitialDownload = IsInitialBlockDownload();
1771+
if ((chainActive.Height() % 20160) == 0 || (!fIsInitialDownload && (chainActive.Height() % 144) == 0))
1772+
g_signals.SetBestChain(chainActive.GetLocator());
1773+
1774+
// New best block
1775+
nTimeBestReceived = GetTime();
1776+
mempool.AddTransactionsUpdated(1);
1777+
LogPrintf("UpdateTip: new best=%s height=%d log2_work=%.8g tx=%lu date=%s progress=%f\n",
1778+
chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), log(chainActive.Tip()->nChainWork.getdouble())/log(2.0), (unsigned long)chainActive.Tip()->nChainTx,
1779+
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
1780+
Checkpoints::GuessVerificationProgress(chainActive.Tip()));
1781+
1782+
// Check the version of the last 100 blocks to see if we need to upgrade:
1783+
if (!fIsInitialDownload)
1784+
{
1785+
int nUpgraded = 0;
1786+
const CBlockIndex* pindex = chainActive.Tip();
1787+
for (int i = 0; i < 100 && pindex != NULL; i++)
1788+
{
1789+
if (pindex->nVersion > CBlock::CURRENT_VERSION)
1790+
++nUpgraded;
1791+
pindex = pindex->pprev;
1792+
}
1793+
if (nUpgraded > 0)
1794+
LogPrintf("SetBestChain: %d of last 100 blocks above version %d\n", nUpgraded, (int)CBlock::CURRENT_VERSION);
1795+
if (nUpgraded > 100/2)
1796+
// strMiscWarning is read by GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
1797+
strMiscWarning = _("Warning: This version is obsolete, upgrade required!");
1798+
}
1799+
1800+
std::string strCmd = GetArg("-blocknotify", "");
1801+
1802+
if (!fIsInitialDownload && !strCmd.empty())
1803+
{
1804+
boost::replace_all(strCmd, "%s", chainActive.Tip()->GetBlockHash().GetHex());
1805+
boost::thread t(runCommand, strCmd); // thread runs free
1806+
}
1807+
}
1808+
17491809
bool SetBestChain(CValidationState &state, CBlockIndex* pindexNew)
17501810
{
17511811
mempool.check(pcoinsTip);
@@ -1839,27 +1899,8 @@ bool SetBestChain(CValidationState &state, CBlockIndex* pindexNew)
18391899
if (fBenchmark)
18401900
LogPrintf("- Flush %i transactions: %.2fms (%.4fms/tx)\n", nModified, 0.001 * nTime, 0.001 * nTime / nModified);
18411901

1842-
// Make sure it's successfully written to disk before changing memory structure
1843-
bool fIsInitialDownload = IsInitialBlockDownload();
1844-
if (!fIsInitialDownload || pcoinsTip->GetCacheSize() > nCoinCacheSize) {
1845-
// Typical CCoins structures on disk are around 100 bytes in size.
1846-
// Pushing a new one to the database can cause it to be written
1847-
// twice (once in the log, and once in the tables). This is already
1848-
// an overestimation, as most will delete an existing entry or
1849-
// overwrite one. Still, use a conservative safety factor of 2.
1850-
if (!CheckDiskSpace(100 * 2 * 2 * pcoinsTip->GetCacheSize()))
1851-
return state.Error();
1852-
FlushBlockFile();
1853-
pblocktree->Sync();
1854-
if (!pcoinsTip->Flush())
1855-
return state.Abort(_("Failed to write to coin database"));
1856-
}
1857-
1858-
// At this point, all changes have been done to the database.
1859-
// Proceed by updating the memory structures.
1860-
1861-
// Register new best chain
1862-
chainActive.SetTip(pindexNew);
1902+
if (!WriteChainState(state))
1903+
return false;
18631904

18641905
// Resurrect memory transactions that were in the disconnected branch
18651906
BOOST_FOREACH(CTransaction& tx, vResurrect) {
@@ -1877,48 +1918,10 @@ bool SetBestChain(CValidationState &state, CBlockIndex* pindexNew)
18771918

18781919
mempool.check(pcoinsTip);
18791920

1880-
// Update best block in wallet (so we can detect restored wallets)
1881-
if ((pindexNew->nHeight % 20160) == 0 || (!fIsInitialDownload && (pindexNew->nHeight % 144) == 0))
1882-
g_signals.SetBestChain(chainActive.GetLocator(pindexNew));
1883-
1884-
// New best block
1885-
nTimeBestReceived = GetTime();
1886-
mempool.AddTransactionsUpdated(1);
1887-
LogPrintf("SetBestChain: new best=%s height=%d log2_work=%.8g tx=%lu date=%s progress=%f\n",
1888-
chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), log(chainActive.Tip()->nChainWork.getdouble())/log(2.0), (unsigned long)pindexNew->nChainTx,
1889-
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
1890-
Checkpoints::GuessVerificationProgress(chainActive.Tip()));
1891-
1892-
// Check the version of the last 100 blocks to see if we need to upgrade:
1893-
if (!fIsInitialDownload)
1894-
{
1895-
int nUpgraded = 0;
1896-
const CBlockIndex* pindex = chainActive.Tip();
1897-
for (int i = 0; i < 100 && pindex != NULL; i++)
1898-
{
1899-
if (pindex->nVersion > CBlock::CURRENT_VERSION)
1900-
++nUpgraded;
1901-
pindex = pindex->pprev;
1902-
}
1903-
if (nUpgraded > 0)
1904-
LogPrintf("SetBestChain: %d of last 100 blocks above version %d\n", nUpgraded, (int)CBlock::CURRENT_VERSION);
1905-
if (nUpgraded > 100/2)
1906-
// strMiscWarning is read by GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
1907-
strMiscWarning = _("Warning: This version is obsolete, upgrade required!");
1908-
}
1909-
1910-
std::string strCmd = GetArg("-blocknotify", "");
1911-
1912-
if (!fIsInitialDownload && !strCmd.empty())
1913-
{
1914-
boost::replace_all(strCmd, "%s", chainActive.Tip()->GetBlockHash().GetHex());
1915-
boost::thread t(runCommand, strCmd); // thread runs free
1916-
}
1917-
1921+
UpdateTip(pindexNew);
19181922
return true;
19191923
}
19201924

1921-
19221925
bool AddToBlockIndex(CBlock& block, CValidationState& state, const CDiskBlockPos& pos)
19231926
{
19241927
// Check for duplicate

0 commit comments

Comments
 (0)