Skip to content

Commit

Permalink
fix: Municipal Inflation: list->map & cli command printout (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
pbukva authored Jul 14, 2023
1 parent 0c003df commit 2f29cb3
Show file tree
Hide file tree
Showing 15 changed files with 635 additions and 255 deletions.
44 changes: 41 additions & 3 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@

- [cosmos/mint/v1beta1/mint.proto](#cosmos/mint/v1beta1/mint.proto)
- [Minter](#cosmos.mint.v1beta1.Minter)
- [Minter.MunicipalInflationEntry](#cosmos.mint.v1beta1.Minter.MunicipalInflationEntry)
- [MunicipalInflation](#cosmos.mint.v1beta1.MunicipalInflation)
- [Params](#cosmos.mint.v1beta1.Params)

Expand All @@ -407,6 +408,7 @@
- [QueryInflationResponse](#cosmos.mint.v1beta1.QueryInflationResponse)
- [QueryMunicipalInflationRequest](#cosmos.mint.v1beta1.QueryMunicipalInflationRequest)
- [QueryMunicipalInflationResponse](#cosmos.mint.v1beta1.QueryMunicipalInflationResponse)
- [QueryMunicipalInflationResponse.InflationsEntry](#cosmos.mint.v1beta1.QueryMunicipalInflationResponse.InflationsEntry)
- [QueryParamsRequest](#cosmos.mint.v1beta1.QueryParamsRequest)
- [QueryParamsResponse](#cosmos.mint.v1beta1.QueryParamsResponse)

Expand Down Expand Up @@ -5751,7 +5753,23 @@ Minter represents the minting state.
| ----- | ---- | ----- | ----------- |
| `inflation` | [string](#string) | | current annual inflation rate |
| `annual_provisions` | [string](#string) | | current annual expected provisions |
| `municipal_inflation` | [MunicipalInflation](#cosmos.mint.v1beta1.MunicipalInflation) | repeated | map<string, Inflation> inflations = 3; |
| `municipal_inflation` | [Minter.MunicipalInflationEntry](#cosmos.mint.v1beta1.Minter.MunicipalInflationEntry) | repeated | |






<a name="cosmos.mint.v1beta1.Minter.MunicipalInflationEntry"></a>

### Minter.MunicipalInflationEntry



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `key` | [string](#string) | | |
| `value` | [MunicipalInflation](#cosmos.mint.v1beta1.MunicipalInflation) | | |



Expand All @@ -5766,7 +5784,6 @@ Inflation holds parameters for individual native token inflation

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `denom` | [string](#string) | | |
| `target_address` | [string](#string) | | |
| `inflation` | [string](#string) | | current annual inflation rate |

Expand Down Expand Up @@ -5899,6 +5916,11 @@ method.
QueryMunicipalInflationRequest is the request type for the Query/MunicipalInflation RPC method.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `denom` | [string](#string) | optional | |





Expand All @@ -5912,7 +5934,23 @@ method.

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `inflations` | [MunicipalInflation](#cosmos.mint.v1beta1.MunicipalInflation) | repeated | inflation is the current minting inflation value. |
| `inflations` | [QueryMunicipalInflationResponse.InflationsEntry](#cosmos.mint.v1beta1.QueryMunicipalInflationResponse.InflationsEntry) | repeated | inflation is the current minting inflation value. |






<a name="cosmos.mint.v1beta1.QueryMunicipalInflationResponse.InflationsEntry"></a>

### QueryMunicipalInflationResponse.InflationsEntry



| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `key` | [string](#string) | | |
| `value` | [MunicipalInflation](#cosmos.mint.v1beta1.MunicipalInflation) | | |



Expand Down
4 changes: 1 addition & 3 deletions proto/cosmos/mint/v1beta1/mint.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ message Minter {
(gogoproto.nullable) = false
];

// map<string, Inflation> inflations = 3;
repeated MunicipalInflation municipal_inflation = 3;
map<string, MunicipalInflation> municipal_inflation = 3;
}

// Inflation holds parameters for individual native token inflation
message MunicipalInflation {
string denom = 1;
string target_address = 2;
// current annual inflation rate
string inflation = 3 [
Expand Down
6 changes: 4 additions & 2 deletions proto/cosmos/mint/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ message QueryParamsResponse {
message QueryInflationRequest {}

// QueryMunicipalInflationRequest is the request type for the Query/MunicipalInflation RPC method.
message QueryMunicipalInflationRequest {}
message QueryMunicipalInflationRequest {
optional string denom = 1;
}

// QueryInflationResponse is the response type for the Query/Inflation RPC
// method.
Expand All @@ -56,7 +58,7 @@ message QueryInflationResponse {
// method.
message QueryMunicipalInflationResponse {
// inflation is the current minting inflation value.
repeated MunicipalInflation inflations = 1;
map<string, MunicipalInflation> inflations = 1;
}

// QueryAnnualProvisionsRequest is the request type for the
Expand Down
13 changes: 6 additions & 7 deletions x/mint/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ var (

// NOTE(pb): Not thread safe, as per comment above.
func (cache *MunicipalInflationCache) refresh(minter *types.Minter, blocksPerYear uint64) {
if err := types.ValidateMunicipalInflations(minter.MunicipalInflation); err != nil {
if err := types.ValidateMunicipalInflations(&minter.MunicipalInflation); err != nil {
panic(err)
}

cache.blocksPerYear = blocksPerYear
cache.perBlockInflations = map[string]sdk.Dec{}

for _, inflation := range minter.MunicipalInflation {
for denom, inflation := range minter.MunicipalInflation {
inflationPerBlock, err := types.CalculateInflationPerBlock(inflation, blocksPerYear)
if err != nil {
panic(err)
}

cache.perBlockInflations[inflation.Denom] = inflationPerBlock
cache.perBlockInflations[denom] = inflationPerBlock
}
}

Expand All @@ -58,13 +58,12 @@ func HandleMunicipalInflation(ctx sdk.Context, k keeper.Keeper) {
infCache.refreshIfNecessary(&minter, params.BlocksPerYear)

// iterate through native denominations
for _, inflation := range minter.MunicipalInflation {
denom := inflation.Denom
for denom, inflation := range minter.MunicipalInflation {
targetAddress := inflation.TargetAddress

// gather supply value & calculate number of new tokens created from relevant inflation
totalDenomSupply := k.BankKeeper.GetSupply(ctx, denom)
coinsToMint := types.CalculateInflationIssuance(infCache.perBlockInflations[inflation.Denom], totalDenomSupply)
coinsToMint := types.CalculateInflationIssuance(infCache.perBlockInflations[denom], totalDenomSupply)

err := k.MintCoins(ctx, coinsToMint)

Expand All @@ -88,7 +87,7 @@ func HandleMunicipalInflation(ctx sdk.Context, k keeper.Keeper) {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeMunicipalMint,
sdk.NewAttribute(types.AttributeKeyDenom, inflation.Denom),
sdk.NewAttribute(types.AttributeKeyDenom, denom),
sdk.NewAttribute(types.AttributeKeyInflation, inflation.Inflation.String()),
sdk.NewAttribute(types.AttributeKeyTargetAddr, inflation.TargetAddress),
sdk.NewAttribute(sdk.AttributeKeyAmount, coinsToMint.String()),
Expand Down
27 changes: 17 additions & 10 deletions x/mint/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package cli

import (
"fmt"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/spf13/cobra"
)

// GetQueryCmd returns the cli query commands for the minting module.
Expand Down Expand Up @@ -92,23 +90,32 @@ func GetCmdQueryInflation() *cobra.Command {
// inflation values.
func GetCmdQueryMunicipalInflation() *cobra.Command {
cmd := &cobra.Command{
Use: "municipal-inflation",
Short: "Query municipal inflation configurations for all registered denominations",
Args: cobra.NoArgs,
Use: "municipal-inflation [denomination]",
Short: "Query municipal inflation configuration",
Long: `If there is NO 'denomination' value is provided, then query returns full
configuration of the municipal inflation (for all registered denominations).
Otherwise it returns only the configuration for the provided 'denomination' value.`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryMunicipalInflationRequest{}
res, err := queryClient.MunicipalInflation(cmd.Context(), params)
var req types.QueryMunicipalInflationRequest
if len(args) > 0 {
req = types.QueryMunicipalInflationRequest{XDenom: &types.QueryMunicipalInflationRequest_Denom{Denom: args[0]}}
} else {
req = types.QueryMunicipalInflationRequest{}
}

res, err := queryClient.MunicipalInflation(cmd.Context(), &req)
if err != nil {
return err
}

return clientCtx.PrintString(fmt.Sprintf("%s\n", res.Inflations))
return clientCtx.PrintProto(res)
},
}

Expand Down
2 changes: 1 addition & 1 deletion x/mint/client/rest/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (s *IntegrationTestSuite) TestQueryGRPC() {
},
{
"gRPC request inflations",
fmt.Sprintf("%s/cosmos/mint/v1beta1/inflations", baseURL),
fmt.Sprintf("%s/cosmos/mint/v1beta1/municipal_inflation", baseURL),
map[string]string{},
&minttypes.QueryMunicipalInflationResponse{},
&minttypes.QueryMunicipalInflationResponse{
Expand Down
17 changes: 14 additions & 3 deletions x/mint/client/testutil/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func (s *IntegrationTestSuite) SetupSuite() {
inflation := sdk.MustNewDecFromStr("1.0")
mintData.Minter.Inflation = inflation

mintData.Minter.MunicipalInflation = map[string]*minttypes.MunicipalInflation{
"denom1": minttypes.NewMunicipalInflation("cosmos12kdu2sy0zcmz84qymyj6zcfvwss3a703xgpczm", sdk.NewDecWithPrec(345, 4)),
"denom0": minttypes.NewMunicipalInflation("cosmos1d9pzg5542spe4anjgu2zmk7wxhgh04ysn2phpq", sdk.NewDecWithPrec(123, 2)),
}

mintDataBz, err := s.cfg.Codec.MarshalJSON(&mintData)
s.Require().NoError(err)
genesisState[minttypes.ModuleName] = mintDataBz
Expand Down Expand Up @@ -89,7 +94,7 @@ mint_denom: stake`,
}
}

func (s *IntegrationTestSuite) TestGetCmdQueryInflations() {
func (s *IntegrationTestSuite) TestGetCmdQueryMunicipalInflation() {
val := s.network.Validators[0]

testCases := []struct {
Expand All @@ -100,12 +105,18 @@ func (s *IntegrationTestSuite) TestGetCmdQueryInflations() {
{
"json output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
`[]`,
`{"inflations":{"denom0":{"target_address":"cosmos1d9pzg5542spe4anjgu2zmk7wxhgh04ysn2phpq","inflation":"1.230000000000000000"},"denom1":{"target_address":"cosmos12kdu2sy0zcmz84qymyj6zcfvwss3a703xgpczm","inflation":"0.034500000000000000"}}}`,
},
{
"text output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", tmcli.OutputFlag)},
`[]`,
`inflations:
denom0:
inflation: "1.230000000000000000"
target_address: cosmos1d9pzg5542spe4anjgu2zmk7wxhgh04ysn2phpq
denom1:
inflation: "0.034500000000000000"
target_address: cosmos12kdu2sy0zcmz84qymyj6zcfvwss3a703xgpczm`,
},
}

Expand Down
15 changes: 13 additions & 2 deletions x/mint/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/mint/types"
Expand All @@ -26,11 +27,21 @@ func (k Keeper) Inflation(c context.Context, _ *types.QueryInflationRequest) (*t
}

// MunicipalInflation returns minter.MunicipalInflation of the mint module.
func (k Keeper) MunicipalInflation(c context.Context, _ *types.QueryMunicipalInflationRequest) (*types.QueryMunicipalInflationResponse, error) {
func (k Keeper) MunicipalInflation(c context.Context, req *types.QueryMunicipalInflationRequest) (*types.QueryMunicipalInflationResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
minter := k.GetMinter(ctx)
denom := req.GetDenom()

return &types.QueryMunicipalInflationResponse{Inflations: minter.MunicipalInflation}, nil
if len(denom) == 0 {
return &types.QueryMunicipalInflationResponse{Inflations: minter.MunicipalInflation}, nil
}

infl, exists := minter.MunicipalInflation[denom]
if exists {
return nil, fmt.Errorf("there is no municipal inflation defined for requested \"%s\" denomination", denom)
}

return &types.QueryMunicipalInflationResponse{Inflations: map[string]*types.MunicipalInflation{denom: infl}}, nil
}

// AnnualProvisions returns minter.AnnualProvisions of the mint module.
Expand Down
2 changes: 1 addition & 1 deletion x/mint/simulation/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestRandomizedGenState(t *testing.T) {
require.Equal(t, "0.170000000000000000", mintGenesis.Minter.NextAnnualProvisions(mintGenesis.Params, sdk.OneInt()).String())
require.Equal(t, "0.170000000000000000", mintGenesis.Minter.Inflation.String())
require.Equal(t, "0.000000000000000000", mintGenesis.Minter.AnnualProvisions.String())
require.Equal(t, nil, mintGenesis.Minter.Inflations)
require.Equal(t, 0, len(mintGenesis.Minter.MunicipalInflation))
}

// TestRandomizedGenState tests abnormal scenarios of applying RandomizedGenState.
Expand Down
23 changes: 8 additions & 15 deletions x/mint/types/inflations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// NewInflation returns a new Inflation object with the given denom, target_address
// NewMunicipalInflation returns a new Inflation object with the given denom, target_address
// and inflation_rate
func NewInflation(denom string, targetAddress string, inflation sdk.Dec) *MunicipalInflation {
func NewMunicipalInflation(targetAddress string, inflation sdk.Dec) *MunicipalInflation {
return &MunicipalInflation{
Denom: denom,
TargetAddress: targetAddress,
Inflation: inflation,
}
Expand Down Expand Up @@ -49,22 +48,16 @@ func (inflation *MunicipalInflation) Validate() error {
inflation.TargetAddress)
}

err = sdk.ValidateDenom(inflation.Denom)
if err != nil {
return fmt.Errorf("inflation object param, denom: %s", err)
}

return nil
}

func ValidateMunicipalInflations(i interface{}) (err error) {
v, ok := i.([]*MunicipalInflation)
if !ok {
err = fmt.Errorf("invalid parameter type: %T", i)
return
}
func ValidateMunicipalInflations(inflations *map[string]*MunicipalInflation) (err error) {
for denom, inflation := range *inflations {
err = sdk.ValidateDenom(denom)
if err != nil {
return fmt.Errorf("inflation object param, denom: %s", err)
}

for _, inflation := range v {
err = inflation.Validate()
if err != nil {
return
Expand Down
Loading

0 comments on commit 2f29cb3

Please sign in to comment.