Skip to content

Commit

Permalink
Merge branch 'brent/fix-bond-amount' (#2374)
Browse files Browse the repository at this point in the history
* brent/fix-bond-amount:
  changelog: add #2374
  fix `bond_amount`
  • Loading branch information
brentstone committed Jan 12, 2024
2 parents ed632b5 + 11acd03 commit 94ceb83
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/improvements/2374-fix-bond-amount.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Fix the function to more accurately account for slashes.
([\#2374](https://github.com/anoma/namada/pull/2374))
34 changes: 30 additions & 4 deletions proof_of_stake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1564,8 +1564,7 @@ where
S: StorageRead,
{
let params = read_pos_params(storage)?;
// Outer key is the start epoch used to calculate slashes. The inner
// keys are discarded after applying slashes.
// Outer key is the start epoch used to calculate slashes.
let mut amounts: BTreeMap<Epoch, token::Amount> = BTreeMap::default();

// Bonds
Expand Down Expand Up @@ -1600,6 +1599,7 @@ where
}
}

// Redelegations
if bond_id.validator != bond_id.source {
// Add outgoing redelegations that are still contributing to the source
// validator's stake
Expand Down Expand Up @@ -1668,6 +1668,9 @@ where

if !amounts.is_empty() {
let slashes = find_validator_slashes(storage, &bond_id.validator)?;
let redelegated_bonded =
delegator_redelegated_bonds_handle(&bond_id.source)
.at(&bond_id.validator);

// Apply slashes
for (&start, amount) in amounts.iter_mut() {
Expand All @@ -1684,7 +1687,30 @@ where
.cloned()
.collect::<Vec<_>>();

*amount = apply_list_slashes(&params, &list_slashes, *amount);
let slash_epoch_filter =
|e: Epoch| e + params.slash_processing_epoch_offset() <= epoch;

let redelegated_bonds =
redelegated_bonded.at(&start).collect_map(storage)?;

let result_fold = fold_and_slash_redelegated_bonds(
storage,
&params,
&redelegated_bonds,
start,
&list_slashes,
slash_epoch_filter,
);

let total_not_redelegated = *amount - result_fold.total_redelegated;

let after_not_redelegated = apply_list_slashes(
&params,
&list_slashes,
total_not_redelegated,
);

*amount = after_not_redelegated + result_fold.total_after_slashing;
}
}

Expand Down Expand Up @@ -1715,7 +1741,7 @@ where
let mut amounts: BTreeMap<Epoch, BTreeMap<Epoch, token::Amount>> =
BTreeMap::default();

// Only need to do bonds since rewwards are accumulated during
// Only need to do bonds since rewards are accumulated during
// `unbond_tokens`
let bonds =
bond_handle(&bond_id.source, &bond_id.validator).get_data_handler();
Expand Down

0 comments on commit 94ceb83

Please sign in to comment.