Skip to content

Commit

Permalink
Remove Require usage (#2582) (#2595)
Browse files Browse the repository at this point in the history
* Remove Require usage from outside dfi/consensus

* Remove Require from gov, txvisitor, mn, oracles, proposals and tokens

* Accounts and poolpairs

* Smart contracts

* Vaults

* Loans

* ICX

* Remove Require
  • Loading branch information
Bushstar authored Oct 17, 2023
1 parent ed73fc1 commit 56969d3
Show file tree
Hide file tree
Showing 35 changed files with 1,426 additions and 655 deletions.
16 changes: 12 additions & 4 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,9 @@ void ClearCheckpoints(CChainParams &params) {

Res UpdateCheckpointsFromFile(CChainParams &params, const std::string &fileName) {
std::ifstream file(fileName);
Require(file.good(), [=]{ return strprintf("Could not read %s. Ensure it exists and has read permissions", fileName); });
if (!file.good()) {
return Res::Err("Could not read %s. Ensure it exists and has read permissions", fileName);
}

ClearCheckpoints(params);

Expand All @@ -1480,13 +1482,19 @@ Res UpdateCheckpointsFromFile(CChainParams &params, const std::string &fileName)

std::istringstream iss(trimmed);
std::string hashStr, heightStr;
Require((iss >> heightStr >> hashStr), [=]{ return strprintf("Error parsing line %s", trimmed); });
if (!(iss >> heightStr >> hashStr)) {
return Res::Err("Error parsing line %s", trimmed);
}

uint256 hash;
Require(ParseHashStr(hashStr, hash), [=]{ return strprintf("Invalid hash: %s", hashStr); });
if (!ParseHashStr(hashStr, hash)) {
return Res::Err("Invalid hash: %s", hashStr);
}

int32_t height;
Require(ParseInt32(heightStr, &height), [=]{ return strprintf("Invalid height: %s", heightStr); });
if (!ParseInt32(heightStr, &height)) {
return Res::Err("Invalid height: %s", heightStr);
}

params.checkpointData.mapCheckpoints[height] = hash;
}
Expand Down
57 changes: 39 additions & 18 deletions src/dfi/consensus/accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ static ResVal<CBalances> BurntTokens(const CTransaction &tx) {
CBalances balances;
for (const auto &out : tx.vout) {
if (out.scriptPubKey.size() > 0 && out.scriptPubKey[0] == OP_RETURN) {
Require(balances.Add(out.TokenAmount()));
if (auto res = balances.Add(out.TokenAmount()); !res) {
return res;
}
}
}
return {balances, Res::Ok()};
Expand All @@ -20,35 +22,44 @@ static ResVal<CBalances> BurntTokens(const CTransaction &tx) {
Res CAccountsConsensus::operator()(const CUtxosToAccountMessage &obj) const {
// check enough tokens are "burnt"
auto burnt = BurntTokens(tx);
Require(burnt);
if (!burnt) {
return burnt;
}

const auto mustBeBurnt = SumAllTransfers(obj.to);
Require(*burnt.val == mustBeBurnt,
"transfer tokens mismatch burnt tokens: (%s) != (%s)",
mustBeBurnt.ToString(),
burnt.val->ToString());
if (*burnt.val != mustBeBurnt) {
return Res::Err(
"transfer tokens mismatch burnt tokens: (%s) != (%s)", mustBeBurnt.ToString(), burnt.val->ToString());
}

// transfer
return AddBalancesSetShares(obj.to);
}

Res CAccountsConsensus::operator()(const CAccountToUtxosMessage &obj) const {
// check auth
Require(HasAuth(obj.from));
if (auto res = HasAuth(obj.from); !res) {
return res;
}

// check that all tokens are minted, and no excess tokens are minted
auto minted = MintedTokens(obj.mintingOutputsStart);
Require(minted);
if (!minted) {
return minted;
}

Require(obj.balances == *minted.val,
"amount of minted tokens in UTXOs and metadata do not match: (%s) != (%s)",
minted.val->ToString(),
obj.balances.ToString());
if (obj.balances != *minted.val) {
return Res::Err("amount of minted tokens in UTXOs and metadata do not match: (%s) != (%s)",
minted.val->ToString(),
obj.balances.ToString());
}

// block for non-DFI transactions
for (const auto &kv : obj.balances.balances) {
const DCT_ID &tokenId = kv.first;
Require(tokenId == DCT_ID{0}, "only available for DFI transactions");
if (tokenId != DCT_ID{0}) {
return Res::Err("only available for DFI transactions");
}
}

// transfer
Expand All @@ -57,28 +68,38 @@ Res CAccountsConsensus::operator()(const CAccountToUtxosMessage &obj) const {

Res CAccountsConsensus::operator()(const CAccountToAccountMessage &obj) const {
// check auth
Require(HasAuth(obj.from));
if (auto res = HasAuth(obj.from); !res) {
return res;
}

// transfer
Require(SubBalanceDelShares(obj.from, SumAllTransfers(obj.to)));
if (auto res = SubBalanceDelShares(obj.from, SumAllTransfers(obj.to)); !res) {
return res;
}
return AddBalancesSetShares(obj.to);
}

Res CAccountsConsensus::operator()(const CAnyAccountsToAccountsMessage &obj) const {
// check auth
for (const auto &kv : obj.from) {
Require(HasAuth(kv.first));
if (auto res = HasAuth(kv.first); !res) {
return res;
}
}

// compare
const auto sumFrom = SumAllTransfers(obj.from);
const auto sumTo = SumAllTransfers(obj.to);

Require(sumFrom == sumTo, "sum of inputs (from) != sum of outputs (to)");
if (sumFrom != sumTo) {
return Res::Err("sum of inputs (from) != sum of outputs (to)");
}

// transfer
// subtraction
Require(SubBalancesDelShares(obj.from));
if (auto res = SubBalancesDelShares(obj.from); !res) {
return res;
}
// addition
return AddBalancesSetShares(obj.to);
}
4 changes: 3 additions & 1 deletion src/dfi/consensus/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

Res CGovernanceConsensus::operator()(const CGovernanceMessage &obj) const {
// check foundation auth
Require(HasFoundationAuth());
if (auto res = HasFoundationAuth(); !res) {
return res;
}
for (const auto &gov : obj.govs) {
if (!gov.second) {
return Res::Err("'%s': variable does not registered", gov.first);
Expand Down
Loading

0 comments on commit 56969d3

Please sign in to comment.