Skip to content

Commit a2711ed

Browse files
mergify[bot]blushi
andauthored
fix: GetBlockWithTxs error when querying block with 0 tx (#12108) (#12123)
## Description Closes: #12040 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) (cherry picked from commit f71d464) Co-authored-by: Marie Gauthier <marie.gauthier63@gmail.com>
1 parent c12cf1e commit a2711ed

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
5050
* (migrations) [#12028](https://github.com/cosmos/cosmos-sdk/pull/12028) Fix v0.45->v0.46 in-place store migrations.
5151
* (baseapp) [#12089](https://github.com/cosmos/cosmos-sdk/pull/12089) Include antehandler and runMsgs events in SimulateTx.
5252
* (cli) [#12095](https://github.com/cosmos/cosmos-sdk/pull/12095) Fix running a tx with --dry-run returns an error
53+
* (x/auth) [#12108](https://github.com/cosmos/cosmos-sdk/pull/12108) Fix GetBlockWithTxs error when querying block with 0 tx
5354

5455
## [v0.46.0-rc1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0-rc1) - 2022-05-23
5556

x/auth/tx/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (s txServer) GetBlockWithTxs(ctx context.Context, req *txtypes.GetBlockWith
198198
blockTxs := block.Data.Txs
199199
blockTxsLn := uint64(len(blockTxs))
200200
txs := make([]*txtypes.Tx, 0, limit)
201-
if offset >= blockTxsLn {
201+
if offset >= blockTxsLn && blockTxsLn != 0 {
202202
return nil, sdkerrors.ErrInvalidRequest.Wrapf("out of range: cannot paginate %d txs with offset %d and limit %d", blockTxsLn, offset, limit)
203203
}
204204
decodeTxAt := func(i uint64) error {

x/auth/tx/service_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -615,14 +615,16 @@ func (s IntegrationTestSuite) TestGetBlockWithTxs_GRPC() {
615615
req *tx.GetBlockWithTxsRequest
616616
expErr bool
617617
expErrMsg string
618+
expTxsLen int
618619
}{
619-
{"nil request", nil, true, "request cannot be nil"},
620-
{"empty request", &tx.GetBlockWithTxsRequest{}, true, "height must not be less than 1 or greater than the current height"},
621-
{"bad height", &tx.GetBlockWithTxsRequest{Height: 99999999}, true, "height must not be less than 1 or greater than the current height"},
622-
{"bad pagination", &tx.GetBlockWithTxsRequest{Height: s.txHeight, Pagination: &query.PageRequest{Offset: 1000, Limit: 100}}, true, "out of range"},
623-
{"good request", &tx.GetBlockWithTxsRequest{Height: s.txHeight}, false, ""},
624-
{"with pagination request", &tx.GetBlockWithTxsRequest{Height: s.txHeight, Pagination: &query.PageRequest{Offset: 0, Limit: 1}}, false, ""},
625-
{"page all request", &tx.GetBlockWithTxsRequest{Height: s.txHeight, Pagination: &query.PageRequest{Offset: 0, Limit: 100}}, false, ""},
620+
{"nil request", nil, true, "request cannot be nil", 0},
621+
{"empty request", &tx.GetBlockWithTxsRequest{}, true, "height must not be less than 1 or greater than the current height", 0},
622+
{"bad height", &tx.GetBlockWithTxsRequest{Height: 99999999}, true, "height must not be less than 1 or greater than the current height", 0},
623+
{"bad pagination", &tx.GetBlockWithTxsRequest{Height: s.txHeight, Pagination: &query.PageRequest{Offset: 1000, Limit: 100}}, true, "out of range", 0},
624+
{"good request", &tx.GetBlockWithTxsRequest{Height: s.txHeight}, false, "", 1},
625+
{"with pagination request", &tx.GetBlockWithTxsRequest{Height: s.txHeight, Pagination: &query.PageRequest{Offset: 0, Limit: 1}}, false, "", 1},
626+
{"page all request", &tx.GetBlockWithTxsRequest{Height: s.txHeight, Pagination: &query.PageRequest{Offset: 0, Limit: 100}}, false, "", 1},
627+
{"block with 0 tx", &tx.GetBlockWithTxsRequest{Height: s.txHeight - 1, Pagination: &query.PageRequest{Offset: 0, Limit: 100}}, false, "", 0},
626628
}
627629
for _, tc := range testCases {
628630
s.Run(tc.name, func() {
@@ -633,7 +635,9 @@ func (s IntegrationTestSuite) TestGetBlockWithTxs_GRPC() {
633635
s.Require().Contains(err.Error(), tc.expErrMsg)
634636
} else {
635637
s.Require().NoError(err)
636-
s.Require().Equal("foobar", grpcRes.Txs[0].Body.Memo)
638+
if tc.expTxsLen > 0 {
639+
s.Require().Equal("foobar", grpcRes.Txs[0].Body.Memo)
640+
}
637641
s.Require().Equal(grpcRes.Block.Header.Height, tc.req.Height)
638642
if tc.req.Pagination != nil {
639643
s.Require().LessOrEqual(len(grpcRes.Txs), int(tc.req.Pagination.Limit))

0 commit comments

Comments
 (0)