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

Merge x/supply into x/bank #6010

Merged
merged 29 commits into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
abae2ec
Remove x/supply module
alexanderbez Apr 16, 2020
449bd18
Start migration
alexanderbez Apr 16, 2020
305b2d1
More updates
alexanderbez Apr 16, 2020
14d1f0d
Update codec
alexanderbez Apr 17, 2020
16cf70f
Update x/staking
alexanderbez Apr 17, 2020
c69418d
Update x/mint
alexanderbez Apr 17, 2020
c4fe6c2
Update x/ibc
alexanderbez Apr 17, 2020
5ba82da
Update x/gov
alexanderbez Apr 17, 2020
e6f2e6c
Lint
alexanderbez Apr 17, 2020
cd5b28e
Fix x/ibc
alexanderbez Apr 17, 2020
feb3a16
Update more mods
alexanderbez Apr 17, 2020
974fb33
Update x/auth
alexanderbez Apr 17, 2020
09194de
Update simapp
alexanderbez Apr 17, 2020
b1666d4
Update querier tests
alexanderbez Apr 17, 2020
124d689
Update keeper tests
alexanderbez Apr 17, 2020
3324a00
Merge branch 'master' into bez/5913-remove-supply-mod
alexanderbez Apr 17, 2020
5f9f528
Lint
alexanderbez Apr 17, 2020
dee0da7
Fix tests
alexanderbez Apr 17, 2020
68f0472
Fix TestImportExportQueues
alexanderbez Apr 17, 2020
2152b36
Add changelog entries
alexanderbez Apr 17, 2020
0c21364
Merge branch 'master' into bez/5913-remove-supply-mod
alexanderbez Apr 17, 2020
6c381e3
Merge branch 'master' into bez/5913-remove-supply-mod
alexanderbez Apr 17, 2020
2749dc3
Merge branch 'master' into bez/5913-remove-supply-mod
fedekunze Apr 18, 2020
702805b
Sim updates
alexanderbez Apr 19, 2020
50f0a14
Merge branch 'bez/5913-remove-supply-mod' of github.com:cosmos/cosmos…
alexanderbez Apr 19, 2020
f7c668a
Use module name
alexanderbez Apr 19, 2020
1bb8543
Fix bui;ld
alexanderbez Apr 19, 2020
e499b62
Merge branch 'master' into bez/5913-remove-supply-mod
fedekunze Apr 20, 2020
3e47909
Update x/auth/types/expected_keepers.go
alexanderbez Apr 20, 2020
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
Prev Previous commit
Next Next commit
Start migration
  • Loading branch information
alexanderbez committed Apr 16, 2020
commit 449bd18d6f33bd611457e50d2c725f373fc3df45
79 changes: 77 additions & 2 deletions x/bank/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)

Expand All @@ -28,7 +30,10 @@ func NewQueryCmd(m codec.Marshaler) *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand(NewBalancesCmd(m))
cmd.AddCommand(
NewBalancesCmd(m),
NewQueryTotalSupplyCmd(m),
)

return cmd
}
Expand Down Expand Up @@ -97,6 +102,39 @@ func NewBalancesCmd(m codec.Marshaler) *cobra.Command {
return flags.GetCommands(cmd)[0]
}

// NewQueryTotalSupplyCmd returns a CLI command handler for querying total supply.
func NewQueryTotalSupplyCmd(m codec.Marshaler) *cobra.Command {
cmd := &cobra.Command{
Use: "total [denom]",
Args: cobra.MaximumNArgs(1),
Short: "Query the total supply of coins of the chain",
Long: strings.TrimSpace(
fmt.Sprintf(`Query total supply of coins that are held by accounts in the
chain.

Example:
$ %s query %s total

To query for the total supply of a specific coin denomination use:
$ %s query %s total stake
`,
version.ClientName, types.ModuleName, version.ClientName, types.ModuleName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithMarshaler(m)

if len(args) == 0 {
return queryTotalSupply(cliCtx, m)
}

return querySupplyOf(cliCtx, m, args[0])
},
}

return flags.GetCommands(cmd)[0]
}

// ---------------------------------------------------------------------------
// Deprecated
//
Expand All @@ -116,7 +154,10 @@ func GetQueryCmd(cdc *codec.Codec) *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand(GetBalancesCmd(cdc))
cmd.AddCommand(
GetBalancesCmd(cdc),
GetCmdQueryTotalSupply(cdc),
)

return cmd
}
Expand Down Expand Up @@ -188,3 +229,37 @@ func GetBalancesCmd(cdc *codec.Codec) *cobra.Command {

return flags.GetCommands(cmd)[0]
}

