Skip to content

Commit 30da9f2

Browse files
committed
Cleanup vote functions
1 parent 333307e commit 30da9f2

File tree

2 files changed

+76
-97
lines changed

2 files changed

+76
-97
lines changed

core/src/consensus/tendermint/message.rs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ use std::cmp;
1818

1919
use ccrypto::blake256;
2020
use ckey::{verify_schnorr, Error as KeyError, Public, SchnorrSignature};
21-
use ctypes::Header;
2221
use primitives::{Bytes, H256};
2322
use rlp::{Decodable, DecoderError, Encodable, RlpStream, UntrustedRlp};
2423
use snap;
2524

26-
use super::super::validator_set::DynamicValidator;
2725
use super::super::BitSet;
2826
use super::{BlockHash, Height, Step, View};
2927

@@ -331,28 +329,6 @@ pub struct ConsensusMessage {
331329
}
332330

333331
impl ConsensusMessage {
334-
/// If a locked node re-proposes locked proposal, the proposed_view is different from the header's view.
335-
pub fn new_proposal(
336-
signature: SchnorrSignature,
337-
validators: &DynamicValidator,
338-
proposal_header: &Header,
339-
proposed_view: View,
340-
prev_proposer_idx: usize,
341-
) -> Result<Self, ::rlp::DecoderError> {
342-
let height = proposal_header.number() as Height;
343-
let signer_index =
344-
validators.proposer_index(*proposal_header.parent_hash(), prev_proposer_idx, proposed_view as usize);
345-
346-
Ok(ConsensusMessage {
347-
signature,
348-
signer_index,
349-
on: VoteOn {
350-
step: VoteStep::new(height, proposed_view, Step::Propose),
351-
block_hash: Some(proposal_header.hash()),
352-
},
353-
})
354-
}
355-
356332
pub fn signature(&self) -> SchnorrSignature {
357333
self.signature
358334
}

core/src/consensus/tendermint/worker.rs

Lines changed: 76 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -819,45 +819,15 @@ impl Worker {
819819
}
820820

821821
fn generate_and_broadcast_message(&mut self, block_hash: Option<BlockHash>, is_restoring: bool) {
822-
if let Some(message) = self.generate_message(block_hash, is_restoring) {
822+
if let Some(message) = self.vote_on_block_hash(block_hash).expect("Error while vote") {
823+
self.handle_valid_message(&message, is_restoring);
823824
if !is_restoring {
824825
self.backup();
825826
}
826827
self.broadcast_message(message);
827828
}
828829
}
829830

830-
fn generate_message(&mut self, block_hash: Option<BlockHash>, is_restoring: bool) -> Option<ConsensusMessage> {
831-
let height = self.height;
832-
let r = self.view;
833-
let on = VoteOn {
834-
step: VoteStep::new(height, r, self.step.to_step()),
835-
block_hash,
836-
};
837-
let signer_index = self.signer_index().or_else(|| {
838-
ctrace!(ENGINE, "No message, since there is no engine signer.");
839-
None
840-
})?;
841-
let signature = self
842-
.sign(&on)
843-
.map_err(|error| {
844-
ctrace!(ENGINE, "{}th validator could not sign the message {}", signer_index, error);
845-
error
846-
})
847-
.ok()?;
848-
let message = ConsensusMessage {
849-
signature,
850-
signer_index,
851-
on,
852-
};
853-
self.votes_received.set(signer_index);
854-
self.votes.vote(message.clone());
855-
cinfo!(ENGINE, "Generated {:?} as {}th validator.", message, signer_index);
856-
self.handle_valid_message(&message, is_restoring);
857-
858-
Some(message)
859-
}
860-
861831
fn handle_valid_message(&mut self, message: &ConsensusMessage, is_restoring: bool) {
862832
let vote_step = &message.on.step;
863833
let is_newer_than_lock = match self.last_two_thirds_majority.view() {
@@ -1111,7 +1081,6 @@ impl Worker {
11111081
}
11121082

11131083
let header = sealed_block.header();
1114-
let hash = header.hash();
11151084
let parent_hash = header.parent_hash();
11161085

11171086
if let TendermintState::ProposeWaitBlockGeneration {
@@ -1131,19 +1100,9 @@ impl Worker {
11311100
);
11321101
return
11331102
}
1134-
let prev_proposer_idx = self.block_proposer_idx(*parent_hash).expect("Prev block must exists");
1135-
11361103
debug_assert_eq!(Ok(self.view), TendermintSealView::new(header.seal()).consensus_view());
11371104

1138-
let vote_on = VoteOn {
1139-
step: VoteStep::new(header.number() as Height, self.view, Step::Propose),
1140-
block_hash: Some(hash),
1141-
};
1142-
let signature = self.sign(&vote_on).expect("I am proposer");
1143-
self.votes.vote(
1144-
ConsensusMessage::new_proposal(signature, &*self.validators, header, self.view, prev_proposer_idx)
1145-
.expect("I am proposer"),
1146-
);
1105+
self.vote_on_header_for_proposal(&header).expect("I'm a proposer");
11471106

11481107
self.step = TendermintState::ProposeWaitImported {
11491108
block: Box::new(sealed_block.clone()),
@@ -1492,18 +1451,7 @@ impl Worker {
14921451

14931452
fn repropose_block(&mut self, block: encoded::Block) {
14941453
let header = block.decode_header();
1495-
let vote_on = VoteOn {
1496-
step: VoteStep::new(header.number() as Height, self.view, Step::Propose),
1497-
block_hash: Some(header.hash()),
1498-
};
1499-
let parent_hash = header.parent_hash();
1500-
let prev_proposer_idx = self.block_proposer_idx(*parent_hash).expect("Prev block must exists");
1501-
let signature = self.sign(&vote_on).expect("I am proposer");
1502-
self.votes.vote(
1503-
ConsensusMessage::new_proposal(signature, &*self.validators, &header, self.view, prev_proposer_idx)
1504-
.expect("I am proposer"),
1505-
);
1506-
1454+
self.vote_on_header_for_proposal(&header).expect("I am proposer");
15071455
self.proposal = Proposal::new_imported(header.hash());
15081456
self.broadcast_proposal_block(self.view, block);
15091457
}
@@ -1531,8 +1479,76 @@ impl Worker {
15311479
self.signer.set_to_keep_decrypted_account(ap, address);
15321480
}
15331481

1534-
fn sign(&self, vote_on: &VoteOn) -> Result<SchnorrSignature, Error> {
1535-
self.signer.sign(vote_on.hash()).map_err(Into::into)
1482+
fn vote_on_block_hash(&mut self, block_hash: Option<BlockHash>) -> Result<Option<ConsensusMessage>, Error> {
1483+
let signer_index = if let Some(signer_index) = self.signer_index() {
1484+
signer_index
1485+
} else {
1486+
ctrace!(ENGINE, "No message, since there is no engine signer.");
1487+
return Ok(None)
1488+
};
1489+
1490+
let on = VoteOn {
1491+
step: VoteStep::new(self.height, self.view, self.step.to_step()),
1492+
block_hash,
1493+
};
1494+
let signature = self.signer.sign(on.hash())?;
1495+
1496+
let vote = ConsensusMessage {
1497+
signature,
1498+
signer_index,
1499+
on,
1500+
};
1501+
1502+
self.votes_received.set(vote.signer_index);
1503+
self.votes.vote(vote.clone());
1504+
cinfo!(ENGINE, "Voted {:?} as {}th validator.", vote, signer_index);
1505+
Ok(Some(vote))
1506+
}
1507+
1508+
fn vote_on_header_for_proposal(&mut self, header: &Header) -> Result<ConsensusMessage, Error> {
1509+
assert!(header.number() == self.height);
1510+
1511+
let parent_hash = header.parent_hash();
1512+
let prev_proposer_idx = self.block_proposer_idx(*parent_hash).expect("Prev block must exists");
1513+
let signer_index = self.validators.proposer_index(*parent_hash, prev_proposer_idx, self.view as usize);
1514+
1515+
let on = VoteOn {
1516+
step: VoteStep::new(self.height, self.view, Step::Propose),
1517+
block_hash: Some(header.hash()),
1518+
};
1519+
let signature = self.signer.sign(on.hash())?;
1520+
1521+
let vote = ConsensusMessage {
1522+
signature,
1523+
signer_index,
1524+
on,
1525+
};
1526+
1527+
self.votes.vote(vote.clone());
1528+
cinfo!(ENGINE, "Voted {:?} as {}th proposer.", vote, signer_index);
1529+
Ok(vote)
1530+
}
1531+
1532+
fn recover_proposal_vote(
1533+
&self,
1534+
header: &Header,
1535+
proposed_view: View,
1536+
signature: SchnorrSignature,
1537+
) -> Option<ConsensusMessage> {
1538+
let prev_proposer_idx = self.block_proposer_idx(*header.parent_hash())?;
1539+
let signer_index =
1540+
self.validators.proposer_index(*header.parent_hash(), prev_proposer_idx, proposed_view as usize);
1541+
1542+
let on = VoteOn {
1543+
step: VoteStep::new(header.number(), proposed_view, Step::Propose),
1544+
block_hash: Some(header.hash()),
1545+
};
1546+
1547+
Some(ConsensusMessage {
1548+
signature,
1549+
signer_index,
1550+
on,
1551+
})
15361552
}
15371553

15381554
fn signer_index(&self) -> Option<usize> {
@@ -1696,27 +1712,14 @@ impl Worker {
16961712
return None
16971713
}
16981714
}
1699-
1700-
let prev_proposer_idx = match self.block_proposer_idx(*parent_hash) {
1701-
Some(idx) => idx,
1715+
let message = match self.recover_proposal_vote(&header_view, proposed_view, signature) {
1716+
Some(vote) => vote,
17021717
None => {
17031718
cwarn!(ENGINE, "Prev block proposer does not exist for height {}", number);
17041719
return None
17051720
}
17061721
};
17071722

1708-
let message = ConsensusMessage::new_proposal(
1709-
signature,
1710-
&*self.validators,
1711-
&header_view,
1712-
proposed_view,
1713-
prev_proposer_idx,
1714-
)
1715-
.map_err(|err| {
1716-
cwarn!(ENGINE, "Invalid proposal received: {:?}", err);
1717-
})
1718-
.ok()?;
1719-
17201723
// If the proposal's height is current height + 1 and the proposal has valid precommits,
17211724
// we should import it and increase height
17221725
if number > (self.height + 1) as u64 {

0 commit comments

Comments
 (0)