fix(consensus): PrevHash validation silently skipped on empty input#1628
fix(consensus): PrevHash validation silently skipped on empty input#1628
Conversation
…or non-genesis blocks and added unit-test cases Signed-off-by: Akhil Repala <arepala@blinklabs.io>
📝 WalkthroughWalkthroughThe pull request adds validation to the 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Tip Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
consensus/validate_test.go (1)
152-197:⚠️ Potential issue | 🟡 MinorUse
requireassertions and validate the exact error message.This test should use the
testify/requireassertions already imported in the file. Lines 159, 170, and 181 currently uset.Errorandt.Errorfinstead.For the non-genesis missing hash case (line 159), use
require.EqualErrorto validate both that an error occurred and that it matches the expected message. For the valid cases (lines 170, 181), userequire.NoError:Suggested update
err := validator.validatePrevHash(input) - if err == nil { - t.Error("expected error for missing previous header hash on non-genesis block") - } + require.EqualError( + t, + err, + "previous header hash is required for non-genesis blocks", + ) // Valid: non-genesis block with matching previous header hash input = &ValidateHeaderInput{ PrevHash: hash, PrevHeaderHash: hash, BlockNumber: 1, } err = validator.validatePrevHash(input) - if err != nil { - t.Errorf("expected no error for matching hashes, got %v", err) - } + require.NoError(t, err) // Valid: genesis block without previous header hash input = &ValidateHeaderInput{ PrevHash: hash, PrevHeaderHash: nil, BlockNumber: 0, } err = validator.validatePrevHash(input) - if err != nil { - t.Errorf("expected no error for genesis block without prev hash, got %v", err) - } + require.NoError(t, err)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@consensus/validate_test.go` around lines 152 - 197, Replace the t.Error/t.Errorf assertions in the validatePrevHash tests with testify/require assertions: for the non-genesis missing previous header hash case (the ValidateHeaderInput with BlockNumber 1 and PrevHeaderHash nil) use require.EqualError(t, err, "<expected error message>") to assert an error and its exact message; for the two valid cases (BlockNumber 1 with matching PrevHeaderHash and BlockNumber 0 genesis case with PrevHeaderHash nil) use require.NoError(t, err) to assert success; keep the same inputs (PrevHash, PrevHeaderHash, BlockNumber) and the call to validator.validatePrevHash so only the assertions change.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@consensus/validate_test.go`:
- Around line 152-197: Replace the t.Error/t.Errorf assertions in the
validatePrevHash tests with testify/require assertions: for the non-genesis
missing previous header hash case (the ValidateHeaderInput with BlockNumber 1
and PrevHeaderHash nil) use require.EqualError(t, err, "<expected error
message>") to assert an error and its exact message; for the two valid cases
(BlockNumber 1 with matching PrevHeaderHash and BlockNumber 0 genesis case with
PrevHeaderHash nil) use require.NoError(t, err) to assert success; keep the same
inputs (PrevHash, PrevHeaderHash, BlockNumber) and the call to
validator.validatePrevHash so only the assertions change.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: d4af235c-73f5-40f6-bb81-377df77a7bf5
📒 Files selected for processing (2)
consensus/validate.goconsensus/validate_test.go
PrevHeaderHashfor all non-genesis blocks by returning an explicit error when it is empty, preventing silent prev-hash validation skipping.Closes #1576
Summary by cubic
Require
PrevHeaderHashfor non-genesis blocks inconsensusheader validation so empty input no longer skips prev-hash checks. Closes #1576.Bug Fixes
BlockNumber > 0andPrevHeaderHashis missing; allow genesis without it.Migration
PrevHeaderHashfor all non-genesis headers.Written for commit 67e703e. Summary will update on new commits.
Summary by CodeRabbit
Bug Fixes
Tests