mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-25 10:43:19 -03:00
Merge bitcoin/bitcoin#25168: refactor: Avoid passing params where not needed
fa1b76aeb0
Do not call global Params() when chainman is in scope (MacroFake)fa30234be8
Do not pass CChainParams& to PeerManager::make (MacroFake)fafe5c0ca2
Do not pass CChainParams& to BlockAssembler constructor (MacroFake)faf012b438
Do not pass Consensus::Params& to Chainstate helpers (MacroFake)fa4ee53dca
Do not pass time getter to Chainstate helpers (MacroFake) Pull request description: It seems confusing to pass chain params, consensus params, or a time function around when it is not needed. Fix this by: * Inlining the passed time getter function. I don't see a use case why this should be mockable. * Using `chainman.GetConsensus()` or `chainman.GetParams()`, where possible. ACKs for top commit: promag: Code review ACKfa1b76aeb0
. vincenzopalazzo: ACKfa1b76aeb0
Tree-SHA512: 1abff5cba4b4871d97f17dbcdf67bc9255ff21fa4150a79a74e39b28f0610eab3e7dee24d56872dd6e111f003b55e288958cdd467e6218368d896f191e4ec9cd
This commit is contained in:
commit
4d0c00dffd
21 changed files with 66 additions and 92 deletions
|
@ -76,7 +76,6 @@ int main(int argc, char* argv[])
|
|||
std::ref(chainman),
|
||||
nullptr,
|
||||
false,
|
||||
chainparams.GetConsensus(),
|
||||
false,
|
||||
2 << 20,
|
||||
2 << 22,
|
||||
|
@ -91,10 +90,8 @@ int main(int argc, char* argv[])
|
|||
auto maybe_verify_error = node::VerifyLoadedChainstate(std::ref(chainman),
|
||||
false,
|
||||
false,
|
||||
chainparams.GetConsensus(),
|
||||
DEFAULT_CHECKBLOCKS,
|
||||
DEFAULT_CHECKLEVEL,
|
||||
/*get_unix_time_seconds=*/static_cast<int64_t (*)()>(GetTime));
|
||||
DEFAULT_CHECKLEVEL);
|
||||
if (maybe_verify_error.has_value()) {
|
||||
std::cerr << "Failed to verify loaded Chain state from your datadir." << std::endl;
|
||||
goto epilogue;
|
||||
|
|
13
src/init.cpp
13
src/init.cpp
|
@ -1435,7 +1435,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||
chainman,
|
||||
Assert(node.mempool.get()),
|
||||
fPruneMode,
|
||||
chainparams.GetConsensus(),
|
||||
fReindexChainState,
|
||||
cache_sizes.block_tree_db,
|
||||
cache_sizes.coins_db,
|
||||
|
@ -1482,7 +1481,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||
break;
|
||||
case ChainstateLoadingError::ERROR_BLOCKS_WITNESS_INSUFFICIENTLY_VALIDATED:
|
||||
strLoadError = strprintf(_("Witness data for blocks after height %d requires validation. Please restart with -reindex."),
|
||||
chainparams.GetConsensus().SegwitHeight);
|
||||
chainman.GetConsensus().SegwitHeight);
|
||||
break;
|
||||
case ChainstateLoadingError::SHUTDOWN_PROBED:
|
||||
break;
|
||||
|
@ -1499,10 +1498,8 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||
maybe_verify_error = VerifyLoadedChainstate(chainman,
|
||||
fReset,
|
||||
fReindexChainState,
|
||||
chainparams.GetConsensus(),
|
||||
check_blocks,
|
||||
args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL),
|
||||
/*get_unix_time_seconds=*/static_cast<int64_t(*)()>(GetTime));
|
||||
args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL));
|
||||
} catch (const std::exception& e) {
|
||||
LogPrintf("%s\n", e.what());
|
||||
maybe_verify_error = ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE;
|
||||
|
@ -1558,7 +1555,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||
ChainstateManager& chainman = *Assert(node.chainman);
|
||||
|
||||
assert(!node.peerman);
|
||||
node.peerman = PeerManager::make(chainparams, *node.connman, *node.addrman, node.banman.get(),
|
||||
node.peerman = PeerManager::make(*node.connman, *node.addrman, node.banman.get(),
|
||||
chainman, *node.mempool, ignores_incoming_txs);
|
||||
RegisterValidationInterface(node.peerman.get());
|
||||
|
||||
|
@ -1680,8 +1677,8 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||
chain_active_height = chainman.ActiveChain().Height();
|
||||
if (tip_info) {
|
||||
tip_info->block_height = chain_active_height;
|
||||
tip_info->block_time = chainman.ActiveChain().Tip() ? chainman.ActiveChain().Tip()->GetBlockTime() : Params().GenesisBlock().GetBlockTime();
|
||||
tip_info->verification_progress = GuessVerificationProgress(Params().TxData(), chainman.ActiveChain().Tip());
|
||||
tip_info->block_time = chainman.ActiveChain().Tip() ? chainman.ActiveChain().Tip()->GetBlockTime() : chainman.GetParams().GenesisBlock().GetBlockTime();
|
||||
tip_info->verification_progress = GuessVerificationProgress(chainman.GetParams().TxData(), chainman.ActiveChain().Tip());
|
||||
}
|
||||
if (tip_info && chainman.m_best_header) {
|
||||
tip_info->header_height = chainman.m_best_header->nHeight;
|
||||
|
|
|
@ -459,7 +459,7 @@ struct CNodeState {
|
|||
class PeerManagerImpl final : public PeerManager
|
||||
{
|
||||
public:
|
||||
PeerManagerImpl(const CChainParams& chainparams, CConnman& connman, AddrMan& addrman,
|
||||
PeerManagerImpl(CConnman& connman, AddrMan& addrman,
|
||||
BanMan* banman, ChainstateManager& chainman,
|
||||
CTxMemPool& pool, bool ignore_incoming_txs);
|
||||
|
||||
|
@ -1584,17 +1584,17 @@ std::optional<std::string> PeerManagerImpl::FetchBlock(NodeId peer_id, const CBl
|
|||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::unique_ptr<PeerManager> PeerManager::make(const CChainParams& chainparams, CConnman& connman, AddrMan& addrman,
|
||||
std::unique_ptr<PeerManager> PeerManager::make(CConnman& connman, AddrMan& addrman,
|
||||
BanMan* banman, ChainstateManager& chainman,
|
||||
CTxMemPool& pool, bool ignore_incoming_txs)
|
||||
{
|
||||
return std::make_unique<PeerManagerImpl>(chainparams, connman, addrman, banman, chainman, pool, ignore_incoming_txs);
|
||||
return std::make_unique<PeerManagerImpl>(connman, addrman, banman, chainman, pool, ignore_incoming_txs);
|
||||
}
|
||||
|
||||
PeerManagerImpl::PeerManagerImpl(const CChainParams& chainparams, CConnman& connman, AddrMan& addrman,
|
||||
PeerManagerImpl::PeerManagerImpl(CConnman& connman, AddrMan& addrman,
|
||||
BanMan* banman, ChainstateManager& chainman,
|
||||
CTxMemPool& pool, bool ignore_incoming_txs)
|
||||
: m_chainparams(chainparams),
|
||||
: m_chainparams(chainman.GetParams()),
|
||||
m_connman(connman),
|
||||
m_addrman(addrman),
|
||||
m_banman(banman),
|
||||
|
|
|
@ -39,7 +39,7 @@ struct CNodeStateStats {
|
|||
class PeerManager : public CValidationInterface, public NetEventsInterface
|
||||
{
|
||||
public:
|
||||
static std::unique_ptr<PeerManager> make(const CChainParams& chainparams, CConnman& connman, AddrMan& addrman,
|
||||
static std::unique_ptr<PeerManager> make(CConnman& connman, AddrMan& addrman,
|
||||
BanMan* banman, ChainstateManager& chainman,
|
||||
CTxMemPool& pool, bool ignore_incoming_txs);
|
||||
virtual ~PeerManager() { }
|
||||
|
|
|
@ -13,7 +13,6 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
|
|||
ChainstateManager& chainman,
|
||||
CTxMemPool* mempool,
|
||||
bool fPruneMode,
|
||||
const Consensus::Params& consensus_params,
|
||||
bool fReindexChainState,
|
||||
int64_t nBlockTreeDBCache,
|
||||
int64_t nCoinDBCache,
|
||||
|
@ -57,7 +56,7 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
|
|||
}
|
||||
|
||||
if (!chainman.BlockIndex().empty() &&
|
||||
!chainman.m_blockman.LookupBlockIndex(consensus_params.hashGenesisBlock)) {
|
||||
!chainman.m_blockman.LookupBlockIndex(chainman.GetConsensus().hashGenesisBlock)) {
|
||||
return ChainstateLoadingError::ERROR_BAD_GENESIS_BLOCK;
|
||||
}
|
||||
|
||||
|
@ -126,10 +125,8 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
|
|||
std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManager& chainman,
|
||||
bool fReset,
|
||||
bool fReindexChainState,
|
||||
const Consensus::Params& consensus_params,
|
||||
int check_blocks,
|
||||
int check_level,
|
||||
std::function<int64_t()> get_unix_time_seconds)
|
||||
int check_level)
|
||||
{
|
||||
auto is_coinsview_empty = [&](CChainState* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
|
||||
return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull();
|
||||
|
@ -140,12 +137,12 @@ std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManage
|
|||
for (CChainState* chainstate : chainman.GetAll()) {
|
||||
if (!is_coinsview_empty(chainstate)) {
|
||||
const CBlockIndex* tip = chainstate->m_chain.Tip();
|
||||
if (tip && tip->nTime > get_unix_time_seconds() + MAX_FUTURE_BLOCK_TIME) {
|
||||
if (tip && tip->nTime > GetTime() + MAX_FUTURE_BLOCK_TIME) {
|
||||
return ChainstateLoadVerifyError::ERROR_BLOCK_FROM_FUTURE;
|
||||
}
|
||||
|
||||
if (!CVerifyDB().VerifyDB(
|
||||
*chainstate, consensus_params, chainstate->CoinsDB(),
|
||||
*chainstate, chainman.GetConsensus(), chainstate->CoinsDB(),
|
||||
check_level,
|
||||
check_blocks)) {
|
||||
return ChainstateLoadVerifyError::ERROR_CORRUPTED_BLOCK_DB;
|
||||
|
|
|
@ -59,7 +59,6 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
|
|||
ChainstateManager& chainman,
|
||||
CTxMemPool* mempool,
|
||||
bool fPruneMode,
|
||||
const Consensus::Params& consensus_params,
|
||||
bool fReindexChainState,
|
||||
int64_t nBlockTreeDBCache,
|
||||
int64_t nCoinDBCache,
|
||||
|
@ -78,10 +77,8 @@ enum class ChainstateLoadVerifyError {
|
|||
std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManager& chainman,
|
||||
bool fReset,
|
||||
bool fReindexChainState,
|
||||
const Consensus::Params& consensus_params,
|
||||
int check_blocks,
|
||||
int check_level,
|
||||
std::function<int64_t()> get_unix_time_seconds);
|
||||
int check_level);
|
||||
} // namespace node
|
||||
|
||||
#endif // BITCOIN_NODE_CHAINSTATE_H
|
||||
|
|
|
@ -228,7 +228,7 @@ public:
|
|||
uint256 getBestBlockHash() override
|
||||
{
|
||||
const CBlockIndex* tip = WITH_LOCK(::cs_main, return chainman().ActiveChain().Tip());
|
||||
return tip ? tip->GetBlockHash() : Params().GenesisBlock().GetHash();
|
||||
return tip ? tip->GetBlockHash() : chainman().GetParams().GenesisBlock().GetHash();
|
||||
}
|
||||
int64_t getLastBlockTime() override
|
||||
{
|
||||
|
@ -236,7 +236,7 @@ public:
|
|||
if (chainman().ActiveChain().Tip()) {
|
||||
return chainman().ActiveChain().Tip()->GetBlockTime();
|
||||
}
|
||||
return Params().GenesisBlock().GetBlockTime(); // Genesis block's time of current network
|
||||
return chainman().GetParams().GenesisBlock().GetBlockTime(); // Genesis block's time of current network
|
||||
}
|
||||
double getVerificationProgress() override
|
||||
{
|
||||
|
@ -245,7 +245,7 @@ public:
|
|||
LOCK(::cs_main);
|
||||
tip = chainman().ActiveChain().Tip();
|
||||
}
|
||||
return GuessVerificationProgress(Params().TxData(), tip);
|
||||
return GuessVerificationProgress(chainman().GetParams().TxData(), tip);
|
||||
}
|
||||
bool isInitialBlockDownload() override {
|
||||
return chainman().ActiveChainstate().IsInitialBlockDownload();
|
||||
|
@ -546,7 +546,7 @@ public:
|
|||
double guessVerificationProgress(const uint256& block_hash) override
|
||||
{
|
||||
LOCK(cs_main);
|
||||
return GuessVerificationProgress(Params().TxData(), chainman().m_blockman.LookupBlockIndex(block_hash));
|
||||
return GuessVerificationProgress(chainman().GetParams().TxData(), chainman().m_blockman.LookupBlockIndex(block_hash));
|
||||
}
|
||||
bool hasBlocks(const uint256& block_hash, int min_height, std::optional<int> max_height) override
|
||||
{
|
||||
|
|
|
@ -62,8 +62,8 @@ BlockAssembler::Options::Options()
|
|||
nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT;
|
||||
}
|
||||
|
||||
BlockAssembler::BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool, const CChainParams& params, const Options& options)
|
||||
: chainparams(params),
|
||||
BlockAssembler::BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool, const Options& options)
|
||||
: chainparams{chainstate.m_chainman.GetParams()},
|
||||
m_mempool(mempool),
|
||||
m_chainstate(chainstate)
|
||||
{
|
||||
|
@ -87,8 +87,8 @@ static BlockAssembler::Options DefaultOptions()
|
|||
return options;
|
||||
}
|
||||
|
||||
BlockAssembler::BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool, const CChainParams& params)
|
||||
: BlockAssembler(chainstate, mempool, params, DefaultOptions()) {}
|
||||
BlockAssembler::BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool)
|
||||
: BlockAssembler(chainstate, mempool, DefaultOptions()) {}
|
||||
|
||||
void BlockAssembler::resetBlock()
|
||||
{
|
||||
|
|
|
@ -157,8 +157,8 @@ public:
|
|||
CFeeRate blockMinFeeRate;
|
||||
};
|
||||
|
||||
explicit BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool, const CChainParams& params);
|
||||
explicit BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool, const CChainParams& params, const Options& options);
|
||||
explicit BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool);
|
||||
explicit BlockAssembler(CChainState& chainstate, const CTxMemPool& mempool, const Options& options);
|
||||
|
||||
/** Construct a new block template with coinbase to scriptPubKeyIn */
|
||||
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);
|
||||
|
|
|
@ -305,7 +305,7 @@ static bool rest_block(const std::any& context,
|
|||
if (chainman.m_blockman.IsBlockPruned(pblockindex))
|
||||
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not available (pruned data)");
|
||||
|
||||
if (!ReadBlockFromDisk(block, pblockindex, Params().GetConsensus()))
|
||||
if (!ReadBlockFromDisk(block, pblockindex, chainman.GetParams().GetConsensus()))
|
||||
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
|
||||
}
|
||||
|
||||
|
|
|
@ -777,11 +777,11 @@ static RPCHelpMan pruneblockchain()
|
|||
|
||||
unsigned int height = (unsigned int) heightParam;
|
||||
unsigned int chainHeight = (unsigned int) active_chain.Height();
|
||||
if (chainHeight < Params().PruneAfterHeight())
|
||||
if (chainHeight < chainman.GetParams().PruneAfterHeight()) {
|
||||
throw JSONRPCError(RPC_MISC_ERROR, "Blockchain is too short for pruning.");
|
||||
else if (height > chainHeight)
|
||||
} else if (height > chainHeight) {
|
||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "Blockchain is shorter than the attempted prune height.");
|
||||
else if (height > chainHeight - MIN_BLOCKS_TO_KEEP) {
|
||||
} else if (height > chainHeight - MIN_BLOCKS_TO_KEEP) {
|
||||
LogPrint(BCLog::RPC, "Attempt to prune blocks close to the tip. Retaining the minimum number of blocks.\n");
|
||||
height = chainHeight - MIN_BLOCKS_TO_KEEP;
|
||||
}
|
||||
|
@ -1058,7 +1058,7 @@ static RPCHelpMan verifychain()
|
|||
|
||||
CChainState& active_chainstate = chainman.ActiveChainstate();
|
||||
return CVerifyDB().VerifyDB(
|
||||
active_chainstate, Params().GetConsensus(), active_chainstate.CoinsTip(), check_level, check_depth);
|
||||
active_chainstate, chainman.GetParams().GetConsensus(), active_chainstate.CoinsTip(), check_level, check_depth);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
@ -1189,14 +1189,14 @@ RPCHelpMan getblockchaininfo()
|
|||
const CBlockIndex& tip{*CHECK_NONFATAL(active_chainstate.m_chain.Tip())};
|
||||
const int height{tip.nHeight};
|
||||
UniValue obj(UniValue::VOBJ);
|
||||
obj.pushKV("chain", Params().NetworkIDString());
|
||||
obj.pushKV("chain", chainman.GetParams().NetworkIDString());
|
||||
obj.pushKV("blocks", height);
|
||||
obj.pushKV("headers", chainman.m_best_header ? chainman.m_best_header->nHeight : -1);
|
||||
obj.pushKV("bestblockhash", tip.GetBlockHash().GetHex());
|
||||
obj.pushKV("difficulty", GetDifficulty(&tip));
|
||||
obj.pushKV("time", tip.GetBlockTime());
|
||||
obj.pushKV("mediantime", tip.GetMedianTimePast());
|
||||
obj.pushKV("verificationprogress", GuessVerificationProgress(Params().TxData(), &tip));
|
||||
obj.pushKV("verificationprogress", GuessVerificationProgress(chainman.GetParams().TxData(), &tip));
|
||||
obj.pushKV("initialblockdownload", active_chainstate.IsInitialBlockDownload());
|
||||
obj.pushKV("chainwork", tip.nChainWork.GetHex());
|
||||
obj.pushKV("size_on_disk", chainman.m_blockman.CalculateCurrentUsage());
|
||||
|
@ -1563,7 +1563,7 @@ static RPCHelpMan getchaintxstats()
|
|||
{
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
const CBlockIndex* pindex;
|
||||
int blockcount = 30 * 24 * 60 * 60 / Params().GetConsensus().nPowTargetSpacing; // By default: 1 month
|
||||
int blockcount = 30 * 24 * 60 * 60 / chainman.GetParams().GetConsensus().nPowTargetSpacing; // By default: 1 month
|
||||
|
||||
if (request.params[1].isNull()) {
|
||||
LOCK(cs_main);
|
||||
|
@ -1879,7 +1879,7 @@ static RPCHelpMan getblockstats()
|
|||
ret_all.pushKV("minfeerate", (minfeerate == MAX_MONEY) ? 0 : minfeerate);
|
||||
ret_all.pushKV("mintxsize", mintxsize == MAX_BLOCK_SERIALIZED_SIZE ? 0 : mintxsize);
|
||||
ret_all.pushKV("outs", outputs);
|
||||
ret_all.pushKV("subsidy", GetBlockSubsidy(pindex.nHeight, Params().GetConsensus()));
|
||||
ret_all.pushKV("subsidy", GetBlockSubsidy(pindex.nHeight, chainman.GetParams().GetConsensus()));
|
||||
ret_all.pushKV("swtotal_size", swtotal_size);
|
||||
ret_all.pushKV("swtotal_weight", swtotal_weight);
|
||||
ret_all.pushKV("swtxs", swtxs);
|
||||
|
|
|
@ -119,9 +119,7 @@ static bool GenerateBlock(ChainstateManager& chainman, CBlock& block, uint64_t&
|
|||
block_hash.SetNull();
|
||||
block.hashMerkleRoot = BlockMerkleRoot(block);
|
||||
|
||||
CChainParams chainparams(Params());
|
||||
|
||||
while (max_tries > 0 && block.nNonce < std::numeric_limits<uint32_t>::max() && !CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus()) && !ShutdownRequested()) {
|
||||
while (max_tries > 0 && block.nNonce < std::numeric_limits<uint32_t>::max() && !CheckProofOfWork(block.GetHash(), block.nBits, chainman.GetConsensus()) && !ShutdownRequested()) {
|
||||
++block.nNonce;
|
||||
--max_tries;
|
||||
}
|
||||
|
@ -145,7 +143,7 @@ static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& me
|
|||
{
|
||||
UniValue blockHashes(UniValue::VARR);
|
||||
while (nGenerate > 0 && !ShutdownRequested()) {
|
||||
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(chainman.ActiveChainstate(), mempool, Params()).CreateNewBlock(coinbase_script));
|
||||
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler{chainman.ActiveChainstate(), mempool}.CreateNewBlock(coinbase_script));
|
||||
if (!pblocktemplate.get())
|
||||
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
|
||||
CBlock *pblock = &pblocktemplate->block;
|
||||
|
@ -349,7 +347,6 @@ static RPCHelpMan generateblock()
|
|||
}
|
||||
}
|
||||
|
||||
CChainParams chainparams(Params());
|
||||
CBlock block;
|
||||
|
||||
ChainstateManager& chainman = EnsureChainman(node);
|
||||
|
@ -357,7 +354,7 @@ static RPCHelpMan generateblock()
|
|||
LOCK(cs_main);
|
||||
|
||||
CTxMemPool empty_mempool;
|
||||
std::unique_ptr<CBlockTemplate> blocktemplate(BlockAssembler(chainman.ActiveChainstate(), empty_mempool, chainparams).CreateNewBlock(coinbase_script));
|
||||
std::unique_ptr<CBlockTemplate> blocktemplate(BlockAssembler{chainman.ActiveChainstate(), empty_mempool}.CreateNewBlock(coinbase_script));
|
||||
if (!blocktemplate) {
|
||||
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
|
||||
}
|
||||
|
@ -374,7 +371,7 @@ static RPCHelpMan generateblock()
|
|||
LOCK(cs_main);
|
||||
|
||||
BlockValidationState state;
|
||||
if (!TestBlockValidity(state, chainparams, chainman.ActiveChainstate(), block, chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock), false, false)) {
|
||||
if (!TestBlockValidity(state, chainman.GetParams(), chainman.ActiveChainstate(), block, chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock), false, false)) {
|
||||
throw JSONRPCError(RPC_VERIFY_ERROR, strprintf("TestBlockValidity failed: %s", state.ToString()));
|
||||
}
|
||||
}
|
||||
|
@ -429,7 +426,7 @@ static RPCHelpMan getmininginfo()
|
|||
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());
|
||||
obj.pushKV("chain", chainman.GetParams().NetworkIDString());
|
||||
obj.pushKV("warnings", GetWarnings(false).original);
|
||||
return obj;
|
||||
},
|
||||
|
@ -643,7 +640,7 @@ static RPCHelpMan getblocktemplate()
|
|||
if (block.hashPrevBlock != pindexPrev->GetBlockHash())
|
||||
return "inconclusive-not-best-prevblk";
|
||||
BlockValidationState state;
|
||||
TestBlockValidity(state, Params(), active_chainstate, block, pindexPrev, false, true);
|
||||
TestBlockValidity(state, chainman.GetParams(), active_chainstate, block, pindexPrev, false, true);
|
||||
return BIP22ValidationResult(state);
|
||||
}
|
||||
|
||||
|
@ -665,7 +662,7 @@ static RPCHelpMan getblocktemplate()
|
|||
if (strMode != "template")
|
||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid mode");
|
||||
|
||||
if (!Params().IsTestChain()) {
|
||||
if (!chainman.GetParams().IsTestChain()) {
|
||||
const CConnman& connman = EnsureConnman(node);
|
||||
if (connman.GetNodeCount(ConnectionDirection::Both) == 0) {
|
||||
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, PACKAGE_NAME " is not connected!");
|
||||
|
@ -726,7 +723,7 @@ static RPCHelpMan getblocktemplate()
|
|||
// TODO: Maybe recheck connections/IBD and (if something wrong) send an expires-immediately template to stop miners?
|
||||
}
|
||||
|
||||
const Consensus::Params& consensusParams = Params().GetConsensus();
|
||||
const Consensus::Params& consensusParams = chainman.GetParams().GetConsensus();
|
||||
|
||||
// GBT must be called with 'signet' set in the rules for signet chains
|
||||
if (consensusParams.signet_blocks && setClientRules.count("signet") != 1) {
|
||||
|
@ -755,7 +752,7 @@ static RPCHelpMan getblocktemplate()
|
|||
|
||||
// Create new block
|
||||
CScript scriptDummy = CScript() << OP_TRUE;
|
||||
pblocktemplate = BlockAssembler(active_chainstate, mempool, Params()).CreateNewBlock(scriptDummy);
|
||||
pblocktemplate = BlockAssembler{active_chainstate, mempool}.CreateNewBlock(scriptDummy);
|
||||
if (!pblocktemplate)
|
||||
throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
|
||||
|
||||
|
|
|
@ -217,7 +217,7 @@ static RPCHelpMan getrawtransaction()
|
|||
uint256 hash = ParseHashV(request.params[0], "parameter 1");
|
||||
const CBlockIndex* blockindex = nullptr;
|
||||
|
||||
if (hash == Params().GenesisBlock().hashMerkleRoot) {
|
||||
if (hash == chainman.GetParams().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 ordinary transaction and cannot be retrieved");
|
||||
}
|
||||
|
@ -245,7 +245,7 @@ static RPCHelpMan getrawtransaction()
|
|||
}
|
||||
|
||||
uint256 hash_block;
|
||||
const CTransactionRef tx = GetTransaction(blockindex, node.mempool.get(), hash, Params().GetConsensus(), hash_block);
|
||||
const CTransactionRef tx = GetTransaction(blockindex, node.mempool.get(), hash, chainman.GetConsensus(), hash_block);
|
||||
if (!tx) {
|
||||
std::string errmsg;
|
||||
if (blockindex) {
|
||||
|
|
|
@ -87,7 +87,7 @@ static RPCHelpMan gettxoutproof()
|
|||
LOCK(cs_main);
|
||||
|
||||
if (pblockindex == nullptr) {
|
||||
const CTransactionRef tx = GetTransaction(/*block_index=*/nullptr, /*mempool=*/nullptr, *setTxids.begin(), Params().GetConsensus(), hashBlock);
|
||||
const CTransactionRef tx = GetTransaction(/*block_index=*/nullptr, /*mempool=*/nullptr, *setTxids.begin(), chainman.GetConsensus(), hashBlock);
|
||||
if (!tx || hashBlock.IsNull()) {
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction not yet in block");
|
||||
}
|
||||
|
@ -98,7 +98,7 @@ static RPCHelpMan gettxoutproof()
|
|||
}
|
||||
|
||||
CBlock block;
|
||||
if (!ReadBlockFromDisk(block, pblockindex, Params().GetConsensus())) {
|
||||
if (!ReadBlockFromDisk(block, pblockindex, chainman.GetConsensus())) {
|
||||
throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk");
|
||||
}
|
||||
|
||||
|
|
|
@ -65,8 +65,7 @@ CBlock BuildChainTestingSetup::CreateBlock(const CBlockIndex* prev,
|
|||
const std::vector<CMutableTransaction>& txns,
|
||||
const CScript& scriptPubKey)
|
||||
{
|
||||
const CChainParams& chainparams = Params();
|
||||
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(m_node.chainman->ActiveChainstate(), *m_node.mempool, chainparams).CreateNewBlock(scriptPubKey);
|
||||
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), *m_node.mempool}.CreateNewBlock(scriptPubKey);
|
||||
CBlock& block = pblocktemplate->block;
|
||||
block.hashPrevBlock = prev->GetBlockHash();
|
||||
block.nTime = prev->nTime + 1;
|
||||
|
@ -83,7 +82,7 @@ CBlock BuildChainTestingSetup::CreateBlock(const CBlockIndex* prev,
|
|||
block.hashMerkleRoot = BlockMerkleRoot(block);
|
||||
}
|
||||
|
||||
while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;
|
||||
while (!CheckProofOfWork(block.GetHash(), block.nBits, m_node.chainman->GetConsensus())) ++block.nNonce;
|
||||
|
||||
return block;
|
||||
}
|
||||
|
|
|
@ -44,11 +44,10 @@ BOOST_FIXTURE_TEST_SUITE(denialofservice_tests, TestingSetup)
|
|||
// work.
|
||||
BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
|
||||
{
|
||||
const CChainParams& chainparams = Params();
|
||||
auto connman = std::make_unique<CConnman>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman);
|
||||
// Disable inactivity checks for this test to avoid interference
|
||||
static_cast<ConnmanTestMsg*>(connman.get())->SetPeerConnectTimeout(99999s);
|
||||
auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, nullptr,
|
||||
auto peerLogic = PeerManager::make(*connman, *m_node.addrman, nullptr,
|
||||
*m_node.chainman, *m_node.mempool, false);
|
||||
|
||||
// Mock an outbound peer
|
||||
|
@ -134,9 +133,8 @@ static void AddRandomOutboundPeer(NodeId& id, std::vector<CNode*>& vNodes, PeerM
|
|||
BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
|
||||
{
|
||||
NodeId id{0};
|
||||
const CChainParams& chainparams = Params();
|
||||
auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman);
|
||||
auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, nullptr,
|
||||
auto peerLogic = PeerManager::make(*connman, *m_node.addrman, nullptr,
|
||||
*m_node.chainman, *m_node.mempool, false);
|
||||
|
||||
constexpr int max_outbound_full_relay = MAX_OUTBOUND_FULL_RELAY_CONNECTIONS;
|
||||
|
@ -147,7 +145,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
|
|||
|
||||
const auto time_init{GetTime<std::chrono::seconds>()};
|
||||
SetMockTime(time_init);
|
||||
const auto time_later{time_init + 3 * std::chrono::seconds{chainparams.GetConsensus().nPowTargetSpacing} + 1s};
|
||||
const auto time_later{time_init + 3 * std::chrono::seconds{m_node.chainman->GetConsensus().nPowTargetSpacing} + 1s};
|
||||
connman->Init(options);
|
||||
std::vector<CNode *> vNodes;
|
||||
|
||||
|
@ -212,9 +210,8 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
|
|||
BOOST_AUTO_TEST_CASE(block_relay_only_eviction)
|
||||
{
|
||||
NodeId id{0};
|
||||
const CChainParams& chainparams = Params();
|
||||
auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman);
|
||||
auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, nullptr,
|
||||
auto peerLogic = PeerManager::make(*connman, *m_node.addrman, nullptr,
|
||||
*m_node.chainman, *m_node.mempool, false);
|
||||
|
||||
constexpr int max_outbound_block_relay{MAX_BLOCK_RELAY_ONLY_CONNECTIONS};
|
||||
|
@ -274,10 +271,9 @@ BOOST_AUTO_TEST_CASE(block_relay_only_eviction)
|
|||
|
||||
BOOST_AUTO_TEST_CASE(peer_discouragement)
|
||||
{
|
||||
const CChainParams& chainparams = Params();
|
||||
auto banman = std::make_unique<BanMan>(m_args.GetDataDirBase() / "banlist", nullptr, DEFAULT_MISBEHAVING_BANTIME);
|
||||
auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman);
|
||||
auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, banman.get(),
|
||||
auto peerLogic = PeerManager::make(*connman, *m_node.addrman, banman.get(),
|
||||
*m_node.chainman, *m_node.mempool, false);
|
||||
|
||||
CNetAddr tor_netaddr;
|
||||
|
@ -390,10 +386,9 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
|
|||
|
||||
BOOST_AUTO_TEST_CASE(DoS_bantime)
|
||||
{
|
||||
const CChainParams& chainparams = Params();
|
||||
auto banman = std::make_unique<BanMan>(m_args.GetDataDirBase() / "banlist", nullptr, DEFAULT_MISBEHAVING_BANTIME);
|
||||
auto connman = std::make_unique<CConnman>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman);
|
||||
auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, banman.get(),
|
||||
auto peerLogic = PeerManager::make(*connman, *m_node.addrman, banman.get(),
|
||||
*m_node.chainman, *m_node.mempool, false);
|
||||
|
||||
banman->ClearBanned();
|
||||
|
|
|
@ -97,7 +97,7 @@ void Finish(FuzzedDataProvider& fuzzed_data_provider, MockedTxPool& tx_pool, CCh
|
|||
BlockAssembler::Options options;
|
||||
options.nBlockMaxWeight = fuzzed_data_provider.ConsumeIntegralInRange(0U, MAX_BLOCK_WEIGHT);
|
||||
options.blockMinFeeRate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /*max=*/COIN)};
|
||||
auto assembler = BlockAssembler{chainstate, *static_cast<CTxMemPool*>(&tx_pool), chainstate.m_params, options};
|
||||
auto assembler = BlockAssembler{chainstate, *static_cast<CTxMemPool*>(&tx_pool), options};
|
||||
auto block_template = assembler.CreateNewBlock(CScript{} << OP_TRUE);
|
||||
Assert(block_template->block.vtx.size() >= 1);
|
||||
}
|
||||
|
|
|
@ -51,7 +51,7 @@ BlockAssembler MinerTestingSetup::AssemblerForTest(const CChainParams& params)
|
|||
|
||||
options.nBlockMaxWeight = MAX_BLOCK_WEIGHT;
|
||||
options.blockMinFeeRate = blockMinFeeRate;
|
||||
return BlockAssembler(m_node.chainman->ActiveChainstate(), *m_node.mempool, params, options);
|
||||
return BlockAssembler{m_node.chainman->ActiveChainstate(), *m_node.mempool, options};
|
||||
}
|
||||
|
||||
constexpr static struct {
|
||||
|
|
|
@ -77,7 +77,7 @@ CTxIn MineBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
|
|||
std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
|
||||
{
|
||||
auto block = std::make_shared<CBlock>(
|
||||
BlockAssembler{Assert(node.chainman)->ActiveChainstate(), *Assert(node.mempool), Params()}
|
||||
BlockAssembler{Assert(node.chainman)->ActiveChainstate(), *Assert(node.mempool)}
|
||||
.CreateNewBlock(coinbase_scriptPubKey)
|
||||
->block);
|
||||
|
||||
|
|
|
@ -192,7 +192,6 @@ ChainTestingSetup::~ChainTestingSetup()
|
|||
TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const char*>& extra_args)
|
||||
: ChainTestingSetup(chainName, extra_args)
|
||||
{
|
||||
const CChainParams& chainparams = Params();
|
||||
// Ideally we'd move all the RPC tests to the functional testing framework
|
||||
// instead of unit tests, but for now we need these here.
|
||||
RegisterAllCoreRPCCommands(tableRPC);
|
||||
|
@ -201,7 +200,6 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
|
|||
*Assert(m_node.chainman.get()),
|
||||
Assert(m_node.mempool.get()),
|
||||
fPruneMode,
|
||||
chainparams.GetConsensus(),
|
||||
m_args.GetBoolArg("-reindex-chainstate", false),
|
||||
m_cache_sizes.block_tree_db,
|
||||
m_cache_sizes.coins_db,
|
||||
|
@ -214,10 +212,8 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
|
|||
*Assert(m_node.chainman),
|
||||
fReindex.load(),
|
||||
m_args.GetBoolArg("-reindex-chainstate", false),
|
||||
chainparams.GetConsensus(),
|
||||
m_args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS),
|
||||
m_args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL),
|
||||
/*get_unix_time_seconds=*/static_cast<int64_t(*)()>(GetTime));
|
||||
m_args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL));
|
||||
assert(!maybe_verify_error.has_value());
|
||||
|
||||
BlockValidationState state;
|
||||
|
@ -231,7 +227,7 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
|
|||
m_node.args->GetIntArg("-checkaddrman", 0));
|
||||
m_node.banman = std::make_unique<BanMan>(m_args.GetDataDirBase() / "banlist", nullptr, DEFAULT_MISBEHAVING_BANTIME);
|
||||
m_node.connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman); // Deterministic randomness for tests.
|
||||
m_node.peerman = PeerManager::make(chainparams, *m_node.connman, *m_node.addrman,
|
||||
m_node.peerman = PeerManager::make(*m_node.connman, *m_node.addrman,
|
||||
m_node.banman.get(), *m_node.chainman,
|
||||
*m_node.mempool, false);
|
||||
{
|
||||
|
@ -276,9 +272,8 @@ CBlock TestChain100Setup::CreateBlock(
|
|||
const CScript& scriptPubKey,
|
||||
CChainState& chainstate)
|
||||
{
|
||||
const CChainParams& chainparams = Params();
|
||||
CTxMemPool empty_pool;
|
||||
CBlock block = BlockAssembler(chainstate, empty_pool, chainparams).CreateNewBlock(scriptPubKey)->block;
|
||||
CBlock block = BlockAssembler{chainstate, empty_pool}.CreateNewBlock(scriptPubKey)->block;
|
||||
|
||||
Assert(block.vtx.size() == 1);
|
||||
for (const CMutableTransaction& tx : txns) {
|
||||
|
@ -286,7 +281,7 @@ CBlock TestChain100Setup::CreateBlock(
|
|||
}
|
||||
RegenerateCommitments(block, *Assert(m_node.chainman));
|
||||
|
||||
while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;
|
||||
while (!CheckProofOfWork(block.GetHash(), block.nBits, m_node.chainman->GetConsensus())) ++block.nNonce;
|
||||
|
||||
return block;
|
||||
}
|
||||
|
|
|
@ -65,7 +65,7 @@ std::shared_ptr<CBlock> MinerTestingSetup::Block(const uint256& prev_hash)
|
|||
static int i = 0;
|
||||
static uint64_t time = Params().GenesisBlock().nTime;
|
||||
|
||||
auto ptemplate = BlockAssembler(m_node.chainman->ActiveChainstate(), *m_node.mempool, Params()).CreateNewBlock(CScript{} << i++ << OP_TRUE);
|
||||
auto ptemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), *m_node.mempool}.CreateNewBlock(CScript{} << i++ << OP_TRUE);
|
||||
auto pblock = std::make_shared<CBlock>(ptemplate->block);
|
||||
pblock->hashPrevBlock = prev_hash;
|
||||
pblock->nTime = ++time;
|
||||
|
@ -327,7 +327,7 @@ BOOST_AUTO_TEST_CASE(witness_commitment_index)
|
|||
{
|
||||
CScript pubKey;
|
||||
pubKey << 1 << OP_TRUE;
|
||||
auto ptemplate = BlockAssembler(m_node.chainman->ActiveChainstate(), *m_node.mempool, Params()).CreateNewBlock(pubKey);
|
||||
auto ptemplate = BlockAssembler{m_node.chainman->ActiveChainstate(), *m_node.mempool}.CreateNewBlock(pubKey);
|
||||
CBlock pblock = ptemplate->block;
|
||||
|
||||
CTxOut witness;
|
||||
|
|
Loading…
Add table
Reference in a new issue