Skip to content

Conversation

deepdring
Copy link

@deepdring deepdring commented Aug 21, 2025

Optimize code using a more modern writing style. Official support from Go, for more details visit https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize.

PR Checklist:

  • Have you read the CONTRIBUTING.md?
  • Does your PR follow the C4 patch requirements?
  • Have you rebased your work on top of the latest master?
  • Have you checked your code compiles? (make)
  • Have you included tests for any non-trivial functionality?
  • Have you checked your code passes the unit tests? (make test)
  • Have you checked your code formatting is correct? (go fmt)
  • Have you checked your basic code style is fine? (golangci-lint run)
  • If you added any dependencies, have you checked they do not contain any known vulnerabilities? (go list -json -m all | nancy sleuth)
  • If your changes affect the client infrastructure, have you run the integration test?
  • If your changes affect public APIs, does your PR follow the C4 evolution of public contracts?
  • If your code changes public APIs, have you incremented the crate version numbers and documented your changes in the CHANGELOG.md?
  • If you are contributing for the first time, please read the agreement in CONTRIBUTING.md now and add a comment to this pull request stating that your PR is in accordance with the Developer's Certificate of Origin.

Thank you for your code, it's appreciated! :)

Summary by CodeRabbit

  • Refactor
    • Streamlined internal data copying to use a standard library approach while preserving existing behavior.
    • Simplified block replay height calculation to a single expression with the same effective bounds.
    • No changes to public APIs or user-facing functionality.

@deepdring deepdring requested a review from a team as a code owner August 21, 2025 09:53
@deepdring deepdring requested review from calvinaco and thomas-nguy and removed request for a team August 21, 2025 09:53
Copy link
Contributor

coderabbitai bot commented Aug 21, 2025

Walkthrough

Refactors internal logic in two areas: replaces a manual map-copy loop with maps.Copy in app/app.go, and simplifies context height calculation in x/cronos/rpc/api.go using max(blockNumber-1, 1). No public APIs changed; behavior preserved.

Changes

Cohort / File(s) Summary
Map copy refactor
app/app.go
Import maps package; replace manual map iteration with maps.Copy for shallow copying in GetMaccPerms.
Context height computation
x/cronos/rpc/api.go
Replace conditional assignment with max(blockNumber-1, 1) in CronosAPI.ReplayBlock.

Sequence Diagram(s)

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • calvinaco
  • thomas-nguy

Poem

I hop through code with nimble cheer,
Copy maps with less to fear.
One step back, but never none—
max keeps heights at minimum one.
Ears up high, refactors neat,
Tiny tweaks make tests compete.
Thump-thump! Another tidy feat.

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
app/app.go (1)

1346-1348: Prefer maps.Clone and pre-size; consider deep-copying slice values if immutability is desired.

Functional behavior is fine and matches the prior shallow-copy semantics. For clarity and a tiny perf win, either use maps.Clone or pre-size the map when using maps.Copy. If external callers must not mutate the inner []string, switch to a deep copy.

Use maps.Clone (simplest):

 func GetMaccPerms() map[string][]string {
-	dupMaccPerms := make(map[string][]string)
-	maps.Copy(dupMaccPerms, maccPerms)
-	return dupMaccPerms
+	return maps.Clone(maccPerms)
 }

Or keep maps.Copy but pre-size:

-dupMaccPerms := make(map[string][]string)
+dupMaccPerms := make(map[string][]string, len(maccPerms))
 maps.Copy(dupMaccPerms, maccPerms)

If you want immutability guarantees, deep copy the slice values:

func GetMaccPerms() map[string][]string {
	out := make(map[string][]string, len(maccPerms))
	for k, v := range maccPerms {
		out[k] = append([]string(nil), v...)
	}
	return out
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between cde48ef and 0a1d8e2.

📒 Files selected for processing (2)
  • app/app.go (2 hunks)
  • x/cronos/rpc/api.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: semgrep-cloud-platform/scan
  • GitHub Check: semgrep-cloud-platform/scan

@@ -8,6 +8,7 @@ import (
"fmt"
"io"
"io/fs"
"maps"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Confirm toolchain supports stdlib “maps” (Go 1.21+).

The new import relies on the standard library package maps, which requires Go 1.21+. It looks consistent with existing usage of slices (also Go 1.21+), but please confirm CI/tooling is pinned accordingly to avoid build breaks.

Run this script to verify the repo’s Go version and CI setup:


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "go.mod go/toolchain directives:"
rg -nP '^(go|toolchain)\s+\d' go.mod || true
sed -n '1,20p' go.mod | nl -ba

echo -e "\nCheck workflows pinning setup-go:"
fd -t f -a .github/workflows -e yml -e yaml | while read -r f; do
  echo "==> $f"
  rg -nP 'setup-go|go-version' "$f" || true
done

echo -e "\nSearch for local helpers named max/min that might mask missing stdlib cmp.Max/Min:"
rg -nP -C2 '(?m)^\s*func\s+(max|min)\s*\[.*\]\s*\(.*\)|^\s*func\s+(max|min)\s*\('

Length of output: 268


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Checking go.mod for go/toolchain directives:"
grep -nE '^\s*(go|toolchain)\s+' go.mod || echo "No go/toolchain directives found"

echo -e "\nInspecting top of go.mod (first 20 lines):"
head -n 20 go.mod

echo -e "\nScanning GitHub Actions workflows for Go version pins:"
find .github/workflows -type f \( -name '*.yml' -o -name '*.yaml' \) | while read -r wf; do
  echo "==> $wf"
  grep -nE 'setup-go|go-version' "$wf" || echo "  (no Go version pin found)"
done

echo -e "\nSearching for local max/min functions:"
rg -nP -C2 '(?m)^\s*func\s+(max|min)\s*\[' --color=always || echo "No generic max/min helpers detected"

Length of output: 5528


Ensure all CI workflows pin Go ≥1.21 (maps package available in Go 1.21+)

go.mod already specifies go 1.23.12, so the new maps import is supported by your toolchain. A search also found no local max/min helpers that could shadow stdlib functions.

However, CI is currently inconsistent:

  • Some workflows pin to older Go (1.22.x)
    .github/workflows/lint.yml uses go-version: '1.22.7'
    .github/workflows/sims.yml uses go-version: '^1.22.0'
  • Several workflows don’t install Go explicitly, relying on the runner’s default (which may be < 1.23)
    build.yml, container.yml, gosec.yml, lint-pr.yml, pr_labeler.yml, proto.yml, release.yml, semgrep.yml, test.yml

Please update each workflow that builds, tests, or lints Go code to include an explicit setup, for example:

- uses: actions/setup-go@v5
  with:
    go-version: '1.23.12'

Or use a semver range:

    go-version: '^1.23.0'

so that your CI always runs on a Go version compatible with the maps package.

🤖 Prompt for AI Agents
In app/app.go around line 11 the new "maps" import requires Go ≥1.21, but CI
uses older or unspecified Go versions; update all GitHub workflow files that
build/test/lint Go (at least .github/workflows/lint.yml, sims.yml, build.yml,
container.yml, gosec.yml, lint-pr.yml, pr_labeler.yml, proto.yml, release.yml,
semgrep.yml, test.yml) to explicitly install a compatible Go version (e.g., add
an actions/setup-go@v5 step with go-version: '1.23.12' or a semver range like
'^1.23.0') so the CI runners always use Go >=1.21 (prefer matching go.mod, e.g.,
1.23.12).

Comment on lines +282 to 285
contextHeight := max(blockNumber-1,
// 0 is a special value in `ContextWithHeight`
contextHeight = 1
}
1)
rsp, err := api.cronosQueryClient.ReplayBlock(rpctypes.ContextWithHeight(contextHeight), req)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

max is undefined here; this will not compile. Use cmp.Max or an explicit guard.

Go has no predeclared max identifier. Unless package rpc defines a local max[T] helper (it doesn’t in this file), this line won’t build. Use cmp.Max (Go 1.21+) or a simple conditional.

Apply one of the following fixes.

Option A — modern and concise (requires Go 1.21+):

@@
-import (
+import (
+	"cmp"
@@
-	// minus one to get the context of block beginning
-	contextHeight := max(blockNumber-1,
-		// 0 is a special value in `ContextWithHeight`
-		1)
+	// minus one to get the context of block beginning; avoid 0 (special "latest") height.
+	contextHeight := cmp.Max(blockNumber-1, int64(1))

Option B — minimal change, no new import:

-// minus one to get the context of block beginning
-contextHeight := max(blockNumber-1,
-	// 0 is a special value in `ContextWithHeight`
-	1)
+// minus one to get the context of block beginning; avoid 0 (special "latest") height.
+contextHeight := blockNumber - 1
+if contextHeight < 1 {
+	contextHeight = 1
}
🤖 Prompt for AI Agents
In x/cronos/rpc/api.go around lines 282-285, the call to max(...) is undefined
and will not compile; replace it either by using cmp.Max (add import "cmp") and
set contextHeight := cmp.Max(blockNumber-1, 1), or implement an explicit guard
without new imports: compute contextHeight by checking if blockNumber-1 < 1 then
contextHeight = 1 else contextHeight = blockNumber-1; update imports accordingly
if you choose cmp.Max.

@deepdring
Copy link
Author

friendly ping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant