lower rounding
This commit is contained in:
parent
df46339a45
commit
accce5c7d8
|
@ -39,11 +39,11 @@ func TestGetInflation(t *testing.T) {
|
||||||
|
|
||||||
// test 7% minimum stop (testing with 100% bonded)
|
// test 7% minimum stop (testing with 100% bonded)
|
||||||
{"test 4", sdk.OneRat(), sdk.ZeroRat(), sdk.NewRat(7, 100), sdk.ZeroRat()},
|
{"test 4", sdk.OneRat(), sdk.ZeroRat(), sdk.NewRat(7, 100), sdk.ZeroRat()},
|
||||||
{"test 5", sdk.OneRat(), sdk.ZeroRat(), sdk.NewRat(70001, 1000000), sdk.NewRat(-1, 1000000).Round(precision)},
|
{"test 5", sdk.OneRat(), sdk.ZeroRat(), sdk.NewRat(70001, 1000000), sdk.NewRat(-1, 1000000)},
|
||||||
|
|
||||||
// test 20% maximum stop (testing with 0% bonded)
|
// test 20% maximum stop (testing with 0% bonded)
|
||||||
{"test 6", sdk.ZeroRat(), sdk.ZeroRat(), sdk.NewRat(20, 100), sdk.ZeroRat()},
|
{"test 6", sdk.ZeroRat(), sdk.ZeroRat(), sdk.NewRat(20, 100), sdk.ZeroRat()},
|
||||||
{"test 7", sdk.ZeroRat(), sdk.ZeroRat(), sdk.NewRat(199999, 1000000), sdk.NewRat(1, 1000000).Round(precision)},
|
{"test 7", sdk.ZeroRat(), sdk.ZeroRat(), sdk.NewRat(199999, 1000000), sdk.NewRat(1, 1000000)},
|
||||||
|
|
||||||
// perfect balance shouldn't change inflation
|
// perfect balance shouldn't change inflation
|
||||||
{"test 8", sdk.NewRat(67), sdk.NewRat(33), sdk.NewRat(15, 100), sdk.ZeroRat()},
|
{"test 8", sdk.NewRat(67), sdk.NewRat(33), sdk.NewRat(15, 100), sdk.ZeroRat()},
|
||||||
|
@ -82,41 +82,6 @@ func TestProcessProvisions(t *testing.T) {
|
||||||
checkFinalPoolValues(t, pool, sdk.NewRat(initialTotalTokens), cumulativeExpProvs)
|
checkFinalPoolValues(t, pool, sdk.NewRat(initialTotalTokens), cumulativeExpProvs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Benchmark precision
|
|
||||||
func BenchmarkProcessProvisions10000(b *testing.B) {
|
|
||||||
precision = 10000
|
|
||||||
pool := InitialPool()
|
|
||||||
params := DefaultParams()
|
|
||||||
|
|
||||||
var initialTotalTokens int64 = 550000000
|
|
||||||
var cumulativeExpProvs = sdk.ZeroRat()
|
|
||||||
pool.LooseTokens = sdk.NewRat(initialTotalTokens)
|
|
||||||
|
|
||||||
// process the provisions for a year
|
|
||||||
for hr := 0; hr < b.N; hr++ {
|
|
||||||
var expProvisions sdk.Rat
|
|
||||||
_, expProvisions, pool, _ = updateProvisionsBare(pool, params, hr)
|
|
||||||
cumulativeExpProvs = cumulativeExpProvs.Add(expProvisions)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkProcessProvisions100000000000(b *testing.B) {
|
|
||||||
precision = 100000000000
|
|
||||||
pool := InitialPool()
|
|
||||||
params := DefaultParams()
|
|
||||||
|
|
||||||
var initialTotalTokens int64 = 550000000
|
|
||||||
var cumulativeExpProvs = sdk.ZeroRat()
|
|
||||||
pool.LooseTokens = sdk.NewRat(initialTotalTokens)
|
|
||||||
|
|
||||||
// process the provisions for a year
|
|
||||||
for hr := 0; hr < b.N; hr++ {
|
|
||||||
var expProvisions sdk.Rat
|
|
||||||
_, expProvisions, pool, _ = updateProvisionsBare(pool, params, hr)
|
|
||||||
cumulativeExpProvs = cumulativeExpProvs.Add(expProvisions)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//_________________________________________________________________________________________
|
//_________________________________________________________________________________________
|
||||||
////////////////////////////////HELPER FUNCTIONS BELOW/////////////////////////////////////
|
////////////////////////////////HELPER FUNCTIONS BELOW/////////////////////////////////////
|
||||||
|
|
||||||
|
@ -129,7 +94,11 @@ func checkFinalPoolValues(t *testing.T, pool Pool, initialTotalTokens, cumulativ
|
||||||
// Processes provisions are added to the pool correctly every hour
|
// Processes provisions are added to the pool correctly every hour
|
||||||
// Returns expected Provisions, expected Inflation, and pool, to help with cumulative calculations back in main Tests
|
// Returns expected Provisions, expected Inflation, and pool, to help with cumulative calculations back in main Tests
|
||||||
func updateProvisions(t *testing.T, pool Pool, params Params, hr int) (sdk.Rat, sdk.Rat, Pool) {
|
func updateProvisions(t *testing.T, pool Pool, params Params, hr int) (sdk.Rat, sdk.Rat, Pool) {
|
||||||
expInflation, expProvisions, pool, startTotalSupply := updateProvisionsBare(pool, params, hr)
|
|
||||||
|
expInflation := pool.NextInflation(params)
|
||||||
|
expProvisions := expInflation.Mul(pool.TokenSupply().Round(precision)).Quo(hrsPerYrRat)
|
||||||
|
startTotalSupply := pool.TokenSupply()
|
||||||
|
pool = pool.ProcessProvisions(params)
|
||||||
|
|
||||||
//check provisions were added to pool
|
//check provisions were added to pool
|
||||||
require.True(sdk.RatEq(t, startTotalSupply.Add(expProvisions), pool.TokenSupply()))
|
require.True(sdk.RatEq(t, startTotalSupply.Add(expProvisions), pool.TokenSupply()))
|
||||||
|
@ -137,14 +106,6 @@ func updateProvisions(t *testing.T, pool Pool, params Params, hr int) (sdk.Rat,
|
||||||
return expInflation, expProvisions, pool
|
return expInflation, expProvisions, pool
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateProvisionsBare(pool Pool, params Params, hr int) (sdk.Rat, sdk.Rat, Pool, sdk.Rat) {
|
|
||||||
expInflation := pool.NextInflation(params)
|
|
||||||
expProvisions := expInflation.Mul(pool.TokenSupply()).Quo(hrsPerYrRat)
|
|
||||||
startTotalSupply := pool.TokenSupply()
|
|
||||||
pool = pool.ProcessProvisions(params)
|
|
||||||
return expInflation, expProvisions, pool, startTotalSupply
|
|
||||||
}
|
|
||||||
|
|
||||||
// Checks that The inflation will correctly increase or decrease after an update to the pool
|
// Checks that The inflation will correctly increase or decrease after an update to the pool
|
||||||
// nolint: gocyclo
|
// nolint: gocyclo
|
||||||
func checkInflation(t *testing.T, pool Pool, previousInflation, updatedInflation sdk.Rat, msg string) {
|
func checkInflation(t *testing.T, pool Pool, previousInflation, updatedInflation sdk.Rat, msg string) {
|
||||||
|
|
|
@ -80,13 +80,15 @@ func (p Pool) bondedTokensToLoose(bondedTokens sdk.Rat) Pool {
|
||||||
//_______________________________________________________________________
|
//_______________________________________________________________________
|
||||||
// Inflation
|
// Inflation
|
||||||
|
|
||||||
var precision int64 = 100000000000 // increased to this precision for accuracy
|
const precision = 10000 // increased to this precision for accuracy
|
||||||
var hrsPerYrRat = sdk.NewRat(8766) // as defined by a julian year of 365.25 days
|
var hrsPerYrRat = sdk.NewRat(8766) // as defined by a julian year of 365.25 days
|
||||||
|
|
||||||
// process provisions for an hour period
|
// process provisions for an hour period
|
||||||
func (p Pool) ProcessProvisions(params Params) Pool {
|
func (p Pool) ProcessProvisions(params Params) Pool {
|
||||||
p.Inflation = p.NextInflation(params)
|
p.Inflation = p.NextInflation(params)
|
||||||
provisions := p.Inflation.Mul(p.TokenSupply()).Quo(hrsPerYrRat)
|
provisions := p.Inflation.
|
||||||
|
Mul(p.TokenSupply().Round(precision)).
|
||||||
|
Quo(hrsPerYrRat)
|
||||||
|
|
||||||
// TODO add to the fees provisions
|
// TODO add to the fees provisions
|
||||||
p.LooseTokens = p.LooseTokens.Add(provisions)
|
p.LooseTokens = p.LooseTokens.Add(provisions)
|
||||||
|
@ -103,7 +105,10 @@ func (p Pool) NextInflation(params Params) (inflation sdk.Rat) {
|
||||||
// 7% and 20%.
|
// 7% and 20%.
|
||||||
|
|
||||||
// (1 - bondedRatio/GoalBonded) * InflationRateChange
|
// (1 - bondedRatio/GoalBonded) * InflationRateChange
|
||||||
inflationRateChangePerYear := sdk.OneRat().Sub(p.BondedRatio().Quo(params.GoalBonded)).Mul(params.InflationRateChange)
|
inflationRateChangePerYear := sdk.OneRat().
|
||||||
|
Sub(p.BondedRatio().Round(precision).
|
||||||
|
Quo(params.GoalBonded)).
|
||||||
|
Mul(params.InflationRateChange)
|
||||||
inflationRateChange := inflationRateChangePerYear.Quo(hrsPerYrRat)
|
inflationRateChange := inflationRateChangePerYear.Quo(hrsPerYrRat)
|
||||||
|
|
||||||
// increase the new annual inflation for this next cycle
|
// increase the new annual inflation for this next cycle
|
||||||
|
|
Loading…
Reference in New Issue