Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Lockwarr committed Oct 3, 2024
1 parent 57c4265 commit f72665f
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 222 deletions.
2 changes: 2 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ linters:
- goimports
- misspell
- whitespace
- unused
- ineffassign

output:
# Sort results by: filepath, line and column.
Expand Down
8 changes: 4 additions & 4 deletions proto/buf.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ deps:
- remote: buf.build
owner: cosmos
repository: ibc
commit: d7191877c70143938cf411467cebf39a
digest: shake256:b397f418dc751326dd35100c5200c56e1fd7fd24746e39f3ae4d6c86b175e48fe12207867f0912eb31e98f695e9a8afdec7cb4076502d3998026501a88641a9d
commit: 41fbc441e4d645119d275150719c5441
digest: shake256:a61d3e152909abddc91dcc1aec8ac997007bebd48a7921bc243681c708e6c9068f6484aa7ed4e6ff60101d3422c98744a4a5294bbda6aacdba07f1dfa4291020
- remote: buf.build
owner: cosmos
repository: ics23
Expand All @@ -29,5 +29,5 @@ deps:
- remote: buf.build
owner: googleapis
repository: googleapis
commit: f0e53af8f2fc4556b94f482688b57223
digest: shake256:de26a277fc28b8b411ecf58729d78d32fcf15090ffd998a4469225b17889bfb51442eaab04bb7a8d88d203ecdf0a9febd4ffd52c18ed1c2229160c7bd353ca95
commit: e7f8d366f5264595bcc4cd4139af9973
digest: shake256:e5e5f1c12f82e028ea696faa43b4f9dc6258a6d1226282962a8c8b282e10946281d815884f574bd279ebd9cd7588629beb3db17b892af6c33b56f92f8f67f509
45 changes: 21 additions & 24 deletions x/mint/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ import (
)

var (
normInitialTotal = types.CalcTokensByIntegral(types.NormOffset)
// normInitialTotal = types.CalcTokensByIntegral(types.Offset)
nanoSecondsInMonth = sdkmath.LegacyNewDec(time.Hour.Nanoseconds() * 24 * 30)
twelveMonths = sdkmath.LegacyMustNewDecFromStr("12.0")

errTimeInFutureBeforeTimePassed = errors.New("time in future can not be before passed time")
errNegativeBlockTime = errors.New("block time can not be less then zero")
)

func calcFunctionIncrement(nanoSecondsPassed sdkmath.Uint) sdkmath.LegacyDec {
return types.NormMonthsRange.Mul(calcFixedIncrement(nanoSecondsPassed))
}
// func calcFractionOfMonthsRange(nanoSecondsPassed sdkmath.Uint) sdkmath.LegacyDec {
// return types.MonthsRange.Mul(calcFractionOfMonth(nanoSecondsPassed))
// }

