Skip to content
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

Paginate supply queries #8798

Merged
merged 31 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
653ce30
paginate grpc query
sahith-narahari Mar 5, 2021
baef976
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/pag…
sahith-narahari Mar 5, 2021
38aae39
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/pag…
sahith-narahari Mar 19, 2021
9232a85
fix lint
sahith-narahari Mar 19, 2021
97c9c44
update tests
sahith-narahari Mar 19, 2021
e352f58
remove GetTotalSupply
sahith-narahari Mar 19, 2021
5efecf3
Merge branch 'master' into sahith/paginate-supply
sahith-narahari Mar 19, 2021
5340037
fix test
sahith-narahari Mar 19, 2021
02c1251
Merge branch 'master' into sahith/paginate-supply
sahith-narahari Mar 23, 2021
4806e3d
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/pag…
sahith-narahari Mar 25, 2021
964d48a
remove GetTotalSupply
sahith-narahari Mar 25, 2021
5c5ec5e
add changelog
sahith-narahari Mar 26, 2021
c806d8b
update changelog
sahith-narahari Mar 26, 2021
6a4ed84
Merge branch 'master' into sahith/paginate-supply
sahith-narahari Mar 26, 2021
001e565
update rosetta data
sahith-narahari Mar 26, 2021
1509da8
Merge branch 'sahith/paginate-supply' of github.com:cosmos/cosmos-sdk…
sahith-narahari Mar 26, 2021
c80850f
Merge branch 'master' into sahith/paginate-supply
sahith-narahari Mar 26, 2021
7c18246
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/pag…
sahith-narahari Mar 29, 2021
77e4022
Merge branch 'master' into sahith/paginate-supply
tac0turtle Mar 29, 2021
adf4c4a
Merge branch 'sahith/paginate-supply' of github.com:cosmos/cosmos-sdk…
sahith-narahari Apr 3, 2021
36c9e6f
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/pag…
sahith-narahari Apr 3, 2021
3a0607d
update limit
sahith-narahari Apr 3, 2021
ba44b06
update genesis
sahith-narahari Apr 3, 2021
d125062
add max limit to query
sahith-narahari Apr 5, 2021
ec14729
fix lint
sahith-narahari Apr 5, 2021
ce1e5ef
go imports
sahith-narahari Apr 5, 2021
a228af9
Update types/query/pagination.go
sahith-narahari Apr 5, 2021
66ca8ab
Merge branch 'master' into sahith/paginate-supply
sahith-narahari Apr 5, 2021
8c9f665
Merge branch 'master' into sahith/paginate-supply
sahith-narahari Apr 6, 2021
d89cfc0
Merge branch 'master' of github.com:cosmos/cosmos-sdk into sahith/pag…
sahith-narahari Apr 6, 2021
b64c77b
update supply
sahith-narahari Apr 6, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* `MsgCreateValidator.Pubkey` type changed from `string` to `codectypes.Any`.
* (client) [\#8926](https://github.com/cosmos/cosmos-sdk/pull/8926) `client/tx.PrepareFactory` has been converted to a private function, as it's only used internally.
* (auth/tx) [\#8926](https://github.com/cosmos/cosmos-sdk/pull/8926) The `ProtoTxProvider` interface used as a workaround for transaction simulation has been removed.
* (x/bank) [\#8798](https://github.com/cosmos/cosmos-sdk/pull/8798) `GetTotalSupply` is removed in favour of `GetPaginatedTotalSupply`

### State Machine Breaking

Expand Down
2 changes: 1 addition & 1 deletion contrib/rosetta/configuration/bootstrap.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"account_identifier": {
"address":"cosmos1gykh2dsytj0lde8wr9msl9xd2nyj88duvmsnn7"
"address":"cosmos1trf09zvqeuc722zyeg34pra8qaqll9ddw0qy8q"
},
"currency":{
"symbol":"stake",
Expand Down
Binary file modified contrib/rosetta/node/data.tar.gz
Binary file not shown.
11 changes: 10 additions & 1 deletion proto/cosmos/bank/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,23 @@ message QueryAllBalancesResponse {

// QueryTotalSupplyRequest is the request type for the Query/TotalSupply RPC
// method.
message QueryTotalSupplyRequest {}
message QueryTotalSupplyRequest {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

// pagination defines an optional pagination for the request.
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

// QueryTotalSupplyResponse is the response type for the Query/TotalSupply RPC
// method
message QueryTotalSupplyResponse {
// supply is the supply of the coins
repeated cosmos.base.v1beta1.Coin supply = 1
[(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"];

// pagination defines the pagination in the response.
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

// QuerySupplyOfRequest is the request type for the Query/SupplyOf RPC method.
Expand Down
5 changes: 5 additions & 0 deletions types/query/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package query

import (
"fmt"
"math"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -14,6 +15,10 @@ import (
// if the `limit` is not supplied, paginate will use `DefaultLimit`
const DefaultLimit = 100

// MaxLimit is the maximum limit the paginate function can handle
// which equals the maximum value that can be stored in uint64
const MaxLimit = math.MaxUint64

// ParsePagination validate PageRequest and returns page number & limit.
func ParsePagination(pageReq *PageRequest) (page, limit int, err error) {
offset := 0
Expand Down
4 changes: 3 additions & 1 deletion x/bank/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ func (s *IntegrationTestSuite) TestGetCmdQueryTotalSupply() {
Supply: sdk.NewCoins(
sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), s.cfg.AccountTokens),
sdk.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Add(sdk.NewInt(10))),
)},
),
Pagination: &query.PageResponse{Total: 2},
},
},
{
name: "total supply of a specific denomination",
Expand Down
7 changes: 6 additions & 1 deletion x/bank/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,13 @@ To query for the total supply of a specific coin denomination use:

queryClient := types.NewQueryClient(clientCtx)
ctx := cmd.Context()

pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
if denom == "" {
res, err := queryClient.TotalSupply(ctx, &types.QueryTotalSupplyRequest{})
res, err := queryClient.TotalSupply(ctx, &types.QueryTotalSupplyRequest{Pagination: pageReq})
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions x/bank/client/rest/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func (s *IntegrationTestSuite) TestTotalSupplyGRPCHandler() {
sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), s.cfg.AccountTokens),
sdk.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Add(sdk.NewInt(10))),
),
Pagination: &query.PageResponse{
Total: 2,
},
},
},
{
Expand Down
14 changes: 9 additions & 5 deletions x/bank/client/rest/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)
Expand Down Expand Up @@ -155,11 +156,14 @@ func (s *IntegrationTestSuite) TestTotalSupplyHandlerFn() {
{
"total supply",
fmt.Sprintf("%s/bank/total?height=1", baseURL),
&sdk.Coins{},
sdk.NewCoins(
sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), s.cfg.AccountTokens),
sdk.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Add(sdk.NewInt(10))),
),
&types.QueryTotalSupplyResponse{},
&types.QueryTotalSupplyResponse{
Supply: sdk.NewCoins(
sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), s.cfg.AccountTokens),
sdk.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Add(sdk.NewInt(10))),
),
Pagination: &query.PageResponse{Total: 2},
},
},
{
"total supply of a specific denom",
Expand Down
8 changes: 7 additions & 1 deletion x/bank/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)

