Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Boost variant #1334

Merged
merged 2 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ std::vector<CTransactionRef> CChainParams::CreateGenesisMasternodes()
txNew.vin[0].scriptSig = CScript(); // << 486604799 << CScriptNum(4) << std::vector<unsigned char>((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));

CTxDestination operatorDest = DecodeDestination(addrs.operatorAddress, *this);
assert(operatorDest.which() == PKHashType || operatorDest.which() == WitV0KeyHashType);
assert(operatorDest.index() == PKHashType || operatorDest.index() == WitV0KeyHashType);
CTxDestination ownerDest = DecodeDestination(addrs.ownerAddress, *this);
assert(ownerDest.which() == PKHashType || ownerDest.which() == WitV0KeyHashType);
assert(ownerDest.index() == PKHashType || ownerDest.index() == WitV0KeyHashType);

CKeyID operatorAuthKey = operatorDest.which() == PKHashType ? CKeyID(*boost::get<PKHash>(&operatorDest)) : CKeyID(*boost::get<WitnessV0KeyHash>(&operatorDest)) ;
CKeyID operatorAuthKey = operatorDest.index() == PKHashType ? CKeyID(std::get<PKHash>(operatorDest)) : CKeyID(std::get<WitnessV0KeyHash>(operatorDest)) ;
genesisTeam.insert(operatorAuthKey);
CDataStream metadata(DfTxMarker, SER_NETWORK, PROTOCOL_VERSION);
metadata << static_cast<unsigned char>(CustomTxType::CreateMasternode)
<< static_cast<char>(operatorDest.which()) << operatorAuthKey;
<< static_cast<char>(operatorDest.index()) << operatorAuthKey;

CScript scriptMeta;
scriptMeta << OP_RETURN << ToByteVector(metadata);
Expand Down
4 changes: 2 additions & 2 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2087,8 +2087,8 @@ bool AppInitMain(InitInterfaces& interfaces)
auto& coinbaseScript = stakerParams.coinbaseScript;

CTxDestination destination = DecodeDestination(op);
operatorId = destination.which() == PKHashType ? CKeyID(*boost::get<PKHash>(&destination)) :
destination.which() == WitV0KeyHashType ? CKeyID(*boost::get<WitnessV0KeyHash>(&destination)) : CKeyID();
operatorId = destination.index() == PKHashType ? CKeyID(std::get<PKHash>(destination)) :
destination.index() == WitV0KeyHashType ? CKeyID(std::get<WitnessV0KeyHash>(destination)) : CKeyID();

