Skip to content

Commit

Permalink
WIP: moving avg period and shift to oracle genesis params
Browse files Browse the repository at this point in the history
  • Loading branch information
gsk967 committed Jun 27, 2023
1 parent ec18ed4 commit 307ec72
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 83 deletions.
13 changes: 13 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 @@ -55,6 +56,18 @@ 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;
// avg_period
google.protobuf.Duration avg_period = 13 [
(gogoproto.nullable) = false,
(gogoproto.stdduration) = true,
(gogoproto.moretags) = "json:\"avg_period\""
];
// avg shift
google.protobuf.Duration avg_shift = 14 [
(gogoproto.nullable) = false,
(gogoproto.stdduration) = true,
(gogoproto.moretags) = "json:\"avg_shift\""
];
}

// Denom - the object to hold configurations of each denom
Expand Down
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}
params := k.GetParams(ctx)
return AvgKeeper{store: ctx.KVStore(k.storeKey), period: params.AvgPeriod, shift: params.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
13 changes: 0 additions & 13 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 Down Expand Up @@ -31,16 +30,8 @@ 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,
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
8 changes: 8 additions & 0 deletions x/oracle/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ 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.DefaultParams()
m.keeper.SetAvgPeriod(ctx, p.AvgPeriod)
m.keeper.SetAvgShift(ctx, p.AvgShift)
return nil
}

// 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
12 changes: 12 additions & 0 deletions x/oracle/keeper/params.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/umee-network/umee/v5/x/oracle/types"
Expand Down Expand Up @@ -136,3 +138,13 @@ 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)
}

// SetAvgPeriod sets the avg shift time duration
func (k Keeper) SetAvgPeriod(ctx sdk.Context, avgPeriod time.Duration) {
k.paramSpace.Set(ctx, types.KeyAvgPeriod, &avgPeriod)
}

// SetAvgShift sets the avg shift time duration
func (k Keeper) SetAvgShift(ctx sdk.Context, avgShift time.Duration) {
k.paramSpace.Set(ctx, types.KeyAvgShift, &avgShift)
}
234 changes: 168 additions & 66 deletions x/oracle/types/oracle.pb.go

Large diffs are not rendered by default.

47 changes: 45 additions & 2 deletions x/oracle/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"fmt"
time "time"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand All @@ -16,8 +17,10 @@ var (

// maxium number of decimals allowed for VoteThreshold
const (
MaxVoteThresholdPrecision = 2
MaxVoteThresholdMultiplier = 100 // must be 10^MaxVoteThresholdPrecision
MaxVoteThresholdPrecision = 2
MaxVoteThresholdMultiplier = 100 // must be 10^MaxVoteThresholdPrecision
DefaultAvgPeriod time.Duration = time.Hour * 16
DefaultAvgShift time.Duration = time.Hour * 2
)

// Parameter keys
Expand All @@ -34,6 +37,8 @@ var (
KeyMedianStampPeriod = []byte("MedianStampPeriod")
KeyMaximumPriceStamps = []byte("MaximumPriceStamps")
KeyMaximumMedianStamps = []byte("MedianStampAmount")
KeyAvgPeriod = []byte("AvgPeriod")
KeyAvgShift = []byte("AvgShift")
)

var _ paramstypes.ParamSet = &Params{}
Expand Down Expand Up @@ -61,6 +66,8 @@ func DefaultParams() Params {
MedianStampPeriod: BlocksPerHour * 3, // 3h
MaximumPriceStamps: 36, // 3h
MaximumMedianStamps: 24, // 3 days
AvgPeriod: DefaultAvgPeriod, // 16 hours
AvgShift: DefaultAvgShift, // 12 hours
}
}

Expand Down Expand Up @@ -133,6 +140,16 @@ func (p *Params) ParamSetPairs() paramstypes.ParamSetPairs {
&p.MaximumMedianStamps,
validateMaximumMedianStamps,
),
paramstypes.NewParamSetPair(
KeyAvgPeriod,
&p.AvgPeriod,
validateAvgPeriod,
),
paramstypes.NewParamSetPair(
KeyAvgShift,
&p.AvgShift,
validateAvgShift,
),
}
}

Expand Down Expand Up @@ -359,6 +376,32 @@ func validateMaximumMedianStamps(i interface{}) error {
return nil
}

func validateAvgPeriod(i interface{}) error {
v, ok := i.(time.Duration)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}

if v < 1 {
return fmt.Errorf("avg period must be positive: %d", v)
}

return nil
}

func validateAvgShift(i interface{}) error {
v, ok := i.(time.Duration)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}

if v < 1 {
return fmt.Errorf("avg shift must be positive: %d", v)
}

return nil
}

// ValidateVoteThreshold validates oracle exchange rates power vote threshold.
// Must be
// * a decimal value > 0.33 and <= 1.
Expand Down

0 comments on commit 307ec72

Please sign in to comment.