diff --git a/src/Makefile.am b/src/Makefile.am index a70ef65aa7..9903c2e9b3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -182,7 +182,6 @@ BITCOIN_CORE_H = \ node/ui_interface.h \ node/utxo_snapshot.h \ noui.h \ - optional.h \ outputtype.h \ policy/feerate.h \ policy/fees.h \ diff --git a/src/bench/wallet_balance.cpp b/src/bench/wallet_balance.cpp index c96ef209e3..d7cc167885 100644 --- a/src/bench/wallet_balance.cpp +++ b/src/bench/wallet_balance.cpp @@ -5,13 +5,14 @@ #include #include #include -#include #include #include #include #include #include +#include + static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const bool add_watchonly, const bool add_mine) { const auto test_setup = MakeNoLogFileContext(); @@ -26,7 +27,7 @@ static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const b } auto handler = test_setup->m_node.chain->handleNotifications({&wallet, [](CWallet*) {}}); - const Optional address_mine{add_mine ? Optional{getnewaddress(wallet)} : nullopt}; + const std::optional address_mine{add_mine ? std::optional{getnewaddress(wallet)} : std::nullopt}; if (add_watchonly) importaddress(wallet, ADDRESS_WATCHONLY); for (int i = 0; i < 100; ++i) { diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index dc4b142f83..ecd08c62eb 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -9,7 +9,6 @@ #include #include -#include #include #include #include @@ -24,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -611,7 +611,7 @@ public: } }; -static UniValue CallRPC(BaseRequestHandler* rh, const std::string& strMethod, const std::vector& args, const Optional& rpcwallet = {}) +static UniValue CallRPC(BaseRequestHandler* rh, const std::string& strMethod, const std::vector& args, const std::optional& rpcwallet = {}) { std::string host; // In preference order, we choose the following for the port: @@ -733,7 +733,7 @@ static UniValue CallRPC(BaseRequestHandler* rh, const std::string& strMethod, co * @returns the RPC response as a UniValue object. * @throws a CConnectionFailed std::runtime_error if connection failed or RPC server still in warmup. */ -static UniValue ConnectAndCallRPC(BaseRequestHandler* rh, const std::string& strMethod, const std::vector& args, const Optional& rpcwallet = {}) +static UniValue ConnectAndCallRPC(BaseRequestHandler* rh, const std::string& strMethod, const std::vector& args, const std::optional& rpcwallet = {}) { UniValue response(UniValue::VOBJ); // Execute and handle connection failures with -rpcwait. @@ -817,7 +817,7 @@ static void GetWalletBalances(UniValue& result) */ static UniValue GetNewAddress() { - Optional wallet_name{}; + std::optional wallet_name{}; if (gArgs.IsArgSet("-rpcwallet")) wallet_name = gArgs.GetArg("-rpcwallet", ""); DefaultRequestHandler rh; return ConnectAndCallRPC(&rh, "getnewaddress", /* args=*/{}, wallet_name); @@ -922,7 +922,7 @@ static int CommandLineRPC(int argc, char *argv[]) } if (nRet == 0) { // Perform RPC call - Optional wallet_name{}; + std::optional wallet_name{}; if (gArgs.IsArgSet("-rpcwallet")) wallet_name = gArgs.GetArg("-rpcwallet", ""); const UniValue reply = ConnectAndCallRPC(rh.get(), method, args, wallet_name); diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index 32f06aec2c..1b4ca3e9a8 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -25,6 +25,7 @@ #include #include +#include const std::function G_TRANSLATION_FUN = nullptr; UrlDecodeFn* const URL_DECODE = urlDecode; diff --git a/src/interfaces/chain.h b/src/interfaces/chain.h index ebc5466173..3395741b1b 100644 --- a/src/interfaces/chain.h +++ b/src/interfaces/chain.h @@ -5,12 +5,12 @@ #ifndef BITCOIN_INTERFACES_CHAIN_H #define BITCOIN_INTERFACES_CHAIN_H -#include // For Optional and nullopt #include // For CTransactionRef #include // For util::SettingsValue #include #include +#include #include #include #include @@ -94,7 +94,7 @@ public: //! Get current chain height, not including genesis block (returns 0 if //! chain only contains genesis block, nullopt if chain does not contain //! any blocks) - virtual Optional getHeight() = 0; + virtual std::optional getHeight() = 0; //! Get block hash. Height must be valid or this function will abort. virtual uint256 getBlockHash(int height) = 0; @@ -109,7 +109,7 @@ public: //! Return height of the highest block on chain in common with the locator, //! which will either be the original block used to create the locator, //! or one of its ancestors. - virtual Optional findLocatorFork(const CBlockLocator& locator) = 0; + virtual std::optional findLocatorFork(const CBlockLocator& locator) = 0; //! Check if transaction will be final given chain height current time. virtual bool checkFinalTx(const CTransaction& tx) = 0; @@ -154,7 +154,7 @@ public: //! Return true if data is available for all blocks in the specified range //! of blocks. This checks all blocks that are ancestors of block_hash in //! the height range from min_height to max_height, inclusive. - virtual bool hasBlocks(const uint256& block_hash, int min_height = 0, Optional max_height = {}) = 0; + virtual bool hasBlocks(const uint256& block_hash, int min_height = 0, std::optional max_height = {}) = 0; //! Check if transaction is RBF opt in. virtual RBFTransactionState isRBFOptIn(const CTransaction& tx) = 0; diff --git a/src/miner.cpp b/src/miner.cpp index fbaef0f224..fe7a54c052 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -96,9 +96,6 @@ void BlockAssembler::resetBlock() nFees = 0; } -Optional BlockAssembler::m_last_block_num_txs{nullopt}; -Optional BlockAssembler::m_last_block_weight{nullopt}; - std::unique_ptr BlockAssembler::CreateNewBlock(CChainState& chainstate, const CScript& scriptPubKeyIn) { int64_t nTimeStart = GetTimeMicros(); diff --git a/src/miner.h b/src/miner.h index a67fec6dd8..023635814c 100644 --- a/src/miner.h +++ b/src/miner.h @@ -6,12 +6,12 @@ #ifndef BITCOIN_MINER_H #define BITCOIN_MINER_H -#include #include #include #include #include +#include #include #include @@ -160,8 +160,8 @@ public: /** Construct a new block template with coinbase to scriptPubKeyIn */ std::unique_ptr CreateNewBlock(CChainState& chainstate, const CScript& scriptPubKeyIn); - static Optional m_last_block_num_txs; - static Optional m_last_block_weight; + inline static std::optional m_last_block_num_txs{}; + inline static std::optional m_last_block_weight{}; private: // utility functions diff --git a/src/net.cpp b/src/net.cpp index 1e4a6a9aa7..6a2469f950 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -18,7 +18,6 @@ #include #include #include -#include #include #include #include @@ -39,6 +38,7 @@ #include #include #include +#include #include #include @@ -193,7 +193,7 @@ bool IsPeerAddrLocalGood(CNode *pnode) IsReachable(addrLocal.GetNetwork()); } -Optional GetLocalAddrForPeer(CNode *pnode) +std::optional GetLocalAddrForPeer(CNode *pnode) { CAddress addrLocal = GetLocalAddress(&pnode->addr, pnode->GetLocalServices()); if (gArgs.GetBoolArg("-addrmantest", false)) { @@ -215,7 +215,7 @@ Optional GetLocalAddrForPeer(CNode *pnode) return addrLocal; } // Address is unroutable. Don't advertise. - return nullopt; + return std::nullopt; } // learn a new local address @@ -632,7 +632,7 @@ bool CNode::ReceiveMsgBytes(Span msg_bytes, bool& complete) if (m_deserializer->Complete()) { // decompose a transport agnostic CNetMessage from the deserializer uint32_t out_err_raw_size{0}; - Optional result{m_deserializer->GetMessage(time, out_err_raw_size)}; + std::optional result{m_deserializer->GetMessage(time, out_err_raw_size)}; if (!result) { // Message deserialization failed. Drop the message but don't disconnect the peer. // store the size of the corrupt message @@ -723,10 +723,10 @@ const uint256& V1TransportDeserializer::GetMessageHash() const return data_hash; } -Optional V1TransportDeserializer::GetMessage(const std::chrono::microseconds time, uint32_t& out_err_raw_size) +std::optional V1TransportDeserializer::GetMessage(const std::chrono::microseconds time, uint32_t& out_err_raw_size) { // decompose a single CNetMessage from the TransportDeserializer - Optional msg(std::move(vRecv)); + std::optional msg(std::move(vRecv)); // store command string, time, and sizes msg->m_command = hdr.GetCommand(); @@ -747,12 +747,12 @@ Optional V1TransportDeserializer::GetMessage(const std::chrono::mic HexStr(hdr.pchChecksum), m_node_id); out_err_raw_size = msg->m_raw_message_size; - msg = nullopt; + msg = std::nullopt; } else if (!hdr.IsCommandValid()) { LogPrint(BCLog::NET, "HEADER ERROR - COMMAND (%s, %u bytes), peer=%d\n", hdr.GetCommand(), msg->m_message_size, m_node_id); out_err_raw_size = msg->m_raw_message_size; - msg = nullopt; + msg.reset(); } // Always reset the network deserializer (prepare for the next message) @@ -879,7 +879,7 @@ static void EraseLastKElements(std::vector &elements, Comparator comparator, elements.erase(elements.end() - eraseSize, elements.end()); } -[[nodiscard]] Optional SelectNodeToEvict(std::vector&& vEvictionCandidates) +[[nodiscard]] std::optional SelectNodeToEvict(std::vector&& vEvictionCandidates) { // Protect connections with certain characteristics @@ -918,7 +918,7 @@ static void EraseLastKElements(std::vector &elements, Comparator comparator, total_protect_size -= initial_size - vEvictionCandidates.size(); EraseLastKElements(vEvictionCandidates, ReverseCompareNodeTimeConnected, total_protect_size); - if (vEvictionCandidates.empty()) return nullopt; + if (vEvictionCandidates.empty()) return std::nullopt; // If any remaining peers are preferred for eviction consider only them. // This happens after the other preferences since if a peer is really the best by other criteria (esp relaying blocks) @@ -989,7 +989,7 @@ bool CConnman::AttemptToEvictConnection() vEvictionCandidates.push_back(candidate); } } - const Optional node_id_to_evict = SelectNodeToEvict(std::move(vEvictionCandidates)); + const std::optional node_id_to_evict = SelectNodeToEvict(std::move(vEvictionCandidates)); if (!node_id_to_evict) { return false; } diff --git a/src/net.h b/src/net.h index beef47f045..48d37084a0 100644 --- a/src/net.h +++ b/src/net.h @@ -18,7 +18,6 @@ #include #include #include -#include #include #include #include @@ -35,6 +34,7 @@ #include #include #include +#include #include #include @@ -200,7 +200,7 @@ enum bool IsPeerAddrLocalGood(CNode *pnode); /** Returns a local address that we should advertise to this peer */ -Optional GetLocalAddrForPeer(CNode *pnode); +std::optional GetLocalAddrForPeer(CNode *pnode); /** * Mark a network as reachable or unreachable (no automatic connects to it) @@ -311,7 +311,7 @@ public: /** read and deserialize data, advances msg_bytes data pointer */ virtual int Read(Span& msg_bytes) = 0; // decomposes a message from the context - virtual Optional GetMessage(std::chrono::microseconds time, uint32_t& out_err) = 0; + virtual std::optional GetMessage(std::chrono::microseconds time, uint32_t& out_err) = 0; virtual ~TransportDeserializer() {} }; @@ -375,7 +375,7 @@ public: } return ret; } - Optional GetMessage(std::chrono::microseconds time, uint32_t& out_err_raw_size) override; + std::optional GetMessage(std::chrono::microseconds time, uint32_t& out_err_raw_size) override; }; /** The TransportSerializer prepares messages for the network transport @@ -1283,6 +1283,6 @@ struct NodeEvictionCandidate bool m_is_local; }; -[[nodiscard]] Optional SelectNodeToEvict(std::vector&& vEvictionCandidates); +[[nodiscard]] std::optional SelectNodeToEvict(std::vector&& vEvictionCandidates); #endif // BITCOIN_NET_H diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 6ce984348c..8ffbd45034 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -34,6 +34,7 @@ #include #include +#include #include /** How long to cache transactions in mapRelay for normal relay */ @@ -4218,7 +4219,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto) if (pto->m_next_local_addr_send != 0us) { pto->m_addr_known->reset(); } - if (Optional local_addr = GetLocalAddrForPeer(pto)) { + if (std::optional local_addr = GetLocalAddrForPeer(pto)) { FastRandomContext insecure_rand; pto->PushAddress(*local_addr, insecure_rand); } diff --git a/src/node/interfaces.cpp b/src/node/interfaces.cpp index 9406ef07c5..50c8c29175 100644 --- a/src/node/interfaces.cpp +++ b/src/node/interfaces.cpp @@ -50,6 +50,7 @@ #endif #include +#include #include using interfaces::BlockTip; @@ -415,7 +416,7 @@ class ChainImpl : public Chain { public: explicit ChainImpl(NodeContext& node) : m_node(node) {} - Optional getHeight() override + std::optional getHeight() override { LOCK(::cs_main); const CChain& active = Assert(m_node.chainman)->ActiveChain(); @@ -423,7 +424,7 @@ public: if (height >= 0) { return height; } - return nullopt; + return std::nullopt; } uint256 getBlockHash(int height) override { @@ -452,7 +453,7 @@ public: assert(std::addressof(::ChainActive()) == std::addressof(m_node.chainman->ActiveChain())); return CheckFinalTx(m_node.chainman->ActiveChain().Tip(), tx); } - Optional findLocatorFork(const CBlockLocator& locator) override + std::optional findLocatorFork(const CBlockLocator& locator) override { LOCK(cs_main); const CChain& active = Assert(m_node.chainman)->ActiveChain(); @@ -460,7 +461,7 @@ public: if (CBlockIndex* fork = m_node.chainman->m_blockman.FindForkInGlobalIndex(active, locator)) { return fork->nHeight; } - return nullopt; + return std::nullopt; } bool findBlock(const uint256& hash, const FoundBlock& block) override { @@ -518,7 +519,7 @@ public: assert(std::addressof(g_chainman) == std::addressof(*m_node.chainman)); return GuessVerificationProgress(Params().TxData(), m_node.chainman->m_blockman.LookupBlockIndex(block_hash)); } - bool hasBlocks(const uint256& block_hash, int min_height, Optional max_height) override + bool hasBlocks(const uint256& block_hash, int min_height, std::optional max_height) override { // hasBlocks returns true if all ancestors of block_hash in specified // range have block data (are not pruned), false if any ancestors in diff --git a/src/node/psbt.h b/src/node/psbt.h index 7384dc415c..def4385c09 100644 --- a/src/node/psbt.h +++ b/src/node/psbt.h @@ -7,6 +7,8 @@ #include +#include + /** * Holds an analysis of one input from a PSBT */ @@ -25,18 +27,18 @@ struct PSBTInputAnalysis { * Holds the results of AnalyzePSBT (miscellaneous information about a PSBT) */ struct PSBTAnalysis { - Optional estimated_vsize; //!< Estimated weight of the transaction - Optional estimated_feerate; //!< Estimated feerate (fee / weight) of the transaction - Optional fee; //!< Amount of fee being paid by the transaction - std::vector inputs; //!< More information about the individual inputs of the transaction - PSBTRole next; //!< Which of the BIP 174 roles needs to handle the transaction next - std::string error; //!< Error message + std::optional estimated_vsize; //!< Estimated weight of the transaction + std::optional estimated_feerate; //!< Estimated feerate (fee / weight) of the transaction + std::optional fee; //!< Amount of fee being paid by the transaction + std::vector inputs; //!< More information about the individual inputs of the transaction + PSBTRole next; //!< Which of the BIP 174 roles needs to handle the transaction next + std::string error; //!< Error message void SetInvalid(std::string err_msg) { - estimated_vsize = nullopt; - estimated_feerate = nullopt; - fee = nullopt; + estimated_vsize = std::nullopt; + estimated_feerate = std::nullopt; + fee = std::nullopt; inputs.clear(); next = PSBTRole::CREATOR; error = err_msg; diff --git a/src/optional.h b/src/optional.h deleted file mode 100644 index 583c56eabd..0000000000 --- a/src/optional.h +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (c) 2017-2020 The Bitcoin Core developers -// Distributed under the MIT software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. - -#ifndef BITCOIN_OPTIONAL_H -#define BITCOIN_OPTIONAL_H - -#include -#include - -//! Substitute for C++17 std::optional -//! DEPRECATED use std::optional in new code. -template -using Optional = std::optional; - -//! Substitute for C++17 std::nullopt -//! DEPRECATED use std::nullopt in new code. -static auto& nullopt = std::nullopt; - -#endif // BITCOIN_OPTIONAL_H diff --git a/src/psbt.h b/src/psbt.h index b566726ee3..96ae39fdb8 100644 --- a/src/psbt.h +++ b/src/psbt.h @@ -7,13 +7,14 @@ #include #include -#include #include #include #include #include