From f2dedc938b648d687259e9cf1d91b82dd0e31c8c Mon Sep 17 00:00:00 2001 From: ezreal1997 Date: Wed, 9 Oct 2024 08:51:00 -0700 Subject: [PATCH] feat(mint): update param type --- client/x/mint/README.md | 6 ++- client/x/mint/keeper/genesis_test.go | 3 +- client/x/mint/types/genesis.go | 2 +- client/x/mint/types/mint.pb.go | 80 +++++++++++++++++----------- client/x/mint/types/mint.proto | 8 ++- client/x/mint/types/params.go | 4 +- 6 files changed, 67 insertions(+), 36 deletions(-) diff --git a/client/x/mint/README.md b/client/x/mint/README.md index 600267c3..425c7fcb 100644 --- a/client/x/mint/README.md +++ b/client/x/mint/README.md @@ -32,7 +32,11 @@ message Params { // type of coin to mint string mint_denom = 1; // inflation amount per year - uint64 inflations_per_year = 2; + string inflations_per_year = 2 [ + (cosmos_proto.scalar) = "cosmos.Dec", + (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec", + (gogoproto.nullable) = false + ]; // expected blocks per year uint64 blocks_per_year = ; } diff --git a/client/x/mint/keeper/genesis_test.go b/client/x/mint/keeper/genesis_test.go index affa5818..2195c6b3 100644 --- a/client/x/mint/keeper/genesis_test.go +++ b/client/x/mint/keeper/genesis_test.go @@ -4,6 +4,7 @@ package keeper_test import ( "testing" + "cosmossdk.io/math" storetypes "cosmossdk.io/store/types" "github.com/cosmos/cosmos-sdk/codec" @@ -62,7 +63,7 @@ func (s *GenesisTestSuite) TestImportExportGenesis() { genesisState := types.DefaultGenesisState() genesisState.Params = types.NewParams( "testDenom", - 24625000_000_000_000, + math.LegacyNewDec(24625000000000000), uint64(60*60*8766/5), ) diff --git a/client/x/mint/types/genesis.go b/client/x/mint/types/genesis.go index ff80226e..656061ae 100644 --- a/client/x/mint/types/genesis.go +++ b/client/x/mint/types/genesis.go @@ -15,7 +15,7 @@ type InflationCalculationFn func(ctx context.Context, params Params, bondedRatio // DefaultInflationCalculationFn is the default function used to calculate inflation. func DefaultInflationCalculationFn(_ context.Context, params Params, _ math.LegacyDec) math.LegacyDec { - return math.LegacyNewDec(int64(params.InflationsPerYear)).Quo(math.LegacyNewDec(int64(params.BlocksPerYear))) + return params.InflationsPerYear.Quo(math.LegacyNewDec(int64(params.BlocksPerYear))) } // NewGenesisState creates a new GenesisState object. diff --git a/client/x/mint/types/mint.pb.go b/client/x/mint/types/mint.pb.go index 59f0e424..30133b79 100644 --- a/client/x/mint/types/mint.pb.go +++ b/client/x/mint/types/mint.pb.go @@ -4,8 +4,11 @@ package types import ( + cosmossdk_io_math "cosmossdk.io/math" fmt "fmt" + _ "github.com/cosmos/cosmos-proto" _ "github.com/cosmos/cosmos-sdk/types/tx/amino" + _ "github.com/cosmos/gogoproto/gogoproto" proto "github.com/cosmos/gogoproto/proto" io "io" math "math" @@ -28,7 +31,7 @@ type Params struct { // type of coin to mint MintDenom string `protobuf:"bytes,1,opt,name=mint_denom,json=mintDenom,proto3" json:"mint_denom,omitempty"` // inflation amount per year - InflationsPerYear uint64 `protobuf:"varint,2,opt,name=inflations_per_year,json=inflationsPerYear,proto3" json:"inflations_per_year,omitempty"` + InflationsPerYear cosmossdk_io_math.LegacyDec `protobuf:"bytes,2,opt,name=inflations_per_year,json=inflationsPerYear,proto3,customtype=cosmossdk.io/math.LegacyDec" json:"inflations_per_year"` // expected blocks per year BlocksPerYear uint64 `protobuf:"varint,3,opt,name=blocks_per_year,json=blocksPerYear,proto3" json:"blocks_per_year,omitempty"` } @@ -73,13 +76,6 @@ func (m *Params) GetMintDenom() string { return "" } -func (m *Params) GetInflationsPerYear() uint64 { - if m != nil { - return m.InflationsPerYear - } - return 0 -} - func (m *Params) GetBlocksPerYear() uint64 { if m != nil { return m.BlocksPerYear @@ -94,21 +90,26 @@ func init() { func init() { proto.RegisterFile("client/x/mint/types/mint.proto", fileDescriptor_9c6e60aec58f52af) } var fileDescriptor_9c6e60aec58f52af = []byte{ - // 219 bytes of a gzipped FileDescriptorProto + // 290 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0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, 0x99, 0x9a, 0x58, 0x24, 0xa4, 0xc6, 0xc5, + 0x9f, 0x94, 0x93, 0x9f, 0x9c, 0x8d, 0x64, 0x3c, 0xb3, 0x02, 0xa3, 0x06, 0x4b, 0x10, 0x2f, 0x44, + 0x18, 0xaa, 0xce, 0x4a, 0xb2, 0xeb, 0xf9, 0x06, 0x2d, 0x11, 0xd4, 0x20, 0x83, 0x78, 0xc2, 0x49, + 0xf7, 0xc4, 0x23, 0x39, 0xc6, 0x0b, 0x8f, 0xe4, 0x18, 0x1f, 0x3c, 0x92, 0x63, 0x9c, 0xf0, 0x58, + 0x8e, 0xe1, 0xc2, 0x63, 0x39, 0x86, 0x1b, 0x8f, 0xe5, 0x18, 0xa2, 0x84, 0xb1, 0x04, 0x71, 0x12, + 0x1b, 0x38, 0x14, 0x8c, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0x30, 0x59, 0xae, 0xed, 0x80, 0x01, + 0x00, 0x00, } func (m *Params) Marshal() (dAtA []byte, err error) { @@ -136,11 +137,16 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { i-- dAtA[i] = 0x18 } - if m.InflationsPerYear != 0 { - i = encodeVarintMint(dAtA, i, uint64(m.InflationsPerYear)) - i-- - dAtA[i] = 0x10 + { + size := m.InflationsPerYear.Size() + i -= size + if _, err := m.InflationsPerYear.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintMint(dAtA, i, uint64(size)) } + i-- + dAtA[i] = 0x12 if len(m.MintDenom) > 0 { i -= len(m.MintDenom) copy(dAtA[i:], m.MintDenom) @@ -172,9 +178,8 @@ func (m *Params) Size() (n int) { if l > 0 { n += 1 + l + sovMint(uint64(l)) } - if m.InflationsPerYear != 0 { - n += 1 + sovMint(uint64(m.InflationsPerYear)) - } + l = m.InflationsPerYear.Size() + n += 1 + l + sovMint(uint64(l)) if m.BlocksPerYear != 0 { n += 1 + sovMint(uint64(m.BlocksPerYear)) } @@ -249,10 +254,10 @@ func (m *Params) Unmarshal(dAtA []byte) error { m.MintDenom = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex case 2: - if wireType != 0 { + if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field InflationsPerYear", wireType) } - m.InflationsPerYear = 0 + var stringLen uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowMint @@ -262,11 +267,26 @@ func (m *Params) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - m.InflationsPerYear |= uint64(b&0x7F) << shift + stringLen |= uint64(b&0x7F) << shift if b < 0x80 { break } } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthMint + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthMint + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.InflationsPerYear.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex case 3: if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field BlocksPerYear", wireType) diff --git a/client/x/mint/types/mint.proto b/client/x/mint/types/mint.proto index 509ccd8f..144a51ea 100644 --- a/client/x/mint/types/mint.proto +++ b/client/x/mint/types/mint.proto @@ -4,6 +4,8 @@ package client.x.mint.types; option go_package = "client/x/mint/types"; import "amino/amino.proto"; +import "cosmos_proto/cosmos.proto"; +import "gogoproto/gogo.proto"; // Params defines the parameters for the x/mint module. message Params { @@ -12,7 +14,11 @@ message Params { // type of coin to mint string mint_denom = 1; // inflation amount per year - uint64 inflations_per_year = 2; + string inflations_per_year = 2 [ + (cosmos_proto.scalar) = "cosmos.Dec", + (gogoproto.customtype) = "cosmossdk.io/math.LegacyDec", + (gogoproto.nullable) = false + ]; // expected blocks per year uint64 blocks_per_year = 3; } diff --git a/client/x/mint/types/params.go b/client/x/mint/types/params.go index cc5197a3..f889f692 100644 --- a/client/x/mint/types/params.go +++ b/client/x/mint/types/params.go @@ -13,7 +13,7 @@ import ( ) // NewParams returns Params instance with the given values. -func NewParams(mintDenom string, inflationsPerYear uint64, blocksPerYear uint64) Params { +func NewParams(mintDenom string, inflationsPerYear math.LegacyDec, blocksPerYear uint64) Params { return Params{ MintDenom: mintDenom, InflationsPerYear: inflationsPerYear, @@ -25,7 +25,7 @@ func NewParams(mintDenom string, inflationsPerYear uint64, blocksPerYear uint64) func DefaultParams() Params { return Params{ MintDenom: sdk.DefaultBondDenom, - InflationsPerYear: 24625000_000_000_000, + InflationsPerYear: math.LegacyNewDec(24625000_000_000_000), BlocksPerYear: uint64(60 * 60 * 8766 / 5), // assuming 5 second block times } }