Skip to content

Commit

Permalink
begin app.go
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Feb 7, 2025
1 parent 62cd901 commit 6606764
Show file tree
Hide file tree
Showing 17 changed files with 411 additions and 1,622 deletions.
7 changes: 2 additions & 5 deletions app/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ func NewAnteHandler(
sigGasConsumer ante.SignatureVerificationGasConsumer,
channelKeeper *ibckeeper.Keeper,
paramKeeper paramkeeper.Keeper,
msgVersioningGateKeeper *MsgVersioningGateKeeper,
forbiddenGovUpdateParams map[string][]string,
) sdk.AnteHandler {
return sdk.ChainAnteDecorators(
// Wraps the panic with the string format of the transaction
NewHandlePanicDecorator(),
// Prevents messages that don't belong to the correct app version
// from being executed
msgVersioningGateKeeper,
// Set up the context with a gas meter.
// Must be called before gas consumption occurs in any other decorator.
ante.NewSetUpContextDecorator(),
Expand Down Expand Up @@ -75,7 +72,7 @@ func NewAnteHandler(
blobante.NewBlobShareDecorator(blobKeeper),
// Ensure that tx's with a MsgSubmitProposal have at least one proposal
// message.
NewGovProposalDecorator(),
NewGovProposalDecorator(forbiddenGovUpdateParams),
// Side effect: increment the nonce for all tx signers.
ante.NewIncrementSequenceDecorator(accountKeeper),
// Ensure that the tx is not an IBC packet or update message that has already been processed.
Expand Down
78 changes: 72 additions & 6 deletions app/ante/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,94 @@ package ante

import (
"cosmossdk.io/errors"
gogoany "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"
gov "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
"github.com/cosmos/cosmos-sdk/x/params/types/proposal"
gogoproto "github.com/cosmos/gogoproto/proto"
)

// GovProposalDecorator ensures that a tx with a MsgSubmitProposal has at least
// one message in the proposal.
type GovProposalDecorator struct{}
// GovProposalDecorator ensures that a tx with a MsgSubmitProposal has at least one message in the proposal.
// Additionally it replace the x/paramfilter module that existed in v3 and earlier versions.
type GovProposalDecorator struct {
// forbiddenGovUpdateParams is a map of type_url to a list of parameter fiels that cannot be changed via governance.
forbiddenGovUpdateParams map[string][]string
}

func NewGovProposalDecorator() GovProposalDecorator {
return GovProposalDecorator{}
func NewGovProposalDecorator(forbiddenParams map[string][]string) GovProposalDecorator {
return GovProposalDecorator{forbiddenParams}
}

// AnteHandle implements the AnteHandler interface. It ensures that MsgSubmitProposal has at least one message
// AnteHandle implements the AnteHandler interface.
// It ensures that MsgSubmitProposal has at least one message
// It ensures params are filtered within messages
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")
}

if err := d.checkNestedMsgs(proposal.Messages); err != nil {
return ctx, err
}
}

// we need to check if a gov proposal wasn't contained in a authz message
if msgExec, ok := m.(*authz.MsgExec); ok {
if err := d.checkNestedMsgs(msgExec.Msgs); err != nil {
return ctx, err
}
}
}

return next(ctx, tx, simulate)
}

func (d GovProposalDecorator) checkNestedMsgs(msgs []*gogoany.Any) error {
for _, msg := range msgs {
if msg.TypeUrl == "/"+gogoproto.MessageName((*authz.MsgExec)(nil)) {
// unmarshal the authz.MsgExec and check nested messages
exec := &authz.MsgExec{}
// todo unmarshal

if err := d.checkNestedMsgs(exec.Msgs); err != nil {
return err
}
}

if msg.TypeUrl == "/"+gogoproto.MessageName((*govv1.MsgSubmitProposal)(nil)) {
// unmarshal the gov.MsgSubmitProposal and check nested messages
proposal := &govv1.MsgSubmitProposal{}
// todo unmarshal

if len(proposal.Messages) == 0 {
return errors.Wrapf(gov.ErrNoProposalMsgs, "must include at least one message in proposal")
}

if err := d.checkNestedMsgs(proposal.Messages); err != nil {
return err
}
}

forbiddenParams, ok := d.forbiddenGovUpdateParams[msg.TypeUrl]
if !ok {
continue
}

if hasForbiddenParams(msg, msg.TypeUrl, forbiddenParams) {
return errors.Wrapf(proposal.ErrSettingParameter, "cannot update %s parameters via governance", msg.TypeUrl)
}
}

return nil
}

