Simplify get_current_position
bounds checking
The logic of the current implementation of Position::get_current_position
is non-trivial to understand from the perspective of an external developer or auditor. We find that it could be simplified by writing some of the results of the arithmetic operations and conditions to appropriately named variables, and then test bounds using those variables.
An example is provided:
match self.unlocking_start {
None => Ok(PositionState::LOCKED),
Some(unlocking_start) => {
let unlock_finished = unlocking_start + (unlocking_duration as u64);
let is_activated = self.activation_epoch <= current_epoch;
let is_unlock_started = unlocking_start <= current_epoch;
let is_unlock_finished = unlock_finished <= current_epoch;
if is_activated && !is_unlock_started {
Ok(PositionState::PREUNLOCKING)
} else if is_unlock_started && !is_unlock_finished {
Ok(PositionState::UNLOCKING)
} else {
Ok(PositionState::UNLOCKED)
}
}
}