From 90456d035830368fb33008f1ae58d5bdcbb67c96 Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Wed, 23 Aug 2023 09:49:20 +0200 Subject: [PATCH 01/24] feat(mint): new mint params --- docs/proto/mint.md | 7 +- proto/mint/v1beta1/mint.proto | 25 ++-- x/mint/types/mint.pb.go | 215 +++++++++++++++++++++------------- 3 files changed, 152 insertions(+), 95 deletions(-) diff --git a/docs/proto/mint.md b/docs/proto/mint.md index ebffced7..14a60bf2 100644 --- a/docs/proto/mint.md +++ b/docs/proto/mint.md @@ -70,7 +70,6 @@ and `target_supply` 230M. | ----- | ---- | ----- | ----------- | | `inflation` | [string](#string) | | current annual inflation rate | | `annual_provisions` | [string](#string) | | current annual expected provisions | -| `target_supply` | [string](#string) | | target supply at end of period | @@ -86,8 +85,10 @@ new inflation will be 12%. | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | | `mint_denom` | [string](#string) | | type of coin to mint | -| `annual_reduction_factor` | [string](#string) | | annual reduction factor inflation rate change | -| `blocks_per_year` | [uint64](#uint64) | | expected blocks per year | +| `inflation_coef` | [string](#string) | | annual inflation coefficient | +| `bounding_adjustment` | [string](#string) | | bounding adjustment | +| `target_bounding_ratio` | [string](#string) | | target boudning ratio | +| `blocks_per_year` | [uint64](#uint64) | | expected blocks per yearmake p | [//]: # (end messages) diff --git a/proto/mint/v1beta1/mint.proto b/proto/mint/v1beta1/mint.proto index 4a3729da..3915952a 100644 --- a/proto/mint/v1beta1/mint.proto +++ b/proto/mint/v1beta1/mint.proto @@ -26,11 +26,6 @@ message Minter { (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false ]; - // target supply at end of period - string target_supply = 3 [ - (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", - (gogoproto.nullable) = false - ]; } // Params holds parameters for the mint module. @@ -44,12 +39,24 @@ message Params { // type of coin to mint string mint_denom = 1; - // annual reduction factor inflation rate change - string annual_reduction_factor = 2 [ + // annual inflation coefficient + string inflation_coef = 2 [ + (cosmos_proto.scalar) = "cosmos.Dec", + (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", + (gogoproto.nullable) = false + ]; + // bounding adjustment + string bounding_adjustment = 3 [ + (cosmos_proto.scalar) = "cosmos.Dec", + (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", + (gogoproto.nullable) = false + ]; + // target boudning ratio + string target_bounding_ratio = 4 [ (cosmos_proto.scalar) = "cosmos.Dec", (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false ]; - // expected blocks per year - uint64 blocks_per_year = 3; + // expected blocks per yearmake p + uint64 blocks_per_year = 5; } diff --git a/x/mint/types/mint.pb.go b/x/mint/types/mint.pb.go index 14252479..b5c103f2 100644 --- a/x/mint/types/mint.pb.go +++ b/x/mint/types/mint.pb.go @@ -36,8 +36,6 @@ type Minter struct { Inflation github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,1,opt,name=inflation,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"inflation"` // current annual expected provisions AnnualProvisions github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,2,opt,name=annual_provisions,json=annualProvisions,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"annual_provisions"` - // target supply at end of period - TargetSupply github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,3,opt,name=target_supply,json=targetSupply,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"target_supply"` } func (m *Minter) Reset() { *m = Minter{} } @@ -82,10 +80,14 @@ var xxx_messageInfo_Minter proto.InternalMessageInfo type Params struct { // type of coin to mint MintDenom string `protobuf:"bytes,1,opt,name=mint_denom,json=mintDenom,proto3" json:"mint_denom,omitempty"` - // annual reduction factor inflation rate change - AnnualReductionFactor github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,2,opt,name=annual_reduction_factor,json=annualReductionFactor,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"annual_reduction_factor"` - // expected blocks per year - BlocksPerYear uint64 `protobuf:"varint,3,opt,name=blocks_per_year,json=blocksPerYear,proto3" json:"blocks_per_year,omitempty"` + // annual inflation coefficient + InflationCoef github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,2,opt,name=inflation_coef,json=inflationCoef,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"inflation_coef"` + // bounding adjustment + BoundingAdjustment github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,3,opt,name=bounding_adjustment,json=boundingAdjustment,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"bounding_adjustment"` + // target boudning ratio + TargetBoundingRatio github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,4,opt,name=target_bounding_ratio,json=targetBoundingRatio,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"target_bounding_ratio"` + // expected blocks per yearmake p + BlocksPerYear uint64 `protobuf:"varint,5,opt,name=blocks_per_year,json=blocksPerYear,proto3" json:"blocks_per_year,omitempty"` } func (m *Params) Reset() { *m = Params{} } @@ -142,31 +144,32 @@ func init() { func init() { proto.RegisterFile("mint/v1beta1/mint.proto", fileDescriptor_06339c129491fd39) } var fileDescriptor_06339c129491fd39 = []byte{ - // 379 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x92, 0xcf, 0x6a, 0xea, 0x40, - 0x14, 0xc6, 0x13, 0xaf, 0x08, 0x0e, 0xca, 0xbd, 0x37, 0xdc, 0x8b, 0x56, 0x68, 0x2c, 0x2e, 0xa4, - 0x1b, 0x13, 0xa4, 0x5d, 0x15, 0x57, 0x22, 0x85, 0x2e, 0x0a, 0x12, 0x57, 0x75, 0x13, 0x26, 0xc9, - 0x98, 0x0e, 0x26, 0x33, 0x61, 0x66, 0x22, 0xf5, 0x2d, 0xba, 0xec, 0xb2, 0x8b, 0x3e, 0x42, 0x1f, - 0x42, 0xe8, 0x46, 0xba, 0x2a, 0x5d, 0x48, 0xd1, 0x17, 0x29, 0x33, 0x93, 0xfe, 0xd9, 0x16, 0xdc, - 0x24, 0x33, 0xdf, 0x19, 0x7e, 0xdf, 0xf9, 0x0e, 0x07, 0x34, 0x52, 0x4c, 0x84, 0xbb, 0xe8, 0x07, - 0x48, 0xc0, 0xbe, 0x2b, 0x2f, 0x4e, 0xc6, 0xa8, 0xa0, 0x56, 0x4d, 0x9d, 0x8b, 0x42, 0xeb, 0x20, - 0xa4, 0x3c, 0xa5, 0xdc, 0x57, 0x35, 0x57, 0x5f, 0xf4, 0xc3, 0xd6, 0xbf, 0x98, 0xc6, 0x54, 0xeb, - 0xf2, 0xa4, 0xd5, 0xce, 0x43, 0x09, 0x54, 0x2e, 0x31, 0x11, 0x88, 0x59, 0x53, 0x50, 0xc5, 0x64, - 0x96, 0x40, 0x81, 0x29, 0x69, 0x9a, 0x47, 0xe6, 0x71, 0x75, 0x38, 0x58, 0x6d, 0xda, 0xc6, 0xeb, - 0xa6, 0xdd, 0x8d, 0xb1, 0xb8, 0xce, 0x03, 0x27, 0xa4, 0x69, 0x01, 0x2d, 0x7e, 0x3d, 0x1e, 0xcd, - 0x5d, 0xb1, 0xcc, 0x10, 0x77, 0x46, 0x28, 0x7c, 0x7e, 0xec, 0x81, 0xc2, 0x73, 0x84, 0x42, 0xef, - 0x0b, 0x67, 0x61, 0xf0, 0x17, 0x12, 0x92, 0xc3, 0x44, 0x76, 0xb6, 0xc0, 0x1c, 0x53, 0xc2, 0x9b, - 0xa5, 0x3d, 0x78, 0xfc, 0xd1, 0xd8, 0xf1, 0x27, 0xd5, 0x9a, 0x80, 0xba, 0x80, 0x2c, 0x46, 0xc2, - 0xe7, 0x79, 0x96, 0x25, 0xcb, 0xe6, 0x2f, 0x65, 0xe3, 0xfc, 0xc0, 0xe6, 0x82, 0x08, 0xaf, 0xa6, - 0x21, 0x13, 0xc5, 0xe8, 0x3c, 0x99, 0xa0, 0x32, 0x86, 0x0c, 0xa6, 0xdc, 0x3a, 0x04, 0x40, 0x8e, - 0xdc, 0x8f, 0x10, 0xa1, 0xa9, 0x9e, 0x93, 0x57, 0x95, 0xca, 0x48, 0x0a, 0x96, 0x00, 0x8d, 0x22, - 0x29, 0x43, 0x51, 0x1e, 0xca, 0xf4, 0xfe, 0x0c, 0x86, 0x82, 0xb2, 0xbd, 0xe4, 0xfd, 0xaf, 0xe1, - 0xde, 0x07, 0xfb, 0x5c, 0xa1, 0xad, 0x2e, 0xf8, 0x1d, 0x24, 0x34, 0x9c, 0x73, 0x3f, 0x43, 0xcc, - 0x5f, 0x22, 0xc8, 0x54, 0xec, 0xb2, 0x57, 0xd7, 0xf2, 0x18, 0xb1, 0x2b, 0x04, 0xd9, 0x59, 0xf9, - 0xee, 0xbe, 0x6d, 0x0c, 0x07, 0xab, 0xad, 0x6d, 0xae, 0xb7, 0xb6, 0xf9, 0xb6, 0xb5, 0xcd, 0xdb, - 0x9d, 0x6d, 0xac, 0x77, 0xb6, 0xf1, 0xb2, 0xb3, 0x8d, 0x69, 0xe7, 0x5b, 0x53, 0x74, 0x9e, 0x9d, - 0xaa, 0x4f, 0xe4, 0xde, 0xa8, 0x8d, 0xd3, 0x4d, 0x05, 0x15, 0xb5, 0x39, 0x27, 0xef, 0x01, 0x00, - 0x00, 0xff, 0xff, 0x71, 0x98, 0x4c, 0x41, 0x93, 0x02, 0x00, 0x00, + // 394 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x92, 0x4f, 0x6e, 0xe2, 0x30, + 0x14, 0xc6, 0x93, 0x81, 0x41, 0xc2, 0x1a, 0xe6, 0x8f, 0x99, 0xd1, 0x64, 0x90, 0x26, 0x20, 0x16, + 0x88, 0x0d, 0x44, 0x68, 0x66, 0x35, 0x62, 0x33, 0x94, 0x6d, 0x25, 0x94, 0x5d, 0xd9, 0x44, 0x4e, + 0x62, 0x52, 0x17, 0x62, 0x47, 0xb6, 0x83, 0xca, 0x2d, 0xba, 0xac, 0xd4, 0x4d, 0x0f, 0xd1, 0x43, + 0xb0, 0x2b, 0xea, 0xaa, 0xea, 0x02, 0x55, 0x70, 0x91, 0xca, 0x71, 0x9a, 0xf6, 0x00, 0xd9, 0x24, + 0xef, 0x7d, 0xcf, 0xfa, 0x7d, 0x9f, 0xad, 0x07, 0x7e, 0xc6, 0x84, 0x4a, 0x67, 0x3d, 0xf2, 0xb1, + 0x44, 0x23, 0x47, 0x35, 0xc3, 0x84, 0x33, 0xc9, 0xe0, 0xa7, 0xac, 0xce, 0x07, 0xad, 0x5f, 0x01, + 0x13, 0x31, 0x13, 0x5e, 0x36, 0x73, 0x74, 0xa3, 0x0f, 0xb6, 0xbe, 0x47, 0x2c, 0x62, 0x5a, 0x57, + 0x95, 0x56, 0xbb, 0xf7, 0x26, 0xa8, 0x9d, 0x12, 0x2a, 0x31, 0x87, 0x73, 0x50, 0x27, 0x74, 0xb1, + 0x42, 0x92, 0x30, 0x6a, 0x99, 0x1d, 0xb3, 0x5f, 0x9f, 0x8c, 0xb7, 0xfb, 0xb6, 0xf1, 0xb4, 0x6f, + 0xf7, 0x22, 0x22, 0xcf, 0x53, 0x7f, 0x18, 0xb0, 0x38, 0x87, 0xe6, 0xbf, 0x81, 0x08, 0x97, 0x8e, + 0xdc, 0x24, 0x58, 0x0c, 0xa7, 0x38, 0x78, 0xb8, 0x1b, 0x80, 0xdc, 0x73, 0x8a, 0x03, 0xf7, 0x0d, + 0x07, 0x09, 0xf8, 0x86, 0x28, 0x4d, 0xd1, 0x4a, 0x25, 0x5b, 0x13, 0x41, 0x18, 0x15, 0xd6, 0x87, + 0x12, 0x3c, 0xbe, 0x6a, 0xec, 0xac, 0xa0, 0x76, 0x6f, 0x2a, 0xa0, 0x36, 0x43, 0x1c, 0xc5, 0x02, + 0xfe, 0x06, 0x40, 0xbd, 0x8e, 0x17, 0x62, 0xca, 0x62, 0x7d, 0x25, 0xb7, 0xae, 0x94, 0xa9, 0x12, + 0x60, 0x00, 0x3e, 0x17, 0x09, 0xbd, 0x80, 0xe1, 0x45, 0x29, 0x89, 0x1a, 0x05, 0xf3, 0x84, 0xe1, + 0x05, 0x8c, 0x41, 0xd3, 0x67, 0x29, 0x0d, 0x09, 0x8d, 0x3c, 0x14, 0x5e, 0xa4, 0x42, 0xc6, 0x98, + 0x4a, 0xab, 0x52, 0x82, 0x13, 0x7c, 0x05, 0xff, 0x2f, 0xb8, 0x30, 0x01, 0x3f, 0x24, 0xe2, 0x11, + 0x96, 0x5e, 0xe1, 0xca, 0x55, 0x18, 0xab, 0x5a, 0x82, 0x61, 0x53, 0xa3, 0x27, 0x39, 0xd9, 0x55, + 0x60, 0xd8, 0x03, 0x5f, 0xfc, 0x15, 0x0b, 0x96, 0xc2, 0x4b, 0x30, 0xf7, 0x36, 0x18, 0x71, 0xeb, + 0x63, 0xc7, 0xec, 0x57, 0xdd, 0x86, 0x96, 0x67, 0x98, 0x9f, 0x61, 0xc4, 0xff, 0x55, 0xaf, 0x6f, + 0xdb, 0xc6, 0x64, 0xbc, 0x3d, 0xd8, 0xe6, 0xee, 0x60, 0x9b, 0xcf, 0x07, 0xdb, 0xbc, 0x3a, 0xda, + 0xc6, 0xee, 0x68, 0x1b, 0x8f, 0x47, 0xdb, 0x98, 0x77, 0xdf, 0x45, 0x62, 0xcb, 0xe4, 0x6f, 0xf6, + 0x09, 0x9d, 0xcb, 0x6c, 0xd9, 0x75, 0x24, 0xbf, 0x96, 0x2d, 0xed, 0x9f, 0x97, 0x00, 0x00, 0x00, + 0xff, 0xff, 0xf8, 0x24, 0xd5, 0x53, 0x0e, 0x03, 0x00, 0x00, } func (m *Minter) Marshal() (dAtA []byte, err error) { @@ -189,16 +192,6 @@ func (m *Minter) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l - { - size := m.TargetSupply.Size() - i -= size - if _, err := m.TargetSupply.MarshalTo(dAtA[i:]); err != nil { - return 0, err - } - i = encodeVarintMint(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0x1a { size := m.AnnualProvisions.Size() i -= size @@ -245,12 +238,32 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { if m.BlocksPerYear != 0 { i = encodeVarintMint(dAtA, i, uint64(m.BlocksPerYear)) i-- - dAtA[i] = 0x18 + dAtA[i] = 0x28 } { - size := m.AnnualReductionFactor.Size() + size := m.TargetBoundingRatio.Size() i -= size - if _, err := m.AnnualReductionFactor.MarshalTo(dAtA[i:]); err != nil { + if _, err := m.TargetBoundingRatio.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintMint(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x22 + { + size := m.BoundingAdjustment.Size() + i -= size + if _, err := m.BoundingAdjustment.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintMint(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x1a + { + size := m.InflationCoef.Size() + i -= size + if _, err := m.InflationCoef.MarshalTo(dAtA[i:]); err != nil { return 0, err } i = encodeVarintMint(dAtA, i, uint64(size)) @@ -288,8 +301,6 @@ func (m *Minter) Size() (n int) { n += 1 + l + sovMint(uint64(l)) l = m.AnnualProvisions.Size() n += 1 + l + sovMint(uint64(l)) - l = m.TargetSupply.Size() - n += 1 + l + sovMint(uint64(l)) return n } @@ -303,7 +314,11 @@ func (m *Params) Size() (n int) { if l > 0 { n += 1 + l + sovMint(uint64(l)) } - l = m.AnnualReductionFactor.Size() + l = m.InflationCoef.Size() + n += 1 + l + sovMint(uint64(l)) + l = m.BoundingAdjustment.Size() + n += 1 + l + sovMint(uint64(l)) + l = m.TargetBoundingRatio.Size() n += 1 + l + sovMint(uint64(l)) if m.BlocksPerYear != 0 { n += 1 + sovMint(uint64(m.BlocksPerYear)) @@ -414,40 +429,6 @@ func (m *Minter) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex - case 3: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field TargetSupply", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowMint - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - 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.TargetSupply.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipMint(dAtA[iNdEx:]) @@ -532,7 +513,7 @@ func (m *Params) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 2: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field AnnualReductionFactor", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field InflationCoef", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 { @@ -560,11 +541,79 @@ func (m *Params) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.AnnualReductionFactor.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.InflationCoef.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field BoundingAdjustment", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMint + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + 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.BoundingAdjustment.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 4: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field TargetBoundingRatio", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMint + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + 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.TargetBoundingRatio.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 5: if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field BlocksPerYear", wireType) } From 83bfc0089e199755363c819e75503349b4b6d892 Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Wed, 23 Aug 2023 10:42:21 +0200 Subject: [PATCH 02/24] feat(mint): remove legacy store migration --- x/mint/keeper/migrations.go | 6 ++-- x/mint/migrations/v2/migrations.go | 49 ------------------------------ x/mint/migrations/v3/migrations.go | 31 +++++++++++++++++++ x/mint/types/params_legacy.go | 25 --------------- 4 files changed, 34 insertions(+), 77 deletions(-) delete mode 100644 x/mint/migrations/v2/migrations.go create mode 100644 x/mint/migrations/v3/migrations.go delete mode 100644 x/mint/types/params_legacy.go diff --git a/x/mint/keeper/migrations.go b/x/mint/keeper/migrations.go index 2da11898..9b5879ee 100644 --- a/x/mint/keeper/migrations.go +++ b/x/mint/keeper/migrations.go @@ -4,7 +4,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/okp4/okp4d/x/mint/exported" - v2 "github.com/okp4/okp4d/x/mint/migrations/v2" + v3 "github.com/okp4/okp4d/x/mint/migrations/v3" ) type Migrator struct { @@ -19,6 +19,6 @@ func NewMigrator(keeper Keeper, legacySubspace exported.Subspace) Migrator { } } -func (m Migrator) Migrate1to2(ctx sdk.Context) error { - return v2.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc, m.legacySubspace) +func (m Migrator) Migrate2to3(ctx sdk.Context) error { + return v3.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc, m.legacySubspace) } diff --git a/x/mint/migrations/v2/migrations.go b/x/mint/migrations/v2/migrations.go deleted file mode 100644 index 069c64ce..00000000 --- a/x/mint/migrations/v2/migrations.go +++ /dev/null @@ -1,49 +0,0 @@ -package v2 - -import ( - "github.com/cosmos/cosmos-sdk/codec" - storetypes "github.com/cosmos/cosmos-sdk/store/types" - sdk "github.com/cosmos/cosmos-sdk/types" - - "github.com/okp4/okp4d/x/mint/exported" - "github.com/okp4/okp4d/x/mint/types" -) - -// MigrateStore migrates the x/mint module state from the consensus version 1 to -// version 2. -// Specifically, it takes the parameters that are currently stored -// and managed by the x/params modules and stores them directly into the x/mint -// module state. -func MigrateStore(ctx sdk.Context, - storeKey storetypes.StoreKey, - cdc codec.BinaryCodec, - legacySubspace exported.Subspace, -) error { - logger := ctx.Logger(). - With("module", "mint"). - With("migration", "v2") - - logger.Debug("starting module migration") - - logger.Debug("migrate mint params") - - store := ctx.KVStore(storeKey) - - var params types.Params - legacySubspace.WithKeyTable(types.ParamKeyTable()). //nolint:staticcheck - GetParamSet(ctx, ¶ms) - - if err := params.Validate(); err != nil { - return err - } - - bz, err := cdc.Marshal(¶ms) - if err != nil { - return err - } - store.Set(types.ParamsKey, bz) - - logger.Debug("module migration done") - - return nil -} diff --git a/x/mint/migrations/v3/migrations.go b/x/mint/migrations/v3/migrations.go new file mode 100644 index 00000000..338c8f1d --- /dev/null +++ b/x/mint/migrations/v3/migrations.go @@ -0,0 +1,31 @@ +package v3 + +import ( + "github.com/cosmos/cosmos-sdk/codec" + storetypes "github.com/cosmos/cosmos-sdk/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/okp4/okp4d/x/mint/exported" +) + +// MigrateStore migrates the x/mint module state from the consensus version 2 to +// version 3. +func MigrateStore(ctx sdk.Context, + storeKey storetypes.StoreKey, + cdc codec.BinaryCodec, + legacySubspace exported.Subspace, +) error { + logger := ctx.Logger(). + With("module", "mint"). + With("migration", "v3") + + logger.Debug("starting module migration") + + logger.Debug("migrate mint params") + + // TODO: + + logger.Debug("module migration done") + + return nil +} diff --git a/x/mint/types/params_legacy.go b/x/mint/types/params_legacy.go deleted file mode 100644 index 0adec0b7..00000000 --- a/x/mint/types/params_legacy.go +++ /dev/null @@ -1,25 +0,0 @@ -package types - -import paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" - -// ParamKeyTable for minting module. -// -// Deprecated: kept for migration purpose, -// will be removed soon. -func ParamKeyTable() paramtypes.KeyTable { - return paramtypes.NewKeyTable().RegisterParamSet(&Params{}) -} - -var _ paramtypes.ParamSet = (*Params)(nil) - -// ParamSetPairs Implements params.ParamSet. -// -// Deprecated: kept for migration purpose, -// will be removed soon. -func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs { - return paramtypes.ParamSetPairs{ - paramtypes.NewParamSetPair(KeyMintDenom, &p.MintDenom, validateMintDenom), - paramtypes.NewParamSetPair(KeyAnnualReductionFactor, &p.AnnualReductionFactor, validateAnnualReductionFactor), - paramtypes.NewParamSetPair(KeyBlocksPerYear, &p.BlocksPerYear, validateBlocksPerYear), - } -} From 6484ce4a6f9dc90baa8f11a54bf6c0704b06dea7 Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Wed, 23 Aug 2023 10:42:51 +0200 Subject: [PATCH 03/24] feat(mint): instantiate default new mint params --- x/mint/abci.go | 53 -------------------------- x/mint/module.go | 4 +- x/mint/simulation/genesis.go | 44 ++++++++++++++++------ x/mint/simulation/proposals.go | 4 +- x/mint/types/minter.go | 26 ++++--------- x/mint/types/params.go | 68 +++++++++++++++++++++++++--------- 6 files changed, 95 insertions(+), 104 deletions(-) diff --git a/x/mint/abci.go b/x/mint/abci.go index 59845fe0..1c33e81d 100644 --- a/x/mint/abci.go +++ b/x/mint/abci.go @@ -1,65 +1,12 @@ package mint import ( - "time" - - "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/okp4/okp4d/x/mint/keeper" - "github.com/okp4/okp4d/x/mint/types" ) // BeginBlocker mints new tokens for the previous block. func BeginBlocker(ctx sdk.Context, k keeper.Keeper) { - defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) - - // fetch stored minter & params - minter := k.GetMinter(ctx) - params := k.GetParams(ctx) - - totalSupply := k.TokenSupply(ctx, params.MintDenom) - - if uint64(ctx.BlockHeight()) == 1 { - minter.AnnualProvisions = minter.NextAnnualProvisions(params, totalSupply) - minter.TargetSupply = totalSupply.Add(minter.AnnualProvisions.TruncateInt()) - k.SetMinter(ctx, minter) - } - - // If we have reached the end of the year by reaching the targeted supply for the year - // We need to re-calculate the next inflation for the next year. - if totalSupply.GTE(minter.TargetSupply) { - minter.Inflation = minter.NextInflation(params) - minter.AnnualProvisions = minter.NextAnnualProvisions(params, totalSupply) - minter.TargetSupply = totalSupply.Add(minter.AnnualProvisions.TruncateInt()) - k.SetMinter(ctx, minter) - } - - // mint coins, update supply - mintedCoin := minter.BlockProvision(params, totalSupply) - mintedCoins := sdk.NewCoins(mintedCoin) - - err := k.MintCoins(ctx, mintedCoins) - if err != nil { - panic(err) - } - - // send the minted coins to the fee collector account - err = k.AddCollectedFees(ctx, mintedCoins) - if err != nil { - panic(err) - } - - if mintedCoin.Amount.IsInt64() { - defer telemetry.ModuleSetGauge(types.ModuleName, float32(mintedCoin.Amount.Int64()), "minted_tokens") - } - ctx.EventManager().EmitEvent( - sdk.NewEvent( - types.EventTypeMint, - sdk.NewAttribute(types.AttributeKeyInflation, minter.Inflation.String()), - sdk.NewAttribute(types.AttributeKeyAnnualProvisions, minter.AnnualProvisions.String()), - sdk.NewAttribute(sdk.AttributeKeyAmount, mintedCoin.Amount.String()), - ), - ) } diff --git a/x/mint/module.go b/x/mint/module.go index e7a66002..2cd8e541 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -121,7 +121,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { fromVersion uint64 migrator func(ctx sdk.Context) error }{ - {1, migrator.Migrate1to2}, + {2, migrator.Migrate2to3}, } for _, migration := range migrations { @@ -150,7 +150,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw } // ConsensusVersion implements AppModule/ConsensusVersion. -func (AppModule) ConsensusVersion() uint64 { return 2 } +func (AppModule) ConsensusVersion() uint64 { return 3 } // BeginBlock returns the begin blocker for the mint module. func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) { diff --git a/x/mint/simulation/genesis.go b/x/mint/simulation/genesis.go index 4c389910..d5512047 100644 --- a/x/mint/simulation/genesis.go +++ b/x/mint/simulation/genesis.go @@ -15,8 +15,10 @@ import ( // Simulation parameter constants. const ( - Inflation = "inflation" - AnnualReductionFactor = "annual_reduction_factor" + Inflation = "inflation" + InflationCoef = "inflation_coef" + BoundingAdjustment = "bounding_adjustment" + TargetBoundingRatio = "target_bounding_ratio" ) // GenInflation randomized Inflation. @@ -24,9 +26,19 @@ func GenInflation(r *rand.Rand) sdk.Dec { return sdk.NewDecWithPrec(int64(r.Intn(99)), 2) } -// GenAnnualReductionFactor randomized AnnualReductionFactor. -func GenAnnualReductionFactorMax(_ *rand.Rand) sdk.Dec { - return sdk.NewDecWithPrec(20, 2) +// GenInflationCoefMax randomized AnnualReductionFactor. +func GenInflationCoefMax(_ *rand.Rand) sdk.Dec { + return sdk.NewDecWithPrec(73, 3) +} + +// GenBoundingAdjustmentMax randomized AnnualReductionFactor. +func GenBoundingAdjustmentMax(_ *rand.Rand) sdk.Dec { + return sdk.NewDecWithPrec(25, 1) +} + +// GenTargetBoundingRatioMax randomized AnnualReductionFactor. +func GenTargetBoundingRatioMax(_ *rand.Rand) sdk.Dec { + return sdk.NewDecWithPrec(66, 2) } // RandomizedGenState generates a random GenesisState for mint. @@ -42,19 +54,29 @@ func RandomizedGenState(simState *module.SimulationState) { // params - var annualReductionFactor sdk.Dec + var inflationCoef sdk.Dec + simState.AppParams.GetOrGenerate( + simState.Cdc, InflationCoef, &inflationCoef, simState.Rand, + func(r *rand.Rand) { inflationCoef = GenInflationCoefMax(r) }, + ) + var targetBoundingRatio sdk.Dec + simState.AppParams.GetOrGenerate( + simState.Cdc, TargetBoundingRatio, &targetBoundingRatio, simState.Rand, + func(r *rand.Rand) { targetBoundingRatio = GenTargetBoundingRatioMax(r) }, + ) + + var boundingAdjustment sdk.Dec simState.AppParams.GetOrGenerate( - simState.Cdc, AnnualReductionFactor, &annualReductionFactor, simState.Rand, - func(r *rand.Rand) { annualReductionFactor = GenAnnualReductionFactorMax(r) }, + simState.Cdc, BoundingAdjustment, &boundingAdjustment, simState.Rand, + func(r *rand.Rand) { boundingAdjustment = GenBoundingAdjustmentMax(r) }, ) mintDenom := sdk.DefaultBondDenom blocksPerYear := uint64(60 * 60 * 8766 / 5) - params := types.NewParams(mintDenom, annualReductionFactor, blocksPerYear) + params := types.NewParams(mintDenom, inflationCoef, boundingAdjustment, targetBoundingRatio, blocksPerYear) annualProvision := inflation.MulInt(simState.InitialStake) - targetSupply := simState.InitialStake.Add(annualProvision.TruncateInt()) - minter := types.InitialMinter(inflation, targetSupply) + minter := types.InitialMinter(inflation) minter.AnnualProvisions = annualProvision mintGenesis := types.NewGenesisState(minter, params) diff --git a/x/mint/simulation/proposals.go b/x/mint/simulation/proposals.go index b34d982e..93c9a216 100644 --- a/x/mint/simulation/proposals.go +++ b/x/mint/simulation/proposals.go @@ -36,7 +36,9 @@ func SimulateMsgUpdateParams(r *rand.Rand, _ sdk.Context, _ []simtypes.Account) params := types.DefaultParams() params.BlocksPerYear = uint64(simtypes.RandIntBetween(r, 1, 1000000)) - params.AnnualReductionFactor = sdk.NewDecWithPrec(int64(simtypes.RandIntBetween(r, 1, 100)), 2) + params.InflationCoef = sdk.NewDecWithPrec(int64(simtypes.RandIntBetween(r, 1, 100)), 2) + params.TargetBoundingRatio = sdk.NewDecWithPrec(int64(simtypes.RandIntBetween(r, 1, 100)), 2) + params.BoundingAdjustment = sdk.NewDecWithPrec(int64(simtypes.RandIntBetween(r, 1, 100)), 2) params.MintDenom = simtypes.RandStringOfLength(r, 10) return &types.MsgUpdateParams{ diff --git a/x/mint/types/minter.go b/x/mint/types/minter.go index 8f64015b..6daf9253 100644 --- a/x/mint/types/minter.go +++ b/x/mint/types/minter.go @@ -10,29 +10,26 @@ import ( // NewMinter returns a new Minter object with the given inflation, annual // provisions values and annual reduction factor. -func NewMinter(inflation, annualProvisions sdk.Dec, targetSupply math.Int) Minter { +func NewMinter(inflation, annualProvisions sdk.Dec) Minter { return Minter{ Inflation: inflation, AnnualProvisions: annualProvisions, - TargetSupply: targetSupply, } } // InitialMinter returns an initial Minter object with a given inflation value and annual reduction factor. -func InitialMinter(inflation sdk.Dec, targetSupply math.Int) Minter { +func InitialMinter(inflation sdk.Dec) Minter { return NewMinter( inflation, sdk.NewDec(0), - targetSupply, ) } // DefaultInitialMinter returns a default initial Minter object for a new chain -// which uses an inflation rate of 15%. +// which uses an inflation rate of 18%. func DefaultInitialMinter() Minter { return InitialMinter( - sdk.NewDecWithPrec(15, 2), - math.NewInt(230000000000000), + sdk.NewDecWithPrec(18, 2), ) } @@ -48,27 +45,18 @@ func ValidateMinter(minter Minter) error { // NextInflation return the new inflation rate for the next year // Get the current inflation and multiply by (1 - annual reduction factor). func (m Minter) NextInflation(params Params) sdk.Dec { - return m.Inflation.Mul(sdk.OneDec().Sub(params.AnnualReductionFactor)) + return m.Inflation } // NextAnnualProvisions returns the annual provisions based on current total // supply and inflation rate. -func (m Minter) NextAnnualProvisions(_ Params, totalSupply math.Int) sdk.Dec { +func (m Minter) NextAnnualProvisions(_ Params, totalSupply math.Int) math.LegacyDec { return m.Inflation.MulInt(totalSupply) } // BlockProvision returns the provisions for a block based on the annual // provisions rate. -func (m Minter) BlockProvision(params Params, totalSupply math.Int) sdk.Coin { +func (m Minter) BlockProvision(params Params) sdk.Coin { provisionAmt := m.AnnualProvisions.QuoInt(sdk.NewInt(int64(params.BlocksPerYear))) - - // Fixe rounding by limiting to the target supply at the end of the year block. - futureSupply := totalSupply.Add(provisionAmt.TruncateInt()) - if futureSupply.GT(m.TargetSupply) { - // In case of a rounding is not precise enough, truncating int of provisionAmt could return Zero - // To avoid negative coin if provisionAmt is equal to Zero, return minimum Zero or more coin. - return sdk.NewCoin(params.MintDenom, sdk.MaxInt(m.TargetSupply.Sub(totalSupply), sdk.ZeroInt())) - } - return sdk.NewCoin(params.MintDenom, provisionAmt.TruncateInt()) } diff --git a/x/mint/types/params.go b/x/mint/types/params.go index 85b9a5ee..7c95c6e1 100644 --- a/x/mint/types/params.go +++ b/x/mint/types/params.go @@ -10,29 +10,26 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -// Parameter store keys. -var ( - KeyMintDenom = []byte("MintDenom") - KeyAnnualReductionFactor = []byte("AnnualReductionFactor") - KeyBlocksPerYear = []byte("BlocksPerYear") -) - func NewParams( - mintDenom string, annualReductionFactor sdk.Dec, blocksPerYear uint64, + mintDenom string, inflationCoef, boundingAdjustment, targetBoundingRatio sdk.Dec, blocksPerYear uint64, ) Params { return Params{ - MintDenom: mintDenom, - AnnualReductionFactor: annualReductionFactor, - BlocksPerYear: blocksPerYear, + MintDenom: mintDenom, + InflationCoef: inflationCoef, + BoundingAdjustment: boundingAdjustment, + TargetBoundingRatio: targetBoundingRatio, + BlocksPerYear: blocksPerYear, } } // default minting module parameters. func DefaultParams() Params { return Params{ - MintDenom: sdk.DefaultBondDenom, - AnnualReductionFactor: sdk.NewDecWithPrec(20, 2), // Tha annual reduction factor is configured to 20% per year - BlocksPerYear: uint64(60 * 60 * 8766 / 5), // assuming 5-second block times + MintDenom: sdk.DefaultBondDenom, + InflationCoef: sdk.NewDecWithPrec(73, 3), + BoundingAdjustment: sdk.NewDecWithPrec(25, 1), + TargetBoundingRatio: sdk.NewDecWithPrec(66, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), // assuming 5-second block times } } @@ -41,7 +38,13 @@ func (p Params) Validate() error { if err := validateMintDenom(p.MintDenom); err != nil { return err } - if err := validateAnnualReductionFactor(p.AnnualReductionFactor); err != nil { + if err := validateInflationCoef(p.InflationCoef); err != nil { + return err + } + if err := validateBoundingAdjustment(p.BoundingAdjustment); err != nil { + return err + } + if err := validateTargetBoundingRatio(p.TargetBoundingRatio); err != nil { return err } @@ -67,17 +70,46 @@ func validateMintDenom(i interface{}) error { return sdk.ValidateDenom(v) } -func validateAnnualReductionFactor(i interface{}) error { +func validateInflationCoef(i interface{}) error { + v, ok := i.(sdk.Dec) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + if v.IsNegative() { + return fmt.Errorf("inflation coefficient cannot be negative: %s", v) + } + if v.GT(sdk.OneDec()) { + return fmt.Errorf("inflation coefficient too large: %s", v) + } + + return nil +} + +func validateBoundingAdjustment(i interface{}) error { + v, ok := i.(sdk.Dec) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + if v.IsNegative() { + return fmt.Errorf("inflation coefficient cannot be negative: %s", v) + } + + return nil +} + +func validateTargetBoundingRatio(i interface{}) error { v, ok := i.(sdk.Dec) if !ok { return fmt.Errorf("invalid parameter type: %T", i) } if v.IsNegative() { - return fmt.Errorf("annual reduction factor cannot be negative: %s", v) + return fmt.Errorf("target bounding ratio cannot be negative: %s", v) } if v.GT(sdk.OneDec()) { - return fmt.Errorf("annual reduction factor too large: %s", v) + return fmt.Errorf("target bounding ratio too large: %s", v) } return nil From a2bc105de2898a23e743b33c51138a6387328677 Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Wed, 23 Aug 2023 11:26:14 +0200 Subject: [PATCH 04/24] feat(mint): add new mint function calculation --- x/mint/abci.go | 44 ++++++++++++++++++++++++++++++++++++++++++ x/mint/types/minter.go | 5 +++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/x/mint/abci.go b/x/mint/abci.go index 1c33e81d..a5ed50ec 100644 --- a/x/mint/abci.go +++ b/x/mint/abci.go @@ -1,12 +1,56 @@ package mint import ( + "time" + + "github.com/cosmos/cosmos-sdk/telemetry" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/okp4/okp4d/x/mint/keeper" + "github.com/okp4/okp4d/x/mint/types" ) // BeginBlocker mints new tokens for the previous block. func BeginBlocker(ctx sdk.Context, k keeper.Keeper) { + defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) + + // fetch stored minter & params + minter := k.GetMinter(ctx) + params := k.GetParams(ctx) + + // recalculate inflation rate + totalSupply := k.TokenSupply(ctx, params.MintDenom) + bondedRatio := k.BondedRatio(ctx) + minter.Inflation = minter.NextInflation(params, bondedRatio) + minter.AnnualProvisions = minter.NextAnnualProvisions(params, totalSupply) + k.SetMinter(ctx, minter) + + // mint coins, update supply + mintedCoin := minter.BlockProvision(params) + mintedCoins := sdk.NewCoins(mintedCoin) + + err := k.MintCoins(ctx, mintedCoins) + if err != nil { + panic(err) + } + + // send the minted coins to the fee collector account + err = k.AddCollectedFees(ctx, mintedCoins) + if err != nil { + panic(err) + } + + if mintedCoin.Amount.IsInt64() { + defer telemetry.ModuleSetGauge(types.ModuleName, float32(mintedCoin.Amount.Int64()), "minted_tokens") + } + ctx.EventManager().EmitEvent( + sdk.NewEvent( + types.EventTypeMint, + sdk.NewAttribute(types.AttributeKeyBondedRatio, bondedRatio.String()), + sdk.NewAttribute(types.AttributeKeyInflation, minter.Inflation.String()), + sdk.NewAttribute(types.AttributeKeyAnnualProvisions, minter.AnnualProvisions.String()), + sdk.NewAttribute(sdk.AttributeKeyAmount, mintedCoin.Amount.String()), + ), + ) } diff --git a/x/mint/types/minter.go b/x/mint/types/minter.go index 6daf9253..c9db82f6 100644 --- a/x/mint/types/minter.go +++ b/x/mint/types/minter.go @@ -44,8 +44,9 @@ func ValidateMinter(minter Minter) error { // NextInflation return the new inflation rate for the next year // Get the current inflation and multiply by (1 - annual reduction factor). -func (m Minter) NextInflation(params Params) sdk.Dec { - return m.Inflation +func (m Minter) NextInflation(params Params, boundedRatio sdk.Dec) sdk.Dec { + bounded := params.BoundingAdjustment.Sub(boundedRatio.Quo(params.TargetBoundingRatio)) + return params.InflationCoef.Mul(bounded).Mul(sdk.NewDec(100)) } // NextAnnualProvisions returns the annual provisions based on current total From 3ea9694788438a7256235ebdd804937c1bf3a7bb Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Thu, 24 Aug 2023 11:39:14 +0200 Subject: [PATCH 05/24] fix(build): update chain-upgrade makefile task to make it work with latest version --- Makefile | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index cf12d42a..13a28fcd 100644 --- a/Makefile +++ b/Makefile @@ -269,11 +269,11 @@ chain-upgrade: build ## Test the chain upgrade from the given FROM_VERSION to th PROPOSAL=${PROPOSAL}; \ if [[ ! -f "$$PROPOSAL" ]]; then \ echo "${COLOR_CYAN} 👩‍🚀 No proposal given ${COLOR_RESET}"; \ - echo '{"messages": [{"@type": "/cosmos.upgrade.v1beta1.MsgSoftwareUpgrade","authority": "okp410d07y265gmmuvt4z0w9aw880jnsr700jh7kd2g","plan": {"name": "","time": "0001-01-01T00:00:00Z","height": "10","info": "","upgraded_client_state": null}}],"metadata": "ipfs://CID","deposit": "1uknow"}' | \ + echo '{"messages": [{"@type": "/cosmos.upgrade.v1beta1.MsgSoftwareUpgrade","authority": "okp410d07y265gmmuvt4z0w9aw880jnsr700jh7kd2g","plan": {"name": "","time": "0001-01-01T00:00:00Z","height": "10","info": "","upgraded_client_state": null}}],"title": "Software update", "summary": "Update the binary", "metadata": "ipfs://CID","deposit": "1uknow"}' | \ jq --arg name "${TO_VERSION}" '.messages[].plan.name = $$name' > ${TARGET_FOLDER}/proposal.json; \ PROPOSAL=${TARGET_FOLDER}/proposal.json; \ fi; \ - cat <<< $$(jq '.app_state.gov.voting_params.voting_period = "20s"' ${CHAIN_HOME}/config/genesis.json) > ${CHAIN_HOME}/config/genesis.json; \ + cat <<< $$(jq '.app_state.gov.params.voting_period = "30s"' ${CHAIN_HOME}/config/genesis.json) > ${CHAIN_HOME}/config/genesis.json; \ \ cosmovisor init $$BINARY_OLD; \ cosmovisor run start --moniker ${CHAIN_MONIKER} \ @@ -286,23 +286,25 @@ chain-upgrade: build ## Test the chain upgrade from the given FROM_VERSION to th --home ${CHAIN_HOME} \ --chain-id okp4-${CHAIN} \ --keyring-backend test \ - -b block; \ + -b sync; \ \ + sleep 5;\ $$BINARY_OLD tx gov deposit 1 10000000uknow \ --from validator \ --yes \ --home ${CHAIN_HOME} \ --chain-id okp4-${CHAIN} \ --keyring-backend test \ - -b block; \ + -b sync; \ \ + sleep 5;\ $$BINARY_OLD tx gov vote 1 yes \ --from validator \ --yes \ --home ${CHAIN_HOME} \ --chain-id okp4-${CHAIN} \ --keyring-backend test \ - -b block; \ + -b sync; \ mkdir -p ${DAEMON_HOME}/cosmovisor/upgrades/${TO_VERSION}/bin && cp ${CHAIN_BINARY} ${DAEMON_HOME}/cosmovisor/upgrades/${TO_VERSION}/bin; \ wait From 3df45574c96cca5b753e465bcea2991459c9b80a Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Thu, 24 Aug 2023 11:40:07 +0200 Subject: [PATCH 06/24] feat(mint): create migration for v3 --- x/mint/keeper/migrations.go | 11 ++++------- x/mint/migrations/v3/migrations.go | 16 ++++++++++------ x/mint/module.go | 8 ++------ 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/x/mint/keeper/migrations.go b/x/mint/keeper/migrations.go index 9b5879ee..8ed612a9 100644 --- a/x/mint/keeper/migrations.go +++ b/x/mint/keeper/migrations.go @@ -3,22 +3,19 @@ package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/okp4/okp4d/x/mint/exported" v3 "github.com/okp4/okp4d/x/mint/migrations/v3" ) type Migrator struct { - keeper Keeper - legacySubspace exported.Subspace + keeper Keeper } -func NewMigrator(keeper Keeper, legacySubspace exported.Subspace) Migrator { +func NewMigrator(keeper Keeper) Migrator { return Migrator{ - keeper: keeper, - legacySubspace: legacySubspace, + keeper: keeper, } } func (m Migrator) Migrate2to3(ctx sdk.Context) error { - return v3.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc, m.legacySubspace) + return v3.MigrateStore(ctx, ctx.KVStore(m.keeper.storeKey), m.keeper.cdc) } diff --git a/x/mint/migrations/v3/migrations.go b/x/mint/migrations/v3/migrations.go index 338c8f1d..49c15f8f 100644 --- a/x/mint/migrations/v3/migrations.go +++ b/x/mint/migrations/v3/migrations.go @@ -2,18 +2,16 @@ package v3 import ( "github.com/cosmos/cosmos-sdk/codec" - storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" - - "github.com/okp4/okp4d/x/mint/exported" + "github.com/okp4/okp4d/x/mint/types" ) // MigrateStore migrates the x/mint module state from the consensus version 2 to // version 3. +// This version include new/deleted parameters in store. func MigrateStore(ctx sdk.Context, - storeKey storetypes.StoreKey, + store sdk.KVStore, cdc codec.BinaryCodec, - legacySubspace exported.Subspace, ) error { logger := ctx.Logger(). With("module", "mint"). @@ -23,7 +21,13 @@ func MigrateStore(ctx sdk.Context, logger.Debug("migrate mint params") - // TODO: + newParams := types.DefaultParams() + + bz, err := cdc.Marshal(&newParams) + if err != nil { + return err + } + store.Set(types.ParamsKey, bz) logger.Debug("module migration done") diff --git a/x/mint/module.go b/x/mint/module.go index 2cd8e541..b9d133d9 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -18,7 +18,6 @@ import ( simtypes "github.com/cosmos/cosmos-sdk/types/simulation" "github.com/okp4/okp4d/x/mint/client/cli" - "github.com/okp4/okp4d/x/mint/exported" "github.com/okp4/okp4d/x/mint/keeper" "github.com/okp4/okp4d/x/mint/simulation" "github.com/okp4/okp4d/x/mint/types" @@ -87,17 +86,14 @@ type AppModule struct { keeper keeper.Keeper authKeeper types.AccountKeeper - // legacySubspace is used principally for migration - legacySubspace exported.Subspace } // NewAppModule creates a new AppModule object. -func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak types.AccountKeeper, legacySubspace exported.Subspace) AppModule { +func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak types.AccountKeeper) AppModule { return AppModule{ AppModuleBasic: AppModuleBasic{cdc: cdc}, keeper: keeper, authKeeper: ak, - legacySubspace: legacySubspace, } } @@ -115,7 +111,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) types.RegisterQueryServer(cfg.QueryServer(), am.keeper) - migrator := keeper.NewMigrator(am.keeper, am.legacySubspace) + migrator := keeper.NewMigrator(am.keeper) migrations := []struct { fromVersion uint64 From e38ae03fea72b3715593287766c08a7d68940d77 Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Thu, 24 Aug 2023 11:40:46 +0200 Subject: [PATCH 07/24] feat(mint): register migration handler --- app/app.go | 2 +- app/upgrades.go | 8 ++++++++ app/upgrades/v6/upgrade.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 app/upgrades/v6/upgrade.go diff --git a/app/app.go b/app/app.go index 7f5c7087..5b7fdc92 100644 --- a/app/app.go +++ b/app/app.go @@ -683,7 +683,7 @@ func New( feegrantmodule.NewAppModule(appCodec, app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, app.interfaceRegistry), groupmodule.NewAppModule(appCodec, app.GroupKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry), gov.NewAppModule(appCodec, &app.GovKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(govtypes.ModuleName)), - mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper, app.GetSubspace(minttypes.ModuleName)), + mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper), slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, diff --git a/app/upgrades.go b/app/upgrades.go index 2f7a8c83..e3819393 100644 --- a/app/upgrades.go +++ b/app/upgrades.go @@ -9,6 +9,7 @@ import ( v4 "github.com/okp4/okp4d/app/upgrades/v4" v41 "github.com/okp4/okp4d/app/upgrades/v41" v5 "github.com/okp4/okp4d/app/upgrades/v5" + v6 "github.com/okp4/okp4d/app/upgrades/v6" ) func (app *App) setupUpgradeHandlers() { @@ -27,6 +28,11 @@ func (app *App) setupUpgradeHandlers() { v5.CreateUpgradeHandler(app.ParamsKeeper, &app.ConsensusParamsKeeper, app.mm, app.configurator), ) + app.UpgradeKeeper.SetUpgradeHandler( + v6.UpgradeName, + v6.CreateUpgradeHandler(app.mm, app.configurator), + ) + upgradeInfo, err := app.UpgradeKeeper.ReadUpgradeInfoFromDisk() if err != nil { panic(fmt.Errorf("failed to read upgrade info from disk: %w", err)) @@ -44,6 +50,8 @@ func (app *App) setupUpgradeHandlers() { storeUpgrades = v41.StoreUpgrades case v5.UpgradeName: storeUpgrades = v5.StoreUpgrades + case v6.UpgradeName: + storeUpgrades = v6.StoreUpgrades } if storeUpgrades != nil { diff --git a/app/upgrades/v6/upgrade.go b/app/upgrades/v6/upgrade.go new file mode 100644 index 00000000..8f3f0660 --- /dev/null +++ b/app/upgrades/v6/upgrade.go @@ -0,0 +1,30 @@ +package v5 + +import ( + storetypes "github.com/cosmos/cosmos-sdk/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" +) + +const UpgradeName = "v6.0.0" + +var StoreUpgrades = &storetypes.StoreUpgrades{ + Added: []string{ + "feeibc", + }, +} + +// CreateUpgradeHandler is the handler that will perform migration from v5.0.0 to v6.0.0. +// Migrate the mint module with new parameters. +func CreateUpgradeHandler( + mm *module.Manager, + configurator module.Configurator, +) upgradetypes.UpgradeHandler { + return func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) { + logger := ctx.Logger().With("upgrade", UpgradeName) + + logger.Debug("running module migrations...") + return mm.RunMigrations(ctx, configurator, vm) + } +} From 8bae96245500965027871f6968dfe02cd867e771 Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Thu, 24 Aug 2023 14:07:25 +0200 Subject: [PATCH 08/24] fix(mint): migration get old mint denom --- x/mint/migrations/v3/migrations.go | 9 + x/mint/migrations/v3/types/minter.pb.go | 690 ++++++++++++++++++++++++ x/mint/migrations/v3/types/params.go | 97 ++++ 3 files changed, 796 insertions(+) create mode 100644 x/mint/migrations/v3/types/minter.pb.go create mode 100644 x/mint/migrations/v3/types/params.go diff --git a/x/mint/migrations/v3/migrations.go b/x/mint/migrations/v3/migrations.go index 49c15f8f..27fd69c6 100644 --- a/x/mint/migrations/v3/migrations.go +++ b/x/mint/migrations/v3/migrations.go @@ -3,6 +3,7 @@ package v3 import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + oldTypes "github.com/okp4/okp4d/x/mint/migrations/v3/types" "github.com/okp4/okp4d/x/mint/types" ) @@ -21,7 +22,15 @@ func MigrateStore(ctx sdk.Context, logger.Debug("migrate mint params") + var oldParams oldTypes.Params + d := store.Get(types.ParamsKey) + err := cdc.Unmarshal(d, &oldParams) + if err != nil { + return err + } + newParams := types.DefaultParams() + newParams.MintDenom = oldParams.MintDenom bz, err := cdc.Marshal(&newParams) if err != nil { diff --git a/x/mint/migrations/v3/types/minter.pb.go b/x/mint/migrations/v3/types/minter.pb.go new file mode 100644 index 00000000..14252479 --- /dev/null +++ b/x/mint/migrations/v3/types/minter.pb.go @@ -0,0 +1,690 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: mint/v1beta1/mint.proto + +package types + +import ( + fmt "fmt" + _ "github.com/cosmos/cosmos-proto" + github_com_cosmos_cosmos_sdk_types "github.com/cosmos/cosmos-sdk/types" + _ "github.com/cosmos/gogoproto/gogoproto" + proto "github.com/cosmos/gogoproto/proto" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +// Minter represents the minting state. +// +// At the beginning of the chain (first block) the mint module will recalculate the `annual_provisions` and +// `target_supply` based on the genesis total token supply and the inflation configured. +// By default inflation is set to 15%. If the genesis total token supply is 200M token, the `annual_provision` will be 30M +// and `target_supply` 230M. +type Minter struct { + // current annual inflation rate + Inflation github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,1,opt,name=inflation,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"inflation"` + // current annual expected provisions + AnnualProvisions github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,2,opt,name=annual_provisions,json=annualProvisions,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"annual_provisions"` + // target supply at end of period + TargetSupply github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,3,opt,name=target_supply,json=targetSupply,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"target_supply"` +} + +func (m *Minter) Reset() { *m = Minter{} } +func (m *Minter) String() string { return proto.CompactTextString(m) } +func (*Minter) ProtoMessage() {} +func (*Minter) Descriptor() ([]byte, []int) { + return fileDescriptor_06339c129491fd39, []int{0} +} +func (m *Minter) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *Minter) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_Minter.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *Minter) XXX_Merge(src proto.Message) { + xxx_messageInfo_Minter.Merge(m, src) +} +func (m *Minter) XXX_Size() int { + return m.Size() +} +func (m *Minter) XXX_DiscardUnknown() { + xxx_messageInfo_Minter.DiscardUnknown(m) +} + +var xxx_messageInfo_Minter proto.InternalMessageInfo + +// Params holds parameters for the mint module. +// +// Configure the annual reduction factor will update at the each end of year the new token distribution rate by reducing +// the actual inflation by the `annual_reduction_factor` configured. +// By default, `annual_reduction_factor` is 20%. For example, with an initial inflation of 15%, at the end of the year, +// new inflation will be 12%. +type Params struct { + // type of coin to mint + MintDenom string `protobuf:"bytes,1,opt,name=mint_denom,json=mintDenom,proto3" json:"mint_denom,omitempty"` + // annual reduction factor inflation rate change + AnnualReductionFactor github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,2,opt,name=annual_reduction_factor,json=annualReductionFactor,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"annual_reduction_factor"` + // expected blocks per year + BlocksPerYear uint64 `protobuf:"varint,3,opt,name=blocks_per_year,json=blocksPerYear,proto3" json:"blocks_per_year,omitempty"` +} + +func (m *Params) Reset() { *m = Params{} } +func (*Params) ProtoMessage() {} +func (*Params) Descriptor() ([]byte, []int) { + return fileDescriptor_06339c129491fd39, []int{1} +} +func (m *Params) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *Params) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_Params.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *Params) XXX_Merge(src proto.Message) { + xxx_messageInfo_Params.Merge(m, src) +} +func (m *Params) XXX_Size() int { + return m.Size() +} +func (m *Params) XXX_DiscardUnknown() { + xxx_messageInfo_Params.DiscardUnknown(m) +} + +var xxx_messageInfo_Params proto.InternalMessageInfo + +func (m *Params) GetMintDenom() string { + if m != nil { + return m.MintDenom + } + return "" +} + +func (m *Params) GetBlocksPerYear() uint64 { + if m != nil { + return m.BlocksPerYear + } + return 0 +} + +func init() { + proto.RegisterType((*Minter)(nil), "mint.v1beta1.Minter") + proto.RegisterType((*Params)(nil), "mint.v1beta1.Params") +} + +func init() { proto.RegisterFile("mint/v1beta1/mint.proto", fileDescriptor_06339c129491fd39) } + +var fileDescriptor_06339c129491fd39 = []byte{ + // 379 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x92, 0xcf, 0x6a, 0xea, 0x40, + 0x14, 0xc6, 0x13, 0xaf, 0x08, 0x0e, 0xca, 0xbd, 0x37, 0xdc, 0x8b, 0x56, 0x68, 0x2c, 0x2e, 0xa4, + 0x1b, 0x13, 0xa4, 0x5d, 0x15, 0x57, 0x22, 0x85, 0x2e, 0x0a, 0x12, 0x57, 0x75, 0x13, 0x26, 0xc9, + 0x98, 0x0e, 0x26, 0x33, 0x61, 0x66, 0x22, 0xf5, 0x2d, 0xba, 0xec, 0xb2, 0x8b, 0x3e, 0x42, 0x1f, + 0x42, 0xe8, 0x46, 0xba, 0x2a, 0x5d, 0x48, 0xd1, 0x17, 0x29, 0x33, 0x93, 0xfe, 0xd9, 0x16, 0xdc, + 0x24, 0x33, 0xdf, 0x19, 0x7e, 0xdf, 0xf9, 0x0e, 0x07, 0x34, 0x52, 0x4c, 0x84, 0xbb, 0xe8, 0x07, + 0x48, 0xc0, 0xbe, 0x2b, 0x2f, 0x4e, 0xc6, 0xa8, 0xa0, 0x56, 0x4d, 0x9d, 0x8b, 0x42, 0xeb, 0x20, + 0xa4, 0x3c, 0xa5, 0xdc, 0x57, 0x35, 0x57, 0x5f, 0xf4, 0xc3, 0xd6, 0xbf, 0x98, 0xc6, 0x54, 0xeb, + 0xf2, 0xa4, 0xd5, 0xce, 0x43, 0x09, 0x54, 0x2e, 0x31, 0x11, 0x88, 0x59, 0x53, 0x50, 0xc5, 0x64, + 0x96, 0x40, 0x81, 0x29, 0x69, 0x9a, 0x47, 0xe6, 0x71, 0x75, 0x38, 0x58, 0x6d, 0xda, 0xc6, 0xeb, + 0xa6, 0xdd, 0x8d, 0xb1, 0xb8, 0xce, 0x03, 0x27, 0xa4, 0x69, 0x01, 0x2d, 0x7e, 0x3d, 0x1e, 0xcd, + 0x5d, 0xb1, 0xcc, 0x10, 0x77, 0x46, 0x28, 0x7c, 0x7e, 0xec, 0x81, 0xc2, 0x73, 0x84, 0x42, 0xef, + 0x0b, 0x67, 0x61, 0xf0, 0x17, 0x12, 0x92, 0xc3, 0x44, 0x76, 0xb6, 0xc0, 0x1c, 0x53, 0xc2, 0x9b, + 0xa5, 0x3d, 0x78, 0xfc, 0xd1, 0xd8, 0xf1, 0x27, 0xd5, 0x9a, 0x80, 0xba, 0x80, 0x2c, 0x46, 0xc2, + 0xe7, 0x79, 0x96, 0x25, 0xcb, 0xe6, 0x2f, 0x65, 0xe3, 0xfc, 0xc0, 0xe6, 0x82, 0x08, 0xaf, 0xa6, + 0x21, 0x13, 0xc5, 0xe8, 0x3c, 0x99, 0xa0, 0x32, 0x86, 0x0c, 0xa6, 0xdc, 0x3a, 0x04, 0x40, 0x8e, + 0xdc, 0x8f, 0x10, 0xa1, 0xa9, 0x9e, 0x93, 0x57, 0x95, 0xca, 0x48, 0x0a, 0x96, 0x00, 0x8d, 0x22, + 0x29, 0x43, 0x51, 0x1e, 0xca, 0xf4, 0xfe, 0x0c, 0x86, 0x82, 0xb2, 0xbd, 0xe4, 0xfd, 0xaf, 0xe1, + 0xde, 0x07, 0xfb, 0x5c, 0xa1, 0xad, 0x2e, 0xf8, 0x1d, 0x24, 0x34, 0x9c, 0x73, 0x3f, 0x43, 0xcc, + 0x5f, 0x22, 0xc8, 0x54, 0xec, 0xb2, 0x57, 0xd7, 0xf2, 0x18, 0xb1, 0x2b, 0x04, 0xd9, 0x59, 0xf9, + 0xee, 0xbe, 0x6d, 0x0c, 0x07, 0xab, 0xad, 0x6d, 0xae, 0xb7, 0xb6, 0xf9, 0xb6, 0xb5, 0xcd, 0xdb, + 0x9d, 0x6d, 0xac, 0x77, 0xb6, 0xf1, 0xb2, 0xb3, 0x8d, 0x69, 0xe7, 0x5b, 0x53, 0x74, 0x9e, 0x9d, + 0xaa, 0x4f, 0xe4, 0xde, 0xa8, 0x8d, 0xd3, 0x4d, 0x05, 0x15, 0xb5, 0x39, 0x27, 0xef, 0x01, 0x00, + 0x00, 0xff, 0xff, 0x71, 0x98, 0x4c, 0x41, 0x93, 0x02, 0x00, 0x00, +} + +func (m *Minter) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *Minter) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *Minter) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + { + size := m.TargetSupply.Size() + i -= size + if _, err := m.TargetSupply.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintMint(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x1a + { + size := m.AnnualProvisions.Size() + i -= size + if _, err := m.AnnualProvisions.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintMint(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + { + size := m.Inflation.Size() + i -= size + if _, err := m.Inflation.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintMint(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + return len(dAtA) - i, nil +} + +func (m *Params) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *Params) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if m.BlocksPerYear != 0 { + i = encodeVarintMint(dAtA, i, uint64(m.BlocksPerYear)) + i-- + dAtA[i] = 0x18 + } + { + size := m.AnnualReductionFactor.Size() + i -= size + if _, err := m.AnnualReductionFactor.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) + i = encodeVarintMint(dAtA, i, uint64(len(m.MintDenom))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func encodeVarintMint(dAtA []byte, offset int, v uint64) int { + offset -= sovMint(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *Minter) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = m.Inflation.Size() + n += 1 + l + sovMint(uint64(l)) + l = m.AnnualProvisions.Size() + n += 1 + l + sovMint(uint64(l)) + l = m.TargetSupply.Size() + n += 1 + l + sovMint(uint64(l)) + return n +} + +func (m *Params) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.MintDenom) + if l > 0 { + n += 1 + l + sovMint(uint64(l)) + } + l = m.AnnualReductionFactor.Size() + n += 1 + l + sovMint(uint64(l)) + if m.BlocksPerYear != 0 { + n += 1 + sovMint(uint64(m.BlocksPerYear)) + } + return n +} + +func sovMint(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozMint(x uint64) (n int) { + return sovMint(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *Minter) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMint + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: Minter: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: Minter: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Inflation", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMint + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + 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.Inflation.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field AnnualProvisions", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMint + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + 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.AnnualProvisions.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field TargetSupply", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMint + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + 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.TargetSupply.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipMint(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthMint + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *Params) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMint + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: Params: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: Params: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field MintDenom", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMint + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + 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 + } + m.MintDenom = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field AnnualReductionFactor", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMint + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + 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.AnnualReductionFactor.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) + } + m.BlocksPerYear = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowMint + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.BlocksPerYear |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + default: + iNdEx = preIndex + skippy, err := skipMint(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthMint + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipMint(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowMint + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowMint + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowMint + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthMint + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupMint + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthMint + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthMint = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowMint = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupMint = fmt.Errorf("proto: unexpected end of group") +) diff --git a/x/mint/migrations/v3/types/params.go b/x/mint/migrations/v3/types/params.go new file mode 100644 index 00000000..85b9a5ee --- /dev/null +++ b/x/mint/migrations/v3/types/params.go @@ -0,0 +1,97 @@ +package types + +import ( + "errors" + "fmt" + "strings" + + "sigs.k8s.io/yaml" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// Parameter store keys. +var ( + KeyMintDenom = []byte("MintDenom") + KeyAnnualReductionFactor = []byte("AnnualReductionFactor") + KeyBlocksPerYear = []byte("BlocksPerYear") +) + +func NewParams( + mintDenom string, annualReductionFactor sdk.Dec, blocksPerYear uint64, +) Params { + return Params{ + MintDenom: mintDenom, + AnnualReductionFactor: annualReductionFactor, + BlocksPerYear: blocksPerYear, + } +} + +// default minting module parameters. +func DefaultParams() Params { + return Params{ + MintDenom: sdk.DefaultBondDenom, + AnnualReductionFactor: sdk.NewDecWithPrec(20, 2), // Tha annual reduction factor is configured to 20% per year + BlocksPerYear: uint64(60 * 60 * 8766 / 5), // assuming 5-second block times + } +} + +// validate params. +func (p Params) Validate() error { + if err := validateMintDenom(p.MintDenom); err != nil { + return err + } + if err := validateAnnualReductionFactor(p.AnnualReductionFactor); err != nil { + return err + } + + return validateBlocksPerYear(p.BlocksPerYear) +} + +// String implements the Stringer interface. +func (p Params) String() string { + out, _ := yaml.Marshal(p) + return string(out) +} + +func validateMintDenom(i interface{}) error { + v, ok := i.(string) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + if strings.TrimSpace(v) == "" { + return errors.New("mint denom cannot be blank") + } + + return sdk.ValidateDenom(v) +} + +func validateAnnualReductionFactor(i interface{}) error { + v, ok := i.(sdk.Dec) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + if v.IsNegative() { + return fmt.Errorf("annual reduction factor cannot be negative: %s", v) + } + if v.GT(sdk.OneDec()) { + return fmt.Errorf("annual reduction factor too large: %s", v) + } + + return nil +} + +func validateBlocksPerYear(i interface{}) error { + v, ok := i.(uint64) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + if v == 0 { + return fmt.Errorf("blocks per year must be positive: %d", v) + } + + return nil +} From e62215aa4bdec783d047c0f17373b4e707d6bbd3 Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Thu, 24 Aug 2023 14:53:37 +0200 Subject: [PATCH 09/24] feat(mint): migrate minter params by removing not used key --- x/mint/migrations/v3/migrations.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/x/mint/migrations/v3/migrations.go b/x/mint/migrations/v3/migrations.go index 27fd69c6..a968aaea 100644 --- a/x/mint/migrations/v3/migrations.go +++ b/x/mint/migrations/v3/migrations.go @@ -20,7 +20,7 @@ func MigrateStore(ctx sdk.Context, logger.Debug("starting module migration") - logger.Debug("migrate mint params") + logger.Debug("migrate old mint params with new params") var oldParams oldTypes.Params d := store.Get(types.ParamsKey) @@ -38,6 +38,22 @@ func MigrateStore(ctx sdk.Context, } store.Set(types.ParamsKey, bz) + logger.Debug("migrate minter store") + + var oldMinter oldTypes.Minter + d = store.Get(types.MinterKey) + err = cdc.Unmarshal(d, &oldMinter) + if err != nil { + return err + } + + newMinter := types.NewMinter(oldMinter.Inflation, oldMinter.AnnualProvisions) + bz, err = cdc.Marshal(&newMinter) + if err != nil { + return err + } + store.Set(types.MinterKey, bz) + logger.Debug("module migration done") return nil From 8acba8c3365653c8fb656a3afbc88f825a76e7e3 Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Thu, 24 Aug 2023 15:17:33 +0200 Subject: [PATCH 10/24] test(mint): add test for next inflation --- x/mint/migrations/v3/migrations.go | 1 + x/mint/types/minter_test.go | 104 ++++++++++++++--------------- 2 files changed, 52 insertions(+), 53 deletions(-) diff --git a/x/mint/migrations/v3/migrations.go b/x/mint/migrations/v3/migrations.go index a968aaea..1ba114eb 100644 --- a/x/mint/migrations/v3/migrations.go +++ b/x/mint/migrations/v3/migrations.go @@ -3,6 +3,7 @@ package v3 import ( "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" + oldTypes "github.com/okp4/okp4d/x/mint/migrations/v3/types" "github.com/okp4/okp4d/x/mint/types" ) diff --git a/x/mint/types/minter_test.go b/x/mint/types/minter_test.go index e93b6844..4db361ae 100644 --- a/x/mint/types/minter_test.go +++ b/x/mint/types/minter_test.go @@ -7,8 +7,6 @@ import ( "github.com/stretchr/testify/require" - "cosmossdk.io/math" - sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -17,17 +15,17 @@ func TestNextInflation(t *testing.T) { params := DefaultParams() tests := []struct { - setInflation, expInflation sdk.Dec + boundedRatio, expInflation sdk.Dec }{ - // With annual reduction factor of 20 % (defined in params), next infaltion should be 12% - {sdk.NewDecWithPrec(15, 2), sdk.NewDecWithPrec(12, 2)}, - // With annual reduction factor of 20 % (defined in params), next infaltion should be 9.6% - {sdk.NewDecWithPrec(12, 2), sdk.NewDecWithPrec(96, 3)}, + // With a bounded ratio of 66 %, next inflation should be 10.95% + {sdk.NewDecWithPrec(66, 2), sdk.NewDecWithPrec(1095, 2)}, + // With a bounded ratio of 0 %, next inflation should be 18.25% + {sdk.NewDecWithPrec(0, 2), sdk.NewDecWithPrec(1825, 2)}, + // With a bounded ratio of 100 %, next inflation should be 18.25% + {sdk.NewDecWithPrec(1, 0), sdk.NewDecWithPrec(7189393939393939400, 18)}, } for i, tc := range tests { - minter.Inflation = tc.setInflation - - inflation := minter.NextInflation(params) + inflation := minter.NextInflation(params, tc.boundedRatio) require.True(t, inflation.Equal(tc.expInflation), "Test Index: %v\nInflation: %v\nExpected: %v\n", i, inflation, tc.expInflation) @@ -35,44 +33,44 @@ func TestNextInflation(t *testing.T) { } //nolint:lll -func TestBlockProvision(t *testing.T) { - minter := InitialMinter(sdk.NewDecWithPrec(1, 1), math.NewInt(1)) - params := DefaultParams() - - secondsPerYear := int64(60 * 60 * 8766) - blockInterval := int64(5) // there is 1 block each 5 second approximately - - tests := []struct { - annualProvisions sdk.Dec - expProvisions int64 - targetSupply math.Int - totalSupply math.Int - }{ - {sdk.NewDec(secondsPerYear / blockInterval), 1, sdk.NewInt(secondsPerYear / blockInterval), sdk.NewInt(1)}, - {sdk.NewDec(secondsPerYear/blockInterval + 1), 1, sdk.NewInt(secondsPerYear/blockInterval + 1), math.NewInt(1)}, - {sdk.NewDec((secondsPerYear / blockInterval) * 2), 2, sdk.NewInt((secondsPerYear / 5) * 2), math.NewInt(1)}, - {sdk.NewDec((secondsPerYear / blockInterval) / 2), 0, sdk.NewInt(1), math.NewInt(1)}, - {sdk.NewDec((secondsPerYear / blockInterval) * 20), 20, sdk.NewInt((secondsPerYear / blockInterval) * 20 * (secondsPerYear / blockInterval)), math.NewInt(1)}, - // Only two token should be minted to reach the target supply - {sdk.NewDec((secondsPerYear / blockInterval) * 20), 2, sdk.NewInt((secondsPerYear / blockInterval) * 20 * (secondsPerYear / blockInterval)), sdk.NewInt(((secondsPerYear / blockInterval) * 20 * (secondsPerYear / blockInterval)) - 2)}, - // Zero token should be minted since the target supply is already reached, the new inflation should be calculated - {sdk.NewDec((secondsPerYear / blockInterval) * 20), 0, sdk.NewInt((secondsPerYear / blockInterval) * 20 * (secondsPerYear / blockInterval)), sdk.NewInt((secondsPerYear / blockInterval) * 20 * (secondsPerYear / blockInterval))}, - // Zero token should be minted since target supply are exceeded (avoid negative coin) - {sdk.NewDec((secondsPerYear / blockInterval) * 20), 0, sdk.NewInt((secondsPerYear / blockInterval) * 20 * (secondsPerYear / blockInterval)), sdk.NewInt(((secondsPerYear / blockInterval) * 20 * (secondsPerYear / blockInterval)) + 2)}, - } - for i, tc := range tests { - minter.AnnualProvisions = tc.annualProvisions - minter.TargetSupply = tc.targetSupply - provisions := minter.BlockProvision(params, tc.totalSupply) - - expProvisions := sdk.NewCoin(params.MintDenom, - sdk.NewInt(tc.expProvisions)) - - require.True(t, expProvisions.IsEqual(provisions), - "test: %v\n\tExp: %v\n\tGot: %v\n", - i, tc.expProvisions, provisions) - } -} +//func TestBlockProvision(t *testing.T) { +// minter := InitialMinter(sdk.NewDecWithPrec(1, 1), math.NewInt(1)) +// params := DefaultParams() +// +// secondsPerYear := int64(60 * 60 * 8766) +// blockInterval := int64(5) // there is 1 block each 5 second approximately +// +// tests := []struct { +// annualProvisions sdk.Dec +// expProvisions int64 +// targetSupply math.Int +// totalSupply math.Int +// }{ +// {sdk.NewDec(secondsPerYear / blockInterval), 1, sdk.NewInt(secondsPerYear / blockInterval), sdk.NewInt(1)}, +// {sdk.NewDec(secondsPerYear/blockInterval + 1), 1, sdk.NewInt(secondsPerYear/blockInterval + 1), math.NewInt(1)}, +// {sdk.NewDec((secondsPerYear / blockInterval) * 2), 2, sdk.NewInt((secondsPerYear / 5) * 2), math.NewInt(1)}, +// {sdk.NewDec((secondsPerYear / blockInterval) / 2), 0, sdk.NewInt(1), math.NewInt(1)}, +// {sdk.NewDec((secondsPerYear / blockInterval) * 20), 20, sdk.NewInt((secondsPerYear / blockInterval) * 20 * (secondsPerYear / blockInterval)), math.NewInt(1)}, +// // Only two token should be minted to reach the target supply +// {sdk.NewDec((secondsPerYear / blockInterval) * 20), 2, sdk.NewInt((secondsPerYear / blockInterval) * 20 * (secondsPerYear / blockInterval)), sdk.NewInt(((secondsPerYear / blockInterval) * 20 * (secondsPerYear / blockInterval)) - 2)}, +// // Zero token should be minted since the target supply is already reached, the new inflation should be calculated +// {sdk.NewDec((secondsPerYear / blockInterval) * 20), 0, sdk.NewInt((secondsPerYear / blockInterval) * 20 * (secondsPerYear / blockInterval)), sdk.NewInt((secondsPerYear / blockInterval) * 20 * (secondsPerYear / blockInterval))}, +// // Zero token should be minted since target supply are exceeded (avoid negative coin) +// {sdk.NewDec((secondsPerYear / blockInterval) * 20), 0, sdk.NewInt((secondsPerYear / blockInterval) * 20 * (secondsPerYear / blockInterval)), sdk.NewInt(((secondsPerYear / blockInterval) * 20 * (secondsPerYear / blockInterval)) + 2)}, +// } +// for i, tc := range tests { +// minter.AnnualProvisions = tc.annualProvisions +// minter.TargetSupply = tc.targetSupply +// provisions := minter.BlockProvision(params, tc.totalSupply) +// +// expProvisions := sdk.NewCoin(params.MintDenom, +// sdk.NewInt(tc.expProvisions)) +// +// require.True(t, expProvisions.IsEqual(provisions), +// "test: %v\n\tExp: %v\n\tGot: %v\n", +// i, tc.expProvisions, provisions) +// } +//} // Benchmarking :) // previously using math.Int operations: @@ -82,7 +80,7 @@ func TestBlockProvision(t *testing.T) { // BenchmarkBlockProvision-4 3000000 429 ns/op. func BenchmarkBlockProvision(b *testing.B) { b.ReportAllocs() - minter := InitialMinter(sdk.NewDecWithPrec(1, 1), math.NewInt(1)) + minter := InitialMinter(sdk.NewDecWithPrec(1, 1)) params := DefaultParams() s1 := rand.NewSource(100) @@ -91,7 +89,7 @@ func BenchmarkBlockProvision(b *testing.B) { // run the BlockProvision function b.N times for n := 0; n < b.N; n++ { - minter.BlockProvision(params, math.NewInt(1)) + minter.BlockProvision(params) } } @@ -99,12 +97,12 @@ func BenchmarkBlockProvision(b *testing.B) { // BenchmarkNextInflation-4 1000000 1828 ns/op. func BenchmarkNextInflation(b *testing.B) { b.ReportAllocs() - minter := InitialMinter(sdk.NewDecWithPrec(1, 1), math.NewInt(1)) + minter := InitialMinter(sdk.NewDecWithPrec(1, 1)) params := DefaultParams() // run the NextInflationRate function b.N times for n := 0; n < b.N; n++ { - minter.NextInflation(params) + minter.NextInflation(params, sdk.NewDecWithPrec(66, 2)) } } @@ -112,7 +110,7 @@ func BenchmarkNextInflation(b *testing.B) { // // BenchmarkNextAnnualProvisions-4 5000000 251 ns/op. func BenchmarkNextAnnualProvisions(b *testing.B) { b.ReportAllocs() - minter := InitialMinter(sdk.NewDecWithPrec(1, 1), math.NewInt(1)) + minter := InitialMinter(sdk.NewDecWithPrec(1, 1)) params := DefaultParams() totalSupply := sdk.NewInt(100000000000000) From c555990962ce4dcc8fdb0a3f54c59c678604df76 Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Fri, 25 Aug 2023 15:08:34 +0200 Subject: [PATCH 11/24] fix(mint): calculate inflation in percent instead of permille --- x/mint/types/minter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/mint/types/minter.go b/x/mint/types/minter.go index c9db82f6..ab5214b8 100644 --- a/x/mint/types/minter.go +++ b/x/mint/types/minter.go @@ -46,7 +46,7 @@ func ValidateMinter(minter Minter) error { // Get the current inflation and multiply by (1 - annual reduction factor). func (m Minter) NextInflation(params Params, boundedRatio sdk.Dec) sdk.Dec { bounded := params.BoundingAdjustment.Sub(boundedRatio.Quo(params.TargetBoundingRatio)) - return params.InflationCoef.Mul(bounded).Mul(sdk.NewDec(100)) + return params.InflationCoef.Mul(bounded) } // NextAnnualProvisions returns the annual provisions based on current total From 5db18b98815ac2e7ad0bdd33f6354bcd689b6615 Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Fri, 25 Aug 2023 15:19:01 +0200 Subject: [PATCH 12/24] test(mint): fix mint test --- x/mint/client/cli/query_test.go | 10 ++-- x/mint/keeper/genesis_test.go | 6 +-- x/mint/keeper/keeper_test.go | 38 ++++++++++++--- x/mint/keeper/msg_server_test.go | 44 ++++++++++++++--- x/mint/simulation/decoder_test.go | 2 +- x/mint/simulation/genesis_test.go | 12 +++-- x/mint/simulation/proposals_test.go | 6 ++- x/mint/types/minter_test.go | 75 +++++++++++++---------------- 8 files changed, 125 insertions(+), 68 deletions(-) diff --git a/x/mint/client/cli/query_test.go b/x/mint/client/cli/query_test.go index 939c8031..c28fc5e0 100644 --- a/x/mint/client/cli/query_test.go +++ b/x/mint/client/cli/query_test.go @@ -46,15 +46,17 @@ func TestGetCmdQueryParams(t *testing.T) { "json output", []string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", flags.FlagOutput)}, `[--height=1 --output=json]`, - `{"mint_denom":"","annual_reduction_factor":"0","blocks_per_year":"0"}`, + `{"mint_denom":"","inflation_coef":"0","bounding_adjustment":"0","target_bounding_ratio":"0","blocks_per_year":"0"}`, }, { "text output", []string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", flags.FlagOutput)}, `[--height=1 --output=text]`, - `annual_reduction_factor: "0" -blocks_per_year: "0" -mint_denom: ""`, + `blocks_per_year: "0" +bounding_adjustment: "0" +inflation_coef: "0" +mint_denom: "" +target_bounding_ratio: "0"`, }, } diff --git a/x/mint/keeper/genesis_test.go b/x/mint/keeper/genesis_test.go index 08a127fa..cde34e40 100644 --- a/x/mint/keeper/genesis_test.go +++ b/x/mint/keeper/genesis_test.go @@ -6,8 +6,6 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/suite" - "cosmossdk.io/math" - "github.com/cosmos/cosmos-sdk/codec" storetypes "github.com/cosmos/cosmos-sdk/store/types" "github.com/cosmos/cosmos-sdk/testutil" @@ -60,10 +58,12 @@ func (s *GenesisTestSuite) SetupTest() { func (s *GenesisTestSuite) TestImportExportGenesis() { genesisState := types.DefaultGenesisState() - genesisState.Minter = types.NewMinter(sdk.OneDec(), sdk.NewDecWithPrec(20, 2), math.NewInt(1)) + genesisState.Minter = types.NewMinter(sdk.OneDec(), sdk.NewDecWithPrec(20, 2)) genesisState.Params = types.NewParams( "testDenom", sdk.NewDecWithPrec(69, 2), + sdk.NewDecWithPrec(14, 2), + sdk.NewDecWithPrec(56, 2), uint64(60*60*8766/5), ) diff --git a/x/mint/keeper/keeper_test.go b/x/mint/keeper/keeper_test.go index 39a014d7..a8fda3d6 100644 --- a/x/mint/keeper/keeper_test.go +++ b/x/mint/keeper/keeper_test.go @@ -77,18 +77,44 @@ func (s *IntegrationTestSuite) TestParams() { { name: "set invalid params", input: types.Params{ - MintDenom: sdk.DefaultBondDenom, - AnnualReductionFactor: sdk.NewDecWithPrec(-13, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), + MintDenom: sdk.DefaultBondDenom, + BoundingAdjustment: sdk.NewDecWithPrec(-13, 2), + TargetBoundingRatio: sdk.NewDecWithPrec(25, 2), + InflationCoef: sdk.NewDecWithPrec(73, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), + }, + expectErr: true, + }, + { + name: "set invalid params", + input: types.Params{ + MintDenom: sdk.DefaultBondDenom, + BoundingAdjustment: sdk.NewDecWithPrec(13, 2), + TargetBoundingRatio: sdk.NewDecWithPrec(-25, 2), + InflationCoef: sdk.NewDecWithPrec(73, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), + }, + expectErr: true, + }, + { + name: "set invalid params", + input: types.Params{ + MintDenom: sdk.DefaultBondDenom, + BoundingAdjustment: sdk.NewDecWithPrec(13, 2), + TargetBoundingRatio: sdk.NewDecWithPrec(25, 2), + InflationCoef: sdk.NewDecWithPrec(-73, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), }, expectErr: true, }, { name: "set full valid params", input: types.Params{ - MintDenom: sdk.DefaultBondDenom, - AnnualReductionFactor: sdk.NewDecWithPrec(8, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), + MintDenom: sdk.DefaultBondDenom, + BoundingAdjustment: sdk.NewDecWithPrec(13, 2), + TargetBoundingRatio: sdk.NewDecWithPrec(25, 2), + InflationCoef: sdk.NewDecWithPrec(73, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), }, expectErr: false, }, diff --git a/x/mint/keeper/msg_server_test.go b/x/mint/keeper/msg_server_test.go index 03f52141..d9df15bc 100644 --- a/x/mint/keeper/msg_server_test.go +++ b/x/mint/keeper/msg_server_test.go @@ -24,9 +24,39 @@ func (s *IntegrationTestSuite) TestUpdateParams() { request: &types.MsgUpdateParams{ Authority: s.mintKeeper.GetAuthority(), Params: types.Params{ - MintDenom: sdk.DefaultBondDenom, - AnnualReductionFactor: sdk.NewDecWithPrec(-13, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), + MintDenom: sdk.DefaultBondDenom, + BoundingAdjustment: sdk.NewDecWithPrec(-13, 2), + TargetBoundingRatio: sdk.NewDecWithPrec(25, 2), + InflationCoef: sdk.NewDecWithPrec(73, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), + }, + }, + expectErr: true, + }, + { + name: "set invalid params", + request: &types.MsgUpdateParams{ + Authority: s.mintKeeper.GetAuthority(), + Params: types.Params{ + MintDenom: sdk.DefaultBondDenom, + BoundingAdjustment: sdk.NewDecWithPrec(13, 2), + TargetBoundingRatio: sdk.NewDecWithPrec(-25, 2), + InflationCoef: sdk.NewDecWithPrec(73, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), + }, + }, + expectErr: true, + }, + { + name: "set invalid params", + request: &types.MsgUpdateParams{ + Authority: s.mintKeeper.GetAuthority(), + Params: types.Params{ + MintDenom: sdk.DefaultBondDenom, + BoundingAdjustment: sdk.NewDecWithPrec(13, 2), + TargetBoundingRatio: sdk.NewDecWithPrec(25, 2), + InflationCoef: sdk.NewDecWithPrec(-73, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), }, }, expectErr: true, @@ -36,9 +66,11 @@ func (s *IntegrationTestSuite) TestUpdateParams() { request: &types.MsgUpdateParams{ Authority: s.mintKeeper.GetAuthority(), Params: types.Params{ - MintDenom: sdk.DefaultBondDenom, - AnnualReductionFactor: sdk.NewDecWithPrec(8, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), + MintDenom: sdk.DefaultBondDenom, + BoundingAdjustment: sdk.NewDecWithPrec(13, 2), + TargetBoundingRatio: sdk.NewDecWithPrec(25, 2), + InflationCoef: sdk.NewDecWithPrec(73, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), }, }, expectErr: false, diff --git a/x/mint/simulation/decoder_test.go b/x/mint/simulation/decoder_test.go index 6734c502..407c8526 100644 --- a/x/mint/simulation/decoder_test.go +++ b/x/mint/simulation/decoder_test.go @@ -20,7 +20,7 @@ func TestDecodeStore(t *testing.T) { dec := simulation.NewDecodeStore(encCfg.Codec) - minter := types.NewMinter(sdk.OneDec(), sdk.NewDec(15), sdk.NewInt(1)) + minter := types.NewMinter(sdk.OneDec(), sdk.NewDec(15)) kvPairs := kv.Pairs{ Pairs: []kv.Pair{ diff --git a/x/mint/simulation/genesis_test.go b/x/mint/simulation/genesis_test.go index 04a990f5..6dc64b8f 100644 --- a/x/mint/simulation/genesis_test.go +++ b/x/mint/simulation/genesis_test.go @@ -44,16 +44,20 @@ func TestRandomizedGenState(t *testing.T) { var mintGenesis types.GenesisState simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &mintGenesis) - dec1, _ := sdk.NewDecFromStr("0.200000000000000000") + dec1, _ := sdk.NewDecFromStr("0.073000000000000000") + dec2, _ := sdk.NewDecFromStr("2.500000000000000000") + dec3, _ := sdk.NewDecFromStr("0.660000000000000000") require.Equal(t, uint64(6311520), mintGenesis.Params.BlocksPerYear) - require.Equal(t, dec1, mintGenesis.Params.AnnualReductionFactor) + require.Equal(t, dec1, mintGenesis.Params.InflationCoef) + require.Equal(t, dec2, mintGenesis.Params.BoundingAdjustment) + require.Equal(t, dec3, mintGenesis.Params.TargetBoundingRatio) require.Equal(t, "stake", mintGenesis.Params.MintDenom) require.Equal(t, "0stake", - mintGenesis.Minter.BlockProvision(mintGenesis.Params, simState.InitialStake).String()) + mintGenesis.Minter.BlockProvision(mintGenesis.Params).String()) require.Equal(t, "170.000000000000000000", mintGenesis.Minter.NextAnnualProvisions(mintGenesis.Params, simState.InitialStake).String()) - require.Equal(t, "0.136000000000000000", mintGenesis.Minter.NextInflation(mintGenesis.Params).String()) + require.Equal(t, "0.109500000000000000", mintGenesis.Minter.NextInflation(mintGenesis.Params, sdk.NewDecWithPrec(66, 2)).String()) require.Equal(t, "0.170000000000000000", mintGenesis.Minter.Inflation.String()) require.Equal(t, "170.000000000000000000", mintGenesis.Minter.AnnualProvisions.String()) } diff --git a/x/mint/simulation/proposals_test.go b/x/mint/simulation/proposals_test.go index e48ab307..6d36853e 100644 --- a/x/mint/simulation/proposals_test.go +++ b/x/mint/simulation/proposals_test.go @@ -40,6 +40,8 @@ func TestProposalMsgs(t *testing.T) { assert.Equal(t, sdk.AccAddress(address.Module("gov")).String(), msgUpdateParams.Authority) assert.Equal(t, uint64(122877), msgUpdateParams.Params.BlocksPerYear) - assert.DeepEqual(t, sdk.NewDecWithPrec(95, 2), msgUpdateParams.Params.AnnualReductionFactor) - assert.Equal(t, "eAerqyNEUz", msgUpdateParams.Params.MintDenom) + assert.DeepEqual(t, sdk.NewDecWithPrec(7, 2), msgUpdateParams.Params.TargetBoundingRatio) + assert.DeepEqual(t, sdk.NewDecWithPrec(95, 2), msgUpdateParams.Params.InflationCoef) + assert.DeepEqual(t, sdk.NewDecWithPrec(6, 1), msgUpdateParams.Params.BoundingAdjustment) + assert.Equal(t, "xKGLwQvuyN", msgUpdateParams.Params.MintDenom) } diff --git a/x/mint/types/minter_test.go b/x/mint/types/minter_test.go index 4db361ae..feef9df8 100644 --- a/x/mint/types/minter_test.go +++ b/x/mint/types/minter_test.go @@ -18,11 +18,11 @@ func TestNextInflation(t *testing.T) { boundedRatio, expInflation sdk.Dec }{ // With a bounded ratio of 66 %, next inflation should be 10.95% - {sdk.NewDecWithPrec(66, 2), sdk.NewDecWithPrec(1095, 2)}, + {sdk.NewDecWithPrec(66, 2), sdk.NewDecWithPrec(1095, 4)}, // With a bounded ratio of 0 %, next inflation should be 18.25% - {sdk.NewDecWithPrec(0, 2), sdk.NewDecWithPrec(1825, 2)}, - // With a bounded ratio of 100 %, next inflation should be 18.25% - {sdk.NewDecWithPrec(1, 0), sdk.NewDecWithPrec(7189393939393939400, 18)}, + {sdk.NewDecWithPrec(0, 2), sdk.NewDecWithPrec(1825, 4)}, + // With a bounded ratio of 100 %, next inflation should be 7.18% + {sdk.NewDecWithPrec(1, 0), sdk.NewDecWithPrec(71893939393939394, 18)}, } for i, tc := range tests { inflation := minter.NextInflation(params, tc.boundedRatio) @@ -33,44 +33,35 @@ func TestNextInflation(t *testing.T) { } //nolint:lll -//func TestBlockProvision(t *testing.T) { -// minter := InitialMinter(sdk.NewDecWithPrec(1, 1), math.NewInt(1)) -// params := DefaultParams() -// -// secondsPerYear := int64(60 * 60 * 8766) -// blockInterval := int64(5) // there is 1 block each 5 second approximately -// -// tests := []struct { -// annualProvisions sdk.Dec -// expProvisions int64 -// targetSupply math.Int -// totalSupply math.Int -// }{ -// {sdk.NewDec(secondsPerYear / blockInterval), 1, sdk.NewInt(secondsPerYear / blockInterval), sdk.NewInt(1)}, -// {sdk.NewDec(secondsPerYear/blockInterval + 1), 1, sdk.NewInt(secondsPerYear/blockInterval + 1), math.NewInt(1)}, -// {sdk.NewDec((secondsPerYear / blockInterval) * 2), 2, sdk.NewInt((secondsPerYear / 5) * 2), math.NewInt(1)}, -// {sdk.NewDec((secondsPerYear / blockInterval) / 2), 0, sdk.NewInt(1), math.NewInt(1)}, -// {sdk.NewDec((secondsPerYear / blockInterval) * 20), 20, sdk.NewInt((secondsPerYear / blockInterval) * 20 * (secondsPerYear / blockInterval)), math.NewInt(1)}, -// // Only two token should be minted to reach the target supply -// {sdk.NewDec((secondsPerYear / blockInterval) * 20), 2, sdk.NewInt((secondsPerYear / blockInterval) * 20 * (secondsPerYear / blockInterval)), sdk.NewInt(((secondsPerYear / blockInterval) * 20 * (secondsPerYear / blockInterval)) - 2)}, -// // Zero token should be minted since the target supply is already reached, the new inflation should be calculated -// {sdk.NewDec((secondsPerYear / blockInterval) * 20), 0, sdk.NewInt((secondsPerYear / blockInterval) * 20 * (secondsPerYear / blockInterval)), sdk.NewInt((secondsPerYear / blockInterval) * 20 * (secondsPerYear / blockInterval))}, -// // Zero token should be minted since target supply are exceeded (avoid negative coin) -// {sdk.NewDec((secondsPerYear / blockInterval) * 20), 0, sdk.NewInt((secondsPerYear / blockInterval) * 20 * (secondsPerYear / blockInterval)), sdk.NewInt(((secondsPerYear / blockInterval) * 20 * (secondsPerYear / blockInterval)) + 2)}, -// } -// for i, tc := range tests { -// minter.AnnualProvisions = tc.annualProvisions -// minter.TargetSupply = tc.targetSupply -// provisions := minter.BlockProvision(params, tc.totalSupply) -// -// expProvisions := sdk.NewCoin(params.MintDenom, -// sdk.NewInt(tc.expProvisions)) -// -// require.True(t, expProvisions.IsEqual(provisions), -// "test: %v\n\tExp: %v\n\tGot: %v\n", -// i, tc.expProvisions, provisions) -// } -//} +func TestBlockProvision(t *testing.T) { + minter := InitialMinter(sdk.NewDecWithPrec(1, 1)) + params := DefaultParams() + + secondsPerYear := int64(60 * 60 * 8766) + blockInterval := int64(5) // there is 1 block each 5 second approximately + + tests := []struct { + annualProvisions sdk.Dec + expProvisions int64 + }{ + {sdk.NewDec(secondsPerYear / blockInterval), 1}, + {sdk.NewDec(secondsPerYear/blockInterval + 1), 1}, + {sdk.NewDec((secondsPerYear / blockInterval) * 2), 2}, + {sdk.NewDec((secondsPerYear / blockInterval) / 2), 0}, + {sdk.NewDec((secondsPerYear / blockInterval) * 20), 20}, + } + for i, tc := range tests { + minter.AnnualProvisions = tc.annualProvisions + provisions := minter.BlockProvision(params) + + expProvisions := sdk.NewCoin(params.MintDenom, + sdk.NewInt(tc.expProvisions)) + + require.True(t, expProvisions.IsEqual(provisions), + "test: %v\n\tExp: %v\n\tGot: %v\n", + i, tc.expProvisions, provisions) + } +} // Benchmarking :) // previously using math.Int operations: From 49c1202ebd634a0c3ec31d9e50d70d03f75582c7 Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Fri, 25 Aug 2023 15:31:15 +0200 Subject: [PATCH 13/24] fix(lint): fix unused linter directive --- x/mint/types/minter_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/x/mint/types/minter_test.go b/x/mint/types/minter_test.go index feef9df8..64766936 100644 --- a/x/mint/types/minter_test.go +++ b/x/mint/types/minter_test.go @@ -32,7 +32,6 @@ func TestNextInflation(t *testing.T) { } } -//nolint:lll func TestBlockProvision(t *testing.T) { minter := InitialMinter(sdk.NewDecWithPrec(1, 1)) params := DefaultParams() From 4367756ed9e8ee6003e8bb51d2f13078dc895d6c Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Fri, 25 Aug 2023 15:50:37 +0200 Subject: [PATCH 14/24] test(mint): add test for NextAnnualProvision --- x/mint/types/minter_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/x/mint/types/minter_test.go b/x/mint/types/minter_test.go index 64766936..d408131a 100644 --- a/x/mint/types/minter_test.go +++ b/x/mint/types/minter_test.go @@ -62,6 +62,28 @@ func TestBlockProvision(t *testing.T) { } } +func TestNextAnnualProvision(t *testing.T) { + minter := InitialMinter(sdk.NewDecWithPrec(1, 1)) + params := DefaultParams() + + tests := []struct { + inflation sdk.Dec + totalSupply int64 + expProvisions sdk.Dec + }{ + {sdk.NewDecWithPrec(18, 2), 1000, sdk.NewDec(180)}, + {sdk.NewDecWithPrec(71893939393939394, 18), 1000, sdk.NewDecWithPrec(71893939393939394, 15)}, + } + for i, tc := range tests { + minter.Inflation = tc.inflation + provisions := minter.NextAnnualProvisions(params, sdk.NewInt(tc.totalSupply)) + + require.True(t, tc.expProvisions.Equal(provisions), + "test: %v\n\tExp: %v\n\tGot: %v\n", + i, tc.expProvisions, provisions) + } +} + // Benchmarking :) // previously using math.Int operations: // BenchmarkBlockProvision-4 5000000 220 ns/op From 6a6471fa01f76cca01f89b8ace4ad9288ca5ade8 Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Wed, 6 Sep 2023 14:37:37 +0200 Subject: [PATCH 15/24] docs(mint): update mint docs --- docs/proto/mint.md | 31 ++++++++++++++----------------- proto/mint/v1beta1/mint.proto | 31 ++++++++++++++----------------- x/mint/types/mint.pb.go | 31 ++++++++++++++----------------- 3 files changed, 42 insertions(+), 51 deletions(-) diff --git a/docs/proto/mint.md b/docs/proto/mint.md index 14a60bf2..4f9a9ed1 100644 --- a/docs/proto/mint.md +++ b/docs/proto/mint.md @@ -59,17 +59,19 @@ then the new `annual_provisions` and `target_supply` is deducted based on the cu ### Minter -Minter represents the minting state. +Minter represents the minting state within the blockchain, tasked with the continuous calculation and distribution +of tokens to validators. -At the beginning of the chain (first block) the mint module will recalculate the `annual_provisions` and -`target_supply` based on the genesis total token supply and the inflation configured. -By default inflation is set to 15%. If the genesis total token supply is 200M token, the `annual_provision` will be 30M -and `target_supply` 230M. +This calculation occurs with each block, where the minting module dynamically recalculates the annual `inflation` rate. +Using the resulting inflation rate, it deduces the quantity of tokens to be provisioned for the upcoming year. + +Furthermore, based on the current block's position within the year, it computes the exact number of tokens to be +minted for that specific block. | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | -| `inflation` | [string](#string) | | current annual inflation rate | -| `annual_provisions` | [string](#string) | | current annual expected provisions | +| `inflation` | [string](#string) | | Represents the current annual inflation rate. | +| `annual_provisions` | [string](#string) | | Represents the current annual expected provisions. | @@ -77,18 +79,13 @@ and `target_supply` 230M. Params holds parameters for the mint module. -Configure the annual reduction factor will update at the each end of year the new token distribution rate by reducing -the actual inflation by the `annual_reduction_factor` configured. -By default, `annual_reduction_factor` is 20%. For example, with an initial inflation of 15%, at the end of the year, -new inflation will be 12%. - | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | -| `mint_denom` | [string](#string) | | type of coin to mint | -| `inflation_coef` | [string](#string) | | annual inflation coefficient | -| `bounding_adjustment` | [string](#string) | | bounding adjustment | -| `target_bounding_ratio` | [string](#string) | | target boudning ratio | -| `blocks_per_year` | [uint64](#uint64) | | expected blocks per yearmake p | +| `mint_denom` | [string](#string) | | Type of coin to mint | +| `inflation_coef` | [string](#string) | | Annual inflation coefficient | +| `bounding_adjustment` | [string](#string) | | Bounding adjustment | +| `target_bounding_ratio` | [string](#string) | | Represent the target bounding ratio to reach | +| `blocks_per_year` | [uint64](#uint64) | | Estimated blocks per year | [//]: # (end messages) diff --git a/proto/mint/v1beta1/mint.proto b/proto/mint/v1beta1/mint.proto index 3915952a..7cfe727f 100644 --- a/proto/mint/v1beta1/mint.proto +++ b/proto/mint/v1beta1/mint.proto @@ -7,20 +7,22 @@ import "gogoproto/gogo.proto"; option go_package = "github.com/okp4/okp4d/x/mint/types"; -// Minter represents the minting state. +// Minter represents the minting state within the blockchain, tasked with the continuous calculation and distribution +// of tokens to validators. // -// At the beginning of the chain (first block) the mint module will recalculate the `annual_provisions` and -// `target_supply` based on the genesis total token supply and the inflation configured. -// By default inflation is set to 15%. If the genesis total token supply is 200M token, the `annual_provision` will be 30M -// and `target_supply` 230M. +// This calculation occurs with each block, where the minting module dynamically recalculates the annual `inflation` rate. +// Using the resulting inflation rate, it deduces the quantity of tokens to be provisioned for the upcoming year. +// +// Furthermore, based on the current block's position within the year, it computes the exact number of tokens to be +// minted for that specific block. message Minter { - // current annual inflation rate + // Represents the current annual inflation rate. string inflation = 1 [ (cosmos_proto.scalar) = "cosmos.Dec", (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false ]; - // current annual expected provisions + // Represents the current annual expected provisions. string annual_provisions = 2 [ (cosmos_proto.scalar) = "cosmos.Dec", (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", @@ -29,34 +31,29 @@ message Minter { } // Params holds parameters for the mint module. -// -// Configure the annual reduction factor will update at the each end of year the new token distribution rate by reducing -// the actual inflation by the `annual_reduction_factor` configured. -// By default, `annual_reduction_factor` is 20%. For example, with an initial inflation of 15%, at the end of the year, -// new inflation will be 12%. message Params { option (gogoproto.goproto_stringer) = false; - // type of coin to mint + // Type of coin to mint string mint_denom = 1; - // annual inflation coefficient + // Annual inflation coefficient string inflation_coef = 2 [ (cosmos_proto.scalar) = "cosmos.Dec", (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false ]; - // bounding adjustment + // Bounding adjustment string bounding_adjustment = 3 [ (cosmos_proto.scalar) = "cosmos.Dec", (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false ]; - // target boudning ratio + // Represent the target bounding ratio to reach string target_bounding_ratio = 4 [ (cosmos_proto.scalar) = "cosmos.Dec", (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false ]; - // expected blocks per yearmake p + // Estimated blocks per year uint64 blocks_per_year = 5; } diff --git a/x/mint/types/mint.pb.go b/x/mint/types/mint.pb.go index b5c103f2..983f30ce 100644 --- a/x/mint/types/mint.pb.go +++ b/x/mint/types/mint.pb.go @@ -25,16 +25,18 @@ var _ = math.Inf // proto package needs to be updated. const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package -// Minter represents the minting state. +// Minter represents the minting state within the blockchain, tasked with the continuous calculation and distribution +// of tokens to validators. // -// At the beginning of the chain (first block) the mint module will recalculate the `annual_provisions` and -// `target_supply` based on the genesis total token supply and the inflation configured. -// By default inflation is set to 15%. If the genesis total token supply is 200M token, the `annual_provision` will be 30M -// and `target_supply` 230M. +// This calculation occurs with each block, where the minting module dynamically recalculates the annual `inflation` rate. +// Using the resulting inflation rate, it deduces the quantity of tokens to be provisioned for the upcoming year. +// +// Furthermore, based on the current block's position within the year, it computes the exact number of tokens to be +// minted for that specific block. type Minter struct { - // current annual inflation rate + // Represents the current annual inflation rate. Inflation github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,1,opt,name=inflation,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"inflation"` - // current annual expected provisions + // Represents the current annual expected provisions. AnnualProvisions github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,2,opt,name=annual_provisions,json=annualProvisions,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"annual_provisions"` } @@ -72,21 +74,16 @@ func (m *Minter) XXX_DiscardUnknown() { var xxx_messageInfo_Minter proto.InternalMessageInfo // Params holds parameters for the mint module. -// -// Configure the annual reduction factor will update at the each end of year the new token distribution rate by reducing -// the actual inflation by the `annual_reduction_factor` configured. -// By default, `annual_reduction_factor` is 20%. For example, with an initial inflation of 15%, at the end of the year, -// new inflation will be 12%. type Params struct { - // type of coin to mint + // Type of coin to mint MintDenom string `protobuf:"bytes,1,opt,name=mint_denom,json=mintDenom,proto3" json:"mint_denom,omitempty"` - // annual inflation coefficient + // Annual inflation coefficient InflationCoef github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,2,opt,name=inflation_coef,json=inflationCoef,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"inflation_coef"` - // bounding adjustment + // Bounding adjustment BoundingAdjustment github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,3,opt,name=bounding_adjustment,json=boundingAdjustment,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"bounding_adjustment"` - // target boudning ratio + // Represent the target bounding ratio to reach TargetBoundingRatio github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,4,opt,name=target_bounding_ratio,json=targetBoundingRatio,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"target_bounding_ratio"` - // expected blocks per yearmake p + // Estimated blocks per year BlocksPerYear uint64 `protobuf:"varint,5,opt,name=blocks_per_year,json=blocksPerYear,proto3" json:"blocks_per_year,omitempty"` } From 569cd775bd5b0adc00d350baa56221d32f88b201 Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Wed, 6 Sep 2023 15:47:02 +0200 Subject: [PATCH 16/24] docs(mint): add summury description --- docs/proto/mint.md | 43 +++++++++++++++++++++++++++++++------------ proto/mint/docs.yaml | 43 +++++++++++++++++++++++++++++++------------ 2 files changed, 62 insertions(+), 24 deletions(-) diff --git a/docs/proto/mint.md b/docs/proto/mint.md index 4f9a9ed1..43b85627 100644 --- a/docs/proto/mint.md +++ b/docs/proto/mint.md @@ -6,22 +6,41 @@ ## Purpose -This module is designed to calculate an inflation rewards each years based on static params. Each block rewards is -the same over the year. +The Minting Module is a fundamental component within the blockchain ecosystem, responsible for managing the dynamic +issuance of tokens to validators, thereby ensuring the stability and sustainability of the network. +This module operates on a continuous basis, recalculating parameters with each new block to maintain an optimal +token supply. -### 🧮 Calculation +## 🧮 Calculation -The initial inflation is set to 15%, `annual_provisions` and `target_supply` will set at the beginning of the chain (on the first block). -It's based on the initial total supply. For example, with a total supply of 200M token, annual_provisions will be configured to 30M and target_supply 230M. +The formula for calculating the yearly staking reward target (srty) is as follows: -At the end of the year (the last block of the year, to be more precise), due to rounding imprecision, if the distributed -tokens for the last block of the year added with the actual tokens total supply is bigger than the `target_supply`, -only the difference to reach the `target_supply` will be minted. Conversely, in some case, due to rounding also, -the `target_supply` is not reached at the last block, but at the next block. +``` +srty = a • i • c - b / bt +``` -The new inflation, `annual_provisions` and `target_supply` is recalculated at the next block after the block that -reach the `target_supply`. To calculate the new inflation, get the current inflation multiplied by (`1 - annual_reduction_factor`), -then the new `annual_provisions` and `target_supply` is deducted based on the current total supply (that is the old `target_supply`) +- `srty`: Represents the yearly staking reward target. +- `a`: Denotes the total token supply within the blockchain. +- `i`: Refers to the inflation coefficient, a configurable parameter set at 0.073 by default. +- `c`: Signifies the bonding ratio adjustment coefficient. +- `b`: A dynamic variable ranging from 0 to 1, symbolizing the current bonding ratio at the current block. +- `bt`: Represents the target bonding ratio, conventionally set to 0.66. + +### Yearly Inflation Rate + +Upon the determination of srty, the Minting Module proceeds to convert it into a yearly inflation rate +expressed as a percentage (Iy): + +``` +Iy = (srty • 100) / a = (a • i • c - (b / bt) • 100) / a = i • c - (b / bt) • 100 +``` + +- `Iy` (in percentage): Represents the yearly inflation rate. + +### Block-Level Token Minting + +With the yearly inflation rate in hand, the Minting Module can calculate the number of tokens to be minted for +each individual block by knowing the number of block in one years. ## Table of Contents diff --git a/proto/mint/docs.yaml b/proto/mint/docs.yaml index 7f2f3707..c9043823 100644 --- a/proto/mint/docs.yaml +++ b/proto/mint/docs.yaml @@ -1,19 +1,38 @@ description: | ## Purpose - This module is designed to calculate an inflation rewards each years based on static params. Each block rewards is - the same over the year. + The Minting Module is a fundamental component within the blockchain ecosystem, responsible for managing the dynamic + issuance of tokens to validators, thereby ensuring the stability and sustainability of the network. + This module operates on a continuous basis, recalculating parameters with each new block to maintain an optimal + token supply. - ### 🧮 Calculation + ## 🧮 Calculation - The initial inflation is set to 15%, `annual_provisions` and `target_supply` will set at the beginning of the chain (on the first block). - It's based on the initial total supply. For example, with a total supply of 200M token, annual_provisions will be configured to 30M and target_supply 230M. + The formula for calculating the yearly staking reward target (srty) is as follows: - At the end of the year (the last block of the year, to be more precise), due to rounding imprecision, if the distributed - tokens for the last block of the year added with the actual tokens total supply is bigger than the `target_supply`, - only the difference to reach the `target_supply` will be minted. Conversely, in some case, due to rounding also, - the `target_supply` is not reached at the last block, but at the next block. + ``` + srty = a • i • c - b / bt + ``` - The new inflation, `annual_provisions` and `target_supply` is recalculated at the next block after the block that - reach the `target_supply`. To calculate the new inflation, get the current inflation multiplied by (`1 - annual_reduction_factor`), - then the new `annual_provisions` and `target_supply` is deducted based on the current total supply (that is the old `target_supply`) + - `srty`: Represents the yearly staking reward target. + - `a`: Denotes the total token supply within the blockchain. + - `i`: Refers to the inflation coefficient, a configurable parameter set at 0.073 by default. + - `c`: Signifies the bonding ratio adjustment coefficient. + - `b`: A dynamic variable ranging from 0 to 1, symbolizing the current bonding ratio at the current block. + - `bt`: Represents the target bonding ratio, conventionally set to 0.66. + + ### Yearly Inflation Rate + + Upon the determination of srty, the Minting Module proceeds to convert it into a yearly inflation rate + expressed as a percentage (Iy): + + ``` + Iy = (srty • 100) / a = (a • i • c - (b / bt) • 100) / a = i • c - (b / bt) • 100 + ``` + + - `Iy` (in percentage): Represents the yearly inflation rate. + + ### Block-Level Token Minting + + With the yearly inflation rate in hand, the Minting Module can calculate the number of tokens to be minted for + each individual block by knowing the number of block in one years. From a93ad5a1226dcdd69f608aec380f283fc3cb573d Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Tue, 19 Sep 2023 11:49:43 +0200 Subject: [PATCH 17/24] docs(mint): fix calculation typo in docs --- docs/proto/mint.md | 4 ++-- proto/mint/docs.yaml | 4 ++-- x/mint/types/minter.go | 3 +-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/docs/proto/mint.md b/docs/proto/mint.md index 43b85627..55745409 100644 --- a/docs/proto/mint.md +++ b/docs/proto/mint.md @@ -16,7 +16,7 @@ token supply. The formula for calculating the yearly staking reward target (srty) is as follows: ``` -srty = a • i • c - b / bt +srty = a • i • (c - b / bt) ``` - `srty`: Represents the yearly staking reward target. @@ -32,7 +32,7 @@ Upon the determination of srty, the Minting Module proceeds to convert it into a expressed as a percentage (Iy): ``` -Iy = (srty • 100) / a = (a • i • c - (b / bt) • 100) / a = i • c - (b / bt) • 100 +Iy = (srty • 100) / a = (a • i • (c - b / bt) • 100) / a = i • (c - b / bt) • 100 ``` - `Iy` (in percentage): Represents the yearly inflation rate. diff --git a/proto/mint/docs.yaml b/proto/mint/docs.yaml index c9043823..b256a1dd 100644 --- a/proto/mint/docs.yaml +++ b/proto/mint/docs.yaml @@ -11,7 +11,7 @@ description: | The formula for calculating the yearly staking reward target (srty) is as follows: ``` - srty = a • i • c - b / bt + srty = a • i • (c - b / bt) ``` - `srty`: Represents the yearly staking reward target. @@ -27,7 +27,7 @@ description: | expressed as a percentage (Iy): ``` - Iy = (srty • 100) / a = (a • i • c - (b / bt) • 100) / a = i • c - (b / bt) • 100 + Iy = (srty • 100) / a = (a • i • (c - b / bt) • 100) / a = i • (c - b / bt) • 100 ``` - `Iy` (in percentage): Represents the yearly inflation rate. diff --git a/x/mint/types/minter.go b/x/mint/types/minter.go index ab5214b8..9ca084ba 100644 --- a/x/mint/types/minter.go +++ b/x/mint/types/minter.go @@ -42,8 +42,7 @@ func ValidateMinter(minter Minter) error { return nil } -// NextInflation return the new inflation rate for the next year -// Get the current inflation and multiply by (1 - annual reduction factor). +// NextInflation return estimated yearly inflation rate for the current block. func (m Minter) NextInflation(params Params, boundedRatio sdk.Dec) sdk.Dec { bounded := params.BoundingAdjustment.Sub(boundedRatio.Quo(params.TargetBoundingRatio)) return params.InflationCoef.Mul(bounded) From 925d47a85f7e9b2eb23f472f9df3c25231908779 Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Tue, 19 Sep 2023 13:49:13 +0200 Subject: [PATCH 18/24] refactor(mint): typo, rename bound by bond --- docs/proto/mint.md | 4 +- proto/mint/v1beta1/mint.proto | 8 +-- x/mint/client/cli/query_test.go | 6 +-- x/mint/keeper/keeper_test.go | 40 +++++++-------- x/mint/keeper/msg_server_test.go | 40 +++++++-------- x/mint/simulation/genesis.go | 30 +++++------ x/mint/simulation/genesis_test.go | 4 +- x/mint/simulation/proposals.go | 4 +- x/mint/simulation/proposals_test.go | 4 +- x/mint/types/mint.pb.go | 80 ++++++++++++++--------------- x/mint/types/minter.go | 6 +-- x/mint/types/minter_test.go | 10 ++-- x/mint/types/params.go | 34 ++++++------ 13 files changed, 135 insertions(+), 135 deletions(-) diff --git a/docs/proto/mint.md b/docs/proto/mint.md index 55745409..bc9616f0 100644 --- a/docs/proto/mint.md +++ b/docs/proto/mint.md @@ -102,8 +102,8 @@ Params holds parameters for the mint module. | ----- | ---- | ----- | ----------- | | `mint_denom` | [string](#string) | | Type of coin to mint | | `inflation_coef` | [string](#string) | | Annual inflation coefficient | -| `bounding_adjustment` | [string](#string) | | Bounding adjustment | -| `target_bounding_ratio` | [string](#string) | | Represent the target bounding ratio to reach | +| `bonding_adjustment` | [string](#string) | | Bonding adjustment | +| `target_bonding_ratio` | [string](#string) | | Represent the target bonding ratio to reach | | `blocks_per_year` | [uint64](#uint64) | | Estimated blocks per year | [//]: # (end messages) diff --git a/proto/mint/v1beta1/mint.proto b/proto/mint/v1beta1/mint.proto index 7cfe727f..25575198 100644 --- a/proto/mint/v1beta1/mint.proto +++ b/proto/mint/v1beta1/mint.proto @@ -42,14 +42,14 @@ message Params { (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false ]; - // Bounding adjustment - string bounding_adjustment = 3 [ + // Bonding adjustment + string bonding_adjustment = 3 [ (cosmos_proto.scalar) = "cosmos.Dec", (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false ]; - // Represent the target bounding ratio to reach - string target_bounding_ratio = 4 [ + // Represent the target bonding ratio to reach + string target_bonding_ratio = 4 [ (cosmos_proto.scalar) = "cosmos.Dec", (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false diff --git a/x/mint/client/cli/query_test.go b/x/mint/client/cli/query_test.go index c28fc5e0..9e9a7620 100644 --- a/x/mint/client/cli/query_test.go +++ b/x/mint/client/cli/query_test.go @@ -46,17 +46,17 @@ func TestGetCmdQueryParams(t *testing.T) { "json output", []string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", flags.FlagOutput)}, `[--height=1 --output=json]`, - `{"mint_denom":"","inflation_coef":"0","bounding_adjustment":"0","target_bounding_ratio":"0","blocks_per_year":"0"}`, + `{"mint_denom":"","inflation_coef":"0","bonding_adjustment":"0","target_bonding_ratio":"0","blocks_per_year":"0"}`, }, { "text output", []string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", flags.FlagOutput)}, `[--height=1 --output=text]`, `blocks_per_year: "0" -bounding_adjustment: "0" +bonding_adjustment: "0" inflation_coef: "0" mint_denom: "" -target_bounding_ratio: "0"`, +target_bonding_ratio: "0"`, }, } diff --git a/x/mint/keeper/keeper_test.go b/x/mint/keeper/keeper_test.go index a8fda3d6..40043068 100644 --- a/x/mint/keeper/keeper_test.go +++ b/x/mint/keeper/keeper_test.go @@ -77,44 +77,44 @@ func (s *IntegrationTestSuite) TestParams() { { name: "set invalid params", input: types.Params{ - MintDenom: sdk.DefaultBondDenom, - BoundingAdjustment: sdk.NewDecWithPrec(-13, 2), - TargetBoundingRatio: sdk.NewDecWithPrec(25, 2), - InflationCoef: sdk.NewDecWithPrec(73, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), + MintDenom: sdk.DefaultBondDenom, + BondingAdjustment: sdk.NewDecWithPrec(-13, 2), + TargetBondingRatio: sdk.NewDecWithPrec(25, 2), + InflationCoef: sdk.NewDecWithPrec(73, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), }, expectErr: true, }, { name: "set invalid params", input: types.Params{ - MintDenom: sdk.DefaultBondDenom, - BoundingAdjustment: sdk.NewDecWithPrec(13, 2), - TargetBoundingRatio: sdk.NewDecWithPrec(-25, 2), - InflationCoef: sdk.NewDecWithPrec(73, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), + MintDenom: sdk.DefaultBondDenom, + BondingAdjustment: sdk.NewDecWithPrec(13, 2), + TargetBondingRatio: sdk.NewDecWithPrec(-25, 2), + InflationCoef: sdk.NewDecWithPrec(73, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), }, expectErr: true, }, { name: "set invalid params", input: types.Params{ - MintDenom: sdk.DefaultBondDenom, - BoundingAdjustment: sdk.NewDecWithPrec(13, 2), - TargetBoundingRatio: sdk.NewDecWithPrec(25, 2), - InflationCoef: sdk.NewDecWithPrec(-73, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), + MintDenom: sdk.DefaultBondDenom, + BondingAdjustment: sdk.NewDecWithPrec(13, 2), + TargetBondingRatio: sdk.NewDecWithPrec(25, 2), + InflationCoef: sdk.NewDecWithPrec(-73, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), }, expectErr: true, }, { name: "set full valid params", input: types.Params{ - MintDenom: sdk.DefaultBondDenom, - BoundingAdjustment: sdk.NewDecWithPrec(13, 2), - TargetBoundingRatio: sdk.NewDecWithPrec(25, 2), - InflationCoef: sdk.NewDecWithPrec(73, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), + MintDenom: sdk.DefaultBondDenom, + BondingAdjustment: sdk.NewDecWithPrec(13, 2), + TargetBondingRatio: sdk.NewDecWithPrec(25, 2), + InflationCoef: sdk.NewDecWithPrec(73, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), }, expectErr: false, }, diff --git a/x/mint/keeper/msg_server_test.go b/x/mint/keeper/msg_server_test.go index d9df15bc..fca95c93 100644 --- a/x/mint/keeper/msg_server_test.go +++ b/x/mint/keeper/msg_server_test.go @@ -24,11 +24,11 @@ func (s *IntegrationTestSuite) TestUpdateParams() { request: &types.MsgUpdateParams{ Authority: s.mintKeeper.GetAuthority(), Params: types.Params{ - MintDenom: sdk.DefaultBondDenom, - BoundingAdjustment: sdk.NewDecWithPrec(-13, 2), - TargetBoundingRatio: sdk.NewDecWithPrec(25, 2), - InflationCoef: sdk.NewDecWithPrec(73, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), + MintDenom: sdk.DefaultBondDenom, + BondingAdjustment: sdk.NewDecWithPrec(-13, 2), + TargetBondingRatio: sdk.NewDecWithPrec(25, 2), + InflationCoef: sdk.NewDecWithPrec(73, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), }, }, expectErr: true, @@ -38,11 +38,11 @@ func (s *IntegrationTestSuite) TestUpdateParams() { request: &types.MsgUpdateParams{ Authority: s.mintKeeper.GetAuthority(), Params: types.Params{ - MintDenom: sdk.DefaultBondDenom, - BoundingAdjustment: sdk.NewDecWithPrec(13, 2), - TargetBoundingRatio: sdk.NewDecWithPrec(-25, 2), - InflationCoef: sdk.NewDecWithPrec(73, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), + MintDenom: sdk.DefaultBondDenom, + BondingAdjustment: sdk.NewDecWithPrec(13, 2), + TargetBondingRatio: sdk.NewDecWithPrec(-25, 2), + InflationCoef: sdk.NewDecWithPrec(73, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), }, }, expectErr: true, @@ -52,11 +52,11 @@ func (s *IntegrationTestSuite) TestUpdateParams() { request: &types.MsgUpdateParams{ Authority: s.mintKeeper.GetAuthority(), Params: types.Params{ - MintDenom: sdk.DefaultBondDenom, - BoundingAdjustment: sdk.NewDecWithPrec(13, 2), - TargetBoundingRatio: sdk.NewDecWithPrec(25, 2), - InflationCoef: sdk.NewDecWithPrec(-73, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), + MintDenom: sdk.DefaultBondDenom, + BondingAdjustment: sdk.NewDecWithPrec(13, 2), + TargetBondingRatio: sdk.NewDecWithPrec(25, 2), + InflationCoef: sdk.NewDecWithPrec(-73, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), }, }, expectErr: true, @@ -66,11 +66,11 @@ func (s *IntegrationTestSuite) TestUpdateParams() { request: &types.MsgUpdateParams{ Authority: s.mintKeeper.GetAuthority(), Params: types.Params{ - MintDenom: sdk.DefaultBondDenom, - BoundingAdjustment: sdk.NewDecWithPrec(13, 2), - TargetBoundingRatio: sdk.NewDecWithPrec(25, 2), - InflationCoef: sdk.NewDecWithPrec(73, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), + MintDenom: sdk.DefaultBondDenom, + BondingAdjustment: sdk.NewDecWithPrec(13, 2), + TargetBondingRatio: sdk.NewDecWithPrec(25, 2), + InflationCoef: sdk.NewDecWithPrec(73, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), }, }, expectErr: false, diff --git a/x/mint/simulation/genesis.go b/x/mint/simulation/genesis.go index d5512047..62ea43b0 100644 --- a/x/mint/simulation/genesis.go +++ b/x/mint/simulation/genesis.go @@ -15,10 +15,10 @@ import ( // Simulation parameter constants. const ( - Inflation = "inflation" - InflationCoef = "inflation_coef" - BoundingAdjustment = "bounding_adjustment" - TargetBoundingRatio = "target_bounding_ratio" + Inflation = "inflation" + InflationCoef = "inflation_coef" + BondingAdjustment = "bonding_adjustment" + TargetBondingRatio = "target_bonding_ratio" ) // GenInflation randomized Inflation. @@ -31,13 +31,13 @@ func GenInflationCoefMax(_ *rand.Rand) sdk.Dec { return sdk.NewDecWithPrec(73, 3) } -// GenBoundingAdjustmentMax randomized AnnualReductionFactor. -func GenBoundingAdjustmentMax(_ *rand.Rand) sdk.Dec { +// GenBondingAdjustmentMax randomized AnnualReductionFactor. +func GenBondingAdjustmentMax(_ *rand.Rand) sdk.Dec { return sdk.NewDecWithPrec(25, 1) } -// GenTargetBoundingRatioMax randomized AnnualReductionFactor. -func GenTargetBoundingRatioMax(_ *rand.Rand) sdk.Dec { +// GenTargetBondingRatioMax randomized AnnualReductionFactor. +func GenTargetBondingRatioMax(_ *rand.Rand) sdk.Dec { return sdk.NewDecWithPrec(66, 2) } @@ -59,21 +59,21 @@ func RandomizedGenState(simState *module.SimulationState) { simState.Cdc, InflationCoef, &inflationCoef, simState.Rand, func(r *rand.Rand) { inflationCoef = GenInflationCoefMax(r) }, ) - var targetBoundingRatio sdk.Dec + var targetBondingRatio sdk.Dec simState.AppParams.GetOrGenerate( - simState.Cdc, TargetBoundingRatio, &targetBoundingRatio, simState.Rand, - func(r *rand.Rand) { targetBoundingRatio = GenTargetBoundingRatioMax(r) }, + simState.Cdc, TargetBondingRatio, &targetBondingRatio, simState.Rand, + func(r *rand.Rand) { targetBondingRatio = GenTargetBondingRatioMax(r) }, ) - var boundingAdjustment sdk.Dec + var bondingAdjustment sdk.Dec simState.AppParams.GetOrGenerate( - simState.Cdc, BoundingAdjustment, &boundingAdjustment, simState.Rand, - func(r *rand.Rand) { boundingAdjustment = GenBoundingAdjustmentMax(r) }, + simState.Cdc, BondingAdjustment, &bondingAdjustment, simState.Rand, + func(r *rand.Rand) { bondingAdjustment = GenBondingAdjustmentMax(r) }, ) mintDenom := sdk.DefaultBondDenom blocksPerYear := uint64(60 * 60 * 8766 / 5) - params := types.NewParams(mintDenom, inflationCoef, boundingAdjustment, targetBoundingRatio, blocksPerYear) + params := types.NewParams(mintDenom, inflationCoef, bondingAdjustment, targetBondingRatio, blocksPerYear) annualProvision := inflation.MulInt(simState.InitialStake) minter := types.InitialMinter(inflation) diff --git a/x/mint/simulation/genesis_test.go b/x/mint/simulation/genesis_test.go index 6dc64b8f..0c75c299 100644 --- a/x/mint/simulation/genesis_test.go +++ b/x/mint/simulation/genesis_test.go @@ -50,8 +50,8 @@ func TestRandomizedGenState(t *testing.T) { require.Equal(t, uint64(6311520), mintGenesis.Params.BlocksPerYear) require.Equal(t, dec1, mintGenesis.Params.InflationCoef) - require.Equal(t, dec2, mintGenesis.Params.BoundingAdjustment) - require.Equal(t, dec3, mintGenesis.Params.TargetBoundingRatio) + require.Equal(t, dec2, mintGenesis.Params.BondingAdjustment) + require.Equal(t, dec3, mintGenesis.Params.TargetBondingRatio) require.Equal(t, "stake", mintGenesis.Params.MintDenom) require.Equal(t, "0stake", mintGenesis.Minter.BlockProvision(mintGenesis.Params).String()) diff --git a/x/mint/simulation/proposals.go b/x/mint/simulation/proposals.go index 93c9a216..14675065 100644 --- a/x/mint/simulation/proposals.go +++ b/x/mint/simulation/proposals.go @@ -37,8 +37,8 @@ func SimulateMsgUpdateParams(r *rand.Rand, _ sdk.Context, _ []simtypes.Account) params := types.DefaultParams() params.BlocksPerYear = uint64(simtypes.RandIntBetween(r, 1, 1000000)) params.InflationCoef = sdk.NewDecWithPrec(int64(simtypes.RandIntBetween(r, 1, 100)), 2) - params.TargetBoundingRatio = sdk.NewDecWithPrec(int64(simtypes.RandIntBetween(r, 1, 100)), 2) - params.BoundingAdjustment = sdk.NewDecWithPrec(int64(simtypes.RandIntBetween(r, 1, 100)), 2) + params.TargetBondingRatio = sdk.NewDecWithPrec(int64(simtypes.RandIntBetween(r, 1, 100)), 2) + params.BondingAdjustment = sdk.NewDecWithPrec(int64(simtypes.RandIntBetween(r, 1, 100)), 2) params.MintDenom = simtypes.RandStringOfLength(r, 10) return &types.MsgUpdateParams{ diff --git a/x/mint/simulation/proposals_test.go b/x/mint/simulation/proposals_test.go index 6d36853e..04f67274 100644 --- a/x/mint/simulation/proposals_test.go +++ b/x/mint/simulation/proposals_test.go @@ -40,8 +40,8 @@ func TestProposalMsgs(t *testing.T) { assert.Equal(t, sdk.AccAddress(address.Module("gov")).String(), msgUpdateParams.Authority) assert.Equal(t, uint64(122877), msgUpdateParams.Params.BlocksPerYear) - assert.DeepEqual(t, sdk.NewDecWithPrec(7, 2), msgUpdateParams.Params.TargetBoundingRatio) + assert.DeepEqual(t, sdk.NewDecWithPrec(7, 2), msgUpdateParams.Params.TargetBondingRatio) assert.DeepEqual(t, sdk.NewDecWithPrec(95, 2), msgUpdateParams.Params.InflationCoef) - assert.DeepEqual(t, sdk.NewDecWithPrec(6, 1), msgUpdateParams.Params.BoundingAdjustment) + assert.DeepEqual(t, sdk.NewDecWithPrec(6, 1), msgUpdateParams.Params.BondingAdjustment) assert.Equal(t, "xKGLwQvuyN", msgUpdateParams.Params.MintDenom) } diff --git a/x/mint/types/mint.pb.go b/x/mint/types/mint.pb.go index 983f30ce..f07c1342 100644 --- a/x/mint/types/mint.pb.go +++ b/x/mint/types/mint.pb.go @@ -79,10 +79,10 @@ type Params struct { MintDenom string `protobuf:"bytes,1,opt,name=mint_denom,json=mintDenom,proto3" json:"mint_denom,omitempty"` // Annual inflation coefficient InflationCoef github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,2,opt,name=inflation_coef,json=inflationCoef,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"inflation_coef"` - // Bounding adjustment - BoundingAdjustment github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,3,opt,name=bounding_adjustment,json=boundingAdjustment,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"bounding_adjustment"` - // Represent the target bounding ratio to reach - TargetBoundingRatio github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,4,opt,name=target_bounding_ratio,json=targetBoundingRatio,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"target_bounding_ratio"` + // Bonding adjustment + BondingAdjustment github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,3,opt,name=bonding_adjustment,json=bondingAdjustment,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"bonding_adjustment"` + // Represent the target bonding ratio to reach + TargetBondingRatio github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,4,opt,name=target_bonding_ratio,json=targetBondingRatio,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"target_bonding_ratio"` // Estimated blocks per year BlocksPerYear uint64 `protobuf:"varint,5,opt,name=blocks_per_year,json=blocksPerYear,proto3" json:"blocks_per_year,omitempty"` } @@ -141,32 +141,32 @@ func init() { func init() { proto.RegisterFile("mint/v1beta1/mint.proto", fileDescriptor_06339c129491fd39) } var fileDescriptor_06339c129491fd39 = []byte{ - // 394 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x92, 0x4f, 0x6e, 0xe2, 0x30, - 0x14, 0xc6, 0x93, 0x81, 0x41, 0xc2, 0x1a, 0xe6, 0x8f, 0x99, 0xd1, 0x64, 0x90, 0x26, 0x20, 0x16, - 0x88, 0x0d, 0x44, 0x68, 0x66, 0x35, 0x62, 0x33, 0x94, 0x6d, 0x25, 0x94, 0x5d, 0xd9, 0x44, 0x4e, - 0x62, 0x52, 0x17, 0x62, 0x47, 0xb6, 0x83, 0xca, 0x2d, 0xba, 0xac, 0xd4, 0x4d, 0x0f, 0xd1, 0x43, - 0xb0, 0x2b, 0xea, 0xaa, 0xea, 0x02, 0x55, 0x70, 0x91, 0xca, 0x71, 0x9a, 0xf6, 0x00, 0xd9, 0x24, - 0xef, 0x7d, 0xcf, 0xfa, 0x7d, 0x9f, 0xad, 0x07, 0x7e, 0xc6, 0x84, 0x4a, 0x67, 0x3d, 0xf2, 0xb1, - 0x44, 0x23, 0x47, 0x35, 0xc3, 0x84, 0x33, 0xc9, 0xe0, 0xa7, 0xac, 0xce, 0x07, 0xad, 0x5f, 0x01, - 0x13, 0x31, 0x13, 0x5e, 0x36, 0x73, 0x74, 0xa3, 0x0f, 0xb6, 0xbe, 0x47, 0x2c, 0x62, 0x5a, 0x57, - 0x95, 0x56, 0xbb, 0xf7, 0x26, 0xa8, 0x9d, 0x12, 0x2a, 0x31, 0x87, 0x73, 0x50, 0x27, 0x74, 0xb1, - 0x42, 0x92, 0x30, 0x6a, 0x99, 0x1d, 0xb3, 0x5f, 0x9f, 0x8c, 0xb7, 0xfb, 0xb6, 0xf1, 0xb4, 0x6f, - 0xf7, 0x22, 0x22, 0xcf, 0x53, 0x7f, 0x18, 0xb0, 0x38, 0x87, 0xe6, 0xbf, 0x81, 0x08, 0x97, 0x8e, - 0xdc, 0x24, 0x58, 0x0c, 0xa7, 0x38, 0x78, 0xb8, 0x1b, 0x80, 0xdc, 0x73, 0x8a, 0x03, 0xf7, 0x0d, - 0x07, 0x09, 0xf8, 0x86, 0x28, 0x4d, 0xd1, 0x4a, 0x25, 0x5b, 0x13, 0x41, 0x18, 0x15, 0xd6, 0x87, - 0x12, 0x3c, 0xbe, 0x6a, 0xec, 0xac, 0xa0, 0x76, 0x6f, 0x2a, 0xa0, 0x36, 0x43, 0x1c, 0xc5, 0x02, - 0xfe, 0x06, 0x40, 0xbd, 0x8e, 0x17, 0x62, 0xca, 0x62, 0x7d, 0x25, 0xb7, 0xae, 0x94, 0xa9, 0x12, - 0x60, 0x00, 0x3e, 0x17, 0x09, 0xbd, 0x80, 0xe1, 0x45, 0x29, 0x89, 0x1a, 0x05, 0xf3, 0x84, 0xe1, - 0x05, 0x8c, 0x41, 0xd3, 0x67, 0x29, 0x0d, 0x09, 0x8d, 0x3c, 0x14, 0x5e, 0xa4, 0x42, 0xc6, 0x98, - 0x4a, 0xab, 0x52, 0x82, 0x13, 0x7c, 0x05, 0xff, 0x2f, 0xb8, 0x30, 0x01, 0x3f, 0x24, 0xe2, 0x11, - 0x96, 0x5e, 0xe1, 0xca, 0x55, 0x18, 0xab, 0x5a, 0x82, 0x61, 0x53, 0xa3, 0x27, 0x39, 0xd9, 0x55, - 0x60, 0xd8, 0x03, 0x5f, 0xfc, 0x15, 0x0b, 0x96, 0xc2, 0x4b, 0x30, 0xf7, 0x36, 0x18, 0x71, 0xeb, - 0x63, 0xc7, 0xec, 0x57, 0xdd, 0x86, 0x96, 0x67, 0x98, 0x9f, 0x61, 0xc4, 0xff, 0x55, 0xaf, 0x6f, - 0xdb, 0xc6, 0x64, 0xbc, 0x3d, 0xd8, 0xe6, 0xee, 0x60, 0x9b, 0xcf, 0x07, 0xdb, 0xbc, 0x3a, 0xda, - 0xc6, 0xee, 0x68, 0x1b, 0x8f, 0x47, 0xdb, 0x98, 0x77, 0xdf, 0x45, 0x62, 0xcb, 0xe4, 0x6f, 0xf6, - 0x09, 0x9d, 0xcb, 0x6c, 0xd9, 0x75, 0x24, 0xbf, 0x96, 0x2d, 0xed, 0x9f, 0x97, 0x00, 0x00, 0x00, - 0xff, 0xff, 0xf8, 0x24, 0xd5, 0x53, 0x0e, 0x03, 0x00, 0x00, + // 391 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x92, 0xc1, 0x6e, 0xda, 0x30, + 0x18, 0xc7, 0x93, 0xc1, 0x90, 0xb0, 0xc6, 0x36, 0x2c, 0xa4, 0x65, 0x48, 0x0b, 0x88, 0x03, 0xe2, + 0x02, 0x11, 0xda, 0x4e, 0x13, 0x97, 0x31, 0xae, 0x95, 0x50, 0x6e, 0xe5, 0x12, 0x39, 0x89, 0x49, + 0xdd, 0x10, 0x3b, 0xb2, 0x0d, 0x2a, 0x6f, 0xd1, 0x5b, 0x7b, 0xec, 0x43, 0xf4, 0x21, 0xb8, 0x15, + 0xf5, 0x54, 0xf5, 0x80, 0x2a, 0x78, 0x91, 0xca, 0x71, 0x9a, 0xf6, 0x01, 0x72, 0x49, 0xbe, 0xef, + 0xff, 0x59, 0xbf, 0xff, 0xdf, 0xd6, 0x07, 0x7e, 0x24, 0x84, 0x4a, 0x67, 0x33, 0xf6, 0xb1, 0x44, + 0x63, 0x47, 0x35, 0xa3, 0x94, 0x33, 0xc9, 0xe0, 0x97, 0xac, 0xce, 0x07, 0xed, 0x9f, 0x01, 0x13, + 0x09, 0x13, 0x5e, 0x36, 0x73, 0x74, 0xa3, 0x0f, 0xb6, 0x5b, 0x11, 0x8b, 0x98, 0xd6, 0x55, 0xa5, + 0xd5, 0xde, 0x83, 0x09, 0x6a, 0x67, 0x84, 0x4a, 0xcc, 0xe1, 0x02, 0xd4, 0x09, 0x5d, 0xae, 0x90, + 0x24, 0x8c, 0x5a, 0x66, 0xd7, 0x1c, 0xd4, 0xa7, 0x93, 0xdd, 0xa1, 0x63, 0x3c, 0x1f, 0x3a, 0xfd, + 0x88, 0xc8, 0x8b, 0xb5, 0x3f, 0x0a, 0x58, 0x92, 0x43, 0xf3, 0xdf, 0x50, 0x84, 0xb1, 0x23, 0xb7, + 0x29, 0x16, 0xa3, 0x19, 0x0e, 0x1e, 0xef, 0x87, 0x20, 0xf7, 0x9c, 0xe1, 0xc0, 0x7d, 0xc7, 0x41, + 0x02, 0x9a, 0x88, 0xd2, 0x35, 0x5a, 0xa9, 0x64, 0x1b, 0x22, 0x08, 0xa3, 0xc2, 0xfa, 0x54, 0x82, + 0xc7, 0x77, 0x8d, 0x9d, 0x17, 0xd4, 0xde, 0x4d, 0x05, 0xd4, 0xe6, 0x88, 0xa3, 0x44, 0xc0, 0x5f, + 0x00, 0xa8, 0xd7, 0xf1, 0x42, 0x4c, 0x59, 0xa2, 0xaf, 0xe4, 0xd6, 0x95, 0x32, 0x53, 0x02, 0x0c, + 0xc0, 0xd7, 0x22, 0xa1, 0x17, 0x30, 0xbc, 0x2c, 0x25, 0x51, 0xa3, 0x60, 0xfe, 0x67, 0x78, 0x09, + 0x63, 0x00, 0x7d, 0x46, 0x43, 0x42, 0x23, 0x0f, 0x85, 0x97, 0x6b, 0x21, 0x13, 0x4c, 0xa5, 0x55, + 0x29, 0xc1, 0xa8, 0x99, 0x73, 0xff, 0x15, 0x58, 0x48, 0x41, 0x4b, 0x22, 0x1e, 0x61, 0xe9, 0xbd, + 0x79, 0x72, 0x95, 0xc4, 0xaa, 0x96, 0x60, 0x07, 0x35, 0x79, 0xaa, 0xc1, 0xae, 0xe2, 0xc2, 0x3e, + 0xf8, 0xe6, 0xaf, 0x58, 0x10, 0x0b, 0x2f, 0xc5, 0xdc, 0xdb, 0x62, 0xc4, 0xad, 0xcf, 0x5d, 0x73, + 0x50, 0x75, 0x1b, 0x5a, 0x9e, 0x63, 0x7e, 0x8e, 0x11, 0xff, 0x5b, 0xbd, 0xbd, 0xeb, 0x18, 0xd3, + 0xc9, 0xee, 0x68, 0x9b, 0xfb, 0xa3, 0x6d, 0xbe, 0x1c, 0x6d, 0xf3, 0xfa, 0x64, 0x1b, 0xfb, 0x93, + 0x6d, 0x3c, 0x9d, 0x6c, 0x63, 0xd1, 0xfb, 0x90, 0x88, 0xc5, 0xe9, 0x9f, 0xec, 0x13, 0x3a, 0x57, + 0xd9, 0xa2, 0xeb, 0x44, 0x7e, 0x2d, 0x5b, 0xd8, 0xdf, 0xaf, 0x01, 0x00, 0x00, 0xff, 0xff, 0x3f, + 0xcf, 0x8a, 0x48, 0x0a, 0x03, 0x00, 0x00, } func (m *Minter) Marshal() (dAtA []byte, err error) { @@ -238,9 +238,9 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { dAtA[i] = 0x28 } { - size := m.TargetBoundingRatio.Size() + size := m.TargetBondingRatio.Size() i -= size - if _, err := m.TargetBoundingRatio.MarshalTo(dAtA[i:]); err != nil { + if _, err := m.TargetBondingRatio.MarshalTo(dAtA[i:]); err != nil { return 0, err } i = encodeVarintMint(dAtA, i, uint64(size)) @@ -248,9 +248,9 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { i-- dAtA[i] = 0x22 { - size := m.BoundingAdjustment.Size() + size := m.BondingAdjustment.Size() i -= size - if _, err := m.BoundingAdjustment.MarshalTo(dAtA[i:]); err != nil { + if _, err := m.BondingAdjustment.MarshalTo(dAtA[i:]); err != nil { return 0, err } i = encodeVarintMint(dAtA, i, uint64(size)) @@ -313,9 +313,9 @@ func (m *Params) Size() (n int) { } l = m.InflationCoef.Size() n += 1 + l + sovMint(uint64(l)) - l = m.BoundingAdjustment.Size() + l = m.BondingAdjustment.Size() n += 1 + l + sovMint(uint64(l)) - l = m.TargetBoundingRatio.Size() + l = m.TargetBondingRatio.Size() n += 1 + l + sovMint(uint64(l)) if m.BlocksPerYear != 0 { n += 1 + sovMint(uint64(m.BlocksPerYear)) @@ -544,7 +544,7 @@ func (m *Params) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 3: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field BoundingAdjustment", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field BondingAdjustment", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 { @@ -572,13 +572,13 @@ func (m *Params) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.BoundingAdjustment.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.BondingAdjustment.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex case 4: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field TargetBoundingRatio", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field TargetBondingRatio", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 { @@ -606,7 +606,7 @@ func (m *Params) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.TargetBoundingRatio.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.TargetBondingRatio.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex diff --git a/x/mint/types/minter.go b/x/mint/types/minter.go index 9ca084ba..10d0f2d1 100644 --- a/x/mint/types/minter.go +++ b/x/mint/types/minter.go @@ -43,9 +43,9 @@ func ValidateMinter(minter Minter) error { } // NextInflation return estimated yearly inflation rate for the current block. -func (m Minter) NextInflation(params Params, boundedRatio sdk.Dec) sdk.Dec { - bounded := params.BoundingAdjustment.Sub(boundedRatio.Quo(params.TargetBoundingRatio)) - return params.InflationCoef.Mul(bounded) +func (m Minter) NextInflation(params Params, bondedRatio sdk.Dec) sdk.Dec { + bonded := params.BondingAdjustment.Sub(bondedRatio.Quo(params.TargetBondingRatio)) + return params.InflationCoef.Mul(bonded) } // NextAnnualProvisions returns the annual provisions based on current total diff --git a/x/mint/types/minter_test.go b/x/mint/types/minter_test.go index d408131a..c289eebd 100644 --- a/x/mint/types/minter_test.go +++ b/x/mint/types/minter_test.go @@ -15,17 +15,17 @@ func TestNextInflation(t *testing.T) { params := DefaultParams() tests := []struct { - boundedRatio, expInflation sdk.Dec + bondedRatio, expInflation sdk.Dec }{ - // With a bounded ratio of 66 %, next inflation should be 10.95% + // With a bonded ratio of 66 %, next inflation should be 10.95% {sdk.NewDecWithPrec(66, 2), sdk.NewDecWithPrec(1095, 4)}, - // With a bounded ratio of 0 %, next inflation should be 18.25% + // With a bonded ratio of 0 %, next inflation should be 18.25% {sdk.NewDecWithPrec(0, 2), sdk.NewDecWithPrec(1825, 4)}, - // With a bounded ratio of 100 %, next inflation should be 7.18% + // With a bonded ratio of 100 %, next inflation should be 7.18% {sdk.NewDecWithPrec(1, 0), sdk.NewDecWithPrec(71893939393939394, 18)}, } for i, tc := range tests { - inflation := minter.NextInflation(params, tc.boundedRatio) + inflation := minter.NextInflation(params, tc.bondedRatio) require.True(t, inflation.Equal(tc.expInflation), "Test Index: %v\nInflation: %v\nExpected: %v\n", i, inflation, tc.expInflation) diff --git a/x/mint/types/params.go b/x/mint/types/params.go index 7c95c6e1..8cd28e22 100644 --- a/x/mint/types/params.go +++ b/x/mint/types/params.go @@ -11,25 +11,25 @@ import ( ) func NewParams( - mintDenom string, inflationCoef, boundingAdjustment, targetBoundingRatio sdk.Dec, blocksPerYear uint64, + mintDenom string, inflationCoef, bondingAdjustment, targetBondingRatio sdk.Dec, blocksPerYear uint64, ) Params { return Params{ - MintDenom: mintDenom, - InflationCoef: inflationCoef, - BoundingAdjustment: boundingAdjustment, - TargetBoundingRatio: targetBoundingRatio, - BlocksPerYear: blocksPerYear, + MintDenom: mintDenom, + InflationCoef: inflationCoef, + BondingAdjustment: bondingAdjustment, + TargetBondingRatio: targetBondingRatio, + BlocksPerYear: blocksPerYear, } } // default minting module parameters. func DefaultParams() Params { return Params{ - MintDenom: sdk.DefaultBondDenom, - InflationCoef: sdk.NewDecWithPrec(73, 3), - BoundingAdjustment: sdk.NewDecWithPrec(25, 1), - TargetBoundingRatio: sdk.NewDecWithPrec(66, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), // assuming 5-second block times + MintDenom: sdk.DefaultBondDenom, + InflationCoef: sdk.NewDecWithPrec(73, 3), + BondingAdjustment: sdk.NewDecWithPrec(25, 1), + TargetBondingRatio: sdk.NewDecWithPrec(66, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), // assuming 5-second block times } } @@ -41,10 +41,10 @@ func (p Params) Validate() error { if err := validateInflationCoef(p.InflationCoef); err != nil { return err } - if err := validateBoundingAdjustment(p.BoundingAdjustment); err != nil { + if err := validateBondingAdjustment(p.BondingAdjustment); err != nil { return err } - if err := validateTargetBoundingRatio(p.TargetBoundingRatio); err != nil { + if err := validateTargetBondingRatio(p.TargetBondingRatio); err != nil { return err } @@ -86,7 +86,7 @@ func validateInflationCoef(i interface{}) error { return nil } -func validateBoundingAdjustment(i interface{}) error { +func validateBondingAdjustment(i interface{}) error { v, ok := i.(sdk.Dec) if !ok { return fmt.Errorf("invalid parameter type: %T", i) @@ -99,17 +99,17 @@ func validateBoundingAdjustment(i interface{}) error { return nil } -func validateTargetBoundingRatio(i interface{}) error { +func validateTargetBondingRatio(i interface{}) error { v, ok := i.(sdk.Dec) if !ok { return fmt.Errorf("invalid parameter type: %T", i) } if v.IsNegative() { - return fmt.Errorf("target bounding ratio cannot be negative: %s", v) + return fmt.Errorf("target bonding ratio cannot be negative: %s", v) } if v.GT(sdk.OneDec()) { - return fmt.Errorf("target bounding ratio too large: %s", v) + return fmt.Errorf("target bonding ratio too large: %s", v) } return nil From 919dccc882e89d32f51e3d98d4b0b0ff6f3ebd40 Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Wed, 20 Sep 2023 11:36:25 +0200 Subject: [PATCH 19/24] test(mint): make test case name more comprehensive --- x/mint/keeper/msg_server_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x/mint/keeper/msg_server_test.go b/x/mint/keeper/msg_server_test.go index fca95c93..5e37463a 100644 --- a/x/mint/keeper/msg_server_test.go +++ b/x/mint/keeper/msg_server_test.go @@ -20,7 +20,7 @@ func (s *IntegrationTestSuite) TestUpdateParams() { expectErr: true, }, { - name: "set invalid params", + name: "set invalid params for bonding adjustment (negative value)", request: &types.MsgUpdateParams{ Authority: s.mintKeeper.GetAuthority(), Params: types.Params{ @@ -34,7 +34,7 @@ func (s *IntegrationTestSuite) TestUpdateParams() { expectErr: true, }, { - name: "set invalid params", + name: "set invalid params for target bonding ratio (negative value)", request: &types.MsgUpdateParams{ Authority: s.mintKeeper.GetAuthority(), Params: types.Params{ @@ -48,7 +48,7 @@ func (s *IntegrationTestSuite) TestUpdateParams() { expectErr: true, }, { - name: "set invalid params", + name: "set invalid params for inflation coef (negative value)", request: &types.MsgUpdateParams{ Authority: s.mintKeeper.GetAuthority(), Params: types.Params{ From 36a965f0c0a4adc8257f66fd1ab023edb8056d2b Mon Sep 17 00:00:00 2001 From: ccamel Date: Mon, 4 Dec 2023 17:25:02 +0100 Subject: [PATCH 20/24] feat(mint): specify token model v2 schemas --- proto/mint/v1beta1/mint.proto | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/proto/mint/v1beta1/mint.proto b/proto/mint/v1beta1/mint.proto index 25575198..05e74f0d 100644 --- a/proto/mint/v1beta1/mint.proto +++ b/proto/mint/v1beta1/mint.proto @@ -7,14 +7,13 @@ import "gogoproto/gogo.proto"; option go_package = "github.com/okp4/okp4d/x/mint/types"; -// Minter represents the minting state within the blockchain, tasked with the continuous calculation and distribution -// of tokens to validators. +// Minter represents the minting state within the blockchain. // -// This calculation occurs with each block, where the minting module dynamically recalculates the annual `inflation` rate. -// Using the resulting inflation rate, it deduces the quantity of tokens to be provisioned for the upcoming year. +// At each block, the minting module recalculates the annual inflation rate dynamically. It then determines the total +// amount of tokens to be provisioned for the upcoming year based on this rate. // -// Furthermore, based on the current block's position within the year, it computes the exact number of tokens to be -// minted for that specific block. +// Additionally, the module computes the precise number of tokens to be minted for the current block, taking into account +// its position within the annual cycle. message Minter { // Represents the current annual inflation rate. string inflation = 1 [ @@ -42,18 +41,6 @@ message Params { (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false ]; - // Bonding adjustment - string bonding_adjustment = 3 [ - (cosmos_proto.scalar) = "cosmos.Dec", - (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", - (gogoproto.nullable) = false - ]; - // Represent the target bonding ratio to reach - string target_bonding_ratio = 4 [ - (cosmos_proto.scalar) = "cosmos.Dec", - (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", - (gogoproto.nullable) = false - ]; // Estimated blocks per year - uint64 blocks_per_year = 5; + uint64 blocks_per_year = 3; } From 473221d1fbd0d2c9b0a536b294cb52d5354f8458 Mon Sep 17 00:00:00 2001 From: ccamel Date: Mon, 4 Dec 2023 17:24:42 +0100 Subject: [PATCH 21/24] feat(mint): implement token model v2 --- app/upgrades/v6/upgrade.go | 2 +- go.mod | 2 +- x/mint/abci.go | 13 ++- x/mint/simulation/genesis.go | 31 +------ x/mint/simulation/proposals.go | 2 - x/mint/types/errors.go | 5 + x/mint/types/genesis.go | 2 +- x/mint/types/mint.pb.go | 160 ++++++-------------------------- x/mint/types/minter.go | 47 ++++++---- x/mint/types/minter_test.go | 162 +++++++++++++++------------------ x/mint/types/params.go | 60 +++--------- 11 files changed, 164 insertions(+), 322 deletions(-) create mode 100644 x/mint/types/errors.go diff --git a/app/upgrades/v6/upgrade.go b/app/upgrades/v6/upgrade.go index 8f3f0660..7f8a5ca7 100644 --- a/app/upgrades/v6/upgrade.go +++ b/app/upgrades/v6/upgrade.go @@ -1,4 +1,4 @@ -package v5 +package v6 import ( storetypes "github.com/cosmos/cosmos-sdk/store/types" diff --git a/go.mod b/go.mod index f0f3dc0e..95280480 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/cosmos/cosmos-sdk v0.47.3 github.com/cosmos/gogoproto v1.4.10 github.com/cosmos/ibc-go/v7 v7.1.0 + github.com/dustinxie/ecc v0.0.0-20210511000915-959544187564 github.com/golang/mock v1.6.0 github.com/golang/protobuf v1.5.3 github.com/grpc-ecosystem/grpc-gateway v1.16.0 @@ -101,7 +102,6 @@ require ( github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect github.com/docker/distribution v2.8.2+incompatible // indirect github.com/dustin/go-humanize v1.0.1 // indirect - github.com/dustinxie/ecc v0.0.0-20210511000915-959544187564 // indirect github.com/dvsekhvalnov/jose2go v1.5.0 // indirect github.com/emirpasic/gods v1.18.1 // indirect github.com/fatih/color v1.15.0 // indirect diff --git a/x/mint/abci.go b/x/mint/abci.go index a5ed50ec..0255a250 100644 --- a/x/mint/abci.go +++ b/x/mint/abci.go @@ -14,22 +14,25 @@ import ( func BeginBlocker(ctx sdk.Context, k keeper.Keeper) { defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker) - // fetch stored minter & params - minter := k.GetMinter(ctx) + // fetch stored params params := k.GetParams(ctx) // recalculate inflation rate totalSupply := k.TokenSupply(ctx, params.MintDenom) bondedRatio := k.BondedRatio(ctx) - minter.Inflation = minter.NextInflation(params, bondedRatio) - minter.AnnualProvisions = minter.NextAnnualProvisions(params, totalSupply) + + minter, err := types.NewMinterWithInflationCoef(params.InflationCoef, bondedRatio, totalSupply) + if err != nil { + panic(err) + } + k.SetMinter(ctx, minter) // mint coins, update supply mintedCoin := minter.BlockProvision(params) mintedCoins := sdk.NewCoins(mintedCoin) - err := k.MintCoins(ctx, mintedCoins) + err = k.MintCoins(ctx, mintedCoins) if err != nil { panic(err) } diff --git a/x/mint/simulation/genesis.go b/x/mint/simulation/genesis.go index 62ea43b0..c54d5bbb 100644 --- a/x/mint/simulation/genesis.go +++ b/x/mint/simulation/genesis.go @@ -15,10 +15,8 @@ import ( // Simulation parameter constants. const ( - Inflation = "inflation" - InflationCoef = "inflation_coef" - BondingAdjustment = "bonding_adjustment" - TargetBondingRatio = "target_bonding_ratio" + Inflation = "inflation" + InflationCoef = "inflation_coef" ) // GenInflation randomized Inflation. @@ -31,16 +29,6 @@ func GenInflationCoefMax(_ *rand.Rand) sdk.Dec { return sdk.NewDecWithPrec(73, 3) } -// GenBondingAdjustmentMax randomized AnnualReductionFactor. -func GenBondingAdjustmentMax(_ *rand.Rand) sdk.Dec { - return sdk.NewDecWithPrec(25, 1) -} - -// GenTargetBondingRatioMax randomized AnnualReductionFactor. -func GenTargetBondingRatioMax(_ *rand.Rand) sdk.Dec { - return sdk.NewDecWithPrec(66, 2) -} - // RandomizedGenState generates a random GenesisState for mint. // //nolint:forbidigo @@ -59,24 +47,13 @@ func RandomizedGenState(simState *module.SimulationState) { simState.Cdc, InflationCoef, &inflationCoef, simState.Rand, func(r *rand.Rand) { inflationCoef = GenInflationCoefMax(r) }, ) - var targetBondingRatio sdk.Dec - simState.AppParams.GetOrGenerate( - simState.Cdc, TargetBondingRatio, &targetBondingRatio, simState.Rand, - func(r *rand.Rand) { targetBondingRatio = GenTargetBondingRatioMax(r) }, - ) - - var bondingAdjustment sdk.Dec - simState.AppParams.GetOrGenerate( - simState.Cdc, BondingAdjustment, &bondingAdjustment, simState.Rand, - func(r *rand.Rand) { bondingAdjustment = GenBondingAdjustmentMax(r) }, - ) mintDenom := sdk.DefaultBondDenom blocksPerYear := uint64(60 * 60 * 8766 / 5) - params := types.NewParams(mintDenom, inflationCoef, bondingAdjustment, targetBondingRatio, blocksPerYear) + params := types.NewParams(mintDenom, inflationCoef, blocksPerYear) annualProvision := inflation.MulInt(simState.InitialStake) - minter := types.InitialMinter(inflation) + minter := types.NewMinterWithInitialInflation(inflation) minter.AnnualProvisions = annualProvision mintGenesis := types.NewGenesisState(minter, params) diff --git a/x/mint/simulation/proposals.go b/x/mint/simulation/proposals.go index 14675065..1818cb9c 100644 --- a/x/mint/simulation/proposals.go +++ b/x/mint/simulation/proposals.go @@ -37,8 +37,6 @@ func SimulateMsgUpdateParams(r *rand.Rand, _ sdk.Context, _ []simtypes.Account) params := types.DefaultParams() params.BlocksPerYear = uint64(simtypes.RandIntBetween(r, 1, 1000000)) params.InflationCoef = sdk.NewDecWithPrec(int64(simtypes.RandIntBetween(r, 1, 100)), 2) - params.TargetBondingRatio = sdk.NewDecWithPrec(int64(simtypes.RandIntBetween(r, 1, 100)), 2) - params.BondingAdjustment = sdk.NewDecWithPrec(int64(simtypes.RandIntBetween(r, 1, 100)), 2) params.MintDenom = simtypes.RandStringOfLength(r, 10) return &types.MsgUpdateParams{ diff --git a/x/mint/types/errors.go b/x/mint/types/errors.go new file mode 100644 index 00000000..c2645a93 --- /dev/null +++ b/x/mint/types/errors.go @@ -0,0 +1,5 @@ +package types + +import sdkerrors "cosmossdk.io/errors" + +var ErrBondedRatioIsZero = sdkerrors.Register(ModuleName, 1, "bonded ratio is zero") diff --git a/x/mint/types/genesis.go b/x/mint/types/genesis.go index c9605bc8..91297eae 100644 --- a/x/mint/types/genesis.go +++ b/x/mint/types/genesis.go @@ -23,5 +23,5 @@ func ValidateGenesis(data GenesisState) error { return err } - return ValidateMinter(data.Minter) + return data.Minter.Validate() } diff --git a/x/mint/types/mint.pb.go b/x/mint/types/mint.pb.go index f07c1342..c4176ab3 100644 --- a/x/mint/types/mint.pb.go +++ b/x/mint/types/mint.pb.go @@ -25,14 +25,13 @@ var _ = math.Inf // proto package needs to be updated. const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package -// Minter represents the minting state within the blockchain, tasked with the continuous calculation and distribution -// of tokens to validators. +// Minter represents the minting state within the blockchain. // -// This calculation occurs with each block, where the minting module dynamically recalculates the annual `inflation` rate. -// Using the resulting inflation rate, it deduces the quantity of tokens to be provisioned for the upcoming year. +// At each block, the minting module recalculates the annual inflation rate dynamically. It then determines the total +// amount of tokens to be provisioned for the upcoming year based on this rate. // -// Furthermore, based on the current block's position within the year, it computes the exact number of tokens to be -// minted for that specific block. +// Additionally, the module computes the precise number of tokens to be minted for the current block, taking into account +// its position within the annual cycle. type Minter struct { // Represents the current annual inflation rate. Inflation github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,1,opt,name=inflation,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"inflation"` @@ -79,12 +78,8 @@ type Params struct { MintDenom string `protobuf:"bytes,1,opt,name=mint_denom,json=mintDenom,proto3" json:"mint_denom,omitempty"` // Annual inflation coefficient InflationCoef github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,2,opt,name=inflation_coef,json=inflationCoef,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"inflation_coef"` - // Bonding adjustment - BondingAdjustment github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,3,opt,name=bonding_adjustment,json=bondingAdjustment,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"bonding_adjustment"` - // Represent the target bonding ratio to reach - TargetBondingRatio github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,4,opt,name=target_bonding_ratio,json=targetBondingRatio,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"target_bonding_ratio"` // Estimated blocks per year - BlocksPerYear uint64 `protobuf:"varint,5,opt,name=blocks_per_year,json=blocksPerYear,proto3" json:"blocks_per_year,omitempty"` + BlocksPerYear uint64 `protobuf:"varint,3,opt,name=blocks_per_year,json=blocksPerYear,proto3" json:"blocks_per_year,omitempty"` } func (m *Params) Reset() { *m = Params{} } @@ -141,32 +136,29 @@ func init() { func init() { proto.RegisterFile("mint/v1beta1/mint.proto", fileDescriptor_06339c129491fd39) } var fileDescriptor_06339c129491fd39 = []byte{ - // 391 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x92, 0xc1, 0x6e, 0xda, 0x30, - 0x18, 0xc7, 0x93, 0xc1, 0x90, 0xb0, 0xc6, 0x36, 0x2c, 0xa4, 0x65, 0x48, 0x0b, 0x88, 0x03, 0xe2, - 0x02, 0x11, 0xda, 0x4e, 0x13, 0x97, 0x31, 0xae, 0x95, 0x50, 0x6e, 0xe5, 0x12, 0x39, 0x89, 0x49, - 0xdd, 0x10, 0x3b, 0xb2, 0x0d, 0x2a, 0x6f, 0xd1, 0x5b, 0x7b, 0xec, 0x43, 0xf4, 0x21, 0xb8, 0x15, - 0xf5, 0x54, 0xf5, 0x80, 0x2a, 0x78, 0x91, 0xca, 0x71, 0x9a, 0xf6, 0x01, 0x72, 0x49, 0xbe, 0xef, - 0xff, 0x59, 0xbf, 0xff, 0xdf, 0xd6, 0x07, 0x7e, 0x24, 0x84, 0x4a, 0x67, 0x33, 0xf6, 0xb1, 0x44, - 0x63, 0x47, 0x35, 0xa3, 0x94, 0x33, 0xc9, 0xe0, 0x97, 0xac, 0xce, 0x07, 0xed, 0x9f, 0x01, 0x13, - 0x09, 0x13, 0x5e, 0x36, 0x73, 0x74, 0xa3, 0x0f, 0xb6, 0x5b, 0x11, 0x8b, 0x98, 0xd6, 0x55, 0xa5, - 0xd5, 0xde, 0x83, 0x09, 0x6a, 0x67, 0x84, 0x4a, 0xcc, 0xe1, 0x02, 0xd4, 0x09, 0x5d, 0xae, 0x90, - 0x24, 0x8c, 0x5a, 0x66, 0xd7, 0x1c, 0xd4, 0xa7, 0x93, 0xdd, 0xa1, 0x63, 0x3c, 0x1f, 0x3a, 0xfd, - 0x88, 0xc8, 0x8b, 0xb5, 0x3f, 0x0a, 0x58, 0x92, 0x43, 0xf3, 0xdf, 0x50, 0x84, 0xb1, 0x23, 0xb7, - 0x29, 0x16, 0xa3, 0x19, 0x0e, 0x1e, 0xef, 0x87, 0x20, 0xf7, 0x9c, 0xe1, 0xc0, 0x7d, 0xc7, 0x41, - 0x02, 0x9a, 0x88, 0xd2, 0x35, 0x5a, 0xa9, 0x64, 0x1b, 0x22, 0x08, 0xa3, 0xc2, 0xfa, 0x54, 0x82, - 0xc7, 0x77, 0x8d, 0x9d, 0x17, 0xd4, 0xde, 0x4d, 0x05, 0xd4, 0xe6, 0x88, 0xa3, 0x44, 0xc0, 0x5f, - 0x00, 0xa8, 0xd7, 0xf1, 0x42, 0x4c, 0x59, 0xa2, 0xaf, 0xe4, 0xd6, 0x95, 0x32, 0x53, 0x02, 0x0c, - 0xc0, 0xd7, 0x22, 0xa1, 0x17, 0x30, 0xbc, 0x2c, 0x25, 0x51, 0xa3, 0x60, 0xfe, 0x67, 0x78, 0x09, - 0x63, 0x00, 0x7d, 0x46, 0x43, 0x42, 0x23, 0x0f, 0x85, 0x97, 0x6b, 0x21, 0x13, 0x4c, 0xa5, 0x55, - 0x29, 0xc1, 0xa8, 0x99, 0x73, 0xff, 0x15, 0x58, 0x48, 0x41, 0x4b, 0x22, 0x1e, 0x61, 0xe9, 0xbd, - 0x79, 0x72, 0x95, 0xc4, 0xaa, 0x96, 0x60, 0x07, 0x35, 0x79, 0xaa, 0xc1, 0xae, 0xe2, 0xc2, 0x3e, - 0xf8, 0xe6, 0xaf, 0x58, 0x10, 0x0b, 0x2f, 0xc5, 0xdc, 0xdb, 0x62, 0xc4, 0xad, 0xcf, 0x5d, 0x73, - 0x50, 0x75, 0x1b, 0x5a, 0x9e, 0x63, 0x7e, 0x8e, 0x11, 0xff, 0x5b, 0xbd, 0xbd, 0xeb, 0x18, 0xd3, - 0xc9, 0xee, 0x68, 0x9b, 0xfb, 0xa3, 0x6d, 0xbe, 0x1c, 0x6d, 0xf3, 0xfa, 0x64, 0x1b, 0xfb, 0x93, - 0x6d, 0x3c, 0x9d, 0x6c, 0x63, 0xd1, 0xfb, 0x90, 0x88, 0xc5, 0xe9, 0x9f, 0xec, 0x13, 0x3a, 0x57, - 0xd9, 0xa2, 0xeb, 0x44, 0x7e, 0x2d, 0x5b, 0xd8, 0xdf, 0xaf, 0x01, 0x00, 0x00, 0xff, 0xff, 0x3f, - 0xcf, 0x8a, 0x48, 0x0a, 0x03, 0x00, 0x00, + // 337 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0xcf, 0xcd, 0xcc, 0x2b, + 0xd1, 0x2f, 0x33, 0x4c, 0x4a, 0x2d, 0x49, 0x34, 0xd4, 0x07, 0x71, 0xf4, 0x0a, 0x8a, 0xf2, 0x4b, + 0xf2, 0x85, 0x78, 0xc0, 0x6c, 0xa8, 0x84, 0x94, 0x64, 0x72, 0x7e, 0x71, 0x6e, 0x7e, 0x71, 0x3c, + 0x58, 0x4e, 0x1f, 0xc2, 0x81, 0x28, 0x94, 0x12, 0x49, 0xcf, 0x4f, 0xcf, 0x87, 0x88, 0x83, 0x58, + 0x10, 0x51, 0xa5, 0xf3, 0x8c, 0x5c, 0x6c, 0xbe, 0x99, 0x79, 0x25, 0xa9, 0x45, 0x42, 0x51, 0x5c, + 0x9c, 0x99, 0x79, 0x69, 0x39, 0x89, 0x25, 0x99, 0xf9, 0x79, 0x12, 0x8c, 0x0a, 0x8c, 0x1a, 0x9c, + 0x4e, 0x36, 0x27, 0xee, 0xc9, 0x33, 0xdc, 0xba, 0x27, 0xaf, 0x96, 0x9e, 0x59, 0x92, 0x51, 0x9a, + 0xa4, 0x97, 0x9c, 0x9f, 0x0b, 0x35, 0x14, 0x4a, 0xe9, 0x16, 0xa7, 0x64, 0xeb, 0x97, 0x54, 0x16, + 0xa4, 0x16, 0xeb, 0xb9, 0xa4, 0x26, 0x5f, 0xda, 0xa2, 0xcb, 0x05, 0xb5, 0xd3, 0x25, 0x35, 0x39, + 0x08, 0x61, 0x9c, 0x50, 0x26, 0x97, 0x60, 0x62, 0x5e, 0x5e, 0x69, 0x62, 0x0e, 0xc8, 0x65, 0x65, + 0x99, 0xc5, 0x99, 0xf9, 0x79, 0xc5, 0x12, 0x4c, 0x54, 0xb0, 0x43, 0x00, 0x62, 0x6c, 0x00, 0xdc, + 0x54, 0xa5, 0x5d, 0x8c, 0x5c, 0x6c, 0x01, 0x89, 0x45, 0x89, 0xb9, 0xc5, 0x42, 0xb2, 0x5c, 0x5c, + 0xa0, 0xd0, 0x89, 0x4f, 0x49, 0xcd, 0xcb, 0xcf, 0x85, 0x78, 0x29, 0x88, 0x13, 0x24, 0xe2, 0x02, + 0x12, 0x10, 0x4a, 0xe6, 0xe2, 0x83, 0xbb, 0x30, 0x3e, 0x39, 0x3f, 0x35, 0x8d, 0x2a, 0x2e, 0xe2, + 0x85, 0x9b, 0xe9, 0x9c, 0x9f, 0x9a, 0x26, 0xa4, 0xc6, 0xc5, 0x9f, 0x94, 0x93, 0x9f, 0x9c, 0x5d, + 0x1c, 0x5f, 0x90, 0x5a, 0x14, 0x5f, 0x99, 0x9a, 0x58, 0x24, 0xc1, 0xac, 0xc0, 0xa8, 0xc1, 0x12, + 0xc4, 0x0b, 0x11, 0x0e, 0x48, 0x2d, 0x8a, 0x4c, 0x4d, 0x2c, 0xb2, 0x62, 0x99, 0xb1, 0x40, 0x9e, + 0xc1, 0xc9, 0xe6, 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, 0x94, 0x90, 0x1c, + 0x93, 0x9f, 0x5d, 0x60, 0x02, 0x26, 0x52, 0xf4, 0x2b, 0xc0, 0x69, 0x01, 0xe2, 0x98, 0x24, 0x36, + 0x70, 0x9c, 0x1a, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0xca, 0xeb, 0xc3, 0xfd, 0x2d, 0x02, 0x00, + 0x00, } func (m *Minter) Marshal() (dAtA []byte, err error) { @@ -235,28 +227,8 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { if m.BlocksPerYear != 0 { i = encodeVarintMint(dAtA, i, uint64(m.BlocksPerYear)) i-- - dAtA[i] = 0x28 + dAtA[i] = 0x18 } - { - size := m.TargetBondingRatio.Size() - i -= size - if _, err := m.TargetBondingRatio.MarshalTo(dAtA[i:]); err != nil { - return 0, err - } - i = encodeVarintMint(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0x22 - { - size := m.BondingAdjustment.Size() - i -= size - if _, err := m.BondingAdjustment.MarshalTo(dAtA[i:]); err != nil { - return 0, err - } - i = encodeVarintMint(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0x1a { size := m.InflationCoef.Size() i -= size @@ -313,10 +285,6 @@ func (m *Params) Size() (n int) { } l = m.InflationCoef.Size() n += 1 + l + sovMint(uint64(l)) - l = m.BondingAdjustment.Size() - n += 1 + l + sovMint(uint64(l)) - l = m.TargetBondingRatio.Size() - n += 1 + l + sovMint(uint64(l)) if m.BlocksPerYear != 0 { n += 1 + sovMint(uint64(m.BlocksPerYear)) } @@ -543,74 +511,6 @@ func (m *Params) Unmarshal(dAtA []byte) error { } iNdEx = postIndex case 3: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field BondingAdjustment", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowMint - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - 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.BondingAdjustment.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - case 4: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field TargetBondingRatio", wireType) - } - var stringLen uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowMint - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - 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.TargetBondingRatio.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex - case 5: if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field BlocksPerYear", wireType) } diff --git a/x/mint/types/minter.go b/x/mint/types/minter.go index 10d0f2d1..b2c186fa 100644 --- a/x/mint/types/minter.go +++ b/x/mint/types/minter.go @@ -17,46 +17,55 @@ func NewMinter(inflation, annualProvisions sdk.Dec) Minter { } } -// InitialMinter returns an initial Minter object with a given inflation value and annual reduction factor. -func InitialMinter(inflation sdk.Dec) Minter { +// NewMinterWithInitialInflation returns an initial Minter object with a given inflation value and zero annual provisions. +func NewMinterWithInitialInflation(inflation sdk.Dec) Minter { return NewMinter( inflation, sdk.NewDec(0), ) } +// NewMinterWithInflationCoef returns a new Minter with updated inflation and annual provisions values. +func NewMinterWithInflationCoef(inflationCoef sdk.Dec, bondedRatio sdk.Dec, totalSupply math.Int) (Minter, error) { + inflationRate, err := inflationRate(inflationCoef, bondedRatio) + if err != nil { + return Minter{}, err + } + minter := NewMinter(inflationRate, inflationRate.MulInt(totalSupply)) + + return minter, minter.Validate() +} + // DefaultInitialMinter returns a default initial Minter object for a new chain -// which uses an inflation rate of 18%. +// which uses an inflation rate of 0%. func DefaultInitialMinter() Minter { - return InitialMinter( - sdk.NewDecWithPrec(18, 2), + return NewMinterWithInitialInflation( + sdk.NewDec(0), ) } -// validate minter. -func ValidateMinter(minter Minter) error { - if minter.Inflation.IsNegative() { +// Validate validates the mint parameters. +func (m Minter) Validate() error { + if m.Inflation.IsNegative() { return fmt.Errorf("mint parameter Inflation should be positive, is %s", - minter.Inflation.String()) + m.Inflation.String()) } return nil } -// NextInflation return estimated yearly inflation rate for the current block. -func (m Minter) NextInflation(params Params, bondedRatio sdk.Dec) sdk.Dec { - bonded := params.BondingAdjustment.Sub(bondedRatio.Quo(params.TargetBondingRatio)) - return params.InflationCoef.Mul(bonded) -} +// inflationRate returns the inflation rate computed from the current bonded ratio +// and the inflation parameter. +func inflationRate(inflationCoef sdk.Dec, bondedRatio sdk.Dec) (sdk.Dec, error) { + if bondedRatio.IsZero() { + return math.LegacyZeroDec(), ErrBondedRatioIsZero + } -// NextAnnualProvisions returns the annual provisions based on current total -// supply and inflation rate. -func (m Minter) NextAnnualProvisions(_ Params, totalSupply math.Int) math.LegacyDec { - return m.Inflation.MulInt(totalSupply) + return inflationCoef.Quo(bondedRatio), nil } // BlockProvision returns the provisions for a block based on the annual // provisions rate. func (m Minter) BlockProvision(params Params) sdk.Coin { - provisionAmt := m.AnnualProvisions.QuoInt(sdk.NewInt(int64(params.BlocksPerYear))) + provisionAmt := m.AnnualProvisions.QuoInt(sdk.NewIntFromUint64(params.BlocksPerYear)) return sdk.NewCoin(params.MintDenom, provisionAmt.TruncateInt()) } diff --git a/x/mint/types/minter_test.go b/x/mint/types/minter_test.go index c289eebd..45ad74e7 100644 --- a/x/mint/types/minter_test.go +++ b/x/mint/types/minter_test.go @@ -2,86 +2,81 @@ package types import ( + "fmt" "math/rand" "testing" - "github.com/stretchr/testify/require" + . "github.com/smartystreets/goconvey/convey" + + "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" ) func TestNextInflation(t *testing.T) { - minter := DefaultInitialMinter() - params := DefaultParams() - - tests := []struct { - bondedRatio, expInflation sdk.Dec - }{ - // With a bonded ratio of 66 %, next inflation should be 10.95% - {sdk.NewDecWithPrec(66, 2), sdk.NewDecWithPrec(1095, 4)}, - // With a bonded ratio of 0 %, next inflation should be 18.25% - {sdk.NewDecWithPrec(0, 2), sdk.NewDecWithPrec(1825, 4)}, - // With a bonded ratio of 100 %, next inflation should be 7.18% - {sdk.NewDecWithPrec(1, 0), sdk.NewDecWithPrec(71893939393939394, 18)}, - } - for i, tc := range tests { - inflation := minter.NextInflation(params, tc.bondedRatio) - - require.True(t, inflation.Equal(tc.expInflation), - "Test Index: %v\nInflation: %v\nExpected: %v\n", i, inflation, tc.expInflation) - } -} - -func TestBlockProvision(t *testing.T) { - minter := InitialMinter(sdk.NewDecWithPrec(1, 1)) - params := DefaultParams() - - secondsPerYear := int64(60 * 60 * 8766) - blockInterval := int64(5) // there is 1 block each 5 second approximately - - tests := []struct { - annualProvisions sdk.Dec - expProvisions int64 - }{ - {sdk.NewDec(secondsPerYear / blockInterval), 1}, - {sdk.NewDec(secondsPerYear/blockInterval + 1), 1}, - {sdk.NewDec((secondsPerYear / blockInterval) * 2), 2}, - {sdk.NewDec((secondsPerYear / blockInterval) / 2), 0}, - {sdk.NewDec((secondsPerYear / blockInterval) * 20), 20}, - } - for i, tc := range tests { - minter.AnnualProvisions = tc.annualProvisions - provisions := minter.BlockProvision(params) - - expProvisions := sdk.NewCoin(params.MintDenom, - sdk.NewInt(tc.expProvisions)) - - require.True(t, expProvisions.IsEqual(provisions), - "test: %v\n\tExp: %v\n\tGot: %v\n", - i, tc.expProvisions, provisions) - } -} - -func TestNextAnnualProvision(t *testing.T) { - minter := InitialMinter(sdk.NewDecWithPrec(1, 1)) - params := DefaultParams() - - tests := []struct { - inflation sdk.Dec - totalSupply int64 - expProvisions sdk.Dec - }{ - {sdk.NewDecWithPrec(18, 2), 1000, sdk.NewDec(180)}, - {sdk.NewDecWithPrec(71893939393939394, 18), 1000, sdk.NewDecWithPrec(71893939393939394, 15)}, - } - for i, tc := range tests { - minter.Inflation = tc.inflation - provisions := minter.NextAnnualProvisions(params, sdk.NewInt(tc.totalSupply)) - - require.True(t, tc.expProvisions.Equal(provisions), - "test: %v\n\tExp: %v\n\tGot: %v\n", - i, tc.expProvisions, provisions) - } + Convey("Given a test cases", t, func() { + cases := []struct { + name string + inflationRatio sdk.Dec + bondedRatio sdk.Dec + totalSupply math.Int + expectedInflation sdk.Dec + expectedAnnualProvisions sdk.Dec + expectedErr error + }{ + { + name: "inflation ratio is 0", + inflationRatio: sdk.NewDec(0), + bondedRatio: sdk.NewDecWithPrec(20, 2), + totalSupply: math.NewInt(1000), + expectedInflation: sdk.NewDec(0), + expectedAnnualProvisions: sdk.NewDec(0), + }, + { + name: "inflation ratio is 0.03", + inflationRatio: sdk.NewDecWithPrec(3, 2), + bondedRatio: sdk.NewDecWithPrec(2, 1), + totalSupply: math.NewInt(1000), + expectedInflation: sdk.NewDecWithPrec(15, 2), + expectedAnnualProvisions: sdk.NewDec(150), + }, + { + name: "bonded ratio is 0", + inflationRatio: sdk.NewDecWithPrec(3, 2), + bondedRatio: sdk.NewDec(0), + totalSupply: math.NewInt(1000), + expectedErr: fmt.Errorf("bonded ratio is zero"), + }, + { + name: "negative inflation ratio", + inflationRatio: sdk.NewDecWithPrec(3, 2), + bondedRatio: sdk.NewDecWithPrec(-2, 1), + totalSupply: math.NewInt(1000), + expectedErr: fmt.Errorf("mint parameter Inflation should be positive, is -0.150000000000000000"), + }, + } + + for nc, tc := range cases { + Convey( + fmt.Sprintf("Given test case #%d: %v", nc, tc.name), func() { + Convey("when calling NewMinterWithInflationCoef function", func() { + minter, err := NewMinterWithInflationCoef(tc.inflationRatio, tc.bondedRatio, tc.totalSupply) + if tc.expectedErr != nil { + Convey("then an error should occur", func() { + So(err, ShouldNotBeNil) + So(err.Error(), ShouldEqual, tc.expectedErr.Error()) + }) + } else { + Convey("then minter values should be as expected", func() { + So(err, ShouldBeNil) + So(minter.Inflation.String(), ShouldEqual, tc.expectedInflation.String()) + So(minter.AnnualProvisions.String(), ShouldEqual, tc.expectedAnnualProvisions.String()) + }) + } + }) + }) + } + }) } // Benchmarking :) @@ -92,7 +87,7 @@ func TestNextAnnualProvision(t *testing.T) { // BenchmarkBlockProvision-4 3000000 429 ns/op. func BenchmarkBlockProvision(b *testing.B) { b.ReportAllocs() - minter := InitialMinter(sdk.NewDecWithPrec(1, 1)) + minter := NewMinterWithInitialInflation(sdk.NewDecWithPrec(1, 1)) params := DefaultParams() s1 := rand.NewSource(100) @@ -109,25 +104,16 @@ func BenchmarkBlockProvision(b *testing.B) { // BenchmarkNextInflation-4 1000000 1828 ns/op. func BenchmarkNextInflation(b *testing.B) { b.ReportAllocs() - minter := InitialMinter(sdk.NewDecWithPrec(1, 1)) - params := DefaultParams() - // run the NextInflationRate function b.N times - for n := 0; n < b.N; n++ { - minter.NextInflation(params, sdk.NewDecWithPrec(66, 2)) - } -} - -// // Next annual provisions benchmarking -// // BenchmarkNextAnnualProvisions-4 5000000 251 ns/op. -func BenchmarkNextAnnualProvisions(b *testing.B) { - b.ReportAllocs() - minter := InitialMinter(sdk.NewDecWithPrec(1, 1)) params := DefaultParams() + bondedRatio := sdk.NewDecWithPrec(66, 2) totalSupply := sdk.NewInt(100000000000000) - // run the NextAnnualProvisions function b.N times + // run the NextInflationRate function b.N times for n := 0; n < b.N; n++ { - minter.NextAnnualProvisions(params, totalSupply) + _, err := NewMinterWithInflationCoef(params.InflationCoef, bondedRatio, totalSupply) + if err != nil { + panic(err) + } } } diff --git a/x/mint/types/params.go b/x/mint/types/params.go index 8cd28e22..933cab09 100644 --- a/x/mint/types/params.go +++ b/x/mint/types/params.go @@ -10,30 +10,27 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) +// NewParams creates a new Params object. func NewParams( - mintDenom string, inflationCoef, bondingAdjustment, targetBondingRatio sdk.Dec, blocksPerYear uint64, + mintDenom string, inflationCoef sdk.Dec, blocksPerYear uint64, ) Params { return Params{ - MintDenom: mintDenom, - InflationCoef: inflationCoef, - BondingAdjustment: bondingAdjustment, - TargetBondingRatio: targetBondingRatio, - BlocksPerYear: blocksPerYear, + MintDenom: mintDenom, + InflationCoef: inflationCoef, + BlocksPerYear: blocksPerYear, } } -// default minting module parameters. +// DefaultParams returns a default set of parameters. func DefaultParams() Params { return Params{ - MintDenom: sdk.DefaultBondDenom, - InflationCoef: sdk.NewDecWithPrec(73, 3), - BondingAdjustment: sdk.NewDecWithPrec(25, 1), - TargetBondingRatio: sdk.NewDecWithPrec(66, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), // assuming 5-second block times + MintDenom: sdk.DefaultBondDenom, + InflationCoef: sdk.NewDecWithPrec(3, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), // assuming 5-second block times } } -// validate params. +// Validate is used for validating the params. func (p Params) Validate() error { if err := validateMintDenom(p.MintDenom); err != nil { return err @@ -41,12 +38,6 @@ func (p Params) Validate() error { if err := validateInflationCoef(p.InflationCoef); err != nil { return err } - if err := validateBondingAdjustment(p.BondingAdjustment); err != nil { - return err - } - if err := validateTargetBondingRatio(p.TargetBondingRatio); err != nil { - return err - } return validateBlocksPerYear(p.BlocksPerYear) } @@ -80,41 +71,14 @@ func validateInflationCoef(i interface{}) error { return fmt.Errorf("inflation coefficient cannot be negative: %s", v) } if v.GT(sdk.OneDec()) { + // while there's no theoretical limit to the inflation rate, a coefficient of + // 1 or more would lead to hyper-hyperinflation. return fmt.Errorf("inflation coefficient too large: %s", v) } return nil } -func validateBondingAdjustment(i interface{}) error { - v, ok := i.(sdk.Dec) - if !ok { - return fmt.Errorf("invalid parameter type: %T", i) - } - - if v.IsNegative() { - return fmt.Errorf("inflation coefficient cannot be negative: %s", v) - } - - return nil -} - -func validateTargetBondingRatio(i interface{}) error { - v, ok := i.(sdk.Dec) - if !ok { - return fmt.Errorf("invalid parameter type: %T", i) - } - - if v.IsNegative() { - return fmt.Errorf("target bonding ratio cannot be negative: %s", v) - } - if v.GT(sdk.OneDec()) { - return fmt.Errorf("target bonding ratio too large: %s", v) - } - - return nil -} - func validateBlocksPerYear(i interface{}) error { v, ok := i.(uint64) if !ok { From 9939b5a0ee62271aca8a7469db1f5456f8344fa1 Mon Sep 17 00:00:00 2001 From: ccamel Date: Mon, 4 Dec 2023 17:25:27 +0100 Subject: [PATCH 22/24] test(mint): enhance tests for the token model v2 --- x/mint/client/cli/query_test.go | 6 ++-- x/mint/keeper/genesis_test.go | 2 -- x/mint/keeper/keeper_test.go | 38 ++++--------------------- x/mint/keeper/msg_server_test.go | 44 ++++------------------------- x/mint/simulation/genesis_test.go | 18 +++++------- x/mint/simulation/proposals_test.go | 4 +-- 6 files changed, 22 insertions(+), 90 deletions(-) diff --git a/x/mint/client/cli/query_test.go b/x/mint/client/cli/query_test.go index 9e9a7620..c30df331 100644 --- a/x/mint/client/cli/query_test.go +++ b/x/mint/client/cli/query_test.go @@ -46,17 +46,15 @@ func TestGetCmdQueryParams(t *testing.T) { "json output", []string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", flags.FlagOutput)}, `[--height=1 --output=json]`, - `{"mint_denom":"","inflation_coef":"0","bonding_adjustment":"0","target_bonding_ratio":"0","blocks_per_year":"0"}`, + `{"mint_denom":"","inflation_coef":"0","blocks_per_year":"0"}`, }, { "text output", []string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", flags.FlagOutput)}, `[--height=1 --output=text]`, `blocks_per_year: "0" -bonding_adjustment: "0" inflation_coef: "0" -mint_denom: "" -target_bonding_ratio: "0"`, +mint_denom: ""`, }, } diff --git a/x/mint/keeper/genesis_test.go b/x/mint/keeper/genesis_test.go index cde34e40..959484a9 100644 --- a/x/mint/keeper/genesis_test.go +++ b/x/mint/keeper/genesis_test.go @@ -62,8 +62,6 @@ func (s *GenesisTestSuite) TestImportExportGenesis() { genesisState.Params = types.NewParams( "testDenom", sdk.NewDecWithPrec(69, 2), - sdk.NewDecWithPrec(14, 2), - sdk.NewDecWithPrec(56, 2), uint64(60*60*8766/5), ) diff --git a/x/mint/keeper/keeper_test.go b/x/mint/keeper/keeper_test.go index 40043068..4623bc10 100644 --- a/x/mint/keeper/keeper_test.go +++ b/x/mint/keeper/keeper_test.go @@ -77,44 +77,18 @@ func (s *IntegrationTestSuite) TestParams() { { name: "set invalid params", input: types.Params{ - MintDenom: sdk.DefaultBondDenom, - BondingAdjustment: sdk.NewDecWithPrec(-13, 2), - TargetBondingRatio: sdk.NewDecWithPrec(25, 2), - InflationCoef: sdk.NewDecWithPrec(73, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), - }, - expectErr: true, - }, - { - name: "set invalid params", - input: types.Params{ - MintDenom: sdk.DefaultBondDenom, - BondingAdjustment: sdk.NewDecWithPrec(13, 2), - TargetBondingRatio: sdk.NewDecWithPrec(-25, 2), - InflationCoef: sdk.NewDecWithPrec(73, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), - }, - expectErr: true, - }, - { - name: "set invalid params", - input: types.Params{ - MintDenom: sdk.DefaultBondDenom, - BondingAdjustment: sdk.NewDecWithPrec(13, 2), - TargetBondingRatio: sdk.NewDecWithPrec(25, 2), - InflationCoef: sdk.NewDecWithPrec(-73, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), + MintDenom: sdk.DefaultBondDenom, + InflationCoef: sdk.NewDecWithPrec(-73, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), }, expectErr: true, }, { name: "set full valid params", input: types.Params{ - MintDenom: sdk.DefaultBondDenom, - BondingAdjustment: sdk.NewDecWithPrec(13, 2), - TargetBondingRatio: sdk.NewDecWithPrec(25, 2), - InflationCoef: sdk.NewDecWithPrec(73, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), + MintDenom: sdk.DefaultBondDenom, + InflationCoef: sdk.NewDecWithPrec(73, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), }, expectErr: false, }, diff --git a/x/mint/keeper/msg_server_test.go b/x/mint/keeper/msg_server_test.go index 5e37463a..ac18821b 100644 --- a/x/mint/keeper/msg_server_test.go +++ b/x/mint/keeper/msg_server_test.go @@ -19,44 +19,14 @@ func (s *IntegrationTestSuite) TestUpdateParams() { }, expectErr: true, }, - { - name: "set invalid params for bonding adjustment (negative value)", - request: &types.MsgUpdateParams{ - Authority: s.mintKeeper.GetAuthority(), - Params: types.Params{ - MintDenom: sdk.DefaultBondDenom, - BondingAdjustment: sdk.NewDecWithPrec(-13, 2), - TargetBondingRatio: sdk.NewDecWithPrec(25, 2), - InflationCoef: sdk.NewDecWithPrec(73, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), - }, - }, - expectErr: true, - }, - { - name: "set invalid params for target bonding ratio (negative value)", - request: &types.MsgUpdateParams{ - Authority: s.mintKeeper.GetAuthority(), - Params: types.Params{ - MintDenom: sdk.DefaultBondDenom, - BondingAdjustment: sdk.NewDecWithPrec(13, 2), - TargetBondingRatio: sdk.NewDecWithPrec(-25, 2), - InflationCoef: sdk.NewDecWithPrec(73, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), - }, - }, - expectErr: true, - }, { name: "set invalid params for inflation coef (negative value)", request: &types.MsgUpdateParams{ Authority: s.mintKeeper.GetAuthority(), Params: types.Params{ - MintDenom: sdk.DefaultBondDenom, - BondingAdjustment: sdk.NewDecWithPrec(13, 2), - TargetBondingRatio: sdk.NewDecWithPrec(25, 2), - InflationCoef: sdk.NewDecWithPrec(-73, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), + MintDenom: sdk.DefaultBondDenom, + InflationCoef: sdk.NewDecWithPrec(-73, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), }, }, expectErr: true, @@ -66,11 +36,9 @@ func (s *IntegrationTestSuite) TestUpdateParams() { request: &types.MsgUpdateParams{ Authority: s.mintKeeper.GetAuthority(), Params: types.Params{ - MintDenom: sdk.DefaultBondDenom, - BondingAdjustment: sdk.NewDecWithPrec(13, 2), - TargetBondingRatio: sdk.NewDecWithPrec(25, 2), - InflationCoef: sdk.NewDecWithPrec(73, 2), - BlocksPerYear: uint64(60 * 60 * 8766 / 5), + MintDenom: sdk.DefaultBondDenom, + InflationCoef: sdk.NewDecWithPrec(73, 2), + BlocksPerYear: uint64(60 * 60 * 8766 / 5), }, }, expectErr: false, diff --git a/x/mint/simulation/genesis_test.go b/x/mint/simulation/genesis_test.go index 0c75c299..6fcdf4f9 100644 --- a/x/mint/simulation/genesis_test.go +++ b/x/mint/simulation/genesis_test.go @@ -44,22 +44,18 @@ func TestRandomizedGenState(t *testing.T) { var mintGenesis types.GenesisState simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &mintGenesis) - dec1, _ := sdk.NewDecFromStr("0.073000000000000000") - dec2, _ := sdk.NewDecFromStr("2.500000000000000000") - dec3, _ := sdk.NewDecFromStr("0.660000000000000000") + inflationCoef := sdk.NewDecWithPrec(3, 2) + bondedRatio := sdk.NewDecWithPrec(2, 1) + minter, _ := types.NewMinterWithInflationCoef(inflationCoef, bondedRatio, simState.InitialStake) require.Equal(t, uint64(6311520), mintGenesis.Params.BlocksPerYear) - require.Equal(t, dec1, mintGenesis.Params.InflationCoef) - require.Equal(t, dec2, mintGenesis.Params.BondingAdjustment) - require.Equal(t, dec3, mintGenesis.Params.TargetBondingRatio) + require.Equal(t, "0.073000000000000000", mintGenesis.Params.InflationCoef.String()) require.Equal(t, "stake", mintGenesis.Params.MintDenom) - require.Equal(t, "0stake", - mintGenesis.Minter.BlockProvision(mintGenesis.Params).String()) - require.Equal(t, "170.000000000000000000", - mintGenesis.Minter.NextAnnualProvisions(mintGenesis.Params, simState.InitialStake).String()) - require.Equal(t, "0.109500000000000000", mintGenesis.Minter.NextInflation(mintGenesis.Params, sdk.NewDecWithPrec(66, 2)).String()) + require.Equal(t, "0stake", mintGenesis.Minter.BlockProvision(mintGenesis.Params).String()) require.Equal(t, "0.170000000000000000", mintGenesis.Minter.Inflation.String()) require.Equal(t, "170.000000000000000000", mintGenesis.Minter.AnnualProvisions.String()) + require.Equal(t, "0.150000000000000000", minter.Inflation.String()) + require.Equal(t, "150.000000000000000000", minter.AnnualProvisions.String()) } // TestRandomizedGenState tests abnormal scenarios of applying RandomizedGenState. diff --git a/x/mint/simulation/proposals_test.go b/x/mint/simulation/proposals_test.go index 04f67274..7e1a2176 100644 --- a/x/mint/simulation/proposals_test.go +++ b/x/mint/simulation/proposals_test.go @@ -40,8 +40,6 @@ func TestProposalMsgs(t *testing.T) { assert.Equal(t, sdk.AccAddress(address.Module("gov")).String(), msgUpdateParams.Authority) assert.Equal(t, uint64(122877), msgUpdateParams.Params.BlocksPerYear) - assert.DeepEqual(t, sdk.NewDecWithPrec(7, 2), msgUpdateParams.Params.TargetBondingRatio) assert.DeepEqual(t, sdk.NewDecWithPrec(95, 2), msgUpdateParams.Params.InflationCoef) - assert.DeepEqual(t, sdk.NewDecWithPrec(6, 1), msgUpdateParams.Params.BondingAdjustment) - assert.Equal(t, "xKGLwQvuyN", msgUpdateParams.Params.MintDenom) + assert.Equal(t, "eAerqyNEUz", msgUpdateParams.Params.MintDenom) } From d43a9a7aa2b3c72ff8ba3b3140a4288641296698 Mon Sep 17 00:00:00 2001 From: ccamel Date: Tue, 5 Dec 2023 17:25:00 +0100 Subject: [PATCH 23/24] docs(mint): generate documentation --- docs/proto/mint.md | 65 +++++++++-------------------------- proto/mint/docs.yaml | 42 ++++++---------------- proto/mint/v1beta1/mint.proto | 22 +++++------- x/mint/types/mint.pb.go | 21 +++++------ 4 files changed, 44 insertions(+), 106 deletions(-) diff --git a/docs/proto/mint.md b/docs/proto/mint.md index bc9616f0..c28ba227 100644 --- a/docs/proto/mint.md +++ b/docs/proto/mint.md @@ -4,43 +4,21 @@ -## Purpose +## Function -The Minting Module is a fundamental component within the blockchain ecosystem, responsible for managing the dynamic -issuance of tokens to validators, thereby ensuring the stability and sustainability of the network. -This module operates on a continuous basis, recalculating parameters with each new block to maintain an optimal -token supply. +The Minting Module plays a crucial role in the blockchain ecosystem, tasked with regulating the issuance of tokens to +validators. This ensures the network's ongoing stability and viability. The module operates consistently, adjusting +key parameters with each block to maintain a balanced token supply. -## 🧮 Calculation +## Calculation of Inflation -The formula for calculating the yearly staking reward target (srty) is as follows: +The method for determining the inflation rate is outlined in the +[OKP4 Whitepaper - Token Model](https://docs.okp4.network/whitepaper/token-model). -``` -srty = a • i • (c - b / bt) -``` +## Per-Block Token Generation -- `srty`: Represents the yearly staking reward target. -- `a`: Denotes the total token supply within the blockchain. -- `i`: Refers to the inflation coefficient, a configurable parameter set at 0.073 by default. -- `c`: Signifies the bonding ratio adjustment coefficient. -- `b`: A dynamic variable ranging from 0 to 1, symbolizing the current bonding ratio at the current block. -- `bt`: Represents the target bonding ratio, conventionally set to 0.66. - -### Yearly Inflation Rate - -Upon the determination of srty, the Minting Module proceeds to convert it into a yearly inflation rate -expressed as a percentage (Iy): - -``` -Iy = (srty • 100) / a = (a • i • (c - b / bt) • 100) / a = i • (c - b / bt) • 100 -``` - -- `Iy` (in percentage): Represents the yearly inflation rate. - -### Block-Level Token Minting - -With the yearly inflation rate in hand, the Minting Module can calculate the number of tokens to be minted for -each individual block by knowing the number of block in one years. +Given the annual inflation rate, the Minting Module calculates the exact amount of tokens to be generated for each +block, based on the total number of blocks in a year. ## Table of Contents @@ -78,33 +56,24 @@ each individual block by knowing the number of block in one years. ### Minter -Minter represents the minting state within the blockchain, tasked with the continuous calculation and distribution -of tokens to validators. - -This calculation occurs with each block, where the minting module dynamically recalculates the annual `inflation` rate. -Using the resulting inflation rate, it deduces the quantity of tokens to be provisioned for the upcoming year. - -Furthermore, based on the current block's position within the year, it computes the exact number of tokens to be -minted for that specific block. +Minter holds the state of minting within the blockchain. | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | -| `inflation` | [string](#string) | | Represents the current annual inflation rate. | -| `annual_provisions` | [string](#string) | | Represents the current annual expected provisions. | +| `inflation` | [string](#string) | | Current annual inflation rate. | +| `annual_provisions` | [string](#string) | | Current annual anticipated provisions. | ### Params -Params holds parameters for the mint module. +Params defines the parameters for the mint module. | Field | Type | Label | Description | | ----- | ---- | ----- | ----------- | -| `mint_denom` | [string](#string) | | Type of coin to mint | -| `inflation_coef` | [string](#string) | | Annual inflation coefficient | -| `bonding_adjustment` | [string](#string) | | Bonding adjustment | -| `target_bonding_ratio` | [string](#string) | | Represent the target bonding ratio to reach | -| `blocks_per_year` | [uint64](#uint64) | | Estimated blocks per year | +| `mint_denom` | [string](#string) | | Denomination of the coin to be minted. | +| `inflation_coef` | [string](#string) | | Annual inflation coefficient, influencing the inflation rate based on the bonded ratio. Values range from 0 to 1, with higher values indicating higher inflation. | +| `blocks_per_year` | [uint64](#uint64) | | Estimated number of blocks per year. | [//]: # (end messages) diff --git a/proto/mint/docs.yaml b/proto/mint/docs.yaml index b256a1dd..bccc0038 100644 --- a/proto/mint/docs.yaml +++ b/proto/mint/docs.yaml @@ -1,38 +1,16 @@ description: | - ## Purpose + ## Function - The Minting Module is a fundamental component within the blockchain ecosystem, responsible for managing the dynamic - issuance of tokens to validators, thereby ensuring the stability and sustainability of the network. - This module operates on a continuous basis, recalculating parameters with each new block to maintain an optimal - token supply. + The Minting Module plays a crucial role in the blockchain ecosystem, tasked with regulating the issuance of tokens to + validators. This ensures the network's ongoing stability and viability. The module operates consistently, adjusting + key parameters with each block to maintain a balanced token supply. - ## 🧮 Calculation + ## Calculation of Inflation - The formula for calculating the yearly staking reward target (srty) is as follows: + The method for determining the inflation rate is outlined in the + [OKP4 Whitepaper - Token Model](https://docs.okp4.network/whitepaper/token-model). - ``` - srty = a • i • (c - b / bt) - ``` + ## Per-Block Token Generation - - `srty`: Represents the yearly staking reward target. - - `a`: Denotes the total token supply within the blockchain. - - `i`: Refers to the inflation coefficient, a configurable parameter set at 0.073 by default. - - `c`: Signifies the bonding ratio adjustment coefficient. - - `b`: A dynamic variable ranging from 0 to 1, symbolizing the current bonding ratio at the current block. - - `bt`: Represents the target bonding ratio, conventionally set to 0.66. - - ### Yearly Inflation Rate - - Upon the determination of srty, the Minting Module proceeds to convert it into a yearly inflation rate - expressed as a percentage (Iy): - - ``` - Iy = (srty • 100) / a = (a • i • (c - b / bt) • 100) / a = i • (c - b / bt) • 100 - ``` - - - `Iy` (in percentage): Represents the yearly inflation rate. - - ### Block-Level Token Minting - - With the yearly inflation rate in hand, the Minting Module can calculate the number of tokens to be minted for - each individual block by knowing the number of block in one years. + Given the annual inflation rate, the Minting Module calculates the exact amount of tokens to be generated for each + block, based on the total number of blocks in a year. diff --git a/proto/mint/v1beta1/mint.proto b/proto/mint/v1beta1/mint.proto index 05e74f0d..fbd24248 100644 --- a/proto/mint/v1beta1/mint.proto +++ b/proto/mint/v1beta1/mint.proto @@ -7,21 +7,15 @@ import "gogoproto/gogo.proto"; option go_package = "github.com/okp4/okp4d/x/mint/types"; -// Minter represents the minting state within the blockchain. -// -// At each block, the minting module recalculates the annual inflation rate dynamically. It then determines the total -// amount of tokens to be provisioned for the upcoming year based on this rate. -// -// Additionally, the module computes the precise number of tokens to be minted for the current block, taking into account -// its position within the annual cycle. +// Minter holds the state of minting within the blockchain. message Minter { - // Represents the current annual inflation rate. + // Current annual inflation rate. string inflation = 1 [ (cosmos_proto.scalar) = "cosmos.Dec", (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false ]; - // Represents the current annual expected provisions. + // Current annual anticipated provisions. string annual_provisions = 2 [ (cosmos_proto.scalar) = "cosmos.Dec", (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", @@ -29,18 +23,20 @@ message Minter { ]; } -// Params holds parameters for the mint module. +// Params defines the parameters for the mint module. message Params { option (gogoproto.goproto_stringer) = false; - // Type of coin to mint + // Denomination of the coin to be minted. string mint_denom = 1; - // Annual inflation coefficient + + // Annual inflation coefficient, influencing the inflation rate based on the bonded ratio. + // Values range from 0 to 1, with higher values indicating higher inflation. string inflation_coef = 2 [ (cosmos_proto.scalar) = "cosmos.Dec", (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false ]; - // Estimated blocks per year + // Estimated number of blocks per year. uint64 blocks_per_year = 3; } diff --git a/x/mint/types/mint.pb.go b/x/mint/types/mint.pb.go index c4176ab3..f8625cab 100644 --- a/x/mint/types/mint.pb.go +++ b/x/mint/types/mint.pb.go @@ -25,17 +25,11 @@ var _ = math.Inf // proto package needs to be updated. const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package -// Minter represents the minting state within the blockchain. -// -// At each block, the minting module recalculates the annual inflation rate dynamically. It then determines the total -// amount of tokens to be provisioned for the upcoming year based on this rate. -// -// Additionally, the module computes the precise number of tokens to be minted for the current block, taking into account -// its position within the annual cycle. +// Minter holds the state of minting within the blockchain. type Minter struct { - // Represents the current annual inflation rate. + // Current annual inflation rate. Inflation github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,1,opt,name=inflation,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"inflation"` - // Represents the current annual expected provisions. + // Current annual anticipated provisions. AnnualProvisions github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,2,opt,name=annual_provisions,json=annualProvisions,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"annual_provisions"` } @@ -72,13 +66,14 @@ func (m *Minter) XXX_DiscardUnknown() { var xxx_messageInfo_Minter proto.InternalMessageInfo -// Params holds parameters for the mint module. +// Params defines the parameters for the mint module. type Params struct { - // Type of coin to mint + // Denomination of the coin to be minted. MintDenom string `protobuf:"bytes,1,opt,name=mint_denom,json=mintDenom,proto3" json:"mint_denom,omitempty"` - // Annual inflation coefficient + // Annual inflation coefficient, influencing the inflation rate based on the bonded ratio. + // Values range from 0 to 1, with higher values indicating higher inflation. InflationCoef github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,2,opt,name=inflation_coef,json=inflationCoef,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"inflation_coef"` - // Estimated blocks per year + // Estimated number of blocks per year. BlocksPerYear uint64 `protobuf:"varint,3,opt,name=blocks_per_year,json=blocksPerYear,proto3" json:"blocks_per_year,omitempty"` } From b87aa451c9d022a95c2dc1d655b04781cd162772 Mon Sep 17 00:00:00 2001 From: Chris Date: Mon, 11 Dec 2023 10:53:27 +0100 Subject: [PATCH 24/24] docs: fix typo Co-authored-by: Arnaud Mimart <33665250+amimart@users.noreply.github.com> --- x/mint/types/minter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/mint/types/minter.go b/x/mint/types/minter.go index b2c186fa..17995f8f 100644 --- a/x/mint/types/minter.go +++ b/x/mint/types/minter.go @@ -9,7 +9,7 @@ import ( ) // NewMinter returns a new Minter object with the given inflation, annual -// provisions values and annual reduction factor. +// provisions values. func NewMinter(inflation, annualProvisions sdk.Dec) Minter { return Minter{ Inflation: inflation,