-
Notifications
You must be signed in to change notification settings - Fork 91
AV-50 (AV-77) - Fetch missing cells from DHT #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
263c77a
to
bf69b72
Compare
088f532
to
3127ee7
Compare
Codecov ReportBase: 58.19% // Head: 59.54% // Increases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## main #13 +/- ##
==========================================
+ Coverage 58.19% 59.54% +1.35%
==========================================
Files 39 39
Lines 5236 5411 +175
==========================================
+ Hits 3047 3222 +175
Misses 2189 2189
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
483be54
to
0c7bea5
Compare
kate/recovery/src/commitments.rs
Outdated
if commitments.len() % config::COMMITMENT_SIZE > 0 { | ||
return Err(Error::InvalidData(DataError::BadCommitmentsData)); | ||
} | ||
|
||
let app_rows = com::app_specific_rows(index, dimension, app_id); | ||
if <u32>::try_from(commitments.len() / config::COMMITMENT_SIZE)? != dimensions.extended_rows() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Always cast to the biggest type:
if <u32>::try_from(commitments.len() / config::COMMITMENT_SIZE)? != dimensions.extended_rows() { | |
if (commitments.len() / config::COMMITMENT_SIZE) != dimensions.extended_rows() as usize { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't that platform dependent and wouldn't as
panic if 32-bit is not supported (should we even consider this case?). If we assume that smallest possible usize is u32 then 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is platform dependent. It could be u32
(like WASM
) or u64
(like current micros).
See https://doc.rust-lang.org/std/primitive.usize.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to
if (commitments.len() / config::COMMITMENT_SIZE) != dimensions.extended_rows().try_into()?
to cast to bigger type and avoid panics. Wdyt?
kate/recovery/src/commitments.rs
Outdated
|
||
if !all_rows_present { | ||
return Err(Error::InvalidData(DataError::RowAndCommitmentsMismatch)); | ||
if <u32>::try_from(rows.len())? != dimensions.extended_rows() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
if <u32>::try_from(rows.len())? != dimensions.extended_rows() { | |
if rows.len() != dimensions.extended_rows() as usize { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, used try_into
for conversion.
kate/recovery/src/commitments.rs
Outdated
.collect::<Result<Vec<(u32, bool)>, Error>>()?; | ||
|
||
let verified = verifications | ||
.iter() | ||
.filter(|(_, is_equal)| *is_equal) | ||
.map(|&(i, _)| i) | ||
.collect::<Vec<u32>>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
verifications
does not need to be consolidated into a Vec
. BTW you could merge the latest .map().filter().map()
into a single filter_map()
call.
.collect::<Result<Vec<(u32, bool)>, Error>>()?; | |
let verified = verifications | |
.iter() | |
.filter(|(_, is_equal)| *is_equal) | |
.map(|&(i, _)| i) | |
.collect::<Vec<u32>>(); | |
.filter(|(_, is_equal)| *is_equal) | |
.map(|&(i, _)| i) | |
.collect::<Vec<u32>>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was interesting to change, removed almost 50% of code, after finding transpose
fn on result :) So now it looks like:
let verified = commitments
.chunks_exact(config::COMMITMENT_SIZE)
.zip(rows.iter())
.zip(0u32..)
.filter(|(.., index)| app_rows.contains(index))
.filter_map(|((commitment, row), index)| {
try_into_scalars(row.as_ref()?)
.map(|scalars| Evaluations::from_vec_and_domain(scalars, domain).interpolate())
.and_then(|polynomial| prover_key.commit(&polynomial).map_err(From::from))
.map(|result| (result.to_bytes() == commitment).then_some(index))
.transpose()
})
.collect::<Result<Vec<u32>, Error>>()?;
app_rows.retain(|row| !verified.contains(row));
Ok((verified, app_rows))
wdyt?
kate/recovery/src/commitments.rs
Outdated
let missing = app_rows | ||
.into_iter() | ||
.filter(|&row| !verified.contains(&row)) | ||
.collect::<Vec<u32>>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove new allocations:
let missing = app_rows | |
.into_iter() | |
.filter(|&row| !verified.contains(&row)) | |
.collect::<Vec<u32>>(); | |
app_rows.retains(|&row| !verified.contains(&row)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MiguelDD1 This might be an issue in regards to retain. Don't know if the subsequent change in the retain implementation changed anything, but it might be prudent to consider the perf impact mentioned in the issue. Wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have updated code @sh3ll3x3c , it seems that issue is solved, and vec should be smaller than 512, so lets revert if we notice anything here.
pub fn rows(cells: &[Cell]) -> Vec<(u32, Vec<u8>)> { | ||
let mut sorted_cells = cells.to_vec(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If 1st step is cells.to_vec()
, let's simplify the interface:
pub fn rows(cells: &[Cell]) -> Vec<(u32, Vec<u8>)> { | |
let mut sorted_cells = cells.to_vec(); | |
pub fn rows(mut cells: Vec<Cell>) -> Vec<(u32, Vec<u8>)> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this mutates argument just for the sake of sorting (and we cannot enforce sorted vector through type I guess), its kinda awkward to have interface like that. Wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MiguelDD1 doesn't this change make the interface more ambiguous in terms whether the data is actually changing or not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about this one, by marking it as mut
we allow caller to clone if needed (or to avoid cloning otherwise), but it introduces implicit coupling. If we remove sorting as some point, we need to change interface of fn. Also, its not clear why cells are mutated. I know its not that important here, but wanted to here more arguments.
kate/recovery/src/data.rs
Outdated
sorted_cells.sort_by(|a, b| { | ||
let by_row = a.position.row.cmp(&b.position.row); | ||
let by_col = a.position.col.cmp(&b.position.col); | ||
by_row.then(by_col) | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is not the default implementation if you add #[derive(PartialOrd, Ord)]
to Position
?
(see Ord Derivable:
sorted_cells.sort_by(|a, b| { | |
let by_row = a.position.row.cmp(&b.position.row); | |
let by_col = a.position.col.cmp(&b.position.col); | |
by_row.then(by_col) | |
}); | |
sorted_cells.sort_by_key(|cell| cell.position); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well it depends whats affects sort more, rows or columns. In this case its row index. Having default implementation would maybe make that implicit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, you're right.
What do you think about next simplification?
sorted_cells.sort_by(|a, b| (a.position.row, a.position.col).cmp(&(b.position.row,b.position.col))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is cool, its intuitive to assume that first element of pair has precedence. Ive updated the code.
kate/recovery/src/index.rs
Outdated
/// Size is nuber of the data cells in the matrix. | ||
/// Index is list of pairs (app_id, start_index), | ||
/// where start index is index of first cell for that application. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move comments (Size
and Index
) into each member.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
40b34cc
to
179877d
Compare
This PR supports fetch of DHT cells on light client. Main changes are:
verify_equality
now returns pair of verified and missing row indexesrows
function is added todata
mod, to be able to construct data row out of a fetched cells