cosmos-sdk/types/query/pagination_test.go

350 lines
14 KiB
Go
Raw Normal View History

package query_test
import (
gocontext "context"
"fmt"
"testing"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
Change `address` from bytes to bech32 strings (#7242) * init * Fix bank proto messages * missing conversions * remove casttype for addresses * Fix tests * Fix consaddress * more test fixes * Fix tests * fixed tests * migrate missing proto declarations * format * Fix format * Fix alignment * Fix more tests * Fix ibc merge issue * Fix fmt * Fix more tests * Fix missing address declarations * Fix staking tests * Fix more tests * Fix config * fixed tests * Fix more tests * Update staking grpc tests * Fix merge issue * fixed failing tests in x/distr * fixed sim tests * fixed failing tests * Fix bugs * Add logs * fixed slashing issue * Fix staking grpc tests * Fix all bank tests :) * Fix tests in distribution * Fix more tests in distr * Fix slashing tests * Fix statking tests * Fix evidence tests * Fix gov tests * Fix bug in create vesting account * Fix test * remove fmt * fixed gov tests * fixed x/ibc tests * fixed x/ibc-transfer tests * fixed staking tests * fixed staking tests * fixed test * fixed distribution issue * fix pagination test * fmt * lint * fix build * fix format * revert tally tests * revert tally tests * lint * Fix sim test * revert * revert * fixed tally issue * fix tests * revert * fmt * refactor * remove `GetAddress()` * remove fmt * revert fmt.Striger usage * Fix tests * Fix rest test * disable interfacer lint check * make proto-format * add nolint rule * remove stray println Co-authored-by: aleem1314 <aleem.md789@gmail.com> Co-authored-by: atheesh <atheesh@vitwit.com>
2020-09-25 03:25:37 -07:00
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/store"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)
const (
holder = "holder"
multiPerm = "multiple permissions account"
randomPerm = "random permission"
numBalances = 235
defaultLimit = 100
overLimit = 101
underLimit = 10
lastPageRecords = 35
)
type paginationTestSuite struct {
suite.Suite
}
func TestPaginationTestSuite(t *testing.T) {
suite.Run(t, new(paginationTestSuite))
}
func (s *paginationTestSuite) TestParsePagination() {
s.T().Log("verify default values for empty page request")
pageReq := &query.PageRequest{}
page, limit, err := query.ParsePagination(pageReq)
s.Require().NoError(err)
s.Require().Equal(limit, query.DefaultLimit)
s.Require().Equal(page, 1)
s.T().Log("verify with custom values")
pageReq = &query.PageRequest{
Offset: 0,
Limit: 10,
}
page, limit, err = query.ParsePagination(pageReq)
s.Require().NoError(err)
s.Require().Equal(page, 1)
s.Require().Equal(limit, 10)
}
func (s *paginationTestSuite) TestPagination() {
app, ctx, _ := setupTest()
queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, app.BankKeeper)
queryClient := types.NewQueryClient(queryHelper)
var balances sdk.Coins
for i := 0; i < numBalances; i++ {
denom := fmt.Sprintf("foo%ddenom", i)
balances = append(balances, sdk.NewInt64Coin(denom, 100))
}
[Bank] Remove the unsafe balance changing API (#8473) * temp commit * setbalance now is internal * remove set balances in genesis * feedback test commit * update tests * fix: genesis panic message * fix not bonded pool * fix(staking): genesis test * fix(simapp): rollback state fix change * fix(staking): genesis large val set test * [Bank Refactor] Frojdi jonathan/remove setsupply (#8491) * init supply in a different way * remove external usage of set supply * change(staking): replace SetSupply with MintCoins in tests * change(evidence): replace SetSupply with MintCoins in tests * change(crisis): remove SetSupply in tests * change(bank): remove set supply from genesis tests * change(bank): remove set supply from keeper tests * change(bank): remove remaining set supply usage from keeper tests * change(bank): remove set supply usage from grpc query and querier tests * change(bank): remove SetSupply from keeper interface Co-authored-by: Frojdi Dymylja <frojdi.dymylja@gmail.com> * remove setbalances from genesis in gov * remove keyring * add init genesis state * change(staking): make genesis checks coherent and add tests * remove setbalances on distribution * fix(staking): genesis tests * [Bank Refactor]: Remove SetBalances usage from the code and tests (#8509) * change(distribution): remove SetBalances usage from keeper tests * add(simapp): FundAccount utility function * chore(staking): use FundAccount in keeper tests * change(staking): remove usage of SetBalance in allocation tests * change(staking): remove usage of SetBalance in delegation tests * change(staking): remove usage of SetBalance in proposal handler tests * change(staking): remove usage of SetBalances in grpc query tests * change(staking): remove usage of SetBalances in operations tests * change(distribution): remove usage of SetBalances in genesis * change(authz): remove usage of SetBalances keeper and operations test * fix(authz): TestKeeperFees failing test * change(slashing): remove SetBalances from expected BankKeeper * change(slashing): remove usage of SetBalances in tests * change(distribution): remove SetBalances from expected BankKeeper * change(genutil): remove usage of SetBalances from tests * change(gov): remove SetBalances from expected BankKeeper * change(gov): remove usage of SetBalances from tests * change(staking): remove usage of SetBalances from slash tests * change(staking): remove SetBalances from expected BankKeeper * change(staking): remove usage of SetBalances from delegation tests * change(staking): remove usage of SetBalances from operations tests * change(staking): remove usage of SetBalances from validator tests * change(bank): remove usage of SetBalances from app tests * change(bank): remove usage of SetBalances from bench tests * change(bank): remove usage of SetBalances from querier tests * change(bank): remove usage of SetBalances from grpc query tests * change(bank): remove usage of SetBalances from operations tests * change(bank): partially remove usage of SetBalances from keeper tests * change(bank): finalize removal of usage of SetBalances from keeper tests * change(auth): remove usage of SetBalances from verify tests * change(auth): partially remove usage of SetBalances from tests * [Bank refactor]: finalize removal of setbalances from auth (#8527) * add tests with is check tx * temp commit * fix test * fix other test and remove setbalances * change(auth): remove usage of SetBalances is vesting tests Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com> * change(types): remove usage of SetBalances in queries * fix(types): pagination tests * [Bank refactor] fix pagination tests (#8550) * fix tests * lint * change(bank): remove SetBalances from keeper public API Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com> Co-authored-by: SaReN <sahithnarahari@gmail.com> * change(bank): remove SubtractCoins from keeper public API * change(ibc/transfer): remove AddCoins from relay tests * change(bank): remove AddCoins from public keeper API * fix imports * remove set balances * fix fee test * remove set balances * fix(staking): remove dependency on minter authorization for staking pools * chore: update CHANGELOG.md * update: x/distribution/keeper/keeper_test.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update simapp/test_helpers.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update x/staking/genesis_test.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * fix(simapp): FundAccount amount variable name * fix some PR issues Co-authored-by: Frojdi Dymylja <frojdi.dymylja@gmail.com> Co-authored-by: Frojdi Dymylja <33157909+fdymylja@users.noreply.github.com> Co-authored-by: SaReN <sahithnarahari@gmail.com> Co-authored-by: Alessio Treglia <alessio@tendermint.com> Co-authored-by: Robert Zaremba <robert@zaremba.ch>
2021-02-17 10:20:33 -08:00
balances = balances.Sort()
Change `address` from bytes to bech32 strings (#7242) * init * Fix bank proto messages * missing conversions * remove casttype for addresses * Fix tests * Fix consaddress * more test fixes * Fix tests * fixed tests * migrate missing proto declarations * format * Fix format * Fix alignment * Fix more tests * Fix ibc merge issue * Fix fmt * Fix more tests * Fix missing address declarations * Fix staking tests * Fix more tests * Fix config * fixed tests * Fix more tests * Update staking grpc tests * Fix merge issue * fixed failing tests in x/distr * fixed sim tests * fixed failing tests * Fix bugs * Add logs * fixed slashing issue * Fix staking grpc tests * Fix all bank tests :) * Fix tests in distribution * Fix more tests in distr * Fix slashing tests * Fix statking tests * Fix evidence tests * Fix gov tests * Fix bug in create vesting account * Fix test * remove fmt * fixed gov tests * fixed x/ibc tests * fixed x/ibc-transfer tests * fixed staking tests * fixed staking tests * fixed test * fixed distribution issue * fix pagination test * fmt * lint * fix build * fix format * revert tally tests * revert tally tests * lint * Fix sim test * revert * revert * fixed tally issue * fix tests * revert * fmt * refactor * remove `GetAddress()` * remove fmt * revert fmt.Striger usage * Fix tests * Fix rest test * disable interfacer lint check * make proto-format * add nolint rule * remove stray println Co-authored-by: aleem1314 <aleem.md789@gmail.com> Co-authored-by: atheesh <atheesh@vitwit.com>
2020-09-25 03:25:37 -07:00
addr1 := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
app.AccountKeeper.SetAccount(ctx, acc1)
[Bank] Remove the unsafe balance changing API (#8473) * temp commit * setbalance now is internal * remove set balances in genesis * feedback test commit * update tests * fix: genesis panic message * fix not bonded pool * fix(staking): genesis test * fix(simapp): rollback state fix change * fix(staking): genesis large val set test * [Bank Refactor] Frojdi jonathan/remove setsupply (#8491) * init supply in a different way * remove external usage of set supply * change(staking): replace SetSupply with MintCoins in tests * change(evidence): replace SetSupply with MintCoins in tests * change(crisis): remove SetSupply in tests * change(bank): remove set supply from genesis tests * change(bank): remove set supply from keeper tests * change(bank): remove remaining set supply usage from keeper tests * change(bank): remove set supply usage from grpc query and querier tests * change(bank): remove SetSupply from keeper interface Co-authored-by: Frojdi Dymylja <frojdi.dymylja@gmail.com> * remove setbalances from genesis in gov * remove keyring * add init genesis state * change(staking): make genesis checks coherent and add tests * remove setbalances on distribution * fix(staking): genesis tests * [Bank Refactor]: Remove SetBalances usage from the code and tests (#8509) * change(distribution): remove SetBalances usage from keeper tests * add(simapp): FundAccount utility function * chore(staking): use FundAccount in keeper tests * change(staking): remove usage of SetBalance in allocation tests * change(staking): remove usage of SetBalance in delegation tests * change(staking): remove usage of SetBalance in proposal handler tests * change(staking): remove usage of SetBalances in grpc query tests * change(staking): remove usage of SetBalances in operations tests * change(distribution): remove usage of SetBalances in genesis * change(authz): remove usage of SetBalances keeper and operations test * fix(authz): TestKeeperFees failing test * change(slashing): remove SetBalances from expected BankKeeper * change(slashing): remove usage of SetBalances in tests * change(distribution): remove SetBalances from expected BankKeeper * change(genutil): remove usage of SetBalances from tests * change(gov): remove SetBalances from expected BankKeeper * change(gov): remove usage of SetBalances from tests * change(staking): remove usage of SetBalances from slash tests * change(staking): remove SetBalances from expected BankKeeper * change(staking): remove usage of SetBalances from delegation tests * change(staking): remove usage of SetBalances from operations tests * change(staking): remove usage of SetBalances from validator tests * change(bank): remove usage of SetBalances from app tests * change(bank): remove usage of SetBalances from bench tests * change(bank): remove usage of SetBalances from querier tests * change(bank): remove usage of SetBalances from grpc query tests * change(bank): remove usage of SetBalances from operations tests * change(bank): partially remove usage of SetBalances from keeper tests * change(bank): finalize removal of usage of SetBalances from keeper tests * change(auth): remove usage of SetBalances from verify tests * change(auth): partially remove usage of SetBalances from tests * [Bank refactor]: finalize removal of setbalances from auth (#8527) * add tests with is check tx * temp commit * fix test * fix other test and remove setbalances * change(auth): remove usage of SetBalances is vesting tests Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com> * change(types): remove usage of SetBalances in queries * fix(types): pagination tests * [Bank refactor] fix pagination tests (#8550) * fix tests * lint * change(bank): remove SetBalances from keeper public API Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com> Co-authored-by: SaReN <sahithnarahari@gmail.com> * change(bank): remove SubtractCoins from keeper public API * change(ibc/transfer): remove AddCoins from relay tests * change(bank): remove AddCoins from public keeper API * fix imports * remove set balances * fix fee test * remove set balances * fix(staking): remove dependency on minter authorization for staking pools * chore: update CHANGELOG.md * update: x/distribution/keeper/keeper_test.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update simapp/test_helpers.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update x/staking/genesis_test.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * fix(simapp): FundAccount amount variable name * fix some PR issues Co-authored-by: Frojdi Dymylja <frojdi.dymylja@gmail.com> Co-authored-by: Frojdi Dymylja <33157909+fdymylja@users.noreply.github.com> Co-authored-by: SaReN <sahithnarahari@gmail.com> Co-authored-by: Alessio Treglia <alessio@tendermint.com> Co-authored-by: Robert Zaremba <robert@zaremba.ch>
2021-02-17 10:20:33 -08:00
s.Require().NoError(simapp.FundAccount(app, ctx, addr1, balances))
s.T().Log("verify empty page request results a max of defaultLimit records and counts total records")
pageReq := &query.PageRequest{}
request := types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err := queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().Equal(res.Pagination.Total, uint64(numBalances))
s.Require().NotNil(res.Pagination.NextKey)
s.Require().LessOrEqual(res.Balances.Len(), defaultLimit)
s.T().Log("verify page request with limit > defaultLimit, returns less or equal to `limit` records")
pageReq = &query.PageRequest{Limit: overLimit}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().Equal(res.Pagination.Total, uint64(0))
s.Require().NotNil(res.Pagination.NextKey)
s.Require().LessOrEqual(res.Balances.Len(), overLimit)
s.T().Log("verify paginate with custom limit and countTotal true")
pageReq = &query.PageRequest{Limit: underLimit, CountTotal: true}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().Equal(res.Balances.Len(), underLimit)
s.Require().NotNil(res.Pagination.NextKey)
s.Require().Equal(res.Pagination.Total, uint64(numBalances))
s.T().Log("verify paginate with custom limit and countTotal false")
pageReq = &query.PageRequest{Limit: defaultLimit, CountTotal: false}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().Equal(res.Balances.Len(), defaultLimit)
s.Require().NotNil(res.Pagination.NextKey)
s.Require().Equal(res.Pagination.Total, uint64(0))
s.T().Log("verify paginate with custom limit, key and countTotal false")
pageReq = &query.PageRequest{Key: res.Pagination.NextKey, Limit: defaultLimit, CountTotal: false}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().Equal(res.Balances.Len(), defaultLimit)
s.Require().NotNil(res.Pagination.NextKey)
s.Require().Equal(res.Pagination.Total, uint64(0))
s.T().Log("verify paginate for last page, results in records less than max limit")
pageReq = &query.PageRequest{Key: res.Pagination.NextKey, Limit: defaultLimit, CountTotal: false}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().LessOrEqual(res.Balances.Len(), defaultLimit)
s.Require().Equal(res.Balances.Len(), lastPageRecords)
s.Require().Nil(res.Pagination.NextKey)
s.Require().Equal(res.Pagination.Total, uint64(0))
s.T().Log("verify paginate with offset and limit")
pageReq = &query.PageRequest{Offset: 200, Limit: defaultLimit, CountTotal: false}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().LessOrEqual(res.Balances.Len(), defaultLimit)
s.Require().Equal(res.Balances.Len(), lastPageRecords)
s.Require().Nil(res.Pagination.NextKey)
s.Require().Equal(res.Pagination.Total, uint64(0))
s.T().Log("verify paginate with offset and limit")
pageReq = &query.PageRequest{Offset: 100, Limit: defaultLimit, CountTotal: false}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().LessOrEqual(res.Balances.Len(), defaultLimit)
s.Require().NotNil(res.Pagination.NextKey)
s.Require().Equal(res.Pagination.Total, uint64(0))
s.T().Log("verify paginate with offset and key - error")
pageReq = &query.PageRequest{Key: res.Pagination.NextKey, Offset: 100, Limit: defaultLimit, CountTotal: false}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
s.Require().Error(err)
s.Require().Equal("rpc error: code = InvalidArgument desc = paginate: invalid request, either offset or key is expected, got both", err.Error())
s.T().Log("verify paginate with offset greater than total results")
pageReq = &query.PageRequest{Offset: 300, Limit: defaultLimit, CountTotal: false}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().LessOrEqual(res.Balances.Len(), 0)
s.Require().Nil(res.Pagination.NextKey)
}
func (s *paginationTestSuite) TestReversePagination() {
app, ctx, _ := setupTest()
queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, app.BankKeeper)
queryClient := types.NewQueryClient(queryHelper)
var balances sdk.Coins
for i := 0; i < numBalances; i++ {
denom := fmt.Sprintf("foo%ddenom", i)
balances = append(balances, sdk.NewInt64Coin(denom, 100))
}
balances = balances.Sort()
addr1 := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
app.AccountKeeper.SetAccount(ctx, acc1)
s.Require().NoError(simapp.FundAccount(app, ctx, addr1, balances))
s.T().Log("verify paginate with custom limit and countTotal, Reverse false")
pageReq := &query.PageRequest{Limit: 2, CountTotal: true, Reverse: true, Key: nil}
request := types.NewQueryAllBalancesRequest(addr1, pageReq)
res1, err := queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().Equal(res1.Balances.Len(), 2)
s.Require().NotNil(res1.Pagination.NextKey)
s.T().Log("verify paginate with custom limit and countTotal, Reverse false")
pageReq = &query.PageRequest{Limit: 150}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res1, err = queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().Equal(res1.Balances.Len(), 150)
s.Require().NotNil(res1.Pagination.NextKey)
s.Require().Equal(res1.Pagination.Total, uint64(0))
s.T().Log("verify paginate with custom limit, key and Reverse true")
pageReq = &query.PageRequest{Limit: defaultLimit, Reverse: true}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err := queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().Equal(res.Balances.Len(), defaultLimit)
s.Require().NotNil(res.Pagination.NextKey)
s.Require().Equal(res.Pagination.Total, uint64(0))
s.T().Log("verify paginate with custom limit, key and Reverse true")
pageReq = &query.PageRequest{Offset: 100, Limit: defaultLimit, Reverse: true}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().Equal(res.Balances.Len(), defaultLimit)
s.Require().NotNil(res.Pagination.NextKey)
s.Require().Equal(res.Pagination.Total, uint64(0))
s.T().Log("verify paginate for last page, Reverse true")
pageReq = &query.PageRequest{Offset: 200, Limit: defaultLimit, Reverse: true}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().Equal(res.Balances.Len(), lastPageRecords)
s.Require().Nil(res.Pagination.NextKey)
s.Require().Equal(res.Pagination.Total, uint64(0))
s.T().Log("verify page request with limit > defaultLimit, returns less or equal to `limit` records")
pageReq = &query.PageRequest{Limit: overLimit, Reverse: true}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().Equal(res.Pagination.Total, uint64(0))
s.Require().NotNil(res.Pagination.NextKey)
s.Require().LessOrEqual(res.Balances.Len(), overLimit)
s.T().Log("verify paginate with custom limit, key, countTotal false and Reverse true")
pageReq = &query.PageRequest{Key: res1.Pagination.NextKey, Limit: 50, Reverse: true}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().Equal(res.Balances.Len(), 50)
s.Require().NotNil(res.Pagination.NextKey)
s.Require().Equal(res.Pagination.Total, uint64(0))
s.T().Log("verify Reverse pagination returns valid result")
s.Require().Equal(balances[101:151].String(), res.Balances.Sort().String())
s.T().Log("verify paginate with custom limit, key, countTotal false and Reverse true")
pageReq = &query.PageRequest{Key: res.Pagination.NextKey, Limit: 50, Reverse: true}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().Equal(res.Balances.Len(), 50)
s.Require().NotNil(res.Pagination.NextKey)
s.Require().Equal(res.Pagination.Total, uint64(0))
s.T().Log("verify Reverse pagination returns valid result")
s.Require().Equal(balances[51:101].String(), res.Balances.Sort().String())
s.T().Log("verify paginate for last page Reverse true")
pageReq = &query.PageRequest{Key: res.Pagination.NextKey, Limit: defaultLimit, Reverse: true}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().Equal(res.Balances.Len(), 51)
s.Require().Nil(res.Pagination.NextKey)
s.Require().Equal(res.Pagination.Total, uint64(0))
s.T().Log("verify Reverse pagination returns valid result")
s.Require().Equal(balances[0:51].String(), res.Balances.Sort().String())
s.T().Log("verify paginate with offset and key - error")
pageReq = &query.PageRequest{Key: res1.Pagination.NextKey, Offset: 100, Limit: defaultLimit, CountTotal: false}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
s.Require().Error(err)
s.Require().Equal("rpc error: code = InvalidArgument desc = paginate: invalid request, either offset or key is expected, got both", err.Error())
s.T().Log("verify paginate with offset greater than total results")
pageReq = &query.PageRequest{Offset: 300, Limit: defaultLimit, CountTotal: false, Reverse: true}
request = types.NewQueryAllBalancesRequest(addr1, pageReq)
res, err = queryClient.AllBalances(gocontext.Background(), request)
s.Require().NoError(err)
s.Require().LessOrEqual(res.Balances.Len(), 0)
s.Require().Nil(res.Pagination.NextKey)
}
func ExamplePaginate() {
app, ctx, _ := setupTest()
var balances sdk.Coins
for i := 0; i < 2; i++ {
denom := fmt.Sprintf("foo%ddenom", i)
balances = append(balances, sdk.NewInt64Coin(denom, 100))
}
[Bank] Remove the unsafe balance changing API (#8473) * temp commit * setbalance now is internal * remove set balances in genesis * feedback test commit * update tests * fix: genesis panic message * fix not bonded pool * fix(staking): genesis test * fix(simapp): rollback state fix change * fix(staking): genesis large val set test * [Bank Refactor] Frojdi jonathan/remove setsupply (#8491) * init supply in a different way * remove external usage of set supply * change(staking): replace SetSupply with MintCoins in tests * change(evidence): replace SetSupply with MintCoins in tests * change(crisis): remove SetSupply in tests * change(bank): remove set supply from genesis tests * change(bank): remove set supply from keeper tests * change(bank): remove remaining set supply usage from keeper tests * change(bank): remove set supply usage from grpc query and querier tests * change(bank): remove SetSupply from keeper interface Co-authored-by: Frojdi Dymylja <frojdi.dymylja@gmail.com> * remove setbalances from genesis in gov * remove keyring * add init genesis state * change(staking): make genesis checks coherent and add tests * remove setbalances on distribution * fix(staking): genesis tests * [Bank Refactor]: Remove SetBalances usage from the code and tests (#8509) * change(distribution): remove SetBalances usage from keeper tests * add(simapp): FundAccount utility function * chore(staking): use FundAccount in keeper tests * change(staking): remove usage of SetBalance in allocation tests * change(staking): remove usage of SetBalance in delegation tests * change(staking): remove usage of SetBalance in proposal handler tests * change(staking): remove usage of SetBalances in grpc query tests * change(staking): remove usage of SetBalances in operations tests * change(distribution): remove usage of SetBalances in genesis * change(authz): remove usage of SetBalances keeper and operations test * fix(authz): TestKeeperFees failing test * change(slashing): remove SetBalances from expected BankKeeper * change(slashing): remove usage of SetBalances in tests * change(distribution): remove SetBalances from expected BankKeeper * change(genutil): remove usage of SetBalances from tests * change(gov): remove SetBalances from expected BankKeeper * change(gov): remove usage of SetBalances from tests * change(staking): remove usage of SetBalances from slash tests * change(staking): remove SetBalances from expected BankKeeper * change(staking): remove usage of SetBalances from delegation tests * change(staking): remove usage of SetBalances from operations tests * change(staking): remove usage of SetBalances from validator tests * change(bank): remove usage of SetBalances from app tests * change(bank): remove usage of SetBalances from bench tests * change(bank): remove usage of SetBalances from querier tests * change(bank): remove usage of SetBalances from grpc query tests * change(bank): remove usage of SetBalances from operations tests * change(bank): partially remove usage of SetBalances from keeper tests * change(bank): finalize removal of usage of SetBalances from keeper tests * change(auth): remove usage of SetBalances from verify tests * change(auth): partially remove usage of SetBalances from tests * [Bank refactor]: finalize removal of setbalances from auth (#8527) * add tests with is check tx * temp commit * fix test * fix other test and remove setbalances * change(auth): remove usage of SetBalances is vesting tests Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com> * change(types): remove usage of SetBalances in queries * fix(types): pagination tests * [Bank refactor] fix pagination tests (#8550) * fix tests * lint * change(bank): remove SetBalances from keeper public API Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com> Co-authored-by: SaReN <sahithnarahari@gmail.com> * change(bank): remove SubtractCoins from keeper public API * change(ibc/transfer): remove AddCoins from relay tests * change(bank): remove AddCoins from public keeper API * fix imports * remove set balances * fix fee test * remove set balances * fix(staking): remove dependency on minter authorization for staking pools * chore: update CHANGELOG.md * update: x/distribution/keeper/keeper_test.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update simapp/test_helpers.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update x/staking/genesis_test.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * fix(simapp): FundAccount amount variable name * fix some PR issues Co-authored-by: Frojdi Dymylja <frojdi.dymylja@gmail.com> Co-authored-by: Frojdi Dymylja <33157909+fdymylja@users.noreply.github.com> Co-authored-by: SaReN <sahithnarahari@gmail.com> Co-authored-by: Alessio Treglia <alessio@tendermint.com> Co-authored-by: Robert Zaremba <robert@zaremba.ch>
2021-02-17 10:20:33 -08:00
balances = balances.Sort()
addr1 := sdk.AccAddress([]byte("addr1"))
acc1 := app.AccountKeeper.NewAccountWithAddress(ctx, addr1)
app.AccountKeeper.SetAccount(ctx, acc1)
[Bank] Remove the unsafe balance changing API (#8473) * temp commit * setbalance now is internal * remove set balances in genesis * feedback test commit * update tests * fix: genesis panic message * fix not bonded pool * fix(staking): genesis test * fix(simapp): rollback state fix change * fix(staking): genesis large val set test * [Bank Refactor] Frojdi jonathan/remove setsupply (#8491) * init supply in a different way * remove external usage of set supply * change(staking): replace SetSupply with MintCoins in tests * change(evidence): replace SetSupply with MintCoins in tests * change(crisis): remove SetSupply in tests * change(bank): remove set supply from genesis tests * change(bank): remove set supply from keeper tests * change(bank): remove remaining set supply usage from keeper tests * change(bank): remove set supply usage from grpc query and querier tests * change(bank): remove SetSupply from keeper interface Co-authored-by: Frojdi Dymylja <frojdi.dymylja@gmail.com> * remove setbalances from genesis in gov * remove keyring * add init genesis state * change(staking): make genesis checks coherent and add tests * remove setbalances on distribution * fix(staking): genesis tests * [Bank Refactor]: Remove SetBalances usage from the code and tests (#8509) * change(distribution): remove SetBalances usage from keeper tests * add(simapp): FundAccount utility function * chore(staking): use FundAccount in keeper tests * change(staking): remove usage of SetBalance in allocation tests * change(staking): remove usage of SetBalance in delegation tests * change(staking): remove usage of SetBalance in proposal handler tests * change(staking): remove usage of SetBalances in grpc query tests * change(staking): remove usage of SetBalances in operations tests * change(distribution): remove usage of SetBalances in genesis * change(authz): remove usage of SetBalances keeper and operations test * fix(authz): TestKeeperFees failing test * change(slashing): remove SetBalances from expected BankKeeper * change(slashing): remove usage of SetBalances in tests * change(distribution): remove SetBalances from expected BankKeeper * change(genutil): remove usage of SetBalances from tests * change(gov): remove SetBalances from expected BankKeeper * change(gov): remove usage of SetBalances from tests * change(staking): remove usage of SetBalances from slash tests * change(staking): remove SetBalances from expected BankKeeper * change(staking): remove usage of SetBalances from delegation tests * change(staking): remove usage of SetBalances from operations tests * change(staking): remove usage of SetBalances from validator tests * change(bank): remove usage of SetBalances from app tests * change(bank): remove usage of SetBalances from bench tests * change(bank): remove usage of SetBalances from querier tests * change(bank): remove usage of SetBalances from grpc query tests * change(bank): remove usage of SetBalances from operations tests * change(bank): partially remove usage of SetBalances from keeper tests * change(bank): finalize removal of usage of SetBalances from keeper tests * change(auth): remove usage of SetBalances from verify tests * change(auth): partially remove usage of SetBalances from tests * [Bank refactor]: finalize removal of setbalances from auth (#8527) * add tests with is check tx * temp commit * fix test * fix other test and remove setbalances * change(auth): remove usage of SetBalances is vesting tests Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com> * change(types): remove usage of SetBalances in queries * fix(types): pagination tests * [Bank refactor] fix pagination tests (#8550) * fix tests * lint * change(bank): remove SetBalances from keeper public API Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com> Co-authored-by: SaReN <sahithnarahari@gmail.com> * change(bank): remove SubtractCoins from keeper public API * change(ibc/transfer): remove AddCoins from relay tests * change(bank): remove AddCoins from public keeper API * fix imports * remove set balances * fix fee test * remove set balances * fix(staking): remove dependency on minter authorization for staking pools * chore: update CHANGELOG.md * update: x/distribution/keeper/keeper_test.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update simapp/test_helpers.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * Update x/staking/genesis_test.go Co-authored-by: Robert Zaremba <robert@zaremba.ch> * fix(simapp): FundAccount amount variable name * fix some PR issues Co-authored-by: Frojdi Dymylja <frojdi.dymylja@gmail.com> Co-authored-by: Frojdi Dymylja <33157909+fdymylja@users.noreply.github.com> Co-authored-by: SaReN <sahithnarahari@gmail.com> Co-authored-by: Alessio Treglia <alessio@tendermint.com> Co-authored-by: Robert Zaremba <robert@zaremba.ch>
2021-02-17 10:20:33 -08:00
err := simapp.FundAccount(app, ctx, addr1, balances)
if err != nil { // should return no error
fmt.Println(err)
}
// Paginate example
pageReq := &query.PageRequest{Key: nil, Limit: 1, CountTotal: true}
request := types.NewQueryAllBalancesRequest(addr1, pageReq)
balResult := sdk.NewCoins()
2021-02-11 01:49:26 -08:00
authStore := ctx.KVStore(app.GetKey(types.StoreKey))
balancesStore := prefix.NewStore(authStore, types.BalancesPrefix)
accountStore := prefix.NewStore(balancesStore, address.MustLengthPrefix(addr1))
pageRes, err := query.Paginate(accountStore, request.Pagination, func(key []byte, value []byte) error {
var tempRes sdk.Coin
err := app.AppCodec().UnmarshalBinaryBare(value, &tempRes)
if err != nil {
return err
}
balResult = append(balResult, tempRes)
return nil
})
if err != nil { // should return no error
fmt.Println(err)
}
fmt.Println(&types.QueryAllBalancesResponse{Balances: balResult, Pagination: pageRes})
// Output:
// balances:<denom:"foo0denom" amount:"100" > pagination:<next_key:"foo1denom" total:2 >
}
func setupTest() (*simapp.SimApp, sdk.Context, codec.Marshaler) {
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{Height: 1})
appCodec := app.AppCodec()
db := dbm.NewMemDB()
ms := store.NewCommitMultiStore(db)
ms.LoadLatestVersion()
return app, ctx, appCodec
}