Skip to content

Commit 811a495

Browse files
Fix empty cells fn.
1 parent 5e6f9c0 commit 811a495

File tree

6 files changed

+38
-27
lines changed

6 files changed

+38
-27
lines changed

proptest-regressions/data.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Seeds for failure cases proptest has generated in the past. It is
2+
# automatically read and these particular cases re-run before any
3+
# novel cases are generated.
4+
#
5+
# It is recommended to check this file in to source control so that
6+
# everyone who runs the test benefits from these saved cases.
7+
cc 8e7c4f7fc5141690d2c174ad161c48f09353b9c00371f477869b046ae509851f # shrinks to rows = 96, cols = 683
8+
cc 0bf7bc7f3be889957f0f95c40241df0c9a8e05bcb684101765c7a11f187872fa # shrinks to matrix = [[None]]

src/client.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rocksdb::{DBWithThreadMode, SingleThreaded};
3131
use crate::{
3232
data::{
3333
construct_matrix, decode_block_cid_ask_message, decode_block_cid_fact_message, empty_cells,
34-
extract_block, extract_cell, extract_links, get_matrix, matrix_cells,
34+
extract_block, extract_cell, extract_links, get_matrix, matrix_cells, non_empty_cells_len,
3535
prepare_block_cid_ask_message, prepare_block_cid_fact_message, push_matrix,
3636
},
3737
rpc::get_cells,
@@ -414,17 +414,11 @@ pub async fn run_client(
414414
vec![]
415415
});
416416

417-
log::info!(
418-
"Fetched {} cells from IPFS",
419-
ipfs_cells
420-
.iter()
421-
.fold(0usize, |sum, val| sum + val.iter().flatten().count())
422-
);
423-
424417
let requested_cells = empty_cells(&ipfs_cells, block.max_cols, block.max_rows);
425418

426419
log::info!(
427-
"Requested {} cells from the full node",
420+
"Got {} cells from IPFS, requesting {} from full node",
421+
non_empty_cells_len(&ipfs_cells),
428422
requested_cells.len()
429423
);
430424

src/data.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,24 @@ pub fn matrix_cells(rows: u16, cols: u16) -> impl Iterator<Item = (usize, usize)
7373
}
7474

7575
pub fn empty_cells(matrix: &Matrix, cols: u16, rows: u16) -> Vec<(usize, usize)> {
76+
// TODO: Optimal solution is to use proper Matrix abstraction to derive empty cells
7677
matrix_cells(rows, cols)
77-
.filter(|(row, col)| matrix.get(*col).and_then(|col| col.get(*row)).is_none())
78+
.filter(|(row, col)| {
79+
matrix
80+
.get(*col)
81+
.and_then(|col| col.get(*row))
82+
.map(|cell| cell.is_none())
83+
.unwrap_or(true)
84+
})
7885
.collect::<Vec<(usize, usize)>>()
7986
}
8087

88+
pub fn non_empty_cells_len(matrix: &Matrix) -> usize {
89+
matrix
90+
.iter()
91+
.fold(0usize, |sum, val| sum + val.iter().flatten().count())
92+
}
93+
8194
fn get_cell(ipfs: &Ipfs<DefaultParams>, cid: &Cid) -> Result<Option<Cell>> {
8295
ipfs.get(cid)
8396
.and_then(|result| result.ipld())
@@ -451,40 +464,35 @@ mod tests {
451464
}
452465

453466
fn matrix_strategy() -> impl Strategy<Value = Vec<Vec<Option<Vec<u8>>>>> {
467+
let rows_len = (1..64usize).next().unwrap();
454468
collection::vec(
455-
collection::vec(any::<Option<Vec<u8>>>(), collection::size_range(1..64)),
469+
collection::vec(any::<Option<Vec<u8>>>(), rows_len),
456470
collection::size_range(1..64),
457471
)
458472
}
459473

460-
fn non_empty_cells(matrix: &Matrix, cols: u16, rows: u16) -> Vec<(usize, usize)> {
461-
matrix_cells(rows as usize, cols as usize)
462-
.filter(|(row, col)| matrix.get(*col).and_then(|col| col.get(*row)).is_some())
463-
.collect::<Vec<(usize, usize)>>()
464-
}
465-
466474
proptest! {
467475
#[test]
468-
fn matrix_cells_length_is_correct(rows in 0usize..1024, cols in 0usize..1024) {
469-
prop_assert_eq!(matrix_cells(rows, cols).count(), rows * cols);
476+
fn matrix_cells_length_is_correct(rows in 0u16..1024, cols in 0u16..1024) {
477+
prop_assert_eq!(matrix_cells(rows, cols).count(), rows as usize * cols as usize);
470478
}
471479

472480
#[test]
473481
fn empty_cells_length_is_correct(matrix in matrix_strategy()) {
474-
let cols = matrix.len() as u16;
475-
let rows = matrix[0].len() as u16;
476-
let empty_cells_len = empty_cells(&matrix, cols, rows).len() as u16;
477-
let non_empty_cells_len = non_empty_cells(&matrix, cols, rows).len() as u16;
482+
let cols = matrix.len() ;
483+
let rows = matrix[0].len() ;
484+
let empty_cells_len = empty_cells(&matrix, cols as u16, rows as u16).len();
485+
let non_empty_cells_len = non_empty_cells_len(&matrix);
478486

479-
prop_assert_eq!(empty_cells_len + non_empty_cells_len, rows * cols);
487+
prop_assert_eq!(empty_cells_len + non_empty_cells_len, (rows * cols) as usize);
480488
}
481489
}
482490

483491
#[test_case(1, 1 => vec![(0, 0)] ; "one cell")]
484492
#[test_case(4, 1 => vec![(0, 0), (1, 0), (2,0), (3,0)] ; "four rows, one column")]
485493
#[test_case(1, 4 => vec![(0, 0), (0, 1), (0,2), (0,3)] ; "four columns, one row")]
486-
#[test_case(2, 2 => vec![(0, 0), (0, 1), (1,0), (1,1)] ; "square matrix")]
487-
fn test_matrix_cells(rows: usize, cols: usize) -> Vec<(usize, usize)> {
494+
#[test_case(2, 2 => vec![(0, 0), (1, 0), (0,1), (1,1)] ; "square matrix")]
495+
fn test_matrix_cells(rows: u16, cols: u16) -> Vec<(usize, usize)> {
488496
matrix_cells(rows, cols).collect::<Vec<(usize, usize)>>()
489497
}
490498

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ pub async fn do_main() -> Result<()> {
185185

186186
let begin = SystemTime::now();
187187

188+
// TODO: Setting max rows * 2 to match extended matrix dimensions
188189
let max_rows = header.extrinsics_root.rows * 2;
189190
let max_cols = header.extrinsics_root.cols;
190191
if max_cols < 3 {

src/sync.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub async fn sync_block_headers(
8383

8484
let begin = SystemTime::now();
8585

86+
// TODO: Setting max rows * 2 to match extended matrix dimensions
8687
let max_rows = block_body.header.extrinsics_root.rows * 2;
8788
let max_cols = block_body.header.extrinsics_root.cols;
8889
let commitment = block_body.header.extrinsics_root.commitment;

src/types.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
extern crate ipfs_embed;
22

3-
use codec::Decode;
43
use ipfs_embed::{Block as IpfsBlock, Cid, DefaultParams, Multiaddr, PeerId, StreamId};
54
use serde::{Deserialize, Deserializer, Serialize};
65

0 commit comments

Comments
 (0)