refactor: x/consensus audit changes (#13949) (#13952)

(cherry picked from commit f124cb71aec67a2a48ad550b9f1afa3d861cdeaf)

Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com>
This commit is contained in:
mergify[bot] 2022-11-21 17:46:20 +01:00 committed by GitHub
parent 121ae53499
commit 12fae01a25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 52 additions and 38 deletions

View File

@ -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;

View File

@ -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

View File

@ -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
}

View File

@ -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 (
@ -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.

View File

@ -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) {
@ -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)
}

View File

@ -5,6 +5,7 @@ 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
@ -12,7 +13,7 @@ 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.
@ -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 {
@ -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
}
}

View File

@ -1,11 +0,0 @@
package types
import (
tmtypes "github.com/tendermint/tendermint/types"
)
// Validate performs basic validation of ConsensusParams returning an error upon
// failure.
func Validate(p tmtypes.ConsensusParams) error {
return p.ValidateBasic()
}

View File

@ -1,17 +0,0 @@
package types
import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// Sentinel errors for the x/consensus module.
var (
ErrUnauthorized = sdkerrors.Register(ModuleName, 2, "unauthorized action")
)
// Events
const (
EventTypeUpdateParam = "update_param"
AttributeKeyParamUpdater = "param_updater"
)