Skip to content

Commit

Permalink
scripted-diff: remove Optional & nullopt
Browse files Browse the repository at this point in the history
-BEGIN VERIFY SCRIPT-
git rm src/optional.h

sed -i -e 's/Optional</std::optional</g' $(git grep -l 'Optional<' src)

sed -i -e 's/{nullopt}/{std::nullopt}/g' $(git grep -l 'nullopt' src)
sed -i -e 's/ nullopt;/ std::nullopt;/g' $(git grep -l 'nullopt' src)
sed -i -e 's/ nullopt)/ std::nullopt)/g' $(git grep -l 'nullopt' src)
sed -i -e 's/(nullopt)/(std::nullopt)/g' $(git grep -l 'nullopt' src)
sed -i -e 's/ nullopt,/ std::nullopt,/g' $(git grep -l 'nullopt' src)
sed -i -e 's/? nullopt :/? std::nullopt :/g' $(git grep -l 'nullopt' src)
sed -i -e 's/: nullopt}/: std::nullopt}/g' $(git grep -l 'nullopt' src)

sed -i -e '/optional.h \\/d' src/Makefile.am

sed -i -e '/#include <optional.h>/d' src/test/fuzz/autofile.cpp src/test/fuzz/buffered_file.cpp src/test/fuzz/node_eviction.cpp

sed -i -e 's/#include <optional.h>/#include <optional>/g' $(git grep -l '#include <optional.h>' src)
-END VERIFY SCRIPT-
  • Loading branch information
fanquake committed Mar 15, 2021
1 parent 3c63191 commit 57e980d
Show file tree
Hide file tree
Showing 49 changed files with 165 additions and 189 deletions.
1 change: 0 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
4 changes: 2 additions & 2 deletions src/bench/wallet_balance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <bench/bench.h>
#include <interfaces/chain.h>
#include <node/context.h>
#include <optional.h>
#include <optional>
#include <test/util/mining.h>
#include <test/util/setup_common.h>
#include <test/util/wallet.h>
Expand All @@ -26,7 +26,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<std::string> address_mine{add_mine ? Optional<std::string>{getnewaddress(wallet)} : nullopt};
const std::optional<std::string> address_mine{add_mine ? std::optional<std::string>{getnewaddress(wallet)} : std::nullopt};
if (add_watchonly) importaddress(wallet, ADDRESS_WATCHONLY);

for (int i = 0; i < 100; ++i) {
Expand Down
10 changes: 5 additions & 5 deletions src/bitcoin-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include <chainparamsbase.h>
#include <clientversion.h>
#include <optional.h>
#include <optional>
#include <rpc/client.h>
#include <rpc/mining.h>
#include <rpc/protocol.h>
Expand Down Expand Up @@ -611,7 +611,7 @@ class DefaultRequestHandler: public BaseRequestHandler {
}
};

