Skip to content

Commit 2f29cb3

Browse files
authored
fix: Municipal Inflation: list->map & cli command printout (#154)
1 parent 0c003df commit 2f29cb3

File tree

15 files changed

+635
-255
lines changed

15 files changed

+635
-255
lines changed

docs/core/proto-docs.md

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@
394394

395395
- [cosmos/mint/v1beta1/mint.proto](#cosmos/mint/v1beta1/mint.proto)
396396
- [Minter](#cosmos.mint.v1beta1.Minter)
397+
- [Minter.MunicipalInflationEntry](#cosmos.mint.v1beta1.Minter.MunicipalInflationEntry)
397398
- [MunicipalInflation](#cosmos.mint.v1beta1.MunicipalInflation)
398399
- [Params](#cosmos.mint.v1beta1.Params)
399400

@@ -407,6 +408,7 @@
407408
- [QueryInflationResponse](#cosmos.mint.v1beta1.QueryInflationResponse)
408409
- [QueryMunicipalInflationRequest](#cosmos.mint.v1beta1.QueryMunicipalInflationRequest)
409410
- [QueryMunicipalInflationResponse](#cosmos.mint.v1beta1.QueryMunicipalInflationResponse)
411+
- [QueryMunicipalInflationResponse.InflationsEntry](#cosmos.mint.v1beta1.QueryMunicipalInflationResponse.InflationsEntry)
410412
- [QueryParamsRequest](#cosmos.mint.v1beta1.QueryParamsRequest)
411413
- [QueryParamsResponse](#cosmos.mint.v1beta1.QueryParamsResponse)
412414

@@ -5751,7 +5753,23 @@ Minter represents the minting state.
57515753
| ----- | ---- | ----- | ----------- |
57525754
| `inflation` | [string](#string) | | current annual inflation rate |
57535755
| `annual_provisions` | [string](#string) | | current annual expected provisions |
5754-
| `municipal_inflation` | [MunicipalInflation](#cosmos.mint.v1beta1.MunicipalInflation) | repeated | map<string, Inflation> inflations = 3; |
5756+
| `municipal_inflation` | [Minter.MunicipalInflationEntry](#cosmos.mint.v1beta1.Minter.MunicipalInflationEntry) | repeated | |
5757+
5758+
5759+
5760+
5761+
5762+
5763+
<a name="cosmos.mint.v1beta1.Minter.MunicipalInflationEntry"></a>
5764+
5765+
### Minter.MunicipalInflationEntry
5766+
5767+
5768+
5769+
| Field | Type | Label | Description |
5770+
| ----- | ---- | ----- | ----------- |
5771+
| `key` | [string](#string) | | |
5772+
| `value` | [MunicipalInflation](#cosmos.mint.v1beta1.MunicipalInflation) | | |
57555773

57565774

57575775

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

57675785
| Field | Type | Label | Description |
57685786
| ----- | ---- | ----- | ----------- |
5769-
| `denom` | [string](#string) | | |
57705787
| `target_address` | [string](#string) | | |
57715788
| `inflation` | [string](#string) | | current annual inflation rate |
57725789

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

59015918

5919+
| Field | Type | Label | Description |
5920+
| ----- | ---- | ----- | ----------- |
5921+
| `denom` | [string](#string) | optional | |
5922+
5923+
59025924

59035925

59045926

@@ -5912,7 +5934,23 @@ method.
59125934

59135935
| Field | Type | Label | Description |
59145936
| ----- | ---- | ----- | ----------- |
5915-
| `inflations` | [MunicipalInflation](#cosmos.mint.v1beta1.MunicipalInflation) | repeated | inflation is the current minting inflation value. |
5937+
| `inflations` | [QueryMunicipalInflationResponse.InflationsEntry](#cosmos.mint.v1beta1.QueryMunicipalInflationResponse.InflationsEntry) | repeated | inflation is the current minting inflation value. |
5938+
5939+
5940+
5941+
5942+
5943+
5944+
<a name="cosmos.mint.v1beta1.QueryMunicipalInflationResponse.InflationsEntry"></a>
5945+
5946+
### QueryMunicipalInflationResponse.InflationsEntry
5947+
5948+
5949+
5950+
| Field | Type | Label | Description |
5951+
| ----- | ---- | ----- | ----------- |
5952+
| `key` | [string](#string) | | |
5953+
| `value` | [MunicipalInflation](#cosmos.mint.v1beta1.MunicipalInflation) | | |
59165954

59175955

59185956

proto/cosmos/mint/v1beta1/mint.proto

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ message Minter {
1717
(gogoproto.nullable) = false
1818
];
1919

20-
// map<string, Inflation> inflations = 3;
21-
repeated MunicipalInflation municipal_inflation = 3;
20+
map<string, MunicipalInflation> municipal_inflation = 3;
2221
}
2322

2423
// Inflation holds parameters for individual native token inflation
2524
message MunicipalInflation {
26-
string denom = 1;
2725
string target_address = 2;
2826
// current annual inflation rate
2927
string inflation = 3 [

proto/cosmos/mint/v1beta1/query.proto

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ message QueryParamsResponse {
4343
message QueryInflationRequest {}
4444

4545
// QueryMunicipalInflationRequest is the request type for the Query/MunicipalInflation RPC method.
46-
message QueryMunicipalInflationRequest {}
46+
message QueryMunicipalInflationRequest {
47+
optional string denom = 1;
48+
}
4749

4850
// QueryInflationResponse is the response type for the Query/Inflation RPC
4951
// method.
@@ -56,7 +58,7 @@ message QueryInflationResponse {
5658
// method.
5759
message QueryMunicipalInflationResponse {
5860
// inflation is the current minting inflation value.
59-
repeated MunicipalInflation inflations = 1;
61+
map<string, MunicipalInflation> inflations = 1;
6062
}
6163

6264
// QueryAnnualProvisionsRequest is the request type for the

x/mint/abci.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ var (
2525

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

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

35-
for _, inflation := range minter.MunicipalInflation {
35+
for denom, inflation := range minter.MunicipalInflation {
3636
inflationPerBlock, err := types.CalculateInflationPerBlock(inflation, blocksPerYear)
3737
if err != nil {
3838
panic(err)
3939
}
4040

41-
cache.perBlockInflations[inflation.Denom] = inflationPerBlock
41+
cache.perBlockInflations[denom] = inflationPerBlock
4242
}
4343
}
4444

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

6060
// iterate through native denominations
61-
for _, inflation := range minter.MunicipalInflation {
62-
denom := inflation.Denom
61+
for denom, inflation := range minter.MunicipalInflation {
6362
targetAddress := inflation.TargetAddress
6463

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

6968
err := k.MintCoins(ctx, coinsToMint)
7069

@@ -88,7 +87,7 @@ func HandleMunicipalInflation(ctx sdk.Context, k keeper.Keeper) {
8887
ctx.EventManager().EmitEvent(
8988
sdk.NewEvent(
9089
types.EventTypeMunicipalMint,
91-
sdk.NewAttribute(types.AttributeKeyDenom, inflation.Denom),
90+
sdk.NewAttribute(types.AttributeKeyDenom, denom),
9291
sdk.NewAttribute(types.AttributeKeyInflation, inflation.Inflation.String()),
9392
sdk.NewAttribute(types.AttributeKeyTargetAddr, inflation.TargetAddress),
9493
sdk.NewAttribute(sdk.AttributeKeyAmount, coinsToMint.String()),

x/mint/client/cli/query.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package cli
22

33
import (
44
"fmt"
5-
6-
"github.com/spf13/cobra"
7-
85
"github.com/cosmos/cosmos-sdk/client"
96
"github.com/cosmos/cosmos-sdk/client/flags"
107
"github.com/cosmos/cosmos-sdk/x/mint/types"
8+
"github.com/spf13/cobra"
119
)
1210

1311
// GetQueryCmd returns the cli query commands for the minting module.
@@ -92,23 +90,32 @@ func GetCmdQueryInflation() *cobra.Command {
9290
// inflation values.
9391
func GetCmdQueryMunicipalInflation() *cobra.Command {
9492
cmd := &cobra.Command{
95-
Use: "municipal-inflation",
96-
Short: "Query municipal inflation configurations for all registered denominations",
97-
Args: cobra.NoArgs,
93+
Use: "municipal-inflation [denomination]",
94+
Short: "Query municipal inflation configuration",
95+
Long: `If there is NO 'denomination' value is provided, then query returns full
96+
configuration of the municipal inflation (for all registered denominations).
97+
Otherwise it returns only the configuration for the provided 'denomination' value.`,
98+
Args: cobra.MaximumNArgs(1),
9899
RunE: func(cmd *cobra.Command, args []string) error {
99100
clientCtx, err := client.GetClientQueryContext(cmd)
100101
if err != nil {
101102
return err
102103
}
104+
103105
queryClient := types.NewQueryClient(clientCtx)
104106

105-
params := &types.QueryMunicipalInflationRequest{}
106-
res, err := queryClient.MunicipalInflation(cmd.Context(), params)
107+
var req types.QueryMunicipalInflationRequest
108+
if len(args) > 0 {
109+
req = types.QueryMunicipalInflationRequest{XDenom: &types.QueryMunicipalInflationRequest_Denom{Denom: args[0]}}
110+
} else {
111+
req = types.QueryMunicipalInflationRequest{}
112+
}
113+
114+
res, err := queryClient.MunicipalInflation(cmd.Context(), &req)
107115
if err != nil {
108116
return err
109117
}
110-
111-
return clientCtx.PrintString(fmt.Sprintf("%s\n", res.Inflations))
118+
return clientCtx.PrintProto(res)
112119
},
113120
}
114121

x/mint/client/rest/grpc_query_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (s *IntegrationTestSuite) TestQueryGRPC() {
8585
},
8686
{
8787
"gRPC request inflations",
88-
fmt.Sprintf("%s/cosmos/mint/v1beta1/inflations", baseURL),
88+
fmt.Sprintf("%s/cosmos/mint/v1beta1/municipal_inflation", baseURL),
8989
map[string]string{},
9090
&minttypes.QueryMunicipalInflationResponse{},
9191
&minttypes.QueryMunicipalInflationResponse{

x/mint/client/testutil/suite.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ func (s *IntegrationTestSuite) SetupSuite() {
3737
inflation := sdk.MustNewDecFromStr("1.0")
3838
mintData.Minter.Inflation = inflation
3939

40+
mintData.Minter.MunicipalInflation = map[string]*minttypes.MunicipalInflation{
41+
"denom1": minttypes.NewMunicipalInflation("cosmos12kdu2sy0zcmz84qymyj6zcfvwss3a703xgpczm", sdk.NewDecWithPrec(345, 4)),
42+
"denom0": minttypes.NewMunicipalInflation("cosmos1d9pzg5542spe4anjgu2zmk7wxhgh04ysn2phpq", sdk.NewDecWithPrec(123, 2)),
43+
}
44+
4045
mintDataBz, err := s.cfg.Codec.MarshalJSON(&mintData)
4146
s.Require().NoError(err)
4247
genesisState[minttypes.ModuleName] = mintDataBz
@@ -89,7 +94,7 @@ mint_denom: stake`,
8994
}
9095
}
9196

92-
func (s *IntegrationTestSuite) TestGetCmdQueryInflations() {
97+
func (s *IntegrationTestSuite) TestGetCmdQueryMunicipalInflation() {
9398
val := s.network.Validators[0]
9499

95100
testCases := []struct {
@@ -100,12 +105,18 @@ func (s *IntegrationTestSuite) TestGetCmdQueryInflations() {
100105
{
101106
"json output",
102107
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
103-
`[]`,
108+
`{"inflations":{"denom0":{"target_address":"cosmos1d9pzg5542spe4anjgu2zmk7wxhgh04ysn2phpq","inflation":"1.230000000000000000"},"denom1":{"target_address":"cosmos12kdu2sy0zcmz84qymyj6zcfvwss3a703xgpczm","inflation":"0.034500000000000000"}}}`,
104109
},
105110
{
106111
"text output",
107112
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", tmcli.OutputFlag)},
108-
`[]`,
113+
`inflations:
114+
denom0:
115+
inflation: "1.230000000000000000"
116+
target_address: cosmos1d9pzg5542spe4anjgu2zmk7wxhgh04ysn2phpq
117+
denom1:
118+
inflation: "0.034500000000000000"
119+
target_address: cosmos12kdu2sy0zcmz84qymyj6zcfvwss3a703xgpczm`,
109120
},
110121
}
111122

x/mint/keeper/grpc_query.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package keeper
22

33
import (
44
"context"
5+
"fmt"
56

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

2829
// MunicipalInflation returns minter.MunicipalInflation of the mint module.
29-
func (k Keeper) MunicipalInflation(c context.Context, _ *types.QueryMunicipalInflationRequest) (*types.QueryMunicipalInflationResponse, error) {
30+
func (k Keeper) MunicipalInflation(c context.Context, req *types.QueryMunicipalInflationRequest) (*types.QueryMunicipalInflationResponse, error) {
3031
ctx := sdk.UnwrapSDKContext(c)
3132
minter := k.GetMinter(ctx)
33+
denom := req.GetDenom()
3234

33-
return &types.QueryMunicipalInflationResponse{Inflations: minter.MunicipalInflation}, nil
35+
if len(denom) == 0 {
36+
return &types.QueryMunicipalInflationResponse{Inflations: minter.MunicipalInflation}, nil
37+
}
38+
39+
infl, exists := minter.MunicipalInflation[denom]
40+
if exists {
41+
return nil, fmt.Errorf("there is no municipal inflation defined for requested \"%s\" denomination", denom)
42+
}
43+
44+
return &types.QueryMunicipalInflationResponse{Inflations: map[string]*types.MunicipalInflation{denom: infl}}, nil
3445
}
3546

3647
// AnnualProvisions returns minter.AnnualProvisions of the mint module.

x/mint/simulation/genesis_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestRandomizedGenState(t *testing.T) {
4646
require.Equal(t, "0.170000000000000000", mintGenesis.Minter.NextAnnualProvisions(mintGenesis.Params, sdk.OneInt()).String())
4747
require.Equal(t, "0.170000000000000000", mintGenesis.Minter.Inflation.String())
4848
require.Equal(t, "0.000000000000000000", mintGenesis.Minter.AnnualProvisions.String())
49-
require.Equal(t, nil, mintGenesis.Minter.Inflations)
49+
require.Equal(t, 0, len(mintGenesis.Minter.MunicipalInflation))
5050
}
5151

5252
// TestRandomizedGenState tests abnormal scenarios of applying RandomizedGenState.

x/mint/types/inflations.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import (
55
sdk "github.com/cosmos/cosmos-sdk/types"
66
)
77

8-
// NewInflation returns a new Inflation object with the given denom, target_address
8+
// NewMunicipalInflation returns a new Inflation object with the given denom, target_address
99
// and inflation_rate
10-
func NewInflation(denom string, targetAddress string, inflation sdk.Dec) *MunicipalInflation {
10+
func NewMunicipalInflation(targetAddress string, inflation sdk.Dec) *MunicipalInflation {
1111
return &MunicipalInflation{
12-
Denom: denom,
1312
TargetAddress: targetAddress,
1413
Inflation: inflation,
1514
}
@@ -49,22 +48,16 @@ func (inflation *MunicipalInflation) Validate() error {
4948
inflation.TargetAddress)
5049
}
5150

52-
err = sdk.ValidateDenom(inflation.Denom)
53-
if err != nil {
54-
return fmt.Errorf("inflation object param, denom: %s", err)
55-
}
56-
5751
return nil
5852
}
5953

60-
func ValidateMunicipalInflations(i interface{}) (err error) {
61-
v, ok := i.([]*MunicipalInflation)
62-
if !ok {
63-
err = fmt.Errorf("invalid parameter type: %T", i)
64-
return
65-
}
54+
func ValidateMunicipalInflations(inflations *map[string]*MunicipalInflation) (err error) {
55+
for denom, inflation := range *inflations {
56+
err = sdk.ValidateDenom(denom)
57+
if err != nil {
58+
return fmt.Errorf("inflation object param, denom: %s", err)
59+
}
6660

67-
for _, inflation := range v {
6861
err = inflation.Validate()
6962
if err != nil {
7063
return

0 commit comments

Comments
 (0)