Expand Down Expand Up @@ -42,10 +43,15 @@ func (k BaseKeeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) {

// ExportGenesis returns the bank module's genesis state.
func (k BaseKeeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
totalSupply, _, err := k.GetPaginatedTotalSupply(ctx, &query.PageRequest{Limit: query.MaxLimit})
if err != nil {
panic(fmt.Errorf("unable to fetch total supply %v", err))
}

return types.NewGenesisState(
k.GetParams(ctx),
k.GetAccountsBalances(ctx),
k.GetTotalSupply(ctx),
totalSupply,
k.GetAllDenomMetaData(ctx),
)
}
5 changes: 4 additions & 1 deletion x/bank/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper_test

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/bank/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
)
Expand Down Expand Up @@ -106,7 +107,9 @@ func (suite *IntegrationTestSuite) TestTotalSupply() {
suite.PanicsWithError(tc.expPanicMsg, func() { suite.app.BankKeeper.InitGenesis(suite.ctx, tc.genesis) })
} else {
suite.app.BankKeeper.InitGenesis(suite.ctx, tc.genesis)
suite.Require().Equal(tc.expSupply, suite.app.BankKeeper.GetTotalSupply(suite.ctx))
totalSupply, _, err := suite.app.BankKeeper.GetPaginatedTotalSupply(suite.ctx, &query.PageRequest{Limit: query.MaxLimit})
suite.Require().NoError(err)
suite.Require().Equal(tc.expSupply, totalSupply)
}
})
}
Expand Down
9 changes: 6 additions & 3 deletions x/bank/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,14 @@ func (k BaseKeeper) AllBalances(ctx context.Context, req *types.QueryAllBalances
}

