Skip to content

Commit

Permalink
change treasury keeper to store only indicators for the each epoch (#269
Browse files Browse the repository at this point in the history
)

* change treasury keeper to store only indicators for the epoch data

* records tax-proceeds only for the tax-rate not whole tx.Fee

* add keeper testcode

* change stored indicators from TRL,SR,MR to TR,SR,TSL

* swagger update
  • Loading branch information
yys authored and dokwon committed Nov 17, 2019
1 parent 157d778 commit 864756a
Show file tree
Hide file tree
Showing 32 changed files with 693 additions and 1,326 deletions.
19 changes: 5 additions & 14 deletions app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

core "github.com/terra-project/core/types"
"github.com/terra-project/core/x/oracle"
"github.com/terra-project/core/x/slashing"
"github.com/terra-project/core/x/staking"
Expand Down Expand Up @@ -194,18 +193,10 @@ func (app *TerraApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []s

/* Handle treasury state. */

// clear all historical issuance info
app.treasuryKeeper.ClearHistoricalIssuance(ctx)
// clear all tax proceeds
app.treasuryKeeper.ClearTaxProceeds(ctx)
// clear all indicators
app.treasuryKeeper.ClearTRs(ctx)
app.treasuryKeeper.ClearSRs(ctx)
app.treasuryKeeper.ClearTSLs(ctx)

// left last tax rate
lastTaxRate := app.treasuryKeeper.GetTaxRate(ctx, core.GetEpoch(ctx))
app.treasuryKeeper.ClearTaxRates(ctx)
app.treasuryKeeper.SetTaxRate(ctx, 0, lastTaxRate)

// left last reward weight
lastRewardWeight := app.treasuryKeeper.GetRewardWeight(ctx, core.GetEpoch(ctx))
app.treasuryKeeper.ClearRewardWeights(ctx)
app.treasuryKeeper.SetRewardWeight(ctx, 0, lastRewardWeight)
app.treasuryKeeper.RecordEpochInitialIssuance(ctx)
}
125 changes: 0 additions & 125 deletions client/lcd/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2477,28 +2477,6 @@ paths:
example: "0.05"
500:
description: Internal Server Error
/treasury/tax_rate/{epoch}:
get:
summary: Get tax rate at epoch
tags:
- Treasury
produces:
- application/json
parameters:
- in: path
name: epoch
description: Epoch number
required: true
type: number
responses:
200:
description: OK
schema:
type: number
format: float
example: "0.05"
500:
description: Internal Server Error
/treasury/tax_cap/{denom}:
get:
summary: Get tax cap of the denom
Expand Down Expand Up @@ -2537,51 +2515,6 @@ paths:
description: 5%
500:
description: Internal Server Error
/treasury/reward_weight/{epoch}:
get:
summary: Get reward weight at epoch
tags:
- Treasury
produces:
- application/json
parameters:
- in: path
name: epoch
description: Epoch number
required: true
type: string
responses:
200:
description: OK
schema:
type: number
format: float
example: "0.05"
description: 5%
500:
description: Internal Server Error
/treasury/historical_issuance/{epoch}:
get:
summary: Get total issuance at the epoch
tags:
- Treasury
produces:
- application/json
parameters:
- in: path
name: epoch
description: Epoch number
required: true
type: integer
responses:
200:
description: OK
schema:
type: array
items:
$ref: "#/definitions/Coin"
500:
description: Internal Server Error
/treasury/tax_proceeds:
get:
summary: Get current tax proceeds
Expand All @@ -2598,28 +2531,6 @@ paths:
$ref: "#/definitions/Coin"
500:
description: Internal Server Error
/treasury/tax_proceeds/{epoch}:
get:
summary: Get tax proceeds at epoch
tags:
- Treasury
produces:
- application/json
parameters:
- in: path
name: epoch
description: Epoch number
required: true
type: integer
responses:
200:
description: OK
schema:
type: array
items:
$ref: "#/definitions/Coin"
500:
description: Internal Server Error
/treasury/seigniorage_proceeds:
get:
summary: retrieves the size of the seigniorage pool
Expand All @@ -2635,42 +2546,6 @@ paths:
example: "0"
500:
description: Internal Server
/treasury/seigniorage_proceeds/{epoch}:
get:
summary: retrieves the size of the seigniorage pool at epoch
tags:
- Treasury
produces:
- application/json
parameters:
- in: path
name: epoch
description: Epoch number
required: true
type: integer
responses:
200:
description: OK
schema:
type: integer
example: "0"
500:
description: Internal Server Error
/treasury/current_epoch:
get:
summary: Get current epoch
tags:
- Treasury
produces:
- application/json
responses:
200:
description: OK
schema:
type: number
example: "724"
404:
description: Not Found
/treasury/parameters:
get:
summary: Get treasury module params
Expand Down
77 changes: 38 additions & 39 deletions docs/specifications/treasury.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ The treasury module has two monetary policy levers in its toolkit. The tax rate,

