Skip to content

Commit c8cdcc6

Browse files
committed
Relplace message_info_rlp, message_hash with VoteOn::hash
1 parent f805518 commit c8cdcc6

File tree

5 files changed

+44
-43
lines changed

5 files changed

+44
-43
lines changed

core/src/consensus/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ pub use self::null_engine::NullEngine;
3131
pub use self::simple_poa::SimplePoA;
3232
pub use self::solo::Solo;
3333
pub use self::tendermint::{
34-
message_info_rlp, ConsensusMessage, Height, Step, Tendermint, TendermintParams, TimeGapParams, View, VoteOn,
35-
VoteStep,
34+
ConsensusMessage, Height, Step, Tendermint, TendermintParams, TimeGapParams, View, VoteOn, VoteStep,
3635
};
3736
pub use self::validator_set::validator_list::RoundRobinValidator;
3837
pub use self::validator_set::{DynamicValidator, ValidatorSet};

core/src/consensus/stake/actions.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,9 @@ impl Decodable for Action {
358358
#[cfg(test)]
359359
mod tests {
360360
use super::*;
361-
use ccrypto::blake256;
362361
use ckey::sign_schnorr;
363362
use client::TestBlockChainClient;
364-
use consensus::{message_info_rlp, ConsensusMessage, DynamicValidator, Step, VoteOn, VoteStep};
363+
use consensus::{ConsensusMessage, DynamicValidator, Step, VoteOn, VoteStep};
365364
use rlp::rlp_encode_and_decode_test;
366365

367366
#[test]
@@ -418,12 +417,14 @@ mod tests {
418417
step: vote_step,
419418
block_hash,
420419
};
421-
let message_for_signature =
422-
blake256(message_info_rlp(vote_step_twister(vote_step), block_hash_twister(block_hash)));
420+
let twisted = VoteOn {
421+
step: vote_step_twister(vote_step),
422+
block_hash: block_hash_twister(block_hash),
423+
};
423424
let reversed_idx = client.get_validators().len() - 1 - signer_index;
424425
let pubkey = *client.get_validators().get(reversed_idx).unwrap().pubkey();
425426
let privkey = *client.validator_keys.read().get(&pubkey).unwrap();
426-
let signature = sign_schnorr(&privkey, &message_for_signature).unwrap();
427+
let signature = sign_schnorr(&privkey, &twisted.hash()).unwrap();
427428

428429
ConsensusMessage {
429430
signature,

core/src/consensus/tendermint/message.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,12 @@ pub struct VoteOn {
316316
pub block_hash: Option<BlockHash>,
317317
}
318318

319+
impl VoteOn {
320+
pub fn hash(&self) -> H256 {
321+
blake256(&self.rlp_bytes())
322+
}
323+
}
324+
319325
/// Message transmitted between consensus participants.
320326
#[derive(Debug, PartialEq, Eq, Clone, Hash, Default, RlpDecodable, RlpEncodable)]
321327
pub struct ConsensusMessage {
@@ -368,27 +374,10 @@ impl ConsensusMessage {
368374
}
369375

370376
pub fn verify(&self, signer_public: &Public) -> Result<bool, KeyError> {
371-
let vote_info = message_info_rlp(self.on.step, self.on.block_hash);
372-
verify_schnorr(signer_public, &self.signature, &blake256(vote_info))
377+
verify_schnorr(signer_public, &self.signature, &self.on.hash())
373378
}
374379
}
375380

376-
pub fn message_info_rlp(step: VoteStep, block_hash: Option<BlockHash>) -> Bytes {
377-
let vote_on = VoteOn {
378-
step,
379-
block_hash,
380-
};
381-
vote_on.rlp_bytes().into_vec()
382-
}
383-
384-
pub fn message_hash(step: VoteStep, block_hash: H256) -> H256 {
385-
let vote_on = VoteOn {
386-
step,
387-
block_hash: Some(block_hash),
388-
};
389-
blake256(&vote_on.rlp_bytes())
390-
}
391-
392381
#[cfg(test)]
393382
mod tests {
394383
use rlp::{self, rlp_encode_and_decode_test};

core/src/consensus/tendermint/mod.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use parking_lot::RwLock;
3535
use primitives::H256;
3636

3737
use self::chain_notify::TendermintChainNotify;
38-
pub use self::message::{message_info_rlp, ConsensusMessage, VoteOn, VoteStep};
38+
pub use self::message::{ConsensusMessage, VoteOn, VoteStep};
3939
pub use self::params::{TendermintParams, TimeGapParams, TimeoutParams};
4040
pub use self::types::{Height, Step, View};
4141
use super::{stake, ValidatorSet};
@@ -129,7 +129,7 @@ mod tests {
129129
use primitives::Bytes;
130130

131131
use super::super::BitSet;
132-
use super::message::{message_info_rlp, VoteStep};
132+
use super::message::VoteStep;
133133
use crate::account_provider::AccountProvider;
134134
use crate::block::{ClosedBlock, OpenBlock};
135135
use crate::client::TestBlockChainClient;
@@ -231,8 +231,11 @@ mod tests {
231231
header.set_author(proposer);
232232
header.set_parent_hash(Default::default());
233233

234-
let vote_info = message_info_rlp(VoteStep::new(3, 0, Step::Precommit), Some(*header.parent_hash()));
235-
let signature2 = tap.get_account(&proposer, None).unwrap().sign_schnorr(&blake256(&vote_info)).unwrap();
234+
let vote_on = VoteOn {
235+
step: VoteStep::new(3, 0, Step::Precommit),
236+
block_hash: Some(*header.parent_hash()),
237+
};
238+
let signature2 = tap.get_account(&proposer, None).unwrap().sign_schnorr(&vote_on.hash()).unwrap();
236239

237240
let seal = Seal::Tendermint {
238241
prev_view: 0,
@@ -267,8 +270,11 @@ mod tests {
267270
header.set_author(proposer);
268271
header.set_parent_hash(block1_hash);
269272

270-
let vote_info = message_info_rlp(VoteStep::new(1, 0, Step::Precommit), Some(*header.parent_hash()));
271-
let signature2 = tap.get_account(&proposer, None).unwrap().sign_schnorr(&blake256(&vote_info)).unwrap();
273+
let vote_info = VoteOn {
274+
step: VoteStep::new(1, 0, Step::Precommit),
275+
block_hash: Some(*header.parent_hash()),
276+
};
277+
let signature2 = tap.get_account(&proposer, None).unwrap().sign_schnorr(&vote_info.hash()).unwrap();
272278

273279
let seal = Seal::Tendermint {
274280
prev_view: 0,
@@ -287,9 +293,9 @@ mod tests {
287293
}
288294

289295
let voter = validator3;
290-
let signature3 = tap.get_account(&voter, None).unwrap().sign_schnorr(&blake256(&vote_info)).unwrap();
296+
let signature3 = tap.get_account(&voter, None).unwrap().sign_schnorr(&vote_info.hash()).unwrap();
291297
let voter = validator0;
292-
let signature0 = tap.get_account(&voter, None).unwrap().sign_schnorr(&blake256(&vote_info)).unwrap();
298+
let signature0 = tap.get_account(&voter, None).unwrap().sign_schnorr(&vote_info.hash()).unwrap();
293299

294300
let seal = Seal::Tendermint {
295301
prev_view: 0,
@@ -304,7 +310,7 @@ mod tests {
304310
assert!(engine.verify_block_external(&header).is_ok());
305311

306312
let bad_voter = insert_and_unlock(&tap, "101");
307-
let bad_signature = tap.get_account(&bad_voter, None).unwrap().sign_schnorr(&blake256(vote_info)).unwrap();
313+
let bad_signature = tap.get_account(&bad_voter, None).unwrap().sign_schnorr(&vote_info.hash()).unwrap();
308314

309315
let seal = Seal::Tendermint {
310316
prev_view: 0,

core/src/consensus/tendermint/worker.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,9 +1120,11 @@ impl Worker {
11201120

11211121
debug_assert_eq!(Ok(self.view), TendermintSealView::new(header.seal()).consensus_view());
11221122

1123-
let vote_step = VoteStep::new(header.number() as Height, self.view, Step::Propose);
1124-
let vote_info = message_info_rlp(vote_step, Some(hash));
1125-
let signature = self.sign(blake256(&vote_info)).expect("I am proposer");
1123+
let vote_on = VoteOn {
1124+
step: VoteStep::new(header.number() as Height, self.view, Step::Propose),
1125+
block_hash: Some(hash),
1126+
};
1127+
let signature = self.sign(vote_on.hash()).expect("I am proposer");
11261128
self.votes.vote(
11271129
ConsensusMessage::new_proposal(signature, &*self.validators, header, self.view, prev_proposer_idx)
11281130
.expect("I am proposer"),
@@ -1191,8 +1193,10 @@ impl Worker {
11911193
}
11921194

11931195
let previous_block_view = TendermintSealView::new(header.seal()).previous_block_view()?;
1194-
let step = VoteStep::new(header.number() - 1, previous_block_view, Step::Precommit);
1195-
let precommit_hash = message_hash(step, *header.parent_hash());
1196+
let precommit_vote_on = VoteOn {
1197+
step: VoteStep::new(header.number() - 1, previous_block_view, Step::Precommit),
1198+
block_hash: Some(*header.parent_hash()),
1199+
};
11961200

11971201
let mut voted_validators = BitSet::new();
11981202
let grand_parent_hash = self
@@ -1202,7 +1206,7 @@ impl Worker {
12021206
.parent_hash();
12031207
for (bitset_index, signature) in seal_view.signatures()? {
12041208
let public = self.validators.get(&grand_parent_hash, bitset_index);
1205-
if !verify_schnorr(&public, &signature, &precommit_hash)? {
1209+
if !verify_schnorr(&public, &signature, &precommit_vote_on.hash())? {
12061210
let address = public_to_address(&public);
12071211
return Err(EngineError::BlockNotAuthorized(address.to_owned()).into())
12081212
}
@@ -1474,11 +1478,13 @@ impl Worker {
14741478

14751479
fn repropose_block(&mut self, block: encoded::Block) {
14761480
let header = block.decode_header();
1477-
let vote_step = VoteStep::new(header.number() as Height, self.view, Step::Propose);
1478-
let vote_info = message_info_rlp(vote_step, Some(header.hash()));
1481+
let vote_on = VoteOn {
1482+
step: VoteStep::new(header.number() as Height, self.view, Step::Propose),
1483+
block_hash: Some(header.hash()),
1484+
};
14791485
let parent_hash = header.parent_hash();
14801486
let prev_proposer_idx = self.block_proposer_idx(*parent_hash).expect("Prev block must exists");
1481-
let signature = self.sign(blake256(&vote_info)).expect("I am proposer");
1487+
let signature = self.sign(vote_on.hash()).expect("I am proposer");
14821488
self.votes.vote(
14831489
ConsensusMessage::new_proposal(signature, &*self.validators, &header, self.view, prev_proposer_idx)
14841490
.expect("I am proposer"),

0 commit comments

Comments
 (0)