Skip to content

Commit

Permalink
Merge PR #5737: x/gov - Proto Migration
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez authored Mar 4, 2020
2 parents 274f389 + 0cbe4e5 commit b9ab834
Show file tree
Hide file tree
Showing 38 changed files with 5,212 additions and 743 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ serialization instead of Amino.
Buffers for state serialization instead of Amino.
* The `internal` sub-package has been removed in order to expose the types proto file.
* The `x/upgrade` module now accepts a `codec.Marshaler` interface.
* (x/gov) [\#5737](https://github.com/cosmos/cosmos-sdk/pull/5737) Migrate the `x/gov` module to use Protocol
Buffers for state serialization instead of Amino.
* `MsgSubmitProposal` will be removed in favor of the application-level proto-defined `MsgSubmitProposal` which
implements the `MsgSubmitProposalI` interface. Applications should extend the `NewMsgSubmitProposalBase` type
to define their own concrete `MsgSubmitProposal` types.
* The module now accepts a `Codec` interface which extends the `codec.Marshaler` interface by
requiring a concrete codec to know how to serialize `Proposal` types.

### Improvements

Expand Down
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func NewSimApp(
AddRoute(distr.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.DistrKeeper)).
AddRoute(upgrade.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.UpgradeKeeper))
app.GovKeeper = gov.NewKeeper(
app.cdc, keys[gov.StoreKey], app.subspaces[gov.ModuleName], app.SupplyKeeper,
appCodec, keys[gov.StoreKey], app.subspaces[gov.ModuleName], app.SupplyKeeper,
&stakingKeeper, govRouter,
)

Expand Down
29 changes: 29 additions & 0 deletions simapp/codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
"github.com/cosmos/cosmos-sdk/x/evidence"
eviexported "github.com/cosmos/cosmos-sdk/x/evidence/exported"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/supply"
supplyexported "github.com/cosmos/cosmos-sdk/x/supply/exported"
)
Expand All @@ -17,6 +18,7 @@ var (
_ auth.Codec = (*Codec)(nil)
_ supply.Codec = (*Codec)(nil)
_ evidence.Codec = (*Codec)(nil)
_ gov.Codec = (*Codec)(nil)
)

// Codec defines the application-level codec. This codec contains all the
Expand Down Expand Up @@ -151,6 +153,33 @@ func (c *Codec) UnmarshalEvidenceJSON(bz []byte) (eviexported.Evidence, error) {
return evidence.GetEvidence(), nil
}

// MarshalProposal marshals a Proposal. It accepts a Proposal defined by the x/gov
// module and uses the application-level Proposal type which has the concrete
// Content implementation to serialize.
func (c *Codec) MarshalProposal(p gov.Proposal) ([]byte, error) {
proposal := &Proposal{ProposalBase: p.ProposalBase}
if err := proposal.Content.SetContent(p.Content); err != nil {
return nil, err
}

return c.Marshaler.MarshalBinaryLengthPrefixed(proposal)
}

// UnmarshalProposal decodes a Proposal defined by the x/gov module and uses the
// application-level Proposal type which has the concrete Content implementation
// to deserialize.
func (c *Codec) UnmarshalProposal(bz []byte) (gov.Proposal, error) {
proposal := &Proposal{}
if err := c.Marshaler.UnmarshalBinaryLengthPrefixed(bz, proposal); err != nil {
return gov.Proposal{}, err
}

return gov.Proposal{
Content: proposal.Content.GetContent(),
ProposalBase: proposal.ProposalBase,
}, nil
}

// ----------------------------------------------------------------------------
// necessary types and interfaces registered. This codec is provided to all the
// modules the application depends on.
Expand Down
1,813 changes: 1,556 additions & 257 deletions simapp/codec/codec.pb.go

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions simapp/codec/codec.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import "x/auth/types/types.proto";
import "x/auth/vesting/types/types.proto";
import "x/supply/types/types.proto";
import "x/evidence/types/types.proto";
import "x/gov/types/types.proto";
import "x/params/types/proposal/types.proto";
import "x/upgrade/types/types.proto";
import "x/distribution/types/types.proto";

option go_package = "github.com/cosmos/cosmos-sdk/simapp/codec";

Expand Down Expand Up @@ -56,3 +60,38 @@ message MsgSubmitEvidence {
Evidence evidence = 1;
cosmos_sdk.x.evidence.v1.MsgSubmitEvidenceBase base = 2 [(gogoproto.nullable) = false, (gogoproto.embed) = true];
}

