Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
brentstone committed Aug 10, 2023
1 parent 03c54fe commit 7d018f9
Show file tree
Hide file tree
Showing 2 changed files with 479 additions and 87 deletions.
42 changes: 17 additions & 25 deletions proof_of_stake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2198,6 +2198,7 @@ where
delegator_redelegated_bonds_handle(source).at(validator);

// `resultUnbonding`
// Find the bonds to fully unbond and one to partially unbond, if necessary
let bonds_to_unbond = find_bonds_to_remove(
storage,
&bonds_handle.get_data_handler(),
Expand All @@ -2206,6 +2207,8 @@ where
dbg!(&bonds_to_unbond);

// `modifiedRedelegation`
// A bond may have both redelegated and non-redelegated tokens in it. If
// this is the case, compute the modified state of the redelegation.
let modified_redelegation = match bonds_to_unbond.new_entry {
Some((bond_epoch, new_bond_amount)) => {
println!(
Expand All @@ -2229,19 +2232,18 @@ where
None => ModifiedRedelegation::default(),
};

// Compute the new unbonds eagerly
// `keysUnbonds`
let unbond_keys = if let Some((start_epoch, _)) = bonds_to_unbond.new_entry
{
let mut to_remove = bonds_to_unbond.epochs.clone();
to_remove.insert(start_epoch);
to_remove
} else {
bonds_to_unbond.epochs.clone()
};

let bond_epochs_to_unbond =
if let Some((start_epoch, _)) = bonds_to_unbond.new_entry {
let mut to_remove = bonds_to_unbond.epochs.clone();
to_remove.insert(start_epoch);
to_remove
} else {
bonds_to_unbond.epochs.clone()
};
// `newUnbonds`
// TODO: in-memory or directly into storage via Lazy?
let new_unbonds_map = unbond_keys
let new_unbonds_map = bond_epochs_to_unbond
.into_iter()
.map(|epoch| {
let cur_bond_value = bonds_handle
Expand Down Expand Up @@ -2274,12 +2276,7 @@ where
}

// `updatedUnbonded`
// TODO: can this be combined with the previous step?
// TODO: figure out what I do here with both unbonds and unbond_records!
// It seems that if this unbond is not a redelegation, then we update the
// unbonds in storage with the `new_unbonds_map`. If it is a redelegation,
// then we don't do anything with this new map in storage.
// Yeah, we don't record unbonds for redelegations
// Update the unbonds in storage using the eager map computed above
if !is_redelegation {
for ((start_epoch, withdraw_epoch), unbond_amount) in
new_unbonds_map.iter()
Expand All @@ -2297,13 +2294,6 @@ where
}
}

// NEW REDELEGATED UNBONDS
// NOTE: I think we only need to update the redelegated unbonds if this is
// NOT a redelegation
// We should do this regardless - the `is_redelegation` is to determine if
// the current call is from redelegation, but there might be redelegation
// already that's being unbonded or re-redelegated which is being updated
// here.
// `newRedelegatedUnbonds`
println!("\nDEBUGGING REDELEGATED UNBONDS\n");
dbg!(
Expand Down Expand Up @@ -2683,6 +2673,8 @@ struct ModifiedRedelegation {
new_amount: Option<token::Change>,
}

/// Used in `fn unbond_tokens` to compute the modified state of a redelegation
/// if redelegated tokens are being unbonded.
fn compute_modified_redelegation<S>(
storage: &S,
redelegated_bonds: &RedelegatedBonds,
Expand Down Expand Up @@ -2836,7 +2828,7 @@ type EagerRedelegatedUnbonds = BTreeMap<
/// Check assumptions in the Quint spec namada-redelegation.qnt
/// TODO: try to optimize this by only writing to storage via Lazy!
fn compute_new_redelegated_unbonds<S>(
storage: &mut S,
storage: &S,
redelegated_bonds: &NestedMap<Epoch, RedelegatedBonds>,
epochs_to_remove: &HashSet<Epoch>,
modified_redelegation: &ModifiedRedelegation,
Expand Down
Loading

0 comments on commit 7d018f9

Please sign in to comment.