Skip to content

Conversation

@m-Peter
Copy link
Collaborator

@m-Peter m-Peter commented Jul 11, 2025

Description

Run the modernize analyzer to simplify code by using modern constructs .


For contributor use:

  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the standards mentioned here.
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

Summary by CodeRabbit

  • Refactor

    • Updated usage of Go's empty interface type to the modern any alias throughout the codebase for improved clarity and consistency.
    • Simplified certain loop constructs and logic expressions for improved code readability and maintainability.
  • Tests

    • Updated test files to use more concise and idiomatic Go loop syntax, enhancing code style without affecting test coverage or behavior.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 11, 2025

Walkthrough

This change replaces all uses of the empty interface type interface{} with the Go 1.18+ alias any throughout the codebase, affecting function signatures, struct fields, and map types. Additionally, several for-loops are refactored to use range-based iteration over integer literals or slices, and some logic is simplified using helper functions like min for concise expressions.

Changes

Files/Areas Change Summary
api/api.go, eth/types/types.go, models/errors/errors.go, api/server.go, bootstrap/bootstrap.go Replaced interface{} with any in method signatures, struct fields, and map types
bootstrap/create-multi-key-account.go, models/events_test.go, models/stream_test.go, services/ingestion/engine_test.go, services/ingestion/event_subscriber_test.go, storage/index_test.go, storage/pebble/storage_test.go Refactored classic for-loops to use range-based iteration over integer literals or slices
services/ingestion/event_subscriber.go, services/requester/requester.go, api/api.go Simplified logic using helper functions like min for concise expressions
storage/mocks/mocks.go Updated byte slice formatting to use fmt.Appendf instead of []byte(fmt.Sprintf(...))

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant API
    participant Blockchain
    Client->>API: Call Syncing()
    API->>Blockchain: Query sync status
    Blockchain-->>API: Return status (any)
    API-->>Client: Return status (any)

    Client->>API: Call GetTransactionReceipt(hash)
    API->>Blockchain: Query receipt for hash
    Blockchain-->>API: Return receipt (map[string]any)
    API-->>Client: Return receipt (map[string]any)
Loading

Possibly related PRs

  • Local index register validator #550: Also modifies api/api.go, changing method signatures and internal logic for Syncing and GetTransactionReceipt, overlapping with this PR's focus on method signatures and return types.

Suggested labels

Improvement, Technical Debt

Suggested reviewers

  • peterargue
  • zhangchiqing

Poem

A hop, a skip, a type replaced,
From interface{} to any—no time to waste!
Loops now range, so neat and spry,
Less code to read, less code to sigh.
With modern Go, we leap ahead—
This rabbit’s proud of what you’ve led! 🐇

✨ Finishing Touches
  • 📝 Generate Docstrings

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.
    • Explain this complex logic.
    • 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. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

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

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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

Documentation and Community

  • 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: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f540b99 and 1405b66.

📒 Files selected for processing (15)
  • api/api.go (4 hunks)
  • api/server.go (1 hunks)
  • bootstrap/bootstrap.go (1 hunks)
  • bootstrap/create-multi-key-account.go (1 hunks)
  • eth/types/types.go (2 hunks)
  • models/errors/errors.go (1 hunks)
  • models/events_test.go (7 hunks)
  • models/stream_test.go (3 hunks)
  • services/ingestion/engine_test.go (1 hunks)
  • services/ingestion/event_subscriber.go (1 hunks)
  • services/ingestion/event_subscriber_test.go (2 hunks)
  • services/requester/requester.go (1 hunks)
  • storage/index_test.go (6 hunks)
  • storage/mocks/mocks.go (2 hunks)
  • storage/pebble/storage_test.go (1 hunks)
