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

checked assign ops #3374

Merged
merged 2 commits into from
Jul 5, 2024
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
2 changes: 2 additions & 0 deletions .changelog/unreleased/improvements/3374-checked-assign-ops.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Refactored checked assign arithmetic operations to use smooth-operator macro.
([\#3374](https://github.com/anoma/namada/pull/3374))
14 changes: 6 additions & 8 deletions crates/governance/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,11 @@ pub fn compute_proposal_result(
let vote_type = votes.validators_vote.get(&address);
if let Some(vote) = vote_type {
if vote.is_yay() {
yay_voting_power = checked!(yay_voting_power + vote_power)?;
checked!(yay_voting_power += vote_power)?;
} else if vote.is_nay() {
nay_voting_power = checked!(nay_voting_power + vote_power)?;
checked!(nay_voting_power += vote_power)?;
} else if vote.is_abstain() {
abstain_voting_power =
checked!(abstain_voting_power + vote_power)?;
checked!(abstain_voting_power += vote_power)?;
}
}
}
Expand Down Expand Up @@ -393,12 +392,11 @@ pub fn compute_proposal_result(
}
}
} else if delegator_vote.is_yay() {
yay_voting_power = checked!(yay_voting_power + vote_power)?;
checked!(yay_voting_power += vote_power)?;
} else if delegator_vote.is_nay() {
nay_voting_power = checked!(nay_voting_power + vote_power)?;
checked!(nay_voting_power += vote_power)?;
} else if delegator_vote.is_abstain() {
abstain_voting_power =
checked!(abstain_voting_power + vote_power)?;
checked!(abstain_voting_power += vote_power)?;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/node/src/storage/rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,7 +1098,7 @@ impl DB for RocksDB {
// Read from latest height
return self.read_subspace_val(key);
} else {
raw_height = checked!(raw_height + 1)?
checked!(raw_height += 1)?
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/proof_of_stake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ where
let rp =
rewards_products.get(storage, &ep)?.unwrap_or_default();
let slashed_rewards = slashed_amount.mul_floor(rp)?;
rewards = checked!(rewards + slashed_rewards)?;
checked!(rewards += slashed_rewards)?;
}
}

Expand Down Expand Up @@ -905,7 +905,7 @@ where
bonds_for_removal.new_entry =
Some((bond_epoch, checked!(bond_amount - to_unbond)?));
}
remaining = checked!(remaining - to_unbond)?;
checked!(remaining -= to_unbond)?;
if remaining.is_zero() {
break;
}
Expand Down Expand Up @@ -2810,7 +2810,7 @@ where
// Add reward tokens tallied during previous withdrawals
let counter_rewards =
take_rewards_from_counter(storage, &source, validator)?;
reward_tokens = checked!(reward_tokens + counter_rewards)?;
checked!(reward_tokens += counter_rewards)?;

// Update the last claim epoch in storage
write_last_reward_claim_epoch(storage, &source, validator, current_epoch)?;
Expand Down
14 changes: 6 additions & 8 deletions crates/proof_of_stake/src/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,21 +325,19 @@ where

// Proposer reward
if address == *proposer_address {
rewards_frac = checked!(rewards_frac + coeffs.proposer_coeff)?;
checked!(rewards_frac += coeffs.proposer_coeff)?;
}

// Signer reward
if signer_set.contains(&address) {
let signing_frac =
checked!(stake_unscaled / signing_stake_unscaled)?;
rewards_frac =
checked!(rewards_frac + (coeffs.signer_coeff * signing_frac))?;
checked!(rewards_frac += (coeffs.signer_coeff * signing_frac))?;
}
// Consensus validator reward
rewards_frac = checked!(
rewards_frac
+ (coeffs.active_val_coeff
* (stake_unscaled / consensus_stake_unscaled))
checked!(
rewards_frac += (coeffs.active_val_coeff
* (stake_unscaled / consensus_stake_unscaled))
)?;

// To be added to the rewards accumulator
Expand Down Expand Up @@ -598,7 +596,7 @@ where
debug_assert!(ep <= claim_end);
let rp = rewards_products.get(storage, &ep)?.unwrap_or_default();
let reward = bond_amount.mul_floor(rp)?;
reward_tokens = checked!(reward_tokens + reward)?;
checked!(reward_tokens += reward)?;
}

