Skip to content

Commit

Permalink
move historic avg counter params into oracle genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
gsk967 committed Jun 30, 2023
1 parent 5f325f2 commit 7f3e963
Show file tree
Hide file tree
Showing 15 changed files with 247 additions and 225 deletions.
5 changes: 5 additions & 0 deletions proto/umee/oracle/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ message GenesisState {
repeated Price medians = 7 [(gogoproto.nullable) = false];
repeated Price historic_prices = 8 [(gogoproto.nullable) = false];
repeated Price medianDeviations = 9 [(gogoproto.nullable) = false];
// Historic Avg Counter params
AvgCounterParams avg_counter_params = 10 [
(gogoproto.moretags) = "yaml:\"avg_counter_params\"",
(gogoproto.nullable) = false
];
}

// FeederDelegation is the address for where oracle feeder authority are
Expand Down
5 changes: 0 additions & 5 deletions proto/umee/oracle/v1/oracle.proto
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ message Params {
// Maximum Median Stamps represents the maximum amount of medians the
// oracle module will store before pruning via FIFO.
uint64 maximum_median_stamps = 12;
// Historic Avg Counter params
AvgCounterParams avg_counter_params = 13 [
(gogoproto.moretags) = "yaml:\"avg_counter_params\"",
(gogoproto.nullable) = false
];
}

// AvgCounterParams - Historic avg counter params
Expand Down
7 changes: 7 additions & 0 deletions x/oracle/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, genState types.GenesisSt
if moduleAcc == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}

// set historic avg counter params (avgPeriod and avgShift)
err := keeper.SetHistoricAvgCounterParams(ctx, genState.AvgCounterParams)
util.Panic(err)
}

// ExportGenesis returns the x/oracle module's exported genesis.
Expand Down Expand Up @@ -128,6 +132,8 @@ func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState {
historicPrices := keeper.AllHistoricPrices(ctx)
medianPrices := keeper.AllMedianPrices(ctx)
medianDeviationPrices := keeper.AllMedianDeviationPrices(ctx)
hacp, err := keeper.GetHistoricAvgCounterParams(ctx)
util.Panic(err)

return types.NewGenesisState(
params,
Expand All @@ -139,5 +145,6 @@ func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState {
historicPrices,
medianPrices,
medianDeviationPrices,
hacp,
)
}
3 changes: 3 additions & 0 deletions x/oracle/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ func (s *IntegrationTestSuite) TestGenesis_ExportGenesis() {
BlockNum: 0,
},
}
hacp := types.DefaultAvgCounterParams()

genesisState := types.GenesisState{
Params: params,
Expand All @@ -211,6 +212,7 @@ func (s *IntegrationTestSuite) TestGenesis_ExportGenesis() {
Medians: medians,
HistoricPrices: historicPrices,
MedianDeviations: medianDeviations,
AvgCounterParams: hacp,
}

oracle.InitGenesis(ctx, keeper, genesisState)
Expand All @@ -225,4 +227,5 @@ func (s *IntegrationTestSuite) TestGenesis_ExportGenesis() {
assert.DeepEqual(s.T(), medians, result.Medians)
assert.DeepEqual(s.T(), historicPrices, result.HistoricPrices)
assert.DeepEqual(s.T(), medianDeviations, result.MedianDeviations)
assert.DeepEqual(s.T(), hacp, result.AvgCounterParams)
}
23 changes: 23 additions & 0 deletions x/oracle/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,26 @@ func (k Keeper) AllMedianDeviationPrices(ctx sdk.Context) types.Prices {
})
return prices
}

// SetAvgPeSetHistoricAvgCounterParams sets avg period and avg shift time duration
func (k Keeper) SetHistoricAvgCounterParams(ctx sdk.Context, acp types.AvgCounterParams) error {
store := ctx.KVStore(k.storeKey)
bz, err := acp.Marshal()
if err != nil {
return err
}
store.Set(types.KeyHistoricAvgCounterParams, bz)
return nil
}

// GetHistoricAvgCounterParams gets the avg period and avg shift time duration from store
func (k Keeper) GetHistoricAvgCounterParams(ctx sdk.Context) (types.AvgCounterParams, error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.KeyHistoricAvgCounterParams)
var acp types.AvgCounterParams
err := acp.Unmarshal(bz)
if err != nil {
return types.AvgCounterParams{}, err
}
return acp, nil
}
3 changes: 2 additions & 1 deletion x/oracle/keeper/historic_avg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type AvgKeeper struct {
}

func (k Keeper) AvgKeeper(ctx sdk.Context) AvgKeeper {
p := k.GetParams(ctx).AvgCounterParams
p, err := k.GetHistoricAvgCounterParams(ctx)
util.Panic(err)
return AvgKeeper{store: ctx.KVStore(k.storeKey), period: p.AvgPeriod, shift: p.AvgShift}
}

