Skip to content

Commit

Permalink
feat: implement anti-spam mechanism for gov proposals
Browse files Browse the repository at this point in the history
  • Loading branch information
kamuikatsurgi committed Jan 12, 2025
1 parent c200868 commit 731fe49
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
24 changes: 17 additions & 7 deletions x/gov/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ func EndBlocker(ctx sdk.Context, keeper *keeper.Keeper) error {
return false, err
}

// TODO HV2: https://polygon.atlassian.net/browse/POS-2755
err = keeper.RefundAndDeleteDeposits(ctx, proposal.Id) // refund deposit if proposal got removed without getting 100% of the proposal
// HV2: https://polygon.atlassian.net/browse/POS-2755
// HV2: send deposit to the fee collector if the proposal got removed without getting 100% of the proposal
err = keeper.SendToFeeCollectorAndDeleteDeposits(ctx, proposal.Id)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -135,10 +136,18 @@ func EndBlocker(ctx sdk.Context, keeper *keeper.Keeper) error {
return false, err
}

// HV2: heimdall refunds and deletes deposits in all cases of proposal failures, without caring about burnDeposits
err = keeper.RefundAndDeleteDeposits(ctx, proposal.Id)
if err != nil {
return false, err
// HV2: https://polygon.atlassian.net/browse/POS-2755
// HV2: heimdall sends deposit to the fee collector and deletes deposits in all cases of proposal failures, without caring about burnDeposits
if passes {
err = keeper.RefundAndDeleteDeposits(ctx, proposal.Id)
if err != nil {
return false, err
}
} else {
err = keeper.SendToFeeCollectorAndDeleteDeposits(ctx, proposal.Id)
if err != nil {
return false, err
}
}

// If an expedited proposal fails, we do not want to update
Expand Down Expand Up @@ -315,7 +324,8 @@ func failUnsupportedProposal(
return err
}

if err := keeper.RefundAndDeleteDeposits(ctx, proposal.Id); err != nil {
// HV2: https://polygon.atlassian.net/browse/POS-2755
if err := keeper.SendToFeeCollectorAndDeleteDeposits(ctx, proposal.Id); err != nil {
return err
}

Expand Down
4 changes: 3 additions & 1 deletion x/gov/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ func trackMockBalances(bankKeeper *govtestutil.MockBankKeeper, distributionKeepe
// We don't track module account balances.
bankKeeper.EXPECT().MintCoins(gomock.Any(), topupTypes.ModuleName, gomock.Any()).AnyTimes()
bankKeeper.EXPECT().BurnCoins(gomock.Any(), types.ModuleName, gomock.Any()).AnyTimes()
bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), topupTypes.ModuleName, types.ModuleName, gomock.Any()).AnyTimes()
bankKeeper.EXPECT().SendCoinsFromModuleToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(_ sdk.Context, senderModule string, receiverModule string, coins sdk.Coins) error {
return nil
}).AnyTimes()

// But we do track normal account balances.
bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), types.ModuleName, gomock.Any()).DoAndReturn(func(_ sdk.Context, sender sdk.AccAddress, _ string, coins sdk.Coins) error {
Expand Down
13 changes: 13 additions & 0 deletions x/gov/keeper/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
disttypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/cosmos/cosmos-sdk/x/gov/types"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
Expand Down Expand Up @@ -285,6 +286,18 @@ func (keeper Keeper) RefundAndDeleteDeposits(ctx context.Context, proposalID uin
})
}

// SendToFeeCollectorAndDeleteDeposits sends deposits to the Fee Collector and deletes all the deposits on a specific proposal.
func (keeper Keeper) SendToFeeCollectorAndDeleteDeposits(ctx context.Context, proposalID uint64) error {
return keeper.IterateDeposits(ctx, proposalID, func(key collections.Pair[uint64, sdk.AccAddress], deposit v1.Deposit) (bool, error) {
err := keeper.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, authtypes.FeeCollectorName, deposit.Amount)
if err != nil {
return false, err
}
err = keeper.Deposits.Remove(ctx, key)
return false, err
})
}

// validateInitialDeposit validates if initial deposit is greater than or equal to the minimum
// required at the time of proposal submission. This threshold amount is determined by
// the deposit parameters. Returns nil on success, error otherwise.
Expand Down
1 change: 1 addition & 0 deletions x/gov/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type BankKeeper interface {
SpendableCoins(ctx context.Context, addr sdk.AccAddress) sdk.Coins

SendCoinsFromModuleToAccount(ctx context.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error
SendCoinsFromModuleToModule(ctx context.Context, senderModule, recipientModule string, amt sdk.Coins) error
SendCoinsFromAccountToModule(ctx context.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error

// HV2: this function was removed in heimdall's gox/expected_keepers.go (from SupplyKeeper, merged now with BankKeeper)
Expand Down

0 comments on commit 731fe49

Please sign in to comment.