Skip to content

Commit

Permalink
Add a unit test to priority message
Browse files Browse the repository at this point in the history
  • Loading branch information
HoOngEe committed Nov 7, 2019
1 parent 0871a6f commit c85cdf9
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions core/src/consensus/sortition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,41 @@ impl PriorityMessage {
&& self.info.verify_vrf_hash(signer_public, &self.seed, Arc::clone(&sortition_scheme.vrf_inst))?)
}
}

#[cfg(test)]
mod priority_message_tests {
use ccrypto::sha256;
use ckey::{KeyPair, Private};
use parking_lot::RwLock;
use vrf::openssl::{CipherSuite, ECVRF};

use super::*;
#[test]
fn check_priority_message_verification() {
let priv_key: Private = sha256("secret_key").into();
let pub_key = *KeyPair::from_private(priv_key).expect("Valid private key").public();

let wrong_priv_key: Private = sha256("wrong_secret_key2").into();
let wrong_pub_key = *KeyPair::from_private(wrong_priv_key).expect("Valid private key").public();

let seed = sha256("seed");
let ec_vrf = ECVRF::from_suite(CipherSuite::SECP256K1_SHA256_SVDW).unwrap();
let ec_vrf = Arc::new(RwLock::new(ec_vrf));
let sortition_scheme = VRFSortition {
total_power: 100,
expectation: 71.85,
vrf_inst: ec_vrf,
};
let voting_power = 50;
let priority_info =
sortition_scheme.create_highest_priority_info(seed, priv_key, voting_power).unwrap().unwrap();

let priority_message = PriorityMessage {
seed,
info: priority_info,
signer_index: 0,
};
assert!(priority_message.verify(&pub_key, voting_power, &sortition_scheme).unwrap());
assert!(priority_message.verify(&wrong_pub_key, voting_power, &sortition_scheme).is_err());
}
}

0 comments on commit c85cdf9

Please sign in to comment.