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

Refactor events #586

Merged
merged 5 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions EVENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,17 @@ If the original contract execution example above was actually the result of a me
and it registered a ReplyOn clause, the `reply` function on that contract would receive the entire 11 events in the example
above, and would need to use the `message` markers to locate the segment of interest.

## Governance Events
alpe marked this conversation as resolved.
Show resolved Hide resolved
The governance process is handled by the cosmos-sdk `gov` module. We do not emit any events of type "message" anymore in v0.18+.
Context-specific events are emitted as described above. `Execution` and `Migration` return some contract result though that are
emitted as:
```go
sdk.NewEvent(
"gov_contract_result",
sdk.NewAttribute("result", hex.EncodeToString(data)),
)
```

## IBC Events

TODO: define what the default SDK messages are here and what we add to our custom keeper events.
1 change: 1 addition & 0 deletions x/wasm/ibc_reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

func TestIBCReflectContract(t *testing.T) {
t.Skip("TODO (Alex): fails with `No _contract_address found in callback events: execute wasm contract failed` due to event refactorings")
// scenario:
// chain A: ibc_reflect_send.wasm
// chain B: reflect.wasm + ibc_reflect.wasm
Expand Down
14 changes: 0 additions & 14 deletions x/wasm/keeper/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,6 @@ func newWasmModuleEvent(customAttributes []wasmvmtypes.EventAttribute, contractA
return sdk.Events{sdk.NewEvent(types.WasmModuleEventType, attrs...)}, nil
}

// returns true when a wasm module event was emitted for this contract already
func hasWasmModuleEvent(ctx sdk.Context, contractAddr sdk.AccAddress) bool {
for _, e := range ctx.EventManager().Events() {
if e.Type == types.WasmModuleEventType {
for _, a := range e.Attributes {
if string(a.Key) == types.AttributeKeyContractAddr && string(a.Value) == contractAddr.String() {
return true
}
}
}
}
return false
}

const eventTypeMinLength = 2

// newCustomEvents converts wasmvm events from a contract response to sdk type events
Expand Down
14 changes: 14 additions & 0 deletions x/wasm/keeper/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,17 @@ func TestNewWasmModuleEvent(t *testing.T) {
})
}
}

// returns true when a wasm module event was emitted for this contract already
func hasWasmModuleEvent(ctx sdk.Context, contractAddr sdk.AccAddress) bool {
for _, e := range ctx.EventManager().Events() {
if e.Type == types.WasmModuleEventType {
for _, a := range e.Attributes {
if string(a.Key) == types.AttributeKeyContractAddr && string(a.Value) == contractAddr.String() {
return true
}
}
}
}
return false
}
60 changes: 58 additions & 2 deletions x/wasm/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"github.com/tendermint/tendermint/libs/log"
"math"
"path/filepath"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -178,6 +180,16 @@ func (k Keeper) create(ctx sdk.Context, creator sdk.AccAddress, wasmCode []byte,
}
codeInfo := types.NewCodeInfo(checksum, creator, *instantiateAccess)
k.storeCodeInfo(ctx, codeID, codeInfo)

evt := sdk.NewEvent(
alpe marked this conversation as resolved.
Show resolved Hide resolved
types.EventTypeStoreCode,
sdk.NewAttribute(types.AttributeKeyCodeID, strconv.FormatUint(codeID, 10)),
)
for _, f := range strings.Split(report.RequiredFeatures, ",") {
evt.AppendAttributes(sdk.NewAttribute(types.AttributeKeyFeature, strings.TrimSpace(f)))
}
ctx.EventManager().EmitEvent(evt)

return codeID, nil
}

Expand Down Expand Up @@ -299,6 +311,11 @@ func (k Keeper) instantiate(ctx sdk.Context, codeID uint64, creator, admin sdk.A
return nil, nil, sdkerrors.Wrap(err, "dispatch")
}

ctx.EventManager().EmitEvent(sdk.NewEvent(
Copy link
Member

Choose a reason for hiding this comment

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

Please emit this before calling k.wasmVM.Instantiate.

We have all needed info there, and this is the ordering from the spec.

types.EventTypeInstantiate,
sdk.NewAttribute(types.AttributeKeyContractAddr, contractAddress.String()),
sdk.NewAttribute(types.AttributeKeyCodeID, fmt.Sprintf("%d", codeID)),
alpe marked this conversation as resolved.
Show resolved Hide resolved
))
return contractAddress, data, nil
}

