Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Implement LegacyGovUpdateParams for querying legacy proposals #476

Merged
merged 4 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion proto/ojo/oracle/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ service Msg {
rpc DelegateFeedConsent(MsgDelegateFeedConsent)
returns (MsgDelegateFeedConsentResponse);

// LegacyGovUpdateParams defines the legacy message that updates the oracle parameters.
rpc LegacyGovUpdateParams(MsgLegacyGovUpdateParams)
returns (MsgLegacyGovUpdateParamsResponse);

// GovUpdateParams updates the oracle parameters.
rpc GovUpdateParams(MsgGovUpdateParams)
returns (MsgGovUpdateParamsResponse);
Expand Down Expand Up @@ -101,6 +105,24 @@ message MsgDelegateFeedConsent {
// type.
message MsgDelegateFeedConsentResponse {}

// MsgLegacyGovUpdateParams defines the Msg/MsgLegacyGovUpdateParams request type.
message MsgLegacyGovUpdateParams {
option (gogoproto.equal) = true;
option (gogoproto.goproto_getters) = false;
option (gogoproto.goproto_stringer) = false;
option (cosmos.msg.v1.signer) = "authority";

// authority is the address of the governance account.
string authority = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
string title = 2;
string description = 3;
repeated string keys = 4;
Params changes = 5 [ (gogoproto.nullable) = false ];
}

// MsgLegacyGovUpdateParams defines the Msg/MsgLegacyGovUpdateParams response type.
message MsgLegacyGovUpdateParamsResponse {}

// MsgGovUpdateParams defines the Msg/GovUpdateParams request type.
message MsgGovUpdateParams {
option (gogoproto.equal) = true;
Expand All @@ -124,7 +146,6 @@ message MsgGovUpdateParams {
// MsgGovUpdateParamsResponse defines the Msg/GovUpdateParams response type.
message MsgGovUpdateParamsResponse {}


// MsgGovAddDenoms defines the Msg/GovAddDenoms request type.
message MsgGovAddDenoms {
option (gogoproto.equal) = true;
Expand Down
106 changes: 106 additions & 0 deletions x/oracle/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"fmt"
"strings"

"cosmossdk.io/errors"
Expand Down Expand Up @@ -143,6 +144,111 @@ func (ms msgServer) DelegateFeedConsent(
return &types.MsgDelegateFeedConsentResponse{}, err
}

func (ms msgServer) LegacyGovUpdateParams(
goCtx context.Context,
msg *types.MsgLegacyGovUpdateParams,
) (*types.MsgLegacyGovUpdateParamsResponse, error) {
if msg.Authority != ms.authority {
err := errors.Wrapf(
types.ErrNoGovAuthority,
"invalid authority; expected %s, got %s",
ms.authority,
msg.Authority,
)
return nil, err
}

ctx := sdk.UnwrapSDKContext(goCtx)

for _, key := range msg.Keys {
switch key {
case string(types.KeyVotePeriod):
ms.SetVotePeriod(ctx, msg.Changes.VotePeriod)

case string(types.KeyVoteThreshold):
ms.SetVoteThreshold(ctx, msg.Changes.VoteThreshold)

case string(types.KeyRewardBands):
ms.SetRewardBand(ctx, msg.Changes.RewardBands)

case string(types.KeyRewardDistributionWindow):
if msg.Changes.RewardDistributionWindow < ms.Keeper.VotePeriod(ctx) {
return nil, fmt.Errorf("oracle parameter RewardDistributionWindow must be greater than or equal with VotePeriod")
}
ms.SetRewardDistributionWindow(ctx, msg.Changes.RewardDistributionWindow)

case string(types.KeyAcceptList):
accept := msg.Changes.AcceptList.Normalize()
mandatory := ms.Keeper.MandatoryList(ctx).Normalize()
if !accept.ContainDenoms(mandatory) {
return nil, fmt.Errorf("denom in MandatoryList not present in AcceptList")
}
ms.SetAcceptList(ctx, accept)

case string(types.KeyMandatoryList):
mandatory := msg.Changes.MandatoryList.Normalize()
accept := ms.Keeper.AcceptList(ctx).Normalize()
if !accept.ContainDenoms(mandatory) {
return nil, fmt.Errorf("denom in MandatoryList not present in AcceptList")
}
ms.SetMandatoryList(ctx, mandatory)

case string(types.KeySlashFraction):
ms.SetSlashFraction(ctx, msg.Changes.SlashFraction)

case string(types.KeySlashWindow):
if msg.Changes.SlashWindow < ms.Keeper.VotePeriod(ctx) {
return nil, fmt.Errorf("oracle parameter SlashWindow must be greater than or equal with VotePeriod")
}
ms.SetSlashWindow(ctx, msg.Changes.SlashWindow)

case string(types.KeyMinValidPerWindow):
ms.SetMinValidPerWindow(ctx, msg.Changes.MinValidPerWindow)

case string(types.KeyHistoricStampPeriod):
if msg.Changes.HistoricStampPeriod < 1 {
return nil, fmt.Errorf("oracle parameters HistoricStampPeriod must be greater than 0")
}
if msg.Changes.HistoricStampPeriod > ms.Keeper.MedianStampPeriod(ctx) {
return nil, fmt.Errorf("oracle parameter HistoricStampPeriod must be less than or equal with MedianStampPeriod")
}
if msg.Changes.HistoricStampPeriod%ms.Keeper.VotePeriod(ctx) != 0 {
return nil, fmt.Errorf("oracle parameters HistoricStampPeriod must be exact multiples of VotePeriod")
}
ms.SetHistoricStampPeriod(ctx, msg.Changes.HistoricStampPeriod)

case string(types.KeyMedianStampPeriod):
if msg.Changes.MedianStampPeriod < 1 {
return nil, fmt.Errorf("oracle parameters MedianStampPeriod must be greater than 0")
}
if msg.Changes.MedianStampPeriod < ms.Keeper.HistoricStampPeriod(ctx) {
return nil, fmt.Errorf("oracle parameter MedianStampPeriod must be greater than or equal with HistoricStampPeriod")
}
if msg.Changes.MedianStampPeriod%ms.Keeper.VotePeriod(ctx) != 0 {
return nil, fmt.Errorf("oracle parameters MedianStampPeriod must be exact multiples of VotePeriod")
}
ms.SetMedianStampPeriod(ctx, msg.Changes.MedianStampPeriod)

case string(types.KeyMaximumPriceStamps):
if msg.Changes.MaximumPriceStamps < 1 {
return nil, fmt.Errorf("oracle parameters MaximumPriceStamps must be greater than 0")
}
ms.SetMaximumPriceStamps(ctx, msg.Changes.MaximumPriceStamps)

case string(types.KeyMaximumMedianStamps):
if msg.Changes.MaximumMedianStamps < 1 {
return nil, fmt.Errorf("oracle parameters MaximumMedianStamps must be greater than 0")
}
ms.SetMaximumMedianStamps(ctx, msg.Changes.MaximumMedianStamps)

default:
return nil, fmt.Errorf("%s is not an existing oracle param key", key)
}
}

return &types.MsgLegacyGovUpdateParamsResponse{}, nil
}

func (ms msgServer) GovUpdateParams(
goCtx context.Context,
msg *types.MsgGovUpdateParams,
Expand Down
3 changes: 3 additions & 0 deletions x/oracle/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgAggregateExchangeRatePrevote{}, "ojo/oracle/MsgAggregateExchangeRatePrevote", nil)
cdc.RegisterConcrete(&MsgAggregateExchangeRateVote{}, "ojo/oracle/MsgAggregateExchangeRateVote", nil)
cdc.RegisterConcrete(&MsgDelegateFeedConsent{}, "ojo/oracle/MsgDelegateFeedConsent", nil)
cdc.RegisterConcrete(&MsgLegacyGovUpdateParams{}, "ojo/oracle/MsgLegacyGovUpdateParams", nil)
cdc.RegisterConcrete(&MsgGovUpdateParams{}, "ojo/oracle/MsgGovUpdateParams", nil)
cdc.RegisterConcrete(&MsgGovAddDenoms{}, "ojo/oracle/MsgGovAddDenoms", nil)
cdc.RegisterConcrete(&MsgGovRemoveCurrencyPairProviders{}, "ojo/oracle/MsgGovRemoveCurrencyPairProviders", nil)
Expand All @@ -49,6 +50,7 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
&MsgDelegateFeedConsent{},
&MsgAggregateExchangeRatePrevote{},
&MsgAggregateExchangeRateVote{},
&MsgLegacyGovUpdateParams{},
&MsgGovUpdateParams{},
&MsgGovAddDenoms{},
&MsgGovRemoveCurrencyPairProviders{},
Expand All @@ -57,6 +59,7 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {

registry.RegisterImplementations(
(*govtypes.Content)(nil),
&MsgLegacyGovUpdateParams{},
&MsgGovUpdateParams{},
&MsgGovAddDenoms{},
&MsgGovRemoveCurrencyPairProviders{},
Expand Down
117 changes: 117 additions & 0 deletions x/oracle/types/msgs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"fmt"

"cosmossdk.io/math"
"github.com/cometbft/cometbft/crypto/tmhash"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -14,6 +16,7 @@ var (
_ sdk.Msg = &MsgDelegateFeedConsent{}
_ sdk.Msg = &MsgAggregateExchangeRatePrevote{}
_ sdk.Msg = &MsgAggregateExchangeRateVote{}
_ sdk.Msg = &MsgLegacyGovUpdateParams{}
_ sdk.Msg = &MsgGovUpdateParams{}
_ sdk.Msg = &MsgGovCancelUpdateParamPlan{}
)
Expand Down Expand Up @@ -158,6 +161,120 @@ func (msg MsgDelegateFeedConsent) ValidateBasic() error {
return nil
}

// NewLegacyMsgUpdateParams will creates a new LegacyMsgUpdateParams instance
func NewLegacyMsgUpdateParams(
authority string,
title string,
description string,
keys []string,
changes Params,
) *MsgLegacyGovUpdateParams {
return &MsgLegacyGovUpdateParams{
Title: title,
Description: description,
Authority: authority,
Keys: keys,
Changes: changes,
}
}

// Type implements Msg interface
func (msg MsgLegacyGovUpdateParams) Type() string { return sdk.MsgTypeURL(&msg) }

// String implements the Stringer interface.
func (msg MsgLegacyGovUpdateParams) String() string {
out, _ := yaml.Marshal(msg)
return string(out)
}

// GetSigners implements Msg
func (msg MsgLegacyGovUpdateParams) GetSigners() []sdk.AccAddress {
return checkers.Signers(msg.Authority)
}

// ValidateBasic implements Msg and validates params for each param key
// specified in the proposal. If one param is invalid, the whole proposal
// will fail to go through.
func (msg MsgLegacyGovUpdateParams) ValidateBasic() error {
if err := checkers.ValidateProposal(msg.Title, msg.Description, msg.Authority); err != nil {
return err
}

for _, key := range msg.Keys {
switch key {
case string(KeyVotePeriod):
if err := validateVotePeriod(msg.Changes.VotePeriod); err != nil {
return err
}

case string(KeyVoteThreshold):
if err := validateVoteThreshold(msg.Changes.VoteThreshold); err != nil {
return err
}

case string(KeyRewardBands):
if err := validateRewardBands(msg.Changes.RewardBands); err != nil {
return err
}

case string(KeyRewardDistributionWindow):
if err := validateRewardDistributionWindow(msg.Changes.RewardDistributionWindow); err != nil {
return err
}

case string(KeyAcceptList):
if err := validateDenomList(msg.Changes.AcceptList); err != nil {
return err
}

case string(KeyMandatoryList):
if err := validateDenomList(msg.Changes.MandatoryList); err != nil {
return err
}

case string(KeySlashFraction):
if err := validateSlashFraction(msg.Changes.SlashFraction); err != nil {
return err
}

case string(KeySlashWindow):
if err := validateSlashWindow(msg.Changes.SlashWindow); err != nil {
return err
}

case string(KeyMinValidPerWindow):
if err := validateMinValidPerWindow(msg.Changes.MinValidPerWindow); err != nil {
return err
}

case string(KeyHistoricStampPeriod):
if err := validateHistoricStampPeriod(msg.Changes.HistoricStampPeriod); err != nil {
return err
}

case string(KeyMedianStampPeriod):
if err := validateMedianStampPeriod(msg.Changes.MedianStampPeriod); err != nil {
return err
}

case string(KeyMaximumPriceStamps):
if err := validateMaximumPriceStamps(msg.Changes.MaximumPriceStamps); err != nil {
return err
}

case string(KeyMaximumMedianStamps):
if err := validateMaximumMedianStamps(msg.Changes.MaximumMedianStamps); err != nil {
return err
}

default:
return fmt.Errorf("%s is not an existing oracle param key", key)
}
}

return nil
}

// NewMsgUpdateParams will creates a new MsgUpdateParams instance
func NewMsgUpdateParams(authority, title, description string, plan ParamUpdatePlan) *MsgGovUpdateParams {
return &MsgGovUpdateParams{
Expand Down
19 changes: 19 additions & 0 deletions x/oracle/types/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

var (
proposalTypeMsgLegacyGovUpdateParams = MsgLegacyGovUpdateParams{}.String()
proposalTypeMsgGovUpdateParams = MsgGovUpdateParams{}.String()
proposalTypeMsgGovCancelUpdateParams = MsgGovCancelUpdateParamPlan{}.String()
proposalTypeMsgGovAddDenoms = MsgGovAddDenoms{}.String()
Expand All @@ -13,13 +14,31 @@ var (
)

func init() {
gov.RegisterProposalType(proposalTypeMsgLegacyGovUpdateParams)
gov.RegisterProposalType(proposalTypeMsgGovUpdateParams)
gov.RegisterProposalType(proposalTypeMsgGovCancelUpdateParams)
gov.RegisterProposalType(proposalTypeMsgGovAddDenoms)
gov.RegisterProposalType(proposalTypeMsgGovRemoveCurrencyPairProviders)
gov.RegisterProposalType(proposalTypeMsgGovRemoveCurrencyDeviationThresholds)
}

// Implements Proposal Interface
var _ gov.Content = &MsgLegacyGovUpdateParams{}

// GetTitle returns the title of a community pool spend proposal.
func (msg *MsgLegacyGovUpdateParams) GetTitle() string { return msg.Title }

// GetDescription returns the description of a community pool spend proposal.
func (msg *MsgLegacyGovUpdateParams) GetDescription() string { return msg.Description }

// GetDescription returns the routing key of a community pool spend proposal.
func (msg *MsgLegacyGovUpdateParams) ProposalRoute() string { return RouterKey }

// ProposalType returns the type of a community pool spend proposal.
func (msg *MsgLegacyGovUpdateParams) ProposalType() string {
return proposalTypeMsgLegacyGovUpdateParams
}

// Implements Proposal Interface
var _ gov.Content = &MsgGovUpdateParams{}

Expand Down
Loading
Loading