```go
// t(t+1) = t(t) * (TL_year(t) + INC) / TL_month(t)
func updateTaxPolicy(ctx sdk.Context, k Keeper) (newTaxRate sdk.Dec) {
params := k.GetParams(ctx)

oldTaxRate := k.GetTaxRate(ctx, util.GetEpoch(ctx))
inc := params.MiningIncrement
tlYear := RollingAverageIndicator(ctx, k, params.WindowLong, TRL)
tlMonth := RollingAverageIndicator(ctx, k, params.WindowShort, TRL)

// No revenues, hike as much as possible.
if tlMonth.Equal(sdk.ZeroDec()) {
newTaxRate = params.TaxPolicy.RateMax
} else {
newTaxRate = oldTaxRate.Mul(tlYear.Mul(inc)).Quo(tlMonth)
}

newTaxRate = params.TaxPolicy.Clamp(oldTaxRate, newTaxRate)

// Set the new tax rate to the store
k.SetTaxRate(ctx, newTaxRate)
return
func (k Keeper) UpdateTaxPolicy(ctx sdk.Context) (newTaxRate sdk.Dec) {
params := k.GetParams(ctx)

oldTaxRate := k.GetTaxRate(ctx)
inc := params.MiningIncrement
tlYear := k.rollingAverageIndicator(ctx, params.WindowLong, types.TRLKey)
tlMonth := k.rollingAverageIndicator(ctx, params.WindowShort, types.TRLKey)

// No revenues, hike as much as possible.
if tlMonth.Equal(sdk.ZeroDec()) {
newTaxRate = params.TaxPolicy.RateMax
} else {
newTaxRate = oldTaxRate.Mul(tlYear.Mul(inc)).Quo(tlMonth)
}

newTaxRate = params.TaxPolicy.Clamp(oldTaxRate, newTaxRate)

// Set the new tax rate to the store
k.SetTaxRate(ctx, newTaxRate)
return
}
```

Expand All @@ -48,30 +48,29 @@ At the point of evaluation, the treasury hikes up tax rates when tax revenues in

```go
// w(t+1) = w(t)*SB_target/SB_rolling(t)
func updateRewardPolicy(ctx sdk.Context, k Keeper) (newRewardWeight sdk.Dec) {
params := k.GetParams(ctx)
func (k Keeper) UpdateRewardPolicy(ctx sdk.Context) (newRewardWeight sdk.Dec) {
params := k.GetParams(ctx)

curEpoch := util.GetEpoch(ctx)
oldWeight := k.GetRewardWeight(ctx, curEpoch)
sbTarget := params.SeigniorageBurdenTarget
oldWeight := k.GetRewardWeight(ctx)
sbTarget := params.SeigniorageBurdenTarget

seigniorageSum := SumIndicator(ctx, k, params.WindowShort, SeigniorageRewardsForEpoch)
totalSum := SumIndicator(ctx, k, params.WindowShort, MiningRewardForEpoch)
seigniorageSum := k.sumIndicator(ctx, params.WindowShort, types.SRKey)
totalSum := k.sumIndicator(ctx, params.WindowShort, types.MRKey)

// No revenues; hike as much as possible
if totalSum.Equal(sdk.ZeroDec()) || seigniorageSum.Equal(sdk.ZeroDec()) {
newRewardWeight = params.RewardPolicy.RateMax
} else {
// Seigniorage burden out of total rewards
sb := seigniorageSum.Quo(totalSum)
newRewardWeight = oldWeight.Mul(sbTarget.Quo(sb))
}
// No revenues; hike as much as possible
if totalSum.Equal(sdk.ZeroDec()) || seigniorageSum.Equal(sdk.ZeroDec()) {
newRewardWeight = params.RewardPolicy.RateMax
} else {
// Seigniorage burden out of total rewards
sb := seigniorageSum.Quo(totalSum)
newRewardWeight = oldWeight.Mul(sbTarget.Quo(sb))
}

newRewardWeight = params.RewardPolicy.Clamp(oldWeight, newRewardWeight)
newRewardWeight = params.RewardPolicy.Clamp(oldWeight, newRewardWeight)

// Set the new reward weight
k.SetRewardWeight(ctx, newRewardWeight)
return
// Set the new reward weight
k.SetRewardWeight(ctx, newRewardWeight)
return
}
```

Expand Down
Empty file added tools-stamp
Empty file.
4 changes: 2 additions & 2 deletions x/auth/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func NewAnteHandler(ak AccountKeeper, supplyKeeper types.SupplyKeeper,
}

