Skip to content

Commit c3e756c

Browse files
committed
fix: refactor CertsError
1 parent c65faac commit c3e756c

File tree

3 files changed

+46
-28
lines changed

3 files changed

+46
-28
lines changed

certs/src/error.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Copyright 2019-2024 ChainSafe Systems
22
// SPDX-License-Identifier: Apache-2.0, MIT
33

4-
use filecoin_f3_gpbft::{ActorId, CborError, GPBFTError};
4+
use std::borrow::Cow;
5+
6+
use filecoin_f3_gpbft::{ActorId, CborError, Cid, GPBFTError, Phase};
57
use thiserror::Error;
68

79
#[derive(Error, Debug, PartialEq)]
@@ -12,23 +14,26 @@ pub enum CertsError {
1214
#[error("empty power delta")]
1315
EmptyPowerDelta,
1416

15-
#[error("invalid justification: {0}")]
16-
InvalidJustification(String),
17+
#[error("invalid justification vote phase: expected {expected}, got {actual}")]
18+
InvalidJustificationVotePhase { expected: Phase, actual: Phase },
19+
20+
#[error("invalid justification vote value chain: {0}")]
21+
InvalidJustificationVoteValueChain(Cow<'static, str>),
1722

1823
#[error("got a decision for bottom for instance {0}")]
1924
BottomDecision(u64),
2025

2126
#[error("invalid power table delta: {0}")]
22-
InvalidPowerTableDelta(String),
27+
InvalidPowerTableDelta(Cow<'static, str>),
2328

2429
#[error("serialization error: {0}")]
25-
SerializationError(String),
30+
SerializationError(Cow<'static, str>),
2631

27-
#[error("expected instance {expected}, found instance {found}")]
28-
InstanceMismatch { expected: u64, found: u64 },
32+
#[error("expected instance {expected}, found instance {actual}")]
33+
InstanceMismatch { expected: u64, actual: u64 },
2934

30-
#[error("invalid round: expected 0, got {0}")]
31-
InvalidRound(u64),
35+
#[error("invalid round: expected {expected}, got {actual}")]
36+
InvalidRound { expected: u64, actual: u64 },
3237

3338
#[error("diff {0} not sorted by participant ID")]
3439
UnsortedDiff(usize),
@@ -58,12 +63,12 @@ pub enum CertsError {
5863
BaseTipsetMismatch(u64),
5964

6065
#[error(
61-
"incorrect power diff from finality certificate for instance {instance}: expected {expected:?}, got {got:?}"
66+
"incorrect power diff from finality certificate for instance {instance}: expected {expected}, got {actual}"
6267
)]
6368
IncorrectPowerDiff {
6469
instance: u64,
65-
expected: String,
66-
got: String,
70+
expected: Box<Cid>, // use `Box` to suppress largest variant warning
71+
actual: Box<Cid>,
6772
},
6873

6974
#[error("cbor encoding error")]

certs/src/lib.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,18 @@ impl FinalityCertificate {
9494
/// # Returns
9595
/// A Result containing the new `FinalityCertificate` if successful
9696
pub fn new(power_delta: PowerTableDiff, justification: &Justification) -> Result<Self> {
97-
if justification.vote.step != Phase::Decide {
98-
return Err(CertsError::InvalidJustification(
99-
justification.vote.step.to_string(),
100-
));
97+
if justification.vote.phase != Phase::Decide {
98+
return Err(CertsError::InvalidJustificationVotePhase {
99+
expected: Phase::Decide,
100+
actual: justification.vote.phase,
101+
});
101102
}
102103

103104
if justification.vote.round != 0 {
104-
return Err(CertsError::InvalidRound(justification.vote.round));
105+
return Err(CertsError::InvalidRound {
106+
expected: 0,
107+
actual: justification.vote.round,
108+
});
105109
}
106110

107111
if justification.vote.value.is_empty() {
@@ -295,7 +299,7 @@ pub fn validate_finality_certificates<'a>(
295299
if cert.gpbft_instance != next_instance {
296300
return Err(CertsError::InstanceMismatch {
297301
expected: next_instance,
298-
found: cert.gpbft_instance,
302+
actual: cert.gpbft_instance,
299303
});
300304
}
301305

@@ -326,8 +330,8 @@ pub fn validate_finality_certificates<'a>(
326330
if cert.supplemental_data.power_table != power_table_cid {
327331
return Err(CertsError::IncorrectPowerDiff {
328332
instance: cert.gpbft_instance,
329-
expected: cert.supplemental_data.power_table.to_string(),
330-
got: power_table_cid.to_string(),
333+
expected: cert.supplemental_data.power_table.into(),
334+
actual: power_table_cid.into(),
331335
});
332336
}
333337

@@ -386,7 +390,7 @@ mod tests {
386390
vote: Payload {
387391
instance: 1,
388392
round: 0,
389-
step,
393+
phase: step,
390394
supplemental_data: SupplementalData {
391395
commitments: keccak_hash::H256::zero(),
392396
power_table: Cid::from_str(cid).unwrap(),
@@ -447,7 +451,10 @@ mod tests {
447451
assert!(result.is_err());
448452
assert_eq!(
449453
result.unwrap_err(),
450-
CertsError::InvalidJustification(Phase::Commit.to_string())
454+
CertsError::InvalidJustificationVotePhase {
455+
expected: Phase::Decide,
456+
actual: Phase::Commit
457+
}
451458
);
452459
}
453460

@@ -460,7 +467,13 @@ mod tests {
460467

461468
let result = FinalityCertificate::new(power_delta, &justification);
462469
assert!(result.is_err());
463-
assert_eq!(result.unwrap_err(), CertsError::InvalidRound(1));
470+
assert_eq!(
471+
result.unwrap_err(),
472+
CertsError::InvalidRound {
473+
expected: 0,
474+
actual: 1
475+
}
476+
);
464477
}
465478

466479
// It makes no sense that ECChain can be empty. Perhaps this warrants a discussion.

gpbft/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub use fvm_ipld_bitfield::BitField;
3636
pub use fvm_ipld_encoding::{Error as CborError, to_vec as to_vec_cbor};
3737
pub use num_bigint::{BigInt, Sign};
3838
pub use num_traits::Zero;
39-
use strum_macros::Display;
39+
use strum_macros::{Display, IntoStaticStr};
4040

4141
pub use crate::chain::{Cid, ECChain, cid_from_bytes};
4242
pub use types::{ActorId, NetworkName, PubKey, StoragePower};
@@ -66,7 +66,7 @@ pub struct SupplementalData {
6666

6767
/// Represents the different phases of the GPBFT consensus protocol
6868
#[repr(u8)]
69-
#[derive(Display, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
69+
#[derive(Display, IntoStaticStr, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
7070
#[strum(serialize_all = "UPPERCASE")]
7171
pub enum Phase {
7272
/// This phase marks the beginning of a new consensus round. During this phase,
@@ -95,7 +95,7 @@ pub struct Payload {
9595
/// GPBFT round number
9696
pub round: u64,
9797
/// Current phase of the GPBFT protocol
98-
pub step: Phase,
98+
pub phase: Phase,
9999
/// Additional data related to this consensus instance
100100
pub supplemental_data: SupplementalData,
101101
/// The agreed-upon value for this instance
@@ -122,7 +122,7 @@ impl Payload {
122122
Payload {
123123
instance,
124124
round,
125-
step,
125+
phase: step,
126126
supplemental_data,
127127
value,
128128
}
@@ -164,7 +164,7 @@ mod tests {
164164

165165
assert_eq!(payload.instance, instance);
166166
assert_eq!(payload.round, round);
167-
assert_eq!(payload.step, step);
167+
assert_eq!(payload.phase, step);
168168
assert_eq!(payload.supplemental_data, supplemental_data);
169169
assert_eq!(payload.value, value);
170170
}

0 commit comments

Comments
 (0)