// TotalSupply implements the Query/TotalSupply gRPC method
func (k BaseKeeper) TotalSupply(ctx context.Context, _ *types.QueryTotalSupplyRequest) (*types.QueryTotalSupplyResponse, error) {
func (k BaseKeeper) TotalSupply(ctx context.Context, req *types.QueryTotalSupplyRequest) (*types.QueryTotalSupplyResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
totalSupply := k.GetTotalSupply(sdkCtx)
totalSupply, pageRes, err := k.GetPaginatedTotalSupply(sdkCtx, req.Pagination)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &types.QueryTotalSupplyResponse{Supply: totalSupply}, nil
return &types.QueryTotalSupplyResponse{Supply: totalSupply, Pagination: pageRes}, nil
}

// SupplyOf implements the Query/SupplyOf gRPC method
Expand Down
8 changes: 7 additions & 1 deletion x/bank/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)

Expand Down Expand Up @@ -50,7 +51,12 @@ func NonnegativeBalanceInvariant(k ViewKeeper) sdk.Invariant {
func TotalSupply(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
expectedTotal := sdk.Coins{}
supply := k.GetTotalSupply(ctx)
supply, _, err := k.GetPaginatedTotalSupply(ctx, &query.PageRequest{Limit: query.MaxLimit})

if err != nil {
return sdk.FormatInvariant(types.ModuleName, "query supply",
fmt.Sprintf("error querying total supply %v", err)), false
}

k.IterateAllBalances(ctx, func(_ sdk.AccAddress, balance sdk.Coin) bool {
expectedTotal = expectedTotal.Add(balance)
Expand Down
29 changes: 21 additions & 8 deletions x/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
vestexported "github.com/cosmos/cosmos-sdk/x/auth/vesting/exported"
"github.com/cosmos/cosmos-sdk/x/bank/types"
Expand All @@ -24,9 +25,8 @@ type Keeper interface {
ExportGenesis(sdk.Context) *types.GenesisState

GetSupply(ctx sdk.Context, denom string) sdk.Coin
GetTotalSupply(ctx sdk.Context) sdk.Coins
GetPaginatedTotalSupply(ctx sdk.Context, pagination *query.PageRequest) (sdk.Coins, *query.PageResponse, error)
IterateTotalSupply(ctx sdk.Context, cb func(sdk.Coin) bool)

GetDenomMetaData(ctx sdk.Context, denom string) (types.Metadata, bool)
SetDenomMetaData(ctx sdk.Context, denomMetaData types.Metadata)
IterateAllDenomMetaData(ctx sdk.Context, cb func(types.Metadata) bool)
Expand Down Expand Up @@ -55,14 +55,27 @@ type BaseKeeper struct {
paramSpace paramtypes.Subspace
}

func (k BaseKeeper) GetTotalSupply(ctx sdk.Context) sdk.Coins {
balances := sdk.NewCoins()
k.IterateTotalSupply(ctx, func(balance sdk.Coin) bool {
balances = balances.Add(balance)
return false
func (k BaseKeeper) GetPaginatedTotalSupply(ctx sdk.Context, pagination *query.PageRequest) (sdk.Coins, *query.PageResponse, error) {
store := ctx.KVStore(k.storeKey)
supplyStore := prefix.NewStore(store, types.SupplyKey)

supply := sdk.NewCoins()

pageRes, err := query.Paginate(supplyStore, pagination, func(key, value []byte) error {
var amount sdk.Int
err := amount.Unmarshal(value)
if err != nil {
return fmt.Errorf("unable to convert amount string to Int %v", err)
}
supply = append(supply, sdk.NewCoin(string(key), amount))
return nil
})

return balances.Sort()
if err != nil {
return nil, nil, err
}

return supply, pageRes, nil
}

// NewBaseKeeper returns a new BaseKeeper object with a given codec, dedicated
Expand Down
36 changes: 25 additions & 11 deletions x/bank/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"github.com/cosmos/cosmos-sdk/types/query"
"testing"
"time"

Expand Down Expand Up @@ -93,7 +94,8 @@ func (suite *IntegrationTestSuite) TestSupply() {
totalSupply := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initTokens))
suite.NoError(app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, totalSupply))

total := app.BankKeeper.GetTotalSupply(ctx)
total, _, err := app.BankKeeper.GetPaginatedTotalSupply(ctx, &query.PageRequest{})
suite.Require().NoError(err)
suite.Require().Equal(totalSupply, total)
}

Expand Down Expand Up @@ -224,12 +226,13 @@ func (suite *IntegrationTestSuite) TestSupply_MintCoins() {
authKeeper.SetModuleAccount(ctx, multiPermAcc)
authKeeper.SetModuleAccount(ctx, randomPermAcc)

initialSupply := keeper.GetTotalSupply(ctx)
initialSupply, _, err := keeper.GetPaginatedTotalSupply(ctx, &query.PageRequest{})
suite.Require().NoError(err)

suite.Require().Panics(func() { keeper.MintCoins(ctx, "", initCoins) }, "no module account") // nolint:errcheck
suite.Require().Panics(func() { keeper.MintCoins(ctx, authtypes.Burner, initCoins) }, "invalid permission") // nolint:errcheck

err := keeper.MintCoins(ctx, authtypes.Minter, sdk.Coins{sdk.Coin{Denom: "denom", Amount: sdk.NewInt(-10)}})
err = keeper.MintCoins(ctx, authtypes.Minter, sdk.Coins{sdk.Coin{Denom: "denom", Amount: sdk.NewInt(-10)}})
suite.Require().Error(err, "insufficient coins")

suite.Require().Panics(func() { keeper.MintCoins(ctx, randomPerm, initCoins) }) // nolint:errcheck
Expand All @@ -238,16 +241,22 @@ func (suite *IntegrationTestSuite) TestSupply_MintCoins() {
suite.Require().NoError(err)

suite.Require().Equal(initCoins, getCoinsByName(ctx, keeper, authKeeper, authtypes.Minter))
suite.Require().Equal(initialSupply.Add(initCoins...), keeper.GetTotalSupply(ctx))
totalSupply, _, err := keeper.GetPaginatedTotalSupply(ctx, &query.PageRequest{})
suite.Require().NoError(err)

suite.Require().Equal(initialSupply.Add(initCoins...), totalSupply)

// test same functionality on module account with multiple permissions
initialSupply = keeper.GetTotalSupply(ctx)
initialSupply, _, err = keeper.GetPaginatedTotalSupply(ctx, &query.PageRequest{})
suite.Require().NoError(err)

err = keeper.MintCoins(ctx, multiPermAcc.GetName(), initCoins)
suite.Require().NoError(err)

totalSupply, _, err = keeper.GetPaginatedTotalSupply(ctx, &query.PageRequest{})
suite.Require().NoError(err)
suite.Require().Equal(initCoins, getCoinsByName(ctx, keeper, authKeeper, multiPermAcc.GetName()))
suite.Require().Equal(initialSupply.Add(initCoins...), keeper.GetTotalSupply(ctx))
suite.Require().Equal(initialSupply.Add(initCoins...), totalSupply)
suite.Require().Panics(func() { keeper.MintCoins(ctx, authtypes.Burner, initCoins) }) // nolint:errcheck
}

Expand Down Expand Up @@ -286,32 +295,37 @@ func (suite *IntegrationTestSuite) TestSupply_BurnCoins() {
suite.
Require().
NoError(keeper.MintCoins(ctx, authtypes.Minter, initCoins))
supplyAfterInflation := keeper.GetTotalSupply(ctx)
supplyAfterInflation, _, err := keeper.GetPaginatedTotalSupply(ctx, &query.PageRequest{})

suite.Require().Panics(func() { keeper.BurnCoins(ctx, "", initCoins) }, "no module account") // nolint:errcheck
suite.Require().Panics(func() { keeper.BurnCoins(ctx, authtypes.Minter, initCoins) }, "invalid permission") // nolint:errcheck
suite.Require().Panics(func() { keeper.BurnCoins(ctx, randomPerm, supplyAfterInflation) }, "random permission") // nolint:errcheck
err := keeper.BurnCoins(ctx, authtypes.Burner, supplyAfterInflation)
err = keeper.BurnCoins(ctx, authtypes.Burner, supplyAfterInflation)
suite.Require().Error(err, "insufficient coins")

err = keeper.BurnCoins(ctx, authtypes.Burner, initCoins)
suite.Require().NoError(err)
supplyAfterBurn, _, err := keeper.GetPaginatedTotalSupply(ctx, &query.PageRequest{})
suite.Require().NoError(err)
suite.Require().Equal(sdk.NewCoins().String(), getCoinsByName(ctx, keeper, authKeeper, authtypes.Burner).String())
suite.Require().Equal(supplyAfterInflation.Sub(initCoins), keeper.GetTotalSupply(ctx))
suite.Require().Equal(supplyAfterInflation.Sub(initCoins), supplyAfterBurn)

// test same functionality on module account with multiple permissions
suite.
Require().
NoError(keeper.MintCoins(ctx, authtypes.Minter, initCoins))
supplyAfterInflation = keeper.GetTotalSupply(ctx)

supplyAfterInflation, _, err = keeper.GetPaginatedTotalSupply(ctx, &query.PageRequest{})
suite.Require().NoError(err)
suite.Require().NoError(keeper.SendCoins(ctx, authtypes.NewModuleAddress(authtypes.Minter), multiPermAcc.GetAddress(), initCoins))
authKeeper.SetModuleAccount(ctx, multiPermAcc)

err = keeper.BurnCoins(ctx, multiPermAcc.GetName(), initCoins)
supplyAfterBurn, _, err = keeper.GetPaginatedTotalSupply(ctx, &query.PageRequest{})
suite.Require().NoError(err)
suite.Require().NoError(err)
suite.Require().Equal(sdk.NewCoins().String(), getCoinsByName(ctx, keeper, authKeeper, multiPermAcc.GetName()).String())
suite.Require().Equal(supplyAfterInflation.Sub(initCoins), keeper.GetTotalSupply(ctx))
suite.Require().Equal(supplyAfterInflation.Sub(initCoins), supplyAfterBurn)
}

func (suite *IntegrationTestSuite) TestSendCoinsNewAccount() {
Expand Down
Loading