func hasForbiddenParams(msg sdk.Msg, typeURL string, forbiddenParams []string) bool {
// unmarshal msg to go struct
// check if any forbidden param is present and different from the default value

return false
}
32 changes: 20 additions & 12 deletions app/ante/gov_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,42 @@ import (
"testing"

"cosmossdk.io/math"
"github.com/celestiaorg/celestia-app/v4/app"
"github.com/celestiaorg/celestia-app/v4/app/ante"
"github.com/celestiaorg/celestia-app/v4/app/encoding"
"github.com/celestiaorg/celestia-app/v4/pkg/appconsts"
"github.com/celestiaorg/celestia-app/v4/test/util/testfactory"
"github.com/celestiaorg/celestia-app/v4/test/util/testnode"
"github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
consensustypes "github.com/cosmos/cosmos-sdk/x/consensus/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
gogoproto "github.com/cosmos/gogoproto/proto"
"github.com/stretchr/testify/require"
)

func TestGovDecorator(t *testing.T) {
decorator := ante.NewGovProposalDecorator()
blockedParams := map[string][]string{
gogoproto.MessageName(&banktypes.MsgUpdateParams{}): {"send_enabled"},
gogoproto.MessageName(&stakingtypes.MsgUpdateParams{}): {"params.bond_denom", "params.unbonding_time"},
gogoproto.MessageName(&consensustypes.MsgUpdateParams{}): {"validator"},
}

decorator := ante.NewGovProposalDecorator(blockedParams)
anteHandler := types.ChainAnteDecorators(decorator)
accounts := testfactory.GenerateAccounts(1)
accountStr := testnode.RandomAddress().String()
coins := types.NewCoins(types.NewCoin(appconsts.BondDenom, math.NewInt(10)))

msgSend := banktypes.NewMsgSend(
testnode.RandomAddress().(types.AccAddress),
testnode.RandomAddress().(types.AccAddress),
coins,
)
encCfg := encoding.MakeConfig(app.ModuleEncodingRegisters...)
encCfg := encoding.MakeConfig()
banktypes.RegisterInterfaces(encCfg.InterfaceRegistry)
from := testnode.RandomAddress().Bytes()
to := testnode.RandomAddress().Bytes()
msgSend := banktypes.NewMsgSend(from, to, coins)

msgProposal, err := govtypes.NewMsgSubmitProposal([]types.Msg{msgSend}, coins, accounts[0], "metadata", "title", "summary", false)
msgProposal, err := govtypes.NewMsgSubmitProposal(
[]types.Msg{msgSend}, coins, accountStr, "", "", "", false)
require.NoError(t, err)
msgEmptyProposal, err := govtypes.NewMsgSubmitProposal([]types.Msg{}, coins, accounts[0], "metadata", "title", "summary", false)
msgEmptyProposal, err := govtypes.NewMsgSubmitProposal(
[]types.Msg{}, coins, accountStr, "do nothing", "", "", false)
require.NoError(t, err)

testCases := []struct {
Expand Down
78 changes: 0 additions & 78 deletions app/ante/msg_gatekeeper.go

This file was deleted.

105 changes: 0 additions & 105 deletions app/ante/msg_gatekeeper_test.go

This file was deleted.

Loading

0 comments on commit 6606764

Please sign in to comment.