Skip to content

Commit

Permalink
Merge branch 'master' into fix/getgovproposal_status
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouzo authored Dec 23, 2022
2 parents 08c5b59 + 009c4c5 commit f66f1aa
Show file tree
Hide file tree
Showing 37 changed files with 2,560 additions and 1,382 deletions.
37 changes: 21 additions & 16 deletions src/amount.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,18 @@ typedef std::map<DCT_ID, CAmount> TAmounts;

inline ResVal<CAmount> SafeAdd(CAmount _a, CAmount _b) {
// check limits
Require(_a >= 0 && _b >= 0, "negative amount");

if (_a < 0 || _b < 0) {
return Res::Err("negative amount");
}
// convert to unsigned, because signed overflow is UB
const uint64_t a = (uint64_t) _a;
const uint64_t b = (uint64_t) _b;

const uint64_t sum = a + b;
// check overflow

Require((sum - a) == b && (uint64_t)std::numeric_limits<CAmount>::max() >= sum, "overflow");
if ((sum - a) != b || ((uint64_t)std::numeric_limits<CAmount>::max()) < sum) {
return Res::Err("overflow");
}
return {(CAmount) sum, Res::Ok()};
}

Expand All @@ -125,26 +127,29 @@ struct CTokenAmount { // simple std::pair is less informative

Res Add(CAmount amount) {
// safety checks
Require(amount >= 0, "negative amount: %s", GetDecimaleString(amount));

if (amount < 0) {
return Res::Err("negative amount: %s", GetDecimaleString(amount));
}
// add
auto sumRes = SafeAdd(nValue, amount);
Require(sumRes);

nValue = *sumRes;
auto sumRes = SafeAdd(this->nValue, amount);
if (!sumRes.ok) {
return std::move(sumRes);
}
this->nValue = *sumRes.val;
return Res::Ok();
}

Res Sub(CAmount amount) {
// safety checks
Require(amount >= 0, "negative amount: %s", GetDecimaleString(amount));
Require(nValue >= amount, "amount %s is less than %s", GetDecimaleString(nValue), GetDecimaleString(amount));

if (amount < 0) {
return Res::Err("negative amount: %s", GetDecimaleString(amount));
}
if (this->nValue < amount) {
return Res::Err("amount %s is less than %s", GetDecimaleString(this->nValue), GetDecimaleString(amount));
}
// sub
nValue -= amount;
this->nValue -= amount;
return Res::Ok();
}

CAmount SubWithRemainder(CAmount amount) {
// safety checks
if (amount < 0) {
Expand Down
16 changes: 12 additions & 4 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,9 @@ void ClearCheckpoints(CChainParams &params) {

Res UpdateCheckpointsFromFile(CChainParams &params, const std::string &fileName) {
std::ifstream file(fileName);
Require(file.good(), "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 @@ -1172,13 +1174,19 @@ Res UpdateCheckpointsFromFile(CChainParams &params, const std::string &fileName)

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

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

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

params.checkpointData.mapCheckpoints[height] = hash;
}
Expand Down
48 changes: 36 additions & 12 deletions src/masternodes/accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ Res CAccountsView::AddBalance(const CScript &owner, CTokenAmount amount) {
return Res::Ok();
}
auto balance = GetBalance(owner, amount.nTokenId);
Require(balance.Add(amount.nValue));
auto res = balance.Add(amount.nValue);
if (!res.ok) {
return res;
}
return SetBalance(owner, balance);
}

Expand All @@ -45,21 +48,30 @@ Res CAccountsView::SubBalance(const CScript &owner, CTokenAmount amount) {
return Res::Ok();
}
auto balance = GetBalance(owner, amount.nTokenId);
Require(balance.Sub(amount.nValue));
auto res = balance.Sub(amount.nValue);
if (!res.ok) {
return res;
}
return SetBalance(owner, balance);
}

Res CAccountsView::AddBalances(const CScript &owner, const CBalances &balances) {
for (const auto &kv : balances.balances)
Require(AddBalance(owner, CTokenAmount{kv.first, kv.second}));

for (const auto &kv : balances.balances) {
auto res = AddBalance(owner, CTokenAmount{kv.first, kv.second});
if (!res.ok) {
return res;
}
}
return Res::Ok();
}

Res CAccountsView::SubBalances(const CScript &owner, const CBalances &balances) {
for (const auto &kv : balances.balances)
Require(SubBalance(owner, CTokenAmount{kv.first, kv.second}));

for (const auto &kv : balances.balances) {
auto res = SubBalance(owner, CTokenAmount{kv.first, kv.second});
if (!res.ok) {
return res;
}
}
return Res::Ok();
}

Expand All @@ -80,7 +92,10 @@ uint32_t CAccountsView::GetBalancesHeight(const CScript &owner) {
}

Res CAccountsView::StoreFuturesUserValues(const CFuturesUserKey &key, const CFuturesUserValue &futures) {
Require(WriteBy<ByFuturesSwapKey>(key, futures), "Failed to store futures");
if (!WriteBy<ByFuturesSwapKey>(key, futures)) {
return Res::Err("Failed to store futures");
}

return Res::Ok();
}

Expand All @@ -91,12 +106,18 @@ void CAccountsView::ForEachFuturesUserValues(
}

Res CAccountsView::EraseFuturesUserValues(const CFuturesUserKey &key) {
Require(EraseBy<ByFuturesSwapKey>(key), "Failed to erase futures");
if (!EraseBy<ByFuturesSwapKey>(key)) {
return Res::Err("Failed to erase futures");
}

return Res::Ok();
}

Res CAccountsView::StoreFuturesDUSD(const CFuturesUserKey &key, const CAmount &amount) {
Require(WriteBy<ByFuturesDUSDKey>(key, amount), "Failed to store futures");
if (!WriteBy<ByFuturesDUSDKey>(key, amount)) {
return Res::Err("Failed to store futures");
}

return Res::Ok();
}

Expand All @@ -106,6 +127,9 @@ void CAccountsView::ForEachFuturesDUSD(std::function<bool(const CFuturesUserKey
}

Res CAccountsView::EraseFuturesDUSD(const CFuturesUserKey &key) {
Require(EraseBy<ByFuturesDUSDKey>(key), "Failed to erase futures");
if (!EraseBy<ByFuturesDUSDKey>(key)) {
return Res::Err("Failed to erase futures");
}

return Res::Ok();
}
34 changes: 20 additions & 14 deletions src/masternodes/balances.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,33 @@ struct CBalances {
return Res::Ok();
}
auto current = CTokenAmount{amount.nTokenId, balances[amount.nTokenId]};
Require(current.Add(amount.nValue));
auto res = current.Add(amount.nValue);
if (!res.ok) {
return res;
}
if (current.nValue == 0) {
balances.erase(amount.nTokenId);
} else {
balances[amount.nTokenId] = current.nValue;
}
return Res::Ok();
}

Res Sub(CTokenAmount amount) {
if (amount.nValue == 0) {
return Res::Ok();
}
auto current = CTokenAmount{amount.nTokenId, balances[amount.nTokenId]};
Require(current.Sub(amount.nValue));

auto res = current.Sub(amount.nValue);
if (!res.ok) {
return res;
}
if (current.nValue == 0) {
balances.erase(amount.nTokenId);
} else {
balances[amount.nTokenId] = current.nValue;
}
return Res::Ok();
}

CTokenAmount SubWithRemainder(CTokenAmount amount) {
if (amount.nValue == 0) {
return CTokenAmount{amount.nTokenId, 0};
Expand All @@ -55,14 +58,15 @@ struct CBalances {
}
return CTokenAmount{amount.nTokenId, remainder};
}

Res SubBalances(const TAmounts &other) {
for (const auto &[tokenId, amount] : other)
Require(Sub(CTokenAmount{tokenId, amount}));

for (const auto &kv : other) {
auto res = Sub(CTokenAmount{kv.first, kv.second});
if (!res.ok) {
return res;
}
}
return Res::Ok();
}

CBalances SubBalancesWithRemainder(const TAmounts &other) {
CBalances remainderBalances;
for (const auto &kv : other) {
Expand All @@ -73,11 +77,13 @@ struct CBalances {
}
return remainderBalances;
}

Res AddBalances(const TAmounts &other) {
for (const auto &[tokenId, amount] : other)
Require(Add(CTokenAmount{tokenId, amount}));

for (const auto &kv : other) {
auto res = Add(CTokenAmount{kv.first, kv.second});
if (!res.ok) {
return res;
}
}
return Res::Ok();
}

Expand Down
Loading

0 comments on commit f66f1aa

Please sign in to comment.