diff --git a/src/index/base.cpp b/src/index/base.cpp --- a/src/index/base.cpp +++ b/src/index/base.cpp @@ -3,6 +3,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include +#include #include #include #include @@ -83,7 +84,7 @@ void BaseIndex::ThreadSync() { const CBlockIndex *pindex = m_best_block_index.load(); if (!m_synced) { - auto &config = GetConfig(); + auto &consensus_params = GetConfig().GetChainParams().GetConsensus(); int64_t last_log_time = 0; int64_t last_locator_write_time = 0; @@ -119,7 +120,7 @@ } CBlock block; - if (!ReadBlockFromDisk(block, pindex, config)) { + if (!ReadBlockFromDisk(block, pindex, consensus_params)) { FatalError("%s: Failed to read block %s from disk", __func__, pindex->GetBlockHash().ToString()); return; diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -2207,7 +2207,8 @@ // ReplayBlocks is a no-op if we cleared the coinsviewdb with // -reindex or -reindex-chainstate - if (!ReplayBlocks(config, pcoinsdbview.get())) { + if (!ReplayBlocks(chainparams.GetConsensus(), + pcoinsdbview.get())) { strLoadError = _("Unable to replay blocks. You will need to rebuild " "the database using -reindex-chainstate."); diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1249,7 +1249,7 @@ connman->ForEachNodeThen(std::move(sortfunc), std::move(pushfunc)); } -void static ProcessGetBlockData(const Config &config, CNode *pfrom, +static void ProcessGetBlockData(const Config &config, CNode *pfrom, const CInv &inv, CConnman *connman, const std::atomic &interruptMsgProc) { const Consensus::Params &consensusParams = @@ -1347,7 +1347,7 @@ } else { // Send block from disk std::shared_ptr pblockRead = std::make_shared(); - if (!ReadBlockFromDisk(*pblockRead, pindex, config)) { + if (!ReadBlockFromDisk(*pblockRead, pindex, consensusParams)) { assert(!"cannot load block from disk"); } pblock = pblockRead; @@ -2392,7 +2392,7 @@ } CBlock block; - bool ret = ReadBlockFromDisk(block, pindex, config); + bool ret = ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()); assert(ret); SendBlockTransactions(block, req, pfrom, connman); @@ -3966,7 +3966,8 @@ } if (!fGotBlockFromCache) { CBlock block; - bool ret = ReadBlockFromDisk(block, pBestIndex, config); + bool ret = + ReadBlockFromDisk(block, pBestIndex, consensusParams); assert(ret); CBlockHeaderAndShortTxIDs cmpctblock(block); connman->PushMessage( diff --git a/src/rest.cpp b/src/rest.cpp --- a/src/rest.cpp +++ b/src/rest.cpp @@ -241,7 +241,8 @@ hashStr + " not available (pruned data)"); } - if (!ReadBlockFromDisk(block, pblockindex, config)) { + if (!ReadBlockFromDisk(block, pblockindex, + config.GetChainParams().GetConsensus())) { return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found"); } } @@ -390,7 +391,8 @@ CTransactionRef tx; uint256 hashBlock = uint256(); - if (!GetTransaction(config, txid, tx, hashBlock, true)) { + if (!GetTransaction(config.GetChainParams().GetConsensus(), txid, tx, + hashBlock, true)) { return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found"); } diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -826,7 +826,8 @@ throw JSONRPCError(RPC_MISC_ERROR, "Block not available (pruned data)"); } - if (!ReadBlockFromDisk(block, pblockindex, config)) { + if (!ReadBlockFromDisk(block, pblockindex, + config.GetChainParams().GetConsensus())) { // Block not found on disk. This could be because we have the block // header in our index but don't have the block (for example if a // non-whitelisted node sends us an unrequested long chain of valid @@ -2023,6 +2024,8 @@ std::vector feerate_array; std::vector txsize_array; + const Consensus::Params ¶ms = config.GetChainParams().GetConsensus(); + for (const auto &tx : block.vtx) { outputs += tx->vout.size(); Amount tx_total_out = Amount::zero(); @@ -2057,17 +2060,17 @@ } if (loop_inputs) { - if (!g_txindex) { throw JSONRPCError(RPC_INVALID_PARAMETER, "One or more of the selected stats requires " "-txindex enabled"); } + Amount tx_total_in = Amount::zero(); for (const CTxIn &in : tx->vin) { CTransactionRef tx_in; uint256 hashBlock; - if (!GetTransaction(config, in.prevout.GetTxId(), tx_in, + if (!GetTransaction(params, in.prevout.GetTxId(), tx_in, hashBlock, false)) { throw JSONRPCError(RPC_INTERNAL_ERROR, std::string("Unexpected internal error " diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -169,7 +169,8 @@ TxId txid = TxId(ParseHashV(request.params[0], "parameter 1")); CBlockIndex *blockindex = nullptr; - if (txid == config.GetChainParams().GenesisBlock().hashMerkleRoot) { + const CChainParams ¶ms = config.GetChainParams(); + if (txid == params.GenesisBlock().hashMerkleRoot) { // Special exception for the genesis block coinbase transaction throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "The genesis block coinbase is not considered an " @@ -203,7 +204,8 @@ CTransactionRef tx; uint256 hash_block; - if (!GetTransaction(config, txid, tx, hash_block, true, blockindex)) { + if (!GetTransaction(params.GetConsensus(), txid, tx, hash_block, true, + blockindex)) { std::string errmsg; if (blockindex) { if (!blockindex->nStatus.hasData()) { @@ -316,11 +318,13 @@ g_txindex->BlockUntilSyncedToCurrentChain(); } + const Consensus::Params ¶ms = config.GetChainParams().GetConsensus(); + LOCK(cs_main); if (pblockindex == nullptr) { CTransactionRef tx; - if (!GetTransaction(config, oneTxId, tx, hashBlock, false) || + if (!GetTransaction(params, oneTxId, tx, hashBlock, false) || hashBlock.IsNull()) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not yet in block"); @@ -333,7 +337,7 @@ } CBlock block; - if (!ReadBlockFromDisk(block, pblockindex, config)) { + if (!ReadBlockFromDisk(block, pblockindex, params)) { throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk"); } diff --git a/src/validation.h b/src/validation.h --- a/src/validation.h +++ b/src/validation.h @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include // For CMessageHeader::MessageMagic @@ -53,6 +52,10 @@ struct PrecomputedTransactionData; struct LockPoints; +namespace Consensus { +struct Params; +} + #define MIN_TRANSACTION_SIZE \ (::GetSerializeSize(CTransaction(), SER_NETWORK, PROTOCOL_VERSION)) @@ -399,9 +402,9 @@ /** * Retrieve a transaction (from memory pool, or from disk, if possible). */ -bool GetTransaction(const Config &config, const TxId &txid, CTransactionRef &tx, - uint256 &hashBlock, bool fAllowSlow = false, - CBlockIndex *blockIndex = nullptr); +bool GetTransaction(const Consensus::Params ¶ms, const TxId &txid, + CTransactionRef &tx, uint256 &hashBlock, + bool fAllowSlow = false, CBlockIndex *blockIndex = nullptr); /** * Find the best known block, and make it the active tip of the block chain. @@ -558,9 +561,9 @@ /** Functions for disk access for blocks */ bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, - const Config &config); + const Consensus::Params ¶ms); bool ReadBlockFromDisk(CBlock &block, const CBlockIndex *pindex, - const Config &config); + const Consensus::Params ¶ms); /** Functions for validating blocks and updating the block tree */ @@ -613,7 +616,7 @@ }; /** Replay blocks that aren't fully applied to the database. */ -bool ReplayBlocks(const Config &config, CCoinsView *view); +bool ReplayBlocks(const Consensus::Params ¶ms, CCoinsView *view); /** Find the last common block between the parameter chain and a locator. */ CBlockIndex *FindForkInGlobalIndex(const CChain &chain, diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -171,7 +171,7 @@ /** Remove parked status from a block and its descendants. */ bool UnparkBlockImpl(CBlockIndex *pindex, bool fClearChildren); - bool ReplayBlocks(const Config &config, CCoinsView *view); + bool ReplayBlocks(const Consensus::Params ¶ms, CCoinsView *view); bool RewindBlockIndex(const Config &config); bool LoadGenesisBlock(const CChainParams &chainparams); @@ -202,7 +202,7 @@ const FlatFilePos &pos); bool RollforwardBlock(const CBlockIndex *pindex, CCoinsViewCache &inputs, - const Config &config); + const Consensus::Params ¶ms); } g_chainstate; /** @@ -859,7 +859,7 @@ * placed in hashBlock. If blockIndex is provided, the transaction is fetched * from the corresponding block. */ -bool GetTransaction(const Config &config, const TxId &txid, +bool GetTransaction(const Consensus::Params ¶ms, const TxId &txid, CTransactionRef &txOut, uint256 &hashBlock, bool fAllowSlow, CBlockIndex *blockIndex) { CBlockIndex *pindexSlow = blockIndex; @@ -889,7 +889,7 @@ if (pindexSlow) { CBlock block; - if (ReadBlockFromDisk(block, pindexSlow, config)) { + if (ReadBlockFromDisk(block, pindexSlow, params)) { for (const auto &tx : block.vtx) { if (tx->GetId() == txid) { txOut = tx; @@ -933,7 +933,7 @@ } bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, - const Config &config) { + const Consensus::Params ¶ms) { block.SetNull(); // Open history file to read @@ -952,8 +952,7 @@ } // Check the header - if (!CheckProofOfWork(block.GetHash(), block.nBits, - config.GetChainParams().GetConsensus())) { + if (!CheckProofOfWork(block.GetHash(), block.nBits, params)) { return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString()); } @@ -962,14 +961,14 @@ } bool ReadBlockFromDisk(CBlock &block, const CBlockIndex *pindex, - const Config &config) { + const Consensus::Params ¶ms) { FlatFilePos blockPos; { LOCK(cs_main); blockPos = pindex->GetBlockPos(); } - if (!ReadBlockFromDisk(block, blockPos, config)) { + if (!ReadBlockFromDisk(block, blockPos, params)) { return false; } @@ -2199,12 +2198,15 @@ bool CChainState::DisconnectTip(const Config &config, CValidationState &state, DisconnectedBlockTransactions *disconnectpool) { CBlockIndex *pindexDelete = chainActive.Tip(); + const Consensus::Params &consensusParams = + config.GetChainParams().GetConsensus(); + assert(pindexDelete); // Read block from disk. std::shared_ptr pblock = std::make_shared(); CBlock &block = *pblock; - if (!ReadBlockFromDisk(block, pindexDelete, config)) { + if (!ReadBlockFromDisk(block, pindexDelete, consensusParams)) { return AbortNode(state, "Failed to read block"); } @@ -2231,9 +2233,6 @@ return false; } - const Consensus::Params &consensusParams = - config.GetChainParams().GetConsensus(); - // If this block is deactivating a fork, we move all mempool transactions // in front of disconnectpool for reprocessing in a future // updateMempoolForReorg call @@ -2438,13 +2437,16 @@ DisconnectedBlockTransactions &disconnectpool) { AssertLockHeld(cs_main); + const Consensus::Params &consensusParams = + config.GetChainParams().GetConsensus(); + assert(pindexNew->pprev == chainActive.Tip()); // Read block from disk. int64_t nTime1 = GetTimeMicros(); std::shared_ptr pthisBlock; if (!pblock) { std::shared_ptr pblockNew = std::make_shared(); - if (!ReadBlockFromDisk(*pblockNew, pindexNew, config)) { + if (!ReadBlockFromDisk(*pblockNew, pindexNew, consensusParams)) { return AbortNode(state, "Failed to read block"); } pthisBlock = pblockNew; @@ -2518,9 +2520,6 @@ g_mempool.removeForBlock(blockConnecting.vtx, pindexNew->nHeight); disconnectpool.removeForBlock(blockConnecting.vtx); - const Consensus::Params &consensusParams = - config.GetChainParams().GetConsensus(); - // If this block is activating a fork, we move all mempool transactions // in front of disconnectpool for reprocessing in a future // updateMempoolForReorg call @@ -4582,6 +4581,10 @@ bool CVerifyDB::VerifyDB(const Config &config, CCoinsView *coinsview, int nCheckLevel, int nCheckDepth) { LOCK(cs_main); + + const Consensus::Params &consensusParams = + config.GetChainParams().GetConsensus(); + if (chainActive.Tip() == nullptr || chainActive.Tip()->pprev == nullptr) { return true; } @@ -4634,7 +4637,7 @@ CBlock block; // check level 0: read from disk - if (!ReadBlockFromDisk(block, pindex, config)) { + if (!ReadBlockFromDisk(block, pindex, consensusParams)) { return error( "VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString()); @@ -4710,7 +4713,7 @@ false); pindex = chainActive.Next(pindex); CBlock block; - if (!ReadBlockFromDisk(block, pindex, config)) { + if (!ReadBlockFromDisk(block, pindex, consensusParams)) { return error( "VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString()); @@ -4739,10 +4742,10 @@ */ bool CChainState::RollforwardBlock(const CBlockIndex *pindex, CCoinsViewCache &view, - const Config &config) { + const Consensus::Params ¶ms) { // TODO: merge with ConnectBlock CBlock block; - if (!ReadBlockFromDisk(block, pindex, config)) { + if (!ReadBlockFromDisk(block, pindex, params)) { return error("ReplayBlock(): ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString()); } @@ -4765,7 +4768,8 @@ return true; } -bool CChainState::ReplayBlocks(const Config &config, CCoinsView *view) { +bool CChainState::ReplayBlocks(const Consensus::Params ¶ms, + CCoinsView *view) { LOCK(cs_main); CCoinsViewCache cache(view); @@ -4814,7 +4818,7 @@ if (pindexOld->nHeight > 0) { // Never disconnect the genesis block. CBlock block; - if (!ReadBlockFromDisk(block, pindexOld, config)) { + if (!ReadBlockFromDisk(block, pindexOld, params)) { return error("RollbackBlock(): ReadBlockFromDisk() failed at " "%d, hash=%s", pindexOld->nHeight, @@ -4848,7 +4852,7 @@ const CBlockIndex *pindex = pindexNew->GetAncestor(nHeight); LogPrintf("Rolling forward %s (%i)\n", pindex->GetBlockHash().ToString(), nHeight); - if (!RollforwardBlock(pindex, cache, config)) { + if (!RollforwardBlock(pindex, cache, params)) { return false; } } @@ -4859,8 +4863,8 @@ return true; } -bool ReplayBlocks(const Config &config, CCoinsView *view) { - return g_chainstate.ReplayBlocks(config, view); +bool ReplayBlocks(const Consensus::Params ¶ms, CCoinsView *view) { + return g_chainstate.ReplayBlocks(params, view); } bool CChainState::RewindBlockIndex(const Config &config) { @@ -5162,7 +5166,7 @@ std::shared_ptr pblockrecursive = std::make_shared(); if (ReadBlockFromDisk(*pblockrecursive, it->second, - config)) { + chainparams.GetConsensus())) { LogPrint( BCLog::REINDEX, "%s: Processing out of order child %s of %s\n", diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -2332,12 +2332,14 @@ } } + const Consensus::Params ¶ms = config.GetChainParams().GetConsensus(); + // when a reorg'd block is requested, we also list any relevant transactions // in the blocks of the chain that was detached UniValue removed(UniValue::VARR); while (include_removed && paltindex && paltindex != pindex) { CBlock block; - if (!ReadBlockFromDisk(block, paltindex, config)) { + if (!ReadBlockFromDisk(block, paltindex, params)) { throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk"); } diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1843,7 +1843,7 @@ } CBlock block; - if (ReadBlockFromDisk(block, pindex, GetConfig())) { + if (ReadBlockFromDisk(block, pindex, chainParams.GetConsensus())) { LOCK2(cs_main, cs_wallet); if (pindex && !chainActive.Contains(pindex)) { // Abort scan if current block is no longer active, to diff --git a/src/zmq/zmqpublishnotifier.cpp b/src/zmq/zmqpublishnotifier.cpp --- a/src/zmq/zmqpublishnotifier.cpp +++ b/src/zmq/zmqpublishnotifier.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -173,7 +174,8 @@ { LOCK(cs_main); CBlock block; - if (!ReadBlockFromDisk(block, pindex, config)) { + if (!ReadBlockFromDisk(block, pindex, + config.GetChainParams().GetConsensus())) { zmqError("Can't read block from disk"); return false; }