Skip to content

Commit

Permalink
fix: set next_epoch_diff to one instead of panic when it is zero
Browse files Browse the repository at this point in the history
  • Loading branch information
doitian committed Oct 10, 2019
1 parent c9d667b commit 103247c
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions spec/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,35 +622,34 @@ impl Consensus {
&adjusted_last_epoch_hash_rate * epoch_duration_target,
U256::one(),
);
let next_epoch_diff = if bound {
let diff_denominator = if bound {
if last_orphan_rate.is_zero() {
let denominator = U256::from(next_epoch_length);
(diff_numerator / denominator).into_u256()
RationalU256::new(next_epoch_length_u256, U256::one())
} else {
let orphan_rate_estimation_recip = ((&last_orphan_rate + U256::one())
* &epoch_duration_target_u256
* &last_epoch_length_u256
/ (&last_orphan_rate * &last_epoch_duration * &next_epoch_length_u256))
.saturating_sub_u256(U256::one());

let denominator = if orphan_rate_estimation_recip.is_zero() {
if orphan_rate_estimation_recip.is_zero() {
// small probability event, use o_ideal for now
(orphan_rate_target + U256::one()) * next_epoch_length_u256
} else {
let orphan_rate_estimation = RationalU256::one() / orphan_rate_estimation_recip;
(orphan_rate_estimation + U256::one()) * next_epoch_length_u256
};
(diff_numerator / denominator).into_u256()
}
}
} else {
let denominator = (orphan_rate_target + U256::one()) * next_epoch_length_u256;
(diff_numerator / denominator).into_u256()
(orphan_rate_target + U256::one()) * next_epoch_length_u256
};

debug_assert!(
next_epoch_diff > U256::zero(),
"next_epoch_diff should greater than one"
);
let next_epoch_diff = if diff_numerator > diff_denominator {
(diff_numerator / diff_denominator).into_u256()
} else {
// next_epoch_diff cannot be zero
U256::one()
};

let primary_epoch_reward = self.primary_epoch_reward_of_next_epoch(last_epoch).as_u64();
let block_reward = Capacity::shannons(primary_epoch_reward / next_epoch_length);
Expand Down

0 comments on commit 103247c

Please sign in to comment.