🧰 Additional context used
🧠 Learnings (10)
📓 Common learnings
Learnt from: m-Peter
PR: onflow/flow-evm-gateway#831
File: tests/tx_batching_test.go:95-104
Timestamp: 2025-06-17T10:22:19.395Z
Learning: Go 1.22 introduced the ability to use `range` directly on integer values. The syntax `for i := range n` where `n` is an integer will iterate from 0 to n-1, equivalent to `for i := 0; i < n; i++`. This is valid Go syntax and should not be flagged as an error.
Learnt from: m-Peter
PR: onflow/flow-evm-gateway#831
File: services/requester/batch_tx_pool.go:0-0
Timestamp: 2025-06-19T11:36:25.478Z
Learning: In Go, when copying maps that contain slices (like `map[gethCommon.Address][]pooledEvmTx`), perform deep copies by iterating over the map and copying each slice individually using `make()` and `copy()` to avoid shared references that could lead to race conditions and data corruption.
storage/pebble/storage_test.go (2)
Learnt from: m-Peter
PR: onflow/flow-evm-gateway#831
File: tests/tx_batching_test.go:95-104
Timestamp: 2025-06-17T10:22:19.395Z
Learning: Go 1.22 introduced the ability to use `range` directly on integer values. The syntax `for i := range n` where `n` is an integer will iterate from 0 to n-1, equivalent to `for i := 0; i < n; i++`. This is valid Go syntax and should not be flagged as an error.
Learnt from: peterargue
PR: onflow/flow-evm-gateway#615
File: services/traces/engine.go:190-190
Timestamp: 2024-10-17T18:04:41.062Z
Learning: In `services/traces/engine.go`, the `e.indexBlockTraces` function limits the number of goroutines to a maximum of 5 and blocks until all are complete, so during backfill, concurrency is already managed to prevent resource exhaustion.
bootstrap/create-multi-key-account.go (4)
Learnt from: m-Peter
PR: onflow/flow-evm-gateway#831
File: tests/tx_batching_test.go:95-104
Timestamp: 2025-06-17T10:22:19.395Z
Learning: Go 1.22 introduced the ability to use `range` directly on integer values. The syntax `for i := range n` where `n` is an integer will iterate from 0 to n-1, equivalent to `for i := 0; i < n; i++`. This is valid Go syntax and should not be flagged as an error.
Learnt from: m-Peter
PR: onflow/flow-evm-gateway#831
File: services/requester/batch_tx_pool.go:0-0
Timestamp: 2025-06-19T11:36:25.478Z
Learning: In Go, when copying maps that contain slices (like `map[gethCommon.Address][]pooledEvmTx`), perform deep copies by iterating over the map and copying each slice individually using `make()` and `copy()` to avoid shared references that could lead to race conditions and data corruption.
Learnt from: m-Peter
PR: onflow/flow-evm-gateway#831
File: services/requester/pool.go:202-214
Timestamp: 2025-06-17T10:29:35.941Z
Learning: In Flow EVM Gateway's transaction pool (services/requester/pool.go), signing keys acquired with keystore.Take() are intentionally held until the transaction is sealed, not released immediately after sending. The key release happens asynchronously when sealed transactions are ingested via event_subscriber.go NotifyTransaction() -> keystore unsafeUnlockKey() -> key.Done(). This pattern ensures transaction integrity by preventing key reuse before transaction finalization.
Learnt from: m-Peter
PR: onflow/flow-evm-gateway#831
File: services/requester/pool.go:202-214
Timestamp: 2025-06-17T10:29:35.941Z
Learning: In Flow EVM Gateway's transaction pool (services/requester/pool.go), signing keys acquired with keystore.Take() are intentionally held until the transaction is sealed, not released immediately after sending. The key release happens asynchronously when sealed transactions are ingested via event_subscriber.go NotifyTransaction() -> keystore unsafeUnlockKey() -> key.Done(). This pattern ensures transaction integrity by preventing key reuse before transaction finalization.
storage/index_test.go (4)
Learnt from: m-Peter
PR: onflow/flow-evm-gateway#831
File: tests/tx_batching_test.go:95-104
Timestamp: 2025-06-17T10:22:19.395Z
Learning: Go 1.22 introduced the ability to use `range` directly on integer values. The syntax `for i := range n` where `n` is an integer will iterate from 0 to n-1, equivalent to `for i := 0; i < n; i++`. This is valid Go syntax and should not be flagged as an error.
Learnt from: m-Peter
PR: onflow/flow-evm-gateway#831
File: services/requester/batch_tx_pool.go:0-0
Timestamp: 2025-06-19T11:36:25.478Z
Learning: In Go, when copying maps that contain slices (like `map[gethCommon.Address][]pooledEvmTx`), perform deep copies by iterating over the map and copying each slice individually using `make()` and `copy()` to avoid shared references that could lead to race conditions and data corruption.
Learnt from: peterargue
PR: onflow/flow-evm-gateway#617
File: api/stream.go:62-67
Timestamp: 2024-10-18T19:26:37.579Z
Learning: In the `flow-evm-gateway` project, within the Go file `api/stream.go`, the `prepareBlockResponse` method includes the Bloom filter as the field `LogsBloom` in the returned `Block` struct.
Learnt from: peterargue
PR: onflow/flow-evm-gateway#615
File: services/traces/engine.go:190-190
Timestamp: 2024-10-17T18:04:41.062Z
Learning: In `services/traces/engine.go`, the `e.indexBlockTraces` function limits the number of goroutines to a maximum of 5 and blocks until all are complete, so during backfill, concurrency is already managed to prevent resource exhaustion.
services/ingestion/engine_test.go (3)
Learnt from: m-Peter
PR: onflow/flow-evm-gateway#831
File: tests/tx_batching_test.go:95-104
Timestamp: 2025-06-17T10:22:19.395Z
Learning: Go 1.22 introduced the ability to use `range` directly on integer values. The syntax `for i := range n` where `n` is an integer will iterate from 0 to n-1, equivalent to `for i := 0; i < n; i++`. This is valid Go syntax and should not be flagged as an error.
Learnt from: peterargue
PR: onflow/flow-evm-gateway#615
File: services/traces/engine.go:190-190
Timestamp: 2024-10-17T18:04:41.062Z
Learning: In `services/traces/engine.go`, the `e.indexBlockTraces` function limits the number of goroutines to a maximum of 5 and blocks until all are complete, so during backfill, concurrency is already managed to prevent resource exhaustion.
Learnt from: m-Peter
PR: onflow/flow-evm-gateway#831
File: services/requester/batch_tx_pool.go:0-0
Timestamp: 2025-06-19T11:36:25.478Z
Learning: In Go, when copying maps that contain slices (like `map[gethCommon.Address][]pooledEvmTx`), perform deep copies by iterating over the map and copying each slice individually using `make()` and `copy()` to avoid shared references that could lead to race conditions and data corruption.
services/ingestion/event_subscriber_test.go (3)
Learnt from: m-Peter
PR: onflow/flow-evm-gateway#831
File: tests/tx_batching_test.go:95-104
Timestamp: 2025-06-17T10:22:19.395Z
Learning: Go 1.22 introduced the ability to use `range` directly on integer values. The syntax `for i := range n` where `n` is an integer will iterate from 0 to n-1, equivalent to `for i := 0; i < n; i++`. This is valid Go syntax and should not be flagged as an error.
Learnt from: m-Peter
PR: onflow/flow-evm-gateway#831
File: services/requester/batch_tx_pool.go:0-0
Timestamp: 2025-06-19T11:36:25.478Z
Learning: In Go, when copying maps that contain slices (like `map[gethCommon.Address][]pooledEvmTx`), perform deep copies by iterating over the map and copying each slice individually using `make()` and `copy()` to avoid shared references that could lead to race conditions and data corruption.
Learnt from: peterargue
PR: onflow/flow-evm-gateway#615
File: bootstrap/bootstrap.go:167-197
Timestamp: 2024-10-17T18:04:04.165Z
Learning: In the `flow-evm-gateway` Go project, the validation ensuring that `startHeight` is less than or equal to `endHeight` is performed before the `StartTraceDownloader` method in `bootstrap/bootstrap.go`, so additional checks in this method are unnecessary.
models/events_test.go (3)
Learnt from: m-Peter
PR: onflow/flow-evm-gateway#831
File: tests/tx_batching_test.go:95-104
Timestamp: 2025-06-17T10:22:19.395Z
Learning: Go 1.22 introduced the ability to use `range` directly on integer values. The syntax `for i := range n` where `n` is an integer will iterate from 0 to n-1, equivalent to `for i := 0; i < n; i++`. This is valid Go syntax and should not be flagged as an error.
Learnt from: m-Peter
PR: onflow/flow-evm-gateway#831
File: services/requester/batch_tx_pool.go:0-0
Timestamp: 2025-06-19T11:36:25.478Z
Learning: In Go, when copying maps that contain slices (like `map[gethCommon.Address][]pooledEvmTx`), perform deep copies by iterating over the map and copying each slice individually using `make()` and `copy()` to avoid shared references that could lead to race conditions and data corruption.
Learnt from: peterargue
PR: onflow/flow-evm-gateway#617
File: api/stream.go:62-67
Timestamp: 2024-10-18T19:26:37.579Z
Learning: In the `flow-evm-gateway` project, within the Go file `api/stream.go`, the `prepareBlockResponse` method includes the Bloom filter as the field `LogsBloom` in the returned `Block` struct.
services/ingestion/event_subscriber.go (4)
Learnt from: peterargue
PR: onflow/flow-evm-gateway#615
File: bootstrap/bootstrap.go:167-197
Timestamp: 2024-10-17T18:04:04.165Z
Learning: In the `flow-evm-gateway` Go project, the validation ensuring that `startHeight` is less than or equal to `endHeight` is performed before the `StartTraceDownloader` method in `bootstrap/bootstrap.go`, so additional checks in this method are unnecessary.
Learnt from: peterargue
PR: onflow/flow-evm-gateway#682
File: services/ingestion/event_subscriber.go:85-90
Timestamp: 2025-01-24T20:15:10.908Z
Learning: When calculating blocks to catch up in Flow, initialize the counter to 0 and only set it when the latest chain height is greater than the current height (latestOnChainHeight > currentHeight). There's no need to handle the else case as 0 blocks to catch up is the correct default when we're at the same height or ahead.
Learnt from: peterargue
PR: onflow/flow-evm-gateway#615
File: services/traces/engine.go:175-178
Timestamp: 2024-10-17T18:05:27.084Z
Learning: In the trace backfill process, returning upon encountering an error when retrieving a block or its Cadence ID is intentional because future blocks are unlikely to exist if these blocks don't.
Learnt from: peterargue
PR: onflow/flow-evm-gateway#615
File: services/traces/engine.go:190-190
Timestamp: 2024-10-17T18:04:41.062Z
Learning: In `services/traces/engine.go`, the `e.indexBlockTraces` function limits the number of goroutines to a maximum of 5 and blocks until all are complete, so during backfill, concurrency is already managed to prevent resource exhaustion.
models/stream_test.go (1)
Learnt from: m-Peter
PR: onflow/flow-evm-gateway#831
File: tests/tx_batching_test.go:95-104
Timestamp: 2025-06-17T10:22:19.395Z
Learning: Go 1.22 introduced the ability to use `range` directly on integer values. The syntax `for i := range n` where `n` is an integer will iterate from 0 to n-1, equivalent to `for i := 0; i < n; i++`. This is valid Go syntax and should not be flagged as an error.
services/requester/requester.go (1)
Learnt from: peterargue
PR: onflow/flow-evm-gateway#615
File: bootstrap/bootstrap.go:167-197
Timestamp: 2024-10-17T18:04:04.165Z
Learning: In the `flow-evm-gateway` Go project, the validation ensuring that `startHeight` is less than or equal to `endHeight` is performed before the `StartTraceDownloader` method in `bootstrap/bootstrap.go`, so additional checks in this method are unnecessary.
🧬 Code Graph Analysis (1)
api/api.go (1)
eth/types/types.go (1)
  • MarshalReceipt (504-560)
⏰ 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: Test
  • GitHub Check: Lint
🔇 Additional comments (24)
storage/pebble/storage_test.go (1)

144-144: LGTM! Modern Go loop syntax.

The range-over-integer syntax is a clean modernization that maintains the same iteration behavior while improving readability.

storage/index_test.go (1)

154-154: LGTM! Consistent loop modernization.

All loop changes use Go 1.22's range-over-integer syntax correctly, improving code readability while maintaining identical iteration behavior across the test suite.

Also applies to: 178-178, 224-224, 430-430, 625-625, 673-673

bootstrap/create-multi-key-account.go (1)

84-84: LGTM! Valid Go 1.22+ range syntax.

Using range on integer variables is supported in Go 1.22+, as confirmed by the language specification. This modernization maintains the same iteration behavior while improving code readability.

models/stream_test.go (1)

79-79: LGTM! Clean test modernization.

The range-over-integer syntax simplifies the concurrent test loops while maintaining identical iteration behavior and test logic.

Also applies to: 83-83, 93-93, 102-102

services/ingestion/engine_test.go (1)

499-499: LGTM! Consistent modernization pattern.

The range-over-integer syntax with the eventCount variable is valid Go 1.22+ syntax and maintains the same iteration behavior for the transaction creation test logic.

services/ingestion/event_subscriber_test.go (2)

