From be00cd1adde6e452cf90c5ee2c525c5e13ebdca9 Mon Sep 17 00:00:00 2001 From: Gian Felipe Date: Mon, 14 Jan 2019 06:34:29 +1100 Subject: [PATCH] Hotfix/validating query result length (#3053) * Validating that there are txs in the query results before loop throught the array * Created tests to validate the error has been fixed * Added comments * Fixing misspeling * check if the variable "skipCount" is bigger than zero. If it is not, we set it to 0. If it, we do not do anything. * using function that validates the skipCount variable * undo Gopkg.lock changes --- CHANGELOG_PENDING.md | 2 ++ rpc/client/rpc_test.go | 5 +++++ rpc/core/pipe.go | 9 +++++++++ rpc/core/tx.go | 3 ++- 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index fd95a9441..41e92255f 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -33,6 +33,8 @@ Special thanks to external contributors on this release: - [rpc] \#3047 Include peer's remote IP in `/net_info` ### BUG FIXES: + - [types] \#2926 do not panic if retrieving the private validator's public key fails +- [rpc] \#3080 check if the variable "skipCount" is bigger than zero. If it is not, we set it to 0. If it, we do not do anything. - [crypto/multisig] \#3102 fix multisig keys address length - [crypto/encoding] \#3101 Fix `PubKeyMultisigThreshold` unmarshalling into `crypto.PubKey` interface diff --git a/rpc/client/rpc_test.go b/rpc/client/rpc_test.go index fa5080f90..dac7ec12d 100644 --- a/rpc/client/rpc_test.go +++ b/rpc/client/rpc_test.go @@ -428,5 +428,10 @@ func TestTxSearch(t *testing.T) { if len(result.Txs) == 0 { t.Fatal("expected a lot of transactions") } + + // query a non existing tx with page 1 and txsPerPage 1 + result, err = c.TxSearch("app.creator='Cosmoshi Neetowoko'", true, 1, 1) + require.Nil(t, err, "%+v", err) + require.Len(t, result.Txs, 0) } } diff --git a/rpc/core/pipe.go b/rpc/core/pipe.go index 7f4596541..3d745e6ad 100644 --- a/rpc/core/pipe.go +++ b/rpc/core/pipe.go @@ -154,3 +154,12 @@ func validatePerPage(perPage int) int { } return perPage } + +func validateSkipCount(page, perPage int) int { + skipCount := (page - 1) * perPage + if skipCount < 0 { + return 0 + } + + return skipCount +} diff --git a/rpc/core/tx.go b/rpc/core/tx.go index 3bb0f28e8..f1bfd56a1 100644 --- a/rpc/core/tx.go +++ b/rpc/core/tx.go @@ -201,10 +201,11 @@ func TxSearch(query string, prove bool, page, perPage int) (*ctypes.ResultTxSear totalCount := len(results) perPage = validatePerPage(perPage) page = validatePage(page, perPage, totalCount) - skipCount := (page - 1) * perPage + skipCount := validateSkipCount(page, perPage) apiResults := make([]*ctypes.ResultTx, cmn.MinInt(perPage, totalCount-skipCount)) var proof types.TxProof + // if there's no tx in the results array, we don't need to loop through the apiResults array for i := 0; i < len(apiResults); i++ { r := results[skipCount+i] height := r.Height