-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add hooks to governance actions #9133
Changes from 9 commits
4293ed7
8abc251
bd1279a
f72c814
a5e60d7
67538db
cb066d2
7e77ac1
1d1530d
24e5109
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
} | ||
} |
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) | ||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you define this on the other test file instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, so the reason it's like this is that the And we don't want to add this function to the See: https://stackoverflow.com/questions/24622388/how-to-test-a-unexported-private-function-in-go-golang There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. makes sense! thanks for the clarification |
||
k.hooks = h | ||
return k | ||
} |
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) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we have a testing suite for this instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wdym?