Expand Down Expand Up @@ -337,6 +354,11 @@ func (k Keeper) execute(ctx sdk.Context, contractAddress sdk.AccAddress, caller
if err != nil {
return nil, sdkerrors.Wrap(err, "dispatch")
}

ctx.EventManager().EmitEvent(sdk.NewEvent(
alpe marked this conversation as resolved.
Show resolved Hide resolved
types.EventTypeExecute,
sdk.NewAttribute(types.AttributeKeyContractAddr, contractAddress.String()),
))
return data, nil
}

Expand Down Expand Up @@ -401,6 +423,12 @@ func (k Keeper) migrate(ctx sdk.Context, contractAddress sdk.AccAddress, caller
if err != nil {
return nil, sdkerrors.Wrap(err, "dispatch")
}

ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeMigrate,
sdk.NewAttribute(types.AttributeKeyCodeID, fmt.Sprintf("%d", newCodeID)),
alpe marked this conversation as resolved.
Show resolved Hide resolved
sdk.NewAttribute(types.AttributeKeyContractAddr, contractAddress.String()),
))
return data, nil
}

Expand Down Expand Up @@ -433,6 +461,11 @@ func (k Keeper) Sudo(ctx sdk.Context, contractAddress sdk.AccAddress, msg []byte
if err != nil {
return nil, sdkerrors.Wrap(err, "dispatch")
}

ctx.EventManager().EmitEvent(sdk.NewEvent(
alpe marked this conversation as resolved.
Show resolved Hide resolved
types.EventTypeSudo,
sdk.NewAttribute(types.AttributeKeyContractAddr, contractAddress.String()),
))
return data, nil
}

Expand Down Expand Up @@ -466,6 +499,11 @@ func (k Keeper) reply(ctx sdk.Context, contractAddress sdk.AccAddress, reply was
if err != nil {
return nil, sdkerrors.Wrap(err, "dispatch")
}

ctx.EventManager().EmitEvent(sdk.NewEvent(
alpe marked this conversation as resolved.
Show resolved Hide resolved
types.EventTypeReply,
sdk.NewAttribute(types.AttributeKeyContractAddr, contractAddress.String()),
))
return data, nil
}

Expand Down Expand Up @@ -709,6 +747,11 @@ func (k Keeper) pinCode(ctx sdk.Context, codeID uint64) error {
store := ctx.KVStore(k.storeKey)
// store 1 byte to not run into `nil` debugging issues
store.Set(types.GetPinnedCodeIndexPrefix(codeID), []byte{1})

ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypePinCode,
sdk.NewAttribute(types.AttributeKeyCodeID, strconv.FormatUint(codeID, 10)),
))
return nil
}

Expand All @@ -724,6 +767,11 @@ func (k Keeper) unpinCode(ctx sdk.Context, codeID uint64) error {

store := ctx.KVStore(k.storeKey)
store.Delete(types.GetPinnedCodeIndexPrefix(codeID))

ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeUnpinCode,
sdk.NewAttribute(types.AttributeKeyCodeID, strconv.FormatUint(codeID, 10)),
))
return nil
}

