Skip to content

Commit cf7dfef

Browse files
committed
Implement priority message handling
1 parent bcd84ac commit cf7dfef

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

core/src/consensus/tendermint/network.rs

+18
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,24 @@ impl NetworkExtension<Event> for TendermintExtension {
277277
}
278278
}
279279
}
280+
Ok(TendermintMessage::PriorityMessage {
281+
height,
282+
view,
283+
signer_idx,
284+
message,
285+
}) => {
286+
let (result, receiver) = crossbeam::bounded(1);
287+
self.inner
288+
.send(worker::Event::Priority {
289+
height,
290+
view,
291+
signer_idx,
292+
message,
293+
result,
294+
})
295+
.unwrap();
296+
if let Some(c) = receiver.recv().unwrap() {}
297+
}
280298
Ok(TendermintMessage::ProposalBlock {
281299
signature,
282300
view,

core/src/consensus/tendermint/worker.rs

+56-1
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,24 @@ use std::sync::{Arc, Weak};
2121
use std::thread::{Builder, JoinHandle};
2222
use std::time::{Duration, SystemTime, UNIX_EPOCH};
2323

24+
use ccrypto::sha256;
2425
use ckey::{public_to_address, verify_schnorr, Address, SchnorrSignature};
2526
use cnetwork::{EventSender, NodeId};
2627
use crossbeam_channel as crossbeam;
2728
use ctypes::transaction::{Action, Transaction};
2829
use ctypes::util::unexpected::Mismatch;
2930
use ctypes::{BlockHash, BlockNumber, Header};
31+
use parking_lot::RwLock;
3032
use primitives::{u256_from_u128, Bytes, U256};
3133
use rlp::{Encodable, Rlp};
34+
use vrf::openssl::{CipherSuite, ECVRF};
3235

3336
use super::super::BitSet;
3437
use super::backup::{backup, restore, BackupView};
3538
use super::message::*;
3639
use super::network;
3740
use super::params::TimeGapParams;
41+
use super::sortition_info_collector::SortitionInfoCollector;
3842
use super::stake::CUSTOM_ACTION_HANDLER_ID;
3943
use super::types::{Height, Proposal, Step, TendermintSealView, TendermintState, TwoThirdsMajority, View};
4044
use super::vote_collector::{DoubleVote, VoteCollector};
@@ -47,7 +51,7 @@ use crate::block::*;
4751
use crate::client::ConsensusClient;
4852
use crate::consensus::signer::EngineSigner;
4953
use crate::consensus::validator_set::{DynamicValidator, ValidatorSet};
50-
use crate::consensus::{EngineError, Seal};
54+
use crate::consensus::{EngineError, Seal, VRFSortition};
5155
use crate::encoded;
5256
use crate::error::{BlockError, Error};
5357
use crate::transaction::{SignedTransaction, UnverifiedTransaction};
@@ -94,6 +98,9 @@ struct Worker {
9498
validators: Arc<DynamicValidator>,
9599
/// Channel to the network extension, must be set later.
96100
extension: EventSender<network::Event>,
101+
/// VRF instance to calculate VRF hash.
102+
vrf_inst: Arc<RwLock<ECVRF>>,
103+
sortition_infos: SortitionInfoCollector,
97104
time_gap_params: TimeGapParams,
98105
timeout_token_nonce: usize,
99106
vote_regression_checker: VoteRegressionChecker,
@@ -137,6 +144,13 @@ pub enum Event {
137144
address: Address,
138145
},
139146
Restore(crossbeam::Sender<()>),
147+
Priority {
148+
height: Height,
149+
view: View,
150+
signer_idx: usize,
151+
message: PriorityMessage,
152+
result: crossbeam::Sender<Option<Arc<dyn ConsensusClient>>>,
153+
},
140154
ProposalBlock {
141155
signature: SchnorrSignature,
142156
view: View,
@@ -194,10 +208,12 @@ impl Worker {
194208
finalized_view_of_current_block: None,
195209
validators,
196210
extension,
211+
sortition_infos: Default::default(),
197212
votes_received: MutTrigger::new(BitSet::new()),
198213
time_gap_params,
199214
timeout_token_nonce: ENGINE_TIMEOUT_TOKEN_NONCE_BASE,
200215
vote_regression_checker: VoteRegressionChecker::new(),
216+
vrf_inst: Arc::new(RwLock::new(ECVRF::from_suite(CipherSuite::SECP256K1_SHA256_SVDW).unwrap())),
201217
}
202218
}
203219

@@ -312,6 +328,16 @@ impl Worker {
312328
inner.restore();
313329
result.send(()).unwrap();
314330
}
331+
Ok(Event::Priority {
332+
height,
333+
view,
334+
signer_idx,
335+
message,
336+
result,
337+
}) => {
338+
let client = inner.on_priority_message(height, view, signer_idx, message);
339+
result.send(client).unwrap();
340+
}
315341
Ok(Event::ProposalBlock {
316342
signature,
317343
view,
@@ -1728,6 +1754,35 @@ impl Worker {
17281754
result.send(message.rlp_bytes().into_vec()).unwrap();
17291755
}
17301756

1757+
fn on_priority_message(
1758+
&mut self,
1759+
height: Height,
1760+
view: View,
1761+
signer_idx: usize,
1762+
message: PriorityMessage,
1763+
) -> Option<Arc<dyn ConsensusClient>> {
1764+
let c = self.client.upgrade()?;
1765+
c.block_hash(&BlockId::Number(height - 1)).and_then(|parent_hash| {
1766+
let signer_public = self.validators.get(&parent_hash, signer_idx);
1767+
//FIXME: Do not recalculate seed values with heavy VRF operations.
1768+
//FIXME: store block_seed into each block.
1769+
let concatenated = [height.to_be_bytes(), view.to_be_bytes()].concat();
1770+
let seed = self.signer.vrf_hash(sha256(concatenated), &mut self.vrf_inst.write());
1771+
let voting_power = 50;
1772+
//FIXME: store sortition scheme.
1773+
let sortition_scheme = VRFSortition {
1774+
total_power: 100,
1775+
expectation: 7.0,
1776+
vrf_inst: Arc::clone(&self.vrf_inst),
1777+
};
1778+
if let Ok(true) = message.verify(&signer_public, voting_power, &sortition_scheme) {
1779+
Some(c)
1780+
} else {
1781+
None
1782+
}
1783+
})
1784+
}
1785+
17311786
fn on_proposal_message(
17321787
&mut self,
17331788
signature: SchnorrSignature,

0 commit comments

Comments
 (0)