Skip to content

Commit 5d37c1b

Browse files
committed
Merge #15976: refactor: move methods under CChainState (pt. 1)
403e677 refactoring: IsInitialBlockDownload -> CChainState (James O'Beirne) 3ccbc37 refactoring: FlushStateToDisk -> CChainState (James O'Beirne) 4d66886 refactoring: introduce ChainstateActive() (James O'Beirne) d7c97ed move-only: make the CChainState interface public (James O'Beirne) Pull request description: This is part of the [assumeutxo project](https://github.com/bitcoin/bitcoin/projects/11): Parent PR: #15606 Issue: #15605 Specification: https://github.com/jamesob/assumeutxo-docs/tree/2019-04-proposal/proposal --- This changeset starts moving functionality intimately related to CChainState into methods. Parameterizing these functions by a particular CChainState is necessary for the use of multiple chainstates simultaneously (e.g. for asynchronous background validation). In this change, we - make the CChainState interface public - since other units will start to invoke its methods directly, - introduce `::ChainstateActive()`, the CChainState equivalent for `::ChainActive()`, - and move `IsInitialBlockDownload()` and `FlushStateToDisk()` into methods on CChainState. Independent of assumeutxo, these changes better encapsulate chainstate behavior and allow easier use from a testing context. There are more methods that we'll move in the future, but they require other substantial changes (i.e. moving ownership of the `CCoinsView*` hierarchy into CChainState) so we'll save them for future PRs. --- The first move-only commit is most easily reviewed with `git diff ... --color-moved=dimmed_zebra`. ACKs for commit 403e67: Empact: utACK bitcoin/bitcoin@403e677 no need to address my nits herein Sjors: utACK 403e677 ryanofsky: utACK 403e677. Only change since previous review is removing global state comment as suggested. MarcoFalke: utACK 403e677, though the diff still seems a bit bloated with some unnecessary changes in the second commit. promag: utACK 403e677 and rebased with current [master](c7cfd20). Tree-SHA512: 6fcf260bb2dc201361170c0b4547405366f5f331fcc3a2bac29b24442814b7b244ca1b58aac5af716885f9a130c343b544590dff780da0bf835c7c5b3ccb2257
2 parents 51858c5 + 403e677 commit 5d37c1b

File tree

8 files changed

+273
-239
lines changed

8 files changed

+273
-239
lines changed

src/init.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ void Shutdown(InitInterfaces& interfaces)
259259

260260
// FlushStateToDisk generates a ChainStateFlushed callback, which we should avoid missing
261261
if (pcoinsTip != nullptr) {
262-
FlushStateToDisk();
262+
::ChainstateActive().ForceFlushStateToDisk();
263263
}
264264

265265
// After there are no more peers/RPC left to give us new data which may generate
@@ -275,7 +275,7 @@ void Shutdown(InitInterfaces& interfaces)
275275
{
276276
LOCK(cs_main);
277277
if (pcoinsTip != nullptr) {
278-
FlushStateToDisk();
278+
::ChainstateActive().ForceFlushStateToDisk();
279279
}
280280
pcoinsTip.reset();
281281
pcoinscatcher.reset();
@@ -1693,7 +1693,7 @@ bool AppInitMain(InitInterfaces& interfaces)
16931693
nLocalServices = ServiceFlags(nLocalServices & ~NODE_NETWORK);
16941694
if (!fReindex) {
16951695
uiInterface.InitMessage(_("Pruning blockstore..."));
1696-
PruneAndFlush();
1696+
::ChainstateActive().PruneAndFlush();
16971697
}
16981698
}
16991699

src/interfaces/chain.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,8 @@ class ChainImpl : public Chain
334334
return ::fHavePruned;
335335
}
336336
bool p2pEnabled() override { return g_connman != nullptr; }
337-
bool isReadyToBroadcast() override { return !::fImporting && !::fReindex && !IsInitialBlockDownload(); }
338-
bool isInitialBlockDownload() override { return IsInitialBlockDownload(); }
337+
bool isReadyToBroadcast() override { return !::fImporting && !::fReindex && !isInitialBlockDownload(); }
338+
bool isInitialBlockDownload() override { return ::ChainstateActive().IsInitialBlockDownload(); }
339339
bool shutdownRequested() override { return ShutdownRequested(); }
340340
int64_t getAdjustedTime() override { return GetAdjustedTime(); }
341341
void initMessage(const std::string& message) override { ::uiInterface.InitMessage(message); }

