remove proposer_nonce

This commit is contained in:
keorn 2016-11-30 16:02:05 +00:00
parent bb8347477a
commit d128c20dc2

View File

@ -74,8 +74,6 @@ pub struct Tendermint {
round: AtomicUsize, round: AtomicUsize,
/// Consensus step. /// Consensus step.
step: RwLock<Step>, step: RwLock<Step>,
/// Used to swith proposer.
proposer_nonce: AtomicUsize,
/// Vote accumulator. /// Vote accumulator.
votes: VoteCollector, votes: VoteCollector,
/// Channel for updating the sealing. /// Channel for updating the sealing.
@ -104,7 +102,6 @@ impl Tendermint {
height: AtomicUsize::new(1), height: AtomicUsize::new(1),
round: AtomicUsize::new(0), round: AtomicUsize::new(0),
step: RwLock::new(Step::Propose), step: RwLock::new(Step::Propose),
proposer_nonce: AtomicUsize::new(0),
votes: VoteCollector::new(), votes: VoteCollector::new(),
message_channel: Mutex::new(None), message_channel: Mutex::new(None),
account_provider: Mutex::new(None), account_provider: Mutex::new(None),
@ -180,7 +177,7 @@ impl Tendermint {
Some(ref m) => m.block_hash, Some(ref m) => m.block_hash,
None => None, None => None,
}; };
self.generate_and_broadcast_message(block_hash) self.generate_and_broadcast_message(block_hash);
}, },
Step::Precommit => { Step::Precommit => {
trace!(target: "poa", "to_step: Transitioning to Precommit."); trace!(target: "poa", "to_step: Transitioning to Precommit.");
@ -225,7 +222,7 @@ impl Tendermint {
/// Round proposer switching. /// Round proposer switching.
fn is_proposer(&self, address: &Address) -> Result<(), EngineError> { fn is_proposer(&self, address: &Address) -> Result<(), EngineError> {
let ref p = self.our_params; let ref p = self.our_params;
let proposer_nonce = self.proposer_nonce.load(AtomicOrdering::SeqCst); let proposer_nonce = self.height.load(AtomicOrdering::SeqCst) + self.round.load(AtomicOrdering::SeqCst);
let proposer = p.authorities.get(proposer_nonce % p.authority_n).expect("There are authority_n authorities; taking number modulo authority_n gives number in authority_n range; qed"); let proposer = p.authorities.get(proposer_nonce % p.authority_n).expect("There are authority_n authorities; taking number modulo authority_n gives number in authority_n range; qed");
if proposer == address { if proposer == address {
Ok(()) Ok(())
@ -243,13 +240,11 @@ impl Tendermint {
} }
fn increment_round(&self, n: Round) { fn increment_round(&self, n: Round) {
self.proposer_nonce.fetch_add(n, AtomicOrdering::SeqCst);
self.round.fetch_add(n, AtomicOrdering::SeqCst); self.round.fetch_add(n, AtomicOrdering::SeqCst);
} }
fn reset_round(&self) { fn reset_round(&self) {
self.last_lock.store(0, AtomicOrdering::SeqCst); self.last_lock.store(0, AtomicOrdering::SeqCst);
self.proposer_nonce.fetch_add(1, AtomicOrdering::SeqCst);
self.height.fetch_add(1, AtomicOrdering::SeqCst); self.height.fetch_add(1, AtomicOrdering::SeqCst);
self.round.store(0, AtomicOrdering::SeqCst); self.round.store(0, AtomicOrdering::SeqCst);
} }
@ -627,7 +622,7 @@ mod tests {
let engine = spec.engine; let engine = spec.engine;
let mut header = Header::default(); let mut header = Header::default();
let validator = insert_and_unlock(&tap, "0"); let validator = insert_and_unlock(&tap, "1");
header.set_author(validator); header.set_author(validator);
let seal = proposal_seal(&tap, &header, 0); let seal = proposal_seal(&tap, &header, 0);
header.set_seal(seal); header.set_seal(seal);
@ -684,7 +679,7 @@ mod tests {
fn can_generate_seal() { fn can_generate_seal() {
let (spec, tap) = setup(); let (spec, tap) = setup();
let proposer = insert_and_register(&tap, &spec.engine, "0"); let proposer = insert_and_register(&tap, &spec.engine, "1");
let (b, seal) = propose_default(&spec, proposer); let (b, seal) = propose_default(&spec, proposer);
assert!(b.try_seal(spec.engine.as_ref(), seal).is_ok()); assert!(b.try_seal(spec.engine.as_ref(), seal).is_ok());
@ -705,7 +700,7 @@ mod tests {
let r = 0; let r = 0;
// Propose // Propose
let (b, mut seal) = propose_default(&spec, v0.clone()); let (b, mut seal) = propose_default(&spec, v1.clone());
let proposal = Some(b.header().bare_hash()); let proposal = Some(b.header().bare_hash());
// Register IoHandler remembers messages. // Register IoHandler remembers messages.
@ -729,4 +724,19 @@ mod tests {
let second = test_io.received.read()[5] == ClientIoMessage::SubmitSeal(proposal.unwrap(), seal); let second = test_io.received.read()[5] == ClientIoMessage::SubmitSeal(proposal.unwrap(), seal);
assert!(first ^ second); assert!(first ^ second);
} }
#[test]
fn timeout_transitioning() {
::env_logger::init().unwrap();
let (spec, tap) = setup();
let engine = spec.engine.clone();
let mut db_result = get_temp_state_db();
let mut db = db_result.take();
spec.ensure_db_good(&mut db, &TrieFactory::new(TrieSpec::Secure)).unwrap();
let v = insert_and_register(&tap, &engine, "0");
::std::thread::sleep(::std::time::Duration::from_millis(15000));
println!("done");
}
} }