Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions apps/fortuna/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,87 @@ mod test {
run_hash_chain_test([0u8; 32], 100, 55);
}
}

#[cfg(test)]
mod hash_chain_state_test {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason to have these in a separate mod?

use {
crate::state::{HashChainState, PebbleHashChain},
anyhow::Result,
};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add some tests for overlapping PebbleHashChains.

#[test]
fn test_from_chain_at_offset() {
let chain = PebbleHashChain::new([0u8; 32], 10, 1);
let hash_chain_state = HashChainState::from_chain_at_offset(5, chain);

assert_eq!(hash_chain_state.offsets.len(), 1);
assert_eq!(hash_chain_state.hash_chains.len(), 1);
assert_eq!(hash_chain_state.offsets[0], 5);
}

#[test]
fn test_reveal_valid_sequence() -> Result<()> {
let chain = PebbleHashChain::new([0u8; 32], 10, 1);
let expected_hash = chain.reveal_ith(3)?;

let hash_chain_state = HashChainState::from_chain_at_offset(5, chain);
let result = hash_chain_state.reveal(8)?;

assert_eq!(result, expected_hash);
Ok(())
}

#[test]
fn test_reveal_sequence_too_small() {
let chain = PebbleHashChain::new([0u8; 32], 10, 1);
let hash_chain_state = HashChainState::from_chain_at_offset(5, chain);

let result = hash_chain_state.reveal(4);
assert!(result.is_err());
}

#[test]
fn test_reveal_sequence_too_large() {
let chain = PebbleHashChain::new([0u8; 32], 10, 1);
let hash_chain_state = HashChainState::from_chain_at_offset(5, chain);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not related to this PR but we should make this from_chain_at_offset function only for tests.


let result = hash_chain_state.reveal(15);
assert!(result.is_err());
}

#[test]
fn test_multiple_hash_chains() -> Result<()> {
let chain1 = PebbleHashChain::new([0u8; 32], 10, 1);
let expected_hash1 = chain1.reveal_ith(3)?;

let chain2 = PebbleHashChain::new([1u8; 32], 10, 1);
let expected_hash2 = chain2.reveal_ith(3)?;

let mut hash_chain_state = HashChainState::from_chain_at_offset(5, chain1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's just construct the hash_chain_state directly from fields for better readability

hash_chain_state.offsets.push(20);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we also test this 20 boundary?

hash_chain_state.hash_chains.push(chain2);

let result1 = hash_chain_state.reveal(8)?;
assert_eq!(result1, expected_hash1);

let result2 = hash_chain_state.reveal(23)?;
assert_eq!(result2, expected_hash2);

let result3 = hash_chain_state.reveal(15);
assert!(result3.is_err());

Ok(())
}

#[test]
fn test_reveal_at_offset() -> Result<()> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very similar to test_reveal_valid_sequence. not adding a lot of value

let chain = PebbleHashChain::new([0u8; 32], 10, 1);
let expected_hash = chain.reveal_ith(0)?;

let hash_chain_state = HashChainState::from_chain_at_offset(5, chain);
let result = hash_chain_state.reveal(5)?;

assert_eq!(result, expected_hash);
Ok(())
}
}
Loading