-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
R4R: keeper custom queries #1918
Changes from all commits
1d1a956
804baa7
cc1d1fb
1311117
0eed9d1
0134c3b
97f7b88
44bf69e
50dd53f
5ae20d2
7f43d3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package baseapp | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// QueryRouter provides queryables for each query path. | ||
type QueryRouter interface { | ||
AddRoute(r string, h sdk.Querier) (rtr QueryRouter) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. h - handler, right? what's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. router I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is fine IMO |
||
Route(path string) (h sdk.Querier) | ||
} | ||
|
||
type queryrouter struct { | ||
routes map[string]sdk.Querier | ||
} | ||
|
||
// nolint | ||
// NewRouter - create new router | ||
// TODO either make Function unexported or make return type (router) Exported | ||
func NewQueryRouter() *queryrouter { | ||
return &queryrouter{ | ||
routes: map[string]sdk.Querier{}, | ||
} | ||
} | ||
|
||
// AddRoute - Adds an sdk.Querier to the route provided. Panics on duplicate | ||
func (rtr *queryrouter) AddRoute(r string, q sdk.Querier) QueryRouter { | ||
if !isAlphaNumeric(r) { | ||
panic("route expressions can only contain alphanumeric characters") | ||
} | ||
if rtr.routes[r] != nil { | ||
panic("route has already been initialized") | ||
} | ||
rtr.routes[r] = q | ||
return rtr | ||
} | ||
|
||
// Returns the sdk.Querier for a certain route path | ||
func (rtr *queryrouter) Route(path string) (h sdk.Querier) { | ||
return rtr.routes[path] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,6 +74,9 @@ func (app *BaseApp) Router() Router { | |
} | ||
return app.router | ||
} | ||
func (app *BaseApp) QueryRouter() QueryRouter { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does sealing do on the baseapp and do we need to seal the query router as well? anything else we're missing? docs would be helpful! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why we have the seal it. I just saw that the router was sealed, so I sealed the QueryRouter as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I think we should restrict sealing to fields that actually need to be sealed for clarity, otherwise readers of the code will have a hard time figuring out what our intent was (as apparently we ourselves have a hard time figuring out already!). As I understand, this is just defensive coding - I believe @mossid wrote the original PR - and we only need to do it for write-once fields or fields that should not be read from after a certain event in the app lifecycle. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, don't we want to query the QueryRouter after the app is sealed? |
||
return app.queryRouter | ||
} | ||
func (app *BaseApp) Seal() { app.sealed = true } | ||
func (app *BaseApp) IsSealed() bool { return app.sealed } | ||
func (app *BaseApp) enforceSeal() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
|
||
The Cosmos SDK has all the necessary pre-built modules to add functionality on top of a `BaseApp`, which is the template to build a blockchain dApp in Cosmos. In this context, a `module` is a fundamental unit in the Cosmos SDK. | ||
|
||
Each module is an extension of the `BaseApp`'s functionalities that defines transactions, handles application state and manages the state transition logic. Each module also contains handlers for messages and transactions, as well as REST and CLI for secure user interactions. | ||
Each module is an extension of the `BaseApp`'s functionalities that defines transactions, handles application state and manages the state transition logic. Each module also contains handlers for messages and transactions, queriers for handling query requests, as well as REST and CLI for secure user interactions. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd add a more detailed description on how There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
Some of the most important modules in the SDK are: | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package types | ||
|
||
import abci "github.com/tendermint/tendermint/abci/types" | ||
|
||
// Type for querier functions on keepers to implement to handle custom queries | ||
type Querier = func(ctx Context, path []string, req abci.RequestQuery) (res []byte, err Error) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a ResponseQuery.Log where we can put the err.String() in? That's for nondeterministic debug info like this.