Expand Down
3 changes: 1 addition & 2 deletions x/oracle/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ func (m Migrator) HistoracleParams3x4(ctx sdk.Context) error {
// SetAvgPeriodAndShift updates the avg shift and period params
func (m Migrator) SetAvgPeriodAndShift(ctx sdk.Context) error {
p := types.DefaultAvgCounterParams()
m.keeper.SetHistoricAvgCounterParams(ctx, *p)
return nil
return m.keeper.SetHistoricAvgCounterParams(ctx, p)
}

// MigrateBNB fixes the BNB base denom for the 4.1 upgrade without using leverage hooks
Expand Down
5 changes: 0 additions & 5 deletions x/oracle/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,3 @@ func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, &params)
}

// SetAvgPeSetHistoricAvgCounterParams sets avg period and avg shift time duration
func (k Keeper) SetHistoricAvgCounterParams(ctx sdk.Context, acp types.AvgCounterParams) {
k.paramSpace.Set(ctx, types.KeyHistoricAvgCounterParams, &acp)
}
8 changes: 4 additions & 4 deletions x/oracle/simulations/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ func RandomizedGenState(simState *module.SimulationState) {
)

oracleGenesis := types.DefaultGenesisState()
oracleGenesis.AvgCounterParams = types.AvgCounterParams{
AvgPeriod: avgPeriod,
AvgShift: avgShift,
}
oracleGenesis.Params = types.Params{
VotePeriod: votePeriod,
VoteThreshold: voteThreshold,
Expand All @@ -190,10 +194,6 @@ func RandomizedGenState(simState *module.SimulationState) {
MedianStampPeriod: medianStampPeriod,
MaximumPriceStamps: historicStampPeriod,
MaximumMedianStamps: historicStampPeriod,
AvgCounterParams: types.AvgCounterParams{
AvgPeriod: avgPeriod,
AvgShift: avgShift,
},
}

bz, err := json.MarshalIndent(&oracleGenesis.Params, "", " ")
Expand Down
33 changes: 32 additions & 1 deletion x/oracle/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"encoding/json"
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
)
Expand All @@ -17,6 +18,7 @@ func NewGenesisState(
historicPrices []Price,
medianPrices []Price,
medianDeviationPrices []Price,
acp AvgCounterParams,
) *GenesisState {
return &GenesisState{
Params: params,
Expand All @@ -28,6 +30,7 @@ func NewGenesisState(
HistoricPrices: historicPrices,
Medians: medianPrices,
MedianDeviations: medianDeviationPrices,
AvgCounterParams: acp,
}
}

Expand All @@ -44,12 +47,17 @@ func DefaultGenesisState() *GenesisState {
HistoricPrices: []Price{},
Medians: []Price{},
MedianDeviations: []Price{},
AvgCounterParams: DefaultAvgCounterParams(),
}
}

// ValidateGenesis validates the oracle genesis state.
func ValidateGenesis(data *GenesisState) error {
return data.Params.Validate()
if err := data.Params.Validate(); err != nil {
return err
}

return data.AvgCounterParams.Validate()
}

// GetGenesisStateFromAppState returns x/oracle GenesisState given raw application
Expand All @@ -63,3 +71,26 @@ func GetGenesisStateFromAppState(cdc codec.JSONCodec, appState map[string]json.R

return &genesisState
}

func DefaultAvgCounterParams() AvgCounterParams {
return AvgCounterParams{
AvgPeriod: DefaultAvgPeriod, // 16 hours
AvgShift: DefaultAvgShift, // 12 hours
}
}

func (acp AvgCounterParams) Equal(other *AvgCounterParams) bool {
return acp.AvgPeriod == other.AvgPeriod && acp.AvgShift == other.AvgShift
}

func (acp AvgCounterParams) Validate() error {
if acp.AvgPeriod.Seconds() <= 0 {
return fmt.Errorf("avg period must be positive: %d", acp.AvgPeriod)
}

if acp.AvgShift.Seconds() <= 0 {
return fmt.Errorf("avg shift must be positive: %d", acp.AvgShift)
}

return nil
}
125 changes: 87 additions & 38 deletions x/oracle/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions x/oracle/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func TestGetGenesisStateFromAppState(t *testing.T) {
MissCounters: []MissCounter{},
AggregateExchangeRatePrevotes: []AggregateExchangeRatePrevote{},
AggregateExchangeRateVotes: []AggregateExchangeRateVote{},
AvgCounterParams: AvgCounterParams{},
}

bz, err := json.Marshal(emptyGenesis)
Expand Down
Loading

0 comments on commit 7f3e963

Please sign in to comment.