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, 0x0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 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, 0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+ 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 {