210-210: LGTM! Modern Go range syntax over slice.

The change to use for i := range txHashes is correct and more idiomatic than the traditional index-based loop.


355-355: LGTM! Modern Go 1.22+ range syntax over integer.

The change to use for i := range txCount is correct and takes advantage of Go 1.22's ability to range directly over integer values, which is equivalent to for i := 0; i < txCount; i++.

models/events_test.go (7)

72-72: LGTM! Modern Go range syntax over integer.

The change to use for i := range txCount is correct and leverages Go 1.22's modern range syntax.


249-249: LGTM! Modern Go range syntax over integer.

The change to use for i := range txCount is correct and more concise than the traditional index-based loop.


318-318: LGTM! Modern Go range syntax over integer.

The change to use for i := range txCount is correct and follows Go 1.22+ idioms.


341-341: LGTM! Modern Go range syntax over integer.

The change to use for i := range txCount is correct and improves code readability.


391-391: LGTM! Modern Go range syntax over integer.

The change to use for i := range txCount is correct and consistent with the modernization effort.


511-511: LGTM! Modern Go range syntax over integer.

The change to use for i := range txCount is correct and follows modern Go conventions.


538-538: LGTM! Modern Go range syntax over integer.

The change to use for i := range txCount is correct and completes the consistent modernization of loop syntax throughout the test file.

