Skip to content

Commit a9d1b40

Browse files
committed
Merge #21415: refactor: remove Optional & nullopt
ebc4ab7 refactor: post Optional<> removal cleanups (fanquake) 57e980d scripted-diff: remove Optional & nullopt (fanquake) Pull request description: Same rationale & motivation as #21404, which turned out to be quite low in the number of potential conflicts. Lets see what the bot has to say here. ACKs for top commit: practicalswift: cr ACK ebc4ab7: patch looks correct jnewbery: utACK ebc4ab7 laanwj: Code review ACK ebc4ab7 Tree-SHA512: 550fbeef09b9d35ddefaa805d1755c18c8fd499c4b0f77ebfece8c20296a7abd1cf6c699e2261f92fe3552deeb7555ec2a2287ffe3ab9e98bb9f8612a4d43be3
2 parents 993ecaf + ebc4ab7 commit a9d1b40

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+184
-194
lines changed

src/Makefile.am

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ BITCOIN_CORE_H = \
182182
node/ui_interface.h \
183183
node/utxo_snapshot.h \
184184
noui.h \
185-
optional.h \
186185
outputtype.h \
187186
policy/feerate.h \
188187
policy/fees.h \

src/bench/wallet_balance.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
#include <bench/bench.h>
66
#include <interfaces/chain.h>
77
#include <node/context.h>
8-
#include <optional.h>
98
#include <test/util/mining.h>
109
#include <test/util/setup_common.h>
1110
#include <test/util/wallet.h>
1211
#include <validationinterface.h>
1312
#include <wallet/wallet.h>
1413

14+
#include <optional>
15+
1516
static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const bool add_watchonly, const bool add_mine)
1617
{
1718
const auto test_setup = MakeNoLogFileContext<const TestingSetup>();
@@ -26,7 +27,7 @@ static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const b
2627
}
2728
auto handler = test_setup->m_node.chain->handleNotifications({&wallet, [](CWallet*) {}});
2829

29-
const Optional<std::string> address_mine{add_mine ? Optional<std::string>{getnewaddress(wallet)} : nullopt};
30+
const std::optional<std::string> address_mine{add_mine ? std::optional<std::string>{getnewaddress(wallet)} : std::nullopt};
3031
if (add_watchonly) importaddress(wallet, ADDRESS_WATCHONLY);
3132

3233
for (int i = 0; i < 100; ++i) {

src/bitcoin-cli.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include <chainparamsbase.h>
1111
#include <clientversion.h>
12-
#include <optional.h>
1312
#include <rpc/client.h>
1413
#include <rpc/mining.h>
1514
#include <rpc/protocol.h>
@@ -24,6 +23,7 @@
2423
#include <cmath>
2524
#include <functional>
2625
#include <memory>
26+
#include <optional>
2727
#include <stdio.h>
2828
#include <string>
2929
#include <tuple>
@@ -611,7 +611,7 @@ class DefaultRequestHandler: public BaseRequestHandler {
611611
}
612612
};
613613

