diff options
40 files changed, 278 insertions, 192 deletions
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ae2379fbd5..5cd4715ef0 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -57,8 +57,8 @@ Communication Channels ---------------------- Most communication about Bitcoin Core development happens on IRC, in the -`#bitcoin-core-dev` channel on Freenode. The easiest way to participate on IRC is -with the web client, [webchat.freenode.net](https://webchat.freenode.net/). Chat +`#bitcoin-core-dev` channel on Libera Chat. The easiest way to participate on IRC is +with the web client, [web.libera.chat](https://web.libera.chat/#bitcoin-core-dev). Chat history logs can be found on [http://www.erisian.com.au/bitcoin-core-dev/](http://www.erisian.com.au/bitcoin-core-dev/) and [http://gnusha.org/bitcoin-core-dev/](http://gnusha.org/bitcoin-core-dev/). diff --git a/contrib/debian/copyright b/contrib/debian/copyright index bc5535b4c7..6d23f600c3 100644 --- a/contrib/debian/copyright +++ b/contrib/debian/copyright @@ -1,7 +1,7 @@ Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ Upstream-Name: Bitcoin Upstream-Contact: Satoshi Nakamoto <satoshin@gmx.com> - irc://#bitcoin@freenode.net + irc://#bitcoin-core-dev@libera.chat Source: https://github.com/bitcoin/bitcoin Files: * diff --git a/doc/README.md b/doc/README.md index f32600d009..c629c2ccfa 100644 --- a/doc/README.md +++ b/doc/README.md @@ -30,6 +30,7 @@ Drag Bitcoin Core to your applications folder, and then run Bitcoin Core. * See the documentation at the [Bitcoin Wiki](https://en.bitcoin.it/wiki/Main_Page) for help and more information. +* Ask for help on the [Bitcoin StackExchange](https://bitcoin.stackexchange.com) * Ask for help on [#bitcoin](https://webchat.freenode.net/#bitcoin) on Freenode. If you don't have an IRC client, use [webchat here](https://webchat.freenode.net/#bitcoin). * Ask for help on the [BitcoinTalk](https://bitcointalk.org/) forums, in the [Technical Support board](https://bitcointalk.org/index.php?board=4.0). @@ -67,7 +68,7 @@ The Bitcoin repo's [root README](/README.md) contains relevant information on th ### Resources * Discuss on the [BitcoinTalk](https://bitcointalk.org/) forums, in the [Development & Technical Discussion board](https://bitcointalk.org/index.php?board=6.0). -* Discuss project-specific development on #bitcoin-core-dev on Freenode. If you don't have an IRC client, use [webchat here](https://webchat.freenode.net/#bitcoin-core-dev). +* Discuss project-specific development on #bitcoin-core-dev on Libera Chat. If you don't have an IRC client, use [webchat here](https://web.libera.chat/#bitcoin-core-dev). * Discuss general Bitcoin development on #bitcoin-dev on Freenode. If you don't have an IRC client, use [webchat here](https://webchat.freenode.net/#bitcoin-dev). ### Miscellaneous diff --git a/doc/release-process.md b/doc/release-process.md index 84b208a0d8..3ead1181b9 100644 --- a/doc/release-process.md +++ b/doc/release-process.md @@ -373,9 +373,7 @@ bitcoin.org (see below for bitcoin.org update instructions). - Bitcoin Core announcements list https://bitcoincore.org/en/list/announcements/join/ - - Update title of #bitcoin on Freenode IRC - - - Optionally twitter, reddit /r/Bitcoin, ... but this will usually sort out itself + - Bitcoin Core Twitter https://twitter.com/bitcoincoreorg - Celebrate diff --git a/doc/translation_process.md b/doc/translation_process.md index 785bb0047b..f132693264 100644 --- a/doc/translation_process.md +++ b/doc/translation_process.md @@ -102,6 +102,5 @@ To create a new language template, you will need to edit the languages manifest **Note:** that the language translation file **must end in `.qm`** (the compiled extension), and not `.ts`. ### Questions and general assistance -The Bitcoin-Core translation maintainers include *tcatm, seone, Diapolo, wumpus and luke-jr*. You can find them, and others, in the Freenode IRC chatroom - `irc.freenode.net #bitcoin-core-dev`. If you are a translator, you should also subscribe to the mailing list, https://groups.google.com/forum/#!forum/bitcoin-translators. Announcements will be posted during application pre-releases to notify translators to check for updates. diff --git a/src/addrman.cpp b/src/addrman.cpp index ceab1689d7..14b412a038 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -36,7 +36,7 @@ int CAddrInfo::GetNewBucket(const uint256& nKey, const CNetAddr& src, const std: int CAddrInfo::GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const { - uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << (fNew ? 'N' : 'K') << nBucket << GetKey()).GetCheapHash(); + uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << (fNew ? uint8_t{'N'} : uint8_t{'K'}) << nBucket << GetKey()).GetCheapHash(); return hash1 % ADDRMAN_BUCKET_SIZE; } @@ -652,7 +652,7 @@ std::vector<bool> CAddrMan::DecodeAsmap(fs::path path) int length = ftell(filestr); LogPrintf("Opened asmap file %s (%d bytes) from disk\n", path, length); fseek(filestr, 0, SEEK_SET); - char cur_byte; + uint8_t cur_byte; for (int i = 0; i < length; ++i) { file >> cur_byte; for (int bit = 0; bit < 8; ++bit) { diff --git a/src/bench/block_assemble.cpp b/src/bench/block_assemble.cpp index aa72981cb4..b4b33d115f 100644 --- a/src/bench/block_assemble.cpp +++ b/src/bench/block_assemble.cpp @@ -37,7 +37,7 @@ static void AssembleBlock(benchmark::Bench& bench) LOCK(::cs_main); // Required for ::AcceptToMemoryPool. for (const auto& txr : txs) { - const MempoolAcceptResult res = ::AcceptToMemoryPool(::ChainstateActive(), *test_setup->m_node.mempool, txr, false /* bypass_limits */); + const MempoolAcceptResult res = ::AcceptToMemoryPool(test_setup->m_node.chainman->ActiveChainstate(), *test_setup->m_node.mempool, txr, false /* bypass_limits */); assert(res.m_result_type == MempoolAcceptResult::ResultType::VALID); } } diff --git a/src/bench/duplicate_inputs.cpp b/src/bench/duplicate_inputs.cpp index 25d1a2b56c..4f6e1122b8 100644 --- a/src/bench/duplicate_inputs.cpp +++ b/src/bench/duplicate_inputs.cpp @@ -25,7 +25,8 @@ static void DuplicateInputs(benchmark::Bench& bench) CMutableTransaction naughtyTx{}; LOCK(cs_main); - CBlockIndex* pindexPrev = ::ChainActive().Tip(); + assert(std::addressof(::ChainActive()) == std::addressof(testing_setup->m_node.chainman->ActiveChain())); + CBlockIndex* pindexPrev = testing_setup->m_node.chainman->ActiveChain().Tip(); assert(pindexPrev != nullptr); block.nBits = GetNextWorkRequired(pindexPrev, &block, chainparams.GetConsensus()); block.nNonce = 0; diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index 4a4710ea3a..c5840b2098 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -418,7 +418,7 @@ private: if (conn_type == "addr-fetch") return "addr"; return ""; } - const int64_t m_time_now{GetSystemTimeInSeconds()}; + const int64_t m_time_now{GetTimeSeconds()}; public: static constexpr int ID_PEERINFO = 0; diff --git a/src/index/base.cpp b/src/index/base.cpp index 4079fc4569..3a61af28b7 100644 --- a/src/index/base.cpp +++ b/src/index/base.cpp @@ -60,33 +60,34 @@ bool BaseIndex::Init() } LOCK(cs_main); + CChain& active_chain = m_chainstate->m_chain; if (locator.IsNull()) { m_best_block_index = nullptr; } else { - m_best_block_index = g_chainman.m_blockman.FindForkInGlobalIndex(::ChainActive(), locator); + m_best_block_index = m_chainstate->m_blockman.FindForkInGlobalIndex(active_chain, locator); } - m_synced = m_best_block_index.load() == ::ChainActive().Tip(); + m_synced = m_best_block_index.load() == active_chain.Tip(); if (!m_synced) { bool prune_violation = false; if (!m_best_block_index) { // index is not built yet // make sure we have all block data back to the genesis - const CBlockIndex* block = ::ChainActive().Tip(); + const CBlockIndex* block = active_chain.Tip(); while (block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) { block = block->pprev; } - prune_violation = block != ::ChainActive().Genesis(); + prune_violation = block != active_chain.Genesis(); } // in case the index has a best block set and is not fully synced // check if we have the required blocks to continue building the index else { const CBlockIndex* block_to_test = m_best_block_index.load(); - if (!ChainActive().Contains(block_to_test)) { + if (!active_chain.Contains(block_to_test)) { // if the bestblock is not part of the mainchain, find the fork // and make sure we have all data down to the fork - block_to_test = ::ChainActive().FindFork(block_to_test); + block_to_test = active_chain.FindFork(block_to_test); } - const CBlockIndex* block = ::ChainActive().Tip(); + const CBlockIndex* block = active_chain.Tip(); prune_violation = true; // check backwards from the tip if we have all block data until we reach the indexes bestblock while (block_to_test && block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) { @@ -104,20 +105,20 @@ bool BaseIndex::Init() return true; } -static const CBlockIndex* NextSyncBlock(const CBlockIndex* pindex_prev) EXCLUSIVE_LOCKS_REQUIRED(cs_main) +static const CBlockIndex* NextSyncBlock(const CBlockIndex* pindex_prev, CChain& chain) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { AssertLockHeld(cs_main); if (!pindex_prev) { - return ::ChainActive().Genesis(); + return chain.Genesis(); } - const CBlockIndex* pindex = ::ChainActive().Next(pindex_prev); + const CBlockIndex* pindex = chain.Next(pindex_prev); if (pindex) { return pindex; } - return ::ChainActive().Next(::ChainActive().FindFork(pindex_prev)); + return chain.Next(chain.FindFork(pindex_prev)); } void BaseIndex::ThreadSync() @@ -140,7 +141,7 @@ void BaseIndex::ThreadSync() { LOCK(cs_main); - const CBlockIndex* pindex_next = NextSyncBlock(pindex); + const CBlockIndex* pindex_next = NextSyncBlock(pindex, m_chainstate->m_chain); if (!pindex_next) { m_best_block_index = pindex; m_synced = true; @@ -203,7 +204,7 @@ bool BaseIndex::Commit() bool BaseIndex::CommitInternal(CDBBatch& batch) { LOCK(cs_main); - GetDB().WriteBestBlock(batch, ::ChainActive().GetLocator(m_best_block_index)); + GetDB().WriteBestBlock(batch, m_chainstate->m_chain.GetLocator(m_best_block_index)); return true; } @@ -279,7 +280,7 @@ void BaseIndex::ChainStateFlushed(const CBlockLocator& locator) const CBlockIndex* locator_tip_index; { LOCK(cs_main); - locator_tip_index = g_chainman.m_blockman.LookupBlockIndex(locator_tip_hash); + locator_tip_index = m_chainstate->m_blockman.LookupBlockIndex(locator_tip_hash); } if (!locator_tip_index) { @@ -320,7 +321,7 @@ bool BaseIndex::BlockUntilSyncedToCurrentChain() const // Skip the queue-draining stuff if we know we're caught up with // ::ChainActive().Tip(). LOCK(cs_main); - const CBlockIndex* chain_tip = ::ChainActive().Tip(); + const CBlockIndex* chain_tip = m_chainstate->m_chain.Tip(); const CBlockIndex* best_block_index = m_best_block_index.load(); if (best_block_index->GetAncestor(chain_tip->nHeight) == chain_tip) { return true; @@ -337,8 +338,10 @@ void BaseIndex::Interrupt() m_interrupt(); } -bool BaseIndex::Start() +bool BaseIndex::Start(CChainState& active_chainstate) { + assert(std::addressof(::ChainstateActive()) == std::addressof(active_chainstate)); + m_chainstate = &active_chainstate; // Need to register this ValidationInterface before running Init(), so that // callbacks are not missed if Init sets m_synced to true. RegisterValidationInterface(this); diff --git a/src/index/base.h b/src/index/base.h index 59eefab29e..df4bdff1ea 100644 --- a/src/index/base.h +++ b/src/index/base.h @@ -12,6 +12,7 @@ #include <validationinterface.h> class CBlockIndex; +class CChainState; struct IndexSummary { std::string name; @@ -75,8 +76,9 @@ private: /// to a chain reorganization), the index must halt until Commit succeeds or else it could end up /// getting corrupted. bool Commit(); - protected: + CChainState* m_chainstate{nullptr}; + void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override; void ChainStateFlushed(const CBlockLocator& locator) override; @@ -117,7 +119,7 @@ public: /// Start initializes the sync state and registers the instance as a /// ValidationInterface so that it stays in sync with blockchain updates. - [[nodiscard]] bool Start(); + [[nodiscard]] bool Start(CChainState& active_chainstate); /// Stops the instance from staying in sync with blockchain updates. void Stop(); diff --git a/src/index/coinstatsindex.cpp b/src/index/coinstatsindex.cpp index e046527283..8c6046489b 100644 --- a/src/index/coinstatsindex.cpp +++ b/src/index/coinstatsindex.cpp @@ -266,7 +266,7 @@ bool CoinStatsIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* n { LOCK(cs_main); - CBlockIndex* iter_tip{g_chainman.m_blockman.LookupBlockIndex(current_tip->GetBlockHash())}; + CBlockIndex* iter_tip{m_chainstate->m_blockman.LookupBlockIndex(current_tip->GetBlockHash())}; const auto& consensus_params{Params().GetConsensus()}; do { diff --git a/src/index/txindex.cpp b/src/index/txindex.cpp index d9e437ad10..782e557478 100644 --- a/src/index/txindex.cpp +++ b/src/index/txindex.cpp @@ -204,7 +204,7 @@ bool TxIndex::Init() // Attempt to migrate txindex from the old database to the new one. Even if // chain_tip is null, the node could be reindexing and we still want to // delete txindex records in the old database. - if (!m_db->MigrateData(*pblocktree, ::ChainActive().GetLocator())) { + if (!m_db->MigrateData(*pblocktree, m_chainstate->m_chain.GetLocator())) { return false; } diff --git a/src/init.cpp b/src/init.cpp index a835df0572..7f64b1acfa 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1549,21 +1549,21 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) // ********************************************************* Step 8: start indexers if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) { g_txindex = std::make_unique<TxIndex>(nTxIndexCache, false, fReindex); - if (!g_txindex->Start()) { + if (!g_txindex->Start(::ChainstateActive())) { return false; } } for (const auto& filter_type : g_enabled_filter_types) { InitBlockFilterIndex(filter_type, filter_index_cache, false, fReindex); - if (!GetBlockFilterIndex(filter_type)->Start()) { + if (!GetBlockFilterIndex(filter_type)->Start(::ChainstateActive())) { return false; } } if (args.GetBoolArg("-coinstatsindex", DEFAULT_COINSTATSINDEX)) { g_coin_stats_index = std::make_unique<CoinStatsIndex>(/* cache size */ 0, false, fReindex); - if (!g_coin_stats_index->Start()) { + if (!g_coin_stats_index->Start(::ChainstateActive())) { return false; } } diff --git a/src/miner.cpp b/src/miner.cpp index 3bc7fdd458..eccddbb04f 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -39,13 +39,21 @@ int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParam return nNewTime - nOldTime; } -void RegenerateCommitments(CBlock& block, CBlockIndex* prev_block) +void RegenerateCommitments(CBlock& block, ChainstateManager& chainman) { CMutableTransaction tx{*block.vtx.at(0)}; tx.vout.erase(tx.vout.begin() + GetWitnessCommitmentIndex(block)); block.vtx.at(0) = MakeTransactionRef(tx); - WITH_LOCK(::cs_main, assert(g_chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock) == prev_block)); + CBlockIndex* prev_block; + { + // TODO: Temporary scope to check correctness of refactored code. + // Should be removed manually after merge of + // https://github.com/bitcoin/bitcoin/pull/20158 + LOCK(::cs_main); + assert(std::addressof(g_chainman.m_blockman) == std::addressof(chainman.m_blockman)); + prev_block = chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock); + } GenerateCoinbaseCommitment(block, prev_block, Params().GetConsensus()); block.hashMerkleRoot = BlockMerkleRoot(block); diff --git a/src/miner.h b/src/miner.h index becf362b79..10a80f4392 100644 --- a/src/miner.h +++ b/src/miner.h @@ -203,6 +203,6 @@ void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev); /** Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed */ -void RegenerateCommitments(CBlock& block, CBlockIndex* prev_block); +void RegenerateCommitments(CBlock& block, ChainstateManager& chainman); #endif // BITCOIN_MINER_H diff --git a/src/net.cpp b/src/net.cpp index 1b6f04dead..6f9f17ed4e 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -796,7 +796,7 @@ size_t CConnman::SocketSendData(CNode& node) const nBytes = send(node.hSocket, reinterpret_cast<const char*>(data.data()) + node.nSendOffset, data.size() - node.nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT); } if (nBytes > 0) { - node.nLastSend = GetSystemTimeInSeconds(); + node.nLastSend = GetTimeSeconds(); node.nSendBytes += nBytes; node.nSendOffset += nBytes; nSentSize += nBytes; @@ -1251,7 +1251,7 @@ void CConnman::NotifyNumConnectionsChanged() bool CConnman::ShouldRunInactivityChecks(const CNode& node, std::optional<int64_t> now_in) const { - const int64_t now = now_in ? now_in.value() : GetSystemTimeInSeconds(); + const int64_t now = now_in ? now_in.value() : GetTimeSeconds(); return node.nTimeConnected + m_peer_connect_timeout < now; } @@ -1259,7 +1259,7 @@ bool CConnman::InactivityCheck(const CNode& node) const { // Use non-mockable system time (otherwise these timers will pop when we // use setmocktime in the tests). - int64_t now = GetSystemTimeInSeconds(); + int64_t now = GetTimeSeconds(); if (!ShouldRunInactivityChecks(node, now)) return false; @@ -2912,7 +2912,7 @@ ServiceFlags CConnman::GetLocalServices() const unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; } CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress& addrBindIn, const std::string& addrNameIn, ConnectionType conn_type_in, bool inbound_onion) - : nTimeConnected(GetSystemTimeInSeconds()), + : nTimeConnected(GetTimeSeconds()), addr(addrIn), addrBind(addrBindIn), m_inbound_onion(inbound_onion), @@ -3048,7 +3048,7 @@ void CaptureMessage(const CAddress& addr, const std::string& msg_type, const Spa ser_writedata64(f, now.count()); f.write(msg_type.data(), msg_type.length()); for (auto i = msg_type.length(); i < CMessageHeader::COMMAND_SIZE; ++i) { - f << '\0'; + f << uint8_t{'\0'}; } uint32_t size = data.size(); ser_writedata32(f, size); diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index 100022bac1..e5dde88bd1 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -43,6 +43,7 @@ #include <QAction> #include <QApplication> #include <QComboBox> +#include <QCursor> #include <QDateTime> #include <QDragEnterEvent> #include <QListWidget> @@ -199,9 +200,6 @@ BitcoinGUI::BitcoinGUI(interfaces::Node& node, const PlatformStyle *_platformSty // Subscribe to notifications from core subscribeToCoreSignals(); - connect(connectionsControl, &GUIUtil::ClickableLabel::clicked, [this] { - m_node.setNetworkActive(!m_node.getNetworkActive()); - }); connect(labelProxyIcon, &GUIUtil::ClickableLabel::clicked, [this] { openOptionsDialogWithTab(OptionsDialog::TAB_NETWORK); }); @@ -586,7 +584,10 @@ void BitcoinGUI::setClientModel(ClientModel *_clientModel, interfaces::BlockAndH createTrayIconMenu(); // Keep up to date with client - updateNetworkState(); + setNetworkActive(m_node.getNetworkActive()); + connect(connectionsControl, &GUIUtil::ClickableLabel::clicked, [this] { + GUIUtil::PopupMenu(m_network_context_menu, QCursor::pos()); + }); connect(_clientModel, &ClientModel::numConnectionsChanged, this, &BitcoinGUI::setNumConnections); connect(_clientModel, &ClientModel::networkActiveChanged, this, &BitcoinGUI::setNetworkActive); @@ -915,14 +916,18 @@ void BitcoinGUI::updateNetworkState() QString tooltip; if (m_node.getNetworkActive()) { - tooltip = tr("%n active connection(s) to Bitcoin network", "", count) + QString(".<br>") + tr("Click to disable network activity."); + //: A substring of the tooltip. + tooltip = tr("%n active connection(s) to Bitcoin network.", "", count); } else { - tooltip = tr("Network activity disabled.") + QString("<br>") + tr("Click to enable network activity again."); + //: A substring of the tooltip. + tooltip = tr("Network activity disabled."); icon = ":/icons/network_disabled"; } // Don't word-wrap this (fixed-width) tooltip - tooltip = QString("<nobr>") + tooltip + QString("</nobr>"); + tooltip = QLatin1String("<nobr>") + tooltip + QLatin1String("<br>") + + //: A substring of the tooltip. "More actions" are available via the context menu. + tr("Click for more actions.") + QLatin1String("</nobr>"); connectionsControl->setToolTip(tooltip); connectionsControl->setThemedPixmap(icon, STATUSBAR_ICONSIZE, STATUSBAR_ICONSIZE); @@ -933,9 +938,24 @@ void BitcoinGUI::setNumConnections(int count) updateNetworkState(); } -void BitcoinGUI::setNetworkActive(bool networkActive) +void BitcoinGUI::setNetworkActive(bool network_active) { updateNetworkState(); + m_network_context_menu->clear(); + m_network_context_menu->addAction( + //: A context menu item. The "Peers tab" is an element of the "Node window". + tr("Show Peers tab"), + [this] { + rpcConsole->setTabFocus(RPCConsole::TabTypes::PEERS); + showDebugWindow(); + }); + m_network_context_menu->addAction( + network_active ? + //: A context menu item. + tr("Disable network activity") : + //: A context menu item. The network activity was disabled previously. + tr("Enable network activity"), + [this, new_state = !network_active] { m_node.setNetworkActive(new_state); }); } void BitcoinGUI::updateHeadersSyncProgressLabel() diff --git a/src/qt/bitcoingui.h b/src/qt/bitcoingui.h index 65c5331d32..c83cd446a0 100644 --- a/src/qt/bitcoingui.h +++ b/src/qt/bitcoingui.h @@ -17,6 +17,7 @@ #include <QLabel> #include <QMainWindow> #include <QMap> +#include <QMenu> #include <QPoint> #include <QSystemTrayIcon> @@ -51,7 +52,6 @@ QT_BEGIN_NAMESPACE class QAction; class QComboBox; class QDateTime; -class QMenu; class QProgressBar; class QProgressDialog; QT_END_NAMESPACE @@ -175,6 +175,8 @@ private: HelpMessageDialog* helpMessageDialog = nullptr; ModalOverlay* modalOverlay = nullptr; + QMenu* m_network_context_menu = new QMenu(this); + #ifdef Q_OS_MAC CAppNapInhibitor* m_app_nap_inhibitor = nullptr; #endif @@ -222,7 +224,7 @@ public Q_SLOTS: /** Set number of connections shown in the UI */ void setNumConnections(int count); /** Set network state shown in the UI */ - void setNetworkActive(bool networkActive); + void setNetworkActive(bool network_active); /** Set number of blocks and last block date shown in the UI */ void setNumBlocks(int count, const QDateTime& blockDate, double nVerificationProgress, bool headers, SynchronizationState sync_state); diff --git a/src/qt/rpcconsole.cpp b/src/qt/rpcconsole.cpp index 83a111e9c8..548b8cb34d 100644 --- a/src/qt/rpcconsole.cpp +++ b/src/qt/rpcconsole.cpp @@ -816,23 +816,29 @@ void RPCConsole::clear(bool keep_prompt) ).arg(fixedFontInfo.family(), QString("%1pt").arg(consoleFontSize)) ); - message(CMD_REPLY, - tr("Welcome to the %1 RPC console.").arg(PACKAGE_NAME) + - "<br>" + - tr("Use up and down arrows to navigate history, and %1 to clear screen.") - .arg("<b>" + ui->clearButton->shortcut().toString(QKeySequence::NativeText) + "</b>") + - "<br>" + - tr("Use %1 and %2 to increase or decrease the font size.") - .arg("<b>" + ui->fontBiggerButton->shortcut().toString(QKeySequence::NativeText) + "</b>") - .arg("<b>" + ui->fontSmallerButton->shortcut().toString(QKeySequence::NativeText) + "</b>") + - "<br>" + - tr("Type %1 for an overview of available commands.").arg("<b>help</b>") + - "<br>" + - tr("For more information on using this console type %1.").arg("<b>help-console</b>") + - "<br><span class=\"secwarning\"><br>" + - tr("WARNING: Scammers have been active, telling users to type commands here, stealing their wallet contents. Do not use this console without fully understanding the ramifications of a command.") + - "</span>", - true); + static const QString welcome_message = + /*: RPC console welcome message. + Placeholders %7 and %8 are style tags for the warning content, and + they are not space separated from the rest of the text intentionally. */ + tr("Welcome to the %1 RPC console.\n" + "Use up and down arrows to navigate history, and %2 to clear screen.\n" + "Use %3 and %4 to increase or decrease the font size.\n" + "Type %5 for an overview of available commands.\n" + "For more information on using this console, type %6.\n" + "\n" + "%7WARNING: Scammers have been active, telling users to type" + " commands here, stealing their wallet contents. Do not use this console" + " without fully understanding the ramifications of a command.%8") + .arg(PACKAGE_NAME, + "<b>" + ui->clearButton->shortcut().toString(QKeySequence::NativeText) + "</b>", + "<b>" + ui->fontBiggerButton->shortcut().toString(QKeySequence::NativeText) + "</b>", + "<b>" + ui->fontSmallerButton->shortcut().toString(QKeySequence::NativeText) + "</b>", + "<b>help</b>", + "<b>help-console</b>", + "<span class=\"secwarning\">", + "<span>"); + + message(CMD_REPLY, welcome_message, true); } void RPCConsole::keyPressEvent(QKeyEvent *event) @@ -924,57 +930,71 @@ void RPCConsole::setMempoolSize(long numberOfTxs, size_t dynUsage) void RPCConsole::on_lineEdit_returnPressed() { - QString cmd = ui->lineEdit->text(); + QString cmd = ui->lineEdit->text().trimmed(); - if(!cmd.isEmpty()) - { - std::string strFilteredCmd; - try { - std::string dummy; - if (!RPCParseCommandLine(nullptr, dummy, cmd.toStdString(), false, &strFilteredCmd)) { - // Failed to parse command, so we cannot even filter it for the history - throw std::runtime_error("Invalid command line"); - } - } catch (const std::exception& e) { - QMessageBox::critical(this, "Error", QString("Error: ") + QString::fromStdString(e.what())); - return; + if (cmd.isEmpty()) { + return; + } + + std::string strFilteredCmd; + try { + std::string dummy; + if (!RPCParseCommandLine(nullptr, dummy, cmd.toStdString(), false, &strFilteredCmd)) { + // Failed to parse command, so we cannot even filter it for the history + throw std::runtime_error("Invalid command line"); } + } catch (const std::exception& e) { + QMessageBox::critical(this, "Error", QString("Error: ") + QString::fromStdString(e.what())); + return; + } + + // A special case allows to request shutdown even a long-running command is executed. + if (cmd == QLatin1String("stop")) { + std::string dummy; + RPCExecuteCommandLine(m_node, dummy, cmd.toStdString()); + return; + } - ui->lineEdit->clear(); + if (m_is_executing) { + return; + } - cmdBeforeBrowsing = QString(); + ui->lineEdit->clear(); #ifdef ENABLE_WALLET - WalletModel* wallet_model = ui->WalletSelector->currentData().value<WalletModel*>(); + WalletModel* wallet_model = ui->WalletSelector->currentData().value<WalletModel*>(); - if (m_last_wallet_model != wallet_model) { - if (wallet_model) { - message(CMD_REQUEST, tr("Executing command using \"%1\" wallet").arg(wallet_model->getWalletName())); - } else { - message(CMD_REQUEST, tr("Executing command without any wallet")); - } - m_last_wallet_model = wallet_model; + if (m_last_wallet_model != wallet_model) { + if (wallet_model) { + message(CMD_REQUEST, tr("Executing command using \"%1\" wallet").arg(wallet_model->getWalletName())); + } else { + message(CMD_REQUEST, tr("Executing command without any wallet")); } -#endif - - message(CMD_REQUEST, QString::fromStdString(strFilteredCmd)); - Q_EMIT cmdRequest(cmd, m_last_wallet_model); - - cmd = QString::fromStdString(strFilteredCmd); - - // Remove command, if already in history - history.removeOne(cmd); - // Append command to history - history.append(cmd); - // Enforce maximum history size - while(history.size() > CONSOLE_HISTORY) - history.removeFirst(); - // Set pointer to end of history - historyPtr = history.size(); + m_last_wallet_model = wallet_model; + } +#endif // ENABLE_WALLET - // Scroll console view to end - scrollToEnd(); + message(CMD_REQUEST, QString::fromStdString(strFilteredCmd)); + //: A console message indicating an entered command is currently being executed. + message(CMD_REPLY, tr("Executing…")); + m_is_executing = true; + Q_EMIT cmdRequest(cmd, m_last_wallet_model); + + cmd = QString::fromStdString(strFilteredCmd); + + // Remove command, if already in history + history.removeOne(cmd); + // Append command to history + history.append(cmd); + // Enforce maximum history size + while (history.size() > CONSOLE_HISTORY) { + history.removeFirst(); } + // Set pointer to end of history + historyPtr = history.size(); + + // Scroll console view to end + scrollToEnd(); } void RPCConsole::browseHistory(int offset) @@ -1004,7 +1024,13 @@ void RPCConsole::startExecutor() executor->moveToThread(&thread); // Replies from executor object must go to this object - connect(executor, &RPCExecutor::reply, this, qOverload<int, const QString&>(&RPCConsole::message)); + connect(executor, &RPCExecutor::reply, this, [this](int category, const QString& command) { + // Remove "Executing…" message. + ui->messagesWidget->undo(); + message(category, command); + scrollToEnd(); + m_is_executing = false; + }); // Requests from this object must go to executor connect(this, &RPCConsole::cmdRequest, executor, &RPCExecutor::request); @@ -1079,7 +1105,7 @@ void RPCConsole::updateDetailWidget() if (stats->nodeStats.m_bip152_highbandwidth_from) bip152_hb_settings += (bip152_hb_settings.isEmpty() ? ts.from : QLatin1Char('/') + ts.from); if (bip152_hb_settings.isEmpty()) bip152_hb_settings = ts.no; ui->peerHighBandwidth->setText(bip152_hb_settings); - const int64_t time_now{GetSystemTimeInSeconds()}; + const int64_t time_now{GetTimeSeconds()}; ui->peerConnTime->setText(GUIUtil::formatDurationStr(time_now - stats->nodeStats.nTimeConnected)); ui->peerLastBlock->setText(TimeDurationField(time_now, stats->nodeStats.nLastBlockTime)); ui->peerLastTx->setText(TimeDurationField(time_now, stats->nodeStats.nLastTXTime)); diff --git a/src/qt/rpcconsole.h b/src/qt/rpcconsole.h index 55a1decceb..75f466642b 100644 --- a/src/qt/rpcconsole.h +++ b/src/qt/rpcconsole.h @@ -166,6 +166,7 @@ private: QCompleter *autoCompleter = nullptr; QThread thread; WalletModel* m_last_wallet_model{nullptr}; + bool m_is_executing{false}; /** Update UI with latest network info from model. */ void updateNetworkState(); diff --git a/src/qt/test/apptests.cpp b/src/qt/test/apptests.cpp index c1d5f84be5..cb3dbd2267 100644 --- a/src/qt/test/apptests.cpp +++ b/src/qt/test/apptests.cpp @@ -40,7 +40,7 @@ void TestRpcCommand(RPCConsole* console) QTest::keyClicks(lineEdit, "getblockchaininfo"); QTest::keyClick(lineEdit, Qt::Key_Return); QVERIFY(mw_spy.wait(1000)); - QCOMPARE(mw_spy.count(), 2); + QCOMPARE(mw_spy.count(), 4); QString output = messagesWidget->toPlainText(); UniValue value; value.read(output.right(output.size() - output.lastIndexOf(QChar::ObjectReplacementCharacter) - 1).toStdString()); diff --git a/src/rest.cpp b/src/rest.cpp index d41f374c49..747c7aea19 100644 --- a/src/rest.cpp +++ b/src/rest.cpp @@ -107,6 +107,27 @@ static CTxMemPool* GetMemPool(const std::any& context, HTTPRequest* req) return node_context->mempool.get(); } +/** + * Get the node context chainstatemanager. + * + * @param[in] req The HTTP request, whose status code will be set if node + * context chainstatemanager is not found. + * @returns Pointer to the chainstatemanager or nullptr if none found. + */ +static ChainstateManager* GetChainman(const std::any& context, HTTPRequest* req) +{ + auto node_context = util::AnyPtr<NodeContext>(context); + if (!node_context || !node_context->chainman) { + RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, + strprintf("%s:%d (%s)\n" + "Internal bug detected: Chainman disabled or instance not found!\n" + "You may report this issue here: %s\n", + __FILE__, __LINE__, __func__, PACKAGE_BUGREPORT)); + return nullptr; + } + return node_context->chainman; +} + static RetFormat ParseDataFormat(std::string& param, const std::string& strReq) { const std::string::size_type pos = strReq.rfind('.'); @@ -181,7 +202,9 @@ static bool rest_headers(const std::any& context, std::vector<const CBlockIndex *> headers; headers.reserve(count); { - ChainstateManager& chainman = EnsureAnyChainman(context); + ChainstateManager* maybe_chainman = GetChainman(context, req); + if (!maybe_chainman) return false; + ChainstateManager& chainman = *maybe_chainman; LOCK(cs_main); CChain& active_chain = chainman.ActiveChain(); tip = active_chain.Tip(); @@ -252,7 +275,9 @@ static bool rest_block(const std::any& context, CBlockIndex* pblockindex = nullptr; CBlockIndex* tip = nullptr; { - ChainstateManager& chainman = EnsureAnyChainman(context); + ChainstateManager* maybe_chainman = GetChainman(context, req); + if (!maybe_chainman) return false; + ChainstateManager& chainman = *maybe_chainman; LOCK(cs_main); tip = chainman.ActiveChain().Tip(); pblockindex = chainman.m_blockman.LookupBlockIndex(hash); @@ -541,7 +566,9 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std:: std::string bitmapStringRepresentation; std::vector<bool> hits; bitmap.resize((vOutPoints.size() + 7) / 8); - ChainstateManager& chainman = EnsureAnyChainman(context); + ChainstateManager* maybe_chainman = GetChainman(context, req); + if (!maybe_chainman) return false; + ChainstateManager& chainman = *maybe_chainman; { auto process_utxos = [&vOutPoints, &outs, &hits](const CCoinsView& view, const CTxMemPool& mempool) { for (const COutPoint& vOutPoint : vOutPoints) { @@ -644,7 +671,9 @@ static bool rest_blockhash_by_height(const std::any& context, HTTPRequest* req, CBlockIndex* pblockindex = nullptr; { - ChainstateManager& chainman = EnsureAnyChainman(context); + ChainstateManager* maybe_chainman = GetChainman(context, req); + if (!maybe_chainman) return false; + ChainstateManager& chainman = *maybe_chainman; LOCK(cs_main); const CChain& active_chain = chainman.ActiveChain(); if (blockheight > active_chain.Height()) { diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 03f28239ba..83c1975d38 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1196,7 +1196,7 @@ static RPCHelpMan gettxoutsetinfo() CCoinsStats prev_stats{hash_type}; if (pindex->nHeight > 0) { - GetUTXOStats(coins_view, WITH_LOCK(::cs_main, return std::ref(g_chainman.m_blockman)), prev_stats, node.rpc_interruption_point, pindex->pprev); + GetUTXOStats(coins_view, *blockman, prev_stats, node.rpc_interruption_point, pindex->pprev); } UniValue block_info(UniValue::VOBJ); diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 8190a2f006..6826e6fd07 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -378,8 +378,7 @@ static RPCHelpMan generateblock() // Add transactions block.vtx.insert(block.vtx.end(), txs.begin(), txs.end()); - CBlockIndex* prev_block = WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock)); - RegenerateCommitments(block, prev_block); + RegenerateCommitments(block, chainman); { LOCK(cs_main); diff --git a/src/serialize.h b/src/serialize.h index 5ef846b9e9..edf10440c6 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -226,12 +226,8 @@ template<typename Stream, int N> inline void Unserialize(Stream& s, char (&a)[N] template<typename Stream, int N> inline void Unserialize(Stream& s, unsigned char (&a)[N]) { s.read(CharCast(a), N); } template<typename Stream> inline void Unserialize(Stream& s, Span<unsigned char>& span) { s.read(CharCast(span.data()), span.size()); } -template<typename Stream> inline void Serialize(Stream& s, bool a) { char f=a; ser_writedata8(s, f); } -template<typename Stream> inline void Unserialize(Stream& s, bool& a) { char f=ser_readdata8(s); a=f; } - - - - +template <typename Stream> inline void Serialize(Stream& s, bool a) { uint8_t f = a; ser_writedata8(s, f); } +template <typename Stream> inline void Unserialize(Stream& s, bool& a) { uint8_t f = ser_readdata8(s); a = f; } /** diff --git a/src/test/blockfilter_index_tests.cpp b/src/test/blockfilter_index_tests.cpp index 1cb1c002f4..2f532ef598 100644 --- a/src/test/blockfilter_index_tests.cpp +++ b/src/test/blockfilter_index_tests.cpp @@ -131,7 +131,7 @@ BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, BuildChainTestingSetup) // BlockUntilSyncedToCurrentChain should return false before index is started. BOOST_CHECK(!filter_index.BlockUntilSyncedToCurrentChain()); - BOOST_REQUIRE(filter_index.Start()); + BOOST_REQUIRE(filter_index.Start(::ChainstateActive())); // Allow filter index to catch up with the block index. constexpr int64_t timeout_ms = 10 * 1000; diff --git a/src/test/coinstatsindex_tests.cpp b/src/test/coinstatsindex_tests.cpp index bf7a80ae5c..106fcd2a33 100644 --- a/src/test/coinstatsindex_tests.cpp +++ b/src/test/coinstatsindex_tests.cpp @@ -32,7 +32,7 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup) // is started. BOOST_CHECK(!coin_stats_index.BlockUntilSyncedToCurrentChain()); - BOOST_REQUIRE(coin_stats_index.Start()); + BOOST_REQUIRE(coin_stats_index.Start(::ChainstateActive())); // Allow the CoinStatsIndex to catch up with the block index that is syncing // in a background thread. diff --git a/src/test/dbwrapper_tests.cpp b/src/test/dbwrapper_tests.cpp index e2e7644dfa..b6f5938892 100644 --- a/src/test/dbwrapper_tests.cpp +++ b/src/test/dbwrapper_tests.cpp @@ -28,7 +28,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper) for (const bool obfuscate : {false, true}) { fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_obfuscate_true" : "dbwrapper_obfuscate_false"); CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate); - char key = 'k'; + uint8_t key{'k'}; uint256 in = InsecureRand256(); uint256 res; @@ -88,21 +88,21 @@ BOOST_AUTO_TEST_CASE(dbwrapper_basic_data) BOOST_CHECK_EQUAL(res.ToString(), in_utxo.ToString()); //Simulate last block file number - "l" - char key_last_blockfile_number = 'l'; + uint8_t key_last_blockfile_number{'l'}; uint32_t lastblockfilenumber = InsecureRand32(); BOOST_CHECK(dbw.Write(key_last_blockfile_number, lastblockfilenumber)); BOOST_CHECK(dbw.Read(key_last_blockfile_number, res_uint_32)); BOOST_CHECK_EQUAL(lastblockfilenumber, res_uint_32); //Simulate Is Reindexing - "R" - char key_IsReindexing = 'R'; + uint8_t key_IsReindexing{'R'}; bool isInReindexing = InsecureRandBool(); BOOST_CHECK(dbw.Write(key_IsReindexing, isInReindexing)); BOOST_CHECK(dbw.Read(key_IsReindexing, res_bool)); BOOST_CHECK_EQUAL(isInReindexing, res_bool); //Simulate last block hash up to which UXTO covers - 'B' - char key_lastblockhash_uxto = 'B'; + uint8_t key_lastblockhash_uxto{'B'}; uint256 lastblock_hash = InsecureRand256(); BOOST_CHECK(dbw.Write(key_lastblockhash_uxto, lastblock_hash)); BOOST_CHECK(dbw.Read(key_lastblockhash_uxto, res)); @@ -129,11 +129,11 @@ BOOST_AUTO_TEST_CASE(dbwrapper_batch) fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_batch_obfuscate_true" : "dbwrapper_batch_obfuscate_false"); CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate); - char key = 'i'; + uint8_t key{'i'}; uint256 in = InsecureRand256(); - char key2 = 'j'; + uint8_t key2{'j'}; uint256 in2 = InsecureRand256(); - char key3 = 'k'; + uint8_t key3{'k'}; uint256 in3 = InsecureRand256(); uint256 res; @@ -166,10 +166,10 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator) CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate); // The two keys are intentionally chosen for ordering - char key = 'j'; + uint8_t key{'j'}; uint256 in = InsecureRand256(); BOOST_CHECK(dbw.Write(key, in)); - char key2 = 'k'; + uint8_t key2{'k'}; uint256 in2 = InsecureRand256(); BOOST_CHECK(dbw.Write(key2, in2)); @@ -178,7 +178,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator) // Be sure to seek past the obfuscation key (if it exists) it->Seek(key); - char key_res; + uint8_t key_res; uint256 val_res; BOOST_REQUIRE(it->GetKey(key_res)); @@ -207,7 +207,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate) // Set up a non-obfuscated wrapper to write some initial data. std::unique_ptr<CDBWrapper> dbw = std::make_unique<CDBWrapper>(ph, (1 << 10), false, false, false); - char key = 'k'; + uint8_t key{'k'}; uint256 in = InsecureRand256(); uint256 res; @@ -248,7 +248,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex) // Set up a non-obfuscated wrapper to write some initial data. std::unique_ptr<CDBWrapper> dbw = std::make_unique<CDBWrapper>(ph, (1 << 10), false, false, false); - char key = 'k'; + uint8_t key{'k'}; uint256 in = InsecureRand256(); uint256 res; @@ -334,7 +334,7 @@ struct StringContentsSerializer { void Serialize(Stream& s) const { for (size_t i = 0; i < str.size(); i++) { - s << str[i]; + s << uint8_t(str[i]); } } @@ -342,7 +342,7 @@ struct StringContentsSerializer { void Unserialize(Stream& s) { str.clear(); - char c = 0; + uint8_t c{0}; while (true) { try { s >> c; diff --git a/src/test/fuzz/banman.cpp b/src/test/fuzz/banman.cpp index 4a04eed463..759a70a857 100644 --- a/src/test/fuzz/banman.cpp +++ b/src/test/fuzz/banman.cpp @@ -32,13 +32,17 @@ void initialize_banman() FUZZ_TARGET_INIT(banman, initialize_banman) { + // The complexity is O(N^2), where N is the input size, because each call + // might call DumpBanlist (or other methods that are at least linear + // complexity of the input size). + int limit_max_ops{300}; FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; SetMockTime(ConsumeTime(fuzzed_data_provider)); const fs::path banlist_file = gArgs.GetDataDirNet() / "fuzzed_banlist.dat"; fs::remove(banlist_file); { BanMan ban_man{banlist_file, nullptr, ConsumeBanTimeOffset(fuzzed_data_provider)}; - while (fuzzed_data_provider.ConsumeBool()) { + while (--limit_max_ops >= 0 && fuzzed_data_provider.ConsumeBool()) { CallOneOf( fuzzed_data_provider, [&] { @@ -52,7 +56,6 @@ FUZZ_TARGET_INIT(banman, initialize_banman) [&] { ban_man.ClearBanned(); }, - [] {}, [&] { ban_man.IsBanned(ConsumeNetAddr(fuzzed_data_provider)); }, @@ -72,7 +75,6 @@ FUZZ_TARGET_INIT(banman, initialize_banman) [&] { ban_man.DumpBanlist(); }, - [] {}, [&] { ban_man.Discourage(ConsumeNetAddr(fuzzed_data_provider)); }); diff --git a/src/test/serialize_tests.cpp b/src/test/serialize_tests.cpp index 58709178a4..4b55e3bc26 100644 --- a/src/test/serialize_tests.cpp +++ b/src/test/serialize_tests.cpp @@ -24,7 +24,7 @@ protected: CTransactionRef txval; public: CSerializeMethodsTestSingle() = default; - CSerializeMethodsTestSingle(int intvalin, bool boolvalin, std::string stringvalin, const char* charstrvalin, const CTransactionRef& txvalin) : intval(intvalin), boolval(boolvalin), stringval(std::move(stringvalin)), txval(txvalin) + CSerializeMethodsTestSingle(int intvalin, bool boolvalin, std::string stringvalin, const uint8_t* charstrvalin, const CTransactionRef& txvalin) : intval(intvalin), boolval(boolvalin), stringval(std::move(stringvalin)), txval(txvalin) { memcpy(charstrval, charstrvalin, sizeof(charstrval)); } @@ -70,8 +70,8 @@ BOOST_AUTO_TEST_CASE(sizes) BOOST_CHECK_EQUAL(sizeof(uint32_t), GetSerializeSize(uint32_t(0), 0)); BOOST_CHECK_EQUAL(sizeof(int64_t), GetSerializeSize(int64_t(0), 0)); BOOST_CHECK_EQUAL(sizeof(uint64_t), GetSerializeSize(uint64_t(0), 0)); - // Bool is serialized as char - BOOST_CHECK_EQUAL(sizeof(char), GetSerializeSize(bool(0), 0)); + // Bool is serialized as uint8_t + BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(bool(0), 0)); // Sanity-check GetSerializeSize and c++ type matching BOOST_CHECK_EQUAL(GetSerializeSize(char(0), 0), 1U); @@ -263,7 +263,7 @@ BOOST_AUTO_TEST_CASE(class_methods) int intval(100); bool boolval(true); std::string stringval("testing"); - const char charstrval[16] = "testing charstr"; + const uint8_t charstrval[16]{"testing charstr"}; CMutableTransaction txval; CTransactionRef tx_ref{MakeTransactionRef(txval)}; CSerializeMethodsTestSingle methodtest1(intval, boolval, stringval, charstrval, tx_ref); diff --git a/src/test/txindex_tests.cpp b/src/test/txindex_tests.cpp index 082655d811..d47c54fd6e 100644 --- a/src/test/txindex_tests.cpp +++ b/src/test/txindex_tests.cpp @@ -7,6 +7,7 @@ #include <script/standard.h> #include <test/util/setup_common.h> #include <util/time.h> +#include <validation.h> #include <boost/test/unit_test.hpp> @@ -27,7 +28,7 @@ BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup) // BlockUntilSyncedToCurrentChain should return false before txindex is started. BOOST_CHECK(!txindex.BlockUntilSyncedToCurrentChain()); - BOOST_REQUIRE(txindex.Start()); + BOOST_REQUIRE(txindex.Start(::ChainstateActive())); // Allow tx index to catch up with the block index. constexpr int64_t timeout_ms = 10 * 1000; diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index 0ac178443a..863c3ab565 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -246,8 +246,7 @@ CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransa for (const CMutableTransaction& tx : txns) { block.vtx.push_back(MakeTransactionRef(tx)); } - CBlockIndex* prev_block = WITH_LOCK(::cs_main, return g_chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock)); - RegenerateCommitments(block, prev_block); + RegenerateCommitments(block, *Assert(m_node.chainman)); while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce; diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index aa95bc37e5..d463bcdd8e 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -182,7 +182,7 @@ BOOST_AUTO_TEST_CASE(util_FormatParseISO8601DateTime) BOOST_CHECK_EQUAL(ParseISO8601DateTime("1960-01-01T00:00:00Z"), 0); BOOST_CHECK_EQUAL(ParseISO8601DateTime("2011-09-30T23:36:17Z"), 1317425777); - auto time = GetSystemTimeInSeconds(); + auto time = GetTimeSeconds(); BOOST_CHECK_EQUAL(ParseISO8601DateTime(FormatISO8601DateTime(time)), time); } diff --git a/src/txdb.cpp b/src/txdb.cpp index c11d46cf88..762f71feb1 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -16,22 +16,22 @@ #include <stdint.h> -static const char DB_COIN = 'C'; -static const char DB_COINS = 'c'; -static const char DB_BLOCK_FILES = 'f'; -static const char DB_BLOCK_INDEX = 'b'; +static constexpr uint8_t DB_COIN{'C'}; +static constexpr uint8_t DB_COINS{'c'}; +static constexpr uint8_t DB_BLOCK_FILES{'f'}; +static constexpr uint8_t DB_BLOCK_INDEX{'b'}; -static const char DB_BEST_BLOCK = 'B'; -static const char DB_HEAD_BLOCKS = 'H'; -static const char DB_FLAG = 'F'; -static const char DB_REINDEX_FLAG = 'R'; -static const char DB_LAST_BLOCK = 'l'; +static constexpr uint8_t DB_BEST_BLOCK{'B'}; +static constexpr uint8_t DB_HEAD_BLOCKS{'H'}; +static constexpr uint8_t DB_FLAG{'F'}; +static constexpr uint8_t DB_REINDEX_FLAG{'R'}; +static constexpr uint8_t DB_LAST_BLOCK{'l'}; namespace { struct CoinEntry { COutPoint* outpoint; - char key; + uint8_t key; explicit CoinEntry(const COutPoint* ptr) : outpoint(const_cast<COutPoint*>(ptr)), key(DB_COIN) {} SERIALIZE_METHODS(CoinEntry, obj) { READWRITE(obj.key, obj.outpoint->hash, VARINT(obj.outpoint->n)); } @@ -143,7 +143,7 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) { size_t CCoinsViewDB::EstimateSize() const { - return m_db->EstimateSize(DB_COIN, (char)(DB_COIN+1)); + return m_db->EstimateSize(DB_COIN, uint8_t(DB_COIN + 1)); } CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe) : CDBWrapper(gArgs.GetDataDirNet() / "blocks" / "index", nCacheSize, fMemory, fWipe) { @@ -155,7 +155,7 @@ bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) { bool CBlockTreeDB::WriteReindexing(bool fReindexing) { if (fReindexing) - return Write(DB_REINDEX_FLAG, '1'); + return Write(DB_REINDEX_FLAG, uint8_t{'1'}); else return Erase(DB_REINDEX_FLAG); } @@ -235,14 +235,14 @@ bool CBlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockF } bool CBlockTreeDB::WriteFlag(const std::string &name, bool fValue) { - return Write(std::make_pair(DB_FLAG, name), fValue ? '1' : '0'); + return Write(std::make_pair(DB_FLAG, name), fValue ? uint8_t{'1'} : uint8_t{'0'}); } bool CBlockTreeDB::ReadFlag(const std::string &name, bool &fValue) { - char ch; + uint8_t ch; if (!Read(std::make_pair(DB_FLAG, name), ch)) return false; - fValue = ch == '1'; + fValue = ch == uint8_t{'1'}; return true; } @@ -255,7 +255,7 @@ bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams, // Load m_block_index while (pcursor->Valid()) { if (ShutdownRequested()) return false; - std::pair<char, uint256> key; + std::pair<uint8_t, uint256> key; if (pcursor->GetKey(key) && key.first == DB_BLOCK_INDEX) { CDiskBlockIndex diskindex; if (pcursor->GetValue(diskindex)) { diff --git a/src/util/time.cpp b/src/util/time.cpp index e6f0986a39..eda710b12c 100644 --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -124,7 +124,7 @@ int64_t GetTimeMicros() return int64_t{GetSystemTime<std::chrono::microseconds>().count()}; } -int64_t GetSystemTimeInSeconds() +int64_t GetTimeSeconds() { return int64_t{GetSystemTime<std::chrono::seconds>().count()}; } diff --git a/src/util/time.h b/src/util/time.h index 7ebcaaa339..4aee01967d 100644 --- a/src/util/time.h +++ b/src/util/time.h @@ -39,7 +39,7 @@ inline double CountSecondsDouble(SecondsDouble t) { return t.count(); } /** * DEPRECATED - * Use either GetSystemTimeInSeconds (not mockable) or GetTime<T> (mockable) + * Use either GetTimeSeconds (not mockable) or GetTime<T> (mockable) */ int64_t GetTime(); @@ -48,7 +48,7 @@ int64_t GetTimeMillis(); /** Returns the system time (not mockable) */ int64_t GetTimeMicros(); /** Returns the system time (not mockable) */ -int64_t GetSystemTimeInSeconds(); // Like GetTime(), but not mockable +int64_t GetTimeSeconds(); // Like GetTime(), but not mockable /** * DEPRECATED diff --git a/src/wallet/transaction.h b/src/wallet/transaction.h index b57ab3db48..131faefe0b 100644 --- a/src/wallet/transaction.h +++ b/src/wallet/transaction.h @@ -204,8 +204,8 @@ public: mapValueCopy["timesmart"] = strprintf("%u", nTimeSmart); } - std::vector<char> dummy_vector1; //!< Used to be vMerkleBranch - std::vector<char> dummy_vector2; //!< Used to be vtxPrev + std::vector<uint8_t> dummy_vector1; //!< Used to be vMerkleBranch + std::vector<uint8_t> dummy_vector2; //!< Used to be vtxPrev bool dummy_bool = false; //!< Used to be fSpent uint256 serializedHash = isAbandoned() ? ABANDON_HASH : m_confirm.hashBlock; int serializedIndex = isAbandoned() || isConflicted() ? -1 : m_confirm.nIndex; diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index 3d9248009f..c06b319b0b 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -155,7 +155,7 @@ bool WalletBatch::WriteWatchOnly(const CScript &dest, const CKeyMetadata& keyMet if (!WriteIC(std::make_pair(DBKeys::WATCHMETA, dest), keyMeta)) { return false; } - return WriteIC(std::make_pair(DBKeys::WATCHS, dest), '1'); + return WriteIC(std::make_pair(DBKeys::WATCHS, dest), uint8_t{'1'}); } bool WalletBatch::EraseWatchOnly(const CScript &dest) @@ -308,8 +308,8 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, { if (!ssValue.empty()) { - char fTmp; - char fUnused; + uint8_t fTmp; + uint8_t fUnused; std::string unused_string; ssValue >> fTmp >> fUnused >> unused_string; strErr = strprintf("LoadWallet() upgrading tx ver=%d %d %s", @@ -336,7 +336,7 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, wss.nWatchKeys++; CScript script; ssKey >> script; - char fYes; + uint8_t fYes; ssValue >> fYes; if (fYes == '1') { pwallet->GetOrCreateLegacyScriptPubKeyMan()->LoadWatchOnly(script); diff --git a/test/functional/feature_cltv.py b/test/functional/feature_cltv.py index d25aaa070d..10d2072dba 100755 --- a/test/functional/feature_cltv.py +++ b/test/functional/feature_cltv.py @@ -37,7 +37,7 @@ CLTV_HEIGHT = 1351 # Helper function to modify a transaction by # 1) prepending a given script to the scriptSig of vin 0 and # 2) (optionally) modify the nSequence of vin 0 and the tx's nLockTime -def cltv_modify_tx(node, tx, prepend_scriptsig, nsequence=None, nlocktime=None): +def cltv_modify_tx(tx, prepend_scriptsig, nsequence=None, nlocktime=None): assert_equal(len(tx.vin), 1) if nsequence is not None: tx.vin[0].nSequence = nsequence @@ -45,10 +45,9 @@ def cltv_modify_tx(node, tx, prepend_scriptsig, nsequence=None, nlocktime=None): tx.vin[0].scriptSig = CScript(prepend_scriptsig + list(CScript(tx.vin[0].scriptSig))) tx.rehash() - return tx -def cltv_invalidate(node, tx, failure_reason): +def cltv_invalidate(tx, failure_reason): # Modify the signature in vin 0 and nSequence/nLockTime of the tx to fail CLTV # # According to BIP65, OP_CHECKLOCKTIMEVERIFY can fail due the following reasons: @@ -69,14 +68,14 @@ def cltv_invalidate(node, tx, failure_reason): [[CScriptNum(500), OP_CHECKLOCKTIMEVERIFY, OP_DROP], 0xffffffff, 500], ][failure_reason] - return cltv_modify_tx(node, tx, prepend_scriptsig=scheme[0], nsequence=scheme[1], nlocktime=scheme[2]) + cltv_modify_tx(tx, prepend_scriptsig=scheme[0], nsequence=scheme[1], nlocktime=scheme[2]) -def cltv_validate(node, tx, height): +def cltv_validate(tx, height): # Modify the signature in vin 0 and nSequence/nLockTime of the tx to pass CLTV scheme = [[CScriptNum(height), OP_CHECKLOCKTIMEVERIFY, OP_DROP], 0, height] - return cltv_modify_tx(node, tx, prepend_scriptsig=scheme[0], nsequence=scheme[1], nlocktime=scheme[2]) + cltv_modify_tx(tx, prepend_scriptsig=scheme[0], nsequence=scheme[1], nlocktime=scheme[2]) class BIP65Test(BitcoinTestFramework): @@ -111,17 +110,17 @@ class BIP65Test(BitcoinTestFramework): self.log.info("Test that invalid-according-to-CLTV transactions can still appear in a block") # create one invalid tx per CLTV failure reason (5 in total) and collect them - invalid_ctlv_txs = [] + invalid_cltv_txs = [] for i in range(5): spendtx = wallet.create_self_transfer(from_node=self.nodes[0])['tx'] - spendtx = cltv_invalidate(self.nodes[0], spendtx, i) - invalid_ctlv_txs.append(spendtx) + cltv_invalidate(spendtx, i) + invalid_cltv_txs.append(spendtx) tip = self.nodes[0].getbestblockhash() block_time = self.nodes[0].getblockheader(tip)['mediantime'] + 1 block = create_block(int(tip, 16), create_coinbase(CLTV_HEIGHT - 1), block_time) block.nVersion = 3 - block.vtx.extend(invalid_ctlv_txs) + block.vtx.extend(invalid_cltv_txs) block.hashMerkleRoot = block.calc_merkle_root() block.solve() @@ -149,7 +148,7 @@ class BIP65Test(BitcoinTestFramework): # create and test one invalid tx per CLTV failure reason (5 in total) for i in range(5): spendtx = wallet.create_self_transfer(from_node=self.nodes[0])['tx'] - spendtx = cltv_invalidate(self.nodes[0], spendtx, i) + cltv_invalidate(spendtx, i) expected_cltv_reject_reason = [ "non-mandatory-script-verify-flag (Operation not valid with the current stack size)", @@ -182,7 +181,7 @@ class BIP65Test(BitcoinTestFramework): peer.sync_with_ping() self.log.info("Test that a version 4 block with a valid-according-to-CLTV transaction is accepted") - spendtx = cltv_validate(self.nodes[0], spendtx, CLTV_HEIGHT - 1) + cltv_validate(spendtx, CLTV_HEIGHT - 1) block.vtx.pop(1) block.vtx.append(spendtx) |