Skip to content

Commit

Permalink
Merge branch 'main' into sai/incentive_cw_handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
gsk967 committed Jul 6, 2023
2 parents 31ab62c + fd82ca2 commit 10da1dc
Show file tree
Hide file tree
Showing 15 changed files with 504 additions and 127 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
19 changes: 19 additions & 0 deletions proto/umee/oracle/v1/oracle.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package umee.oracle.v1;

import "gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";
import "google/protobuf/duration.proto";

option go_package = "github.com/umee-network/umee/v5/x/oracle/types";

Expand Down Expand Up @@ -57,6 +58,24 @@ message Params {
uint64 maximum_median_stamps = 12;
}

// AvgCounterParams - Historic avg counter params
message AvgCounterParams {
// avg_period
google.protobuf.Duration avg_period = 1 [
(gogoproto.nullable) = false,
(gogoproto.stdduration) = true,
(gogoproto.jsontag) = "avg_period,omitempty",
(gogoproto.moretags) = "yaml:\"avg_period\""
];
// avg shift
google.protobuf.Duration avg_shift = 2 [
(gogoproto.nullable) = false,
(gogoproto.stdduration) = true,
(gogoproto.jsontag) = "avg_shift,omitempty",
(gogoproto.moretags) = "yaml:\"avg_shift\""
];
}

// Denom - the object to hold configurations of each denom
message Denom {
option (gogoproto.equal) = false;
Expand Down
6 changes: 6 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,7 @@ func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState {
historicPrices := keeper.AllHistoricPrices(ctx)
medianPrices := keeper.AllMedianPrices(ctx)
medianDeviationPrices := keeper.AllMedianDeviationPrices(ctx)
hacp := keeper.GetHistoricAvgCounterParams(ctx)

return types.NewGenesisState(
params,
Expand All @@ -139,5 +144,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)
}
18 changes: 18 additions & 0 deletions x/oracle/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
storeutil "github.com/umee-network/umee/v5/util/store"
"github.com/umee-network/umee/v5/x/oracle/types"
)

Expand Down Expand Up @@ -111,3 +112,20 @@ 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 {
kvs := ctx.KVStore(k.storeKey)
return storeutil.SetObject(kvs, k.cdc, types.KeyHistoricAvgCounterParams, &acp, "historic avg counter params")
}

// GetHistoricAvgCounterParams gets the avg period and avg shift time duration from store
func (k Keeper) GetHistoricAvgCounterParams(ctx sdk.Context) types.AvgCounterParams {
kvs := ctx.KVStore(k.storeKey)
var acp types.AvgCounterParams
ok := storeutil.GetObject(kvs, k.cdc, types.KeyHistoricAvgCounterParams, &acp, "historic avg counter params")
if ok {
return acp
}
return types.AvgCounterParams{}
}
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 {
return AvgKeeper{store: ctx.KVStore(k.storeKey), period: k.AvgPeriod, shift: k.AvgShift}
p := k.GetHistoricAvgCounterParams(ctx)
return AvgKeeper{store: ctx.KVStore(k.storeKey), period: p.AvgPeriod, shift: p.AvgShift}
}

func (k AvgKeeper) numCounters() int64 {
Expand Down
2 changes: 1 addition & 1 deletion x/oracle/keeper/historic_avg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (s AvgKeeperSuite) newAvgKeeper(t *testing.T, period, shift time.Duration)
}

func (s AvgKeeperSuite) newDefAvgKeeper(t *testing.T) AvgKeeper {
return s.newAvgKeeper(t, defaultAvgPeriod, defaultAvgShift)
return s.newAvgKeeper(t, types.DefaultAvgPeriod, types.DefaultAvgPeriod)
}

