This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Problem: AuRa's unsafeties around step duration #7282
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,8 +51,12 @@ mod finality; | |
|
||
/// `AuthorityRound` params. | ||
pub struct AuthorityRoundParams { | ||
/// Time to wait before next block or authority switching. | ||
pub step_duration: Duration, | ||
/// Time to wait before next block or authority switching, | ||
/// in seconds. | ||
/// | ||
/// Deliberately typed as u16 as too high of a value leads | ||
/// to slow block issuance. | ||
pub step_duration: u16, | ||
/// Starting step, | ||
pub start_step: Option<u64>, | ||
/// Valid validators. | ||
|
@@ -71,10 +75,17 @@ pub struct AuthorityRoundParams { | |
pub maximum_uncle_count: usize, | ||
} | ||
|
||
const U16_MAX: usize = ::std::u16::MAX as usize; | ||
|
||
impl From<ethjson::spec::AuthorityRoundParams> for AuthorityRoundParams { | ||
fn from(p: ethjson::spec::AuthorityRoundParams) -> Self { | ||
let mut step_duration_usize: usize = p.step_duration.into(); | ||
if step_duration_usize > U16_MAX { | ||
step_duration_usize = U16_MAX; | ||
warn!(target: "engine", "step_duration is too high ({}), setting it to {}", step_duration_usize, U16_MAX); | ||
} | ||
AuthorityRoundParams { | ||
step_duration: Duration::from_secs(p.step_duration.into()), | ||
step_duration: step_duration_usize as u16, | ||
validators: new_validator_set(p.validators), | ||
start_step: p.start_step.map(Into::into), | ||
validate_score_transition: p.validate_score_transition.map_or(0, Into::into), | ||
|
@@ -92,26 +103,47 @@ impl From<ethjson::spec::AuthorityRoundParams> for AuthorityRoundParams { | |
struct Step { | ||
calibrate: bool, // whether calibration is enabled. | ||
inner: AtomicUsize, | ||
duration: Duration, | ||
duration: u16, | ||
} | ||
|
||
impl Step { | ||
fn load(&self) -> usize { self.inner.load(AtomicOrdering::SeqCst) } | ||
fn duration_remaining(&self) -> Duration { | ||
let now = unix_now(); | ||
let step_end = self.duration * (self.load() as u32 + 1); | ||
if step_end > now { | ||
step_end - now | ||
} else { | ||
Duration::from_secs(0) | ||
let expected_seconds = (self.load() as u64) | ||
.checked_add(1) | ||
.and_then(|ctr| ctr.checked_mul(self.duration as u64)); | ||
match expected_seconds { | ||
Some(secs) => { | ||
let step_end = Duration::from_secs(secs); | ||
if step_end > now { | ||
step_end - now | ||
} else { | ||
Duration::from_secs(0) | ||
} | ||
}, | ||
None => { | ||
let ctr = self.load(); | ||
error!(target: "engine", "Step counter is too high: {}, aborting", ctr); | ||
panic!("step counter is too high: {}", ctr) | ||
}, | ||
} | ||
|
||
} | ||
fn increment(&self) { | ||
self.inner.fetch_add(1, AtomicOrdering::SeqCst); | ||
use std::usize; | ||
// fetch_add won't panic on overflow but will rather wrap | ||
// around, leading to zero as the step counter, which might | ||
// lead to unexpected situations, so it's better to shut down. | ||
if self.inner.fetch_add(1, AtomicOrdering::SeqCst) == usize::MAX { | ||
error!(target: "engine", "Step counter is too high: {}, aborting", usize::MAX); | ||
panic!("step counter is too high: {}", usize::MAX); | ||
} | ||
|
||
} | ||
fn calibrate(&self) { | ||
if self.calibrate { | ||
let new_step = unix_now().as_secs() / self.duration.as_secs(); | ||
let new_step = unix_now().as_secs() / (self.duration as u64); | ||
self.inner.store(new_step as usize, AtomicOrdering::SeqCst); | ||
} | ||
} | ||
|
@@ -359,8 +391,12 @@ impl AsMillis for Duration { | |
impl AuthorityRound { | ||
/// Create a new instance of AuthorityRound engine. | ||
pub fn new(our_params: AuthorityRoundParams, machine: EthereumMachine) -> Result<Arc<Self>, Error> { | ||
if our_params.step_duration == 0 { | ||
error!(target: "engine", "Authority Round step duration can't be zero, aborting"); | ||
panic!("authority_round: step duration can't be zero") | ||
} | ||
let should_timeout = our_params.start_step.is_none(); | ||
let initial_step = our_params.start_step.unwrap_or_else(|| (unix_now().as_secs() / our_params.step_duration.as_secs())) as usize; | ||
let initial_step = our_params.start_step.unwrap_or_else(|| (unix_now().as_secs() / (our_params.step_duration as u64))) as usize; | ||
let engine = Arc::new( | ||
AuthorityRound { | ||
transition_service: IoService::<()>::start()?, | ||
|
@@ -1011,7 +1047,7 @@ mod tests { | |
fn reports_skipped() { | ||
let last_benign = Arc::new(AtomicUsize::new(0)); | ||
let params = AuthorityRoundParams { | ||
step_duration: Default::default(), | ||
step_duration: 1, | ||
start_step: Some(1), | ||
validators: Box::new(TestSet::new(Default::default(), last_benign.clone())), | ||
validate_score_transition: 0, | ||
|
@@ -1051,7 +1087,7 @@ mod tests { | |
fn test_uncles_transition() { | ||
let last_benign = Arc::new(AtomicUsize::new(0)); | ||
let params = AuthorityRoundParams { | ||
step_duration: Default::default(), | ||
step_duration: 1, | ||
start_step: Some(1), | ||
validators: Box::new(TestSet::new(Default::default(), last_benign.clone())), | ||
validate_score_transition: 0, | ||
|
@@ -1073,4 +1109,50 @@ mod tests { | |
assert_eq!(aura.maximum_uncle_count(1), 0); | ||
assert_eq!(aura.maximum_uncle_count(100), 0); | ||
} | ||
|
||
#[test] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indentation issue here |
||
#[should_panic(expected="counter is too high")] | ||
fn test_counter_increment_too_high() { | ||
use super::Step; | ||
let step = Step { | ||
calibrate: false, | ||
inner: AtomicUsize::new(::std::usize::MAX), | ||
duration: 1, | ||
}; | ||
step.increment(); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected="counter is too high")] | ||
fn test_counter_duration_remaining_too_high() { | ||
use super::Step; | ||
let step = Step { | ||
calibrate: false, | ||
inner: AtomicUsize::new(::std::usize::MAX), | ||
duration: 1, | ||
}; | ||
step.duration_remaining(); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected="authority_round: step duration can't be zero")] | ||
fn test_step_duration_zero() { | ||
let last_benign = Arc::new(AtomicUsize::new(0)); | ||
let params = AuthorityRoundParams { | ||
step_duration: 0, | ||
start_step: Some(1), | ||
validators: Box::new(TestSet::new(Default::default(), last_benign.clone())), | ||
validate_score_transition: 0, | ||
validate_step_transition: 0, | ||
immediate_transitions: true, | ||
maximum_uncle_count_transition: 0, | ||
maximum_uncle_count: 0, | ||
block_reward: Default::default(), | ||
}; | ||
|
||
let mut c_params = ::spec::CommonParams::default(); | ||
c_params.gas_limit_bound_divisor = 5.into(); | ||
let machine = ::machine::EthereumMachine::regular(c_params, Default::default()); | ||
AuthorityRound::new(params, machine).unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.