static UniValue CallRPC(BaseRequestHandler* rh, const std::string& strMethod, const std::vector<std::string>& args, const Optional<std::string>& rpcwallet = {})
static UniValue CallRPC(BaseRequestHandler* rh, const std::string& strMethod, const std::vector<std::string>& args, const std::optional<std::string>& rpcwallet = {})
{
std::string host;
// In preference order, we choose the following for the port:
Expand Down Expand Up @@ -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<std::string>& args, const Optional<std::string>& rpcwallet = {})
static UniValue ConnectAndCallRPC(BaseRequestHandler* rh, const std::string& strMethod, const std::vector<std::string>& args, const std::optional<std::string>& rpcwallet = {})
{
UniValue response(UniValue::VOBJ);
// Execute and handle connection failures with -rpcwait.
Expand Down Expand Up @@ -817,7 +817,7 @@ static void GetWalletBalances(UniValue& result)
*/
static UniValue GetNewAddress()
{
Optional<std::string> wallet_name{};
std::optional<std::string> wallet_name{};
if (gArgs.IsArgSet("-rpcwallet")) wallet_name = gArgs.GetArg("-rpcwallet", "");
DefaultRequestHandler rh;
return ConnectAndCallRPC(&rh, "getnewaddress", /* args=*/{}, wallet_name);
Expand Down Expand Up @@ -922,7 +922,7 @@ static int CommandLineRPC(int argc, char *argv[])
}
if (nRet == 0) {
// Perform RPC call
Optional<std::string> wallet_name{};
std::optional<std::string> wallet_name{};
if (gArgs.IsArgSet("-rpcwallet")) wallet_name = gArgs.GetArg("-rpcwallet", "");
const UniValue reply = ConnectAndCallRPC(rh.get(), method, args, wallet_name);

Expand Down
8 changes: 4 additions & 4 deletions src/interfaces/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifndef BITCOIN_INTERFACES_CHAIN_H
#define BITCOIN_INTERFACES_CHAIN_H

#include <optional.h> // For Optional and nullopt
#include <optional> // For Optional and nullopt
#include <primitives/transaction.h> // For CTransactionRef
#include <util/settings.h> // For util::SettingsValue

Expand Down Expand Up @@ -94,7 +94,7 @@ class Chain
//! 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<int> getHeight() = 0;
virtual std::optional<int> getHeight() = 0;

//! Get block hash. Height must be valid or this function will abort.
virtual uint256 getBlockHash(int height) = 0;
Expand All @@ -109,7 +109,7 @@ class Chain
//! 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<int> findLocatorFork(const CBlockLocator& locator) = 0;
virtual std::optional<int> findLocatorFork(const CBlockLocator& locator) = 0;

//! Check if transaction will be final given chain height current time.
virtual bool checkFinalTx(const CTransaction& tx) = 0;
Expand Down Expand Up @@ -154,7 +154,7 @@ class Chain
//! 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<int> max_height = {}) = 0;
virtual bool hasBlocks(const uint256& block_hash, int min_height = 0, std::optional<int> max_height = {}) = 0;

//! Check if transaction is RBF opt in.
virtual RBFTransactionState isRBFOptIn(const CTransaction& tx) = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ void BlockAssembler::resetBlock()
nFees = 0;
}

Optional<int64_t> BlockAssembler::m_last_block_num_txs{nullopt};
Optional<int64_t> BlockAssembler::m_last_block_weight{nullopt};
std::optional<int64_t> BlockAssembler::m_last_block_num_txs{std::nullopt};
std::optional<int64_t> BlockAssembler::m_last_block_weight{std::nullopt};

std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(CChainState& chainstate, const CScript& scriptPubKeyIn)
{
Expand Down
6 changes: 3 additions & 3 deletions src/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#ifndef BITCOIN_MINER_H
#define BITCOIN_MINER_H

#include <optional.h>
#include <optional>
#include <primitives/block.h>
#include <txmempool.h>
#include <validation.h>
Expand Down Expand Up @@ -160,8 +160,8 @@ class BlockAssembler
/** Construct a new block template with coinbase to scriptPubKeyIn */
std::unique_ptr<CBlockTemplate> CreateNewBlock(CChainState& chainstate, const CScript& scriptPubKeyIn);

static Optional<int64_t> m_last_block_num_txs;
static Optional<int64_t> m_last_block_weight;
static std::optional<int64_t> m_last_block_num_txs;
static std::optional<int64_t> m_last_block_weight;

private:
// utility functions
Expand Down
22 changes: 11 additions & 11 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <net_permissions.h>
#include <netbase.h>
#include <node/ui_interface.h>
#include <optional.h>
#include <optional>
#include <protocol.h>
#include <random.h>
#include <scheduler.h>
Expand Down Expand Up @@ -193,7 +193,7 @@ bool IsPeerAddrLocalGood(CNode *pnode)
IsReachable(addrLocal.GetNetwork());
}

Optional<CAddress> GetLocalAddrForPeer(CNode *pnode)
std::optional<CAddress> GetLocalAddrForPeer(CNode *pnode)
{
CAddress addrLocal = GetLocalAddress(&pnode->addr, pnode->GetLocalServices());
if (gArgs.GetBoolArg("-addrmantest", false)) {
Expand All @@ -215,7 +215,7 @@ Optional<CAddress> GetLocalAddrForPeer(CNode *pnode)
return addrLocal;
}
// Address is unroutable. Don't advertise.
return nullopt;
return std::nullopt;
}

// learn a new local address
Expand Down Expand Up @@ -632,7 +632,7 @@ bool CNode::ReceiveMsgBytes(Span<const uint8_t> msg_bytes, bool& complete)
if (m_deserializer->Complete()) {
// decompose a transport agnostic CNetMessage from the deserializer
uint32_t out_err_raw_size{0};
Optional<CNetMessage> result{m_deserializer->GetMessage(time, out_err_raw_size)};
std::optional<CNetMessage> 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
Expand Down Expand Up @@ -723,10 +723,10 @@ const uint256& V1TransportDeserializer::GetMessageHash() const
return data_hash;
}

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

// store command string, time, and sizes
msg->m_command = hdr.GetCommand();
Expand All @@ -747,12 +747,12 @@ Optional<CNetMessage> 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 = std::nullopt;
}

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

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

Expand Down Expand Up @@ -918,7 +918,7 @@ static void EraseLastKElements(std::vector<T> &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)
Expand Down Expand Up @@ -989,7 +989,7 @@ bool CConnman::AttemptToEvictConnection()
vEvictionCandidates.push_back(candidate);
}
}
const Optional<NodeId> node_id_to_evict = SelectNodeToEvict(std::move(vEvictionCandidates));
const std::optional<NodeId> node_id_to_evict = SelectNodeToEvict(std::move(vEvictionCandidates));
if (!node_id_to_evict) {
return false;
}
Expand Down
10 changes: 5 additions & 5 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <net_permissions.h>
#include <netaddress.h>
#include <netbase.h>
#include <optional.h>
#include <optional>
#include <policy/feerate.h>
#include <protocol.h>
#include <random.h>
Expand Down Expand Up @@ -200,7 +200,7 @@ enum

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

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

