feat: add mayachain rpc endpoint#1273
Conversation
📝 WalkthroughWalkthroughThe changes add an RPC proxy routing feature to the MayaChain API with a shared response handler for centralized proxy management. Existing LCD and Midgard handlers are refactored to use this helper. Documentation is updated with new RPC endpoints, and a Docker build dependency is pinned to a specific version for deterministic builds. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
go/coinstacks/mayachain/api/api.go (1)
394-407: Consider streaming the upstream body instead of unmarshal + re-marshal.
handleProxyResponsedecodes the upstream JSON intoanyand then re-encodes it viaapi.HandleResponse. For a pass-through proxy this is wasteful, reorders JSON object keys, loses numeric precision for large integers (tendermint RPC returns heights, IDs, etc. as numbers that can exceed JS safe-int range; after round-tripping throughany, largeint64values becomefloat64), and only acceptsapplication/jsonupstream responses. Writing the raw bytes through preserves fidelity and is cheaper.♻️ Suggested refactor
func (a *API) handleProxyResponse(w http.ResponseWriter, res *resty.Response) { if res.StatusCode() != http.StatusOK { api.HandleError(w, res.StatusCode(), string(res.Body())) return } - var result any - if err := json.Unmarshal(res.Body(), &result); err != nil { - api.HandleError(w, http.StatusInternalServerError, err.Error()) - return - } - - api.HandleResponse(w, http.StatusOK, result) + if ct := res.Header().Get("Content-Type"); ct != "" { + w.Header().Set("Content-Type", ct) + } + w.WriteHeader(http.StatusOK) + _, _ = w.Write(res.Body()) }Note: this would make the LCD/Midgard handlers also pass through raw upstream bytes, which is a behavior change worth a quick sanity-check against any clients relying on the previous re-encoded form.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@go/coinstacks/mayachain/api/api.go` around lines 394 - 407, The handleProxyResponse function currently unmarshals upstream JSON into a Go any and then re-encodes via api.HandleResponse, which is inefficient and can change numeric precision and key ordering; change handleProxyResponse to stream the raw response body bytes and copy headers/status directly to the client instead of json.Unmarshal/json.Marshal: read res.Body() (or io.Copy from res.RawResponse.Body if available) and write those bytes to w while preserving Content-Type and status code, and use api.HandleError only on non-200 responses or read errors; update references in handleProxyResponse to stop using the intermediate result variable and avoid api.HandleResponse re-encoding.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@go/coinstacks/mayachain/api/api.go`:
- Around line 344-359: The POST /rpc handler should explicitly forward the
incoming request's Content-Type (and other proxy-relevant headers) to the resty
request and avoid shadowing err: replace body, err := io.ReadAll(r.Body) with
body, readErr := io.ReadAll(r.Body) and handle readErr, and after setting the
body call req.SetHeader("Content-Type", r.Header.Get("Content-Type")) (and
optionally forward headers like "Content-Encoding", "Authorization", "Accept")
before calling req.Post(path) so the proxied request preserves header fidelity.
---
Nitpick comments:
In `@go/coinstacks/mayachain/api/api.go`:
- Around line 394-407: The handleProxyResponse function currently unmarshals
upstream JSON into a Go any and then re-encodes via api.HandleResponse, which is
inefficient and can change numeric precision and key ordering; change
handleProxyResponse to stream the raw response body bytes and copy
headers/status directly to the client instead of json.Unmarshal/json.Marshal:
read res.Body() (or io.Copy from res.RawResponse.Body if available) and write
those bytes to w while preserving Content-Type and status code, and use
api.HandleError only on non-200 responses or read errors; update references in
handleProxyResponse to stop using the intermediate result variable and avoid
api.HandleResponse re-encoding.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 1596b06d-398c-437c-b616-3096b791f9ab
📒 Files selected for processing (3)
go/coinstacks/mayachain/api/api.gogo/coinstacks/mayachain/api/swagger.jsongo/coinstacks/mayachain/build/Dockerfile.local
Summary by CodeRabbit
New Features
/rpcAPI endpoint supporting GET and POST requests for RPC proxy services.Documentation
/rpcendpoint specifications.