Skip to content

Commit

Permalink
Make errors more informative
Browse files Browse the repository at this point in the history
  • Loading branch information
ii-cruz committed Nov 28, 2024
1 parent f151505 commit 8b9e5c8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
4 changes: 2 additions & 2 deletions zkp-component/src/proof_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ pub(crate) fn get_proof_macro_blocks(
let new_block = blockchain
.read()
.get_block_at(block_number, true)
.map_err(|_| Error::InvalidBlock)?;
.map_err(|_| Error::UnknownBlock(block_number))?;

if !new_block.is_election() {
return Err(Error::InvalidBlock);
return Err(Error::NotElectionBlock);
}
new_block.unwrap_macro()
};
Expand Down
10 changes: 8 additions & 2 deletions zkp-component/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,14 @@ pub enum Error {
#[error("Nano Zkp Error: {0}")]
NanoZKP(#[from] NanoZKPError),

#[error("Proof's blocks are not valid")]
InvalidBlock,
#[error("Proof's blocks is not an election block")]
NotElectionBlock,

#[error("Proof's block is not known: {0}")]
UnknownBlock(u32),

#[error("Response was invalid: requested_block: {0}")]
InvalidResponse(bool),

#[error("Outdated proof")]
OutdatedProof,
Expand Down
18 changes: 9 additions & 9 deletions zkp-component/src/zkp_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,18 @@ impl<N: Network + 'static> Stream for ZKPRequests<N> {
while let Poll::Ready(result) = self.zkp_request_results.poll_next_unpin(cx) {
match result {
Some((peer_id, request_election_block, response_channel, result)) => match result {
Ok(RequestZKPResponse::Proof(proof, mut election_block)) => {
Ok(RequestZKPResponse::Proof(proof, election_block)) => {
// Check that the response is in-line with whether we asked for the election block or not.
if request_election_block {
if election_block.is_none() {
if let Some(tx) = response_channel {
let _ = tx.send(Err(Error::InvalidBlock));
}
continue;
if (request_election_block && election_block.is_none())
|| (!request_election_block && !election_block.is_none())

Check warning on line 125 in zkp-component/src/zkp_requests.rs

View workflow job for this annotation

GitHub Actions / Clippy Report

this boolean expression can be simplified

warning: this boolean expression can be simplified --> zkp-component/src/zkp_requests.rs:125:60 | 125 | ... || (!request_election_block && !election_block.is_none()) | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `election_block.is_some()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#nonminimal_bool = note: `#[warn(clippy::nonminimal_bool)]` on by default
{
if let Some(tx) = response_channel {
let _ =
tx.send(Err(Error::InvalidResponse(request_election_block)));
}
} else {
election_block = None;
continue;
}

return Poll::Ready(Some(ZKPRequestsItem {
peer_id,
proof,
Expand Down

0 comments on commit 8b9e5c8

Please sign in to comment.