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

Fix bond amount for addresses #2374

Merged
merged 2 commits into from
Jan 13, 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/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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Fix the function to more accurately account for slashes.
- Fix the function `bond_amount` to more accurately account for slashes.

looks like this maybe got lost

([\#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
Loading