func (s AvgKeeperSuite) testNewCounters(t *testing.T) {
Expand Down
17 changes: 2 additions & 15 deletions x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package keeper
import (
"fmt"
"strings"
"time"

"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
Expand All @@ -21,7 +20,7 @@ var ten = sdk.MustNewDecFromStr("10")

// Keeper of the oracle store
type Keeper struct {
cdc codec.BinaryCodec
cdc codec.Codec
storeKey storetypes.StoreKey
paramSpace paramstypes.Subspace

Expand All @@ -31,19 +30,11 @@ type Keeper struct {
StakingKeeper types.StakingKeeper

distrName string

AvgPeriod time.Duration
AvgShift time.Duration
}

const (
defaultAvgPeriod time.Duration = time.Hour * 16
defaultAvgShift time.Duration = time.Hour * 2
)

// NewKeeper constructs a new keeper for oracle
func NewKeeper(
cdc codec.BinaryCodec,
cdc codec.Codec,
storeKey storetypes.StoreKey,
paramspace paramstypes.Subspace,
accountKeeper types.AccountKeeper,
Expand Down Expand Up @@ -71,10 +62,6 @@ func NewKeeper(
distrKeeper: distrKeeper,
StakingKeeper: stakingKeeper,
distrName: distrName,

// AvgPeriod and AvgShift must not be changed after an app started.
AvgPeriod: defaultAvgPeriod,
AvgShift: defaultAvgShift,
}
}

Expand Down
6 changes: 6 additions & 0 deletions x/oracle/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ func (m Migrator) HistoracleParams3x4(ctx sdk.Context) error {
return nil
}

// SetAvgPeriodAndShift updates the avg shift and period params
func (m Migrator) SetAvgPeriodAndShift(ctx sdk.Context) error {
p := types.DefaultAvgCounterParams()
return m.keeper.SetHistoricAvgCounterParams(ctx, p)
}

// MigrateBNB fixes the BNB base denom for the 4.1 upgrade without using leverage hooks
func (m Migrator) MigrateBNB(ctx sdk.Context) {
badDenom := "ibc/77BCD42E49E5B7E0FC6B269FEBF0185B15044F13F6F38CA285DF0AF883459F40"
Expand Down
30 changes: 30 additions & 0 deletions x/oracle/simulations/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"encoding/json"
"fmt"
"math/rand"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/umee-network/umee/v5/x/oracle/types"
)

Expand All @@ -23,13 +25,25 @@ const (
medianStampPeriodKey = "median_stamp_period"
maximumPriceStampsKey = "maximum_price_stamps"
maximumMedianStampsKey = "maximum_median_stamps"
avgPeriodKey = "avg_period"
avgShiftKey = "avg_shift"
)

// GenVotePeriod produces a randomized VotePeriod in the range of [5, 100]
func GenVotePeriod(r *rand.Rand) uint64 {
return uint64(5 + r.Intn(100))
}

// GetAvgPeriod produces a randomized AvgPeriod in the range of 1 second to 1 day
func GetAvgPeriod(r *rand.Rand) time.Duration {
return time.Duration(simulation.RandIntBetween(r, 1, 60*60*24)) * time.Second
}

// GetAvgShift produces a randomized AvgShift in the range of 1 second to 5 hours
func GetAvgShift(r *rand.Rand) time.Duration {
return time.Duration(simulation.RandIntBetween(r, 1, 60*60*5)) * time.Second
}

// GenVoteThreshold produces a randomized VoteThreshold in the range of [0.34, 0.67]
func GenVoteThreshold(r *rand.Rand) sdk.Dec {
return sdk.NewDecWithPrec(34, 2).Add(sdk.NewDecWithPrec(int64(r.Intn(33)), 2))
Expand Down Expand Up @@ -148,7 +162,23 @@ func RandomizedGenState(simState *module.SimulationState) {
func(r *rand.Rand) { maximumMedianStamps = GenMaximumMedianStamps(r) },
)

var avgShift time.Duration
simState.AppParams.GetOrGenerate(
simState.Cdc, avgShiftKey, &avgShift, simState.Rand,
func(r *rand.Rand) { avgShift = GetAvgShift(r) },
)

var avgPeriod time.Duration
simState.AppParams.GetOrGenerate(
simState.Cdc, avgPeriodKey, &avgPeriod, simState.Rand,
func(r *rand.Rand) { avgPeriod = GetAvgPeriod(r) },
)

oracleGenesis := types.DefaultGenesisState()
oracleGenesis.AvgCounterParams = types.AvgCounterParams{
AvgPeriod: avgPeriod,
AvgShift: avgShift,
}
oracleGenesis.Params = types.Params{
VotePeriod: votePeriod,
VoteThreshold: voteThreshold,
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
}
Loading

0 comments on commit 10da1dc

Please sign in to comment.