src/interfaces/node.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class NodeImpl : public Node
197197
}
198198
return GuessVerificationProgress(Params().TxData(), tip);
199199
}
200-
bool isInitialBlockDownload() override { return IsInitialBlockDownload(); }
200+
bool isInitialBlockDownload() override { return ::ChainstateActive().IsInitialBlockDownload(); }
201201
bool getReindex() override { return ::fReindex; }
202202
bool getImporting() override { return ::fImporting; }
203203
void setNetworkActive(bool active) override

src/net_processing.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ void PeerLogicValidation::BlockChecked(const CBlock& block, const CValidationSta
12401240
// the tip yet so we have no way to check this directly here. Instead we
12411241
// just check that there are currently no other blocks in flight.
12421242
else if (state.IsValid() &&
1243-
!IsInitialBlockDownload() &&
1243+
!::ChainstateActive().IsInitialBlockDownload() &&
12441244
mapBlocksInFlight.count(hash) == mapBlocksInFlight.size()) {
12451245
if (it != mapBlockSource.end()) {
12461246
MaybeSetPeerAsAnnouncingHeaderAndIDs(it->second.first, connman);
@@ -1728,7 +1728,7 @@ bool static ProcessHeadersMessage(CNode *pfrom, CConnman *connman, const std::ve
17281728
}
17291729
// If we're in IBD, we want outbound peers that will serve us a useful
17301730
// chain. Disconnect peers that are on chains with insufficient work.
1731-
if (IsInitialBlockDownload() && nCount != MAX_HEADERS_RESULTS) {
1731+
if (::ChainstateActive().IsInitialBlockDownload() && nCount != MAX_HEADERS_RESULTS) {
17321732
// When nCount < MAX_HEADERS_RESULTS, we know we have no more
17331733
// headers to fetch from this peer.
17341734
if (nodestate->pindexBestKnownBlock && nodestate->pindexBestKnownBlock->nChainWork < nMinimumChainWork) {
@@ -1994,7 +1994,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
19941994
if (!pfrom->fInbound)
19951995
{
19961996
// Advertise our address
1997-
if (fListen && !IsInitialBlockDownload())
1997+
if (fListen && !::ChainstateActive().IsInitialBlockDownload())
19981998
{
19991999
CAddress addr = GetLocalAddress(&pfrom->addr, pfrom->GetLocalServices());
20002000
FastRandomContext insecure_rand;
@@ -2229,7 +2229,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
22292229
pfrom->AddInventoryKnown(inv);
22302230
if (fBlocksOnly) {
22312231
LogPrint(BCLog::NET, "transaction (%s) inv sent in violation of protocol peer=%d\n", inv.hash.ToString(), pfrom->GetId());
2232-
} else if (!fAlreadyHave && !fImporting && !fReindex && !IsInitialBlockDownload()) {
2232+
} else if (!fAlreadyHave && !fImporting && !fReindex && !::ChainstateActive().IsInitialBlockDownload()) {
22332233
RequestTx(State(pfrom->GetId()), inv.hash, nNow);
22342234
}
22352235
}
@@ -2387,7 +2387,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
23872387
}
23882388

23892389
LOCK(cs_main);
2390-
if (IsInitialBlockDownload() && !pfrom->fWhitelisted) {
2390+
if (::ChainstateActive().IsInitialBlockDownload() && !pfrom->fWhitelisted) {
23912391
LogPrint(BCLog::NET, "Ignoring getheaders from peer=%d because node is in initial block download\n", pfrom->GetId());
23922392
return true;
23932393
}
@@ -2609,7 +2609,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
26092609

26102610
if (!LookupBlockIndex(cmpctblock.header.hashPrevBlock)) {
26112611
// Doesn't connect (or is genesis), instead of DoSing in AcceptBlockHeader, request deeper headers
2612-
if (!IsInitialBlockDownload())
2612+
if (!::ChainstateActive().IsInitialBlockDownload())
26132613
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, ::ChainActive().GetLocator(pindexBestHeader), uint256()));
26142614
return true;
26152615
}
@@ -3524,7 +3524,7 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
35243524

35253525
// Address refresh broadcast
35263526
int64_t nNow = GetTimeMicros();
3527-
if (!IsInitialBlockDownload() && pto->nNextLocalAddrSend < nNow) {
3527+
if (!::ChainstateActive().IsInitialBlockDownload() && pto->nNextLocalAddrSend < nNow) {
35283528
AdvertiseLocal(pto);
35293529
pto->nNextLocalAddrSend = PoissonNextSend(nNow, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
35303530
}
@@ -3925,7 +3925,7 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
39253925
// Message: getdata (blocks)
39263926
//
39273927
std::vector<CInv> vGetData;
3928-
if (!pto->fClient && ((fFetch && !pto->m_limited_node) || !IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
3928+
if (!pto->fClient && ((fFetch && !pto->m_limited_node) || !::ChainstateActive().IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
39293929
std::vector<const CBlockIndex*> vToDownload;
39303930
NodeId staller = -1;
39313931
FindNextBlocksToDownload(pto->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller, consensusParams);

src/rpc/blockchain.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,7 @@ static UniValue gettxoutsetinfo(const JSONRPCRequest& request)
10951095
UniValue ret(UniValue::VOBJ);
10961096

10971097
CCoinsStats stats;
1098-
FlushStateToDisk();
1098+
::ChainstateActive().ForceFlushStateToDisk();
10991099
if (GetUTXOStats(pcoinsdbview.get(), stats)) {
11001100
ret.pushKV("height", (int64_t)stats.nHeight);
11011101
ret.pushKV("bestblock", stats.hashBlock.GetHex());
@@ -1360,7 +1360,7 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
13601360
obj.pushKV("difficulty", (double)GetDifficulty(tip));
13611361
obj.pushKV("mediantime", (int64_t)tip->GetMedianTimePast());
13621362
obj.pushKV("verificationprogress", GuessVerificationProgress(Params().TxData(), tip));
1363-
obj.pushKV("initialblockdownload", IsInitialBlockDownload());
1363+
obj.pushKV("initialblockdownload", ::ChainstateActive().IsInitialBlockDownload());
13641364
obj.pushKV("chainwork", tip->nChainWork.GetHex());
13651365
obj.pushKV("size_on_disk", CalculateCurrentUsage());
13661366
obj.pushKV("pruned", fPruneMode);
@@ -2292,7 +2292,7 @@ UniValue scantxoutset(const JSONRPCRequest& request)
22922292
std::unique_ptr<CCoinsViewCursor> pcursor;
22932293
{
22942294
LOCK(cs_main);
2295-
FlushStateToDisk();
2295+
::ChainstateActive().ForceFlushStateToDisk();
22962296
pcursor = std::unique_ptr<CCoinsViewCursor>(pcoinsdbview->Cursor());
22972297
assert(pcursor);
22982298
}

src/rpc/mining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ static UniValue getblocktemplate(const JSONRPCRequest& request)
442442
if (g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL) == 0)
443443
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, PACKAGE_NAME " is not connected!");
444444

445-
if (IsInitialBlockDownload())
445+
if (::ChainstateActive().IsInitialBlockDownload())
446446
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, PACKAGE_NAME " is in initial sync and waiting for blocks...");
447447

448448
static unsigned int nTransactionsUpdatedLast;

0 commit comments

Comments
 (0)