Skip to content

Commit ab829a7

Browse files
committed
Merge branch 'tomas/checked-assign-ops' (#3374)
* origin/tomas/checked-assign-ops: changelog: add #3374 use the new smooth-operator checked assign ops
2 parents 5cc23b4 + 183aa61 commit ab829a7

File tree

8 files changed

+33
-40
lines changed

8 files changed

+33
-40
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Refactored checked assign arithmetic operations to use smooth-operator macro.
2+
([\#3374](https://github.com/anoma/namada/pull/3374))

crates/governance/src/utils.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,11 @@ pub fn compute_proposal_result(
338338
let vote_type = votes.validators_vote.get(&address);
339339
if let Some(vote) = vote_type {
340340
if vote.is_yay() {
341-
yay_voting_power = checked!(yay_voting_power + vote_power)?;
341+
checked!(yay_voting_power += vote_power)?;
342342
} else if vote.is_nay() {
343-
nay_voting_power = checked!(nay_voting_power + vote_power)?;
343+
checked!(nay_voting_power += vote_power)?;
344344
} else if vote.is_abstain() {
345-
abstain_voting_power =
346-
checked!(abstain_voting_power + vote_power)?;
345+
checked!(abstain_voting_power += vote_power)?;
347346
}
348347
}
349348
}
@@ -393,12 +392,11 @@ pub fn compute_proposal_result(
393392
}
394393
}
395394
} else if delegator_vote.is_yay() {
396-
yay_voting_power = checked!(yay_voting_power + vote_power)?;
395+
checked!(yay_voting_power += vote_power)?;
397396
} else if delegator_vote.is_nay() {
398-
nay_voting_power = checked!(nay_voting_power + vote_power)?;
397+
checked!(nay_voting_power += vote_power)?;
399398
} else if delegator_vote.is_abstain() {
400-
abstain_voting_power =
401-
checked!(abstain_voting_power + vote_power)?;
399+
checked!(abstain_voting_power += vote_power)?;
402400
}
403401
}
404402
}

crates/node/src/storage/rocksdb.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ impl DB for RocksDB {
10981098
// Read from latest height
10991099
return self.read_subspace_val(key);
11001100
} else {
1101-
raw_height = checked!(raw_height + 1)?
1101+
checked!(raw_height += 1)?
11021102
}
11031103
}
11041104
}

crates/proof_of_stake/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ where
801801
let rp =
802802
rewards_products.get(storage, &ep)?.unwrap_or_default();
803803
let slashed_rewards = slashed_amount.mul_floor(rp)?;
804-
rewards = checked!(rewards + slashed_rewards)?;
804+
checked!(rewards += slashed_rewards)?;
805805
}
806806
}
807807

@@ -905,7 +905,7 @@ where
905905
bonds_for_removal.new_entry =
906906
Some((bond_epoch, checked!(bond_amount - to_unbond)?));
907907
}
908-
remaining = checked!(remaining - to_unbond)?;
908+
checked!(remaining -= to_unbond)?;
909909
if remaining.is_zero() {
910910
break;
911911
}
@@ -2810,7 +2810,7 @@ where
28102810
// Add reward tokens tallied during previous withdrawals
28112811
let counter_rewards =
28122812
take_rewards_from_counter(storage, &source, validator)?;
2813-
reward_tokens = checked!(reward_tokens + counter_rewards)?;
2813+
checked!(reward_tokens += counter_rewards)?;
28142814

28152815
// Update the last claim epoch in storage
28162816
write_last_reward_claim_epoch(storage, &source, validator, current_epoch)?;

crates/proof_of_stake/src/rewards.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -325,21 +325,19 @@ where
325325

326326
// Proposer reward
327327
if address == *proposer_address {
328-
rewards_frac = checked!(rewards_frac + coeffs.proposer_coeff)?;
328+
checked!(rewards_frac += coeffs.proposer_coeff)?;
329329
}
330330