models/errors/errors.go (1)

95-95: LGTM: Modern Go type alias usage

Good modernization change replacing interface{} with any for improved readability. This aligns with Go 1.18+ best practices.

bootstrap/bootstrap.go (1)

534-534: LGTM: Consistent type modernization

Proper use of any type alias for gRPC interceptor parameters, maintaining interface compliance while improving readability.

services/ingestion/event_subscriber.go (1)

275-275: LGTM: Simplified boundary calculation

Excellent use of the min function to simplify the endHeight calculation. This is more concise and readable than the previous conditional approach while maintaining the same logic.

storage/mocks/mocks.go (1)

51-51: LGTM: Efficient byte slice creation

Good modernization using fmt.Appendf(nil, ...) which directly returns a byte slice, avoiding the intermediate string allocation from fmt.Sprintf().

Also applies to: 61-61

services/requester/requester.go (1)

403-407: LGTM: Streamlined binary search logic

Excellent simplification of the binary search midpoint calculation using min(). This maintains the same gas estimation logic while improving code clarity and consistency with other modernization changes in the codebase.

eth/types/types.go (2)

471-472: Idiomatic alias looks good

Replacing interface{} with the any alias is fully idiomatic since Go 1.18 and keeps the existing polymorphic semantics of the Transactions field unchanged. 👍


507-515: Consistent return-type modernisation

The function signature and composite literals were updated to map[string]any. All call-sites shown in this PR already compile against the new type, so no further action is required.

api/api.go (2)

137-160: Return-type switch is harmless

Syncing now returns any, matching the RPC expectation of false | object. All internal callers already treat the value opaquely, so behaviour is preserved.


325-350: Receipt path compiles after any switch

GetTransactionReceipt was adapted to the new map[string]any return type and all handleError sites were updated accordingly—good consistency.

api/server.go (1)

439-442: Struct field modernised correctly

Switching jsonError.Data to any mirrors the Go standard and aligns with other changes in the PR. No behavioural change introduced.

@m-Peter m-Peter merged commit d46ae32 into main Jul 14, 2025
2 checks passed
@m-Peter m-Peter deleted the mpeter/modernize-go-codebase branch July 14, 2025 09:01
@coderabbitai coderabbitai bot mentioned this pull request Nov 7, 2025
6 tasks
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.

4 participants