|
| 1 | +package types |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + errorsmod "cosmossdk.io/errors" |
| 7 | + upgradetypes "cosmossdk.io/x/upgrade/types" |
| 8 | + |
| 9 | + codectypes "github.com/cosmos/cosmos-sdk/codec/types" |
| 10 | + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" |
| 11 | + |
| 12 | + "github.com/cosmos/ibc-go/v10/modules/core/exported" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + // ProposalTypeClientUpdate defines the type for a ClientUpdateProposal |
| 17 | + ProposalTypeClientUpdate = "ClientUpdate" |
| 18 | + // ProposalTypeUpgrade defines the type for an UpgradeProposal |
| 19 | + ProposalTypeUpgrade = "IBCUpgrade" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + _ govtypes.Content = &ClientUpdateProposal{} |
| 24 | + _ govtypes.Content = &UpgradeProposal{} |
| 25 | + _ codectypes.UnpackInterfacesMessage = &UpgradeProposal{} |
| 26 | +) |
| 27 | + |
| 28 | +// func init() { |
| 29 | +// govtypes.RegisterProposalType(ProposalTypeClientUpdate) |
| 30 | +// govtypes.RegisterProposalType(ProposalTypeUpgrade) |
| 31 | +// } |
| 32 | + |
| 33 | +// NewClientUpdateProposal creates a new client update proposal. |
| 34 | +// |
| 35 | +// Deprecated: The legacy v1beta1 gov ClientUpdateProposal is deprecated |
| 36 | +// and will be removed in a future release. Please use MsgRecoverClient instead. |
| 37 | +func NewClientUpdateProposal(title, description, subjectClientID, substituteClientID string) govtypes.Content { |
| 38 | + return &ClientUpdateProposal{ |
| 39 | + Title: title, |
| 40 | + Description: description, |
| 41 | + SubjectClientId: subjectClientID, |
| 42 | + SubstituteClientId: substituteClientID, |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// GetTitle returns the title of a client update proposal. |
| 47 | +func (cup *ClientUpdateProposal) GetTitle() string { return cup.Title } |
| 48 | + |
| 49 | +// GetDescription returns the description of a client update proposal. |
| 50 | +func (cup *ClientUpdateProposal) GetDescription() string { return cup.Description } |
| 51 | + |
| 52 | +// ProposalRoute returns the routing key of a client update proposal. |
| 53 | +func (*ClientUpdateProposal) ProposalRoute() string { return RouterKey } |
| 54 | + |
| 55 | +// ProposalType returns the type of a client update proposal. |
| 56 | +func (*ClientUpdateProposal) ProposalType() string { return ProposalTypeClientUpdate } |
| 57 | + |
| 58 | +// ValidateBasic runs basic stateless validity checks |
| 59 | +func (cup *ClientUpdateProposal) ValidateBasic() error { |
| 60 | + err := govtypes.ValidateAbstract(cup) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + if cup.SubjectClientId == cup.SubstituteClientId { |
| 66 | + return errorsmod.Wrap(ErrInvalidSubstitute, "subject and substitute client identifiers are equal") |
| 67 | + } |
| 68 | + if _, _, err := ParseClientIdentifier(cup.SubjectClientId); err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + if _, _, err := ParseClientIdentifier(cup.SubstituteClientId); err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +// NewUpgradeProposal creates a new IBC breaking upgrade proposal. |
| 79 | +// |
| 80 | +// Deprecated: The legacy v1beta1 gov UpgradeProposal is deprecated |
| 81 | +// and will be removed in a future release. Please use MsgIBCSoftwareUpgrade instead. |
| 82 | +func NewUpgradeProposal(title, description string, plan upgradetypes.Plan, upgradedClientState exported.ClientState) (govtypes.Content, error) { |
| 83 | + clientAny, err := PackClientState(upgradedClientState) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + return &UpgradeProposal{ |
| 89 | + Title: title, |
| 90 | + Description: description, |
| 91 | + Plan: plan, |
| 92 | + UpgradedClientState: clientAny, |
| 93 | + }, nil |
| 94 | +} |
| 95 | + |
| 96 | +// GetTitle returns the title of a upgrade proposal. |
| 97 | +func (up *UpgradeProposal) GetTitle() string { return up.Title } |
| 98 | + |
| 99 | +// GetDescription returns the description of a upgrade proposal. |
| 100 | +func (up *UpgradeProposal) GetDescription() string { return up.Description } |
| 101 | + |
| 102 | +// ProposalRoute returns the routing key of a upgrade proposal. |
| 103 | +func (*UpgradeProposal) ProposalRoute() string { return RouterKey } |
| 104 | + |
| 105 | +// ProposalType returns the upgrade proposal type. |
| 106 | +func (*UpgradeProposal) ProposalType() string { return ProposalTypeUpgrade } |
| 107 | + |
| 108 | +// ValidateBasic runs basic stateless validity checks |
| 109 | +func (up *UpgradeProposal) ValidateBasic() error { |
| 110 | + if err := govtypes.ValidateAbstract(up); err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + |
| 114 | + if err := up.Plan.ValidateBasic(); err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + |
| 118 | + if up.UpgradedClientState == nil { |
| 119 | + return errorsmod.Wrap(ErrInvalidUpgradeProposal, "upgraded client state cannot be nil") |
| 120 | + } |
| 121 | + |
| 122 | + _, err := UnpackClientState(up.UpgradedClientState) |
| 123 | + if err != nil { |
| 124 | + return errorsmod.Wrap(err, "failed to unpack upgraded client state") |
| 125 | + } |
| 126 | + |
| 127 | + return nil |
| 128 | +} |
| 129 | + |
| 130 | +// String returns the string representation of the UpgradeProposal. |
| 131 | +func (up UpgradeProposal) String() string { |
| 132 | + var upgradedClientStr string |
| 133 | + upgradedClient, err := UnpackClientState(up.UpgradedClientState) |
| 134 | + if err != nil { |
| 135 | + upgradedClientStr = "invalid IBC Client State" |
| 136 | + } else { |
| 137 | + upgradedClientStr = upgradedClient.String() |
| 138 | + } |
| 139 | + |
| 140 | + return fmt.Sprintf(`IBC Upgrade Proposal |
| 141 | + Title: %s |
| 142 | + Description: %s |
| 143 | + %s |
| 144 | + Upgraded IBC Client: %s`, up.Title, up.Description, up.Plan.String(), upgradedClientStr) |
| 145 | +} |
| 146 | + |
| 147 | +// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces |
| 148 | +func (up UpgradeProposal) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { |
| 149 | + return unpacker.UnpackAny(up.UpgradedClientState, new(exported.ClientState)) |
| 150 | +} |
0 commit comments