Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tree-wide: De-globalize ChainstateManager #20158

Closed
wants to merge 70 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
333d823
validation: Pass in chainstate to ::LimitMempoolSize
dongcarl Sep 16, 2020
7f51417
validation: Pass in chainstate to IsCurrentForFeeEstimation
dongcarl Sep 16, 2020
e83c853
validation: Pass in chainstate to CheckInputsFromMempoolAndCache
dongcarl Sep 16, 2020
e5198ef
validation: Pass in chain tip to ::CheckFinalTx
dongcarl Sep 16, 2020
6e0f4ae
scripted-diff: Invoke ::CheckFinalTx with chain tip
dongcarl Sep 16, 2020
3404ac0
validation: Remove old CheckFinalTx w/o chain tip param
dongcarl Dec 18, 2020
76e0811
validation: Pass in chainstate to ::CheckSequenceLocks
dongcarl Sep 16, 2020
dd7e989
validation: Add chainstate member to MemPoolAccept
dongcarl Sep 16, 2020
7d90cf9
validation: Pass in chainstate to AcceptToMemoryPoolWithTime
dongcarl Sep 16, 2020
cffd76a
validation: Pass in chainstate to ::LoadMempool
dongcarl Sep 16, 2020
314c92b
validation: Pass in chainstate to ::AcceptToMemoryPool
dongcarl Sep 16, 2020
8b65ddd
scripted-diff: Invoke ::AcceptToMemoryPool with chainstate
dongcarl Sep 16, 2020
85f9844
validation: Remove old AcceptToMemoryPool w/o chainstate param
dongcarl Dec 18, 2020
d722f7d
tree-wide: Fix erroneous AcceptToMemoryPool replacements
dongcarl Oct 15, 2020
b9185dc
validation: Pass in chain to ::TestLockPointValidity
dongcarl Sep 16, 2020
28dd955
validation: Pass in chainstate to CTxMemPool::removeForReorg
dongcarl Sep 16, 2020
5203b85
validation: Pass in chainstate to UpdateMempoolForReorg
dongcarl Sep 16, 2020
64db9d6
validation: Use *this in CChainState::LoadMempool
dongcarl Sep 17, 2020
107a028
COMMITS AFTER THIS ARE NON-BASE
dongcarl Dec 22, 2020
eb8fff3
validation: Remove global ::LoadGenesisBlock
dongcarl Aug 26, 2020
7b76fdb
validation: Remove global ::{{Precious,Invalidate}Block,ResetBlockFai…
dongcarl Sep 15, 2020
403c5cb
validation: Pass in chainstate to UpdateTip
dongcarl Sep 15, 2020
081631e
validation: Pass in chainstate to ::PruneBlockFilesManual
dongcarl Sep 15, 2020
a756ecb
validation: Remove global ::VersionBitsTip{State,SinceHeight,Statistics}
dongcarl Sep 15, 2020
bcf23b5
validation: Pass in chainstate to CVerifyDB::VerifyDB
dongcarl Sep 15, 2020
61a45ad
validation: Move invalid block handling to CChainState
dongcarl Oct 5, 2020
fa730f7
validation: Move LoadBlockIndexDB to CChainState
dongcarl Aug 26, 2020
6b7cf5d
validation: Use *this in CChainState::InvalidateBlock
dongcarl Sep 17, 2020
a765747
validation: Pass in spendheight to CTxMemPool::check
dongcarl Sep 9, 2020
4582551
validation: Use *this in CChainState::ActivateBestChainStep
dongcarl Sep 17, 2020
55a2f2c
validation: Pass in chain to FindBlockPos+SaveBlockToDisk
dongcarl Oct 6, 2020
1886642
validation: Use existing chain member in CChainState::AcceptBlock
dongcarl Oct 6, 2020
f5ee2f3
validation: Use existing chain member in CChainState::LoadGenesisBlock
dongcarl Oct 6, 2020
15b6e6a
COMMITS AFTER THIS ARE NON-BASE
dongcarl Dec 22, 2020
f80378c
miner: Pass in chainstate to BlockAssembler::CreateNewBlock
dongcarl Nov 5, 2020
363d6d5
scripted-diff: Invoke CreateNewBlock with chainstate
dongcarl Nov 5, 2020
ff53263
miner: Remove old CreateNewBlock w/o chainstate param
dongcarl Dec 18, 2020
4620ba1
miner: Pass in blockman to ::RegenerateCommitments
dongcarl Oct 6, 2020
05083dc
node/coinstats: Pass in BlockManager to GetUTXOStats
dongcarl Sep 1, 2020
973fbe8
node: Use existing NodeContext
dongcarl Oct 14, 2020
b652f40
node/ifaces: NodeImpl: Use existing NodeContext member
dongcarl Dec 18, 2020
1bd97da
node/ifaces: ChainImpl: Use existing NodeContext member
dongcarl Dec 18, 2020
2ad7239
net_processing: Move some static functions to PeerManager
dongcarl Sep 22, 2020
4328d09
scripted-diff: net_processing: Use existing chainman
dongcarl Oct 14, 2020
9890464
net_processing: Add review-only assertion to PeerManager
dongcarl Oct 15, 2020
fa970fb
COMMITS AFTER THIS ARE NON-BASE
dongcarl Dec 22, 2020
8919757
rpc/*,rest: Add review-only assertion to EnsureChainman
dongcarl Oct 15, 2020
1ac12e9
rpc/blockchain: Use existing NodeContext
dongcarl Oct 14, 2020
2cb0446
rpc/mining: Use existing NodeContext
dongcarl Oct 14, 2020
a910e41
rpc/rawtx: Use existing NodeContext
dongcarl Oct 14, 2020
0be47f8
rest: Pass in NodeContext to rest_block
dongcarl Sep 2, 2020
3a2a8cf
rest: Use existing NodeContext
dongcarl Oct 14, 2020
2acbe46
fixup! rpc/mining: Use existing NodeContext
dongcarl Jan 26, 2021
711f37a
COMMITS AFTER THIS ARE NON-BASE
dongcarl Dec 22, 2020
409ebc3
bench: Use existing NodeContext in DuplicateInputs
dongcarl Sep 9, 2020
053893a
bench: Use existing chainman in AssembleBlock
dongcarl Sep 18, 2020
b476400
index: Add chainstate member to BaseIndex
dongcarl Oct 14, 2020
9cac0b0
COMMITS AFTER THIS ARE NON-BASE
dongcarl Dec 22, 2020
e391187
init: Use existing chainman
dongcarl Aug 26, 2020
a7461a5
test/util: Use existing chainman in ::PrepareBlock
dongcarl Oct 14, 2020
755ceea
test/miner_tests: Pass in chain tip to CreateBlockIndex
dongcarl Oct 14, 2020
b38c3c9
test: Pass in CoinsTip to ValidateCheckInputsForAllFlags
dongcarl Sep 9, 2020
3d54028
scripted-diff: test: Use existing chainman in unit tests
dongcarl Sep 9, 2020
c3efbcb
fuzz: Initialize a TestingSetup for test_one_input
dongcarl Oct 27, 2020
b24576e
scripted-diff: wallet/test: Use existing chainman
dongcarl Sep 18, 2020
947b3c5
qt/test: Use existing chainman in ::TestGUI (can be scripted-diff)
dongcarl Sep 18, 2020
a3d401c
miner: Remove stray review-only assertion
dongcarl Nov 4, 2020
9b3925b
scripted-diff: tree-wide: Remove all review-only assertions
dongcarl Oct 6, 2020
62893af
qt/test: Reset chainman in ~ChainstateManager instead
dongcarl Oct 6, 2020
6bda9eb
validation: Farewell, global Chainstate!
dongcarl Oct 6, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
net_processing: Move some static functions to PeerManager
- BlockRequestAllowed
- AlreadyHaveTx
- AlreadyHaveBlock
- ProcessGetBlockData
- ProcessGetData
- PrepareBlockFilterRequest
- ProcessGetCFilters
- ProcessGetCFHeaders
- ProcessGetCFCheckPt

Moved out of anonymous namespace:
- ProcessBlockAvailability
- UpdateBlockAvailability
- CanDirectFetch
- FindNextBlocksToDownload
  • Loading branch information
dongcarl committed Feb 2, 2021
commit 2ad7239a8438ee035e49f89b5e9f6ebd781582d4
127 changes: 74 additions & 53 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,27 @@ class PeerManagerImpl final : public PeerManager
* their own locks.
*/
std::map<NodeId, PeerRef> m_peer_map GUARDED_BY(m_peer_mutex);

void ProcessBlockAvailability(NodeId nodeid) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
bool CanDirectFetch(const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller, const Consensus::Params& consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
bool BlockRequestAllowed(const CBlockIndex* pindex, const Consensus::Params& consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
bool AlreadyHaveTx(const GenTxid& gtxid, const CTxMemPool& mempool) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
bool AlreadyHaveBlock(const uint256& block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
void ProcessGetBlockData(CNode& pfrom, Peer& peer, const CChainParams& chainparams, const CInv& inv, CConnman& connman);
void ProcessGetData(CNode& pfrom, Peer& peer, const CChainParams& chainparams, CConnman& connman, CTxMemPool& mempool, const std::atomic<bool>& interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(!cs_main, peer.m_getdata_requests_mutex);
bool PrepareBlockFilterRequest(CNode& peer, const CChainParams& chain_params,
BlockFilterType filter_type, uint32_t start_height,
const uint256& stop_hash, uint32_t max_height_diff,
const CBlockIndex*& stop_index,
BlockFilterIndex*& filter_index);
void ProcessGetCFilters(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
CConnman& connman);
void ProcessGetCFHeaders(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
CConnman& connman);
void ProcessGetCFCheckPt(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
CConnman& connman);
};
} // namespace

Expand Down Expand Up @@ -670,41 +691,6 @@ static bool MarkBlockAsInFlight(CTxMemPool& mempool, NodeId nodeid, const uint25
return true;
}

/** Check whether the last unknown block a peer advertised is not yet known. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In commit "net_processing: Move some static functions to PeerManager" (053c84c)

Might be nice to split this commit, and move the function bodies in a MOVEONLY commit. But this is maybe less important than it used to be now that git diff displays moved code better than it used to

static void ProcessBlockAvailability(NodeId nodeid) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
CNodeState *state = State(nodeid);
assert(state != nullptr);

if (!state->hashLastUnknownBlock.IsNull()) {
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(state->hashLastUnknownBlock);
if (pindex && pindex->nChainWork > 0) {
if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork) {
state->pindexBestKnownBlock = pindex;
}
state->hashLastUnknownBlock.SetNull();
}
}
}

/** Update tracking information about which blocks a peer is assumed to have. */
static void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
CNodeState *state = State(nodeid);
assert(state != nullptr);

ProcessBlockAvailability(nodeid);

const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(hash);
if (pindex && pindex->nChainWork > 0) {
// An actually better block was announced.
if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork) {
state->pindexBestKnownBlock = pindex;
}
} else {
// An unknown block was announced; just assume that the latest one is the best one.
state->hashLastUnknownBlock = hash;
}
}

/**
* When a peer sends us a valid block, instruct it to announce blocks to us
* using CMPCTBLOCK if possible by adding its nodeid to the end of
Expand Down Expand Up @@ -759,11 +745,6 @@ static bool TipMayBeStale(const Consensus::Params &consensusParams) EXCLUSIVE_LO
return g_last_tip_update < GetTime() - consensusParams.nPowTargetSpacing * 3 && mapBlocksInFlight.empty();
}

static bool CanDirectFetch(const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
return ::ChainActive().Tip()->GetBlockTime() > GetAdjustedTime() - consensusParams.nPowTargetSpacing * 20;
}

static bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
if (state->pindexBestKnownBlock && pindex == state->pindexBestKnownBlock->GetAncestor(pindex->nHeight))
Expand All @@ -773,9 +754,51 @@ static bool PeerHasHeader(CNodeState *state, const CBlockIndex *pindex) EXCLUSIV
return false;
}

} // namespace

/** Check whether the last unknown block a peer advertised is not yet known. */
void PeerManagerImpl::ProcessBlockAvailability(NodeId nodeid) {
CNodeState *state = State(nodeid);
assert(state != nullptr);

if (!state->hashLastUnknownBlock.IsNull()) {
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(state->hashLastUnknownBlock);
if (pindex && pindex->nChainWork > 0) {
if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork) {
state->pindexBestKnownBlock = pindex;
}
state->hashLastUnknownBlock.SetNull();
}
}
}

