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

Docs: Anys Usage, Events & small cleanups #8895

Merged
merged 29 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9f47a92
Update v042 link
amaury1093 Mar 16, 2021
5bfab35
Add docs for events
amaury1093 Mar 16, 2021
fc08893
Document usage of any
amaury1093 Mar 16, 2021
da42fec
Random cleanups
amaury1093 Mar 16, 2021
a19b48c
Add UnpackInterfaces
amaury1093 Mar 16, 2021
9e06261
Use tag instead of commit
amaury1093 Mar 16, 2021
97d8739
Update docs/architecture/adr-041-in-place-store-migrations.md
amaury1093 Mar 17, 2021
f8b4e1c
Update docs/basics/app-anatomy.md
amaury1093 Mar 17, 2021
e0d1a45
Update docs/building-modules/simulator.md
amaury1093 Mar 17, 2021
9c92904
Update docs/core/baseapp.md
amaury1093 Mar 17, 2021
35ffaa4
Update docs/core/baseapp.md
amaury1093 Mar 17, 2021
7ddd370
Update docs/core/encoding.md
amaury1093 Mar 17, 2021
68dd03b
Update docs/core/events.md
amaury1093 Mar 17, 2021
4cb4a3a
Update docs/core/events.md
amaury1093 Mar 17, 2021
583a541
Update docs/core/events.md
amaury1093 Mar 17, 2021
46a738d
Update docs/core/events.md
amaury1093 Mar 17, 2021
3e2f4b5
Update docs/core/events.md
amaury1093 Mar 17, 2021
b0fd14a
Update docs/core/encoding.md
amaury1093 Mar 17, 2021
de52a4f
Update docs/core/encoding.md
amaury1093 Mar 17, 2021
b74cfa4
Update docs/core/encoding.md
amaury1093 Mar 17, 2021
26f09a5
Update docs/core/encoding.md
amaury1093 Mar 17, 2021
6e94d8a
Update docs/core/encoding.md
amaury1093 Mar 17, 2021
fc789d6
Update docs/core/encoding.md
amaury1093 Mar 17, 2021
1afbd0c
Update docs/core/encoding.md
amaury1093 Mar 17, 2021
a3867c0
Update docs/core/encoding.md
amaury1093 Mar 17, 2021
d84d262
Update docs/core/encoding.md
amaury1093 Mar 17, 2021
b791fd0
Update docs/core/events.md
amaury1093 Mar 17, 2021
9f75522
Use consistent casing
amaury1093 Mar 17, 2021
2501e84
Merge branch 'master' into am/random-docs
Mar 17, 2021
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
2 changes: 1 addition & 1 deletion docs/architecture/adr-041-in-place-store-migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (m Migrator) Migrate1to2(ctx sdk.Context) error {
}
```

Each module's migration functions are specific to the module's store evolutions, and are not described in this ADR. An example of x/bank store key migrations following the introduction of ADR-028 length-prefixed addresses can be seen [here](https://github.com/cosmos/cosmos-sdk/blob/ef8dabcf0f2ecaf26db1c6c6d5922e9399458bb3/x/bank/legacy/v042/store.go#L15).
Each module's migration functions are specific to the module's store evolutions, and are not described in this ADR. An example of x/bank store key migrations following the introduction of ADR-028 length-prefixed addresses can be seen [here](https://github.com/cosmos/cosmos-sdk/blob/36f68eb9e041e20a5bb47e216ac5eb8b91f95471/x/bank/legacy/v043/store.go#L41-L62).
Copy link
Contributor Author

Choose a reason for hiding this comment

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

amaury1093 marked this conversation as resolved.
Show resolved Hide resolved

### Tracking Module Versions in `x/upgrade`

Expand Down
74 changes: 48 additions & 26 deletions docs/core/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,28 @@ order: 9
## Pre-requisite Readings

- [Anatomy of an SDK application](../basics/app-anatomy.md) {prereq}
- [Tendermint Documentation on Events](https://docs.tendermint.com/master/spec/abci/abci.html#events) {prereq}

## Events

Events are implemented in the Cosmos SDK as an alias of the ABCI `Event` type and
take the form of: `{eventType}.{eventAttribute}={value}`.
take the form of: `{eventType}.{attributeKey}={attributeValue}`.

+++ https://github.com/tendermint/tendermint/blob/bc572217c07b90ad9cee851f193aaa8e9557cbc7/abci/types/types.pb.go#L2187-L2193
+++ https://github.com/tendermint/tendermint/blob/v0.34.8/proto/tendermint/abci/types.proto#L304-L313

Events contain:
An Event contains:

- A `type`, which is meant to categorize an event at a high-level (e.g. by module (e.g. `module=bank`) or action (e.g. `action=/cosmos.bank.v1beta1.Msg/Send`)).
- A list of `attributes`, which are key-value pairs that give more information about
the categorized `event`.
+++ https://github.com/cosmos/cosmos-sdk/blob/7d7821b9af132b0f6131640195326aa02b6751db/types/events.go#L51-L56
- A `type`, which is meant to categorize an event at a high-level, e.g. the SDK uses the `"message"` type to filter events by `Msg`s.
- A list of `attributes`, which are 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 using `message.action={some_action}`, `message.module={some_module}` or `message.sender={some_sender}`.
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved

::: tip
If you want the attribute values to be parsed 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 [`Msg` service](../building-modules/msg-services.md)
via the [`EventManager`](#eventmanager). In addition, each module documents its events under
`spec/xx_events.md`.
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved

Events are returned to the underlying consensus engine in the response of the following ABCI messages:

Expand All @@ -31,24 +39,31 @@ Events are returned to the underlying consensus engine in the response of the fo
- [`CheckTx`](./baseapp.md#checktx)
- [`DeliverTx`](./baseapp.md#delivertx)

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 [`Msg` service](../building-modules/msg-services.md)
via the [`EventManager`](#eventmanager). In addition, each module documents its events under
`spec/xx_events.md`.
### Examples

Below are some examples of Events you can query using the SDK.
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved

| 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`](../building-modules/msg-services.md). Note the `'`s around the value. |
| `message.action='send'` | Query all transactions containing a x/bank `Send` [legacy `Msg`](../building-modules/msg-services.md#legacy-amino-msgs). 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](../../../cosmos-sdk/x/staking/spec/07_events.md). |

## 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/7d7821b9af132b0f6131640195326aa02b6751db/types/events.go#L16-L20
+++ https://github.com/cosmos/cosmos-sdk/blob/21814558eaa47b018018711e5fe16e0b16811fce/types/events.go#L17-L25

The `EventManager` comes with a set of useful methods to manage events. Among them, the one that is
used the most by module and application developers is the `EmitEvent` method, which tracks
an `event` in the `EventManager`.
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved

+++ https://github.com/cosmos/cosmos-sdk/blob/7d7821b9af132b0f6131640195326aa02b6751db/types/events.go#L29-L31
+++ https://github.com/cosmos/cosmos-sdk/blob/21814558eaa47b018018711e5fe16e0b16811fce/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
Expand All @@ -61,6 +76,7 @@ ctx.EventManager().EmitEvent(
```

Module's `handler` function should also set a new `EventManager` to the `context` to isolate emitted events per `message`:

```go
func NewHandler(keeper Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
Expand All @@ -73,17 +89,17 @@ view on how to typically implement `Events` and use the `EventManager` in module

## Subscribing to Events

It is possible to subscribe to `Events` via Tendermint's [Websocket](https://tendermint.com/docs/app-dev/subscribing-to-events-via-websocket.html#subscribing-to-events-via-websocket).
It is possible to subscribe to `Events` via Tendermint's [Websocket](https://docs.tendermint.com/master/tendermint-core/subscription.html#subscribing-to-events-via-websocket).
This is done by calling the `subscribe` RPC method via Websocket:
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved

```json
{
"jsonrpc": "2.0",
"method": "subscribe",
"id": "0",
"params": {
"query": "tm.event='eventCategory' AND eventType.eventAttribute='attributeValue'"
}
"jsonrpc": "2.0",
"method": "subscribe",
"id": "0",
"params": {
"query": "tm.event='eventCategory' AND eventType.eventAttribute='attributeValue'"
}
}
```

Expand All @@ -100,17 +116,23 @@ The `type` and `attribute` value of the `query` allow you to filter the specific

```json
{
"jsonrpc": "2.0",
"method": "subscribe",
"id": "0",
"params": {
"query": "tm.event='Tx' AND transfer.sender='senderAddress'"
}
"jsonrpc": "2.0",
"method": "subscribe",
"id": "0",
"params": {
"query": "tm.event='Tx' AND transfer.sender='senderAddress'"
}
}
```

where `senderAddress` is an address following the [`AccAddress`](../basics/accounts.md#addresses) format.

## Typed Events (coming soon)

As described above, events are defined on a per-module basis, and it is the responsability of the module developer to define event types and event attributes. Except in the `spec/XX_events.md` file, these types and attributes are unfortunately not easily discoverable, so the SDK proposes to use Protobuf-defined [Typed Events](../architecture/adr-032-typed-events.md) for emitting and querying for events.

The Typed Events proposal has not yet been fully implemented, and its documentation will follow with a future release of the SDK.
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved

## Next {hide}

Learn about SDK [telemetry](./telemetry.md) {hide}