Ok(reward_tokens)
Expand Down
4 changes: 2 additions & 2 deletions crates/proof_of_stake/src/slashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ where
// Then update validator and total deltas
for (epoch, slash_amount) in slash_amounts {
let slash_delta = checked!(slash_amount - slash_acc)?;
slash_acc = checked!(slash_acc + slash_delta)?;
checked!(slash_acc += slash_delta)?;

let neg_slash_delta = checked!(-slash_delta.change())?;
update_validator_deltas(
Expand Down Expand Up @@ -681,7 +681,7 @@ where
redelegated_bonds = new_redelegated_bonds;

// `newSum`
sum = checked!(sum + amount)?;
checked!(sum += amount)?;

// `newSlashesMap`
let cur = slashed_amounts.entry(epoch).or_default();
Expand Down
29 changes: 12 additions & 17 deletions crates/sdk/src/queries/vp/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ where
amount,
) = result?;
if epoch >= withdrawable {
total = checked!(total + amount)?;
checked!(total += amount)?;
}
}
Ok(total)
Expand Down Expand Up @@ -733,32 +733,27 @@ fn enrich_bonds_and_unbonds(

for bond in &detail.bonds {
let slashed_bond = bond.slashed_amount.unwrap_or_default();
bond_total = checked!(bond_total + bond.amount)?;
bond_total_slashed =
checked!(bond_total_slashed + slashed_bond)?;
checked!(bond_total += bond.amount)?;
checked!(bond_total_slashed += slashed_bond)?;
}
for unbond in &detail.unbonds {
let slashed_unbond =
unbond.slashed_amount.unwrap_or_default();
unbond_total = checked!(unbond_total + unbond.amount)?;
unbond_total_slashed =
checked!(unbond_total_slashed + slashed_unbond)?;
checked!(unbond_total += unbond.amount)?;
checked!(unbond_total_slashed += slashed_unbond)?;

if current_epoch >= unbond.withdraw {
withdrawable = checked!(
withdrawable + unbond.amount - slashed_unbond
checked!(
withdrawable += unbond.amount - slashed_unbond
)?;
}
}

bonds_total = checked!(bonds_total + bond_total)?;
bonds_total_slashed =
checked!(bonds_total_slashed + bond_total_slashed)?;
unbonds_total = checked!(unbonds_total + unbond_total)?;
unbonds_total_slashed =
checked!(unbonds_total_slashed + unbond_total_slashed)?;
total_withdrawable =
checked!(total_withdrawable + withdrawable)?;
checked!(bonds_total += bond_total)?;
checked!(bonds_total_slashed += bond_total_slashed)?;
checked!(unbonds_total += unbond_total)?;
checked!(unbonds_total_slashed += unbond_total_slashed)?;
checked!(total_withdrawable += withdrawable)?;

let enriched_detail = EnrichedBondsAndUnbondsDetail {
data: detail,
Expand Down
2 changes: 1 addition & 1 deletion crates/sdk/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ pub async fn query_and_print_unbonds(
let mut not_yet_withdrawable = HashMap::<Epoch, token::Amount>::new();
for ((_start_epoch, withdraw_epoch), amount) in unbonds.into_iter() {
if withdraw_epoch <= current_epoch {
total_withdrawable = checked!(total_withdrawable + amount)?;
checked!(total_withdrawable += amount)?;
} else {
let withdrawable_amount =
not_yet_withdrawable.entry(withdraw_epoch).or_default();
Expand Down
Loading