614-
static UniValue CallRPC(BaseRequestHandler* rh, const std::string& strMethod, const std::vector<std::string>& args, const Optional<std::string>& rpcwallet = {})
614+
static UniValue CallRPC(BaseRequestHandler* rh, const std::string& strMethod, const std::vector<std::string>& args, const std::optional<std::string>& rpcwallet = {})
615615
{
616616
std::string host;
617617
// In preference order, we choose the following for the port:
@@ -733,7 +733,7 @@ static UniValue CallRPC(BaseRequestHandler* rh, const std::string& strMethod, co
733733
* @returns the RPC response as a UniValue object.
734734
* @throws a CConnectionFailed std::runtime_error if connection failed or RPC server still in warmup.
735735
*/
736-
static UniValue ConnectAndCallRPC(BaseRequestHandler* rh, const std::string& strMethod, const std::vector<std::string>& args, const Optional<std::string>& rpcwallet = {})
736+
static UniValue ConnectAndCallRPC(BaseRequestHandler* rh, const std::string& strMethod, const std::vector<std::string>& args, const std::optional<std::string>& rpcwallet = {})
737737
{
738738
UniValue response(UniValue::VOBJ);
739739
// Execute and handle connection failures with -rpcwait.
@@ -817,7 +817,7 @@ static void GetWalletBalances(UniValue& result)
817817
*/
818818
static UniValue GetNewAddress()
819819
{
820-
Optional<std::string> wallet_name{};
820+
std::optional<std::string> wallet_name{};
821821
if (gArgs.IsArgSet("-rpcwallet")) wallet_name = gArgs.GetArg("-rpcwallet", "");
822822
DefaultRequestHandler rh;
823823
return ConnectAndCallRPC(&rh, "getnewaddress", /* args=*/{}, wallet_name);
@@ -922,7 +922,7 @@ static int CommandLineRPC(int argc, char *argv[])
922922
}
923923
if (nRet == 0) {
924924
// Perform RPC call
925-
Optional<std::string> wallet_name{};
925+
std::optional<std::string> wallet_name{};
926926
if (gArgs.IsArgSet("-rpcwallet")) wallet_name = gArgs.GetArg("-rpcwallet", "");
927927
const UniValue reply = ConnectAndCallRPC(rh.get(), method, args, wallet_name);
928928

src/bitcoind.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <util/url.h>
2626

2727
#include <functional>
28+
#include <optional>
2829

2930
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
3031
UrlDecodeFn* const URL_DECODE = urlDecode;

src/interfaces/chain.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
#ifndef BITCOIN_INTERFACES_CHAIN_H
66
#define BITCOIN_INTERFACES_CHAIN_H
77

8-
#include <optional.h> // For Optional and nullopt
98
#include <primitives/transaction.h> // For CTransactionRef
109
#include <util/settings.h> // For util::SettingsValue
1110

1211
#include <functional>
1312
#include <memory>
13+
#include <optional>
1414
#include <stddef.h>
1515
#include <stdint.h>
1616
#include <string>
@@ -94,7 +94,7 @@ class Chain
9494
//! Get current chain height, not including genesis block (returns 0 if
9595
//! chain only contains genesis block, nullopt if chain does not contain
9696
//! any blocks)
97-
virtual Optional<int> getHeight() = 0;
97+
virtual std::optional<int> getHeight() = 0;
9898

9999
//! Get block hash. Height must be valid or this function will abort.
100100
virtual uint256 getBlockHash(int height) = 0;
@@ -109,7 +109,7 @@ class Chain
109109
//! Return height of the highest block on chain in common with the locator,
110110
//! which will either be the original block used to create the locator,
111111
//! or one of its ancestors.
112-
virtual Optional<int> findLocatorFork(const CBlockLocator& locator) = 0;
112+
virtual std::optional<int> findLocatorFork(const CBlockLocator& locator) = 0;
113113

114114
//! Check if transaction will be final given chain height current time.
115115
virtual bool checkFinalTx(const CTransaction& tx) = 0;
@@ -154,7 +154,7 @@ class Chain
154154
//! Return true if data is available for all blocks in the specified range
155155
//! of blocks. This checks all blocks that are ancestors of block_hash in
156156
//! the height range from min_height to max_height, inclusive.
157-
virtual bool hasBlocks(const uint256& block_hash, int min_height = 0, Optional<int> max_height = {}) = 0;
157+
virtual bool hasBlocks(const uint256& block_hash, int min_height = 0, std::optional<int> max_height = {}) = 0;
158158

159159
//! Check if transaction is RBF opt in.
160160
virtual RBFTransactionState isRBFOptIn(const CTransaction& tx) = 0;

src/miner.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ void BlockAssembler::resetBlock()
9696
nFees = 0;
9797
}
9898

99-
Optional<int64_t> BlockAssembler::m_last_block_num_txs{nullopt};
100-
Optional<int64_t> BlockAssembler::m_last_block_weight{nullopt};
101-
10299
std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(CChainState& chainstate, const CScript& scriptPubKeyIn)
103100
{
104101
int64_t nTimeStart = GetTimeMicros();

src/miner.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
#ifndef BITCOIN_MINER_H
77
#define BITCOIN_MINER_H
88

9-
#include <optional.h>
109
#include <primitives/block.h>
1110
#include <txmempool.h>
1211
#include <validation.h>
1312

1413
#include <memory>
14+
#include <optional>
1515
#include <stdint.h>
1616

1717
#include <boost/multi_index_container.hpp>
@@ -160,8 +160,8 @@ class BlockAssembler
160160
/** Construct a new block template with coinbase to scriptPubKeyIn */
161161
std::unique_ptr<CBlockTemplate> CreateNewBlock(CChainState& chainstate, const CScript& scriptPubKeyIn);
162162

163-
static Optional<int64_t> m_last_block_num_txs;
164-
static Optional<int64_t> m_last_block_weight;
163+
inline static std::optional<int64_t> m_last_block_num_txs{};
164+
inline static std::optional<int64_t> m_last_block_weight{};
165165

166166
private:
167167
// utility functions

src/net.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <net_permissions.h>
1919
#include <netbase.h>
2020
#include <node/ui_interface.h>
21-
#include <optional.h>
2221
#include <protocol.h>
2322
#include <random.h>
2423
#include <scheduler.h>
@@ -39,6 +38,7 @@
3938
#include <algorithm>
4039
#include <cstdint>
4140
#include <functional>
41+
#include <optional>
4242
#include <unordered_map>
4343

4444
#include <math.h>
@@ -193,7 +193,7 @@ bool IsPeerAddrLocalGood(CNode *pnode)
193193
IsReachable(addrLocal.GetNetwork());
194194
}
195195

196-
Optional<CAddress> GetLocalAddrForPeer(CNode *pnode)
196+
std::optional<CAddress> GetLocalAddrForPeer(CNode *pnode)
197197
{
198198
CAddress addrLocal = GetLocalAddress(&pnode->addr, pnode->GetLocalServices());
199199
if (gArgs.GetBoolArg("-addrmantest", false)) {
@@ -215,7 +215,7 @@ Optional<CAddress> GetLocalAddrForPeer(CNode *pnode)
215215
return addrLocal;
216216
}
217217
// Address is unroutable. Don't advertise.
218-
return nullopt;
218+
return std::nullopt;
219219
}
220220

221221
// learn a new local address
@@ -632,7 +632,7 @@ bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete)
632632
if (m_deserializer->Complete()) {
633633
// decompose a transport agnostic CNetMessage from the deserializer
634634
uint32_t out_err_raw_size{0};
635-
Optional<CNetMessage> result{m_deserializer->GetMessage(time, out_err_raw_size)};
635+
std::optional<CNetMessage> result{m_deserializer->GetMessage(time, out_err_raw_size)};
636636
if (!result) {
637637
// Message deserialization failed. Drop the message but don't disconnect the peer.
638638
// store the size of the corrupt message
@@ -723,10 +723,10 @@ const uint256& V1TransportDeserializer::GetMessageHash() const
723723
return data_hash;
724724
}
725725

726-
Optional<CNetMessage> V1TransportDeserializer::GetMessage(const std::chrono::microseconds time, uint32_t& out_err_raw_size)
726+
std::optional<CNetMessage> V1TransportDeserializer::GetMessage(const std::chrono::microseconds time, uint32_t& out_err_raw_size)
727727
{
728728
// decompose a single CNetMessage from the TransportDeserializer
729-
Optional<CNetMessage> msg(std::move(vRecv));
729+
std::optional<CNetMessage> msg(std::move(vRecv));
730730

731731
// store command string, time, and sizes
732732
msg->m_command = hdr.GetCommand();
@@ -747,12 +747,12 @@ Optional<CNetMessage> V1TransportDeserializer::GetMessage(const std::chrono::mic
747747
HexStr(hdr.pchChecksum),
748748
m_node_id);
749749
out_err_raw_size = msg->m_raw_message_size;
750-
msg = nullopt;
750+
msg = std::nullopt;
751751
} else if (!hdr.IsCommandValid()) {
752752
LogPrint(BCLog::NET, "HEADER ERROR - COMMAND (%s, %u bytes), peer=%d\n",
753753
hdr.GetCommand(), msg->m_message_size, m_node_id);
754754
out_err_raw_size = msg->m_raw_message_size;
755-
msg = nullopt;
755+
msg.reset();
756756
}
757757

758758
// Always reset the network deserializer (prepare for the next message)
@@ -879,7 +879,7 @@ static void EraseLastKElements(std::vector<T> &elements, Comparator comparator,
879879
elements.erase(elements.end() - eraseSize, elements.end());
880880
}
881881

882-
[[nodiscard]] Optional<NodeId> SelectNodeToEvict(std::vector<NodeEvictionCandidate>&& vEvictionCandidates)
882+
[[nodiscard]] std::optional<NodeId> SelectNodeToEvict(std::vector<NodeEvictionCandidate>&& vEvictionCandidates)
883883
{
884884
// Protect connections with certain characteristics
885885

@@ -918,7 +918,7 @@ static void EraseLastKElements(std::vector<T> &elements, Comparator comparator,
918918
total_protect_size -= initial_size - vEvictionCandidates.size();
919919
EraseLastKElements(vEvictionCandidates, ReverseCompareNodeTimeConnected, total_protect_size);
920920

921-
if (vEvictionCandidates.empty()) return nullopt;
921+
if (vEvictionCandidates.empty()) return std::nullopt;
922922

923923
// If any remaining peers are preferred for eviction consider only them.
924924
// 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()
989989
vEvictionCandidates.push_back(candidate);
990990
}
991991
}
992-
const Optional<NodeId> node_id_to_evict = SelectNodeToEvict(std::move(vEvictionCandidates));
992+
const std::optional<NodeId> node_id_to_evict = SelectNodeToEvict(std::move(vEvictionCandidates));
993993
if (!node_id_to_evict) {
994994
return false;
995995
}

src/net.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <net_permissions.h>
1919
#include <netaddress.h>
2020
#include <netbase.h>
21-
#include <optional.h>
2221
#include <policy/feerate.h>
2322
#include <protocol.h>
2423
#include <random.h>
@@ -35,6 +34,7 @@
3534
#include <deque>
3635
#include <map>
3736
#include <memory>
37+
#include <optional>
3838
#include <thread>
3939
#include <vector>
4040

@@ -200,7 +200,7 @@ enum
200200

201201
bool IsPeerAddrLocalGood(CNode *pnode);
202202
/** Returns a local address that we should advertise to this peer */
203-
Optional<CAddress> GetLocalAddrForPeer(CNode *pnode);
203+
std::optional<CAddress> GetLocalAddrForPeer(CNode *pnode);
204204

205205
/**
206206
* Mark a network as reachable or unreachable (no automatic connects to it)
@@ -311,7 +311,7 @@ class TransportDeserializer {
311311
/** read and deserialize data, advances msg_bytes data pointer */
312312
virtual int Read(Span<const uint8_t>& msg_bytes) = 0;
313313
// decomposes a message from the context
314-
virtual Optional<CNetMessage> GetMessage(std::chrono::microseconds time, uint32_t& out_err) = 0;
314+
virtual std::optional<CNetMessage> GetMessage(std::chrono::microseconds time, uint32_t& out_err) = 0;
315315
virtual ~TransportDeserializer() {}
316316
};
317317

@@ -375,7 +375,7 @@ class V1TransportDeserializer final : public TransportDeserializer
375375
}
376376
return ret;
377377
}
378-
Optional<CNetMessage> GetMessage(std::chrono::microseconds time, uint32_t& out_err_raw_size) override;
378+
std::optional<CNetMessage> GetMessage(std::chrono::microseconds time, uint32_t& out_err_raw_size) override;
379379
};
380380

