Skip to content

Commit bffb073

Browse files
committed
Implement priority message
1 parent c7d42a1 commit bffb073

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

core/src/consensus/sortition/mod.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,36 @@
1717
mod binom_cdf;
1818
mod lot;
1919
mod vrf_sortition;
20+
21+
use ckey::Public;
22+
use primitives::H256;
23+
use vrf::openssl::Error as VrfError;
24+
25+
use self::vrf_sortition::Priority;
26+
use self::vrf_sortition::VRFSortition;
27+
28+
pub struct PriorityMessage {
29+
pub seed: H256,
30+
pub priority: Priority,
31+
pub vrf_hash: Vec<u8>,
32+
pub vrf_proof: Vec<u8>,
33+
pub sub_user_index: u64,
34+
pub signer_index: usize,
35+
}
36+
37+
impl PriorityMessage {
38+
pub fn signer_index(&self) -> usize {
39+
self.signer_index
40+
}
41+
42+
pub fn verify(
43+
&self,
44+
signer_public: &Public,
45+
voting_power: u64,
46+
sortition_scheme: &VRFSortition,
47+
) -> Result<bool, VrfError> {
48+
Ok(sortition_scheme.verify_sub_user_idx(voting_power, &self.vrf_hash, self.sub_user_index)
49+
&& sortition_scheme.verify_priority(&self.vrf_hash, self.sub_user_index, &self.priority)
50+
&& sortition_scheme.verify_vrf_hash(signer_public, &self.vrf_proof, &self.seed, &self.vrf_hash)?)
51+
}
52+
}

core/src/consensus/sortition/vrf_sortition.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,7 @@ impl VRFSortition {
8080
Ok(verified_hash == vrf_hash)
8181
}
8282

83-
pub fn verify_sub_user_idx(
84-
&self,
85-
voting_power: u64,
86-
vrf_hash: &[u8],
87-
sub_user_idx: u64,
88-
) -> bool {
83+
pub fn verify_sub_user_idx(&self, voting_power: u64, vrf_hash: &[u8], sub_user_idx: u64) -> bool {
8984
let j = lot(voting_power, self.total_power, self.expectaion, vrf_hash);
9085
sub_user_idx < j
9186
}

core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ extern crate snap;
5555
extern crate statrs;
5656
extern crate table;
5757
extern crate util_error;
58+
extern crate vrf;
5859
#[macro_use]
5960
extern crate log;
6061
extern crate core;

0 commit comments

Comments
 (0)