Skip to content

Commit

Permalink
refactor: x/consensus audit changes (#13949)
Browse files Browse the repository at this point in the history
  • Loading branch information
amaury1093 authored Nov 21, 2022
1 parent 82b64e6 commit f124cb7
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 38 deletions.
2 changes: 2 additions & 0 deletions proto/cosmos/consensus/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ message MsgUpdateParams {
string authority = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];

// params defines the x/consensus_params parameters to update.
// VersionsParams is not included in this Msg because it is tracked
// separarately in x/upgrade.
//
// NOTE: All parameters must be supplied.
tendermint.types.BlockParams block = 2;
Expand Down
4 changes: 2 additions & 2 deletions x/consensus/exported/exported.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type (
Get(ctx sdk.Context, key []byte, ptr interface{})
}

// ConsensusParamSetter defines the interface fulfilled by BaseApp
// which allows setting its appVersion field.
// ConsensusParamSetter defines the interface fulfilled by BaseApp's
// ParamStore which allows setting its appVersion field.
ConsensusParamSetter interface {
Get(ctx sdk.Context) (*tmproto.ConsensusParams, error)
Has(ctx sdk.Context) bool
Expand Down
2 changes: 1 addition & 1 deletion x/consensus/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (k msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParam
ctx := sdk.UnwrapSDKContext(goCtx)

consensusParams := req.ToProtoConsensusParams()
if err := types.Validate(tmtypes.ConsensusParamsFromProto(consensusParams)); err != nil {
if err := tmtypes.ConsensusParamsFromProto(consensusParams).ValidateBasic(); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion x/consensus/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (s *KeeperTestSuite) TestUpdateParams() {
expErrMsg: "block.MaxBytes must be greater than 0. Got -10",
},
{
name: "invalid authority",
name: "invalid authority",
input: &types.MsgUpdateParams{
Authority: "invalid",
Block: defaultConsensusParams.Block,
Expand Down
6 changes: 4 additions & 2 deletions x/consensus/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
)

// ConsensusVersion defines the current x/bank module consensus version.
// ConsensusVersion defines the current x/consensus module consensus version.
const ConsensusVersion = 1

var (
Expand All @@ -42,7 +42,9 @@ type AppModuleBasic struct {
func (AppModuleBasic) Name() string { return types.ModuleName }

// RegisterLegacyAminoCodec registers the consensus_param module's types on the LegacyAmino codec.
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {}
func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
types.RegisterLegacyAminoCodec(cdc)
}

// DefaultGenesis returns default genesis state as raw bytes for the consensus_param
// module.
Expand Down
29 changes: 29 additions & 0 deletions x/consensus/types/codec.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package types

import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/legacy"
"github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec"
govcodec "github.com/cosmos/cosmos-sdk/x/gov/codec"
groupcodec "github.com/cosmos/cosmos-sdk/x/group/codec"
)

func RegisterInterfaces(registry types.InterfaceRegistry) {
Expand All @@ -14,3 +20,26 @@ func RegisterInterfaces(registry types.InterfaceRegistry) {

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}

// RegisterLegacyAminoCodec registers the necessary x/consensus interfaces and concrete types
// on the provided LegacyAmino codec. These types are used for Amino JSON serialization.
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
legacy.RegisterAminoMsg(cdc, &MsgUpdateParams{}, "cosmos-sdk/x/consensus/MsgUpdateParams")
}

var (
amino = codec.NewLegacyAmino()
ModuleCdc = codec.NewAminoCodec(amino)
)

func init() {
RegisterLegacyAminoCodec(amino)
cryptocodec.RegisterCrypto(amino)
sdk.RegisterLegacyAminoCodec(amino)

// Register all Amino interfaces and concrete types on the authz and gov Amino codec so that this can later be
// used to properly serialize MsgUpdate instances
RegisterLegacyAminoCodec(authzcodec.Amino)
RegisterLegacyAminoCodec(govcodec.Amino)
RegisterLegacyAminoCodec(groupcodec.Amino)
}
17 changes: 13 additions & 4 deletions x/consensus/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
tmtypes "github.com/tendermint/tendermint/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
)

// bank message types
const (
TypeMsgUpdateParams = "update_params"
)

var _ sdk.Msg = &MsgUpdateParams{}
var _ legacytx.LegacyMsg = &MsgUpdateParams{}

// GetSigners returns the signer addresses that are expected to sign the result
// of GetSignBytes.
Expand All @@ -24,13 +25,21 @@ func (msg MsgUpdateParams) GetSigners() []sdk.AccAddress {
// GetSignBytes returns the raw bytes for a MsgUpdateParams message that
// the expected signer needs to sign.
func (msg MsgUpdateParams) GetSignBytes() []byte {
return []byte{}
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}

func (msg MsgUpdateParams) Route() string {
return sdk.MsgTypeURL(&msg)
}

func (msg MsgUpdateParams) Type() string {
return sdk.MsgTypeURL(&msg)
}

// ValidateBasic performs basic MsgUpdateParams message validation.
func (msg MsgUpdateParams) ValidateBasic() error {
params := tmtypes.ConsensusParamsFromProto(msg.ToProtoConsensusParams())
return Validate(params)
return params.ValidateBasic()
}

func (msg MsgUpdateParams) ToProtoConsensusParams() tmproto.ConsensusParams {
Expand All @@ -47,6 +56,6 @@ func (msg MsgUpdateParams) ToProtoConsensusParams() tmproto.ConsensusParams {
Validator: &tmproto.ValidatorParams{
PubKeyTypes: msg.Validator.PubKeyTypes,
},
Version: tmtypes.DefaultConsensusParams().ToProto().Version,
Version: tmtypes.DefaultConsensusParams().ToProto().Version, // Version is stored in x/upgrade
}
}
11 changes: 0 additions & 11 deletions x/consensus/types/params.go

This file was deleted.

17 changes: 0 additions & 17 deletions x/consensus/types/util.go

This file was deleted.

0 comments on commit f124cb7

Please sign in to comment.