|
| 1 | +package ante |
| 2 | + |
| 3 | +import ( |
| 4 | + errorsmod "cosmossdk.io/errors" |
| 5 | + |
| 6 | + "github.com/cosmos/cosmos-sdk/codec" |
| 7 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 8 | + govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" |
| 9 | + |
| 10 | + gaiaerrors "github.com/cosmos/gaia/v18/types/errors" |
| 11 | +) |
| 12 | + |
| 13 | +var expeditedPropDecoratorEnabled = true |
| 14 | + |
| 15 | +// SetExpeditedProposalsEnabled toggles the expedited proposals decorator on/off. |
| 16 | +// Should only be used in testing - this is to allow simtests to pass. |
| 17 | +func SetExpeditedProposalsEnabled(val bool) { |
| 18 | + expeditedPropDecoratorEnabled = val |
| 19 | +} |
| 20 | + |
| 21 | +var expeditedPropsWhitelist = map[string]struct{}{ |
| 22 | + "/cosmos.upgrade.v1beta1.MsgSoftwareUpgrade": {}, |
| 23 | + "/cosmos.upgrade.v1beta1.MsgCancelUpgrade": {}, |
| 24 | + // legacy proposals can still be submitted using govv1.MsgSubmitProposal |
| 25 | + "/cosmos.upgrade.v1beta1.SoftwareUpgradeProposal": {}, |
| 26 | + "/cosmos.upgrade.v1beta1.CancelSoftwareUpgradeProposal": {}, |
| 27 | +} |
| 28 | + |
| 29 | +// Check if the proposal is whitelisted for expedited voting. |
| 30 | +type GovExpeditedProposalsDecorator struct { |
| 31 | + cdc codec.BinaryCodec |
| 32 | +} |
| 33 | + |
| 34 | +func NewGovExpeditedProposalsDecorator(cdc codec.BinaryCodec) GovExpeditedProposalsDecorator { |
| 35 | + return GovExpeditedProposalsDecorator{ |
| 36 | + cdc: cdc, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// AnteHandle checks if the proposal is whitelisted for expedited voting. |
| 41 | +// Only proposals submitted using "gaiad tx gov submit-proposal" can be expedited. |
| 42 | +// Legacy proposals submitted using "gaiad tx gov submit-legacy-proposal" cannot be marked as expedited. |
| 43 | +func (g GovExpeditedProposalsDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { |
| 44 | + if expeditedPropDecoratorEnabled { |
| 45 | + for _, msg := range tx.GetMsgs() { |
| 46 | + prop, ok := msg.(*govv1.MsgSubmitProposal) |
| 47 | + if !ok { |
| 48 | + continue |
| 49 | + } |
| 50 | + if prop.Expedited { |
| 51 | + if err := g.validateExpeditedGovProp(prop); err != nil { |
| 52 | + return ctx, err |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + return next(ctx, tx, simulate) |
| 58 | +} |
| 59 | + |
| 60 | +func (g GovExpeditedProposalsDecorator) isWhitelisted(msgType string) bool { |
| 61 | + _, ok := expeditedPropsWhitelist[msgType] |
| 62 | + return ok |
| 63 | +} |
| 64 | + |
| 65 | +func (g GovExpeditedProposalsDecorator) validateExpeditedGovProp(prop *govv1.MsgSubmitProposal) error { |
| 66 | + msgs := prop.GetMessages() |
| 67 | + if len(msgs) == 0 { |
| 68 | + return gaiaerrors.ErrInvalidExpeditedProposal |
| 69 | + } |
| 70 | + for _, message := range msgs { |
| 71 | + // in case of legacy content submitted using govv1.MsgSubmitProposal |
| 72 | + if sdkMsg, isLegacy := message.GetCachedValue().(*govv1.MsgExecLegacyContent); isLegacy { |
| 73 | + if !g.isWhitelisted(sdkMsg.Content.TypeUrl) { |
| 74 | + return errorsmod.Wrapf(gaiaerrors.ErrInvalidExpeditedProposal, "invalid Msg type: %s", sdkMsg.Content.TypeUrl) |
| 75 | + } |
| 76 | + continue |
| 77 | + } |
| 78 | + if !g.isWhitelisted(message.TypeUrl) { |
| 79 | + return errorsmod.Wrapf(gaiaerrors.ErrInvalidExpeditedProposal, "invalid Msg type: %s", message.TypeUrl) |
| 80 | + } |
| 81 | + } |
| 82 | + return nil |
| 83 | +} |
0 commit comments