/** Update tracking information about which blocks a peer is assumed to have. */
void PeerManagerImpl::UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
CNodeState *state = State(nodeid);
assert(state != nullptr);

ProcessBlockAvailability(nodeid);

const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(hash);
if (pindex && pindex->nChainWork > 0) {
// An actually better block was announced.
if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork) {
state->pindexBestKnownBlock = pindex;
}
} else {
// An unknown block was announced; just assume that the latest one is the best one.
state->hashLastUnknownBlock = hash;
}
}

bool PeerManagerImpl::CanDirectFetch(const Consensus::Params &consensusParams)
{
return ::ChainActive().Tip()->GetBlockTime() > GetAdjustedTime() - consensusParams.nPowTargetSpacing * 20;
}

/** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
* at most count entries. */
static void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller, const Consensus::Params& consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void PeerManagerImpl::FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller, const Consensus::Params& consensusParams)
{
if (count == 0)
return;
Expand Down Expand Up @@ -862,9 +885,7 @@ static void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vec
}
}

} // namespace

void PeerManagerImpl::PushNodeVersion(CNode& pnode, int64_t nTime)
void PeerManagerImpl::PushNodeVersion(CNode& pnode, int64_t nTime)
{
// Note that pnode->GetLocalServices() is a reflection of the local
// services we were offering when the CNode object was created for this
Expand Down Expand Up @@ -1305,7 +1326,7 @@ bool PeerManagerImpl::MaybePunishNodeForTx(NodeId nodeid, const TxValidationStat
// active chain if they are no more than a month older (both in time, and in
// best equivalent proof of work) than the best header chain we know about and
// we fully-validated them at some point.
static bool BlockRequestAllowed(const CBlockIndex* pindex, const Consensus::Params& consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex* pindex, const Consensus::Params& consensusParams)
{
AssertLockHeld(cs_main);
if (::ChainActive().Contains(pindex)) return true;
Expand Down Expand Up @@ -1563,7 +1584,7 @@ void PeerManagerImpl::BlockChecked(const CBlock& block, const BlockValidationSta
//


bool static AlreadyHaveTx(const GenTxid& gtxid, const CTxMemPool& mempool) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool PeerManagerImpl::AlreadyHaveTx(const GenTxid& gtxid, const CTxMemPool& mempool)
{
assert(recentRejects);
if (::ChainActive().Tip()->GetBlockHash() != hashRecentRejectsChainTip) {
Expand Down Expand Up @@ -1594,7 +1615,7 @@ bool static AlreadyHaveTx(const GenTxid& gtxid, const CTxMemPool& mempool) EXCLU
return recentRejects->contains(hash) || mempool.exists(gtxid);
}

bool static AlreadyHaveBlock(const uint256& block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool PeerManagerImpl::AlreadyHaveBlock(const uint256& block_hash)
{
return g_chainman.m_blockman.LookupBlockIndex(block_hash) != nullptr;
}
Expand Down Expand Up @@ -1669,7 +1690,7 @@ static void RelayAddress(const CNode& originator,
connman.ForEachNodeThen(std::move(sortfunc), std::move(pushfunc));
}

void static ProcessGetBlockData(CNode& pfrom, Peer& peer, const CChainParams& chainparams, const CInv& inv, CConnman& connman)
void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CChainParams& chainparams, const CInv& inv, CConnman& connman)
{
bool send = false;
std::shared_ptr<const CBlock> a_recent_block;
Expand Down Expand Up @@ -1853,7 +1874,7 @@ static CTransactionRef FindTxForGetData(const CTxMemPool& mempool, const CNode&
return {};
}

void static ProcessGetData(CNode& pfrom, Peer& peer, const CChainParams& chainparams, CConnman& connman, CTxMemPool& mempool, const std::atomic<bool>& interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(!cs_main, peer.m_getdata_requests_mutex)
void PeerManagerImpl::ProcessGetData(CNode& pfrom, Peer& peer, const CChainParams& chainparams, CConnman& connman, CTxMemPool& mempool, const std::atomic<bool>& interruptMsgProc)
{
AssertLockNotHeld(cs_main);

Expand Down Expand Up @@ -2263,7 +2284,7 @@ void PeerManagerImpl::ProcessOrphanTx(std::set<uint256>& orphan_work_set)
* @param[out] filter_index The filter index, if the request can be serviced.
* @return True if the request can be serviced.
*/
static bool PrepareBlockFilterRequest(CNode& peer, const CChainParams& chain_params,
bool PeerManagerImpl::PrepareBlockFilterRequest(CNode& peer, const CChainParams& chain_params,
BlockFilterType filter_type, uint32_t start_height,
const uint256& stop_hash, uint32_t max_height_diff,
const CBlockIndex*& stop_index,
Expand Down Expand Up @@ -2326,7 +2347,7 @@ static bool PrepareBlockFilterRequest(CNode& peer, const CChainParams& chain_par
* @param[in] chain_params Chain parameters
* @param[in] connman Pointer to the connection manager
*/
static void ProcessGetCFilters(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
void PeerManagerImpl::ProcessGetCFilters(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
CConnman& connman)
{
uint8_t filter_type_ser;
Expand Down Expand Up @@ -2368,7 +2389,7 @@ static void ProcessGetCFilters(CNode& peer, CDataStream& vRecv, const CChainPara
* @param[in] chain_params Chain parameters
* @param[in] connman Pointer to the connection manager
*/
static void ProcessGetCFHeaders(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
void PeerManagerImpl::ProcessGetCFHeaders(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
CConnman& connman)
{
uint8_t filter_type_ser;
Expand Down Expand Up @@ -2423,7 +2444,7 @@ static void ProcessGetCFHeaders(CNode& peer, CDataStream& vRecv, const CChainPar
* @param[in] chain_params Chain parameters
* @param[in] connman Pointer to the connection manager
*/
static void ProcessGetCFCheckPt(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
void PeerManagerImpl::ProcessGetCFCheckPt(CNode& peer, CDataStream& vRecv, const CChainParams& chain_params,
CConnman& connman)
{
uint8_t filter_type_ser;
Expand Down