Skip to content

Commit

Permalink
Revert "Remove Require usage (#2582)"
Browse files Browse the repository at this point in the history
This reverts commit 1b00f53.
  • Loading branch information
Bushstar committed Oct 17, 2023
1 parent 1b00f53 commit 03ba17d
Show file tree
Hide file tree
Showing 35 changed files with 655 additions and 1,426 deletions.
16 changes: 4 additions & 12 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1467,9 +1467,7 @@ void ClearCheckpoints(CChainParams &params) {

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

ClearCheckpoints(params);

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

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

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

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

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

const auto mustBeBurnt = SumAllTransfers(obj.to);
if (*burnt.val != mustBeBurnt) {
return Res::Err(
"transfer tokens mismatch burnt tokens: (%s) != (%s)", mustBeBurnt.ToString(), burnt.val->ToString());
}
Require(*burnt.val == mustBeBurnt,
"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
if (auto res = HasAuth(obj.from); !res) {
return res;
}
Require(HasAuth(obj.from));

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

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());
}
Require(obj.balances == *minted.val,
"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;
if (tokenId != DCT_ID{0}) {
return Res::Err("only available for DFI transactions");
}
Require(tokenId == DCT_ID{0}, "only available for DFI transactions");
}

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

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

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

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

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

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

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

Res CGovernanceConsensus::operator()(const CGovernanceMessage &obj) const {
// check foundation auth
if (auto res = HasFoundationAuth(); !res) {
return res;
}
Require(HasFoundationAuth());
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 03ba17d

Please sign in to comment.