2018-06-26 14:21:46 -07:00
|
|
|
# Handlers
|
2018-06-16 19:24:48 -07:00
|
|
|
|
|
|
|
A handler takes a context and a message and returns a result. All
|
|
|
|
information necessary for processing a message should be available in the
|
|
|
|
context.
|
|
|
|
|
2018-06-16 22:56:53 -07:00
|
|
|
While the context holds the entire application state (ie. the
|
|
|
|
MultiStore), handlers are restricted in what they can do based on the
|
|
|
|
capabilities they were given when the application was set up.
|
|
|
|
|
|
|
|
For instance, suppose we have a `newFooHandler`:
|
2018-06-16 19:24:48 -07:00
|
|
|
|
|
|
|
```go
|
2018-06-16 22:56:53 -07:00
|
|
|
func newFooHandler(key sdk.StoreKey) sdk.Handler {
|
|
|
|
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
|
|
|
|
store := ctx.KVStore(key)
|
|
|
|
// ...
|
|
|
|
}
|
2018-06-16 19:24:48 -07:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-06-16 22:56:53 -07:00
|
|
|
This handler can only access one store based on whichever key its given.
|
|
|
|
So when we register the handler for the `foo` message type, we make sure
|
|
|
|
to give it the `fooKey`:
|
|
|
|
|
|
|
|
```
|
|
|
|
app.Router().AddRoute("foo", newFooHandler(fooKey))
|
|
|
|
```
|
|
|
|
|
|
|
|
Now it can only access the `foo` store, but not the `bar` or `cat` stores!
|
2018-06-26 14:21:46 -07:00
|
|
|
|