feat(cli): use either Repository or JSON-RPC API to read data#738
feat(cli): use either Repository or JSON-RPC API to read data#738renatomaia wants to merge 5 commits intofeatures/cli-read-prt-datafrom
Conversation
efdc751 to
305b9fd
Compare
01145bf to
5ded42b
Compare
There was a problem hiding this comment.
Pull request overview
This PR introduces a ReadService abstraction for the CLI read subcommands so they can fetch data either from the local DB (via repository) or from a remote JSON-RPC endpoint, aligning CLI “read” outputs with the JSON-RPC API shapes.
Changes:
- Added
internal/read.ReadServiceinterface plus DB-backed (RepositoryReadService) and JSON-RPC-backed (JsonrpcReadService) implementations. - Updated multiple CLI
readsubcommands to use the new datasource abstraction. - Added a new config/env var/flag for selecting a remote JSON-RPC endpoint (
CARTESI_JSONRPC_ENDPOINT,--jsonrpc-endpoint).
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 30 comments.
Show a summary per file
| File | Description |
|---|---|
| internal/read/types.go | Introduces ReadService interface for read operations. |
| internal/read/repository.go | Implements DB-backed read service using repository and marshals JSON-RPC-shaped responses. |
| internal/read/jsonrpc.go | Implements JSON-RPC-backed read service using the JSON-RPC client. |
| internal/config/generated.go | Adds CARTESI_JSONRPC_ENDPOINT to generated config and loads it into node/jsonrpc configs. |
| internal/config/generate/Config.toml | Declares CARTESI_JSONRPC_ENDPOINT in the config generator inputs. |
| cmd/cartesi-rollups-cli/root/root.go | Adds --jsonrpc-endpoint flag (bound to config). |
| cmd/cartesi-rollups-cli/root/read//.go | Switches subcommands to use ReadService and JSON-RPC params types. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| Cmd.PersistentFlags().StringVar(&databaseConnection, "jsonrpc-endpoint", "", | ||
| "JSON-RPC endpoint string in the URL format\n(eg.: 'https://localhost:10011/rpc')") | ||
| cobra.CheckErr(viper.BindPFlag(config.JSONRPC_ENDPOINT, Cmd.PersistentFlags().Lookup("jsonrpc-endpoint"))) | ||
| cobra.CheckErr(Cmd.PersistentFlags().MarkHidden("jsonrpc-endpoint")) |
There was a problem hiding this comment.
--jsonrpc-endpoint is marked hidden, but issue #723/PR intent is to expose and document this flag/env var for selecting the datasource. Consider not hiding it (or at least unhide it in read help output) so it shows up in --help as required.
| // Add epoch index filter if provided | ||
| if cmd.Flags().Changed("epoch-index") { | ||
| filter.EpochIndex = &epochIndex | ||
| params.EpochIndex = &epochIndex | ||
| } |
There was a problem hiding this comment.
--epoch-index / --input-index are described as "decimal or hex encoded", but the JSON-RPC API expects 0x... hex strings (see internal/jsonrpc.ParseIndex). If decimals should remain supported, normalize these flag values to hex before setting params.EpochIndex/params.InputIndex.
| addr, err := config.GetJsonrpcEndpoint() | ||
| cobra.CheckErr(err) | ||
|
|
||
| repo, err := factory.NewRepositoryFromConnectionString(ctx, dsn.String()) | ||
| if addr != "" { | ||
| readServ, err = read.NewJsonrpcReadService(ctx, addr) | ||
| } else { |
There was a problem hiding this comment.
config.GetJsonrpcEndpoint() returns ErrNotDefined when unset, so cobra.CheckErr(err) prevents falling back to the DB datasource. Treat ErrNotDefined as "endpoint not provided" (addr="") instead of a fatal error.
| addr, err := config.GetJsonrpcEndpoint() | ||
| cobra.CheckErr(err) | ||
|
|
||
| repo, err := factory.NewRepositoryFromConnectionString(ctx, dsn.String()) | ||
| if addr != "" { | ||
| readServ, err = read.NewJsonrpcReadService(ctx, addr) | ||
| } else { |
There was a problem hiding this comment.
config.GetJsonrpcEndpoint() returns ErrNotDefined when unset, so cobra.CheckErr(err) prevents falling back to the DB datasource. Treat ErrNotDefined as "endpoint not provided" (addr="") instead of a fatal error.
| Cmd.PersistentFlags().StringVar(&databaseConnection, "jsonrpc-endpoint", "", | ||
| "JSON-RPC endpoint string in the URL format\n(eg.: 'https://localhost:10011/rpc')") | ||
| cobra.CheckErr(viper.BindPFlag(config.JSONRPC_ENDPOINT, Cmd.PersistentFlags().Lookup("jsonrpc-endpoint"))) |
There was a problem hiding this comment.
Issue #723 proposes --jsonrpc-endpoint with env var CARTESI_JSONRPC_API_ADDRESS, but the implementation introduces a new env var CARTESI_JSONRPC_ENDPOINT. If the issue text is the source of truth, consider reusing CARTESI_JSONRPC_API_ADDRESS (or clearly documenting why a new variable is needed and how it relates to the existing one).
| // Create argumment variables | ||
| params.Application = args[0] | ||
| params.OutputIndex = args[1] | ||
|
|
||
| response = map[string]any{ | ||
| "data": dataVal, | ||
| } | ||
| result, err = readServ.GetOutput(ctx, params) |
There was a problem hiding this comment.
params.OutputIndex = args[1] passes the user-provided index through unchanged, but the JSON-RPC API requires 0x... hex strings for indices. This also makes the example ... outputs echo-dapp 10 invalid. Normalize decimal-or-hex input into a 0x... string before calling the datasource.
| // Create argumment variables | ||
| params.Application = args[0] | ||
| params.EpochIndex = args[1] | ||
| params.TournamentAddress = args[2] | ||
| params.Commitment = args[3] |
There was a problem hiding this comment.
params.EpochIndex = args[1] passes the user-provided epoch index through unchanged, but the JSON-RPC API requires 0x... hex strings for indices. This also makes the example ... commitments echo-dapp 10 ... invalid. Normalize decimal-or-hex input into a 0x... string before calling the datasource.
| // Create argumment variables | ||
| params.Application = args[0] | ||
| params.InputIndex = args[1] | ||
|
|
||
| response = map[string]any{ | ||
| "data": dataVal, | ||
| } | ||
| result, err = readServ.GetInput(ctx, params) |
There was a problem hiding this comment.
params.InputIndex = args[1] passes user input through unchanged, but the JSON-RPC API requires 0x... hex strings for indices. This also makes the example ... inputs echo-dapp 10 invalid. Consider parsing decimal-or-hex input and normalizing it to a 0x... string before calling the datasource.
| addr, err := config.GetJsonrpcEndpoint() | ||
| cobra.CheckErr(err) | ||
|
|
||
| repo, err := factory.NewRepositoryFromConnectionString(ctx, dsn.String()) | ||
| if addr != "" { | ||
| readServ, err = read.NewJsonrpcReadService(ctx, addr) | ||
| } else { |
There was a problem hiding this comment.
config.GetJsonrpcEndpoint() returns ErrNotDefined when unset, so cobra.CheckErr(err) prevents falling back to the DB datasource. Treat ErrNotDefined as "endpoint not provided" (addr="") instead of a fatal error.
| // Add epoch index filter if provided | ||
| if cmd.Flags().Changed("epoch-index") { | ||
| filter.EpochIndex = &epochIndex | ||
| params.EpochIndex = &epochIndex | ||
| } |
There was a problem hiding this comment.
--epoch-index / --level are described as "decimal or hex encoded", but the JSON-RPC API expects 0x... hex strings for these fields (see internal/jsonrpc.ParseIndex). If decimals should remain supported, normalize these flag values to hex before setting params.EpochIndex/params.Level (and update examples accordingly).
5ded42b to
18aea27
Compare
e84cea7 to
1b7160a
Compare
18aea27 to
a23cf08
Compare
1b7160a to
d93c155
Compare
6a0c122 to
2f44070
Compare
d93c155 to
ac88e77
Compare
ac88e77 to
90c3e94
Compare
2f44070 to
e7b09d6
Compare
closes #723