// TODO: Remove once client-side Protobuf migration has been completed.
// ref: https://github.com/cosmos/cosmos-sdk/issues/5864
func GetCmdQueryTotalSupply(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "total [denom]",
Args: cobra.MaximumNArgs(1),
Short: "Query the total supply of coins of the chain",
Long: strings.TrimSpace(
fmt.Sprintf(`Query total supply of coins that are held by accounts in the
chain.

Example:
$ %s query %s total

To query for the total supply of a specific coin denomination use:
$ %s query %s total stake
`,
version.ClientName, types.ModuleName, version.ClientName, types.ModuleName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

if len(args) == 0 {
return queryTotalSupply(cliCtx, cdc)
}

return querySupplyOf(cliCtx, cdc, args[0])
},
}

return flags.GetCommands(cmd)[0]
}
51 changes: 51 additions & 0 deletions x/bank/client/cli/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cli

import (
"fmt"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)

func queryTotalSupply(cliCtx context.CLIContext, m codec.Marshaler) error {
params := types.NewQueryTotalSupplyParams(1, 0) // no pagination
bz, err := m.MarshalJSON(params)
if err != nil {
return err
}

res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryTotalSupply), bz)
if err != nil {
return err
}

var totalSupply sdk.Coins
err = m.UnmarshalJSON(res, &totalSupply)
if err != nil {
return err
}

return cliCtx.PrintOutput(totalSupply)
}

func querySupplyOf(cliCtx context.CLIContext, m codec.Marshaler, denom string) error {
params := types.NewQuerySupplyOfParams(denom)
bz, err := m.MarshalJSON(params)
if err != nil {
return err
}

res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QuerySupplyOf), bz)
if err != nil {
return err
}

var supply sdk.Int
err = m.UnmarshalJSON(res, &supply)
if err != nil {
return err
}

return cliCtx.PrintOutput(supply)
}
54 changes: 54 additions & 0 deletions x/bank/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,57 @@ func QueryBalancesRequestHandlerFn(ctx context.CLIContext) http.HandlerFunc {
rest.PostProcessResponse(w, ctx, res)
}
}

// HTTP request handler to query the total supply of coins
func totalSupplyHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
if rest.CheckBadRequestError(w, err) {
return
}

cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}

params := types.NewQueryTotalSupplyParams(page, limit)
bz, err := cliCtx.Codec.MarshalJSON(params)
if rest.CheckBadRequestError(w, err) {
return
}

res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryTotalSupply), bz)
if rest.CheckInternalServerError(w, err) {
return
}

cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}

// HTTP request handler to query the supply of a single denom
func supplyOfHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
denom := mux.Vars(r)["denom"]
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}

params := types.NewQuerySupplyOfParams(denom)
bz, err := cliCtx.Codec.MarshalJSON(params)
if rest.CheckBadRequestError(w, err) {
return
}

res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QuerySupplyOf), bz)
if rest.CheckInternalServerError(w, err) {
return
}

cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}
2 changes: 2 additions & 0 deletions x/bank/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ func RegisterHandlers(ctx context.CLIContext, m codec.Marshaler, txg tx.Generato
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc("/bank/accounts/{address}/transfers", SendRequestHandlerFn(cliCtx)).Methods("POST")
r.HandleFunc("/bank/balances/{address}", QueryBalancesRequestHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/bank/total", totalSupplyHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc("/bank/total/{denom}", supplyOfHandlerFn(cliCtx)).Methods("GET")
}
24 changes: 24 additions & 0 deletions x/bank/exported/exported.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package exported

import (
sdk "github.com/cosmos/cosmos-sdk/types"
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
)

// GenesisBalance defines a genesis balance interface that allows for account
Expand All @@ -10,3 +11,26 @@ type GenesisBalance interface {
GetAddress() sdk.AccAddress
GetCoins() sdk.Coins
}

// ModuleAccountI defines an account interface for modules that hold tokens in
// an escrow.
type ModuleAccountI interface {
authexported.Account

GetName() string
GetPermissions() []string
HasPermission(string) bool
}

// SupplyI defines an inflationary supply interface for modules that handle
// token supply.
type SupplyI interface {
GetTotal() sdk.Coins
SetTotal(total sdk.Coins)

Inflate(amount sdk.Coins)
Deflate(amount sdk.Coins)

String() string
ValidateBasic() error
}
24 changes: 23 additions & 1 deletion x/bank/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/x/bank/types"
)

// InitGenesis initializes the bank module's state from a given genesis state.
Expand All @@ -20,6 +21,21 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, genState GenesisState) {
panic(fmt.Errorf("error on setting balances %w", err))
}
}

if data.Supply.Empty() {
var totalSupply sdk.Coins

keeper.IterateAllBalances(ctx,
func(_ sdk.AccAddress, balance sdk.Coin) (stop bool) {
totalSupply = totalSupply.Add(balance)
return false
},
)

data.Supply = totalSupply
}

keeper.SetSupply(ctx, NewSupply(data.Supply))
}

// ExportGenesis returns the bank module's genesis state.
Expand All @@ -45,5 +61,11 @@ func ExportGenesis(ctx sdk.Context, keeper Keeper) GenesisState {
})
}

return NewGenesisState(keeper.GetSendEnabled(ctx), balances)
return NewGenesisState(keeper.GetSendEnabled(ctx), balances, keeper.GetSupply(ctx).GetTotal())
}

// ValidateGenesis performs basic validation of supply genesis data returning an
// error for any failed validation criteria.
func ValidateGenesis(data GenesisState) error {
return types.NewSupply(data.Supply).ValidateBasic()
}
38 changes: 33 additions & 5 deletions x/bank/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,27 @@ import (
)

// RegisterInvariants registers the bank module invariants
func RegisterInvariants(ir sdk.InvariantRegistry, bk ViewKeeper) {
ir.RegisterRoute(types.ModuleName, "nonnegative-outstanding",
NonnegativeBalanceInvariant(bk))
func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {
ir.RegisterRoute(types.ModuleName, "nonnegative-outstanding", NonnegativeBalanceInvariant(k))
ir.RegisterRoute(types.ModuleName, "total-supply", TotalSupply(k))
}

// AllInvariants runs all invariants of the X/bank module.
func AllInvariants(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
return TotalSupply(k)(ctx)
}
}

// NonnegativeBalanceInvariant checks that all accounts in the application have non-negative balances
func NonnegativeBalanceInvariant(bk ViewKeeper) sdk.Invariant {
func NonnegativeBalanceInvariant(k ViewKeeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var (
msg string
count int
)

bk.IterateAllBalances(ctx, func(addr sdk.AccAddress, balance sdk.Coin) bool {
k.IterateAllBalances(ctx, func(addr sdk.AccAddress, balance sdk.Coin) bool {
if balance.IsNegative() {
count++
msg += fmt.Sprintf("\t%s has a negative balance of %s\n", addr, balance)
Expand All @@ -38,3 +45,24 @@ func NonnegativeBalanceInvariant(bk ViewKeeper) sdk.Invariant {
), broken
}
}

// TotalSupply checks that the total supply reflects all the coins held in accounts
func TotalSupply(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
var expectedTotal sdk.Coins
supply := k.GetSupply(ctx)

k.IterateAllBalances(ctx, func(_ sdk.AccAddress, balance sdk.Coin) bool {
expectedTotal = expectedTotal.Add(balance)
return false
})

broken := !expectedTotal.IsEqual(supply.GetTotal())

return sdk.FormatInvariant(types.ModuleName, "total supply",
fmt.Sprintf(
"\tsum of accounts coins: %v\n"+
"\tsupply.Total: %v\n",
expectedTotal, supply.GetTotal())), broken
}
}
Loading