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
rpc/mining: Use existing NodeContext
Also pass in appropriate object to:
- GetNetworkHashPS
- [gG]enerateBlock{,s}
  • Loading branch information
dongcarl committed Feb 2, 2021
commit 2cb0446d10fcaa658cfaccb7bca0ea3420290750
65 changes: 37 additions & 28 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@
* or from the last difficulty change if 'lookup' is nonpositive.
* If 'height' is nonnegative, compute the estimate at the time when a given block was found.
*/
static UniValue GetNetworkHashPS(int lookup, int height) {
CBlockIndex *pb = ::ChainActive().Tip();
static UniValue GetNetworkHashPS(int lookup, int height, CChain& active_chain) {
CBlockIndex *pb = active_chain.Tip();

if (height >= 0 && height < ::ChainActive().Height())
pb = ::ChainActive()[height];
if (height >= 0 && height < active_chain.Height()) {
pb = active_chain[height];
}

if (pb == nullptr || !pb->nHeight)
return 0;
Expand Down Expand Up @@ -100,7 +101,8 @@ static RPCHelpMan getnetworkhashps()
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
LOCK(cs_main);
return GetNetworkHashPS(!request.params[0].isNull() ? request.params[0].get_int() : 120, !request.params[1].isNull() ? request.params[1].get_int() : -1);
CChain& active_chain = EnsureChainman(request.context).ActiveChain();
return GetNetworkHashPS(!request.params[0].isNull() ? request.params[0].get_int() : 120, !request.params[1].isNull() ? request.params[1].get_int() : -1, active_chain);
},
};
}
Expand All @@ -111,7 +113,8 @@ static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t&

{
LOCK(cs_main);
IncrementExtraNonce(&block, ::ChainActive().Tip(), extra_nonce);
assert(std::addressof(::ChainActive()) == std::addressof(chainman.ActiveChain()));
IncrementExtraNonce(&block, chainman.ActiveChain().Tip(), extra_nonce);
}

CChainParams chainparams(Params());
Expand Down Expand Up @@ -143,14 +146,15 @@ static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& me

{ // Don't keep cs_main locked
LOCK(cs_main);
nHeight = ::ChainActive().Height();
assert(std::addressof(::ChainActive()) == std::addressof(chainman.ActiveChain()));
nHeight = chainman.ActiveChain().Height();
nHeightEnd = nHeight+nGenerate;
}
unsigned int nExtraNonce = 0;
UniValue blockHashes(UniValue::VARR);
while (nHeight < nHeightEnd && !ShutdownRequested())
{
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(mempool, Params()).CreateNewBlock(::ChainstateActive(), coinbase_script));
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(mempool, Params()).CreateNewBlock(chainman.ActiveChainstate(), coinbase_script));
if (!pblocktemplate.get())
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
CBlock *pblock = &pblocktemplate->block;
Expand Down Expand Up @@ -354,11 +358,12 @@ static RPCHelpMan generateblock()
CChainParams chainparams(Params());
CBlock block;

ChainstateManager& chainman = EnsureChainman(request.context);
{
LOCK(cs_main);

CTxMemPool empty_mempool;
std::unique_ptr<CBlockTemplate> blocktemplate(BlockAssembler(empty_mempool, chainparams).CreateNewBlock(::ChainstateActive(), coinbase_script));
std::unique_ptr<CBlockTemplate> blocktemplate(BlockAssembler(empty_mempool, chainparams).CreateNewBlock(chainman.ActiveChainstate(), coinbase_script));
if (!blocktemplate) {
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
}
Expand All @@ -369,13 +374,13 @@ static RPCHelpMan generateblock()

// Add transactions
block.vtx.insert(block.vtx.end(), txs.begin(), txs.end());
RegenerateCommitments(block, WITH_LOCK(::cs_main, return std::ref(g_chainman.m_blockman)));
RegenerateCommitments(block, WITH_LOCK(::cs_main, return std::ref(chainman.m_blockman)));

{
LOCK(cs_main);

BlockValidationState state;
if (!TestBlockValidity(state, chainparams, ::ChainstateActive(), block, g_chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock), false, false)) {
if (!TestBlockValidity(state, chainparams, chainman.ActiveChainstate(), block, chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock), false, false)) {
throw JSONRPCError(RPC_VERIFY_ERROR, strprintf("TestBlockValidity failed: %s", state.ToString()));
}
}
Expand All @@ -384,7 +389,7 @@ static RPCHelpMan generateblock()
uint64_t max_tries{DEFAULT_MAX_TRIES};
unsigned int extra_nonce{0};

