Skip to content

Commit 8cfc430

Browse files
committed
fix(consensus-db): declare count with Self::Error
Every method across the six repository traits and the pruning service is declared with Self::Error. PendingProposalsRepository::count is the one exception: it hardcodes StoreError. That is a compile error rather than a style point. The trait cannot be implemented by anything whose Error is not StoreError — an implementor returning its own error type from count fails with E0271. It also leaves the generated mock, built with Error = std::io::Error, returning io::Error from enforce_limit and StoreError from count. The blanket impl for &T carried the same signature and now forwards Self::Error as well. Store is unaffected, its Error already is StoreError.
1 parent 3f1efe9 commit 8cfc430

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

‎crates/consensus-db/src/repositories/pending_proposals.rs‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub trait PendingProposalsRepository {
3232
) -> Result<Vec<(Height, Round, BlockHash)>, Self::Error>;
3333

3434
/// Return the total number of stored pending proposal parts.
35-
async fn count(&self) -> Result<usize, StoreError>;
35+
async fn count(&self) -> Result<usize, Self::Error>;
3636
}
3737

3838
impl<T> PendingProposalsRepository for &T
@@ -51,7 +51,7 @@ where
5151
.await
5252
}
5353

54-
async fn count(&self) -> Result<usize, StoreError> {
54+
async fn count(&self) -> Result<usize, Self::Error> {
5555
(**self).count().await
5656
}
5757
}
@@ -68,7 +68,46 @@ impl PendingProposalsRepository for Store {
6868
.await
6969
}
7070

71-
async fn count(&self) -> Result<usize, StoreError> {
71+
async fn count(&self) -> Result<usize, Self::Error> {
7272
self.get_pending_proposal_parts_count().await
7373
}
7474
}
75+
76+
#[cfg(test)]
77+
mod tests {
78+
use super::*;
79+
80+
#[derive(Debug, thiserror::Error)]
81+
#[error("count failed")]
82+
struct CountError;
83+
84+
/// An implementor whose error type is not `StoreError`, which is the point
85+
/// of the associated `Error` type.
86+
struct FailingCount;
87+
88+
impl PendingProposalsRepository for FailingCount {
89+
type Error = CountError;
90+
91+
async fn enforce_limit(
92+
&self,
93+
_max_pending_proposals: usize,
94+
_current_height: Height,
95+
) -> Result<Vec<(Height, Round, BlockHash)>, Self::Error> {
96+
Ok(Vec::new())
97+
}
98+
99+
async fn count(&self) -> Result<usize, Self::Error> {
100+
Err(CountError)
101+
}
102+
}
103+
104+
#[tokio::test]
105+
async fn count_reports_the_implementor_error_type() {
106+
let repo = FailingCount;
107+
assert!(matches!(repo.count().await, Err(CountError)));
108+
109+
// The blanket impl for `&T` forwards the same error type.
110+
let by_ref = &repo;
111+
assert!(matches!(by_ref.count().await, Err(CountError)));
112+
}
113+
}

0 commit comments

Comments
 (0)