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

Environment in context #19640

Closed
julienrbrt opened this issue Mar 4, 2024 · 2 comments · Fixed by #20529
Closed

Environment in context #19640

julienrbrt opened this issue Mar 4, 2024 · 2 comments · Fixed by #20529
Assignees
Labels
C:server/v2 Issues related to server/v2 C:x/authz C:x/feegrant C:x/gov T: Dev UX UX for SDK developers (i.e. how to call our code)

Comments

@julienrbrt
Copy link
Member

julienrbrt commented Mar 4, 2024

With the migration of cosmos sdk modules to be compatible with server/v2, modules must not use sdk.UnwrapSDKContext.
The environment provides everything a module needs.

The goal is to keep cosmos sdk modules compatible with baseapp and server/v2, with the minimum api break possible.
However, there are a few places where the SDK context is still required, and it cannot be easily refactored by taking an environment due to the api/interface breakage this would bring.

x/feegrant

feegrant.FeeAllowanceI Accept method takes only a sdk.Context:

func (a *BasicAllowance) Accept(ctx context.Context, fee sdk.Coins, _ []sdk.Msg) (bool, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)

x/authz

Like feegrant, authz Accept method takes only a sdk.Context:

resp, err := authorization.Accept(sdkCtx, msg)

x/gov legacy proposals

x/gov v1beta1 proposals handler takes only a sdk.Context:

type Handler func(ctx sdk.Context, content Content) error

Ante Handlers

Lastly, some ante handlers, such as TxFeeChecker, need access to services provided by the environment:

// TxFeeChecker check if the provided fee is enough and returns the effective fee and tx priority,
// the effective fee should be deducted later, and the priority should be returned in abci response.
type TxFeeChecker func(ctx sdk.Context, tx sdk.Tx) (sdk.Coins, int64, error)

Solutions

A few solutions have been discussed, but consensus haven't been reached.

  • One solution is to have those modules set their environment in context before passing it to those methods.
    We could make the caller aware that it is a module-specific context by having a module-scope context key: FeeGrantEnvContextKey, AuthzEnvConextKey, GovEnvContextKey.
    Drawback: The module developer needs then to be aware of which module calls the method and unwrap the right context.
    The module implementing those methods has no access to their environment.

  • A second proposal is to store a subset of appmodule.Environment containing non module-scoped services
    Drawback: It adds a new notion differing from the environment, and may not be compatible with gov v1beta1 proposal handler usage (needs to be checked on sourcegraph.com).

  • The third proposal is to add environment in the interfaces
    Drawback: It breaks everyone, and still has the same drawbacks as 1.

We are as well thinking of deprecating gov v1beta1 proposals totally or only for server/v2 if none of the solution above is a right fit.

@julienrbrt julienrbrt added T: Dev UX UX for SDK developers (i.e. how to call our code) C:server/v2 Issues related to server/v2 labels Mar 4, 2024
@github-project-automation github-project-automation bot moved this to 👀 To Do in Cosmos-SDK Mar 4, 2024
@kocubinski
Copy link
Member

I'm in favor of solution (1) here, I think it's an appropriate use of context.WithValue, the godoc reads

// Use context Values only for request-scoped data that transits processes and
// APIs, not for passing optional parameters to functions.

This is request (and module) scope data required for state machine execution that transits APIs. I prefer scoping to module since it will force us to be more careful with this pattern and it should be an exception to normal flow not the rule.

@julienrbrt
Copy link
Member Author

We could make the caller aware that it is a module-specific context by having a module-scope context key: FeeGrantEnvContextKey, AuthzEnvConextKey, GovEnvContextKey.
Drawback: The module developer needs then to be aware of which module calls the method and unwrap the right context.
The module implementing those methods has no access to their environment.

Unfortunately, this wasn't possible, as we had to make the module defining an authz grant or a feegrant import x/authz or x/feegrant for the context key. As we want to avoid unnecessary deps, we have made a singel EnvironementContextKey available in core and precise in the documentation to not assume this key is present in all contexts.

@tac0turtle tac0turtle moved this from 📋 Backlog to 🤸‍♂️ In Progress in Cosmos-SDK Jun 3, 2024
@github-project-automation github-project-automation bot moved this from 🤸‍♂️ In Progress to 🥳 Done in Cosmos-SDK Jun 3, 2024
@tac0turtle tac0turtle removed this from Cosmos-SDK Jul 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:server/v2 Issues related to server/v2 C:x/authz C:x/feegrant C:x/gov T: Dev UX UX for SDK developers (i.e. how to call our code)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants