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

Abstract client.Context from client/v2 to remove dependency on client (pass directly address codecs and interface registry) #22397

Open
Tracked by #18580
JulianToledano opened this issue Oct 30, 2024 · 0 comments
Assignees
Labels
C:client/v2 client/v2 package

Comments

@JulianToledano
Copy link
Contributor

JulianToledano commented Oct 30, 2024

Currently client.Context has some really useful functionalities that should be kept in v2.

  1. The context is responsible for formatting the result and writing to the output writer:
    // PrintString prints the raw string to ctx.Output if it's defined, otherwise to os.Stdout
    func (ctx Context) PrintString(str string) error {
    return ctx.PrintBytes([]byte(str))
    }
    // PrintBytes prints the raw bytes to ctx.Output if it's defined, otherwise to os.Stdout.
    // NOTE: for printing a complex state object, you should use ctx.PrintOutput
    func (ctx Context) PrintBytes(o []byte) error {
    writer := ctx.Output
    if writer == nil {
    writer = os.Stdout
    }
    _, err := writer.Write(o)
    return err
    }
    // PrintProto outputs toPrint to the ctx.Output based on ctx.OutputFormat which is
    // either text or json. If text, toPrint will be YAML encoded. Otherwise, toPrint
    // will be JSON encoded using ctx.Codec. An error is returned upon failure.
    func (ctx Context) PrintProto(toPrint proto.Message) error {
    // always serialize JSON initially because proto json can't be directly YAML encoded
    out, err := ctx.Codec.MarshalJSON(toPrint)
    if err != nil {
    return err
    }
    return ctx.printOutput(out)
    }
    // PrintRaw is a variant of PrintProto that doesn't require a proto.Message type
    // and uses a raw JSON message. No marshaling is performed.
    func (ctx Context) PrintRaw(toPrint json.RawMessage) error {
    return ctx.printOutput(toPrint)
    }
    func (ctx Context) printOutput(out []byte) error {
    var err error
    if ctx.OutputFormat == "text" {
    out, err = yaml.JSONToYAML(out)
    if err != nil {
    return err
    }
    }
    writer := ctx.Output
    if writer == nil {
    writer = os.Stdout
    }
    _, err = writer.Write(out)
    if err != nil {
    return err
    }
    if ctx.OutputFormat != "text" {
    // append new-line for formats besides YAML
    _, err = writer.Write([]byte("\n"))
    if err != nil {
    return err
    }
    }
    return nil
    }
  2. Parse the config file and creates a default one when this doesn't exist. This is really convenient for the user. For example don't have to specify the chain-id for each command:
    // ReadFromClientConfig reads values from client.toml file and updates them in client.Context
    // It uses CreateClientConfig internally with no custom template and custom config.
    // Deprecated: use CreateClientConfig instead.
    func ReadFromClientConfig(ctx client.Context) (client.Context, error) {
    return CreateClientConfig(ctx, "", nil)
    }
    // CreateClientConfig reads the client.toml file and returns a new populated client.Context
    // If the client.toml file does not exist, it creates one with default values.
    // It takes a customClientTemplate and customConfig as input that can be used to overwrite the default config and enhance the client.toml file.
    // The custom template/config must be both provided or be "" and nil.
    func CreateClientConfig(ctx client.Context, customClientTemplate string, customConfig interface{}) (client.Context, error) {
  3. Parse the from flag so address or key name can be used interchangeably.
    // GetFromFields returns a from account address, account name and keyring type, given either an address or key name.
    // If clientCtx.Simulate is true the keystore is not accessed and a valid address must be provided
    // If clientCtx.GenerateOnly is true the keystore is only accessed if a key name is provided
    // If from is empty, the default key if specified in the context will be used
    func GetFromFields(clientCtx Context, kr keyring.Keyring, from string) (sdk.AccAddress, string, keyring.KeyType, error) {
  4. It also implements grpc.ClientConn
    // Invoke implements the grpc ClientConn.Invoke method
    func (ctx Context) Invoke(grpcCtx gocontext.Context, method string, req, reply interface{}, opts ...grpc.CallOption) (err error) {
    // Two things can happen here:
    // 1. either we're broadcasting a Tx, in which call we call CometBFT's broadcast endpoint directly,
    // 2-1. or we are querying for state, in which case we call grpc if grpc client set.
    // 2-2. or we are querying for state, in which case we call ABCI's Query if grpc client not set.
@github-actions github-actions bot added the needs-triage Issue that needs to be triaged label Oct 30, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Cosmos-SDK Oct 30, 2024
@JulianToledano JulianToledano changed the title Abstract from client.Context to remove dependency on client (pass directly address codecs and interface registry) Abstract client.Context from client/v2 to remove dependency on client (pass directly address codecs and interface registry) Oct 30, 2024
@JulianToledano JulianToledano added C:client/v2 client/v2 package and removed needs-triage Issue that needs to be triaged labels Oct 30, 2024
@julienrbrt julienrbrt moved this from 📋 Backlog to 🤸‍♂️ In Progress in Cosmos-SDK Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:client/v2 client/v2 package
Projects
Status: 🤸‍♂️ In Progress
Development

No branches or pull requests

1 participant