if (!GenerateBlock(EnsureChainman(request.context), block, max_tries, extra_nonce, block_hash) || block_hash.IsNull()) {
if (!GenerateBlock(chainman, block, max_tries, extra_nonce, block_hash) || block_hash.IsNull()) {
throw JSONRPCError(RPC_MISC_ERROR, "Failed to make block.");
}

Expand Down Expand Up @@ -420,12 +425,13 @@ static RPCHelpMan getmininginfo()
{
LOCK(cs_main);
const CTxMemPool& mempool = EnsureMemPool(request.context);
CChain& active_chain = EnsureChainman(request.context).ActiveChain();

UniValue obj(UniValue::VOBJ);
obj.pushKV("blocks", (int)::ChainActive().Height());
obj.pushKV("blocks", (int)active_chain.Height());
if (BlockAssembler::m_last_block_weight) obj.pushKV("currentblockweight", *BlockAssembler::m_last_block_weight);
if (BlockAssembler::m_last_block_num_txs) obj.pushKV("currentblocktx", *BlockAssembler::m_last_block_num_txs);
obj.pushKV("difficulty", (double)GetDifficulty(::ChainActive().Tip()));
obj.pushKV("difficulty", (double)GetDifficulty(active_chain.Tip()));
obj.pushKV("networkhashps", getnetworkhashps().HandleRequest(request));
obj.pushKV("pooledtx", (uint64_t)mempool.size());
obj.pushKV("chain", Params().NetworkIDString());
Expand Down Expand Up @@ -588,6 +594,7 @@ static RPCHelpMan getblocktemplate()
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
LOCK(cs_main);
ChainstateManager& chainman = EnsureChainman(request.context);

std::string strMode = "template";
UniValue lpval = NullUniValue;
Expand Down Expand Up @@ -618,7 +625,7 @@ static RPCHelpMan getblocktemplate()
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");

uint256 hash = block.GetHash();
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(hash);
const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
if (pindex) {
if (pindex->IsValid(BLOCK_VALID_SCRIPTS))
return "duplicate";
Expand All @@ -627,12 +634,12 @@ static RPCHelpMan getblocktemplate()
return "duplicate-inconclusive";
}

CBlockIndex* const pindexPrev = ::ChainActive().Tip();
CBlockIndex* const pindexPrev = chainman.ActiveChain().Tip();
// TestBlockValidity only supports blocks built on the current Tip
if (block.hashPrevBlock != pindexPrev->GetBlockHash())
return "inconclusive-not-best-prevblk";
BlockValidationState state;
TestBlockValidity(state, Params(), ::ChainstateActive(), block, pindexPrev, false, true);
TestBlockValidity(state, Params(), chainman.ActiveChainstate(), block, pindexPrev, false, true);
return BIP22ValidationResult(state);
}

Expand Down Expand Up @@ -663,7 +670,7 @@ static RPCHelpMan getblocktemplate()
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, PACKAGE_NAME " is not connected!");
}

if (::ChainstateActive().IsInitialBlockDownload()) {
if (chainman.IsInitialBlockDownload()) {
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, PACKAGE_NAME " is in initial sync and waiting for blocks...");
}
}
Expand All @@ -689,7 +696,7 @@ static RPCHelpMan getblocktemplate()
else
{
// NOTE: Spec does not specify behaviour for non-string longpollid, but this makes testing easier
hashWatchedChain = ::ChainActive().Tip()->GetBlockHash();
hashWatchedChain = chainman.ActiveChain().Tip()->GetBlockHash();
nTransactionsUpdatedLastLP = nTransactionsUpdatedLast;
}

