-
Notifications
You must be signed in to change notification settings - Fork 174
fix(rpc): guard nil height deref in CometBlockResultByNumber #1067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Aboudjem
wants to merge
2
commits into
cosmos:main
Choose a base branch
from
Aboudjem:fix/comet-block-result-nil-deref
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package backend | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/mock" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| tmrpctypes "github.com/cometbft/cometbft/rpc/core/types" | ||
|
|
||
| "github.com/cosmos/evm/rpc/backend/mocks" | ||
| ) | ||
|
|
||
| func TestCometBlockResultByNumber_NilHeightErrorPath(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| height *int64 | ||
| rpcErr error | ||
| expectErr bool | ||
| }{ | ||
| { | ||
| name: "nil height, BlockResults succeeds", | ||
| height: nil, | ||
| rpcErr: nil, | ||
| expectErr: false, | ||
| }, | ||
| { | ||
| name: "non-zero height, BlockResults succeeds", | ||
| height: func() *int64 { h := int64(5); return &h }(), | ||
| rpcErr: nil, | ||
| expectErr: false, | ||
| }, | ||
| { | ||
| name: "zero height remapped to nil, BlockResults errors - must not panic", | ||
| height: func() *int64 { h := int64(0); return &h }(), | ||
| rpcErr: errors.New("connection timeout"), | ||
| expectErr: true, | ||
| }, | ||
| { | ||
| name: "nil height, BlockResults errors", | ||
| height: nil, | ||
| rpcErr: errors.New("pruned state"), | ||
| expectErr: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| backend := setupMockBackend(t) | ||
| mockClient := backend.ClientCtx.Client.(*mocks.Client) | ||
|
|
||
| // The function remaps height=0 to nil before calling BlockResults, | ||
| // so always mock with nil when input is 0. | ||
| var mockHeight *int64 | ||
| if tc.height != nil && *tc.height != 0 { | ||
| mockHeight = tc.height | ||
| } | ||
|
|
||
| if tc.rpcErr != nil { | ||
| mockClient.On("BlockResults", mock.Anything, mockHeight). | ||
| Return((*tmrpctypes.ResultBlockResults)(nil), tc.rpcErr).Once() | ||
| } else { | ||
| mockClient.On("BlockResults", mock.Anything, mockHeight). | ||
| Return(&tmrpctypes.ResultBlockResults{Height: 1}, nil).Once() | ||
| } | ||
|
|
||
| require.NotPanics(t, func() { | ||
| _, err := backend.CometBlockResultByNumber(context.Background(), tc.height) | ||
| if tc.expectErr { | ||
| require.Error(t, err) | ||
| } else { | ||
| require.NoError(t, err) | ||
| } | ||
| }) | ||
| }) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ambiguous height in error message when caller passes
nilWhen
CometBlockResultByNumberis called with anilheight (i.e., "fetch latest block"),heightAttrdefaults to0. IfBlockResultsthen fails, the error message will read"failed to fetch block result from CometBFT 0: ...", which is indistinguishable from a caller that explicitly requested block 0 (which is also remapped tonil). This is a pre-existing ambiguity but the fix keeps it intact.Consider using a more descriptive format string to differentiate the two cases, for example:
This is a minor style concern and does not affect correctness.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!