Expand Down Expand Up @@ -375,7 +375,7 @@ class V1TransportDeserializer final : public TransportDeserializer
}
return ret;
}
Optional<CNetMessage> GetMessage(std::chrono::microseconds time, uint32_t& out_err_raw_size) override;
std::optional<CNetMessage> GetMessage(std::chrono::microseconds time, uint32_t& out_err_raw_size) override;
};

/** The TransportSerializer prepares messages for the network transport
Expand Down Expand Up @@ -1283,6 +1283,6 @@ struct NodeEvictionCandidate
bool m_is_local;
};

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

#endif // BITCOIN_NET_H
2 changes: 1 addition & 1 deletion src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4222,7 +4222,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
if (pto->m_next_local_addr_send != 0us) {
pto->m_addr_known->reset();
}
if (Optional<CAddress> local_addr = GetLocalAddrForPeer(pto)) {
if (std::optional<CAddress> local_addr = GetLocalAddrForPeer(pto)) {
FastRandomContext insecure_rand;
pto->PushAddress(*local_addr, insecure_rand);
}
Expand Down
10 changes: 5 additions & 5 deletions src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,15 +415,15 @@ class ChainImpl : public Chain
{
public:
explicit ChainImpl(NodeContext& node) : m_node(node) {}
Optional<int> getHeight() override
std::optional<int> getHeight() override
{
LOCK(::cs_main);
const CChain& active = Assert(m_node.chainman)->ActiveChain();
int height = active.Height();
if (height >= 0) {
return height;
}
return nullopt;
return std::nullopt;
}
uint256 getBlockHash(int height) override
{
Expand Down Expand Up @@ -452,15 +452,15 @@ class ChainImpl : public Chain
assert(std::addressof(::ChainActive()) == std::addressof(m_node.chainman->ActiveChain()));
return CheckFinalTx(m_node.chainman->ActiveChain().Tip(), tx);
}
Optional<int> findLocatorFork(const CBlockLocator& locator) override
std::optional<int> findLocatorFork(const CBlockLocator& locator) override
{
LOCK(cs_main);
const CChain& active = Assert(m_node.chainman)->ActiveChain();
assert(std::addressof(g_chainman) == std::addressof(*m_node.chainman));
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
{
Expand Down Expand Up @@ -518,7 +518,7 @@ class ChainImpl : public Chain
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<int> max_height) override
bool hasBlocks(const uint256& block_hash, int min_height, std::optional<int> 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
Expand Down
12 changes: 6 additions & 6 deletions src/node/psbt.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ struct PSBTInputAnalysis {
* Holds the results of AnalyzePSBT (miscellaneous information about a PSBT)
*/
struct PSBTAnalysis {
Optional<size_t> estimated_vsize; //!< Estimated weight of the transaction
Optional<CFeeRate> estimated_feerate; //!< Estimated feerate (fee / weight) of the transaction
Optional<CAmount> fee; //!< Amount of fee being paid by the transaction
std::optional<size_t> estimated_vsize; //!< Estimated weight of the transaction
std::optional<CFeeRate> estimated_feerate; //!< Estimated feerate (fee / weight) of the transaction
std::optional<CAmount> fee; //!< Amount of fee being paid by the transaction
std::vector<PSBTInputAnalysis> 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;
Expand Down
20 changes: 0 additions & 20 deletions src/optional.h

This file was deleted.

4 changes: 2 additions & 2 deletions src/psbt.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include <attributes.h>
#include <node/transaction.h>
#include <optional.h>
#include <optional>
#include <policy/feerate.h>
#include <primitives/transaction.h>
#include <pubkey.h>
Expand Down Expand Up @@ -389,7 +389,7 @@ struct PSBTOutput
/** A version of CTransaction with the PSBT format*/
struct PartiallySignedTransaction
{
Optional<CMutableTransaction> tx;
std::optional<CMutableTransaction> tx;
std::vector<PSBTInput> inputs;
std::vector<PSBTOutput> outputs;
std::map<std::vector<unsigned char>, std::vector<unsigned char>> unknown;
Expand Down
2 changes: 1 addition & 1 deletion src/qt/test/addressbooktests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void TestAddAddressesToSendBook(interfaces::Node& node)
ClientModel clientModel(node, &optionsModel);
AddWallet(wallet);
WalletModel walletModel(interfaces::MakeWallet(wallet), clientModel, platformStyle.get());
RemoveWallet(wallet, nullopt);
RemoveWallet(wallet, std::nullopt);
EditAddressDialog editAddressDialog(EditAddressDialog::NewSendingAddress);
editAddressDialog.setModel(walletModel.getAddressTableModel());

Expand Down
Loading

0 comments on commit 57e980d

Please sign in to comment.