Skip to content

Commit

Permalink
Merge branch 'master' of github.com:cosmos/cosmos-sdk into frojdi/iss…
Browse files Browse the repository at this point in the history
…ue-8959-extend-cosmos-reflection
  • Loading branch information
sahith-narahari committed Apr 6, 2021
2 parents 839b5b4 + 7f9bc05 commit 437d7c7
Show file tree
Hide file tree
Showing 22 changed files with 347 additions and 118 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,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 All @@ -88,6 +89,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/bank) [\#8656](https://github.com/cosmos/cosmos-sdk/pull/8656) balance and supply are now correctly tracked via `coin_spent`, `coin_received`, `coinbase` and `burn` events.
* (x/bank) [\#8517](https://github.com/cosmos/cosmos-sdk/pull/8517) Supply is now stored and tracked as `sdk.Coins`
* (store) [\#8790](https://github.com/cosmos/cosmos-sdk/pull/8790) Reduce gas costs by 10x for transient store operations.
* (x/bank) [\#9051](https://github.com/cosmos/cosmos-sdk/pull/9051) Supply value is stored as `sdk.Int` rather than `string`.

### Improvements

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
FROM golang:alpine AS build-env

# Install minimum necessary dependencies,
ENV PACKAGES curl make git libc-dev bash gcc linux-headers eudev-dev python3 plantuml
ENV PACKAGES curl make git libc-dev bash gcc linux-headers eudev-dev python3
RUN apk add --no-cache $PACKAGES

# Set working directory for the build
Expand Down
2 changes: 1 addition & 1 deletion docs/ibc/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ In IBC, blockchains do not directly pass messages to each other over the network

- A relayer process monitors for updates to these paths and relays messages by submitting the data stored under the path along with a proof of that data to the counterparty chain.

- The paths that all IBC implementations must support for committing IBC messages are defined in [ICS-24 host requirements](https://github.com/cosmos/ics/tree/master/spec/ics-024-host-requirements).
- The paths that all IBC implementations must support for committing IBC messages are defined in [ICS-24 host requirements](https://github.com/cosmos/ics/tree/master/spec/core/ics-024-host-requirements).

- The proof format that all implementations must produce and verify is defined in [ICS-23 implementation](https://github.com/confio/ics23).

Expand Down
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
51 changes: 36 additions & 15 deletions x/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package keeper

import (
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
"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 @@ -22,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 @@ -53,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 Expand Up @@ -184,9 +199,10 @@ func (k BaseKeeper) GetSupply(ctx sdk.Context, denom string) sdk.Coin {
}
}

amount, ok := sdk.NewIntFromString(string(bz))
if !ok {
panic("unexpected supply")
var amount sdk.Int
err := amount.Unmarshal(bz)
if err != nil {
panic(fmt.Errorf("unable to unmarshal supply value %v", err))
}

return sdk.Coin{
Expand Down Expand Up @@ -413,7 +429,11 @@ func (k BaseKeeper) setSupply(ctx sdk.Context, coin sdk.Coin) {
store := ctx.KVStore(k.storeKey)
supplyStore := prefix.NewStore(store, types.SupplyKey)

supplyStore.Set([]byte(coin.GetDenom()), []byte(coin.Amount.String()))
intBytes, err := coin.Amount.Marshal()
if err != nil {
panic(fmt.Errorf("unable to marshal amount value %v", err))
}
supplyStore.Set([]byte(coin.GetDenom()), intBytes)
}

func (k BaseKeeper) trackDelegation(ctx sdk.Context, addr sdk.AccAddress, balance, amt sdk.Coins) error {
Expand Down Expand Up @@ -454,9 +474,10 @@ func (k BaseViewKeeper) IterateTotalSupply(ctx sdk.Context, cb func(sdk.Coin) bo
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
amount, ok := sdk.NewIntFromString(string(iterator.Value()))
if !ok {
panic("unexpected supply")
var amount sdk.Int
err := amount.Unmarshal(iterator.Value())
if err != nil {
panic(fmt.Errorf("unable to unmarshal supply value %v", err))
}

balance := sdk.Coin{
Expand Down
Loading

0 comments on commit 437d7c7

Please sign in to comment.