-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: define custom error values for
consensus
crate (#6)
Refactoring the `consensus` crate to define custom error values to be used instead of the on-the-fly, non-formatted `anyhow` errors. Closing [BFT-308](https://linear.app/matterlabs/issue/BFT-308/refactor-errors-in-consensus-crate).
- Loading branch information
Showing
9 changed files
with
195 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,55 @@ | ||
use roles::validator; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
#[allow(clippy::missing_docs_in_private_items)] | ||
pub(crate) enum Error { | ||
#[error("Received replica commit message for a proposal that we don't have.")] | ||
MissingProposal, | ||
#[error(transparent)] | ||
Other(#[from] anyhow::Error), | ||
#[error("received replica commit message for a missing proposal")] | ||
ReplicaCommitMissingProposal, | ||
#[error("received replica commit message for a past view/phase (current view: {current_view:?}, current phase: {current_phase:?})")] | ||
ReplicaCommitOld { | ||
current_view: validator::ViewNumber, | ||
current_phase: validator::Phase, | ||
}, | ||
#[error("received replica prepare message for a past view/phase (current view: {current_view:?}, current phase: {current_phase:?})")] | ||
ReplicaPrepareOld { | ||
current_view: validator::ViewNumber, | ||
current_phase: validator::Phase, | ||
}, | ||
#[error("received replica commit message for a view when we are not a leader")] | ||
ReplicaCommitWhenNotLeaderInView, | ||
#[error("received replica prepare message for a view when we are not a leader")] | ||
ReplicaPrepareWhenNotLeaderInView, | ||
#[error("received replica commit message that already exists (existing message: {existing_message:?}")] | ||
ReplicaCommitExists { existing_message: String }, | ||
#[error("received replica prepare message that already exists (existing message: {existing_message:?}")] | ||
ReplicaPrepareExists { existing_message: String }, | ||
#[error("received replica commit message while number of received messages is below threshold. waiting for more (received: {num_messages:?}, threshold: {threshold:?}")] | ||
ReplicaCommitNumReceivedBelowThreshold { | ||
num_messages: usize, | ||
threshold: usize, | ||
}, | ||
#[error("received replica prepare message while number of received messages below threshold. waiting for more (received: {num_messages:?}, threshold: {threshold:?}")] | ||
ReplicaPrepareNumReceivedBelowThreshold { | ||
num_messages: usize, | ||
threshold: usize, | ||
}, | ||
#[error("received replica prepare message with high QC of a future view (high QC view: {high_qc_view:?}, current view: {current_view:?}")] | ||
ReplicaPrepareHighQCOfFutureView { | ||
high_qc_view: validator::ViewNumber, | ||
current_view: validator::ViewNumber, | ||
}, | ||
#[error("received replica commit message with invalid signature")] | ||
ReplicaCommitInvalidSignature(#[source] crypto::bls12_381::Error), | ||
#[error("received replica prepare message with invalid signature")] | ||
ReplicaPrepareInvalidSignature(#[source] crypto::bls12_381::Error), | ||
#[error("received replica prepare message with invalid high QC")] | ||
ReplicaPrepareInvalidHighQC(#[source] anyhow::Error), | ||
} | ||
|
||
// TODO: This is only a temporary measure because anyhow::Error doesn't implement | ||
// PartialEq. When we change all errors to thiserror, we can just derive PartialEq. | ||
/// Needed due to source errors. | ||
impl PartialEq for Error { | ||
fn eq(&self, other: &Self) -> bool { | ||
matches!( | ||
(self, other), | ||
(Error::MissingProposal, Error::MissingProposal) | (Error::Other(_), Error::Other(_)) | ||
) | ||
self.to_string() == other.to_string() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use crate::validator; | ||
use roles::validator::BlockHash; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
#[allow(clippy::missing_docs_in_private_items)] | ||
pub(crate) enum Error { | ||
#[error("received leader commit message with invalid leader (correct leader: {correct_leader:?}, received leader: {received_leader:?})]")] | ||
LeaderCommitInvalidLeader { | ||
correct_leader: validator::PublicKey, | ||
received_leader: validator::PublicKey, | ||
}, | ||
#[error("received leader prepare message with invalid leader (correct leader: {correct_leader:?}, received leader: {received_leader:?}])")] | ||
LeaderPrepareInvalidLeader { | ||
correct_leader: validator::PublicKey, | ||
received_leader: validator::PublicKey, | ||
}, | ||
#[error("received leader commit message for a past view/phase (current view: {current_view:?}, current phase: {current_phase:?})")] | ||
LeaderCommitOld { | ||
current_view: validator::ViewNumber, | ||
current_phase: validator::Phase, | ||
}, | ||
#[error("received leader commit message for a past view/phase (current view: {current_view:?}, current phase: {current_phase:?})")] | ||
LeaderPrepareOld { | ||
current_view: validator::ViewNumber, | ||
current_phase: validator::Phase, | ||
}, | ||
#[error("received leader commit message with invalid signature")] | ||
LeaderCommitInvalidSignature(#[source] crypto::bls12_381::Error), | ||
#[error("received leader prepare message with invalid signature")] | ||
LeaderPrepareInvalidSignature(#[source] crypto::bls12_381::Error), | ||
#[error("received leader commit message with invalid justification")] | ||
LeaderCommitInvalidJustification(#[source] anyhow::Error), | ||
#[error("received leader prepare message with empty map in the justification")] | ||
LeaderPrepareJustificationWithEmptyMap, | ||
#[error("received leader prepare message with invalid PrepareQC")] | ||
LeaderPrepareInvalidPrepareQC(#[source] anyhow::Error), | ||
#[error("received leader prepare message with invalid high QC")] | ||
LeaderPrepareInvalidHighQC(#[source] anyhow::Error), | ||
#[error("received leader prepare message with high QC of a future view (high QC view: {high_qc_view:?}, current view: {current_view:?}")] | ||
LeaderPrepareHighQCOfFutureView { | ||
high_qc_view: validator::ViewNumber, | ||
current_view: validator::ViewNumber, | ||
}, | ||
#[error("received leader prepare message with new block proposal when the previous proposal was not finalized")] | ||
LeaderPrepareProposalWhenPreviousNotFinalized, | ||
#[error("received leader prepare message with new block proposal with invalid parent hash (correct parent hash: {correct_parent_hash:#?}, received parent hash: {received_parent_hash:#?}, block: {block:?})")] | ||
LeaderPrepareProposalInvalidParentHash { | ||
correct_parent_hash: BlockHash, | ||
received_parent_hash: BlockHash, | ||
block: validator::Block, | ||
}, | ||
#[error("received leader prepare message with block proposal with non-sequential number (correct proposal number: {correct_number}, received proposal number: {received_number}, block: {block:?})")] | ||
LeaderPrepareProposalNonSequentialNumber { | ||
correct_number: u64, | ||
received_number: u64, | ||
block: validator::Block, | ||
}, | ||
#[error("received leader prepare message with block proposal with an oversized payload (payload size: {payload_size}, block: {block:?}")] | ||
LeaderPrepareProposalOversizedPayload { | ||
payload_size: usize, | ||
block: validator::Block, | ||
}, | ||
#[error("received leader prepare message with block re-proposal when the previous proposal was finalized")] | ||
LeaderPrepareReproposalWhenFinalized, | ||
#[error("received leader prepare message with block re-proposal of invalid block")] | ||
LeaderPrepareReproposalInvalidBlock, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.