From cfbef115e3cf53263db63554d5f08fe5460e5c58 Mon Sep 17 00:00:00 2001 From: Erwan Date: Mon, 29 Jan 2024 14:23:15 -0500 Subject: [PATCH] staking: only fetch `current_epoch` once --- crates/core/component/stake/src/component.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crates/core/component/stake/src/component.rs b/crates/core/component/stake/src/component.rs index 416bdf6e56..33334813c8 100644 --- a/crates/core/component/stake/src/component.rs +++ b/crates/core/component/stake/src/component.rs @@ -1484,6 +1484,7 @@ pub trait StateReadExt: StateRead { /// TODO(erwan): move this to the `ValidatorManager` async fn compute_unbonding_delay_for_validator( &self, + current_epoch: Epoch, validator_identity: &IdentityKey, ) -> Result { let Some(val_bonding_state) = self.validator_bonding_state(validator_identity).await? @@ -1498,7 +1499,7 @@ pub trait StateReadExt: StateRead { let epoch_delay = match val_bonding_state { Bonded => min_epoch_delay, - Unbonding { unbonds_at_epoch } => unbonds_at_epoch.saturating_sub(unbonds_at_epoch), + Unbonding { unbonds_at_epoch } => unbonds_at_epoch.saturating_sub(current_epoch.index), Unbonded => 0u64, }; @@ -1513,7 +1514,9 @@ pub trait StateReadExt: StateRead { /// unbonding epoch. async fn compute_unbonding_epoch_for_validator(&self, id: &IdentityKey) -> Result { let current_epoch = self.get_current_epoch().await?; - let unbonding_delay = self.compute_unbonding_delay_for_validator(id).await?; + let unbonding_delay = self + .compute_unbonding_delay_for_validator(current_epoch, id) + .await?; let unbonding_epoch = current_epoch.index.saturating_add(unbonding_delay); Ok(unbonding_epoch) }