Move pkg to std/

This commit is contained in:
Aleksandr Bezobchuk
2020-03-05 09:32:43 -05:00
parent 9acd722db2
commit 3f2f4b984e
5 changed files with 79 additions and 79 deletions
+198
View File
@@ -0,0 +1,198 @@
package std
import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
authexported "github.com/cosmos/cosmos-sdk/x/auth/exported"
"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"
)
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
// required module-specific codecs that are to be provided upon initialization.
type Codec struct {
codec.Marshaler
// Keep reference to the amino codec to allow backwards compatibility along
// with type, and interface registration.
amino *codec.Codec
}
func NewAppCodec(amino *codec.Codec) *Codec {
return &Codec{Marshaler: codec.NewHybridCodec(amino), amino: amino}
}
// MarshalAccount marshals an Account interface. If the given type implements
// the Marshaler interface, it is treated as a Proto-defined message and
// serialized that way. Otherwise, it falls back on the internal Amino codec.
func (c *Codec) MarshalAccount(accI authexported.Account) ([]byte, error) {
acc := &Account{}
if err := acc.SetAccount(accI); err != nil {
return nil, err
}
return c.Marshaler.MarshalBinaryLengthPrefixed(acc)
}
// UnmarshalAccount returns an Account interface from raw encoded account bytes
// of a Proto-based Account type. An error is returned upon decoding failure.
func (c *Codec) UnmarshalAccount(bz []byte) (authexported.Account, error) {
acc := &Account{}
if err := c.Marshaler.UnmarshalBinaryLengthPrefixed(bz, acc); err != nil {
return nil, err
}
return acc.GetAccount(), nil
}
// MarshalAccountJSON JSON encodes an account object implementing the Account
// interface.
func (c *Codec) MarshalAccountJSON(acc authexported.Account) ([]byte, error) {
return c.Marshaler.MarshalJSON(acc)
}
// UnmarshalAccountJSON returns an Account from JSON encoded bytes.
func (c *Codec) UnmarshalAccountJSON(bz []byte) (authexported.Account, error) {
acc := &Account{}
if err := c.Marshaler.UnmarshalJSON(bz, acc); err != nil {
return nil, err
}
return acc.GetAccount(), nil
}
// MarshalSupply marshals a SupplyI interface. If the given type implements
// the Marshaler interface, it is treated as a Proto-defined message and
// serialized that way. Otherwise, it falls back on the internal Amino codec.
func (c *Codec) MarshalSupply(supplyI supplyexported.SupplyI) ([]byte, error) {
supply := &Supply{}
if err := supply.SetSupplyI(supplyI); err != nil {
return nil, err
}
return c.Marshaler.MarshalBinaryLengthPrefixed(supply)
}
// UnmarshalSupply returns a SupplyI interface from raw encoded account bytes
// of a Proto-based SupplyI type. An error is returned upon decoding failure.
func (c *Codec) UnmarshalSupply(bz []byte) (supplyexported.SupplyI, error) {
supply := &Supply{}
if err := c.Marshaler.UnmarshalBinaryLengthPrefixed(bz, supply); err != nil {
return nil, err
}
return supply.GetSupplyI(), nil
}
// MarshalSupplyJSON JSON encodes a supply object implementing the SupplyI
// interface.
func (c *Codec) MarshalSupplyJSON(supply supplyexported.SupplyI) ([]byte, error) {
return c.Marshaler.MarshalJSON(supply)
}
// UnmarshalSupplyJSON returns a SupplyI from JSON encoded bytes.
func (c *Codec) UnmarshalSupplyJSON(bz []byte) (supplyexported.SupplyI, error) {
supply := &Supply{}
if err := c.Marshaler.UnmarshalJSON(bz, supply); err != nil {
return nil, err
}
return supply.GetSupplyI(), nil
}
// MarshalEvidence marshals an Evidence interface. If the given type implements
// the Marshaler interface, it is treated as a Proto-defined message and
// serialized that way. Otherwise, it falls back on the internal Amino codec.
func (c *Codec) MarshalEvidence(evidenceI eviexported.Evidence) ([]byte, error) {
evidence := &Evidence{}
if err := evidence.SetEvidence(evidenceI); err != nil {
return nil, err
}
return c.Marshaler.MarshalBinaryLengthPrefixed(evidence)
}
// UnmarshalEvidence returns an Evidence interface from raw encoded evidence
// bytes of a Proto-based Evidence type. An error is returned upon decoding
// failure.
func (c *Codec) UnmarshalEvidence(bz []byte) (eviexported.Evidence, error) {
evidence := &Evidence{}
if err := c.Marshaler.UnmarshalBinaryLengthPrefixed(bz, evidence); err != nil {
return nil, err
}
return evidence.GetEvidence(), nil
}
// MarshalEvidenceJSON JSON encodes an evidence object implementing the Evidence
// interface.
func (c *Codec) MarshalEvidenceJSON(evidence eviexported.Evidence) ([]byte, error) {
return c.Marshaler.MarshalJSON(evidence)
}
// UnmarshalEvidenceJSON returns an Evidence from JSON encoded bytes
func (c *Codec) UnmarshalEvidenceJSON(bz []byte) (eviexported.Evidence, error) {
evidence := &Evidence{}
if err := c.Marshaler.UnmarshalJSON(bz, evidence); err != nil {
return nil, err
}
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.
//
// NOTE: This codec will be deprecated in favor of AppCodec once all modules are
// migrated.
func MakeCodec(bm module.BasicManager) *codec.Codec {
cdc := codec.New()
bm.RegisterCodec(cdc)
vesting.RegisterCodec(cdc)
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
return cdc
}
File diff suppressed because it is too large Load Diff
+97
View File
@@ -0,0 +1,97 @@
syntax = "proto3";
package cosmos_sdk.codec.std.v1;
import "third_party/proto/cosmos-proto/cosmos.proto";
import "third_party/proto/gogoproto/gogo.proto";
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/codec/std";
// Account defines the application-level Account type.
message Account {
option (cosmos_proto.interface_type) = "*github.com/cosmos/cosmos-sdk/x/auth/exported.Account";
// sum defines a list of all acceptable concrete Account implementations.
oneof sum {
cosmos_sdk.x.auth.v1.BaseAccount base_account = 1;
cosmos_sdk.x.auth.vesting.v1.ContinuousVestingAccount continuous_vesting_account = 2;
cosmos_sdk.x.auth.vesting.v1.DelayedVestingAccount delayed_vesting_account = 3;
cosmos_sdk.x.auth.vesting.v1.PeriodicVestingAccount periodic_vesting_account = 4;
cosmos_sdk.x.supply.v1.ModuleAccount module_account = 5;
}
}
// Supply defines the application-level Supply type.
message Supply {
option (gogoproto.equal) = true;
option (cosmos_proto.interface_type) = "*github.com/cosmos/cosmos-sdk/x/supply/exported.SupplyI";
// sum defines a set of all acceptable concrete Supply implementations.
oneof sum {
cosmos_sdk.x.supply.v1.Supply supply = 1;
}
}
// Evidence defines the application-level allowed Evidence to be submitted via a
// MsgSubmitEvidence message.
message Evidence {
option (gogoproto.equal) = true;
option (cosmos_proto.interface_type) = "github.com/cosmos/cosmos-sdk/x/evidence/exported.Evidence";
// sum defines a set of all acceptable concrete Evidence implementations.
oneof sum {
cosmos_sdk.x.evidence.v1.Equivocation equivocation = 1;
}
}
// MsgSubmitEvidence defines the application-level message type for handling
// evidence submission.
message MsgSubmitEvidence {
option (gogoproto.equal) = true;
option (gogoproto.goproto_getters) = false;
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;
}
}
+84
View File
@@ -0,0 +1,84 @@
package std
import (
sdk "github.com/cosmos/cosmos-sdk/types"
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{}
_ gov.MsgSubmitProposalI = MsgSubmitProposal{}
)
// NewMsgSubmitEvidence returns a new MsgSubmitEvidence.
func NewMsgSubmitEvidence(evidenceI eviexported.Evidence, s sdk.AccAddress) (MsgSubmitEvidence, error) {
e := &Evidence{}
if err := e.SetEvidence(evidenceI); err != nil {
return MsgSubmitEvidence{}, err
}
return MsgSubmitEvidence{
Evidence: e,
MsgSubmitEvidenceBase: evidence.NewMsgSubmitEvidenceBase(s),
}, nil
}
// ValidateBasic performs basic (non-state-dependant) validation on a
// MsgSubmitEvidence.
func (msg MsgSubmitEvidence) ValidateBasic() error {
if err := msg.MsgSubmitEvidenceBase.ValidateBasic(); err != nil {
return nil
}
if msg.Evidence == nil {
return sdkerrors.Wrap(evidence.ErrInvalidEvidence, "missing evidence")
}
if err := msg.Evidence.GetEvidence().ValidateBasic(); err != nil {
return err
}
return nil
}
// 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 }