Skip to content

Commit

Permalink
Replace boost::optional with std::optional
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Jul 17, 2020
1 parent 25a225f commit aa1567f
Show file tree
Hide file tree
Showing 64 changed files with 531 additions and 491 deletions.
27 changes: 14 additions & 13 deletions src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "tinyformat.h"
#include "uint256.h"

#include <optional>
#include <vector>

static const int SPROUT_VALUE_VERSION = 1001400;
Expand Down Expand Up @@ -228,7 +229,7 @@ class CBlockIndex
//! Branch ID corresponding to the consensus rules used to validate this block.
//! Only cached if block validity is BLOCK_VALID_CONSENSUS.
//! Persisted at each activation height, memory-only for intervening blocks.
boost::optional<uint32_t> nCachedBranchId;
std::optional<uint32_t> nCachedBranchId;

//! The anchor for the tree state up to the start of this block
uint256 hashSproutAnchor;
Expand All @@ -237,22 +238,22 @@ class CBlockIndex
uint256 hashFinalSproutRoot;

//! Change in value held by the Sprout circuit over this block.
//! Will be boost::none for older blocks on old nodes until a reindex has taken place.
boost::optional<CAmount> nSproutValue;
//! Will be std::nullopt for older blocks on old nodes until a reindex has taken place.
std::optional<CAmount> nSproutValue;

//! (memory only) Total value held by the Sprout circuit up to and including this block.
//! Will be boost::none for on old nodes until a reindex has taken place.
//! Will be boost::none if nChainTx is zero.
boost::optional<CAmount> nChainSproutValue;
//! Will be std::nullopt for on old nodes until a reindex has taken place.
//! Will be std::nullopt if nChainTx is zero.
std::optional<CAmount> nChainSproutValue;

//! Change in value held by the Sapling circuit over this block.
//! Not a boost::optional because this was added before Sapling activated, so we can
//! Not a std::optional because this was added before Sapling activated, so we can
//! rely on the invariant that every block before this was added had nSaplingValue = 0.
CAmount nSaplingValue;

//! (memory only) Total value held by the Sapling circuit up to and including this block.
//! Will be boost::none if nChainTx is zero.
boost::optional<CAmount> nChainSaplingValue;
//! Will be std::nullopt if nChainTx is zero.
std::optional<CAmount> nChainSaplingValue;

//! Root of the Sapling commitment tree as of the end of this block.
//!
Expand Down Expand Up @@ -295,14 +296,14 @@ class CBlockIndex
nTx = 0;
nChainTx = 0;
nStatus = 0;
nCachedBranchId = boost::none;
nCachedBranchId = std::nullopt;
hashSproutAnchor = uint256();
hashFinalSproutRoot = uint256();
nSequenceId = 0;
nSproutValue = boost::none;
nChainSproutValue = boost::none;
nSproutValue = std::nullopt;
nChainSproutValue = std::nullopt;
nSaplingValue = 0;
nChainSaplingValue = boost::none;
nChainSaplingValue = std::nullopt;

nVersion = 0;
hashMerkleRoot = uint256();
Expand Down
3 changes: 2 additions & 1 deletion src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "utilstrencodings.h"

#include <assert.h>
#include <optional>
#include <variant>