331331
// Signer reward
332332
if signer_set.contains(&address) {
333333
let signing_frac =
334334
checked!(stake_unscaled / signing_stake_unscaled)?;
335-
rewards_frac =
336-
checked!(rewards_frac + (coeffs.signer_coeff * signing_frac))?;
335+
checked!(rewards_frac += (coeffs.signer_coeff * signing_frac))?;
337336
}
338337
// Consensus validator reward
339-
rewards_frac = checked!(
340-
rewards_frac
341-
+ (coeffs.active_val_coeff
342-
* (stake_unscaled / consensus_stake_unscaled))
338+
checked!(
339+
rewards_frac += (coeffs.active_val_coeff
340+
* (stake_unscaled / consensus_stake_unscaled))
343341
)?;
344342

345343
// To be added to the rewards accumulator
@@ -598,7 +596,7 @@ where
598596
debug_assert!(ep <= claim_end);
599597
let rp = rewards_products.get(storage, &ep)?.unwrap_or_default();
600598
let reward = bond_amount.mul_floor(rp)?;
601-
reward_tokens = checked!(reward_tokens + reward)?;
599+
checked!(reward_tokens += reward)?;
602600
}
603601

604602
Ok(reward_tokens)

crates/proof_of_stake/src/slashing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ where
334334
// Then update validator and total deltas
335335
for (epoch, slash_amount) in slash_amounts {
336336
let slash_delta = checked!(slash_amount - slash_acc)?;
337-
slash_acc = checked!(slash_acc + slash_delta)?;
337+
checked!(slash_acc += slash_delta)?;
338338

339339
let neg_slash_delta = checked!(-slash_delta.change())?;
340340
update_validator_deltas(
@@ -681,7 +681,7 @@ where
681681
redelegated_bonds = new_redelegated_bonds;
682682

683683
// `newSum`
684-
sum = checked!(sum + amount)?;
684+
checked!(sum += amount)?;
685685

686686
// `newSlashesMap`
687687
let cur = slashed_amounts.entry(epoch).or_default();

crates/sdk/src/queries/vp/pos.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ where
535535
amount,
536536
) = result?;
537537
if epoch >= withdrawable {
538-
total = checked!(total + amount)?;
538+
checked!(total += amount)?;
539539
}
540540
}
541541
Ok(total)
@@ -733,32 +733,27 @@ fn enrich_bonds_and_unbonds(
733733

734734
for bond in &detail.bonds {
735735
let slashed_bond = bond.slashed_amount.unwrap_or_default();
736-
bond_total = checked!(bond_total + bond.amount)?;
737-
bond_total_slashed =
738-
checked!(bond_total_slashed + slashed_bond)?;
736+
checked!(bond_total += bond.amount)?;
737+
checked!(bond_total_slashed += slashed_bond)?;
739738
}
740739
for unbond in &detail.unbonds {
741740
let slashed_unbond =
742741
unbond.slashed_amount.unwrap_or_default();
743-
unbond_total = checked!(unbond_total + unbond.amount)?;
744-
unbond_total_slashed =
745-
checked!(unbond_total_slashed + slashed_unbond)?;
742+
checked!(unbond_total += unbond.amount)?;
743+
checked!(unbond_total_slashed += slashed_unbond)?;
746744

747745
if current_epoch >= unbond.withdraw {
748-
withdrawable = checked!(
749-
withdrawable + unbond.amount - slashed_unbond
746+
checked!(
747+
withdrawable += unbond.amount - slashed_unbond
750748
)?;
751749
}
752750
}
753751

754-
bonds_total = checked!(bonds_total + bond_total)?;
755-
bonds_total_slashed =
756-
checked!(bonds_total_slashed + bond_total_slashed)?;
757-
unbonds_total = checked!(unbonds_total + unbond_total)?;
758-
unbonds_total_slashed =
759-
checked!(unbonds_total_slashed + unbond_total_slashed)?;
760-
total_withdrawable =
761-
checked!(total_withdrawable + withdrawable)?;
752+
checked!(bonds_total += bond_total)?;
753+
checked!(bonds_total_slashed += bond_total_slashed)?;
754+
checked!(unbonds_total += unbond_total)?;
755+
checked!(unbonds_total_slashed += unbond_total_slashed)?;
756+
checked!(total_withdrawable += withdrawable)?;
762757

763758
let enriched_detail = EnrichedBondsAndUnbondsDetail {
764759
data: detail,

crates/sdk/src/rpc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ pub async fn query_and_print_unbonds(
994994
let mut not_yet_withdrawable = HashMap::<Epoch, token::Amount>::new();
995995
for ((_start_epoch, withdraw_epoch), amount) in unbonds.into_iter() {
996996
if withdraw_epoch <= current_epoch {
997-
total_withdrawable = checked!(total_withdrawable + amount)?;
997+
checked!(total_withdrawable += amount)?;
998998
} else {
999999
let withdrawable_amount =
10001000
not_yet_withdrawable.entry(withdraw_epoch).or_default();

0 commit comments

Comments
 (0)