Skip to content
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

fix: propagate msg events correctly in x/gov #13728

Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/gov) [#13051](https://github.com/cosmos/cosmos-sdk/pull/13051) In SubmitPropsal, when a legacy msg fails it's handler call, wrap the error as ErrInvalidProposalContent (instead of ErrNoProposalHandlerExists).
* (x/gov) [#13045](https://github.com/cosmos/cosmos-sdk/pull/13045) Fix gov migrations for v3(0.46).
* (snapshot) [#13400](https://github.com/cosmos/cosmos-sdk/pull/13400) Fix snapshot checksum issue in golang 1.19.
* (x/gov) [#13728](https://github.com/cosmos/cosmos-sdk/pull/13728) Fix propagation of message events to the current context in `EndBlocker`.

### Deprecated

Expand Down
12 changes: 9 additions & 3 deletions x/gov/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ func EndBlocker(ctx sdk.Context, keeper *keeper.Keeper) {

if passes {
var (
idx int
msg sdk.Msg
idx int
events sdk.Events
msg sdk.Msg
Comment on lines +61 to +63
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to declare idx and msg?

Copy link
Member Author

@damiannolan damiannolan Nov 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like all they're used for is building a log msg below in the case of failure:
logMsg = fmt.Sprintf("passed, but msg %d (%s) failed on execution: %s", idx, sdk.MsgTypeURL(msg), err)

If they weren't declared they would only be available inside the scope of the for.

)

// attempt to execute all messages within the passed proposal
Expand All @@ -71,10 +72,12 @@ func EndBlocker(ctx sdk.Context, keeper *keeper.Keeper) {
if err == nil {
for idx, msg = range messages {
handler := keeper.Router().Handler(msg)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A defensive nil check on the handler here would probably be useful too.

What if I tried to exec an osmosis MsgSwap or something on the hub. I'm assuming it should fail before ever getting here, but the code looks like it relies on the conversion to Any as an unregistered type URL to prevent this.
I don't see any harm in writing this as:

if handler := keeper.Router().Handler(msg); handler != nil {
    res, err := handler(...)
...
}

_, err = handler(cacheCtx, msg)
res, err := handler(cacheCtx, msg)
if err != nil {
break
}

events = append(events, res.GetEvents()...)
}
}

Expand All @@ -87,6 +90,9 @@ func EndBlocker(ctx sdk.Context, keeper *keeper.Keeper) {

// write state to the underlying multi-store
writeCache()

// propagate the msg events to the current context
ctx.EventManager().EmitEvents(events)
} else {
proposal.Status = v1.StatusFailed
tagValue = types.AttributeValueProposalFailed
Expand Down