Skip to content

Commit ccbb294

Browse files
committed
fix(staking): mining eligibility strategy
1 parent dda3a0b commit ccbb294

File tree

1 file changed

+17
-30
lines changed

1 file changed

+17
-30
lines changed

validations/src/eligibility/current.rs

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -173,38 +173,25 @@ where
173173
where
174174
ISK: Into<Address>,
175175
{
176-
let power = match self.query_power(validator, Capability::Mining, epoch) {
177-
Ok(p) => p,
178-
Err(e) => {
179-
// Early exit if the stake key does not exist
180-
return match e {
181-
StakesError::ValidatorNotFound { .. } => {
182-
Ok(IneligibilityReason::NotStaking.into())
183-
}
184-
e => Err(e),
185-
};
186-
}
187-
};
176+
let validator: Address = validator.into();
188177

189-
// Validators with power 0 should not be eligible to mine a block
190-
if power == Power::from(0) {
191-
return Ok(IneligibilityReason::InsufficientPower.into());
192-
}
193-
194-
// Requirement no. 2 from the WIP:
195-
// "the mining power of the block proposer is in the `rf / stakers`th quantile among the mining powers of all
196-
// the stakers"
197-
// TODO: verify if defaulting to 0 makes sense
198-
let mut rank = self.rank(Capability::Mining, epoch);
199-
let (_, threshold) = rank
200-
.nth((replication_factor - 1).into())
201-
.unwrap_or_default();
202-
if power < threshold {
203-
return Ok(IneligibilityReason::InsufficientPower.into());
204-
}
178+
// Cap replication factor to 2/3rds of total stake entries count
179+
let max_replication_factor = u16::try_from((((self.stakes_count() * 2) as f64) / 3.0) as u32).unwrap_or(u16::MAX);
180+
let replication_factor = if replication_factor > max_replication_factor {
181+
max_replication_factor
182+
} else {
183+
replication_factor
184+
};
205185

206-
// If all the requirements are met, we can deem it as eligible
207-
Ok(Eligible::Yes)
186+
Ok(
187+
match self.by_rank(Capability::Mining, epoch)
188+
.take(replication_factor as usize)
189+
.find(|(key, _)| key.validator == validator)
190+
{
191+
Some(_) => Eligible::Yes,
192+
None => IneligibilityReason::InsufficientPower.into()
193+
}
194+
)
208195
}
209196

210197
fn witnessing_eligibility<ISK>(

0 commit comments

Comments
 (0)