Skip to content

Commit

Permalink
Remove Boost Optional (#1319)
Browse files Browse the repository at this point in the history
* Remove Boost Optional

* Include optional

Co-authored-by: Prasanna Loganathar <[email protected]>
  • Loading branch information
Bushstar and prasannavl authored Jun 3, 2022
1 parent 85ae6a2 commit 3dad9a4
Show file tree
Hide file tree
Showing 59 changed files with 221 additions and 254 deletions.
1 change: 0 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ DEFI_CORE_H = \
node/psbt.h \
node/transaction.h \
noui.h \
optional.h \
outputtype.h \
policy/feerate.h \
policy/fees.h \
Expand Down
7 changes: 4 additions & 3 deletions src/bench/wallet_balance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

#include <bench/bench.h>
#include <interfaces/chain.h>
#include <optional.h>
#include <test/util.h>
#include <validationinterface.h>
#include <wallet/wallet.h>

#include <optional>

static void WalletBalance(benchmark::State& state, const bool set_dirty, const bool add_watchonly, const bool add_mine)
{
const auto& ADDRESS_WATCHONLY = ADDRESS_BCRT1_UNSPENDABLE;
Expand All @@ -22,11 +23,11 @@ static void WalletBalance(benchmark::State& state, const bool set_dirty, const b
}


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) {
generatetoaddress(address_mine.get_value_or(ADDRESS_WATCHONLY));
generatetoaddress(address_mine.value_or(ADDRESS_WATCHONLY));
generatetoaddress(ADDRESS_WATCHONLY);
}
SyncWithValidationInterfaceQueue();
Expand Down
4 changes: 2 additions & 2 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ class CRegTestParams : public CChainParams {
};

/// Check for fork height based flag, validate and set the value to a target var
boost::optional<int> UpdateHeightValidation(const std::string& argName, const std::string& argFlag, int& argTarget) {
std::optional<int> UpdateHeightValidation(const std::string& argName, const std::string& argFlag, int& argTarget) {
if (gArgs.IsArgSet(argFlag)) {
int64_t height = gArgs.GetArg(argFlag, argTarget);
if (height < -1 || height >= std::numeric_limits<int>::max()) {
Expand Down Expand Up @@ -953,7 +953,7 @@ void SetupCommonArgActivationParams(Consensus::Params &consensus) {
UpdateHeightValidation("Dakota Crescent", "-dakotacrescentheight", consensus.DakotaCrescentHeight);
auto eunosHeight = UpdateHeightValidation("Eunos", "-eunosheight", consensus.EunosHeight);
if (eunosHeight.has_value()){
consensus.EunosKampungHeight = static_cast<int>(eunosHeight.get());
consensus.EunosKampungHeight = static_cast<int>(eunosHeight.value());
}
UpdateHeightValidation("Eunos Paya", "-eunospayaheight", consensus.EunosPayaHeight);
UpdateHeightValidation("Fort Canning", "-fortcanningheight", consensus.FortCanningHeight);
Expand Down
15 changes: 8 additions & 7 deletions src/flushablestorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

#include <dbwrapper.h>
#include <functional>
#include <optional.h>
#include <map>
#include <memusage.h>

#include <optional>

#include <boost/thread.hpp>

using TBytes = std::vector<unsigned char>;
using MapKV = std::map<TBytes, Optional<TBytes>>;
using MapKV = std::map<TBytes, std::optional<TBytes>>;

template<typename T>
static TBytes DbTypeToBytes(const T& value) {
Expand Down Expand Up @@ -291,7 +292,7 @@ class CFlushableStorageKV : public CStorageKV {
if (it == changed.end()) {
return db.Read(key, value);
} else if (it->second) {
value = it->second.get();
value = it->second.value();
return true;
} else {
return false;
Expand All @@ -303,7 +304,7 @@ class CFlushableStorageKV : public CStorageKV {
if (!db.Erase(it.first)) {
return false;
}
} else if (!db.Write(it.first, it.second.get())) {
} else if (!db.Write(it.first, it.second.value())) {
return false;
}
}
Expand Down Expand Up @@ -331,7 +332,7 @@ class CFlushableStorageKV : public CStorageKV {

template<typename T>
class CLazySerialize {
Optional<T> value;
std::optional<T> value;
std::unique_ptr<CStorageKVIterator>& it;

public:
Expand Down Expand Up @@ -482,10 +483,10 @@ class CStorageView {
}
// second type of 'ReadBy' (may be 'GetBy'?)
template<typename By, typename ResultType, typename KeyType>
boost::optional<ResultType> ReadBy(KeyType const & id) const {
std::optional<ResultType> ReadBy(KeyType const & id) const {
ResultType result{};
if (ReadBy<By>(id, result))
return {result};
return result;
return {};
}
template<typename By, typename KeyType>
Expand Down
32 changes: 16 additions & 16 deletions src/interfaces/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,28 @@ class LockImpl : public Chain::Lock
LockImpl(CCriticalSection& mutex) : m_mutex(mutex)
{
}
Optional<int> getHeight() override
std::optional<int> getHeight() override
{
LockAssertion lock(m_mutex);
int height = ::ChainActive().Height();
if (height >= 0) {
return height;
}
return nullopt;
return std::nullopt;
}
Optional<int> getBlockHeight(const uint256& hash) override
std::optional<int> getBlockHeight(const uint256& hash) override
{
LockAssertion lock(m_mutex);
CBlockIndex* block = LookupBlockIndex(hash);
if (block && ::ChainActive().Contains(block)) {
return block->nHeight;
}
return nullopt;
return std::nullopt;
}
int getBlockDepth(const uint256& hash) override
{
const Optional<int> tip_height = getHeight();
const Optional<int> height = getBlockHeight(hash);
const std::optional<int> tip_height = getHeight();
const std::optional<int> height = getBlockHeight(hash);
return tip_height && height ? *tip_height - *height + 1 : 0;
}
uint256 getBlockHash(int height) override
Expand Down Expand Up @@ -98,17 +98,17 @@ class LockImpl : public Chain::Lock
CBlockIndex* block = ::ChainActive()[height];
return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0) && block->nTx > 0;
}
Optional<int> findFirstBlockWithTimeAndHeight(int64_t time, int height, uint256* hash) override
std::optional<int> findFirstBlockWithTimeAndHeight(int64_t time, int height, uint256* hash) override
{
LockAssertion lock(m_mutex);
CBlockIndex* block = ::ChainActive().FindEarliestAtLeast(time, height);
if (block) {
if (hash) *hash = block->GetBlockHash();
return block->nHeight;
}
return nullopt;
return std::nullopt;
}
Optional<int> findPruned(int start_height, Optional<int> stop_height) override
std::optional<int> findPruned(int start_height, std::optional<int> stop_height) override
{
LockAssertion lock(m_mutex);
if (::fPruneMode) {
Expand All @@ -120,9 +120,9 @@ class LockImpl : public Chain::Lock
block = block->pprev;
}
}
return nullopt;
return std::nullopt;
}
Optional<int> findFork(const uint256& hash, Optional<int>* height) override
std::optional<int> findFork(const uint256& hash, std::optional<int>* height) override
{
LockAssertion lock(m_mutex);
const CBlockIndex* block = LookupBlockIndex(hash);
Expand All @@ -137,20 +137,20 @@ class LockImpl : public Chain::Lock
if (fork) {
return fork->nHeight;
}
return nullopt;
return std::nullopt;
}
CBlockLocator getTipLocator() override
{
LockAssertion lock(m_mutex);
return ::ChainActive().GetLocator();
}
Optional<int> findLocatorFork(const CBlockLocator& locator) override
std::optional<int> findLocatorFork(const CBlockLocator& locator) override
{
LockAssertion lock(m_mutex);
if (CBlockIndex* fork = FindForkInGlobalIndex(::ChainActive(), locator)) {
return fork->nHeight;
}
return nullopt;
return std::nullopt;
}
bool checkFinalTx(const CTransaction& tx) override
{
Expand Down Expand Up @@ -280,12 +280,12 @@ class ChainImpl : public Chain
return pcustomcsview->CanSpend(nodeId, height);
}

boost::optional<CMasternode> mnExists(const uint256 & nodeId) const override
std::optional<CMasternode> mnExists(const uint256 & nodeId) const override
{
LOCK(cs_main);
return pcustomcsview->GetMasternode(nodeId);
}
boost::optional<CTokensView::CTokenImpl> existTokenGuessId(const std::string & str, DCT_ID & id) const override
std::optional<CTokensView::CTokenImpl> existTokenGuessId(const std::string & str, DCT_ID & id) const override
{
LOCK(cs_main);
return pcustomcsview->GetTokenGuessId(str, id);
Expand Down
19 changes: 10 additions & 9 deletions src/interfaces/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
#ifndef DEFI_INTERFACES_CHAIN_H
#define DEFI_INTERFACES_CHAIN_H

#include <optional.h> // For Optional and nullopt
#include <primitives/transaction.h> // For CTransactionRef
#include <sync.h>

#include <functional>
#include <memory>
#include <optional>
#include <stddef.h>
#include <stdint.h>
#include <string>
Expand Down Expand Up @@ -71,12 +72,12 @@ 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 height above genesis block. Returns 0 for genesis block,
//! 1 for following block, and so on. Returns nullopt for a block not
//! included in the current chain.
virtual Optional<int> getBlockHeight(const uint256& hash) = 0;
virtual std::optional<int> getBlockHeight(const uint256& hash) = 0;

//! Get block depth. Returns 1 for chain tip, 2 for preceding block, and
//! so on. Returns 0 for a block not included in the current chain.
Expand All @@ -101,27 +102,27 @@ class Chain
//! given height, or nullopt if there is no block with a high enough
//! timestamp and height. Also return the block hash as an optional output parameter
//! (to avoid the cost of a second lookup in case this information is needed.)
virtual Optional<int> findFirstBlockWithTimeAndHeight(int64_t time, int height, uint256* hash) = 0;
virtual std::optional<int> findFirstBlockWithTimeAndHeight(int64_t time, int height, uint256* hash) = 0;

//! Return height of last block in the specified range which is pruned, or
//! nullopt if no block in the range is pruned. Range is inclusive.
virtual Optional<int> findPruned(int start_height = 0, Optional<int> stop_height = nullopt) = 0;
virtual std::optional<int> findPruned(int start_height = 0, std::optional<int> stop_height = std::nullopt) = 0;

//! Return height of the specified block if it is on the chain, otherwise
//! return the height of the highest block on chain that's an ancestor
//! of the specified block, or nullopt if there is no common ancestor.
//! Also return the height of the specified block as an optional output
//! parameter (to avoid the cost of a second hash lookup in case this
//! information is desired).
virtual Optional<int> findFork(const uint256& hash, Optional<int>* height) = 0;
virtual std::optional<int> findFork(const uint256& hash, std::optional<int>* height) = 0;

//! Get locator for the current chain tip.
virtual CBlockLocator getTipLocator() = 0;

//! 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 @@ -151,8 +152,8 @@ class Chain
virtual void findCoins(std::map<COutPoint, Coin>& coins) = 0;

virtual bool mnCanSpend(const uint256 & nodeId, int height) const = 0;
virtual boost::optional<CMasternode> mnExists(const uint256 & nodeId) const = 0;
virtual boost::optional<CTokenImplementation> existTokenGuessId(const std::string & str, DCT_ID & id) const = 0;
virtual std::optional<CMasternode> mnExists(const uint256 & nodeId) const = 0;
virtual std::optional<CTokenImplementation> existTokenGuessId(const std::string & str, DCT_ID & id) const = 0;

//! Estimate fraction of total transactions verified if blocks up to
//! the specified block hash are verified.
Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ Res CAccountsView::EraseFuturesUserValues(const CFuturesUserKey& key)
return Res::Ok();
}

boost::optional<uint32_t> CAccountsView::GetMostRecentFuturesHeight()
std::optional<uint32_t> CAccountsView::GetMostRecentFuturesHeight()
{
const CFuturesUserKey key{std::numeric_limits<uint32_t>::max(), {}, std::numeric_limits<uint32_t>::max()};
auto it = LowerBound<ByFuturesSwapKey>(key);
Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/accounts.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class CAccountsView : public virtual CStorageView
Res StoreFuturesUserValues(const CFuturesUserKey& key, const CFuturesUserValue& futures);
ResVal<CFuturesUserValue> GetFuturesUserValues(const CFuturesUserKey& key);
Res EraseFuturesUserValues(const CFuturesUserKey& key);
boost::optional<uint32_t> GetMostRecentFuturesHeight();
std::optional<uint32_t> GetMostRecentFuturesHeight();
void ForEachFuturesUserValues(std::function<bool(const CFuturesUserKey&, const CFuturesUserValue&)> callback, const CFuturesUserKey& start =
{std::numeric_limits<uint32_t>::max(), {}, std::numeric_limits<uint32_t>::max()});

Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/accountshistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Res CAccountsHistoryView::WriteAccountHistory(const AccountHistoryKey& key, cons
return Res::Ok();
}

boost::optional<AccountHistoryValue> CAccountsHistoryView::ReadAccountHistory(AccountHistoryKey const & key) const
std::optional<AccountHistoryValue> CAccountsHistoryView::ReadAccountHistory(AccountHistoryKey const & key) const
{
return ReadBy<ByAccountHistoryKey, AccountHistoryValue>(key);
}
Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/accountshistory.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class CAccountsHistoryView : public virtual CStorageView
{
public:
Res WriteAccountHistory(AccountHistoryKey const & key, AccountHistoryValue const & value);
boost::optional<AccountHistoryValue> ReadAccountHistory(AccountHistoryKey const & key) const;
std::optional<AccountHistoryValue> ReadAccountHistory(AccountHistoryKey const & key) const;
Res EraseAccountHistory(AccountHistoryKey const & key);
void ForEachAccountHistory(std::function<bool(AccountHistoryKey const &, CLazySerialize<AccountHistoryValue>)> callback, AccountHistoryKey const & start = {});

Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/anchors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ uint256 CAnchorConfirmDataPlus::GetSignHash() const
return Hash(ss.begin(), ss.end());
}

boost::optional<CAnchorConfirmMessage> CAnchorConfirmMessage::CreateSigned(const CAnchor& anchor, const THeight prevAnchorHeight,
std::optional<CAnchorConfirmMessage> CAnchorConfirmMessage::CreateSigned(const CAnchor& anchor, const THeight prevAnchorHeight,
const uint256 &btcTxHash, CKey const & key, const THeight btcTxHeight)
{
// Potential post-fork unrewarded anchor
Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/anchors.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ class CAnchorConfirmMessage : public CAnchorConfirmDataPlus
, signature()
{}

static boost::optional<CAnchorConfirmMessage> CreateSigned(const CAnchor &anchor, const THeight prevAnchorHeight,
static std::optional<CAnchorConfirmMessage> CreateSigned(const CAnchor &anchor, const THeight prevAnchorHeight,
const uint256 &btcTxHash, CKey const & key, const THeight btcTxHeight);
uint256 GetHash() const;
CKeyID GetSigner() const;
Expand Down
Loading

0 comments on commit 3dad9a4

Please sign in to comment.