Skip to content

Commit 9fb7b76

Browse files
committed
fix: check prefix error to contain the index of the problematic block
1 parent 80e5865 commit 9fb7b76

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

ledger/src/check_next_block.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ pub enum CheckBlockError<N: Network> {
5252
#[error("Block has invalid hash")]
5353
InvalidHash,
5454
/// An error related to the given prefix of pending blocks.
55-
#[error("Prefix of the block at height {height} is incorrect - {error:?}")]
56-
InvalidPrefix { height: u32, error: Box<CheckBlockError<N>> },
55+
#[error("The prefix as an error at index {index} - {error:?}")]
56+
InvalidPrefix { index: usize, error: Box<CheckBlockError<N>> },
5757
#[error("The block contains solution '{solution_id}', but it already exists in the ledger")]
5858
SolutionAlreadyExists { solution_id: SolutionID<N> },
5959
#[error("Failed to speculate over unconfirmed transactions - {inner}")]
@@ -115,10 +115,10 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
115115
// First check that the heights and hashes of the pending block sequence and of the new block are correct.
116116
// The hash checks should be redundant, but we perform them out of extra caution.
117117
let mut expected_height = latest_block.height() + 1;
118-
for prefix_block in prefix {
118+
for (index, prefix_block) in prefix.iter().enumerate() {
119119
if prefix_block.height() != expected_height {
120120
return Err(CheckBlockError::InvalidPrefix {
121-
height: prefix_block.height(),
121+
index,
122122
error: Box::new(CheckBlockError::InvalidHeight {
123123
expected: expected_height,
124124
actual: prefix_block.height(),
@@ -128,7 +128,7 @@ impl<N: Network, C: ConsensusStorage<N>> Ledger<N, C> {
128128

129129
if self.contains_block_hash(&prefix_block.hash())? {
130130
return Err(CheckBlockError::InvalidPrefix {
131-
height: prefix_block.height(),
131+
index,
132132
error: Box::new(CheckBlockError::BlockAlreadyExists { hash: prefix_block.hash() }),
133133
});
134134
}

ledger/tests/pending_blocks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,13 @@ fn test_prefix_with_duplicate_block_error() {
121121

122122
// Test a prefix that contains block2 twice.
123123
let result = ledger.check_block_subdag(block4.clone(), &[block2.clone(), block2.clone(), block3.clone()]);
124-
assert!(matches!(result, Err(CheckBlockError::InvalidPrefix { height: 2, .. })));
124+
assert!(matches!(result, Err(CheckBlockError::InvalidPrefix { index: 1, .. })));
125125
let CheckBlockError::InvalidPrefix { error, .. } = result.unwrap_err() else { unreachable!() };
126126
assert!(matches!(*error, CheckBlockError::InvalidHeight { expected: 3, actual: 2 }));
127127

128128
// Test a prefix that misses block 2.
129129
let result = ledger.check_block_subdag(block4.clone(), &[block3]);
130-
assert!(matches!(result, Err(CheckBlockError::InvalidPrefix { height: 3, .. })));
130+
assert!(matches!(result, Err(CheckBlockError::InvalidPrefix { index: 0, .. })));
131131
let CheckBlockError::InvalidPrefix { error, .. } = result.unwrap_err() else { unreachable!() };
132132
assert!(matches!(*error, CheckBlockError::InvalidHeight { expected: 2, actual: 3 }));
133133
}

0 commit comments

Comments
 (0)