// record tax proceeds
treasuryKeeper.RecordTaxProceeds(newCtx, stdTx.Fee.Amount)
treasuryKeeper.RecordEpochTaxProceeds(newCtx, taxes)

// reload the account as fees have been deducted
signerAccs[0] = ak.GetAccount(newCtx, signerAccs[0].GetAddress())
Expand Down Expand Up @@ -402,7 +402,7 @@ func filterMsgAndComputeTax(ctx sdk.Context, tk TreasuryKeeper, msgs []sdk.Msg)

// computes the stability tax according to tax-rate and tax-cap
func computeTax(ctx sdk.Context, tk TreasuryKeeper, principal sdk.Coins) (taxes sdk.Coins) {
taxRate := tk.GetTaxRate(ctx, core.GetEpoch(ctx))
taxRate := tk.GetTaxRate(ctx)
if taxRate.Equal(sdk.ZeroDec()) {
return
}
Expand Down
13 changes: 1 addition & 12 deletions x/auth/client/utils/feeutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,20 +226,9 @@ func computeTax(cliCtx context.CLIContext, taxRate sdk.Dec, principal sdk.Coins)
}

func queryTaxRate(cliCtx context.CLIContext) (sdk.Dec, error) {
// Query current-epoch
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", treasury.QuerierRoute, treasury.QueryCurrentEpoch), nil)
if err != nil {
return sdk.Dec{}, err
}

var epoch int64
cliCtx.Codec.MustUnmarshalJSON(res, &epoch)

params := treasury.NewQueryTaxRateParams(epoch)
bz := cliCtx.Codec.MustMarshalJSON(params)

// Query tax-rate
res, _, err = cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", treasury.QuerierRoute, treasury.QueryTaxRate), bz)
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", treasury.QuerierRoute, treasury.QueryTaxRate), nil)
if err != nil {
return sdk.Dec{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions x/auth/internal/types/expected_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (

// TreasuryKeeper is expected keeper for treasury
type TreasuryKeeper interface {
GetTaxRate(ctx sdk.Context, epoch int64) (rate sdk.Dec)
GetTaxRate(ctx sdk.Context) (rate sdk.Dec)
GetTaxCap(ctx sdk.Context, denom string) (taxCap sdk.Int)
RecordTaxProceeds(ctx sdk.Context, delta sdk.Coins)
RecordEpochTaxProceeds(ctx sdk.Context, delta sdk.Coins)
}

// SupplyKeeper defines the expected supply Keeper (noalias)
Expand Down
7 changes: 4 additions & 3 deletions x/auth/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@ func setupTestInput() testInput {
// DummyTreasuryKeeper no-lint
type DummyTreasuryKeeper struct{}

// NewDummyTreasuryKeeper no-lint
func NewDummyTreasuryKeeper() DummyTreasuryKeeper { return DummyTreasuryKeeper{} }

// GetTaxRate for the dummy treasury keeper
func (tk DummyTreasuryKeeper) GetTaxRate(_ sdk.Context, _ int64) (rate sdk.Dec) {
func (tk DummyTreasuryKeeper) GetTaxRate(_ sdk.Context) (rate sdk.Dec) {
return sdk.NewDecWithPrec(1, 3) // 0.1%
}

Expand All @@ -99,8 +100,8 @@ func (tk DummyTreasuryKeeper) GetTaxCap(_ sdk.Context, _ string) (taxCap sdk.Int
return sdk.OneInt()
}

// RecordTaxProceeds for the dummy treasury keeper
func (tk DummyTreasuryKeeper) RecordTaxProceeds(_ sdk.Context, _ sdk.Coins) {
// RecordEpochTaxProceeds for the dummy treasury keeper
func (tk DummyTreasuryKeeper) RecordEpochTaxProceeds(_ sdk.Context, _ sdk.Coins) {
return
}

Expand Down
6 changes: 5 additions & 1 deletion x/treasury/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ func EndBlocker(ctx sdk.Context, k Keeper) {
}

// Update luna issuance after finish all works
defer k.RecordHistoricalIssuance(ctx)
defer k.RecordEpochInitialIssuance(ctx)

// Compute & Update internal indicators for the current epoch
k.UpdateIndicators(ctx)

// Check probation period
if ctx.BlockHeight() < (core.BlocksPerEpoch * k.WindowProbation(ctx)) {
return
}

// Settle seiniorage to oracle & distribution(community-pool) module-account
k.SettleSeigniorage(ctx)

// Update tax-rate and reward-weight of next epoch
Expand Down
Loading

0 comments on commit 864756a

Please sign in to comment.