Skip to content

Commit

Permalink
Move assert into GetAttributes()
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar committed Oct 20, 2023
1 parent 0f1aba8 commit 60976b0
Show file tree
Hide file tree
Showing 22 changed files with 88 additions and 177 deletions.
9 changes: 1 addition & 8 deletions src/dfi/consensus/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ Res CGovernanceConsensus::operator()(const CGovernanceMessage &obj) const {
// Add to existing ATTRIBUTES instead of overwriting.
auto govVar = mnview.GetAttributes();

if (!govVar) {
return Res::Err("%s: %s", var->GetName(), "Failed to get existing ATTRIBUTES");
}

govVar->time = time;
govVar->evmTemplateId = evmTemplateId;

Expand Down Expand Up @@ -138,7 +134,7 @@ Res CGovernanceConsensus::operator()(const CGovernanceUnsetMessage &obj) const {
}

const auto attributes = mnview.GetAttributes();
assert(attributes);

CDataStructureV0 key{AttributeTypes::Param, ParamIDs::Feature, DFIPKeys::GovUnset};
if (!attributes->GetValue(key, false)) {
return Res::Err("Unset Gov variables not currently enabled in attributes.");
Expand Down Expand Up @@ -179,9 +175,6 @@ Res CGovernanceConsensus::operator()(const CGovernanceHeightMessage &obj) const
if (height >= static_cast<uint32_t>(consensus.DF16FortCanningCrunchHeight) &&
obj.govVar->GetName() == "ATTRIBUTES") {
auto govVar = mnview.GetAttributes();
if (!govVar) {
return Res::Err("%s: %s", obj.govVar->GetName(), "Failed to get existing ATTRIBUTES");
}

if (height >= static_cast<uint32_t>(consensus.DF22MetachainHeight)) {
auto newVar = std::dynamic_pointer_cast<ATTRIBUTES>(obj.govVar);
Expand Down
1 change: 0 additions & 1 deletion src/dfi/consensus/icxorders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ bool IsICXEnabled(const int height, const CCustomCSView &view, const Consensus::
if (height >= consensus.DF22MetachainHeight) {
const CDataStructureV0 enabledKey{AttributeTypes::Param, ParamIDs::Feature, DFIPKeys::ICXEnabled};
auto attributes = view.GetAttributes();
assert(attributes);
return attributes->GetValue(enabledKey, false);
}
// ICX transactions allowed before NextNetwrokUpgrade and some of these conditions
Expand Down
4 changes: 0 additions & 4 deletions src/dfi/consensus/loans.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ static Res PaybackWithCollateral(CCustomCSView &view,
uint32_t height,
uint64_t time) {
const auto attributes = view.GetAttributes();
if (!attributes) {
return DeFiErrors::MNInvalidAttribute();
}

const auto dUsdToken = view.GetToken("DUSD");
if (!dUsdToken) {
Expand Down Expand Up @@ -857,7 +854,6 @@ Res CLoansConsensus::operator()(const CLoanPaybackLoanV2Message &obj) const {

auto shouldSetVariable = false;
auto attributes = mnview.GetAttributes();
assert(attributes);

for (const auto &[loanTokenId, paybackAmounts] : obj.loans) {
const auto loanToken = mnview.GetLoanTokenByID(loanTokenId);
Expand Down
1 change: 0 additions & 1 deletion src/dfi/consensus/masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ Res CMasternodesConsensus::operator()(const CUpdateMasterNodeMessage &obj) const
}

const auto attributes = mnview.GetAttributes();
assert(attributes);

bool ownerType{}, operatorType{}, rewardType{};
for (const auto &[type, addressPair] : obj.updates) {
Expand Down
2 changes: 0 additions & 2 deletions src/dfi/consensus/proposals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Res CProposalsConsensus::IsOnChainGovernanceEnabled() const {
CDataStructureV0 enabledKey{AttributeTypes::Param, ParamIDs::Feature, DFIPKeys::GovernanceEnabled};

auto attributes = mnview.GetAttributes();
assert(attributes);

if (!attributes->GetValue(enabledKey, false)) {
return Res::Err("Cannot create tx, on-chain governance is not enabled");
Expand Down Expand Up @@ -88,7 +87,6 @@ Res CProposalsConsensus::operator()(const CCreateProposalMessage &obj) const {
}

auto attributes = mnview.GetAttributes();
assert(attributes);
CDataStructureV0 cfpMaxCycles{AttributeTypes::Governance, GovernanceIDs::Proposals, GovernanceKeys::CFPMaxCycles};
auto maxCycles = attributes->GetValue(cfpMaxCycles, static_cast<uint32_t>(MAX_CYCLES));

Expand Down
2 changes: 0 additions & 2 deletions src/dfi/consensus/smartcontracts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

Res CSmartContractsConsensus::HandleDFIP2201Contract(const CSmartContractMessage &obj) const {
const auto attributes = mnview.GetAttributes();
assert(attributes);

CDataStructureV0 activeKey{AttributeTypes::Param, ParamIDs::DFIP2201, DFIPKeys::Active};

Expand Down Expand Up @@ -124,7 +123,6 @@ Res CSmartContractsConsensus::operator()(const CFutureSwapMessage &obj) const {
}

const auto attributes = mnview.GetAttributes();
assert(attributes);

bool dfiToDUSD = !obj.source.nTokenId.v;
const auto paramID = dfiToDUSD ? ParamIDs::DFIP2206F : ParamIDs::DFIP2203;
Expand Down
4 changes: 0 additions & 4 deletions src/dfi/consensus/tokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ Res CTokensConsensus::operator()(const CUpdateTokenMessage &obj) const {
const Coin &auth = coins.AccessCoin(COutPoint(token.creationTx, 1)); // always n=1 output

const auto attributes = mnview.GetAttributes();
assert(attributes);
std::set<CScript> databaseMembers;
if (attributes->GetValue(CDataStructureV0{AttributeTypes::Param, ParamIDs::Feature, DFIPKeys::GovFoundation},
false)) {
Expand Down Expand Up @@ -195,7 +194,6 @@ Res CTokensConsensus::operator()(const CMintTokensMessage &obj) const {

CDataStructureV0 enabledKey{AttributeTypes::Param, ParamIDs::Feature, DFIPKeys::MintTokens};
const auto attributes = mnview.GetAttributes();
assert(attributes);
const auto toAddressEnabled = attributes->GetValue(enabledKey, false);

if (!toAddressEnabled && !obj.to.empty()) {
Expand Down Expand Up @@ -255,7 +253,6 @@ Res CTokensConsensus::operator()(const CMintTokensMessage &obj) const {
}

auto attributes = mnview.GetAttributes();
assert(attributes);

CDataStructureV0 enableKey{AttributeTypes::Param, ParamIDs::Feature, DFIPKeys::ConsortiumEnabled};
CDataStructureV0 membersKey{AttributeTypes::Consortium, tokenId.v, ConsortiumKeys::MemberValues};
Expand Down Expand Up @@ -401,7 +398,6 @@ Res CTokensConsensus::operator()(const CBurnTokensMessage &obj) const {
}

auto attributes = mnview.GetAttributes();
assert(attributes);

CDataStructureV0 membersKey{AttributeTypes::Consortium, tokenId.v, ConsortiumKeys::MemberValues};
const auto members = attributes->GetValue(membersKey, CConsortiumMembers{});
Expand Down
4 changes: 2 additions & 2 deletions src/dfi/consensus/txvisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Res CCustomTxVisitor::HasCollateralAuth(const uint256 &collateralTx) const {
Res CCustomTxVisitor::HasFoundationAuth() const {
auto members = consensus.foundationMembers;
const auto attributes = mnview.GetAttributes();
assert(attributes);

if (attributes->GetValue(CDataStructureV0{AttributeTypes::Param, ParamIDs::Feature, DFIPKeys::GovFoundation},
false)) {
if (const auto databaseMembers = attributes->GetValue(
Expand Down Expand Up @@ -297,7 +297,7 @@ Res CCustomTxVisitor::CollateralPctCheck(const bool hasDUSDLoans,
if (isPostNext) {
const CDataStructureV0 enabledKey{AttributeTypes::Vaults, VaultIDs::DUSDVault, VaultKeys::DUSDVaultEnabled};
auto attributes = mnview.GetAttributes();
assert(attributes);

auto DUSDVaultsAllowed = attributes->GetValue(enabledKey, false);
if (DUSDVaultsAllowed && hasDUSDColl && !hasOtherColl) {
return Res::Ok(); // every loan ok when DUSD loops allowed and 100% DUSD collateral
Expand Down
7 changes: 3 additions & 4 deletions src/dfi/consensus/vaults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,9 @@ Res CVaultsConsensus::operator()(const CDepositToVaultMessage &obj) const {
// If collateral token exist make sure it is enabled.
if (mnview.GetCollateralTokenFromAttributes(obj.amount.nTokenId)) {
CDataStructureV0 collateralKey{AttributeTypes::Token, obj.amount.nTokenId.v, TokenKeys::LoanCollateralEnabled};
if (const auto attributes = mnview.GetAttributes()) {
if (!attributes->GetValue(collateralKey, false)) {
return Res::Err("Collateral token (%d) is disabled", obj.amount.nTokenId.v);
}
const auto attributes = mnview.GetAttributes();
if (!attributes->GetValue(collateralKey, false)) {
return Res::Err("Collateral token (%d) is disabled", obj.amount.nTokenId.v);
}
}

Expand Down
1 change: 0 additions & 1 deletion src/dfi/consensus/xvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ static bool IsTransferDomainEnabled(const int height, const CCustomCSView &view,

const CDataStructureV0 enabledKey{AttributeTypes::Param, ParamIDs::Feature, DFIPKeys::TransferDomain};
auto attributes = view.GetAttributes();
assert(attributes);
return attributes->GetValue(enabledKey, false);
}

Expand Down
2 changes: 0 additions & 2 deletions src/dfi/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ class DeFiErrors {

static Res ICXSubmitEXTInvalid() { return Res::Err("Invalid refundPubkey is not a valid pubkey!"); }

static Res MNInvalidAttribute() { return Res::Err("Attributes unavailable"); }

static Res TokenInvalidForName(const std::string &tokenName) { return Res::Err("Cannot find token %s", tokenName); }

static Res LoanPaybackWithCollateralDisable() {
Expand Down
5 changes: 2 additions & 3 deletions src/dfi/govvariables/attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ static void TrackLiveBalance(CCustomCSView &mnview,
const EconomyKeys dataKey,
const bool add) {
auto attributes = mnview.GetAttributes();
assert(attributes);

CDataStructureV0 key{AttributeTypes::Live, ParamIDs::Economy, dataKey};
auto balances = attributes->GetValue(key, CBalances{});
Res res{};
Expand Down Expand Up @@ -911,7 +911,7 @@ void TrackDUSDSub(CCustomCSView &mnview, const CTokenAmount &amount) {

void TrackLiveBalances(CCustomCSView &mnview, const CBalances &balances, const uint8_t key) {
auto attributes = mnview.GetAttributes();
assert(attributes);

const CDataStructureV0 liveKey{AttributeTypes::Live, ParamIDs::Auction, key};
auto storedBalances = attributes->GetValue(liveKey, CBalances{});
for (const auto &[tokenID, amount] : balances.balances) {
Expand All @@ -932,7 +932,6 @@ bool IsEVMEnabled(const std::shared_ptr<ATTRIBUTES> attributes) {

bool IsEVMEnabled(const CCustomCSView &view, const Consensus::Params &consensus) {
auto attributes = view.GetAttributes();
assert(attributes);

return IsEVMEnabled(attributes);
}
Expand Down
12 changes: 5 additions & 7 deletions src/dfi/gv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ Res CGovView::SetVariable(const GovVariable &var) {
return WriteOrEraseVar(var);
}
auto attributes = GetAttributes();
if (!attributes) {
return WriteOrEraseVar(var);
}
auto &current = dynamic_cast<const ATTRIBUTES &>(var);
if (current.changed.empty()) {
return Res::Ok();
Expand Down Expand Up @@ -120,8 +117,9 @@ void CGovView::EraseStoredVariables(const uint32_t height) {
}

std::shared_ptr<ATTRIBUTES> CGovView::GetAttributes() const {
if (const auto var = GetVariable("ATTRIBUTES")) {
return std::dynamic_pointer_cast<ATTRIBUTES>(var);
}
return {};
const auto var = GetVariable("ATTRIBUTES");
assert(var);
auto attributes = std::dynamic_pointer_cast<ATTRIBUTES>(var);
assert(attributes);
return attributes;
}
79 changes: 32 additions & 47 deletions src/dfi/masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ CAmount GetProposalCreationFee(int, const CCustomCSView &view, const CCreateProp
auto type = static_cast<CProposalType>(msg.type);
auto options = static_cast<CProposalOption>(msg.options);
auto attributes = view.GetAttributes();
assert(attributes);

CDataStructureV0 CFPKey{AttributeTypes::Governance, GovernanceIDs::Proposals, GovernanceKeys::CFPFee};
CDataStructureV0 VOCKey{AttributeTypes::Governance, GovernanceIDs::Proposals, GovernanceKeys::VOCFee};
Expand Down Expand Up @@ -1198,9 +1197,6 @@ uint256 CCustomCSView::MerkleRoot() {

bool CCustomCSView::AreTokensLocked(const std::set<uint32_t> &tokenIds) const {
const auto attributes = GetAttributes();
if (!attributes) {
return false;
}

for (const auto &tokenId : tokenIds) {
CDataStructureV0 lockKey{AttributeTypes::Locks, ParamIDs::TokenID, tokenId};
Expand Down Expand Up @@ -1279,72 +1275,63 @@ std::map<CKeyID, CKey> AmISignerNow(int height, const CAnchorData::CTeam &team)
}

std::optional<CLoanView::CLoanSetLoanTokenImpl> CCustomCSView::GetLoanTokenFromAttributes(const DCT_ID &id) const {
if (const auto attributes = GetAttributes()) {
CDataStructureV0 pairKey{AttributeTypes::Token, id.v, TokenKeys::FixedIntervalPriceId};
CDataStructureV0 interestKey{AttributeTypes::Token, id.v, TokenKeys::LoanMintingInterest};
CDataStructureV0 mintableKey{AttributeTypes::Token, id.v, TokenKeys::LoanMintingEnabled};

if (const auto token = GetToken(id); token && attributes->CheckKey(pairKey) &&
attributes->CheckKey(interestKey) && attributes->CheckKey(mintableKey)) {
CLoanView::CLoanSetLoanTokenImpl loanToken;
loanToken.fixedIntervalPriceId = attributes->GetValue(pairKey, CTokenCurrencyPair{});
loanToken.interest = attributes->GetValue(interestKey, CAmount{});
loanToken.mintable = attributes->GetValue(mintableKey, false);
loanToken.symbol = token->symbol;
loanToken.name = token->name;
return loanToken;
}
const auto attributes = GetAttributes();
CDataStructureV0 pairKey{AttributeTypes::Token, id.v, TokenKeys::FixedIntervalPriceId};
CDataStructureV0 interestKey{AttributeTypes::Token, id.v, TokenKeys::LoanMintingInterest};
CDataStructureV0 mintableKey{AttributeTypes::Token, id.v, TokenKeys::LoanMintingEnabled};

if (const auto token = GetToken(id); token && attributes->CheckKey(pairKey) && attributes->CheckKey(interestKey) &&
attributes->CheckKey(mintableKey)) {
CLoanView::CLoanSetLoanTokenImpl loanToken;
loanToken.fixedIntervalPriceId = attributes->GetValue(pairKey, CTokenCurrencyPair{});
loanToken.interest = attributes->GetValue(interestKey, CAmount{});
loanToken.mintable = attributes->GetValue(mintableKey, false);
loanToken.symbol = token->symbol;
loanToken.name = token->name;
return loanToken;
}

return {};
}

std::optional<CLoanView::CLoanSetCollateralTokenImpl> CCustomCSView::GetCollateralTokenFromAttributes(
const DCT_ID &id) const {
if (const auto attributes = GetAttributes()) {
CLoanSetCollateralTokenImplementation collToken;

CDataStructureV0 pairKey{AttributeTypes::Token, id.v, TokenKeys::FixedIntervalPriceId};
CDataStructureV0 factorKey{AttributeTypes::Token, id.v, TokenKeys::LoanCollateralFactor};
const auto attributes = GetAttributes();
CLoanSetCollateralTokenImplementation collToken;

if (attributes->CheckKey(pairKey) && attributes->CheckKey(factorKey)) {
collToken.fixedIntervalPriceId = attributes->GetValue(pairKey, CTokenCurrencyPair{});
collToken.factor = attributes->GetValue(factorKey, CAmount{0});
collToken.idToken = id;
CDataStructureV0 pairKey{AttributeTypes::Token, id.v, TokenKeys::FixedIntervalPriceId};
CDataStructureV0 factorKey{AttributeTypes::Token, id.v, TokenKeys::LoanCollateralFactor};

auto token = GetToken(id);
if (token) {
collToken.creationTx = token->creationTx;
}
if (attributes->CheckKey(pairKey) && attributes->CheckKey(factorKey)) {
collToken.fixedIntervalPriceId = attributes->GetValue(pairKey, CTokenCurrencyPair{});
collToken.factor = attributes->GetValue(factorKey, CAmount{0});
collToken.idToken = id;

return collToken;
auto token = GetToken(id);
if (token) {
collToken.creationTx = token->creationTx;
}

return collToken;
}

return {};
}

uint32_t CCustomCSView::GetVotingPeriodFromAttributes() const {
auto attributes = GetAttributes();
assert(attributes);

const auto attributes = GetAttributes();
CDataStructureV0 votingKey{AttributeTypes::Governance, GovernanceIDs::Proposals, GovernanceKeys::VotingPeriod};

return attributes->GetValue(votingKey, Params().GetConsensus().props.votingPeriod);
}

uint32_t CCustomCSView::GetEmergencyPeriodFromAttributes(const CProposalType &type) const {
auto attributes = GetAttributes();
assert(attributes);

const auto attributes = GetAttributes();
CDataStructureV0 VOCKey{AttributeTypes::Governance, GovernanceIDs::Proposals, GovernanceKeys::VOCEmergencyPeriod};
return attributes->GetValue(VOCKey, Params().GetConsensus().props.emergencyPeriod);
}

CAmount CCustomCSView::GetApprovalThresholdFromAttributes(const CProposalType &type) const {
auto attributes = GetAttributes();
assert(attributes);

const auto attributes = GetAttributes();
CDataStructureV0 CFPKey{AttributeTypes::Governance, GovernanceIDs::Proposals, GovernanceKeys::CFPApprovalThreshold};
CDataStructureV0 VOCKey{AttributeTypes::Governance, GovernanceIDs::Proposals, GovernanceKeys::VOCApprovalThreshold};

Expand All @@ -1359,8 +1346,7 @@ CAmount CCustomCSView::GetApprovalThresholdFromAttributes(const CProposalType &t
}

CAmount CCustomCSView::GetQuorumFromAttributes(const CProposalType &type, bool emergency) const {
auto attributes = GetAttributes();
assert(attributes);
const auto attributes = GetAttributes();

CDataStructureV0 quorumKey{AttributeTypes::Governance, GovernanceIDs::Proposals, GovernanceKeys::Quorum};
CDataStructureV0 vocEmergencyQuorumKey{
Expand All @@ -1374,8 +1360,7 @@ CAmount CCustomCSView::GetQuorumFromAttributes(const CProposalType &type, bool e
}

CAmount CCustomCSView::GetFeeBurnPctFromAttributes() const {
auto attributes = GetAttributes();
assert(attributes);
const auto attributes = GetAttributes();

CDataStructureV0 feeBurnPctKey{AttributeTypes::Governance, GovernanceIDs::Proposals, GovernanceKeys::FeeBurnPct};

Expand Down
Loading

0 comments on commit 60976b0

Please sign in to comment.