381381
/** The TransportSerializer prepares messages for the network transport
@@ -1283,6 +1283,6 @@ struct NodeEvictionCandidate
12831283
bool m_is_local;
12841284
};
12851285

1286-
[[nodiscard]] Optional<NodeId> SelectNodeToEvict(std::vector<NodeEvictionCandidate>&& vEvictionCandidates);
1286+
[[nodiscard]] std::optional<NodeId> SelectNodeToEvict(std::vector<NodeEvictionCandidate>&& vEvictionCandidates);
12871287

12881288
#endif // BITCOIN_NET_H

src/net_processing.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <validation.h>
3535

3636
#include <memory>
37+
#include <optional>
3738
#include <typeinfo>
3839

3940
/** How long to cache transactions in mapRelay for normal relay */
@@ -4218,7 +4219,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
42184219
if (pto->m_next_local_addr_send != 0us) {
42194220
pto->m_addr_known->reset();
42204221
}
4221-
if (Optional<CAddress> local_addr = GetLocalAddrForPeer(pto)) {
4222+
if (std::optional<CAddress> local_addr = GetLocalAddrForPeer(pto)) {
42224223
FastRandomContext insecure_rand;
42234224
pto->PushAddress(*local_addr, insecure_rand);
42244225
}

0 commit comments

Comments
 (0)