forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hooks to governance actions (cosmos#9133)
* add governance hooks * fix lint * fix lint * CHANGELOG * sh -> gh * improve comments * add test * add more tests * rename two of the hooks Co-authored-by: ahmedaly113 <[email protected]>
- Loading branch information
1 parent
603e895
commit bffcae5
Showing
12 changed files
with
240 additions
and
1 deletion.
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
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,44 @@ | ||
package keeper | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/gov/types" | ||
) | ||
|
||
// Implements GovHooks interface | ||
var _ types.GovHooks = Keeper{} | ||
|
||
// AfterProposalSubmission - call hook if registered | ||
func (keeper Keeper) AfterProposalSubmission(ctx sdk.Context, proposalID uint64) { | ||
if keeper.hooks != nil { | ||
keeper.hooks.AfterProposalSubmission(ctx, proposalID) | ||
} | ||
} | ||
|
||
// AfterProposalDeposit - call hook if registered | ||
func (keeper Keeper) AfterProposalDeposit(ctx sdk.Context, proposalID uint64, depositorAddr sdk.AccAddress) { | ||
if keeper.hooks != nil { | ||
keeper.hooks.AfterProposalDeposit(ctx, proposalID, depositorAddr) | ||
} | ||
} | ||
|
||
// AfterProposalVote - call hook if registered | ||
func (keeper Keeper) AfterProposalVote(ctx sdk.Context, proposalID uint64, voterAddr sdk.AccAddress) { | ||
if keeper.hooks != nil { | ||
keeper.hooks.AfterProposalVote(ctx, proposalID, voterAddr) | ||
} | ||
} | ||
|
||
// AfterProposalFailedMinDeposit - call hook if registered | ||
func (keeper Keeper) AfterProposalFailedMinDeposit(ctx sdk.Context, proposalID uint64) { | ||
if keeper.hooks != nil { | ||
keeper.hooks.AfterProposalFailedMinDeposit(ctx, proposalID) | ||
} | ||
} | ||
|
||
// AfterProposalVotingPeriodEnded - call hook if registered | ||
func (keeper Keeper) AfterProposalVotingPeriodEnded(ctx sdk.Context, proposalID uint64) { | ||
if keeper.hooks != nil { | ||
keeper.hooks.AfterProposalVotingPeriodEnded(ctx, proposalID) | ||
} | ||
} |
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,96 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
|
||
"github.com/cosmos/cosmos-sdk/simapp" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/gov" | ||
"github.com/cosmos/cosmos-sdk/x/gov/keeper" | ||
"github.com/cosmos/cosmos-sdk/x/gov/types" | ||
) | ||
|
||
var _ types.GovHooks = &MockGovHooksReceiver{} | ||
|
||
// GovHooks event hooks for governance proposal object (noalias) | ||
type MockGovHooksReceiver struct { | ||
AfterProposalSubmissionValid bool | ||
AfterProposalDepositValid bool | ||
AfterProposalVoteValid bool | ||
AfterProposalFailedMinDepositValid bool | ||
AfterProposalVotingPeriodEndedValid bool | ||
} | ||
|
||
func (h *MockGovHooksReceiver) AfterProposalSubmission(ctx sdk.Context, proposalID uint64) { | ||
h.AfterProposalSubmissionValid = true | ||
} | ||
|
||
func (h *MockGovHooksReceiver) AfterProposalDeposit(ctx sdk.Context, proposalID uint64, depositorAddr sdk.AccAddress) { | ||
h.AfterProposalDepositValid = true | ||
} | ||
|
||
func (h *MockGovHooksReceiver) AfterProposalVote(ctx sdk.Context, proposalID uint64, voterAddr sdk.AccAddress) { | ||
h.AfterProposalVoteValid = true | ||
} | ||
func (h *MockGovHooksReceiver) AfterProposalFailedMinDeposit(ctx sdk.Context, proposalID uint64) { | ||
h.AfterProposalFailedMinDepositValid = true | ||
} | ||
func (h *MockGovHooksReceiver) AfterProposalVotingPeriodEnded(ctx sdk.Context, proposalID uint64) { | ||
h.AfterProposalVotingPeriodEndedValid = true | ||
} | ||
|
||
func TestHooks(t *testing.T) { | ||
app := simapp.Setup(false) | ||
ctx := app.BaseApp.NewContext(false, tmproto.Header{}) | ||
|
||
minDeposit := app.GovKeeper.GetDepositParams(ctx).MinDeposit | ||
addrs := simapp.AddTestAddrs(app, ctx, 1, minDeposit[0].Amount) | ||
|
||
govHooksReceiver := MockGovHooksReceiver{} | ||
|
||
app.GovKeeper = *keeper.UpdateHooks(&app.GovKeeper, | ||
types.NewMultiGovHooks( | ||
&govHooksReceiver, | ||
), | ||
) | ||
|
||
require.False(t, govHooksReceiver.AfterProposalSubmissionValid) | ||
require.False(t, govHooksReceiver.AfterProposalDepositValid) | ||
require.False(t, govHooksReceiver.AfterProposalVoteValid) | ||
require.False(t, govHooksReceiver.AfterProposalFailedMinDepositValid) | ||
require.False(t, govHooksReceiver.AfterProposalVotingPeriodEndedValid) | ||
|
||
tp := TestProposal | ||
_, err := app.GovKeeper.SubmitProposal(ctx, tp) | ||
require.NoError(t, err) | ||
require.True(t, govHooksReceiver.AfterProposalSubmissionValid) | ||
|
||
newHeader := ctx.BlockHeader() | ||
newHeader.Time = ctx.BlockHeader().Time.Add(app.GovKeeper.GetDepositParams(ctx).MaxDepositPeriod).Add(time.Duration(1) * time.Second) | ||
ctx = ctx.WithBlockHeader(newHeader) | ||
gov.EndBlocker(ctx, app.GovKeeper) | ||
|
||
require.True(t, govHooksReceiver.AfterProposalFailedMinDepositValid) | ||
|
||
p2, err := app.GovKeeper.SubmitProposal(ctx, tp) | ||
require.NoError(t, err) | ||
|
||
activated, err := app.GovKeeper.AddDeposit(ctx, p2.ProposalId, addrs[0], minDeposit) | ||
require.True(t, activated) | ||
require.NoError(t, err) | ||
require.True(t, govHooksReceiver.AfterProposalDepositValid) | ||
|
||
err = app.GovKeeper.AddVote(ctx, p2.ProposalId, addrs[0], types.NewNonSplitVoteOption(types.OptionYes)) | ||
require.NoError(t, err) | ||
require.True(t, govHooksReceiver.AfterProposalVoteValid) | ||
|
||
newHeader = ctx.BlockHeader() | ||
newHeader.Time = ctx.BlockHeader().Time.Add(app.GovKeeper.GetVotingParams(ctx).VotingPeriod).Add(time.Duration(1) * time.Second) | ||
ctx = ctx.WithBlockHeader(newHeader) | ||
gov.EndBlocker(ctx, app.GovKeeper) | ||
require.True(t, govHooksReceiver.AfterProposalVotingPeriodEndedValid) | ||
} |
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,8 @@ | ||
package keeper | ||
|
||
import "github.com/cosmos/cosmos-sdk/x/gov/types" | ||
|
||
func UpdateHooks(k *Keeper, h types.GovHooks) *Keeper { | ||
k.hooks = h | ||
return k | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package types | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
var _ GovHooks = MultiGovHooks{} | ||
|
||
// combine multiple governance hooks, all hook functions are run in array sequence | ||
type MultiGovHooks []GovHooks | ||
|
||
func NewMultiGovHooks(hooks ...GovHooks) MultiGovHooks { | ||
return hooks | ||
} | ||
|
||
func (h MultiGovHooks) AfterProposalSubmission(ctx sdk.Context, proposalID uint64) { | ||
for i := range h { | ||
h[i].AfterProposalSubmission(ctx, proposalID) | ||
} | ||
} | ||
|
||
func (h MultiGovHooks) AfterProposalDeposit(ctx sdk.Context, proposalID uint64, depositorAddr sdk.AccAddress) { | ||
for i := range h { | ||
h[i].AfterProposalDeposit(ctx, proposalID, depositorAddr) | ||
} | ||
} | ||
|
||
func (h MultiGovHooks) AfterProposalVote(ctx sdk.Context, proposalID uint64, voterAddr sdk.AccAddress) { | ||
for i := range h { | ||
h[i].AfterProposalVote(ctx, proposalID, voterAddr) | ||
} | ||
} | ||
func (h MultiGovHooks) AfterProposalFailedMinDeposit(ctx sdk.Context, proposalID uint64) { | ||
for i := range h { | ||
h[i].AfterProposalFailedMinDeposit(ctx, proposalID) | ||
} | ||
} | ||
func (h MultiGovHooks) AfterProposalVotingPeriodEnded(ctx sdk.Context, proposalID uint64) { | ||
for i := range h { | ||
h[i].AfterProposalVotingPeriodEnded(ctx, proposalID) | ||
} | ||
} |