Merge PR #5583: Add to the docs of events and handler to set a new EventManager to the Context

This commit is contained in:
Nicolas Mahé
2020-01-29 08:43:48 -05:00
committed by GitHub
parent ec35cf2d91
commit ca952bc9c6
2 changed files with 11 additions and 1 deletions
+3 -1
View File
@@ -34,6 +34,7 @@ which looks like the following:
```go
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) {
case MsgType1:
return handleMsgType1(ctx, keeper, msg)
@@ -48,7 +49,8 @@ func NewHandler(keeper Keeper) sdk.Handler {
}
```
This simple switch returns a `handler` function specific to the type of the received `message`. These `handler` functions are the ones that actually process `message`s, and usually follow the following 2 steps:
First, the `handler` function sets a new `EventManager` to the context to isolate events per `msg`.
Then, this simple switch returns a `handler` function specific to the type of the received `message`. These `handler` functions are the ones that actually process `message`s, and usually follow the following 2 steps:
- First, they perform *stateful* checks to make sure the `message` is valid. At this stage, the `message`'s `ValidateBasic()` method has already been called, meaning *stateless* checks on the message (like making sure parameters are correctly formatted) have already been performed. Checks performed in the `handler` can be more expensive and require access to the state. For example, a `handler` for a `transfer` message might check that the sending account has enough funds to actually perform the transfer. To access the state, the `handler` needs to call the [`keeper`'s](./keeper.md) getter functions.
- Then, if the checks are successfull, the `handler` calls the [`keeper`'s](./keeper.md) setter functions to actually perform the state transition.