Clean up Delegation::stake_and_activating (#13460)
This commit is contained in:
parent
a81e7e7749
commit
737d3e376d
|
@ -276,54 +276,81 @@ impl Delegation {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn stake_and_activating(&self, epoch: Epoch, history: Option<&StakeHistory>) -> (u64, u64) {
|
fn stake_and_activating(
|
||||||
|
&self,
|
||||||
|
target_epoch: Epoch,
|
||||||
|
history: Option<&StakeHistory>,
|
||||||
|
) -> (u64, u64) {
|
||||||
if self.is_bootstrap() {
|
if self.is_bootstrap() {
|
||||||
|
// fully effective immediately
|
||||||
(self.stake, 0)
|
(self.stake, 0)
|
||||||
} else if epoch == self.activation_epoch {
|
} else if target_epoch == self.activation_epoch {
|
||||||
|
// all is activating
|
||||||
(0, self.stake)
|
(0, self.stake)
|
||||||
} else if epoch < self.activation_epoch {
|
} else if target_epoch < self.activation_epoch {
|
||||||
|
// not yet enabled
|
||||||
(0, 0)
|
(0, 0)
|
||||||
} else if let Some((history, mut entry)) = history.and_then(|history| {
|
} else if let Some((history, mut prev_epoch, mut prev_cluster_stake)) =
|
||||||
history
|
history.and_then(|history| {
|
||||||
.get(&self.activation_epoch)
|
history
|
||||||
.map(|entry| (history, entry))
|
.get(&self.activation_epoch)
|
||||||
}) {
|
.map(|cluster_stake_at_activation_epoch| {
|
||||||
// && !is_bootstrap() && epoch > self.activation_epoch
|
(
|
||||||
let mut effective_stake = 0;
|
history,
|
||||||
let mut next_epoch = self.activation_epoch;
|
self.activation_epoch,
|
||||||
|
cluster_stake_at_activation_epoch,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
{
|
||||||
|
// target_epoch > self.activation_epoch
|
||||||
|
|
||||||
// loop from my activation epoch until the current epoch
|
// loop from my activation epoch until the target epoch summing up my entitlement
|
||||||
// summing up my entitlement
|
// current stake is updated using its previous epoch's cluster stake
|
||||||
|
let mut current_epoch;
|
||||||
|
let mut current_effective_stake = 0;
|
||||||
loop {
|
loop {
|
||||||
if entry.activating == 0 {
|
current_epoch = prev_epoch + 1;
|
||||||
|
// if there is no activating stake at prev epoch, we should have been fully effective at this moment
|
||||||
|
if prev_cluster_stake.activating == 0 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// how much of the growth in stake this account is
|
// how much of the growth in stake this account is
|
||||||
// entitled to take
|
// entitled to take
|
||||||
let weight = (self.stake - effective_stake) as f64 / entry.activating as f64;
|
let remaining_activating_stake = self.stake - current_effective_stake;
|
||||||
|
let weight =
|
||||||
|
remaining_activating_stake as f64 / prev_cluster_stake.activating as f64;
|
||||||
|
|
||||||
// portion of activating stake in this epoch I'm entitled to
|
// portion of newly effective cluster stake I'm entitled to at current epoch
|
||||||
effective_stake +=
|
let newly_effective_cluster_stake =
|
||||||
((weight * entry.effective as f64 * self.warmup_cooldown_rate) as u64).max(1);
|
prev_cluster_stake.effective as f64 * self.warmup_cooldown_rate;
|
||||||
|
let newly_effective_stake =
|
||||||
|
((weight * newly_effective_cluster_stake) as u64).max(1);
|
||||||
|
|
||||||
if effective_stake >= self.stake {
|
current_effective_stake += newly_effective_stake;
|
||||||
effective_stake = self.stake;
|
if current_effective_stake >= self.stake {
|
||||||
|
current_effective_stake = self.stake;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
next_epoch += 1;
|
if current_epoch >= target_epoch || current_epoch >= self.deactivation_epoch {
|
||||||
if next_epoch >= epoch || next_epoch >= self.deactivation_epoch {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if let Some(next_entry) = history.get(&next_epoch) {
|
if let Some(current_cluster_stake) = history.get(¤t_epoch) {
|
||||||
entry = next_entry;
|
prev_epoch = current_epoch;
|
||||||
|
prev_cluster_stake = current_cluster_stake;
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(effective_stake, self.stake - effective_stake)
|
|
||||||
|
(
|
||||||
|
current_effective_stake,
|
||||||
|
self.stake - current_effective_stake,
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
// no history or I've dropped out of history, so assume fully activated
|
// no history or I've dropped out of history, so assume fully effective
|
||||||
(self.stake, 0)
|
(self.stake, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue