diff --git a/src/entry.rs b/src/entry.rs index 14bc6e8c71..36db0d8417 100644 --- a/src/entry.rs +++ b/src/entry.rs @@ -87,9 +87,14 @@ pub fn next_tick(start_hash: &Hash, num_hashes: u64) -> Entry { #[cfg(test)] mod tests { use super::*; + use hash::hash; + use signature::{KeyPair, KeyPairUtil}; + use transaction::Transaction; + use event::Event; + use entry::create_entry; #[test] - fn test_event_verify() { + fn test_entry_verify() { let zero = Hash::default(); let one = hash(&zero); assert!(Entry::new_tick(0, &zero).verify(&zero)); // base case @@ -98,6 +103,23 @@ mod tests { assert!(!next_tick(&zero, 1).verify(&one)); // inductive step, bad } + #[test] + fn test_event_reorder_attack() { + let zero = Hash::default(); + + // First, verify entries + let keypair = KeyPair::new(); + let tr0 = Event::Transaction(Transaction::new(&keypair, keypair.pubkey(), 0, zero)); + let tr1 = Event::Transaction(Transaction::new(&keypair, keypair.pubkey(), 1, zero)); + let mut e0 = create_entry(&zero, 0, vec![tr0.clone(), tr1.clone()]); + assert!(e0.verify(&zero)); + + // Next, swap two events and ensure verification fails. + e0.events[0] = tr1; // <-- attack + e0.events[1] = tr0; + assert!(!e0.verify(&zero)); + } + #[test] fn test_next_tick() { let zero = Hash::default(); diff --git a/src/log.rs b/src/log.rs index 9632714639..058e5eaa25 100644 --- a/src/log.rs +++ b/src/log.rs @@ -14,8 +14,7 @@ /// was generated by the fastest processor at the time the entry was logged. use hash::Hash; -use entry::{create_entry, next_tick, Entry}; -use event::Event; +use entry::{next_tick, Entry}; use rayon::prelude::*; /// Verifies the hashes and counts of a slice of events are all consistent. @@ -25,10 +24,6 @@ pub fn verify_slice(entries: &[Entry], start_hash: &Hash) -> bool { event_pairs.all(|(x0, x1)| x1.verify(&x0.id)) } -pub fn create_entries(start_hash: &Hash, events: Vec) -> Vec { - vec![create_entry(start_hash, 0, events)] -} - /// Create a vector of Ticks of length 'len' from 'start_hash' hash and 'num_hashes'. pub fn next_ticks(start_hash: &Hash, num_hashes: u64, len: usize) -> Vec { let mut id = *start_hash; @@ -59,27 +54,6 @@ mod tests { bad_ticks[1].id = one; assert!(!verify_slice(&bad_ticks, &zero)); // inductive step, bad } - - // TODO: This is no longer relevant. Instead, test for reordered ticks. - //#[test] - //fn test_reorder_attack() { - // let zero = Hash::default(); - - // // First, verify entries - // let keypair = KeyPair::new(); - // let tr0 = Transaction::new(&keypair, keypair.pubkey(), 0, zero); - // let tr1 = Transaction::new(&keypair, keypair.pubkey(), 1, zero); - // let events = vec![Event::Transaction(tr0), Event::Transaction(tr1)]; - // let mut entries = create_entries(&zero, events); - // assert!(verify_slice(&entries, &zero)); - - // // Next, swap two events and ensure verification fails. - // let event0 = entries[0].event.clone(); - // let event1 = entries[1].event.clone(); - // entries[0].event = event1; - // entries[1].event = event0; - // assert!(!verify_slice(&entries, &zero)); - //} } #[cfg(all(feature = "unstable", test))]