Skip to content

Commit

Permalink
fix: oracle keeper
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yang committed Aug 19, 2024
1 parent 2b8cb36 commit da76c39
Show file tree
Hide file tree
Showing 10 changed files with 657 additions and 592 deletions.
2 changes: 1 addition & 1 deletion app/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ func (app *NibiruApp) initAppModules(
authzmodule.NewAppModule(appCodec, app.authzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),

// Nibiru modules
oracle.NewAppModule(appCodec, app.OracleKeeper, app.AccountKeeper, app.BankKeeper),
oracle.NewAppModule(appCodec, app.OracleKeeper, app.AccountKeeper, app.BankKeeper, app.SudoKeeper),
epochs.NewAppModule(appCodec, app.EpochsKeeper),
inflation.NewAppModule(app.InflationKeeper, app.AccountKeeper, *app.StakingKeeper),
sudo.NewAppModule(appCodec, app.SudoKeeper),
Expand Down
103 changes: 70 additions & 33 deletions proto/nibiru/oracle/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package nibiru.oracle.v1;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "google/protobuf/duration.proto";
import "nibiru/oracle/v1/oracle.proto";

option go_package = "github.com/NibiruChain/nibiru/v2/x/oracle/types";
Expand Down Expand Up @@ -33,7 +34,7 @@ service Msg {

rpc EditOracleParams(MsgEditOracleParams)
returns (MsgEditOracleParamsResponse) {
option (google.api.http).post = "/nibiru/oracle/edit-oracle-params";
option (google.api.http).post = "/nibiru/oracle/params";
}
}

Expand Down Expand Up @@ -91,64 +92,100 @@ message MsgDelegateFeedConsent {
// type.
message MsgDelegateFeedConsentResponse {}

// MsgEditOracleParams: gRPC tx message for updating the x/oracle module params
// [SUDO] Only callable by sudoers.
message MsgEditOracleParams {

Check failure on line 95 in proto/nibiru/oracle/v1/tx.proto

View workflow job for this annotation

GitHub Actions / break-check

Previously present field "10" with name "min_voters" on message "MsgEditOracleParams" was deleted.

Check failure on line 95 in proto/nibiru/oracle/v1/tx.proto

View workflow job for this annotation

GitHub Actions / break-check

Previously present field "11" with name "validator_fee_ratio" on message "MsgEditOracleParams" was deleted.

Check failure on line 95 in proto/nibiru/oracle/v1/tx.proto

View workflow job for this annotation

GitHub Actions / break-check

Previously present field "3" with name "vote_threshold" on message "MsgEditOracleParams" was deleted.

Check failure on line 95 in proto/nibiru/oracle/v1/tx.proto

View workflow job for this annotation

GitHub Actions / break-check

Previously present field "4" with name "reward_band" on message "MsgEditOracleParams" was deleted.

Check failure on line 95 in proto/nibiru/oracle/v1/tx.proto

View workflow job for this annotation

GitHub Actions / break-check

Previously present field "5" with name "whitelist" on message "MsgEditOracleParams" was deleted.

Check failure on line 95 in proto/nibiru/oracle/v1/tx.proto

View workflow job for this annotation

GitHub Actions / break-check

Previously present field "6" with name "slash_fraction" on message "MsgEditOracleParams" was deleted.

Check failure on line 95 in proto/nibiru/oracle/v1/tx.proto

View workflow job for this annotation

GitHub Actions / break-check

Previously present field "7" with name "slash_window" on message "MsgEditOracleParams" was deleted.

Check failure on line 95 in proto/nibiru/oracle/v1/tx.proto

View workflow job for this annotation

GitHub Actions / break-check

Previously present field "8" with name "min_valid_per_window" on message "MsgEditOracleParams" was deleted.

Check failure on line 95 in proto/nibiru/oracle/v1/tx.proto

View workflow job for this annotation

GitHub Actions / break-check

Previously present field "9" with name "twap_lookback_window" on message "MsgEditOracleParams" was deleted.
string sender = 1;
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

string sender = 1 [ (gogoproto.moretags) = "yaml:\"sender\"" ];

OracleParamsMsg params = 2 [ (gogoproto.moretags) = "yaml:\"params\"" ];

Check failure on line 101 in proto/nibiru/oracle/v1/tx.proto

View workflow job for this annotation

GitHub Actions / break-check

Field "2" with name "params" on message "MsgEditOracleParams" changed cardinality from "optional with implicit presence" to "optional with explicit presence".
}

string vote_period = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
message MsgEditOracleParamsResponse {}

message OracleParamsMsg {
option (gogoproto.equal) = true;
option (gogoproto.goproto_stringer) = true;

// VotePeriod defines the number of blocks during which voting takes place.
uint64 vote_period = 1 [
(gogoproto.moretags) = "yaml:\"vote_period\"",
(gogoproto.nullable) = true
];

// vote_threshold: [cosmossdk.io/math.LegacyDec] TODO:
string vote_threshold = 3 [
// VoteThreshold specifies the minimum proportion of votes that must be
// received for a ballot to pass.
string vote_threshold = 2 [
(gogoproto.moretags) = "yaml:\"vote_threshold\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = true
];

// reward_band: [cosmossdk.io/math.LegacyDec] TODO:
string reward_band = 4 [
// RewardBand defines a maxium divergence that a price vote can have from the
// weighted median in the ballot. If a vote lies within the valid range
// defined by:
// μ := weightedMedian,
// validRange := μ ± (μ * rewardBand / 2),
// then rewards are added to the validator performance.
// Note that if the reward band is smaller than 1 standard
// deviation, the band is taken to be 1 standard deviation.a price
string reward_band = 3 [
(gogoproto.moretags) = "yaml:\"reward_band\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = true
];

repeated string whitelist = 5 [ (gogoproto.nullable) = true ];

// slash_fraction: [cosmossdk.io/math.LegacyDec] TODO:
string slash_fraction = 6 [
// The set of whitelisted markets, or asset pairs, for the module.
// Ex. '["unibi:uusd","ubtc:uusd"]'
repeated string whitelist = 4 [
(gogoproto.moretags) = "yaml:\"whitelist\"",
(gogoproto.customtype) = "github.com/NibiruChain/nibiru/v2/x/common/asset.Pair",
(gogoproto.nullable) = true
];
// SlashFraction returns the proportion of an oracle's stake that gets
// slashed in the event of slashing. `SlashFraction` specifies the exact
// penalty for failing a voting period.
string slash_fraction = 5 [
(gogoproto.moretags) = "yaml:\"slash_fraction\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = true
];

string slash_window = 7 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
// SlashWindow returns the number of voting periods that specify a
// "slash window". After each slash window, all oracles that have missed more
// than the penalty threshold are slashed. Missing the penalty threshold is
// synonymous with submitting fewer valid votes than `MinValidPerWindow`.
uint64 slash_window = 6 [
(gogoproto.moretags) = "yaml:\"slash_window\"",
(gogoproto.nullable) = true
];

// min_valid_per_window: [cosmossdk.io/math.LegacyDec] TODO:
string min_valid_per_window = 8 [
string min_valid_per_window = 7 [
(gogoproto.moretags) = "yaml:\"min_valid_per_window\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = true
];

string twap_lookback_window = 9 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = true
// Amount of time to look back for TWAP calculations
google.protobuf.Duration twap_lookback_window = 8 [
(gogoproto.nullable) = true,
(gogoproto.stdduration) = true,
(gogoproto.jsontag) = "twap_lookback_window,omitempty",
(gogoproto.moretags) = "yaml:\"twap_lookback_window\""
];

string min_voters = 10 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
// The minimum number of voters (i.e. oracle validators) per pair for it to be
// considered a passing ballot. Recommended at least 4.
uint64 min_voters = 9 [
(gogoproto.moretags) = "yaml:\"min_voters\"",
(gogoproto.nullable) = true
];

// VoteThreshold: [cosmossdk.io/math.LegacyDec] TODO:
string validator_fee_ratio = 11 [
// The validator fee ratio that is given to validators every epoch.
string validator_fee_ratio = 10 [
(gogoproto.moretags) = "yaml:\"validator_fee_ratio\"",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = true
];
}

// MsgEditOracleParamsResponse defines the Msg/EditOracleParams response
// type.
message MsgEditOracleParamsResponse { nibiru.oracle.v1.Params new_params = 1; }
uint64 expiration_blocks = 11 [
(gogoproto.moretags) = "yaml:\"expiration_blocks\"",
(gogoproto.nullable) = true
];
}
44 changes: 28 additions & 16 deletions x/oracle/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@ package keeper

import (
"context"

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

sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/NibiruChain/nibiru/v2/x/oracle/types"
sudokeeper "github.com/NibiruChain/nibiru/v2/x/sudo/keeper"
sudotypes "github.com/NibiruChain/nibiru/v2/x/sudo/types"
)

type msgServer struct {
Keeper
SudoKeeper sudokeeper.Keeper
}

// NewMsgServerImpl returns an implementation of the oracle MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{Keeper: keeper}
func NewMsgServerImpl(keeper Keeper, sudoKeeper sudokeeper.Keeper) types.MsgServer {
return &msgServer{Keeper: keeper, SudoKeeper: sudoKeeper}
}

func (ms msgServer) AggregateExchangeRatePrevote(
Expand Down Expand Up @@ -170,18 +173,27 @@ func (ms msgServer) DelegateFeedConsent(

// EditOracleParams: gRPC tx msg for editing the oracle module params.
// [SUDO] Only callable by sudoers.
func (ms msgServer) EditOracleParams(
goCtx context.Context, msg *types.MsgEditOracleParams,
) (resp *types.MsgEditOracleParamsResponse, err error) {
func (ms msgServer) EditOracleParams(goCtx context.Context, msg *types.MsgEditOracleParams) (*types.MsgEditOracleParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Stateless field validation is already performed in msg.ValidateBasic()
// before the current scope is reached.
sender, _ := sdk.AccAddressFromBech32(msg.Sender)
newParams, err := ms.Sudo().EditOracleParams(
ctx, *msg, sender,
)
resp = &types.MsgEditOracleParamsResponse{
NewParams: &newParams,

sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return nil, fmt.Errorf("invalid address")
}

err = ms.SudoKeeper.CheckPermissions(sender, ctx)
if err != nil {
return nil, sudotypes.ErrUnauthorized
}

params, err := ms.Keeper.Params.Get(ctx)
if err != nil {
return nil, fmt.Errorf("get oracle params error: %s", err.Error())
}
return resp, err

mergedParams := mergeOracleParams(msg, params)

ms.Keeper.UpdateParams(ctx, mergedParams)

return &types.MsgEditOracleParamsResponse{}, nil
}
1 change: 1 addition & 0 deletions x/oracle/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

"github.com/NibiruChain/nibiru/v2/x/common/asset"
Expand Down
100 changes: 0 additions & 100 deletions x/oracle/keeper/sudo.go

This file was deleted.

Loading

0 comments on commit da76c39

Please sign in to comment.