Expand Down Expand Up @@ -734,20 +741,20 @@ static RPCHelpMan getblocktemplate()
static CBlockIndex* pindexPrev;
static int64_t nStart;
static std::unique_ptr<CBlockTemplate> pblocktemplate;
if (pindexPrev != ::ChainActive().Tip() ||
if (pindexPrev != chainman.ActiveChain().Tip() ||
(mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 5))
{
// Clear pindexPrev so future calls make a new block, despite any failures from here on
pindexPrev = nullptr;

// Store the pindexBest used before CreateNewBlock, to avoid races
nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
CBlockIndex* pindexPrevNew = ::ChainActive().Tip();
CBlockIndex* pindexPrevNew = chainman.ActiveChain().Tip();
nStart = GetTime();

// Create new block
CScript scriptDummy = CScript() << OP_TRUE;
pblocktemplate = BlockAssembler(mempool, Params()).CreateNewBlock(::ChainstateActive(), scriptDummy);
pblocktemplate = BlockAssembler(mempool, Params()).CreateNewBlock(chainman.ActiveChainstate(), scriptDummy);
if (!pblocktemplate)
throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");

Expand Down Expand Up @@ -883,7 +890,7 @@ static RPCHelpMan getblocktemplate()
result.pushKV("transactions", transactions);
result.pushKV("coinbaseaux", aux);
result.pushKV("coinbasevalue", (int64_t)pblock->vtx[0]->vout[0].nValue);
result.pushKV("longpollid", ::ChainActive().Tip()->GetBlockHash().GetHex() + ToString(nTransactionsUpdatedLast));
result.pushKV("longpollid", chainman.ActiveChain().Tip()->GetBlockHash().GetHex() + ToString(nTransactionsUpdatedLast));
result.pushKV("target", hashTarget.GetHex());
result.pushKV("mintime", (int64_t)pindexPrev->GetMedianTimePast()+1);
result.pushKV("mutable", aMutable);
Expand Down Expand Up @@ -963,10 +970,11 @@ static RPCHelpMan submitblock()
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block does not start with a coinbase");
}

ChainstateManager& chainman = EnsureChainman(request.context);
uint256 hash = block.GetHash();
{
LOCK(cs_main);
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(hash);
const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
if (pindex) {
if (pindex->IsValid(BLOCK_VALID_SCRIPTS)) {
return "duplicate";
Expand All @@ -979,7 +987,7 @@ static RPCHelpMan submitblock()

{
LOCK(cs_main);
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock);
const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock);
if (pindex) {
UpdateUncommittedBlockStructures(block, pindex, Params().GetConsensus());
}
Expand All @@ -988,7 +996,7 @@ static RPCHelpMan submitblock()
bool new_block;
auto sc = std::make_shared<submitblock_StateCatcher>(block.GetHash());
RegisterSharedValidationInterface(sc);
bool accepted = EnsureChainman(request.context).ProcessNewBlock(Params(), blockptr, /* fForceProcessing */ true, /* fNewBlock */ &new_block);
bool accepted = chainman.ProcessNewBlock(Params(), blockptr, /* fForceProcessing */ true, /* fNewBlock */ &new_block);
UnregisterSharedValidationInterface(sc);
if (!new_block && accepted) {
return "duplicate";
Expand Down Expand Up @@ -1021,15 +1029,16 @@ static RPCHelpMan submitheader()
if (!DecodeHexBlockHeader(h, request.params[0].get_str())) {
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block header decode failed");
}
ChainstateManager& chainman = EnsureChainman(request.context);
{
LOCK(cs_main);
if (!g_chainman.m_blockman.LookupBlockIndex(h.hashPrevBlock)) {
if (!chainman.m_blockman.LookupBlockIndex(h.hashPrevBlock)) {
throw JSONRPCError(RPC_VERIFY_ERROR, "Must submit previous header (" + h.hashPrevBlock.GetHex() + ") first");
}
}

BlockValidationState state;
EnsureChainman(request.context).ProcessNewBlockHeaders({h}, state, Params());
chainman.ProcessNewBlockHeaders({h}, state, Params());
if (state.IsValid()) return NullUniValue;
if (state.IsError()) {
throw JSONRPCError(RPC_VERIFY_ERROR, state.ToString());
Expand Down