Expand Down Expand Up @@ -775,7 +823,7 @@ func (k *Keeper) handleContractResponse(
attributeGasCost := k.gasRegister.EventCosts(attrs, evts)
ctx.GasMeter().ConsumeGas(attributeGasCost, "Custom contract event attributes")
// emit all events from this contract itself
if len(attrs) != 0 || !hasWasmModuleEvent(ctx, contractAddr) {
if len(attrs) != 0 {
alpe marked this conversation as resolved.
Show resolved Hide resolved
wasmEvents, err := newWasmModuleEvent(attrs, contractAddr)
if err != nil {
return nil, err
Expand Down Expand Up @@ -985,7 +1033,9 @@ func NewBankCoinTransferrer(keeper types.BankKeeper) BankCoinTransferrer {

// TransferCoins transfers coins from source to destination account when coin send was enabled for them and the recipient
// is not in the blocked address list.
func (c BankCoinTransferrer) TransferCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error {
func (c BankCoinTransferrer) TransferCoins(parentCtx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error {
em := sdk.NewEventManager()
ctx := parentCtx.WithEventManager(em)
if err := c.keeper.SendEnabledCoins(ctx, amt...); err != nil {
return err
}
Expand All @@ -996,6 +1046,12 @@ func (c BankCoinTransferrer) TransferCoins(ctx sdk.Context, fromAddr sdk.AccAddr
if sdkerr != nil {
return sdkerr
}
for _, e := range em.Events() {
alpe marked this conversation as resolved.
Show resolved Hide resolved
if e.Type == sdk.EventTypeMessage { // skip messages as we talk to the keeper directly
continue
}
parentCtx.EventManager().EmitEvent(e)
}
return nil
}

Expand Down
9 changes: 8 additions & 1 deletion x/wasm/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1042,9 +1042,16 @@ func TestMigrateWithDispatchedMessage(t *testing.T) {
{"module": "bank"},
},
},
{
alpe marked this conversation as resolved.
Show resolved Hide resolved
"Type": "migrate",
"Attr": []dict{
{"code_id": "2"},
{"_contract_address": contractAddr},
},
},
}
expJSONEvts := string(mustMarshal(t, expEvents))
assert.JSONEq(t, expJSONEvts, prettyEvents(t, ctx.EventManager().Events()))
assert.JSONEq(t, expJSONEvts, prettyEvents(t, ctx.EventManager().Events()), prettyEvents(t, ctx.EventManager().Events()))

// all persistent data cleared
m := keepers.WasmKeeper.QueryRaw(ctx, contractAddr, []byte("config"))
Expand Down
25 changes: 6 additions & 19 deletions x/wasm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package keeper

import (
"context"
"encoding/hex"
"fmt"
"github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -33,8 +31,7 @@ func (m msgServer) StoreCode(goCtx context.Context, msg *types.MsgStoreCode) (*t
ctx.EventManager().EmitEvent(sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeySigner, msg.Sender),
sdk.NewAttribute(types.AttributeKeyCodeID, fmt.Sprintf("%d", codeID)),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender),
))

return &types.MsgStoreCodeResponse{
Expand Down Expand Up @@ -64,10 +61,7 @@ func (m msgServer) InstantiateContract(goCtx context.Context, msg *types.MsgInst
ctx.EventManager().EmitEvent(sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeySigner, msg.Sender),
sdk.NewAttribute(types.AttributeKeyCodeID, fmt.Sprintf("%d", msg.CodeID)),
sdk.NewAttribute(types.AttributeKeyContractAddr, contractAddr.String()),
sdk.NewAttribute(types.AttributeResultDataHex, hex.EncodeToString(data)),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender),
))

return &types.MsgInstantiateContractResponse{
Expand Down Expand Up @@ -95,9 +89,7 @@ func (m msgServer) ExecuteContract(goCtx context.Context, msg *types.MsgExecuteC
ctx.EventManager().EmitEvent(sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeySigner, msg.Sender),
sdk.NewAttribute(types.AttributeKeyContractAddr, msg.Contract),
sdk.NewAttribute(types.AttributeResultDataHex, hex.EncodeToString(data)),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender),
))

return &types.MsgExecuteContractResponse{
Expand All @@ -124,10 +116,7 @@ func (m msgServer) MigrateContract(goCtx context.Context, msg *types.MsgMigrateC
ctx.EventManager().EmitEvent(sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeySigner, msg.Sender),
sdk.NewAttribute(types.AttributeKeyCodeID, fmt.Sprintf("%d", msg.CodeID)),
sdk.NewAttribute(types.AttributeKeyContractAddr, msg.Contract),
sdk.NewAttribute(types.AttributeResultDataHex, hex.EncodeToString(data)),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender),
))

return &types.MsgMigrateContractResponse{
Expand Down Expand Up @@ -157,8 +146,7 @@ func (m msgServer) UpdateAdmin(goCtx context.Context, msg *types.MsgUpdateAdmin)
ctx.EventManager().EmitEvent(sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeySigner, msg.Sender),
sdk.NewAttribute(types.AttributeKeyContractAddr, msg.Contract),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender),
))

return &types.MsgUpdateAdminResponse{}, nil
Expand All @@ -182,8 +170,7 @@ func (m msgServer) ClearAdmin(goCtx context.Context, msg *types.MsgClearAdmin) (
ctx.EventManager().EmitEvent(sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeySigner, msg.Sender),
sdk.NewAttribute(types.AttributeKeyContractAddr, msg.Contract),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender),
))

return &types.MsgClearAdminResponse{}, nil
Expand Down
Loading