From c8443c0e95741960b35d056c6025beb9b175b5ec Mon Sep 17 00:00:00 2001 From: blushi Date: Wed, 1 Jun 2022 14:26:42 +0200 Subject: [PATCH 1/5] Fix GetBlockWithTxs if 0 tx --- x/auth/tx/service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/auth/tx/service.go b/x/auth/tx/service.go index e00b7a519aa2..414ed31b200e 100644 --- a/x/auth/tx/service.go +++ b/x/auth/tx/service.go @@ -198,7 +198,7 @@ func (s txServer) GetBlockWithTxs(ctx context.Context, req *txtypes.GetBlockWith blockTxs := block.Data.Txs blockTxsLn := uint64(len(blockTxs)) txs := make([]*txtypes.Tx, 0, limit) - if offset >= blockTxsLn { + if offset >= blockTxsLn && blockTxsLn != 0 { return nil, sdkerrors.ErrInvalidRequest.Wrapf("out of range: cannot paginate %d txs with offset %d and limit %d", blockTxsLn, offset, limit) } decodeTxAt := func(i uint64) error { From 19c640e295eb711c2935e09d1141dbb9f492ef34 Mon Sep 17 00:00:00 2001 From: blushi Date: Wed, 1 Jun 2022 14:46:26 +0200 Subject: [PATCH 2/5] Add test --- x/auth/tx/service_test.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/x/auth/tx/service_test.go b/x/auth/tx/service_test.go index 4b670684c017..7c1ca9f53fc8 100644 --- a/x/auth/tx/service_test.go +++ b/x/auth/tx/service_test.go @@ -615,14 +615,16 @@ func (s IntegrationTestSuite) TestGetBlockWithTxs_GRPC() { req *tx.GetBlockWithTxsRequest expErr bool expErrMsg string + expTxLen int }{ - {"nil request", nil, true, "request cannot be nil"}, - {"empty request", &tx.GetBlockWithTxsRequest{}, true, "height must not be less than 1 or greater than the current height"}, - {"bad height", &tx.GetBlockWithTxsRequest{Height: 99999999}, true, "height must not be less than 1 or greater than the current height"}, - {"bad pagination", &tx.GetBlockWithTxsRequest{Height: s.txHeight, Pagination: &query.PageRequest{Offset: 1000, Limit: 100}}, true, "out of range"}, - {"good request", &tx.GetBlockWithTxsRequest{Height: s.txHeight}, false, ""}, - {"with pagination request", &tx.GetBlockWithTxsRequest{Height: s.txHeight, Pagination: &query.PageRequest{Offset: 0, Limit: 1}}, false, ""}, - {"page all request", &tx.GetBlockWithTxsRequest{Height: s.txHeight, Pagination: &query.PageRequest{Offset: 0, Limit: 100}}, false, ""}, + {"nil request", nil, true, "request cannot be nil", 0}, + {"empty request", &tx.GetBlockWithTxsRequest{}, true, "height must not be less than 1 or greater than the current height", 0}, + {"bad height", &tx.GetBlockWithTxsRequest{Height: 99999999}, true, "height must not be less than 1 or greater than the current height", 0}, + {"bad pagination", &tx.GetBlockWithTxsRequest{Height: s.txHeight, Pagination: &query.PageRequest{Offset: 1000, Limit: 100}}, true, "out of range", 0}, + {"good request", &tx.GetBlockWithTxsRequest{Height: s.txHeight}, false, "", 2}, + {"with pagination request", &tx.GetBlockWithTxsRequest{Height: s.txHeight, Pagination: &query.PageRequest{Offset: 0, Limit: 1}}, false, "", 1}, + {"page all request", &tx.GetBlockWithTxsRequest{Height: s.txHeight, Pagination: &query.PageRequest{Offset: 0, Limit: 100}}, false, "", 2}, + {"block with 0 tx", &tx.GetBlockWithTxsRequest{Height: s.txHeight - 1, Pagination: &query.PageRequest{Offset: 0, Limit: 100}}, false, "", 0}, } for _, tc := range testCases { s.Run(tc.name, func() { @@ -633,7 +635,10 @@ func (s IntegrationTestSuite) TestGetBlockWithTxs_GRPC() { s.Require().Contains(err.Error(), tc.expErrMsg) } else { s.Require().NoError(err) - s.Require().Equal("foobar", grpcRes.Txs[0].Body.Memo) + s.Require().Equal(tc.expTxLen, len(grpcRes.Txs)) + if tc.expTxLen > 0 { + s.Require().Equal("foobar", grpcRes.Txs[0].Body.Memo) + } s.Require().Equal(grpcRes.Block.Header.Height, tc.req.Height) if tc.req.Pagination != nil { s.Require().LessOrEqual(len(grpcRes.Txs), int(tc.req.Pagination.Limit)) From 864b3e455353b20d7e5abfd8edb05906482e7b2e Mon Sep 17 00:00:00 2001 From: blushi Date: Wed, 1 Jun 2022 14:49:25 +0200 Subject: [PATCH 3/5] Update naming --- x/auth/tx/service_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x/auth/tx/service_test.go b/x/auth/tx/service_test.go index 7c1ca9f53fc8..b40d8e9aac26 100644 --- a/x/auth/tx/service_test.go +++ b/x/auth/tx/service_test.go @@ -615,7 +615,7 @@ func (s IntegrationTestSuite) TestGetBlockWithTxs_GRPC() { req *tx.GetBlockWithTxsRequest expErr bool expErrMsg string - expTxLen int + expTxsLen int }{ {"nil request", nil, true, "request cannot be nil", 0}, {"empty request", &tx.GetBlockWithTxsRequest{}, true, "height must not be less than 1 or greater than the current height", 0}, @@ -635,8 +635,8 @@ func (s IntegrationTestSuite) TestGetBlockWithTxs_GRPC() { s.Require().Contains(err.Error(), tc.expErrMsg) } else { s.Require().NoError(err) - s.Require().Equal(tc.expTxLen, len(grpcRes.Txs)) - if tc.expTxLen > 0 { + s.Require().Equal(tc.expTxsLen, len(grpcRes.Txs)) + if tc.expTxsLen > 0 { s.Require().Equal("foobar", grpcRes.Txs[0].Body.Memo) } s.Require().Equal(grpcRes.Block.Header.Height, tc.req.Height) From 911a69542143692646efb5b1538ce826b512a2bd Mon Sep 17 00:00:00 2001 From: blushi Date: Wed, 1 Jun 2022 16:59:32 +0200 Subject: [PATCH 4/5] Fix tests --- x/auth/tx/service_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/x/auth/tx/service_test.go b/x/auth/tx/service_test.go index b40d8e9aac26..d144cc406c29 100644 --- a/x/auth/tx/service_test.go +++ b/x/auth/tx/service_test.go @@ -621,9 +621,9 @@ func (s IntegrationTestSuite) TestGetBlockWithTxs_GRPC() { {"empty request", &tx.GetBlockWithTxsRequest{}, true, "height must not be less than 1 or greater than the current height", 0}, {"bad height", &tx.GetBlockWithTxsRequest{Height: 99999999}, true, "height must not be less than 1 or greater than the current height", 0}, {"bad pagination", &tx.GetBlockWithTxsRequest{Height: s.txHeight, Pagination: &query.PageRequest{Offset: 1000, Limit: 100}}, true, "out of range", 0}, - {"good request", &tx.GetBlockWithTxsRequest{Height: s.txHeight}, false, "", 2}, + {"good request", &tx.GetBlockWithTxsRequest{Height: s.txHeight}, false, "", 1}, {"with pagination request", &tx.GetBlockWithTxsRequest{Height: s.txHeight, Pagination: &query.PageRequest{Offset: 0, Limit: 1}}, false, "", 1}, - {"page all request", &tx.GetBlockWithTxsRequest{Height: s.txHeight, Pagination: &query.PageRequest{Offset: 0, Limit: 100}}, false, "", 2}, + {"page all request", &tx.GetBlockWithTxsRequest{Height: s.txHeight, Pagination: &query.PageRequest{Offset: 0, Limit: 100}}, false, "", 1}, {"block with 0 tx", &tx.GetBlockWithTxsRequest{Height: s.txHeight - 1, Pagination: &query.PageRequest{Offset: 0, Limit: 100}}, false, "", 0}, } for _, tc := range testCases { @@ -635,7 +635,6 @@ func (s IntegrationTestSuite) TestGetBlockWithTxs_GRPC() { s.Require().Contains(err.Error(), tc.expErrMsg) } else { s.Require().NoError(err) - s.Require().Equal(tc.expTxsLen, len(grpcRes.Txs)) if tc.expTxsLen > 0 { s.Require().Equal("foobar", grpcRes.Txs[0].Body.Memo) } From 557d4b1297953c26e3926e4cc115374c5e562a5c Mon Sep 17 00:00:00 2001 From: blushi Date: Wed, 1 Jun 2022 17:23:13 +0200 Subject: [PATCH 5/5] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b9d5d76f982..a9b9c0665b5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (migrations) [#12028](https://github.com/cosmos/cosmos-sdk/pull/12028) Fix v0.45->v0.46 in-place store migrations. * (baseapp) [#12089](https://github.com/cosmos/cosmos-sdk/pull/12089) Include antehandler and runMsgs events in SimulateTx. * (cli) [#12095](https://github.com/cosmos/cosmos-sdk/pull/12095) Fix running a tx with --dry-run returns an error +* (x/auth) [#12108](https://github.com/cosmos/cosmos-sdk/pull/12108) Fix GetBlockWithTxs error when querying block with 0 tx ## [v0.46.0-rc1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.0-rc1) - 2022-05-23