// MsgSubmitProposal defines the application-level message type for handling
// governance proposals.
message MsgSubmitProposal {
option (gogoproto.equal) = true;
option (gogoproto.goproto_getters) = false;

cosmos_sdk.x.gov.v1.MsgSubmitProposalBase base = 1 [(gogoproto.nullable) = false, (gogoproto.embed) = true];
Content content = 2;
}

// Proposal defines the application-level concrete proposal type used in governance
// proposals.
message Proposal {
option (gogoproto.equal) = true;

cosmos_sdk.x.gov.v1.ProposalBase base = 1 [(gogoproto.embed) = true, (gogoproto.nullable) = false];
Content content = 2 [(gogoproto.nullable) = false];
}

// Content defines the application-level allowed Content to be included in a
// governance proposal.
message Content {
option (gogoproto.equal) = true;
option (cosmos_proto.interface_type) = "github.com/cosmos/cosmos-sdk/x/gov/types.Content";

// sum defines a set of all acceptable concrete governance proposal Content types.
oneof sum {
cosmos_sdk.x.gov.v1.TextProposal text = 1;
cosmos_sdk.x.params.v1.ParameterChangeProposal parameter_change = 2;
cosmos_sdk.x.upgrade.v1.SoftwareUpgradeProposal software_upgrade = 3;
cosmos_sdk.x.upgrade.v1.CancelSoftwareUpgradeProposal cancel_software_upgrade = 4;
cosmos_sdk.x.distribution.v1.CommunityPoolSpendProposal community_pool_spend = 5;
}
}
48 changes: 46 additions & 2 deletions simapp/codec/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/evidence"
eviexported "github.com/cosmos/cosmos-sdk/x/evidence/exported"
"github.com/cosmos/cosmos-sdk/x/gov"
)

var _ eviexported.MsgSubmitEvidence = MsgSubmitEvidence{}
var (
_ eviexported.MsgSubmitEvidence = MsgSubmitEvidence{}
_ gov.MsgSubmitProposalI = MsgSubmitProposal{}
)

// NewMsgSubmitEvidence returns a new MsgSubmitEvidence.
func NewMsgSubmitEvidence(evidenceI eviexported.Evidence, s sdk.AccAddress) (MsgSubmitEvidence, error) {
Expand All @@ -16,7 +20,10 @@ func NewMsgSubmitEvidence(evidenceI eviexported.Evidence, s sdk.AccAddress) (Msg
return MsgSubmitEvidence{}, err
}

return MsgSubmitEvidence{Evidence: e, MsgSubmitEvidenceBase: evidence.NewMsgSubmitEvidenceBase(s)}, nil
return MsgSubmitEvidence{
Evidence: e,
MsgSubmitEvidenceBase: evidence.NewMsgSubmitEvidenceBase(s),
}, nil
}

// ValidateBasic performs basic (non-state-dependant) validation on a
Expand All @@ -38,3 +45,40 @@ func (msg MsgSubmitEvidence) ValidateBasic() error {
// nolint
func (msg MsgSubmitEvidence) GetEvidence() eviexported.Evidence { return msg.Evidence.GetEvidence() }
func (msg MsgSubmitEvidence) GetSubmitter() sdk.AccAddress { return msg.Submitter }

// NewMsgSubmitProposal returns a new MsgSubmitProposal.
func NewMsgSubmitProposal(c gov.Content, d sdk.Coins, p sdk.AccAddress) (MsgSubmitProposal, error) {
content := &Content{}
if err := content.SetContent(c); err != nil {
return MsgSubmitProposal{}, err
}

return MsgSubmitProposal{
Content: content,
MsgSubmitProposalBase: gov.NewMsgSubmitProposalBase(d, p),
}, nil
}

// ValidateBasic performs basic (non-state-dependant) validation on a
// MsgSubmitProposal.
func (msg MsgSubmitProposal) ValidateBasic() error {
if err := msg.MsgSubmitProposalBase.ValidateBasic(); err != nil {
return nil
}
if msg.Content == nil {
return sdkerrors.Wrap(gov.ErrInvalidProposalContent, "missing content")
}
if !gov.IsValidProposalType(msg.Content.GetContent().ProposalType()) {
return sdkerrors.Wrap(gov.ErrInvalidProposalType, msg.Content.GetContent().ProposalType())
}
if err := msg.Content.GetContent().ValidateBasic(); err != nil {
return err
}

return nil
}

// nolint
func (msg MsgSubmitProposal) GetContent() gov.Content { return msg.Content.GetContent() }
func (msg MsgSubmitProposal) GetInitialDeposit() sdk.Coins { return msg.InitialDeposit }
func (msg MsgSubmitProposal) GetProposer() sdk.AccAddress { return msg.Proposer }
Loading

0 comments on commit b9ab834

Please sign in to comment.