if (operatorId.IsNull()) {
LogPrintf("Error: wrong masternode_operator address (%s)\n", op);
Expand Down
20 changes: 7 additions & 13 deletions src/key_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@
#include <base58.h>
#include <bech32.h>
#include <chainparams.h>
#include <script/script.h>
#include <util/strencodings.h>

#include <boost/variant/apply_visitor.hpp>
#include <boost/variant/static_visitor.hpp>

#include <assert.h>
#include <string.h>
#include <algorithm>

namespace
{
class DestinationEncoder : public boost::static_visitor<std::string>
namespace {
class DestinationEncoder
{
private:
const CChainParams& m_params;
Expand Down Expand Up @@ -209,7 +203,7 @@ std::string EncodeExtKey(const CExtKey& key)

std::string EncodeDestination(const CTxDestination& dest)
{
return boost::apply_visitor(DestinationEncoder(Params()), dest);
return std::visit(DestinationEncoder(Params()), dest);
}

CTxDestination DecodeDestination(const std::string& str)
Expand All @@ -228,9 +222,9 @@ bool IsValidDestinationString(const std::string& str)
}

CKeyID getCKeyIDFromDestination(const CTxDestination& dest) {
switch (dest.which()) {
case PKHashType : return CKeyID(*boost::get<PKHash>(&dest));
case WitV0KeyHashType : return CKeyID(*boost::get<WitnessV0KeyHash>(&dest));
default : return CKeyID();
switch (dest.index()) {
case PKHashType : return CKeyID(std::get<PKHash>(dest));
case WitV0KeyHashType : return CKeyID(std::get<WitnessV0KeyHash>(dest));
default : return {};
}
}
6 changes: 3 additions & 3 deletions src/masternodes/anchors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ CKeyID CAnchorAuthMessage::GetSigner() const
CAnchor CAnchor::Create(const std::vector<CAnchorAuthMessage> & auths, CTxDestination const & rewardDest)
{
// assumed here that all of the auths are uniform, were checked for sigs and consensus has been reached!
assert(rewardDest.which() == PKHashType || rewardDest.which() == WitV0KeyHashType);
assert(rewardDest.index() == PKHashType || rewardDest.index() == WitV0KeyHashType);

if (auths.size() > 0) {
CAnchor anchor(static_cast<CAnchorData const &> (auths.at(0)));

for (size_t i = 0; i < auths.size(); ++i) {
anchor.sigs.push_back(auths[i].GetSignature());
}
anchor.rewardKeyID = rewardDest.which() == PKHashType ? CKeyID(*boost::get<PKHash>(&rewardDest)) : CKeyID(*boost::get<WitnessV0KeyHash>(&rewardDest));
anchor.rewardKeyType = rewardDest.which();
anchor.rewardKeyID = rewardDest.index() == PKHashType ? CKeyID(std::get<PKHash>(rewardDest)) : CKeyID(std::get<WitnessV0KeyHash>(rewardDest));
anchor.rewardKeyType = rewardDest.index();
return anchor;
}
return {};
Expand Down
66 changes: 40 additions & 26 deletions src/masternodes/govvariables/attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ static ResVal<CAttributeValue> VerifyPct(const std::string& str) {
if (!resVal) {
return resVal;
}
if (boost::get<CAmount>(*resVal.val) > COIN) {
if (std::get<CAmount>(*resVal.val) > COIN) {
return Res::Err("Percentage exceeds 100%%");
}
return resVal;
Expand Down Expand Up @@ -576,15 +576,15 @@ Res ATTRIBUTES::Import(const UniValue & val) {
auto res = ProcessVariable(
pair.first, pair.second.get_str(),
[this](const CAttributeType& attribute, const CAttributeValue& value) {
if (auto attrV0 = boost::get<const CDataStructureV0>(&attribute)) {
if (const auto attrV0 = std::get_if<CDataStructureV0>(&attribute)) {
if (attrV0->type == AttributeTypes::Live ||
(attrV0->type == AttributeTypes::Token &&
(attrV0->key == TokenKeys::Ascendant ||
attrV0->key == TokenKeys::Descendant ||
attrV0->key == TokenKeys::Epitaph))) {
return Res::Err("Attribute cannot be set externally");
} else if (attrV0->type == AttributeTypes::Oracles && attrV0->typeId == OracleIDs::Splits) {
auto splitValue = boost::get<OracleSplits>(&value);
const auto splitValue = std::get_if<OracleSplits>(&value);
if (!splitValue) {
return Res::Err("Failed to get Oracle split value");
}
Expand Down Expand Up @@ -638,7 +638,7 @@ std::set<uint32_t> attrsVersion27TokenHiddenSet = {
UniValue ATTRIBUTES::ExportFiltered(GovVarsFilter filter, const std::string &prefix) const {
UniValue ret(UniValue::VOBJ);
for (const auto& attribute : attributes) {
auto attrV0 = boost::get<const CDataStructureV0>(&attribute.first);
const auto attrV0 = std::get_if<CDataStructureV0>(&attribute.first);
if (!attrV0) {
continue;
}
Expand Down Expand Up @@ -677,9 +677,9 @@ UniValue ATTRIBUTES::ExportFiltered(GovVarsFilter filter, const std::string &pre
}
}

if (auto bool_val = boost::get<const bool>(&attribute.second)) {
if (const auto bool_val = std::get_if<bool>(&attribute.second)) {
ret.pushKV(key, *bool_val ? "true" : "false");
} else if (auto amount = boost::get<const CAmount>(&attribute.second)) {
} else if (const auto amount = std::get_if<CAmount>(&attribute.second)) {
if (attrV0->typeId == DFIP2203 && attrV0->key == DFIPKeys::BlockPeriod) {
ret.pushKV(key, KeyBuilder(*amount));
} else {
Expand All @@ -690,14 +690,14 @@ UniValue ATTRIBUTES::ExportFiltered(GovVarsFilter filter, const std::string &pre
}
ret.pushKV(key, decimalStr);
}
} else if (auto balances = boost::get<const CBalances>(&attribute.second)) {
} else if (const auto balances = std::get_if<CBalances>(&attribute.second)) {
ret.pushKV(key, AmountsToJSON(balances->balances));
} else if (auto paybacks = boost::get<const CTokenPayback>(&attribute.second)) {
} else if (const auto paybacks = std::get_if<CTokenPayback>(&attribute.second)) {
UniValue result(UniValue::VOBJ);
result.pushKV("paybackfees", AmountsToJSON(paybacks->tokensFee.balances));
result.pushKV("paybacktokens", AmountsToJSON(paybacks->tokensPayback.balances));
ret.pushKV(key, result);
} else if (const auto splitValues = boost::get<OracleSplits>(&attribute.second)) {
} else if (const auto splitValues = std::get_if<OracleSplits>(&attribute.second)) {
std::string keyValue;
for (auto it{splitValues->begin()}; it != splitValues->end(); ++it) {
if (it != splitValues->begin()) {
Expand All @@ -706,11 +706,11 @@ UniValue ATTRIBUTES::ExportFiltered(GovVarsFilter filter, const std::string &pre
keyValue += KeyBuilder(it->first, it->second);
}
ret.pushKV(key, keyValue);
} else if (const auto& descendantPair = boost::get<DescendantValue>(&attribute.second)) {
} else if (const auto& descendantPair = std::get_if<DescendantValue>(&attribute.second)) {
ret.pushKV(key, KeyBuilder(descendantPair->first, descendantPair->second));
} else if (const auto& ascendantPair = boost::get<AscendantValue>(&attribute.second)) {
} else if (const auto& ascendantPair = std::get_if<AscendantValue>(&attribute.second)) {
ret.pushKV(key, KeyBuilder(ascendantPair->first, ascendantPair->second));
} else if (auto currencyPair = boost::get<CTokenCurrencyPair>(&attribute.second)) {
} else if (const auto currencyPair = std::get_if<CTokenCurrencyPair>(&attribute.second)) {
ret.pushKV(key, currencyPair->first + '/' + currencyPair->second);
}
} catch (const std::out_of_range&) {
Expand All @@ -730,7 +730,7 @@ Res ATTRIBUTES::Validate(const CCustomCSView & view) const
return Res::Err("Cannot be set before FortCanningHill");

for (const auto& attribute : attributes) {
auto attrV0 = boost::get<const CDataStructureV0>(&attribute.first);
const auto attrV0 = std::get_if<CDataStructureV0>(&attribute.first);
if (!attrV0) {
return Res::Err("Unsupported version");
}
Expand Down Expand Up @@ -811,7 +811,7 @@ Res ATTRIBUTES::Validate(const CCustomCSView & view) const
return Res::Err("Cannot be set before FortCanningCrunch");
}
if (attrV0->typeId == OracleIDs::Splits) {
const auto splitMap = boost::get<OracleSplits>(&attribute.second);
const auto splitMap = std::get_if<OracleSplits>(&attribute.second);
if (!splitMap) {
return Res::Err("Unsupported value");
}
Expand Down Expand Up @@ -839,7 +839,7 @@ Res ATTRIBUTES::Validate(const CCustomCSView & view) const
break;

case AttributeTypes::Poolpairs:
if (!boost::get<const CAmount>(&attribute.second)) {
if (!std::get_if<CAmount>(&attribute.second)) {
return Res::Err("Unsupported value");
}
switch (attrV0->key) {
Expand Down Expand Up @@ -891,7 +891,7 @@ Res ATTRIBUTES::Validate(const CCustomCSView & view) const
Res ATTRIBUTES::Apply(CCustomCSView & mnview, const uint32_t height)
{
for (const auto& attribute : attributes) {
auto attrV0 = boost::get<const CDataStructureV0>(&attribute.first);
const auto attrV0 = std::get_if<CDataStructureV0>(&attribute.first);
if (!attrV0) {
continue;
}
Expand All @@ -904,8 +904,11 @@ Res ATTRIBUTES::Apply(CCustomCSView & mnview, const uint32_t height)
auto tokenId = attrV0->key == PoolKeys::TokenAFeePCT ?
pool->idTokenA : pool->idTokenB;

auto valuePct = boost::get<const CAmount>(attribute.second);
if (auto res = mnview.SetDexFeePct(poolId, tokenId, valuePct); !res) {
const auto valuePct = std::get_if<CAmount>(&attribute.second);
if (!valuePct) {
return Res::Err("Unexpected type");
}
if (auto res = mnview.SetDexFeePct(poolId, tokenId, *valuePct); !res) {
return res;
}
} else if (attrV0->type == AttributeTypes::Token) {
Expand All @@ -915,13 +918,16 @@ Res ATTRIBUTES::Apply(CCustomCSView & mnview, const uint32_t height)
if (attrV0->key == TokenKeys::DexOutFeePct) {
std::swap(tokenA, tokenB);
}
auto valuePct = boost::get<CAmount>(attribute.second);
if (auto res = mnview.SetDexFeePct(tokenA, tokenB, valuePct); !res) {
const auto valuePct = std::get_if<CAmount>(&attribute.second);
if (!valuePct) {
return Res::Err("Unexpected type");
}
if (auto res = mnview.SetDexFeePct(tokenA, tokenB, *valuePct); !res) {
return res;
}
}
if (attrV0->key == TokenKeys::FixedIntervalPriceId) {
if (const auto &currencyPair = boost::get<CTokenCurrencyPair>(&attribute.second)) {
if (const auto &currencyPair = std::get_if<CTokenCurrencyPair>(&attribute.second)) {
// Already exists, skip.
if (auto it = mnview.LowerBound<COracleView::FixedIntervalPriceKey>(*currencyPair);
it.Valid() && it.Key() == *currencyPair) {
Expand Down Expand Up @@ -958,8 +964,12 @@ Res ATTRIBUTES::Apply(CCustomCSView & mnview, const uint32_t height)
continue;
}

auto value = boost::get<bool>(attribute.second);
if (value) {
const auto value = std::get_if<bool>(&attribute.second);
if (!value) {
return Res::Err("Unexpected type");
}

if (*value) {
continue;
}

Expand Down Expand Up @@ -989,8 +999,12 @@ Res ATTRIBUTES::Apply(CCustomCSView & mnview, const uint32_t height)
continue;
}

auto value = boost::get<bool>(attribute.second);
if (value) {
const auto value = std::get_if<bool>(&attribute.second);
if (!value) {
return Res::Err("Unexpected type");
}

if (*value) {
continue;
}

Expand All @@ -1013,7 +1027,7 @@ Res ATTRIBUTES::Apply(CCustomCSView & mnview, const uint32_t height)
}
}
} else if (attrV0->type == AttributeTypes::Oracles && attrV0->typeId == OracleIDs::Splits) {
const auto value = boost::get<OracleSplits>(&attribute.second);
const auto value = std::get_if<OracleSplits>(&attribute.second);
if (!value) {
return Res::Err("Unsupported value");
}
Expand Down
29 changes: 21 additions & 8 deletions src/masternodes/govvariables/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ ResVal<CScript> GetFutureSwapContractAddress();
using OracleSplits = std::map<uint32_t, int32_t>;
using DescendantValue = std::pair<uint32_t, int32_t>;
using AscendantValue = std::pair<uint32_t, std::string>;
using CAttributeType = boost::variant<CDataStructureV0, CDataStructureV1>;
using CAttributeValue = boost::variant<bool, CAmount, CBalances, CTokenPayback, CTokenCurrencyPair, OracleSplits, DescendantValue, AscendantValue>;
using CAttributeType = std::variant<CDataStructureV0, CDataStructureV1>;
using CAttributeValue = std::variant<bool, CAmount, CBalances, CTokenPayback, CTokenCurrencyPair, OracleSplits, DescendantValue, AscendantValue>;

enum GovVarsFilter {
All,
Expand Down Expand Up @@ -164,14 +164,27 @@ class ATTRIBUTES : public GovVariable, public AutoRegistrator<GovVariable, ATTRI
static GovVariable * Create() { return new ATTRIBUTES(); }

template<typename T>
[[nodiscard]] T GetValue(const CAttributeType& key, T value) const {
static void GetIf(std::optional<T>& opt, const CAttributeValue& var) {
if (auto value = std::get_if<T>(&var)) {
opt = *value;
}
}

template<typename T>
static void GetIf(T& val, const CAttributeValue& var) {
if (auto value = std::get_if<T>(&var)) {
val = *value;
}
}

template<typename K, typename T>
[[nodiscard]] T GetValue(const K& key, T value) const {
static_assert(std::is_convertible_v<K, CAttributeType>);
auto it = attributes.find(key);
if (it != attributes.end()) {
if (auto val = boost::get<const T>(&it->second)) {
value = std::move(*val);
}
GetIf(value, it->second);
}
return std::move(value);
return value;
}

template<typename K, typename T>
Expand Down Expand Up @@ -200,7 +213,7 @@ class ATTRIBUTES : public GovVariable, public AutoRegistrator<GovVariable, ATTRI
static_assert(std::is_convertible_v<K, CAttributeType>);
static_assert(std::is_invocable_r_v<bool, C, K, CAttributeValue>);
for (auto it = attributes.lower_bound(key); it != attributes.end(); ++it) {
if (auto attrV0 = boost::get<K>(&it->first)) {
if (auto attrV0 = std::get_if<K>(&it->first)) {
if (!std::invoke(callback, *attrV0, it->second)) {
break;
}
Expand Down
10 changes: 5 additions & 5 deletions src/masternodes/masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ std::optional<std::pair<CKeyID, uint256> > CMasternodesView::AmIOperator() const
auto const operators = gArgs.GetArgs("-masternode_operator");
for(auto const & key : operators) {
CTxDestination const dest = DecodeDestination(key);
CKeyID const authAddress = dest.which() == PKHashType ? CKeyID(*boost::get<PKHash>(&dest)) :
dest.which() == WitV0KeyHashType ? CKeyID(*boost::get<WitnessV0KeyHash>(&dest)) : CKeyID();
CKeyID const authAddress = dest.index() == PKHashType ? CKeyID(std::get<PKHash>(dest)) :
dest.index() == WitV0KeyHashType ? CKeyID(std::get<WitnessV0KeyHash>(dest)) : CKeyID();
if (!authAddress.IsNull()) {
if (auto nodeId = GetMasternodeIdByOperator(authAddress)) {
return std::make_pair(authAddress, *nodeId);
Expand All @@ -245,8 +245,8 @@ std::set<std::pair<CKeyID, uint256>> CMasternodesView::GetOperatorsMulti() const
std::set<std::pair<CKeyID, uint256>> operatorPairs;
for(auto const & key : operators) {
CTxDestination const dest = DecodeDestination(key);
CKeyID const authAddress = dest.which() == PKHashType ? CKeyID(*boost::get<PKHash>(&dest)) :
dest.which() == WitV0KeyHashType ? CKeyID(*boost::get<WitnessV0KeyHash>(&dest)) : CKeyID();
CKeyID const authAddress = dest.index() == PKHashType ? CKeyID(std::get<PKHash>(dest)) :
dest.index() == WitV0KeyHashType ? CKeyID(std::get<WitnessV0KeyHash>(dest)) : CKeyID();
if (!authAddress.IsNull()) {
if (auto nodeId = GetMasternodeIdByOperator(authAddress)) {
operatorPairs.insert(std::make_pair(authAddress, *nodeId));
Expand All @@ -260,7 +260,7 @@ std::set<std::pair<CKeyID, uint256>> CMasternodesView::GetOperatorsMulti() const
std::optional<std::pair<CKeyID, uint256> > CMasternodesView::AmIOwner() const
{
CTxDestination dest = DecodeDestination(gArgs.GetArg("-masternode_owner", ""));
CKeyID const authAddress = dest.which() == PKHashType ? CKeyID(*boost::get<PKHash>(&dest)) : (dest.which() == WitV0KeyHashType ? CKeyID(*boost::get<WitnessV0KeyHash>(&dest)) : CKeyID());
CKeyID const authAddress = dest.index() == PKHashType ? CKeyID(std::get<PKHash>(dest)) : (dest.index() == WitV0KeyHashType ? CKeyID(std::get<WitnessV0KeyHash>(dest)) : CKeyID());
if (!authAddress.IsNull()) {
auto nodeId = GetMasternodeIdByOwner(authAddress);
if (nodeId)
Expand Down
Loading