Skip to content

Commit

Permalink
Merge pull request #5235 from kittywhiskers/chainman_prune
Browse files Browse the repository at this point in the history
  • Loading branch information
PastaPastaPasta authored Apr 4, 2023
2 parents 15a9783 + 24a5552 commit 389f959
Show file tree
Hide file tree
Showing 74 changed files with 2,091 additions and 2,096 deletions.
2 changes: 1 addition & 1 deletion src/bench/block_assemble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static void AssembleBlock(benchmark::Bench& bench)

for (const auto& txr : txs) {
CValidationState state;
bool ret{::AcceptToMemoryPool(*test_setup.m_node.mempool, state, txr, nullptr /* pfMissingInputs */, false /* bypass_limits */, /* nAbsurdFee */ 0)};
bool ret{::AcceptToMemoryPool(::ChainstateActive(), *test_setup.m_node.mempool, state, txr, nullptr /* pfMissingInputs */, false /* bypass_limits */, /* nAbsurdFee */ 0)};
assert(ret);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/coinjoin/coinjoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ bool CCoinJoin::IsCollateralValid(CTxMemPool& mempool, const CTransaction& txCol
{
LOCK(cs_main);
CValidationState validationState;
if (!AcceptToMemoryPool(mempool, validationState, MakeTransactionRef(txCollateral), /*pfMissingInputs=*/nullptr, /*bypass_limits=*/false, /*nAbsurdFee=*/DEFAULT_MAX_RAW_TX_FEE, /*test_accept=*/true)) {
if (!AcceptToMemoryPool(::ChainstateActive(), mempool, validationState, MakeTransactionRef(txCollateral), /*pfMissingInputs=*/nullptr, /*bypass_limits=*/false, /*nAbsurdFee=*/DEFAULT_MAX_RAW_TX_FEE, /*test_accept=*/true)) {
LogPrint(BCLog::COINJOIN, "CCoinJoin::IsCollateralValid -- didn't pass AcceptToMemoryPool()\n");
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/coinjoin/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ void CCoinJoinServer::CommitFinalTransaction()
TRY_LOCK(cs_main, lockMain);
CValidationState validationState;
mempool.PrioritiseTransaction(hashTx, 0.1 * COIN);
if (!lockMain || !AcceptToMemoryPool(mempool, validationState, finalTransaction, nullptr /* pfMissingInputs */, false /* bypass_limits */, DEFAULT_MAX_RAW_TX_FEE /* nAbsurdFee */)) {
if (!lockMain || !AcceptToMemoryPool(::ChainstateActive(), mempool, validationState, finalTransaction, nullptr /* pfMissingInputs */, false /* bypass_limits */, DEFAULT_MAX_RAW_TX_FEE /* nAbsurdFee */)) {
LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CommitFinalTransaction -- AcceptToMemoryPool() error: Transaction not valid\n");
WITH_LOCK(cs_coinjoin, SetNull());
// not much we can do in this case, just notify clients
Expand Down Expand Up @@ -455,7 +455,7 @@ void CCoinJoinServer::ConsumeCollateral(const CTransactionRef& txref) const
{
LOCK(cs_main);
CValidationState validationState;
if (!AcceptToMemoryPool(mempool, validationState, txref, nullptr /* pfMissingInputs */, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
if (!AcceptToMemoryPool(::ChainstateActive(), mempool, validationState, txref, nullptr /* pfMissingInputs */, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
LogPrint(BCLog::COINJOIN, "%s -- AcceptToMemoryPool failed\n", __func__);
} else {
connman.RelayTransaction(*txref);
Expand Down
2 changes: 1 addition & 1 deletion src/evo/mnhftx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ bool CheckMNHFTx(const CTransaction& tx, const CBlockIndex* pindexPrev, CValidat
return state.Invalid(ValidationInvalidReason::CONSENSUS, false, REJECT_INVALID, "bad-mnhf-version");
}

const CBlockIndex* pindexQuorum = LookupBlockIndex(mnhfTx.signal.quorumHash);
const CBlockIndex* pindexQuorum = g_chainman.m_blockman.LookupBlockIndex(mnhfTx.signal.quorumHash);
if (!pindexQuorum) {
return state.Invalid(ValidationInvalidReason::CONSENSUS, false, REJECT_INVALID, "bad-mnhf-quorum-hash");
}
Expand Down
4 changes: 2 additions & 2 deletions src/evo/simplifiedmns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,14 @@ bool BuildSimplifiedMNListDiff(const uint256& baseBlockHash, const uint256& bloc

const CBlockIndex* baseBlockIndex = ::ChainActive().Genesis();
if (!baseBlockHash.IsNull()) {
baseBlockIndex = LookupBlockIndex(baseBlockHash);
baseBlockIndex = g_chainman.m_blockman.LookupBlockIndex(baseBlockHash);
if (!baseBlockIndex) {
errorRet = strprintf("block %s not found", baseBlockHash.ToString());
return false;
}
}

const CBlockIndex* blockIndex = LookupBlockIndex(blockHash);
const CBlockIndex* blockIndex = g_chainman.m_blockman.LookupBlockIndex(blockHash);
if (!blockIndex) {
errorRet = strprintf("block %s not found", blockHash.ToString());
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/evo/specialtxman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ bool CheckSpecialTx(const CTransaction& tx, const CBlockIndex* pindexPrev, CVali
case TRANSACTION_QUORUM_COMMITMENT:
return llmq::CheckLLMQCommitment(tx, pindexPrev, state);
case TRANSACTION_MNHF_SIGNAL:
return VersionBitsTipState(Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0024) == ThresholdState::ACTIVE && CheckMNHFTx(tx, pindexPrev, state);
return VersionBitsState(::ChainActive().Tip(), Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0024, versionbitscache) == ThresholdState::ACTIVE && CheckMNHFTx(tx, pindexPrev, state);
}
} catch (const std::exception& e) {
LogPrintf("%s -- failed: %s\n", __func__, e.what());
Expand Down
2 changes: 1 addition & 1 deletion src/governance/classes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ void CSuperblock::ParsePaymentSchedule(const std::string& strPaymentAddresses, c

// TODO: script addresses limit here and cs_main lock in
// CGovernanceManager::InitOnLoad()once DIP0024 is active
bool fAllowScript = (VersionBitsTipState(Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0024) == ThresholdState::ACTIVE);
bool fAllowScript = (VersionBitsState(::ChainActive().Tip(), Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0024, versionbitscache) == ThresholdState::ACTIVE);
for (int i = 0; i < (int)vecParsed1.size(); i++) {
CTxDestination dest = DecodeDestination(vecParsed1[i]);
if (!IsValidDestination(dest)) {
Expand Down
4 changes: 2 additions & 2 deletions src/governance/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ void CGovernanceManager::UpdateCachesAndClean()
} else {
// NOTE: triggers are handled via triggerman
if (pObj->GetObjectType() == GOVERNANCE_OBJECT_PROPOSAL) {
bool fAllowScript = (VersionBitsTipState(Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0024) == ThresholdState::ACTIVE);
bool fAllowScript = (VersionBitsState(::ChainActive().Tip(), Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0024, versionbitscache) == ThresholdState::ACTIVE);
CProposalValidator validator(pObj->GetDataAsHexString(), fAllowScript);
if (!validator.Validate()) {
LogPrint(BCLog::GOBJECT, "CGovernanceManager::UpdateCachesAndClean -- set for deletion expired obj %s\n", strHash);
Expand Down Expand Up @@ -635,7 +635,7 @@ void CGovernanceManager::SyncObjects(CNode& peer, CConnman& connman) const

LogPrint(BCLog::GOBJECT, "CGovernanceManager::%s -- syncing all objects to peer=%d\n", __func__, peer.GetId());

bool fAllowScript = WITH_LOCK(cs_main, return VersionBitsTipState(Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0024) == ThresholdState::ACTIVE);
bool fAllowScript = WITH_LOCK(cs_main, return VersionBitsState(::ChainActive().Tip(), Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0024, versionbitscache) == ThresholdState::ACTIVE);

LOCK(cs);

Expand Down
8 changes: 4 additions & 4 deletions src/governance/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ bool CGovernanceObject::IsValidLocally(std::string& strError, bool& fMissingConf

switch (nObjectType) {
case GOVERNANCE_OBJECT_PROPOSAL: {
bool fAllowScript = (VersionBitsTipState(Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0024) == ThresholdState::ACTIVE);
bool fAllowScript = (VersionBitsState(::ChainActive().Tip(), Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0024, versionbitscache) == ThresholdState::ACTIVE);
CProposalValidator validator(GetDataAsHexString(), fAllowScript);
// Note: It's ok to have expired proposals
// they are going to be cleared by CGovernanceManager::UpdateCachesAndClean()
Expand Down Expand Up @@ -563,7 +563,7 @@ bool CGovernanceObject::IsCollateralValid(std::string& strError, bool& fMissingC
findScript << OP_RETURN << ToByteVector(nExpectedHash);

AssertLockHeld(cs_main);
bool fork_active = VersionBitsState(LookupBlockIndex(nBlockHash), Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0024, versionbitscache) == ThresholdState::ACTIVE;
bool fork_active = VersionBitsState(g_chainman.m_blockman.LookupBlockIndex(nBlockHash), Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0024, versionbitscache) == ThresholdState::ACTIVE;
CAmount nMinFee = GetMinCollateralFee(fork_active);

LogPrint(BCLog::GOBJECT, "CGovernanceObject::IsCollateralValid -- txCollateral->vout.size() = %s, findScript = %s, nMinFee = %lld\n",
Expand Down Expand Up @@ -594,7 +594,7 @@ bool CGovernanceObject::IsCollateralValid(std::string& strError, bool& fMissingC
AssertLockHeld(cs_main);
int nConfirmationsIn = 0;
if (nBlockHash != uint256()) {
const CBlockIndex* pindex = LookupBlockIndex(nBlockHash);
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(nBlockHash);
if (pindex && ::ChainActive().Contains(pindex)) {
nConfirmationsIn += ::ChainActive().Height() - pindex->nHeight + 1;
}
Expand Down Expand Up @@ -692,7 +692,7 @@ void CGovernanceObject::Relay(CConnman& connman) const
// But we don't want to relay it to pre-GOVSCRIPT_PROTO_VERSION peers if payment_address is p2sh
// because they won't accept it anyway and will simply ban us eventually.
LOCK(cs_main);
bool fAllowScript = (VersionBitsTipState(Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0024) == ThresholdState::ACTIVE);
bool fAllowScript = (VersionBitsState(::ChainActive().Tip(), Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0024, versionbitscache) == ThresholdState::ACTIVE);
if (fAllowScript) {
CProposalValidator validator(GetDataAsHexString(), false /* no script */);
if (!validator.Validate(false /* ignore expiration */)) {
Expand Down
4 changes: 2 additions & 2 deletions src/index/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ bool BaseIndex::Init()
if (locator.IsNull()) {
m_best_block_index = nullptr;
} else {
m_best_block_index = FindForkInGlobalIndex(::ChainActive(), locator);
m_best_block_index = g_chainman.m_blockman.FindForkInGlobalIndex(::ChainActive(), locator);
}
m_synced = m_best_block_index.load() == ::ChainActive().Tip();
return true;
Expand Down Expand Up @@ -238,7 +238,7 @@ void BaseIndex::ChainStateFlushed(const CBlockLocator& locator)
const CBlockIndex* locator_tip_index;
{
LOCK(cs_main);
locator_tip_index = LookupBlockIndex(locator_tip_hash);
locator_tip_index = g_chainman.m_blockman.LookupBlockIndex(locator_tip_hash);
}

if (!locator_tip_index) {
Expand Down
18 changes: 9 additions & 9 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ static void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImp
if (!file)
break; // This error is logged in OpenBlockFile
LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile);
LoadExternalBlockFile(chainparams, file, &pos);
::ChainstateActive().LoadExternalBlockFile(chainparams, file, &pos);
if (ShutdownRequested()) {
LogPrintf("Shutdown requested. Exit %s\n", __func__);
return;
Expand All @@ -904,15 +904,15 @@ static void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImp
fReindex = false;
LogPrintf("Reindexing finished\n");
// To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
LoadGenesisBlock(chainparams);
::ChainstateActive().LoadGenesisBlock(chainparams);
}

// -loadblock=
for (const fs::path& path : vImportFiles) {
FILE *file = fsbridge::fopen(path, "rb");
if (file) {
LogPrintf("Importing blocks file %s...\n", path.string());
LoadExternalBlockFile(chainparams, file);
::ChainstateActive().LoadExternalBlockFile(chainparams, file);
if (ShutdownRequested()) {
LogPrintf("Shutdown requested. Exit %s\n", __func__);
return;
Expand Down Expand Up @@ -2015,7 +2015,7 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
chainman.m_total_coinstip_cache = nCoinCacheUsage;
chainman.m_total_coinsdb_cache = nCoinDBCache;

UnloadBlockIndex(node.mempool.get());
UnloadBlockIndex(node.mempool.get(), chainman);

// new CBlockTreeDB tries to delete the existing file, which
// fails if it's still open from the previous loop. Close it first:
Expand Down Expand Up @@ -2056,12 +2056,12 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
// If the loaded chain has a wrong genesis, bail out immediately
// (we're likely using a testnet datadir, or the other way around).
if (!chainman.BlockIndex().empty() &&
!LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) {
!g_chainman.m_blockman.LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) {
return InitError(_("Incorrect or no genesis block found. Wrong datadir for network?"));
}

if (!chainparams.GetConsensus().hashDevnetGenesisBlock.IsNull() && !chainman.BlockIndex().empty() &&
!LookupBlockIndex(chainparams.GetConsensus().hashDevnetGenesisBlock)) {
!g_chainman.m_blockman.LookupBlockIndex(chainparams.GetConsensus().hashDevnetGenesisBlock)) {
return InitError(_("Incorrect or no devnet genesis block found. Wrong datadir for devnet specified?"));
}

Expand Down Expand Up @@ -2094,7 +2094,7 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
// If we're not mid-reindex (based on disk + args), add a genesis block on disk
// (otherwise we use the one already on disk).
// This is called again in ThreadImport after the reindex completes.
if (!fReindex && !LoadGenesisBlock(chainparams)) {
if (!fReindex && !::ChainstateActive().LoadGenesisBlock(chainparams)) {
strLoadError = _("Error initializing block database");
break;
}
Expand Down Expand Up @@ -2194,7 +2194,7 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
// work when we allow VerifyDB to be parameterized by chainstate.
if (&::ChainstateActive() == chainstate &&
!CVerifyDB().VerifyDB(
chainparams, &chainstate->CoinsDB(),
chainparams, *chainstate, &chainstate->CoinsDB(),
*node.evodb,
args.GetArg("-checklevel", DEFAULT_CHECKLEVEL),
args.GetArg("-checkblocks", DEFAULT_CHECKBLOCKS))) {
Expand All @@ -2215,7 +2215,7 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
}

if (args.GetArg("-checklevel", DEFAULT_CHECKLEVEL) >= 3) {
ResetBlockFailureFlags(nullptr);
::ChainstateActive().ResetBlockFailureFlags(nullptr);
}
}
} catch (const std::exception& e) {
Expand Down
12 changes: 6 additions & 6 deletions src/interfaces/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class ChainImpl : public Chain
std::optional<int> getBlockHeight(const uint256& hash) override
{
LOCK(::cs_main);
CBlockIndex* block = LookupBlockIndex(hash);
CBlockIndex* block = g_chainman.m_blockman.LookupBlockIndex(hash);
if (block && ::ChainActive().Contains(block)) {
return block->nHeight;
}
Expand Down Expand Up @@ -209,7 +209,7 @@ class ChainImpl : public Chain
std::optional<int> findFork(const uint256& hash, std::optional<int>* height) override
{
LOCK(cs_main);
const CBlockIndex* block = LookupBlockIndex(hash);
const CBlockIndex* block = g_chainman.m_blockman.LookupBlockIndex(hash);
const CBlockIndex* fork = block ? ::ChainActive().FindFork(block) : nullptr;
if (height) {
if (block) {
Expand All @@ -231,22 +231,22 @@ class ChainImpl : public Chain
std::optional<int> findLocatorFork(const CBlockLocator& locator) override
{
LOCK(cs_main);
if (CBlockIndex* fork = FindForkInGlobalIndex(::ChainActive(), locator)) {
if (CBlockIndex* fork = g_chainman.m_blockman.FindForkInGlobalIndex(::ChainActive(), locator)) {
return fork->nHeight;
}
return std::nullopt;
}
bool checkFinalTx(const CTransaction& tx) override
{
LOCK(cs_main);
return CheckFinalTx(tx);
return CheckFinalTx(::ChainActive().Tip(), tx);
}
bool findBlock(const uint256& hash, CBlock* block, int64_t* time, int64_t* time_max) override
{
CBlockIndex* index;
{
LOCK(cs_main);
index = LookupBlockIndex(hash);
index = g_chainman.m_blockman.LookupBlockIndex(hash);
if (!index) {
return false;
}
Expand All @@ -266,7 +266,7 @@ class ChainImpl : public Chain
double guessVerificationProgress(const uint256& block_hash) override
{
LOCK(cs_main);
return GuessVerificationProgress(Params().TxData(), LookupBlockIndex(block_hash));
return GuessVerificationProgress(Params().TxData(), g_chainman.m_blockman.LookupBlockIndex(block_hash));
}
bool hasDescendantsInMempool(const uint256& txid) override
{
Expand Down
6 changes: 3 additions & 3 deletions src/llmq/blockprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void CQuorumBlockProcessor::ProcessMessage(const CNode& peer, std::string_view m
const CBlockIndex* pQuorumBaseBlockIndex;
{
LOCK(cs_main);
pQuorumBaseBlockIndex = LookupBlockIndex(qc.quorumHash);
pQuorumBaseBlockIndex = g_chainman.m_blockman.LookupBlockIndex(qc.quorumHash);
if (pQuorumBaseBlockIndex == nullptr) {
LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s -- unknown block %s in commitment, peer=%d\n", __func__,
qc.quorumHash.ToString(), peer.GetId());
Expand Down Expand Up @@ -261,7 +261,7 @@ bool CQuorumBlockProcessor::ProcessCommitment(int nHeight, const uint256& blockH
return state.Invalid(ValidationInvalidReason::CONSENSUS, false, REJECT_INVALID, "bad-qc-height");
}

const auto* pQuorumBaseBlockIndex = LookupBlockIndex(qc.quorumHash);
const auto* pQuorumBaseBlockIndex = g_chainman.m_blockman.LookupBlockIndex(qc.quorumHash);

if (!qc.Verify(pQuorumBaseBlockIndex, fBLSChecks)) {
LogPrint(BCLog::LLMQ, "CQuorumBlockProcessor::%s height=%d, type=%d, quorumIndex=%d, quorumHash=%s, signers=%s, validMembers=%d, quorumPublicKey=%s qc verify failed.\n", __func__,
Expand Down Expand Up @@ -386,7 +386,7 @@ bool CQuorumBlockProcessor::UpgradeDB()
if (qc.IsNull()) {
continue;
}
const auto* pQuorumBaseBlockIndex = LookupBlockIndex(qc.quorumHash);
const auto* pQuorumBaseBlockIndex = g_chainman.m_blockman.LookupBlockIndex(qc.quorumHash);
m_evoDb.GetRawDB().Write(std::make_pair(DB_MINED_COMMITMENT, std::make_pair(qc.llmqType, qc.quorumHash)), std::make_pair(qc, pindex->GetBlockHash()));
const auto& llmq_params_opt = GetLLMQParams(qc.llmqType);
assert(llmq_params_opt.has_value());
Expand Down
Loading

0 comments on commit 389f959

Please sign in to comment.