#include <boost/assign/list_of.hpp>
Expand Down Expand Up @@ -104,7 +105,7 @@ class CMainParams : public CChainParams {
consensus.nPowMaxAdjustUp = 16; // 16% adjustment up
consensus.nPreBlossomPowTargetSpacing = Consensus::PRE_BLOSSOM_POW_TARGET_SPACING;
consensus.nPostBlossomPowTargetSpacing = Consensus::POST_BLOSSOM_POW_TARGET_SPACING;
consensus.nPowAllowMinDifficultyBlocksAfterHeight = boost::none;
consensus.nPowAllowMinDifficultyBlocksAfterHeight = std::nullopt;
consensus.vUpgrades[Consensus::BASE_SPROUT].nProtocolVersion = 170002;
consensus.vUpgrades[Consensus::BASE_SPROUT].nActivationHeight =
Consensus::NetworkUpgrade::ALWAYS_ACTIVE;
Expand Down
6 changes: 3 additions & 3 deletions src/consensus/funding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ std::set<FundingStreamElement> GetActiveFundingStreamElements(
// in the definition of vFundingStreams.
auto fs = params.vFundingStreams[idx];
// Funding period is [startHeight, endHeight)
if (fs && nHeight >= fs.get().GetStartHeight() && nHeight < fs.get().GetEndHeight()) {
if (fs && nHeight >= fs.value().GetStartHeight() && nHeight < fs.value().GetEndHeight()) {
requiredElements.insert(std::make_pair(
fs.get().RecipientAddress(params, nHeight),
fs.value().RecipientAddress(params, nHeight),
FundingStreamInfo[idx].Value(blockSubsidy)));
}
}
Expand All @@ -64,7 +64,7 @@ std::vector<FSInfo> GetActiveFundingStreams(
std::vector<FSInfo> activeStreams;
for (uint32_t idx = Consensus::FIRST_FUNDING_STREAM; idx < Consensus::MAX_FUNDING_STREAMS; idx++) {
auto fs = params.vFundingStreams[idx];
if (fs && nHeight >= fs.get().GetStartHeight() && nHeight < fs.get().GetEndHeight()) {
if (fs && nHeight >= fs.value().GetStartHeight() && nHeight < fs.value().GetEndHeight()) {
activeStreams.push_back(FundingStreamInfo[idx]);
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
#include "key_constants.h"
#include <zcash/address/sapling.hpp>

#include <optional>
#include <variant>

#include <boost/optional.hpp>

namespace Consensus {

// Early declaration to ensure it is accessible.
Expand Down Expand Up @@ -80,7 +79,7 @@ struct NetworkUpgrade {
* scrutiny than regular releases. nMinimumChainWork MUST be set to at least the chain
* work of this block, otherwise this detection will have false positives.
*/
boost::optional<uint256> hashActivationBlock;
std::optional<uint256> hashActivationBlock;
};

typedef std::variant<libzcash::SaplingPaymentAddress, CScript> FundingStreamAddress;
Expand Down Expand Up @@ -211,7 +210,7 @@ struct Params {
NetworkUpgrade vUpgrades[MAX_NETWORK_UPGRADES];

int nFundingPeriodLength;
boost::optional<FundingStream> vFundingStreams[MAX_FUNDING_STREAMS];
std::optional<FundingStream> vFundingStreams[MAX_FUNDING_STREAMS];
void AddZIP207FundingStream(
const KeyConstants& keyConstants,
FundingStreamIndex idx,
Expand Down Expand Up @@ -263,7 +262,7 @@ struct Params {
unsigned int nEquihashN = 0;
unsigned int nEquihashK = 0;
uint256 powLimit;
boost::optional<uint32_t> nPowAllowMinDifficultyBlocksAfterHeight;
std::optional<uint32_t> nPowAllowMinDifficultyBlocksAfterHeight;
int64_t nPowAveragingWindow;
int64_t nPowMaxAdjustDown;
int64_t nPowMaxAdjustUp;
Expand Down
12 changes: 6 additions & 6 deletions src/consensus/upgrades.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ bool IsActivationHeightForAnyUpgrade(
return false;
}

boost::optional<int> NextEpoch(int nHeight, const Consensus::Params& params) {
std::optional<int> NextEpoch(int nHeight, const Consensus::Params& params) {
if (nHeight < 0) {
return boost::none;
return std::nullopt;
}

// Sprout is never pending
Expand All @@ -152,16 +152,16 @@ boost::optional<int> NextEpoch(int nHeight, const Consensus::Params& params) {
}
}

return boost::none;
return std::nullopt;
}

boost::optional<int> NextActivationHeight(
std::optional<int> NextActivationHeight(
int nHeight,
const Consensus::Params& params)
{
auto idx = NextEpoch(nHeight, params);
if (idx) {
return params.vUpgrades[idx.get()].nActivationHeight;
return params.vUpgrades[idx.value()].nActivationHeight;
}
return boost::none;
return std::nullopt;
}
10 changes: 5 additions & 5 deletions src/consensus/upgrades.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "consensus/params.h"

#include <boost/optional.hpp>
#include <optional>

enum UpgradeState {
UPGRADE_DISABLED,
Expand Down Expand Up @@ -83,15 +83,15 @@ bool IsActivationHeightForAnyUpgrade(

/**
* Returns the index of the next upgrade after the given block height, or
* boost::none if there are no more known upgrades.
* std::nullopt if there are no more known upgrades.
*/
boost::optional<int> NextEpoch(int nHeight, const Consensus::Params& params);
std::optional<int> NextEpoch(int nHeight, const Consensus::Params& params);

/**
* Returns the activation height for the next upgrade after the given block height,
* or boost::none if there are no more known upgrades.
* or std::nullopt if there are no more known upgrades.
*/
boost::optional<int> NextActivationHeight(
std::optional<int> NextActivationHeight(
int nHeight,
const Consensus::Params& params);

Expand Down
9 changes: 4 additions & 5 deletions src/crypto/equihash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@

#include <algorithm>
#include <iostream>
#include <optional>
#include <stdexcept>

#include <boost/optional.hpp>

static EhSolverCancelledException solver_cancelled;

eh_HashState::eh_HashState(
Expand Down Expand Up @@ -657,7 +656,7 @@ bool Equihash<N,K>::OptimisedSolve(const eh_HashState& base_state,
size_t hashLen;
size_t lenIndices;
unsigned char tmpHash[HashOutput];
std::vector<boost::optional<std::vector<FullStepRow<FinalFullWidth>>>> X;
std::vector<std::optional<std::vector<FullStepRow<FinalFullWidth>>>> X;
X.reserve(K+1);

// 3) Repeat steps 1 and 2 for each partial index
Expand All @@ -675,7 +674,7 @@ bool Equihash<N,K>::OptimisedSolve(const eh_HashState& base_state,
N/8, HashLength, CollisionBitLength, newIndex);
if (cancelled(PartialGeneration)) throw solver_cancelled;
}
boost::optional<std::vector<FullStepRow<FinalFullWidth>>> ic = icv;
std::optional<std::vector<FullStepRow<FinalFullWidth>>> ic = icv;

// 2a) For each pair of lists:
hashLen = HashLength;
Expand All @@ -700,7 +699,7 @@ bool Equihash<N,K>::OptimisedSolve(const eh_HashState& base_state,
if (ic->size() == 0)
goto invalidsolution;

X[r] = boost::none;
X[r] = std::nullopt;
hashLen -= CollisionByteLength;
lenIndices *= 2;
rti = lti;
Expand Down
4 changes: 2 additions & 2 deletions src/experimental_features.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ bool fExperimentalPaymentDisclosure = false;
bool fExperimentalInsightExplorer = false;
bool fExperimentalLightWalletd = false;

boost::optional<std::string> InitExperimentalMode()
std::optional<std::string> InitExperimentalMode()
{
auto fExperimentalMode = GetBoolArg("-experimentalfeatures", false);
fExperimentalDeveloperEncryptWallet = GetBoolArg("-developerencryptwallet", false);
Expand All @@ -35,7 +35,7 @@ boost::optional<std::string> InitExperimentalMode()
return _("Light Walletd requires -experimentalfeatures.");
}
}
return boost::none;
return std::nullopt;
}

std::vector<std::string> GetExperimentalFeatures()
Expand Down
4 changes: 2 additions & 2 deletions src/experimental_features.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://www.opensource.org/licenses/mit-license.php .

#include <optional>
#include <string>
#include <vector>
#include <boost/optional.hpp>

extern bool fExperimentalDeveloperEncryptWallet;
extern bool fExperimentalDeveloperSetPoolSizeZero;
extern bool fExperimentalPaymentDisclosure;
extern bool fExperimentalInsightExplorer;
extern bool fExperimentalLightWalletd;

boost::optional<std::string> InitExperimentalMode();
std::optional<std::string> InitExperimentalMode();
std::vector<std::string> GetExperimentalFeatures();
4 changes: 2 additions & 2 deletions src/gtest/test_checktransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,7 @@ TEST(ChecktransactionTests, HeartwoodAcceptsShieldedCoinbase) {
auto output = OutputDescriptionInfo(ovk, note, {{0xF6}});

auto ctx = librustzcash_sapling_proving_ctx_init();
auto odesc = output.Build(ctx).get();
auto odesc = output.Build(ctx).value();
librustzcash_sapling_proving_ctx_free(ctx);

CMutableTransaction mtx = GetValidTransaction();
Expand Down Expand Up @@ -1244,7 +1244,7 @@ TEST(ChecktransactionTests, HeartwoodEnforcesSaplingRulesOnShieldedCoinbase) {

// Add a Sapling output.
auto ctx = librustzcash_sapling_proving_ctx_init();
auto odesc = output.Build(ctx).get();
auto odesc = output.Build(ctx).value();
librustzcash_sapling_proving_ctx_free(ctx);
mtx.vShieldedOutput.push_back(odesc);

Expand Down
4 changes: 2 additions & 2 deletions src/gtest/test_foundersreward.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ void checkNumberOfUniqueAddresses(int nUnique) {
int GetMaxFundingStreamHeight(const Consensus::Params& params) {
int result = 0;
for (auto fs : params.vFundingStreams) {
if (fs && result < fs.get().GetEndHeight() - 1) {
result = fs.get().GetEndHeight() - 1;
if (fs && result < fs.value().GetEndHeight() - 1) {
result = fs.value().GetEndHeight() - 1;
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/gtest/test_mempoollimit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ TEST(MempoolLimitTests, WeightedTxTreeCheckSizeAfterDropping)
tree.add(WeightedTxInfo(TX_ID2, TxWeight(MIN_TX_COST, MIN_TX_COST)));
EXPECT_EQ(8000, tree.getTotalWeight().cost);
EXPECT_EQ(8000, tree.getTotalWeight().evictionWeight);
EXPECT_FALSE(tree.maybeDropRandom().is_initialized());
EXPECT_FALSE(tree.maybeDropRandom().has_value());
tree.add(WeightedTxInfo(TX_ID3, TxWeight(MIN_TX_COST, MIN_TX_COST + LOW_FEE_PENALTY)));
EXPECT_EQ(12000, tree.getTotalWeight().cost);
EXPECT_EQ(12000 + LOW_FEE_PENALTY, tree.getTotalWeight().evictionWeight);
boost::optional<uint256> drop = tree.maybeDropRandom();
ASSERT_TRUE(drop.is_initialized());
uint256 txid = drop.get();
auto drop = tree.maybeDropRandom();
ASSERT_TRUE(drop.has_value());
uint256 txid = drop.value();
testedDropping.insert(txid);
// Do not continue to test if a particular trial fails
ASSERT_EQ(8000, tree.getTotalWeight().cost);
Expand Down
Loading

0 comments on commit aa1567f

Please sign in to comment.