-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
15 changed files
with
605 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ante | ||
|
||
import ( | ||
"cosmossdk.io/errors" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
gov "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" | ||
) | ||
|
||
type GovProposalDecorator struct{} | ||
|
||
func NewGovProposalDecorator() GovProposalDecorator { | ||
return GovProposalDecorator{} | ||
} | ||
|
||
// AnteHandle implements the AnteHandler interface. It ensures that MsgSubmitProposal has at least one message | ||
func (d GovProposalDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) { | ||
for _, m := range tx.GetMsgs() { | ||
if proposal, ok := m.(*govv1.MsgSubmitProposal); ok { | ||
if len(proposal.Messages) == 0 { | ||
return ctx, errors.Wrapf(gov.ErrNoProposalMsgs, "must include at least one message in proposal") | ||
} | ||
} | ||
} | ||
|
||
return next(ctx, tx, simulate) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package ante_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/celestiaorg/celestia-app/app" | ||
"github.com/celestiaorg/celestia-app/app/ante" | ||
"github.com/celestiaorg/celestia-app/app/encoding" | ||
"github.com/celestiaorg/celestia-app/test/util/testfactory" | ||
"github.com/cosmos/cosmos-sdk/types" | ||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGovDecorator(t *testing.T) { | ||
decorator := ante.NewGovProposalDecorator() | ||
anteHandler := types.ChainAnteDecorators(decorator) | ||
accounts := testfactory.GenerateAccounts(1) | ||
coins := types.NewCoins(types.NewCoin("utia", types.NewInt(10))) | ||
|
||
msgSend := banktypes.NewMsgSend( | ||
testfactory.RandomAddress().(types.AccAddress), | ||
testfactory.RandomAddress().(types.AccAddress), | ||
coins, | ||
) | ||
encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...) | ||
|
||
msgProposal, err := govtypes.NewMsgSubmitProposal([]types.Msg{msgSend}, coins, accounts[0], "") | ||
require.NoError(t, err) | ||
msgEmptyProposal, err := govtypes.NewMsgSubmitProposal([]types.Msg{}, coins, accounts[0], "do nothing") | ||
require.NoError(t, err) | ||
|
||
testCases := []struct { | ||
name string | ||
msg []types.Msg | ||
expErr bool | ||
}{ | ||
{ | ||
name: "good proposal; has at least one message", | ||
msg: []types.Msg{msgProposal}, | ||
expErr: false, | ||
}, | ||
{ | ||
name: "bad proposal; has no messages", | ||
msg: []types.Msg{msgEmptyProposal}, | ||
expErr: true, | ||
}, | ||
{ | ||
name: "good proposal; multiple messages in tx", | ||
msg: []types.Msg{msgProposal, msgSend}, | ||
expErr: false, | ||
}, | ||
{ | ||
name: "bad proposal; multiple messages in tx but proposal has no messages", | ||
msg: []types.Msg{msgEmptyProposal, msgSend}, | ||
expErr: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
ctx := types.Context{} | ||
builder := encCfg.TxConfig.NewTxBuilder() | ||
require.NoError(t, builder.SetMsgs(tc.msg...)) | ||
tx := builder.GetTx() | ||
_, err := anteHandler(ctx, tx, false) | ||
if tc.expErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.