Skip to content

Commit

Permalink
fix: fixed select_indices err
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkingLee committed Dec 16, 2023
1 parent dfb1133 commit c82f4b6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 46 deletions.
14 changes: 7 additions & 7 deletions crates/pallet-farmers-fortune/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ fn claim_reward_should_work() {
#[test]
fn claim_reward_works_for_different_farmer_ids() {
new_test_ext().execute_with(|| {
System::set_block_number(6);
<frame_system::BlockHash<Runtime>>::insert(5, H256::from(BLOCK_HASH1));
<frame_system::BlockHash<Runtime>>::insert(3, H256::from(BLOCK_HASH1));
System::set_block_number(16);
<frame_system::BlockHash<Runtime>>::insert(15, H256::from(BLOCK_HASH1));
<frame_system::BlockHash<Runtime>>::insert(13, H256::from(BLOCK_HASH1));

let segs = get_mock_row(&BLS_SCALAR11, &BLS_SCALAR12, 0, &PROOF_11, &PROOF_12, 16);
let commit = KZGCommitment::try_from(COMMIT1).unwrap();

let pre_cell = PreCell::new(PiecePosition::Row(0), segs[0].clone());
let piece_metadata = PieceMetadata::new(3, PiecePosition::Row(0));
let piece_metadata = PieceMetadata::new(13, PiecePosition::Row(0));

let left_cell_metadata = CellMetadata::new(piece_metadata.clone(), 0);
let right_cell_metadata = CellMetadata::new(piece_metadata, 1);
Expand All @@ -120,11 +120,11 @@ fn claim_reward_works_for_different_farmer_ids() {

let wrong_commit = KZGCommitment::try_from(COMMIT2).unwrap();

insert_mock_commitment(5, Position { x: 0, y: 0 }, wrong_commit);
insert_mock_commitment(15, Position { x: 0, y: 0 }, wrong_commit);

insert_mock_commitment(3, Position { x: 0, y: 0 }, commit);
insert_mock_commitment(13, Position { x: 0, y: 0 }, commit);

insert_mock_commitment(3, Position { x: 1, y: 0 }, commit);
insert_mock_commitment(13, Position { x: 1, y: 0 }, commit);

assert_noop!(
FarmersFortune::claim(
Expand Down
1 change: 1 addition & 0 deletions crates/pallet-melo-store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
mod mock;
#[cfg(test)]
mod tests;

pub mod weights;
Expand Down
2 changes: 0 additions & 2 deletions crates/pallet-melo-store/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#![cfg(test)]

use super::*;
use crate as pallet_melo_store;
use crate::mock::*;
Expand Down
11 changes: 0 additions & 11 deletions crates/proof-of-space/src/solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,17 +365,6 @@ mod tests {
assert!(!result);
}

#[test]
fn test_select_indices() {
let farmer_id = FarmerId::default();
let block_hash: [u8; 32] = [0b00010001; 32];
let end = 100;
let n = 1;

let indices = Solution::<H256, u32>::select_indices(&farmer_id, &block_hash.into(), end, n);
assert!(!indices.is_empty());
}

#[test]
fn test_find_solutions_with_z() {
let mut db = MockDb::new();
Expand Down
64 changes: 38 additions & 26 deletions crates/proof-of-space/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,42 @@ pub fn fold_hash(hash: &[u8]) -> u32 {
/// - Each pair of bytes is converted to a `u16` and then XORed with the accumulator.
/// - If the number of bytes in the slice is odd, the last byte is XORed with 0.
pub fn hash_to_u16_xor(hash: &[u8]) -> u16 {
hash.chunks(2).fold(0u16, |acc, chunk| {
acc ^ u16::from_be_bytes([chunk[0], chunk.get(1).cloned().unwrap_or(0)])
})
hash.chunks(2).fold(0u16, |acc, chunk| {
acc ^ u16::from_be_bytes([chunk[0], chunk.get(1).cloned().unwrap_or(0)])
})
}

/// Selects indices from a hash where a specified number of consecutive bits are 1.
/// Selects indices from a hash where a specified number of consecutive bits are 1,
/// with the `end` parameter indicating the bit position in binary.
///
/// Parameters:
/// * `hash`: A reference to a 32-byte hash array.
/// * `start`: The starting index for the selection.
/// * `end`: The ending index for the selection.
/// * `end`: The bit position in binary to check in each byte.
/// * `n`: The number of consecutive 1 bits required.
///
/// Returns:
/// A vector of indices where each index meets the specified bit criteria.
pub fn select_indices(hash: &[u8; 32], start: usize, end: usize, n: usize) -> Vec<u32> {
(start..end)
.flat_map(|i| (0..8).map(move |bit| (i, bit))) // Generate index-bit pairs.
.filter_map(|(i, bit)| {
// Filter pairs where `n` consecutive bits are 1.
assert!(end <= 7, "end must be a bit position within a byte (0-7)");

(start..32)
.filter_map(|i| {
// Check if `n` consecutive bits starting from the `end` bit are 1.
if (0..n).all(|offset| {
let next_bit_index = (bit + offset) % 8;
let next_byte_index = (i + (bit + offset) / 8) % 32;
// Calculate the bit position and byte index for the offset.
let bit_position = (end + offset) % 8;
let byte_index = i + (end + offset) / 8;

// Prevent index out of bounds.
if byte_index >= 32 {
return false
}

hash[next_byte_index] & (1 << next_bit_index) != 0
// Check if the bit at the position is 1.
hash[byte_index] & (1 << bit_position) != 0
}) {
Some((i * 8 + bit) as u32)
Some(i as u32)
} else {
None
}
Expand Down Expand Up @@ -209,36 +218,39 @@ mod tests {
#[test]
fn test_select_indices() {
let hash = [0b1111_1111u8; 32];
let indices = select_indices(&hash, 0, 32, 3);
assert_eq!(indices.len(), 256);
// Check the 1st bit position in each byte
let indices = select_indices(&hash, 0, 1, 3);
// Every byte should match, since it's full of 1s
assert_eq!(indices.len(), 32);
}

#[test]
fn test_select_indices_non_uniform() {
let hash = [0b1010_1010u8; 32];
let indices = select_indices(&hash, 0, 32, 2);
// Check the 1st bit position in each byte
let indices = select_indices(&hash, 0, 1, 2);
// No match as there are no 2 consecutive 1s starting from 1st bit position
assert!(indices.is_empty());
}

#[test]
fn test_select_indices_no_match() {
let hash = [0b0101_0101u8; 32];
let indices = select_indices(&hash, 0, 32, 3);
// Check the 1st bit position in each byte
let indices = select_indices(&hash, 0, 1, 3);
// No match as there are no 3 consecutive 1s starting from 1st bit position
assert!(indices.is_empty());
}

#[test]
fn test_select_indices_partial_bytes() {
let hash = [0b1111_0000u8; 32];
let indices = select_indices(&hash, 2, 30, 4);
assert_eq!(
indices,
vec![
20, 28, 36, 44, 52, 60, 68, 76, 84, 92, 100, 108, 116, 124, 132, 140, 148, 156,
164, 172, 180, 188, 196, 204, 212, 220, 228, 236
]
);
}
// Check the 1st bit position in bytes from 2 to 29
let indices = select_indices(&hash, 2, 1, 4);
// No matches should be found
assert!(indices.is_empty());
}

#[test]
fn test_is_index_valid_short_hash() {
let hash = [0b1111_1111u8; 16]; // Shorter than 32 bytes
Expand Down

0 comments on commit c82f4b6

Please sign in to comment.