Skip to content

Commit 1bd1259

Browse files
authored
refactor(state): improve error propagation for ReconsiderError (#9919)
1 parent 07a9940 commit 1bd1259

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

zebra-state/src/error.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ pub enum ReconsiderError {
5959
#[error("Invalidated blocks list is empty when it should contain at least one block")]
6060
InvalidatedBlocksEmpty,
6161

62+
#[error("cannot reconsider blocks while still committing checkpointed blocks")]
63+
CheckpointCommitInProgress,
64+
65+
#[error("failed to send reconsider block request to block write task")]
66+
ReconsiderSendFailed,
67+
68+
#[error("reconsider block request was unexpectedly dropped")]
69+
ReconsiderResponseDropped,
70+
6271
#[error("{0}")]
6372
ValidationError(#[from] Box<ValidateContextError>),
6473
}

zebra-state/src/service.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use crate::{
4545
constants::{
4646
MAX_FIND_BLOCK_HASHES_RESULTS, MAX_FIND_BLOCK_HEADERS_RESULTS, MAX_LEGACY_CHAIN_BLOCKS,
4747
},
48+
error::ReconsiderError,
4849
response::NonFinalizedBlocksListener,
4950
service::{
5051
block_iter::any_ancestor_blocks,
@@ -842,13 +843,11 @@ impl StateService {
842843
fn send_reconsider_block(
843844
&self,
844845
hash: block::Hash,
845-
) -> oneshot::Receiver<Result<Vec<block::Hash>, BoxError>> {
846+
) -> oneshot::Receiver<Result<Vec<block::Hash>, ReconsiderError>> {
846847
let (rsp_tx, rsp_rx) = oneshot::channel();
847848

848849
let Some(sender) = &self.block_write_sender.non_finalized else {
849-
let _ = rsp_tx.send(Err(
850-
"cannot reconsider blocks while still committing checkpointed blocks".into(),
851-
));
850+
let _ = rsp_tx.send(Err(ReconsiderError::CheckpointCommitInProgress));
852851
return rsp_rx;
853852
};
854853

@@ -859,9 +858,7 @@ impl StateService {
859858
unreachable!("should return the same Reconsider message could not be sent");
860859
};
861860

862-
let _ = rsp_tx.send(Err(
863-
"failed to send reconsider block request to block write task".into(),
864-
));
861+
let _ = rsp_tx.send(Err(ReconsiderError::ReconsiderSendFailed));
865862
}
866863

867864
rsp_rx
@@ -1204,11 +1201,11 @@ impl Service<Request> for StateService {
12041201
rsp_rx
12051202
.await
12061203
.map_err(|_recv_error| {
1207-
BoxError::from("reconsider block request was unexpectedly dropped")
1204+
BoxError::from(ReconsiderError::ReconsiderResponseDropped)
12081205
})
12091206
// TODO: replace with Result::flatten once it stabilises
12101207
// https://github.com/rust-lang/rust/issues/70142
1211-
.and_then(convert::identity)
1208+
.and_then(|res| res.map_err(BoxError::from))
12121209
.map(Response::Reconsidered)
12131210
}
12141211
.instrument(span)

zebra-state/src/service/write.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{
2121
finalized_state::{FinalizedState, ZebraDb},
2222
non_finalized_state::NonFinalizedState,
2323
queued_blocks::{QueuedCheckpointVerified, QueuedSemanticallyVerified},
24-
BoxError, ChainTipBlock, ChainTipSender,
24+
BoxError, ChainTipBlock, ChainTipSender, ReconsiderError,
2525
},
2626
CommitSemanticallyVerifiedError, SemanticallyVerifiedBlock,
2727
};
@@ -145,7 +145,7 @@ pub enum NonFinalizedWriteMessage {
145145
/// reconsidered and reinserted into the non-finalized state.
146146
Reconsider {
147147
hash: block::Hash,
148-
rsp_tx: oneshot::Sender<Result<Vec<block::Hash>, BoxError>>,
148+
rsp_tx: oneshot::Sender<Result<Vec<block::Hash>, ReconsiderError>>,
149149
},
150150
}
151151

@@ -349,11 +349,8 @@ impl WriteBlockWorkerTask {
349349
}
350350
NonFinalizedWriteMessage::Reconsider { hash, rsp_tx } => {
351351
tracing::info!(?hash, "reconsidering a block in the non-finalized state");
352-
let _ = rsp_tx.send(
353-
non_finalized_state
354-
.reconsider_block(hash, &finalized_state.db)
355-
.map_err(BoxError::from),
356-
);
352+
let _ = rsp_tx
353+
.send(non_finalized_state.reconsider_block(hash, &finalized_state.db));
357354
None
358355
}
359356
};

0 commit comments

Comments
 (0)