Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.

Calibrate step before rejection #4800

Merged
merged 4 commits into from
Mar 8, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions ethcore/src/engines/authority_round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ pub struct AuthorityRound {
client: RwLock<Option<Weak<EngineClient>>>,
signer: EngineSigner,
validators: Box<ValidatorSet + Send + Sync>,
/// Is this Engine just for testing (prevents step calibration).
test: bool,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be better to name it according to what it does. E.g. disable_step_calibration. Or, even better, invert it and name as calibrate_step

}

fn header_step(header: &Header) -> Result<usize, ::rlp::DecoderError> {
Expand Down Expand Up @@ -122,6 +124,7 @@ impl AuthorityRound {
client: RwLock::new(None),
signer: Default::default(),
validators: new_validator_set(our_params.validators),
test: our_params.start_step.is_some(),
});
// Do not initialize timeouts for tests.
if should_timeout {
Expand All @@ -131,6 +134,12 @@ impl AuthorityRound {
Ok(engine)
}

fn calibrate_step(&self) {
if !self.test {
self.step.store((unix_now().as_secs() / self.step_duration.as_secs()) as usize, AtomicOrdering::SeqCst);
}
}

fn remaining_step_duration(&self) -> Duration {
let now = unix_now();
let step_end = self.step_duration * (self.step.load(AtomicOrdering::SeqCst) as u32 + 1);
Expand All @@ -148,6 +157,16 @@ impl AuthorityRound {
fn is_step_proposer(&self, step: usize, address: &Address) -> bool {
self.step_proposer(step) == *address
}

fn is_future_step(&self, step: usize) -> bool {
if step > self.step.load(AtomicOrdering::SeqCst) + 1 {
// Make absolutely sure that the step is correct.
self.calibrate_step();
step > self.step.load(AtomicOrdering::SeqCst) + 1
} else {
false
}
}
}

fn unix_now() -> Duration {
Expand Down Expand Up @@ -285,7 +304,11 @@ impl Engine for AuthorityRound {
fn verify_block_unordered(&self, header: &Header, _block: Option<&[u8]>) -> Result<(), Error> {
let header_step = header_step(header)?;
// Give one step slack if step is lagging, double vote is still not possible.
if header_step <= self.step.load(AtomicOrdering::SeqCst) + 1 {
if self.is_future_step(header_step) {
trace!(target: "engine", "verify_block_unordered: block from the future");
self.validators.report_benign(header.author());
Err(BlockError::InvalidSeal)?
} else {
let proposer_signature = header_signature(header)?;
let correct_proposer = self.step_proposer(header_step);
if verify_address(&correct_proposer, &proposer_signature, &header.bare_hash())? {
Expand All @@ -294,10 +317,6 @@ impl Engine for AuthorityRound {
trace!(target: "engine", "verify_block_unordered: bad proposer for step: {}", header_step);
Err(EngineError::NotProposer(Mismatch { expected: correct_proposer, found: header.author().clone() }))?
}
} else {
trace!(target: "engine", "verify_block_unordered: block from the future");
self.validators.report_benign(header.author());
Err(BlockError::InvalidSeal)?
}
}

Expand Down