## Description Closes: #9404 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
7.0 KiB
Events
Events are objects that contain information about the execution of the application. They are mainly used by service providers like block explorers and wallet to track the execution of various messages and index transactions. {synopsis}
Pre-requisite Readings
- Anatomy of a Cosmos SDK application {prereq}
- Tendermint Documentation on Events {prereq}
Events
Events are implemented in the Cosmos SDK as an alias of the ABCI Event type and
take the form of: {eventType}.{attributeKey}={attributeValue}.
+++ https://github.com/tendermint/tendermint/blob/v0.34.8/proto/tendermint/abci/types.proto#L304-L313
An Event contains:
- A
typeto categorize the Event at a high-level; for example, the Cosmos SDK uses the"message"type to filter Events byMsgs. - A list of
attributesare key-value pairs that give more information about the categorized Event. For example, for the"message"type, we can filter Events by key-value pairs usingmessage.action={some_action},message.module={some_module}ormessage.sender={some_sender}.
::: tip
To parse the attribute values as strings, make sure to add ' (single quotes) around each attribute value.
:::
Events, the type and attributes are defined on a per-module basis in the module's
/types/events.go file, and triggered from the module's Protobuf Msg service
by using the EventManager. In addition, each module documents its Events under
spec/xx_events.md.
Events are returned to the underlying consensus engine in the response of the following ABCI messages:
Examples
The following examples show how to query Events using the Cosmos SDK.
| Event | Description |
|---|---|
tx.height=23 |
Query all transactions at height 23 |
message.action='/cosmos.bank.v1beta1.Msg/Send' |
Query all transactions containing a x/bank Send Service Msg. Note the 's around the value. |
message.action='send' |
Query all transactions containing a x/bank Send legacy Msg. Note the 's around the value. |
message.module='bank' |
Query all transactions containing messages from the x/bank module. Note the 's around the value. |
create_validator.validator='cosmosval1...' |
x/staking-specific Event, see x/staking SPEC. |
EventManager
In Cosmos SDK applications, Events are managed by an abstraction called the EventManager.
Internally, the EventManager tracks a list of Events for the entire execution flow of a
transaction or BeginBlock/EndBlock.
+++ https://github.com/cosmos/cosmos-sdk/blob/v0.42.1/types/events.go#L17-L25
The EventManager comes with a set of useful methods to manage Events. The method
that is used most by module and application developers is EmitEvent that tracks
an Event in the EventManager.
+++ https://github.com/cosmos/cosmos-sdk/blob/v0.42.1/types/events.go#L33-L37
Module developers should handle Event emission via the EventManager#EmitEvent in each message
Handler and in each BeginBlock/EndBlock handler. The EventManager is accessed via
the Context, where Event emission generally follows this pattern:
ctx.EventManager().EmitEvent(
sdk.NewEvent(eventType, sdk.NewAttribute(attributeKey, attributeValue)),
)
Module's handler function should also set a new EventManager to the context to isolate emitted Events per message:
func NewHandler(keeper Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx = ctx.WithEventManager(sdk.NewEventManager())
switch msg := msg.(type) {
See the Msg services concept doc for a more detailed
view on how to typically implement Events and use the EventManager in modules.
Subscribing to Events
You can use Tendermint's Websocket to subscribe to Events by calling the subscribe RPC method:
{
"jsonrpc": "2.0",
"method": "subscribe",
"id": "0",
"params": {
"query": "tm.event='eventCategory' AND eventType.eventAttribute='attributeValue'"
}
}
The main eventCategory you can subscribe to are:
NewBlock: Contains Events triggered duringBeginBlockandEndBlock.Tx: Contains Events triggered duringDeliverTx(i.e. transaction processing).ValidatorSetUpdates: Contains validator set updates for the block.
These Events are triggered from the state package after a block is committed. You can get the
full list of Event categories on the Tendermint Godoc page.
The type and attribute value of the query allow you to filter the specific Event you are looking for. For example, a transfer transaction triggers an Event of type Transfer and has Recipient and Sender as attributes (as defined in the events.go file of the bank module). Subscribing to this Event would be done like so:
{
"jsonrpc": "2.0",
"method": "subscribe",
"id": "0",
"params": {
"query": "tm.event='Tx' AND transfer.sender='senderAddress'"
}
}
where senderAddress is an address following the AccAddress format.
Typed Events (coming soon)
As previously described, Events are defined on a per-module basis. It is the responsibility of the module developer to define Event types and Event attributes. Except in the spec/XX_events.md file, these Event types and attributes are unfortunately not easily discoverable, so the Cosmos SDK proposes to use Protobuf-defined Typed Events for emitting and querying Events.
The Typed Events proposal has not yet been fully implemented. Documentation is not yet available.
Next {hide}
Learn about Cosmos SDK telemetry {hide}