-
Notifications
You must be signed in to change notification settings - Fork 138
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
* Democracy governance whitelisting #284 * Democracy gov whitelist - added logs and event * Gov whitelisting - delete only active forbidden prop. * Democracy gov whitelisting - test moved Co-authored-by: Daniel T <[email protected]> Co-authored-by: Jehan <[email protected]>
- Loading branch information
1 parent
149b42e
commit fbd6c40
Showing
9 changed files
with
433 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package ante | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
) | ||
|
||
type ForbiddenProposalsDecorator struct { | ||
IsProposalWhitelisted func(govtypes.Content) bool | ||
} | ||
|
||
func NewForbiddenProposalsDecorator(whiteListFn func(govtypes.Content) bool) ForbiddenProposalsDecorator { | ||
return ForbiddenProposalsDecorator{IsProposalWhitelisted: whiteListFn} | ||
} | ||
|
||
func (decorator ForbiddenProposalsDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) { | ||
currHeight := ctx.BlockHeight() | ||
|
||
for _, msg := range tx.GetMsgs() { | ||
submitProposalMgs, ok := msg.(*govtypes.MsgSubmitProposal) | ||
//if the message is MsgSubmitProposal, check if proposal is whitelisted | ||
if ok { | ||
if !decorator.IsProposalWhitelisted(submitProposalMgs.GetContent()) { | ||
return ctx, fmt.Errorf("tx contains unsupported proposal message types at height %d", currHeight) | ||
} | ||
} | ||
} | ||
|
||
return next(ctx, tx, simulate) | ||
} |
97 changes: 97 additions & 0 deletions
97
app/consumer-democracy/ante/forbidden_proposals_ante_test.go
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,97 @@ | ||
package ante_test | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
"github.com/cosmos/cosmos-sdk/x/params/types/proposal" | ||
app "github.com/cosmos/interchain-security/app/consumer-democracy" | ||
"github.com/cosmos/interchain-security/app/consumer-democracy/ante" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tendermint/spm/cosmoscmd" | ||
) | ||
|
||
func TestForbiddenProposalsDecorator(t *testing.T) { | ||
txCfg := cosmoscmd.MakeEncodingConfig(app.ModuleBasics).TxConfig | ||
|
||
testCases := []struct { | ||
name string | ||
ctx sdk.Context | ||
msgs []sdk.Msg | ||
expectErr bool | ||
}{ | ||
{ | ||
name: "Allowed param change", | ||
ctx: sdk.Context{}, | ||
msgs: []sdk.Msg{ | ||
newParamChangeProposalMsg([]proposal.ParamChange{ | ||
//only subspace and key are relevant for testing | ||
{Subspace: banktypes.ModuleName, Key: "SendEnabled", Value: ""}, | ||
}), | ||
}, | ||
expectErr: false, | ||
}, | ||
{ | ||
name: "Forbidden param change", | ||
ctx: sdk.Context{}, | ||
msgs: []sdk.Msg{ | ||
newParamChangeProposalMsg([]proposal.ParamChange{ | ||
{Subspace: authtypes.ModuleName, Key: "MaxMemoCharacters", Value: ""}, | ||
}), | ||
}, | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "Allowed and forbidden param changes in the same msg", | ||
ctx: sdk.Context{}, | ||
msgs: []sdk.Msg{ | ||
newParamChangeProposalMsg([]proposal.ParamChange{ | ||
{Subspace: banktypes.ModuleName, Key: "SendEnabled", Value: ""}, | ||
{Subspace: authtypes.ModuleName, Key: "MaxMemoCharacters", Value: ""}, | ||
}), | ||
}, | ||
expectErr: true, | ||
}, | ||
{ | ||
name: "Allowed and forbidden param changes in different msg", | ||
ctx: sdk.Context{}, | ||
msgs: []sdk.Msg{ | ||
newParamChangeProposalMsg([]proposal.ParamChange{ | ||
{Subspace: banktypes.ModuleName, Key: "SendEnabled", Value: ""}, | ||
}), | ||
newParamChangeProposalMsg([]proposal.ParamChange{ | ||
{Subspace: authtypes.ModuleName, Key: "MaxMemoCharacters", Value: ""}, | ||
}), | ||
}, | ||
expectErr: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
handler := ante.NewForbiddenProposalsDecorator(app.IsProposalWhitelisted) | ||
|
||
txBuilder := txCfg.NewTxBuilder() | ||
require.NoError(t, txBuilder.SetMsgs(tc.msgs...)) | ||
|
||
_, err := handler.AnteHandle(tc.ctx, txBuilder.GetTx(), false, | ||
func(ctx sdk.Context, _ sdk.Tx, _ bool) (sdk.Context, error) { return ctx, nil }) | ||
if tc.expectErr { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func newParamChangeProposalMsg(changes []proposal.ParamChange) *govtypes.MsgSubmitProposal { | ||
paramChange := proposal.ParameterChangeProposal{Changes: changes} | ||
msg, _ := govtypes.NewMsgSubmitProposal(¶mChange, sdk.NewCoins(), sdk.AccAddress{}) | ||
return msg | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package app | ||
|
||
import ( | ||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" | ||
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" | ||
"github.com/cosmos/cosmos-sdk/x/params/types/proposal" | ||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
ibctransfertypes "github.com/cosmos/ibc-go/v3/modules/apps/transfer/types" | ||
) | ||
|
||
func IsProposalWhitelisted(content govtypes.Content) bool { | ||
|
||
switch c := content.(type) { | ||
case *proposal.ParameterChangeProposal: | ||
return isParamChangeWhitelisted(c.Changes) | ||
|
||
default: | ||
return false | ||
} | ||
|
||
} | ||
|
||
func isParamChangeWhitelisted(paramChanges []proposal.ParamChange) bool { | ||
for _, paramChange := range paramChanges { | ||
_, found := WhitelistedParams[paramChangeKey{Subspace: paramChange.Subspace, Key: paramChange.Key}] | ||
if !found { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
type paramChangeKey struct { | ||
Subspace, Key string | ||
} | ||
|
||
var WhitelistedParams = map[paramChangeKey]struct{}{ | ||
//bank | ||
{Subspace: banktypes.ModuleName, Key: "SendEnabled"}: {}, | ||
//governance | ||
{Subspace: govtypes.ModuleName, Key: "depositparams"}: {}, //min_deposit, max_deposit_period | ||
{Subspace: govtypes.ModuleName, Key: "votingparams"}: {}, //voting_period | ||
{Subspace: govtypes.ModuleName, Key: "tallyparams"}: {}, //quorum,threshold,veto_threshold | ||
//staking | ||
{Subspace: stakingtypes.ModuleName, Key: "UnbondingTime"}: {}, | ||
{Subspace: stakingtypes.ModuleName, Key: "MaxValidators"}: {}, | ||
{Subspace: stakingtypes.ModuleName, Key: "MaxEntries"}: {}, | ||
{Subspace: stakingtypes.ModuleName, Key: "HistoricalEntries"}: {}, | ||
{Subspace: stakingtypes.ModuleName, Key: "BondDenom"}: {}, | ||
//distribution | ||
{Subspace: distrtypes.ModuleName, Key: "communitytax"}: {}, | ||
{Subspace: distrtypes.ModuleName, Key: "baseproposerreward"}: {}, | ||
{Subspace: distrtypes.ModuleName, Key: "bonusproposerreward"}: {}, | ||
{Subspace: distrtypes.ModuleName, Key: "withdrawaddrenabled"}: {}, | ||
//mint | ||
{Subspace: minttypes.ModuleName, Key: "MintDenom"}: {}, | ||
{Subspace: minttypes.ModuleName, Key: "InflationRateChange"}: {}, | ||
{Subspace: minttypes.ModuleName, Key: "InflationMax"}: {}, | ||
{Subspace: minttypes.ModuleName, Key: "InflationMin"}: {}, | ||
{Subspace: minttypes.ModuleName, Key: "GoalBonded"}: {}, | ||
{Subspace: minttypes.ModuleName, Key: "BlocksPerYear"}: {}, | ||
//ibc transfer | ||
{Subspace: ibctransfertypes.ModuleName, Key: "SendEnabled"}: {}, | ||
{Subspace: ibctransfertypes.ModuleName, Key: "ReceiveEnabled"}: {}, | ||
//add interchain account params(HostEnabled, AllowedMessages) once the module is added to the consumer app | ||
} |
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,21 @@ | ||
package app_test | ||
|
||
import ( | ||
"testing" | ||
|
||
ibctesting "github.com/cosmos/ibc-go/v3/testing" | ||
appConsumer "github.com/cosmos/interchain-security/app/consumer-democracy" | ||
simapp "github.com/cosmos/interchain-security/testutil/simapp" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDemocracyGovernanceWhitelistingKeys(t *testing.T) { | ||
chain := ibctesting.NewTestChain(t, simapp.NewBasicCoordinator(t), simapp.SetupTestingAppConsumerDemocracy, "test") | ||
paramKeeper := chain.App.(*appConsumer.App).ParamsKeeper | ||
for paramKey := range appConsumer.WhitelistedParams { | ||
ss, ok := paramKeeper.GetSubspace(paramKey.Subspace) | ||
require.True(t, ok, "Unknown subspace %s", paramKey.Subspace) | ||
hasKey := ss.Has(chain.GetContext(), []byte(paramKey.Key)) | ||
require.True(t, hasKey, "Invalid key %s for subspace %s", paramKey.Key, paramKey.Subspace) | ||
} | ||
} |
Oops, something went wrong.