func calcFixedIncrement(nanoSecondsPassed sdkmath.Uint) sdkmath.LegacyDec {
func calcFractionOfMonth(nanoSecondsPassed sdkmath.Uint) sdkmath.LegacyDec {
return types.DecFromUint(nanoSecondsPassed).Quo(nanoSecondsInMonth)
}

Expand All @@ -53,61 +53,58 @@ func calcTokens(blockTime sdkmath.Uint, minter *types.Minter, maxMintableSeconds
}

nsecPassed := calcTimeDifference(blockTime, minter.PrevBlockTimestamp, maxMintableSeconds)
if minter.NormTimePassed.LT(types.MonthsInFormula) {
newTime := minter.NormTimePassed.Add(calcFractionOfMonth(nsecPassed))
if newTime.LT(types.MonthsInFormula) {
// First 120 months follow the minting formula
// As the integral starts from NormOffset (ie > 0), previous total needs to be incremented by predetermined amount
previousTotal := minter.TotalMinted.Add(normInitialTotal)
newNormTime := minter.NormTimePassed.Add(calcFunctionIncrement(nsecPassed))
nextTotal := types.CalcTokensByIntegral(newNormTime)

delta := nextTotal.Sub(previousTotal)
nextTotal := types.CalcTokensByIntegral(newTime)
delta := nextTotal.Sub(minter.TotalMinted)

return updateMinter(minter, blockTime, newNormTime, delta)
return updateMinter(minter, blockTime, newTime, delta)
} else {
// After 120 months, we don't mint any more tokens.
return sdkmath.ZeroUint()
}
}

func updateMinter(minter *types.Minter, blockTime sdkmath.Uint, newNormTime sdkmath.LegacyDec, newlyMinted sdkmath.Uint) sdkmath.Uint {
func updateMinter(minter *types.Minter, blockTime sdkmath.Uint, newTimePassed sdkmath.LegacyDec, newlyMinted sdkmath.Uint) sdkmath.Uint {
if newlyMinted.LT(sdkmath.ZeroUint()) {
// Sanity check, should not happen. However, if this were to happen,
// do not update the minter state (primary the previous block timestamp)
// and wait for a new block which should increase the minted amount
return sdkmath.ZeroUint()
}
minter.NormTimePassed = newNormTime
minter.NormTimePassed = newTimePassed
minter.PrevBlockTimestamp = blockTime
minter.TotalMinted = minter.TotalMinted.Add(newlyMinted)
return newlyMinted
}

// Returns the amount of tokens that should be minted by the integral formula
// for the period between normTimePassed and the timeInFuture.
func predictMintedByIntegral(totalMinted sdkmath.Uint, normTimePassed, timeAhead sdkmath.LegacyDec) (sdkmath.Uint, error) {
func predictMintedByIntegral(totalMinted sdkmath.Uint, timePassed, timeAhead sdkmath.LegacyDec) (sdkmath.Uint, error) {
timeAheadNs := timeAhead.Mul(nanoSecondsInMonth).TruncateInt()
normTimeInFuture := normTimePassed.Add(calcFunctionIncrement(sdkmath.Uint(timeAheadNs)))
if normTimePassed.GT(normTimeInFuture) {
timeInFuture := timePassed.Add(calcFractionOfMonth(sdkmath.Uint(timeAheadNs)))
if timePassed.GT(timeInFuture) {
return sdkmath.ZeroUint(), errTimeInFutureBeforeTimePassed
}

if normTimePassed.GTE(types.MonthsInFormula) {
if timePassed.GTE(types.MonthsInFormula) {
return sdkmath.ZeroUint(), nil
}

// integral minting is caped to the 120th month
if normTimeInFuture.GT(types.MonthsInFormula) {
normTimeInFuture = types.MonthsInFormula
if timeInFuture.GT(types.MonthsInFormula) {
timeInFuture = types.MonthsInFormula
}

return types.CalcTokensByIntegral(normTimeInFuture).Sub(normInitialTotal).Sub(totalMinted), nil
return types.CalcTokensByIntegral(timeInFuture).Sub(totalMinted), nil
}

// Returns the amount of tokens that should be minted
// between the NormTimePassed and the timeAhead
// timeAhead expects months represented in decimal form.
func predictTotalMinted(totalMinted sdkmath.Uint, normTimePassed, timeAhead sdkmath.LegacyDec) sdkmath.Uint {
integralAmount, err := predictMintedByIntegral(totalMinted, normTimePassed, timeAhead)
func predictTotalMinted(totalMinted sdkmath.Uint, timePassed, timeAhead sdkmath.LegacyDec) sdkmath.Uint {
integralAmount, err := predictMintedByIntegral(totalMinted, timePassed, timeAhead)
if err != nil {
return sdkmath.ZeroUint()
}
Expand Down
54 changes: 27 additions & 27 deletions x/mint/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
)

var (
expectedCoins60Sec = sdkmath.NewUint(110671242932283)
expectedNormTime20Sec = sdkmath.LegacyMustNewDecFromStr("119.999976884641925729")
expectedCoins60Sec = sdkmath.NewUint(110812991999999)
expectedNormTime20Sec = sdkmath.LegacyMustNewDecFromStr("119.999999999999340800")
normTimeThreshold = sdkmath.LegacyMustNewDecFromStr("0.0001")
fiveMinutesInNano = sdkmath.NewUint(uint64(time.Minute.Nanoseconds() * 5))
expectedTokensInFormula = []int64{
Expand Down Expand Up @@ -144,7 +144,7 @@ func Test_CalcTokensDuringFormula_WhenUsingVaryingIncrements_OutputExpectedToken
timeOffset = timeOffset.Add(i)
}

mintThreshold := sdkmath.NewUint(20_000_000) // 10 tokens
mintThreshold := sdkmath.NewUint(20_000_000) // 20 tokens
fmt.Printf("%v Returned Total, %v Total Minted(in store), %v Norm Time \n", mintedCoins, minter.TotalMinted, minter.NormTimePassed)

if types.GetAbsDiff(expectedCoins60Sec, mintedCoins).GT(mintThreshold) || types.GetAbsDiff(expectedCoins60Sec, sdkmath.Uint(minter.TotalMinted)).GT(mintThreshold) {
Expand Down Expand Up @@ -216,36 +216,36 @@ func Test_CalcTokens_WhenGivenBlockWithDiffBiggerThanMax_MaxMintedTokensAreCreat
require.Equal(t, expectedCoins, coins)
}

func Test_CalcIncrementDuringFormula_OutputsExpectedIncrementWithinEpsilon(t *testing.T) {
increment5s := calcFunctionIncrement(sdkmath.NewUint(uint64(time.Second.Nanoseconds() * 5)))
increment30s := calcFunctionIncrement(sdkmath.NewUint(uint64(time.Second.Nanoseconds() * 30)))
increment60s := calcFunctionIncrement(sdkmath.NewUint(uint64(time.Second.Nanoseconds() * 60)))
// func Test_CalcIncrementDuringFormula_OutputsExpectedIncrementWithinEpsilon(t *testing.T) {
// increment5s := calcFractionOfMonth(sdkmath.NewUint(uint64(time.Second.Nanoseconds() * 5)))
// increment30s := calcFractionOfMonth(sdkmath.NewUint(uint64(time.Second.Nanoseconds() * 30)))
// increment60s := calcFractionOfMonth(sdkmath.NewUint(uint64(time.Second.Nanoseconds() * 60)))

minutesInPeriod := int64(60) * 24 * 30 * types.MonthsInFormula.TruncateInt64()
sumIncrements5s := sdkmath.LegacyNewDec(12 * minutesInPeriod).Mul(increment5s)
sumIncrements30s := sdkmath.LegacyNewDec(2 * minutesInPeriod).Mul(increment30s)
sumIncrements60s := sdkmath.LegacyNewDec(1 * minutesInPeriod).Mul(increment60s)
// minutesInPeriod := int64(60) * 24 * 30 * types.MonthsInFormula.TruncateInt64()
// sumIncrements5s := sdkmath.LegacyNewDec(12 * minutesInPeriod).Mul(increment5s)
// sumIncrements30s := sdkmath.LegacyNewDec(2 * minutesInPeriod).Mul(increment30s)
// sumIncrements60s := sdkmath.LegacyNewDec(1 * minutesInPeriod).Mul(increment60s)

if sumIncrements5s.Sub(types.AbsMonthsRange).Abs().GT(normTimeThreshold) {
t.Errorf("Increment with 5 second step results in range %v, deviating with more than epsilon from expected %v",
sumIncrements5s, types.AbsMonthsRange)
}
// if sumIncrements5s.Sub(types.AbsMonthsRange).Abs().GT(normTimeThreshold) {
// t.Errorf("Increment with 5 second step results in range %v, deviating with more than epsilon from expected %v",
// sumIncrements5s, types.AbsMonthsRange)
// }

if sumIncrements30s.Sub(types.AbsMonthsRange).Abs().GT(normTimeThreshold) {
t.Errorf("Increment with 30 second step results in range %v, deviating with more than epsilon from expected %v",
sumIncrements30s, types.AbsMonthsRange)
}
// if sumIncrements30s.Sub(types.AbsMonthsRange).Abs().GT(normTimeThreshold) {
// t.Errorf("Increment with 30 second step results in range %v, deviating with more than epsilon from expected %v",
// sumIncrements30s, types.AbsMonthsRange)
// }

if sumIncrements60s.Sub(types.AbsMonthsRange).Abs().GT(normTimeThreshold) {
t.Errorf("Increment with 60 second step results in range %v, deviating with more than epsilon from expected %v",
sumIncrements60s, types.AbsMonthsRange)
}
}
// if sumIncrements60s.Sub(types.AbsMonthsRange).Abs().GT(normTimeThreshold) {
// t.Errorf("Increment with 60 second step results in range %v, deviating with more than epsilon from expected %v",
// sumIncrements60s, types.AbsMonthsRange)
// }
// }

func Test_CalcFixedIncrement_OutputsExpectedIncrementWithinEpsilon(t *testing.T) {
increment5s := calcFixedIncrement(sdkmath.NewUint(uint64(time.Second.Nanoseconds() * 5)))
increment30s := calcFixedIncrement(sdkmath.NewUint(uint64(time.Second.Nanoseconds() * 30)))
increment60s := calcFixedIncrement(sdkmath.NewUint(uint64(time.Second.Nanoseconds() * 60)))
increment5s := calcFractionOfMonth(sdkmath.NewUint(uint64(time.Second.Nanoseconds() * 5)))
increment30s := calcFractionOfMonth(sdkmath.NewUint(uint64(time.Second.Nanoseconds() * 30)))
increment60s := calcFractionOfMonth(sdkmath.NewUint(uint64(time.Second.Nanoseconds() * 60)))

minutesInMonth := int64(time.Hour.Minutes()) * 24 * 30
sumIncrements5s := sdkmath.LegacyNewDec(12 * minutesInMonth).Mul(increment5s)
Expand Down
2 changes: 1 addition & 1 deletion x/mint/client/rest/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (s *IntegrationTestSuite) TestQueryGRPC() {
map[string]string{},
&minttypes.QueryMintStateResponse{},
&minttypes.QueryMintStateResponse{
NormTimePassed: minttypes.NormOffset,
NormTimePassed: minttypes.Offset,
TotalMinted: sdkmath.ZeroUint(),
},
},
Expand Down
4 changes: 2 additions & 2 deletions x/mint/client/testutil/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ func (s *IntegrationTestSuite) TestGetCmdQueryMintState() {
{
"json output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
`{"norm_time_passed":"0.170000000000000000","total_minted":"0"}`,
`{"norm_time_passed":"17.0000000000000000","total_minted":"0"}`,
},
{
"text output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", tmcli.OutputFlag)},
`norm_time_passed: "0.170000000000000000"
`norm_time_passed: "17.0000000000000000"
total_minted: "0"`,
},
}
Expand Down
50 changes: 0 additions & 50 deletions x/mint/keeper/querier.go

This file was deleted.

33 changes: 0 additions & 33 deletions x/mint/keeper/querier_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion x/mint/simulation/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestRandomizedGenState(t *testing.T) {

require.Equal(t, sdkmath.NewUint(uint64(time.Second.Nanoseconds()*13)), mintGenesis.Params.MaxMintableNanoseconds)
require.Equal(t, "0", mintGenesis.Minter.TotalMinted.String())
require.Equal(t, "0.170000000000000000", mintGenesis.Minter.NormTimePassed.String())
require.Equal(t, "17.000000000000000000", mintGenesis.Minter.NormTimePassed.String())
require.Equal(t, sdkmath.ZeroUint(), mintGenesis.Minter.PrevBlockTimestamp)
}

Expand Down
37 changes: 13 additions & 24 deletions x/mint/types/minter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ import (
"github.com/Nolus-Protocol/nolus-core/custom/util"
)

// Legacy Minting formula integral-4.33275 x^3 + 952.82456 x^2 - 88567.49981 x + 3.86381×10^6 dx = -1.08319 x^4 + 317.608 x^3 - 44283.7 x^2 + 3863810 x + constant.
// Legacy Minting formula f(x)=-4.33275 x^3 + 944.61206 x^2 - 88567.25194 x + 3.86335×10^6 integrated over 0.47 to 96

// Current Minting formula integral-0.11175 x^3 + 50.82456 x^2 - 1767.49981 x + 0.83381×10^6 dx = -0.0279375 x^4 + 16.9415 x^3 - 883.75 x^2 + 833810 x + constant.
// Current Minting formula f(x)=-0.11175 x^3 + 50.82456 x^2 - 1767.49981 x + 0.83381×10^6 integrated over 17 to 120.
// After reaching month 120, we stop minting new coins.
var (
QuadCoef = sdkmath.LegacyMustNewDecFromStr("-0.0279375")
CubeCoef = sdkmath.LegacyMustNewDecFromStr("16.9415")
SquareCoef = sdkmath.LegacyMustNewDecFromStr("-883.75")
Coef = sdkmath.LegacyMustNewDecFromStr("833810")
FixedMintedAmount = util.ConvertToMicroNolusInt64(103125)
NormOffset = sdkmath.LegacyMustNewDecFromStr("0.17")
TotalMonths = sdkmath.LegacyMustNewDecFromStr("120")
MonthsInFormula = TotalMonths
AbsMonthsRange = MonthsInFormula.Sub(NormOffset)
NormMonthsRange = AbsMonthsRange.Quo(MonthsInFormula)
QuadCoef = sdkmath.LegacyMustNewDecFromStr("-0.0279375")
CubeCoef = sdkmath.LegacyMustNewDecFromStr("16.9415")
SquareCoef = sdkmath.LegacyMustNewDecFromStr("-883.75")
Coef = sdkmath.LegacyMustNewDecFromStr("833810")
Offset = sdkmath.LegacyMustNewDecFromStr("17")
TotalMonths = sdkmath.LegacyMustNewDecFromStr("120")
MonthsInFormula = TotalMonths
// AbsMonthsRange = MonthsInFormula.Sub(Offset)
// MonthsRange = AbsMonthsRange.Quo(MonthsInFormula)
)

// NewMinter returns a new Minter object with the given inflation and annual
Expand All @@ -38,7 +38,7 @@ func NewMinter(normTimePassed sdkmath.LegacyDec, totalMinted, prevBlockTimestamp
// InitialMinter returns an initial Minter object with zero-value parameters.
func InitialMinter() Minter {
return NewMinter(
NormOffset,
Offset,
sdkmath.ZeroUint(),
sdkmath.ZeroUint(),
sdkmath.ZeroUint(),
Expand All @@ -62,20 +62,9 @@ func ValidateMinter(minter Minter) error {
return fmt.Errorf("mint parameter normTimePassed: %v should not be bigger than TotalMonths: %v", minter.NormTimePassed, TotalMonths)
}

calculatedMintedTokens := calcMintedTokens(minter)

if !calculatedMintedTokens.Equal(minter.TotalMinted) {
return fmt.Errorf("minted unexpected amount of tokens for %s months. act: %v, exp: %v",
minter.NormTimePassed, minter.TotalMinted, calculatedMintedTokens)
}

return nil
}

func calcMintedTokens(m Minter) sdkmath.Uint {
return CalcTokensByIntegral(m.NormTimePassed).Sub(CalcTokensByIntegral(NormOffset))
}

// Integral: -0.0279375 x^4 + 16.9415 x^3 - 883.75 x^2 + 833810 x + constant
// transformed to: (((-0.0279375 x + 16.9415) x - 883.75) x + 833810) x.
func CalcTokensByIntegral(x sdkmath.LegacyDec) sdkmath.Uint {
Expand Down
Loading

0 comments on commit f72665f

Please sign in to comment.